コード例 #1
0
    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
コード例 #2
0
    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)
コード例 #3
0
    def _create_device_item(self, devname, add_cat=False):
        ldevname = devname.lower()
        # get all cache keys pertaining to the device
        params = self.client.getDeviceParams(devname)
        if not params:
            return
        lowlevel_device = params.get('lowlevel') or False
        if lowlevel_device and not self._show_lowlevel:
            return
        if 'nicos.core.data.sink.DataSink' in params.get('classes', []) and \
           not self._show_lowlevel:
            return

        # remove still-existing previous item for the same device name
        if ldevname in self._devitems:
            self.on_client_device(('destroy', [devname]))

        cat = self._dev2setup.get(devname)
        if cat is None:  # device is not in any setup? reread setup info
            self._read_setup_info()
            cat = self._dev2setup.get(devname)
            if cat is None:  # still not there -> give up
                return

        if cat not in self._catitems:
            display_order = self._setupinfo[cat].get('display_order', 50)
            representative = self._setupinfo[cat].get('extended', {}).get(
                'representative', '').lower()
            catitem = SetupTreeWidgetItem(cat, display_order, representative)
            catitem.setToolTip(0, self._setupinfo[cat].get('description', ''))
            f = catitem.font(0)
            f.setBold(True)
            catitem.setFont(0, f)
            catitem.setIcon(0, self.groupIcon)
            self._catitems[cat] = catitem
            if add_cat:
                self.tree.addTopLevelItem(catitem)
                catitem.setExpanded(True)
        else:
            catitem = self._catitems[cat]

        # create a tree node for the device
        devitem = QTreeWidgetItem(catitem, [devname, '', ''], DEVICE_TYPE)

        devitem.setForeground(0, lowlevelBrush[lowlevel_device])
        devitem.setFont(0, lowlevelFont[lowlevel_device])

        if self.useicons:
            devitem.setIcon(0, self.statusIcon[OK])
        devitem.setToolTip(0, params.get('description', ''))
        self._devitems[ldevname] = devitem
        # fill the device info with dummy values, will be populated below
        self._devinfo[ldevname] = DevInfo(devname)

        # let the cache handler process all properties
        for key, value in iteritems(params):
            self.on_client_cache(
                (0, ldevname + '/' + key, OP_TELL, cache_dump(value)))
コード例 #4
0
    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
コード例 #5
0
    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)
コード例 #6
0
ファイル: instr_select.py プロジェクト: ess-dmsc/nicos
    def __init__(self, reason, parent=None):
        QDialog.__init__(self, parent)
        loadUi(self, 'dialogs/instr_select.ui')
        icon = QIcon(':/appicon-16')
        if reason:
            self.reasonLbl.setText(reason)
        else:
            self.reasonLbl.hide()
            self.saveBox.hide()

        self.confTree.itemDoubleClicked.connect(self.handleDoubleClick)
        for entry in sorted(os.listdir(config.nicos_root)):
            full = path.join(config.nicos_root, entry)
            if not (entry.startswith('nicos_') and path.isdir(full)):
                continue
            pkgitem = QTreeWidgetItem(self.confTree, [entry])
            pkgitem.setIcon(0, icon)
            for subentry in sorted(os.listdir(full)):
                configfile = path.join(full, subentry, 'guiconfig.py')
                if not path.isfile(configfile):
                    continue
                item = QTreeWidgetItem(pkgitem, [subentry])
                item.setData(0, QTreeWidgetItem.UserType, configfile)