Beispiel #1
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)
Beispiel #2
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