1 | # -*- coding: utf-8 -*- |
||
2 | """ |
||
3 | |||
4 | Qudi is free software: you can redistribute it and/or modify |
||
5 | it under the terms of the GNU General Public License as published by |
||
6 | the Free Software Foundation, either version 3 of the License, or |
||
7 | (at your option) any later version. |
||
8 | |||
9 | Qudi is distributed in the hope that it will be useful, |
||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | GNU General Public License for more details. |
||
13 | |||
14 | You should have received a copy of the GNU General Public License |
||
15 | along with Qudi. If not, see <http://www.gnu.org/licenses/>. |
||
16 | |||
17 | Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the |
||
18 | top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/> |
||
19 | """ |
||
20 | import os |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
21 | from qtpy import QtCore, QtWidgets |
||
0 ignored issues
–
show
|
|||
22 | |||
23 | class MenuItem: |
||
24 | |||
25 | def __init__(self, menu, children=None, actions=None): |
||
26 | if children is None: |
||
27 | children = {} |
||
28 | if actions is None: |
||
29 | actions = {} |
||
30 | self.menu = menu |
||
31 | self.children = children |
||
32 | self.actions = actions |
||
33 | |||
34 | class ModMenu(QtWidgets.QMenu): |
||
35 | """ This class represents the module selection menu. |
||
36 | """ |
||
37 | |||
38 | def __init__(self, modules): |
||
39 | """ Create new menu from module tree. |
||
40 | |||
41 | @param dict m: module tree |
||
42 | """ |
||
43 | super().__init__() |
||
44 | |||
45 | self.modules = [] |
||
46 | |||
47 | self.hwmenu = QtWidgets.QMenu('Hardware') |
||
48 | self.logicmenu = QtWidgets.QMenu('Logic') |
||
49 | self.guimenu = QtWidgets.QMenu('Gui') |
||
50 | self.addMenu(self.hwmenu) |
||
0 ignored issues
–
show
|
|||
51 | self.addMenu(self.logicmenu) |
||
0 ignored issues
–
show
|
|||
52 | self.addMenu(self.guimenu) |
||
0 ignored issues
–
show
|
|||
53 | |||
54 | self.hwmenuitems = MenuItem(self.hwmenu) |
||
55 | self.logicmenuitems = MenuItem(self.logicmenu) |
||
56 | self.guimenuitems = MenuItem(self.guimenu) |
||
57 | |||
58 | for module_path, module in sorted(modules['hardware'].items()): |
||
59 | self.build_submenu(self.hwmenuitems, module_path, module) |
||
60 | |||
61 | for module_path, module in sorted(modules['logic'].items()): |
||
62 | self.build_submenu(self.logicmenuitems, module_path, module) |
||
63 | |||
64 | for module_path, module in sorted(modules['gui'].items()): |
||
65 | self.build_submenu(self.guimenuitems, module_path, module) |
||
66 | |||
67 | def build_submenu(self, menu_item, modpath, module) : |
||
68 | """ Create a submenu from a module list, a module path and a module definition. |
||
69 | |||
70 | @param dict mlist: module list dict |
||
71 | @param str modpath: Qudi module path |
||
72 | @param dict moddef: module definition dict |
||
73 | """ |
||
74 | k_parts = modpath.split('.') |
||
75 | child_item = menu_item |
||
76 | if len(k_parts) > 3: |
||
77 | for part in k_parts[1:-2]: |
||
78 | if part in menu_item.children: |
||
79 | child_item = menu_item.children[part] |
||
80 | else: |
||
81 | new_menu = menu_item.menu.addMenu(part) |
||
82 | menu_item.children[part] = MenuItem(new_menu) |
||
83 | child_item = menu_item.children[part] |
||
84 | |||
85 | action = child_item.menu.addAction(k_parts[-2] + ' ' + k_parts[-1]) |
||
86 | child_item.actions[k_parts[-2] + ' ' + k_parts[-1]] = action |
||
87 | action.triggered.connect(module.addModule) |
||
88 | self.modules.append(module) |
||
89 | |||
90 | def hasModule(self, modpath): |
||
91 | """ Return whther module with given path is present |
||
92 | |||
93 | @param str modpath: Qudi module path |
||
94 | |||
95 | @return bool: wether a module has the given path |
||
96 | """ |
||
97 | return modpath in (x.path for x in self.modules) |
||
98 | |||
99 | def getModule(self, modpath): |
||
100 | """ Get module corresponding to module path. |
||
101 | |||
102 | @prarm str modpath: Qudi module path |
||
103 | |||
104 | @return ModuleObject: module object |
||
105 | """ |
||
106 | return next(x for x in self.modules if x.path == modpath) |
||
107 | |||
108 |