def newDeviceAddedSlot(self, deviceName, _classString): setupItem = self.getCurrentSetupItem() if setupItem.setup.abspath not in self.setupWidgets.keys(): self.loadSetup(setupItem.setup, setupItem.parent().text(0)) uncombinedModule = _classString.split('.') classname = uncombinedModule.pop() module = '.'.join(uncombinedModule) classes = inspect.getmembers(classparser.modules[module], predicate=inspect.isclass) _class = [_class[1] for _class in classes if _class[0] == classname][0] parameters = { key: '' for key in _class.parameters.keys() if _class.parameters[key].mandatory is True } device = setupcontroller.Device(deviceName, _classString, parameters=parameters) setupItem.setup.devices[deviceName] = device deviceWidget = DeviceWidget(self.setupWidgets[setupItem.setup.abspath]) deviceWidget.editedDevice.connect(self.editedSetupSlot) deviceWidget.loadDevice(device) self.workarea.addWidget(deviceWidget) self.deviceWidgets[setupItem.setup.abspath][deviceName] = deviceWidget deviceItem = QTreeWidgetItem([deviceName], ItemTypes.Device) deviceItem.setFlags(deviceItem.flags() & ~Qt.ItemIsDropEnabled) deviceItem.device = setupItem.setup.devices[deviceName] deviceItem.setIcon(0, QIcon(path.join(getResDir(), 'device.png'))) setupItem.insertChild(0, deviceItem) self.treeWidget.itemActivated.emit(deviceItem, 0) if not setupItem.setup.edited: setupItem.setText(0, '*' + setupItem.text(0)) setupItem.setup.edited = True
def loadNicosData(self): while self.topLevelItemCount() > 0: self.takeTopLevelItem(0) # list of topLevelItems representing the directories in # nicos_*: for example instruments, ... self.topLevelItems = [] for directory in setupcontroller.setup_directories: self.topLevelItems.append( QTreeWidgetItem([directory], ItemTypes.Directory)) self.addTopLevelItems(self.topLevelItems) # ask the controller to return the setups for current directory # add the setups as childitems for setup_directory in self.topLevelItems: # directories can neither be dragged nor have something dropped on setup_directory.setFlags(setup_directory.flags() & ~Qt.ItemIsDragEnabled & ~Qt.ItemIsDropEnabled) setup_directory.setIcon( 0, QIcon(path.join(getResDir(), 'folder.png'))) for setup in setupcontroller.setup_directories[ setup_directory.text(0)]: treeWidgetItem = QTreeWidgetItem([setup.name], ItemTypes.Setup) # scripts cannot be dragged, but they can have # something dropped on them (a device). treeWidgetItem.setFlags(treeWidgetItem.flags() & ~Qt.ItemIsDragEnabled) treeWidgetItem.setIcon( 0, QIcon(path.join(getResDir(), 'setup.png'))) treeWidgetItem.setup = setup setup_directory.addChild(treeWidgetItem) # setup for this directory has been loaded, add the devices. currentIndex = 0 while currentIndex < setup_directory.childCount(): currentItem = setup_directory.child(currentIndex) setup = currentItem.setup devices = setup.devices.keys() for device in devices: # read setup and add all the devices as child tree items deviceItem = QTreeWidgetItem([device], ItemTypes.Device) # devices can be dragged, but they can't have something # dropped on them. deviceItem.setFlags(deviceItem.flags() & ~Qt.ItemIsDropEnabled) deviceItem.device = setup.devices[device] currentItem.addChild(deviceItem) # icons for all devices deviceIndex = 0 while deviceIndex < currentItem.childCount(): currentItem.child(deviceIndex).setIcon( 0, QIcon(path.join(getResDir(), 'device.png'))) deviceIndex += 1 currentIndex += 1 self.setSortingEnabled(True) self.sortByColumn(0, Qt.AscendingOrder)
def _newSetup(self, instrument=None): dlg = NewSetupDialog([ item.text(0) for item in self.topLevelItems if item.type() == ItemTypes.Directory ], instrument) if dlg.exec_(): fileName = dlg.getValue() if not fileName: QMessageBox.warning(self, 'Error', 'No setup name entered.') return None, None if not fileName.endswith('.py'): fileName += '.py' if dlg.isSpecialSetup(): abspath = path.join(getNicosDir(), 'nicos_mlz', dlg.currentInstrument(), 'setups', 'special', fileName) else: abspath = path.join(getNicosDir(), 'nicos_mlz', dlg.currentInstrument(), 'setups', fileName) if abspath in [ i.abspath for i in setupcontroller.setup_directories[ dlg.currentInstrument()] ]: QMessageBox.warning(self, 'Error', 'Setup already exists!') return None, None try: open(abspath, 'w', encoding='utf-8').close() except OSError: QMessageBox.warning(self, 'Error', 'Could not create new ' 'setup!') return None, None setupcontroller.addSetup(dlg.currentInstrument(), abspath) newSetup = None for setup in setupcontroller.setup_directories[ dlg.currentInstrument()]: if setup.abspath == abspath: newSetup = setup treeWidgetItem = QTreeWidgetItem(['*' + newSetup.name], ItemTypes.Setup) treeWidgetItem.setFlags(treeWidgetItem.flags() & ~Qt.ItemIsDragEnabled) treeWidgetItem.setIcon(0, QIcon(path.join(getResDir(), 'setup.png'))) treeWidgetItem.setup = newSetup treeWidgetItem.setup.edited = True return treeWidgetItem, dlg.currentInstrument() return None, None
def dropEvent(self, event): """ To satisfy Qt, a call to TreeWidgetContextMenu.dropEvent is neccessary. Else, the animation would look really gross and suggest the object might actually not have been copied. But after the call, the newly appended child item seems to be completely useless: It carries neither data nor does it emit the treeWidget's itemActivated signal. This may be a bug or my inability to find what's wrong. Because of that, I need to construct my own item, find the old one and replace it with my new one. """ target = self.getSetupOfDropPos(event.pos()) if self.dragItem.device.name in target.setup.devices.keys(): QMessageBox.warning( self, 'Error', 'The target setup already ' 'contains a device with that name!') self.dragItem = None event.ignore() return count = 0 previousItems = [] while count < target.childCount(): previousItems.append(target.child(count)) count += 1 TreeWidgetContextMenu.dropEvent(self, event) count = 0 afterDropItems = [] while count < target.childCount(): afterDropItems.append(target.child(count)) count += 1 newItem = None for child in afterDropItems: if child not in previousItems: newItem = child index = target.indexOfChild(newItem) target.takeChild(index) deviceName = self.dragItem.device.name target.setup.devices[deviceName] = deepcopy(self.dragItem.device) deviceItem = QTreeWidgetItem([deviceName], ItemTypes.Device) deviceItem.setFlags(deviceItem.flags() & ~Qt.ItemIsDropEnabled) deviceItem.device = target.setup.devices[deviceName] deviceItem.setIcon(0, QIcon(path.join(getResDir(), 'device.png'))) target.insertChild(index, deviceItem) self.deviceAdded.emit(target, deviceName) self.itemActivated.emit(deviceItem, 0)