| Total Complexity | 60 |
| Total Lines | 281 |
| Duplicated Lines | 3.2 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ConfigMainWindow often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 54 | class ConfigMainWindow(QtWidgets.QMainWindow): |
||
| 55 | """ This class represents the Manager Window. |
||
| 56 | """ |
||
| 57 | def __init__(self, loadfile=None): |
||
| 58 | """ Create the Manager Window. |
||
| 59 | """ |
||
| 60 | # Get the path to the *.ui file |
||
| 61 | this_dir = os.path.dirname(__file__) |
||
| 62 | ui_file = os.path.join(this_dir, 'ui_config_window.ui') |
||
| 63 | self.log = logging.getLogger(__name__) |
||
| 64 | |||
| 65 | # Load it |
||
| 66 | super().__init__() |
||
| 67 | uic.loadUi(ui_file, self) |
||
| 68 | |||
| 69 | self.modnodes = dict() |
||
| 70 | self.globalsection = OrderedDict() |
||
| 71 | self.currentFile = '' |
||
| 72 | |||
| 73 | # palette |
||
| 74 | self.colors = { |
||
| 75 | 'hardware': palette.c2, |
||
| 76 | 'logic': palette.c1, |
||
| 77 | 'gui': palette.c4, |
||
| 78 | '': palette.c3 |
||
| 79 | } |
||
| 80 | |||
| 81 | # init |
||
| 82 | self.setupUi() |
||
| 83 | self.show() |
||
| 84 | |||
| 85 | if loadfile is not None: |
||
| 86 | self.loadConfigFile(loadfile) |
||
| 87 | |||
| 88 | def setupUi(self): |
||
| 89 | View Code Duplication | self.actionNew_configuration.triggered.connect(self.newConfigFile) |
|
| 90 | self.actionSave_configuration.triggered.connect(self.saveConfigFile) |
||
| 91 | self.actionSave_configuration_as.triggered.connect(self.saveConfigFileAs) |
||
| 92 | self.actionOpen_configuration.triggered.connect(self.openConfigFile) |
||
| 93 | self.actionDelete_selected_nodes.triggered.connect(self.graphView.deleteSelectedNodes) |
||
| 94 | self.actionFrame_selected_nodes.triggered.connect(self.graphView.frameSelectedNodes) |
||
| 95 | self.actionFrame_all_nodes.triggered.connect(self.graphView.frameAllNodes) |
||
| 96 | |||
| 97 | # add module menu |
||
| 98 | self.findModules() |
||
| 99 | self.mmroot = ModMenu(self.m) |
||
| 100 | for mod in self.mmroot.modules: |
||
| 101 | mod.sigAddModule.connect(self.addModule) |
||
| 102 | self.actionAdd_Module.setMenu(self.mmroot) |
||
| 103 | |||
| 104 | # node change signals |
||
| 105 | self.graphView.nodeAdded.connect(self.nodeAdded) |
||
| 106 | self.graphView.nodeRemoved.connect(self.nodeRemoved) |
||
| 107 | self.graphView.nodeNameChanged.connect(self.nodeNameChanged) |
||
| 108 | self.graphView.selectionChanged.connect(self.selectedNodesChanged) |
||
| 109 | |||
| 110 | def findModules(self): |
||
| 111 | modules = listmods.find_pyfiles(os.getcwd()) |
||
| 112 | m, i_s, ie, oe = listmods.check_qudi_modules(modules) |
||
| 113 | self.m = m |
||
| 114 | |||
| 115 | if len(oe) > 0 or len(ie) > 0: |
||
| 116 | print('\n========== ERRORS: ===========', file=sys.stderr) |
||
| 117 | for e in oe: |
||
| 118 | print(e[0], file=sys.stderr) |
||
| 119 | print(e[1], file=sys.stderr) |
||
| 120 | |||
| 121 | for e in ie: |
||
| 122 | print(e[0], file=sys.stderr) |
||
| 123 | print(e[1], file=sys.stderr) |
||
| 124 | # print(self.m) |
||
| 125 | |||
| 126 | def addModule(self, module, name=None, pos=(0,0)): |
||
| 127 | """ Add a module to the GraphView |
||
| 128 | """ |
||
| 129 | |||
| 130 | # sort out the module name |
||
| 131 | if name is None: |
||
| 132 | name = 'new_module' |
||
| 133 | n = 1 |
||
| 134 | if self.graphView.hasNode(name): |
||
| 135 | while self.graphView.hasNode('{}{}'.format(name, n)): |
||
| 136 | n += 1 |
||
| 137 | name = '{}{}'.format(name, n) |
||
| 138 | |||
| 139 | # chart view |
||
| 140 | g = self.graphView |
||
| 141 | |||
| 142 | # new node in chart |
||
| 143 | node = Node(g, name) |
||
| 144 | |||
| 145 | # coloring |
||
| 146 | node.setColor(self.colors[module.base]) |
||
| 147 | |||
| 148 | # check where the module belongs and what it can connect to |
||
| 149 | for cname, conn in module.connections.items(): |
||
| 150 | port_type = QudiPortType('in', module.base, [conn.interface]) |
||
| 151 | node.addPort(InputPort(node, g, conn.name, palette.c3, port_type)) |
||
| 152 | |||
| 153 | if module.base != 'gui': |
||
| 154 | port_type = QudiPortType('out', module.base, module.interfaces) |
||
| 155 | node.addPort(OutputPort(node, g, 'out', palette.c3, port_type)) |
||
| 156 | |||
| 157 | # set position in view |
||
| 158 | node.setGraphPos(QtCore.QPointF(pos[0], pos[1])) |
||
| 159 | |||
| 160 | # save the module instance and node relatonship |
||
| 161 | self.modnodes[name] = ModNode(module, node) |
||
| 162 | # add node to view |
||
| 163 | g.addNode(node) |
||
| 164 | |||
| 165 | def openConfigFile(self): |
||
| 166 | defaultconfigpath = '' |
||
| 167 | filename = QtWidgets.QFileDialog.getOpenFileName( |
||
| 168 | self, |
||
| 169 | 'Load Configration', |
||
| 170 | defaultconfigpath , |
||
| 171 | 'Configuration files (*.cfg)')[0] |
||
| 172 | if len(filename) > 0: |
||
| 173 | print('Open:', filename) |
||
| 174 | self.loadConfigFile(filename) |
||
| 175 | |||
| 176 | def loadConfigFile(self, filename): |
||
| 177 | config = core.config.load(filename) |
||
| 178 | self.configToNodes(config) |
||
| 179 | self.currentFile = filename |
||
| 180 | self.graphView.frameAllNodes() |
||
| 181 | |||
| 182 | def newConfigFile(self): |
||
| 183 | self.graphView.reset() |
||
| 184 | self.configFileName = 'New configuration' |
||
| 185 | self.updateWindowTitle(self.configFileName, extra='*') |
||
| 186 | |||
| 187 | def saveConfigFile(self): |
||
| 188 | if os.path.isfile(self.currentFile): |
||
| 189 | config = self.nodesToConfig() |
||
| 190 | core.config.save(self.currentFile, config) |
||
| 191 | else: |
||
| 192 | self.saveConfigFileAs() |
||
| 193 | |||
| 194 | def saveConfigFileAs(self): |
||
| 195 | defaultconfigpath = os.path.dirname(self.currentFile) |
||
| 196 | filename = QtWidgets.QFileDialog.getSaveFileName( |
||
| 197 | self, |
||
| 198 | 'Save Configration As', |
||
| 199 | defaultconfigpath , |
||
| 200 | 'Configuration files (*.cfg)')[0] |
||
| 201 | if len(filename) > 0: |
||
| 202 | print('Save:', filename) |
||
| 203 | config = self.nodesToConfig() |
||
| 204 | core.config.save(filename, config) |
||
| 205 | self.currentFile = filename |
||
| 206 | |||
| 207 | def updateWindowTitle(self, filename, extra=''): |
||
| 208 | self.setWindowTitle('{}{} - Qudi configuration editor'.format(filename, extra)) |
||
| 209 | |||
| 210 | def configToNodes(self, config): |
||
| 211 | self.addNodes(config) |
||
| 212 | self.connectNodes(config) |
||
| 213 | |||
| 214 | def addNodes(self, config): |
||
| 215 | pos = [0, 0] |
||
| 216 | for base, conf_modules in config.items(): |
||
| 217 | if base not in ['hardware', 'logic', 'gui']: |
||
| 218 | continue |
||
| 219 | for mod_conf_name, mod_conf_values in conf_modules.items(): |
||
| 220 | mc = 'module.Class' |
||
| 221 | #print(base, mod_conf_name, mod_conf_values) |
||
| 222 | if mc in mod_conf_values and self.mmroot.hasModule(base + '.' + mod_conf_values[mc]): |
||
| 223 | mod = self.mmroot.getModule(base + '.' + mod_conf_values[mc]) |
||
| 224 | self.addModule(mod, mod_conf_name, pos) |
||
| 225 | pos[1] += 100 |
||
| 226 | pos[0] += 600 |
||
| 227 | pos[1] = 0 |
||
| 228 | |||
| 229 | def connectNodes(self, config): |
||
| 230 | for base, conf_modules in config.items(): |
||
| 231 | if base not in ['hardware', 'logic', 'gui']: |
||
| 232 | continue |
||
| 233 | for conf_mod_name, conf_mod_values in conf_modules.items(): |
||
| 234 | if 'connect' in conf_mod_values: |
||
| 235 | dst_mod_name = conf_mod_name |
||
| 236 | for conn_in, conn_out in conf_mod_values['connect'].items(): |
||
| 237 | src_mod_name = conn_out |
||
| 238 | if conf_mod_name not in self.modnodes: |
||
| 239 | self.log.error( |
||
| 240 | 'Target module {} not present while connecting {} to {}' |
||
| 241 | ''.format(dst_mod_name, conn_in, src_mod_name)) |
||
| 242 | continue |
||
| 243 | conn_names_dst = [c.name for cn, c in self.modnodes[dst_mod_name].module.connections.items()] |
||
| 244 | if conn_in not in conn_names_dst: |
||
| 245 | self.log.error( |
||
| 246 | 'Target connector {} not present while connecting {} to {}.{}' |
||
| 247 | ''.format(conn_in, src_mod_name, dst_mod_name, conn_in)) |
||
| 248 | continue |
||
| 249 | if src_mod_name not in self.modnodes: |
||
| 250 | self.log.error( |
||
| 251 | 'Source module {} not present while connecting it to {}.{}' |
||
| 252 | ''.format(src_mod_name, dst_mod_name, conn_in)) |
||
| 253 | continue |
||
| 254 | |||
| 255 | try: |
||
| 256 | self.graphView.connectPorts( |
||
| 257 | self.modnodes[src_mod_name].node, |
||
| 258 | 'out', |
||
| 259 | self.modnodes[dst_mod_name].node, |
||
| 260 | conn_in) |
||
| 261 | except: |
||
| 262 | self.log.exception( |
||
| 263 | 'pyflowgraph failed while connecting {} to {}.{}' |
||
| 264 | ''.format(src_mod_name, dst_mod_name, conn_in)) |
||
| 265 | |||
| 266 | self.globalsection = config['global'] |
||
| 267 | |||
| 268 | def nodesToConfig(self): |
||
| 269 | """ Convert nodes into OrderedDict for saving. |
||
| 270 | """ |
||
| 271 | config = OrderedDict() |
||
| 272 | config['global'] = OrderedDict() |
||
| 273 | config['hardware'] = OrderedDict() |
||
| 274 | config['logic'] = OrderedDict() |
||
| 275 | config['gui'] = OrderedDict() |
||
| 276 | |||
| 277 | for key, value in self.globalsection.items(): |
||
| 278 | config['global'][key] = value |
||
| 279 | |||
| 280 | for mod_name, mod in self.modnodes.items(): |
||
| 281 | entry = OrderedDict() |
||
| 282 | path = mod.module.path.split('.') |
||
| 283 | |||
| 284 | if len(path) > 1 and path[0] in ('hardware', 'logic', 'gui'): |
||
| 285 | config[path[0]][mod_name] = entry |
||
| 286 | entry['module.Class'] = '.'.join(path[1:]) |
||
| 287 | |||
| 288 | portin = (mod.node.getPort(x[0]) for x in mod.module.conn) |
||
| 289 | conndict = OrderedDict() |
||
| 290 | for port in portin: |
||
| 291 | conns = port.inCircle().getConnections() |
||
| 292 | if len(conns) == 1: |
||
| 293 | c = tuple(conns)[0] |
||
| 294 | src = c.getSrcPort() |
||
| 295 | node = src.getNode() |
||
| 296 | conndict[port.getName()] = '{}.{}'.format(node.getName(), src.getName()) |
||
| 297 | if len(conndict) > 0: |
||
| 298 | entry['connect'] = conndict |
||
| 299 | # FIXME: rest of the configuration |
||
| 300 | print(entry) |
||
| 301 | return config |
||
| 302 | |||
| 303 | def nodeAdded(self, node): |
||
| 304 | pass |
||
| 305 | |||
| 306 | def nodeRemoved(self, node): |
||
| 307 | pass |
||
| 308 | |||
| 309 | def nodeNameChanged(self, oldName, newName): |
||
| 310 | if oldName == newName: |
||
| 311 | return |
||
| 312 | |||
| 313 | modnode = self.modnodes.pop(oldName) |
||
| 314 | if newName in self.modnodes: |
||
| 315 | newName = newName + '2' |
||
| 316 | self.modnodes[newName] = modnode |
||
| 317 | |||
| 318 | @QtCore.Slot(list, list) |
||
| 319 | def selectedNodesChanged(self, oldSelection, newSelection): |
||
| 320 | if len(newSelection) > 0: |
||
| 321 | self.setConfigEditor(newSelection[0]) |
||
| 322 | |||
| 323 | def setConfigEditor(self, node): |
||
| 324 | name = node.getName() |
||
| 325 | modnode = self.modnodes[name] |
||
| 326 | try: |
||
| 327 | self.nameLineEdit.textEdited.disconnect() |
||
| 328 | except: |
||
| 329 | pass |
||
| 330 | self.nameLineEdit.setText(name) |
||
| 331 | self.nameLineEdit.textEdited.connect(modnode.node.setName) |
||
| 332 | self.mcLabel.setText(modnode.module.path) |
||
| 333 | self.tableView.setModel(modnode.cfgmodel) |
||
| 334 | self.tableView.resizeColumnsToContents() |
||
| 335 | |||
| 346 |