def __init__(self): super(PersistendIndexProve, self).__init__() self.uiw = loadUi("/u/isaito/data/Dropbox/ROS/groovy_quantal/catkin_ws/src/rqt_prove/resource/treeview.ui") self._std_model = QStandardItemModel() self._rootitem = self._std_model.invisibleRootItem() item_r_1 = QStandardItem("r1") item_r_2 = QStandardItem("r2") print 'index before {}'.format(item_r_1.index()) qindex_persistent = QPersistentModelIndex(item_r_1.index()) self._std_model.beginInsertRows(self._rootitem.index(), 0, 0) self._rootitem.appendRow(item_r_1) self._rootitem.appendRow(item_r_2) self._std_model.endInsertRows() print 'index after {}'.format(item_r_1.index()) print 'index after 2 {}'.format(self._std_model.index(0, 0, QModelIndex())) print '_rootitem index={} persistent list={}'.format( self._rootitem.index(), self._std_model.persistentIndexList()) self.uiw._treeview.setModel(self._std_model) self.selectionModel = self.uiw._treeview.selectionModel() self.selectionModel.selectionChanged.connect( self._selection_changed_slot) self.selectionModel.currentChanged.connect( self._current_changed_slot) print('del/sel?\tde/sel index\tde/sel.row\tde/sel.dat\tparent\tinternal id') self.uiw.show()
def _add_widget(self, n): item_toplevel = QStandardItem('{}th item'.format(n)) self._datamodel.setItem(n, 0, item_toplevel) widget_toplevel = QPushButton('{}th button'.format(n)) qindex_toplevel = self._datamodel.index(n, 1, QModelIndex()) self.setIndexWidget(qindex_toplevel, widget_toplevel) if n == 2: item_child_col0 = QStandardItem('child col0') item_child_col1 = QStandardItem('child col1') #item_toplevel.appendRow(item_child_col0) item_toplevel.insertRow(0, [item_child_col0, item_child_col1]) widget_child = QPushButton('child widget') qindex_child = item_child_col1.index() self.setIndexWidget(qindex_child, widget_child)
def _add_widget(self, n): item_toplevel = QStandardItem('{}th item'.format(n)) self._datamodel.setItem(n, 0, item_toplevel) widget_toplevel = QPushButton('{}th button'.format(n)) qindex_toplevel = self._datamodel.index(n, 1, QModelIndex()) self.setIndexWidget(qindex_toplevel, widget_toplevel) if n == 2: item_child_col0 = QStandardItem('child col0') item_child_col1 = QStandardItem('child col1') #item_toplevel.appendRow(item_child_col0) item_child_col2 = QStandardItem('child col2') item_toplevel.insertRow(0, [item_child_col0, item_child_col1]) #item_child_col0.insertColumn(0, [item_child_col1, item_child_col2]) #item_child_col0.appendColumn([item_child_col1]) # appends another child widget_child = QPushButton('child widget') #qindex_child = self._datamodel.index(n, 1, QModelIndex()) qindex_child = item_child_col1.index() #qindex_child = item_toplevel.index(0, 1, QModelIndex()) self.setIndexWidget(qindex_child, widget_child)
def fillTree(self): self.items = {} self.model.clear() self.model.setHorizontalHeaderLabels([self.tr('Setting'), self.tr('Value')]) text = unicode(self.searchBox.text()) settings = ProcessingConfig.getSettings() rootItem = self.model.invisibleRootItem() priorityKeys = [self.tr('General'), self.tr('Models'), self.tr('Scripts')] for group in priorityKeys: groupItem = QStandardItem(group) icon = ProcessingConfig.getGroupIcon(group) groupItem.setIcon(icon) groupItem.setEditable(False) emptyItem = QStandardItem() emptyItem.setEditable(False) rootItem.insertRow(0, [groupItem, emptyItem]) for setting in settings[group]: if setting.hidden: continue if text == '' or text.lower() in setting.description.lower(): labelItem = QStandardItem(setting.description) labelItem.setIcon(icon) labelItem.setEditable(False) self.items[setting] = SettingItem(setting) groupItem.insertRow(0, [labelItem, self.items[setting]]) if text != '': self.tree.expand(groupItem.index()) providersItem = QStandardItem(self.tr('Providers')) icon = QIcon(os.path.join(pluginPath, 'images', 'alg.png')) providersItem.setIcon(icon) providersItem.setEditable(False) emptyItem = QStandardItem() emptyItem.setEditable(False) rootItem.insertRow(0, [providersItem, emptyItem]) for group in settings.keys(): if group in priorityKeys: continue groupItem = QStandardItem(group) icon = ProcessingConfig.getGroupIcon(group) groupItem.setIcon(icon) groupItem.setEditable(False) for setting in settings[group]: if setting.hidden: continue if text == '' or text.lower() in setting.description.lower(): labelItem = QStandardItem(setting.description) labelItem.setIcon(icon) labelItem.setEditable(False) self.items[setting] = SettingItem(setting) groupItem.insertRow(0, [labelItem, self.items[setting]]) emptyItem = QStandardItem() emptyItem.setEditable(False) providersItem.appendRow([groupItem, emptyItem]) self.tree.sortByColumn(0, Qt.AscendingOrder) self.adjustColumns()
def _onAddAction(self): item = QStandardItem("") item.setFlags(item.flags() ^ Qt.ItemIsDropEnabled) self._listView.model().appendRow(item) self._listView.setCurrentIndex(item.index()) self._listView.edit(item.index())
class SubPowerWidget(QWidget): """ @brief Zeigt alle Unterkräfte in einer Baumstruktur an. """ def __init__(self, template, character, parent=None): super(SubPowerWidget, self).__init__(parent) self.__storage = template self.__character = character self.__model = QStandardItemModel() # Das ungenutzte Model dient dazu, alle Unterkräfte aufzunehmen, die ich nicht darstellen möchte. Ist einfacher, als diese im View zu verstecken. self.__modelUnused = QStandardItemModel() self._layout = QVBoxLayout() self.setLayout(self._layout) self.__view = QTreeView() self.__view.setHeaderHidden(True) self.__view.setModel(self.__model) self._layout.addWidget(self.__view) self._typ = "Subpower" categories = self.__storage.categories(self._typ) self.__items = {} self.__rootItem = QStandardItem() self.__rootItem = self.__model.invisibleRootItem() self.__rootItemUnused = QStandardItem() self.__rootItemUnused = self.__modelUnused.invisibleRootItem() for item in categories: categoryItem = QStandardItem(item) self.__rootItem.appendRow(categoryItem) ## Ich benötige diese Items auch im ungenutzten Model. categoryItemUnused = QStandardItem(item) self.__rootItemUnused.appendRow(categoryItemUnused) traitList = list(self.__character.traits[self._typ][item].items()) traitList.sort() for trait in traitList: traitItem = QStandardItem(trait[1].name) traitItem.setCheckable(True) ## Unhashable Type self.__items[trait[1]] = traitItem categoryItem.appendRow(traitItem) ## Funktioniert mit PySide nicht: #trait[1].availableChanged.connect(traitItem.setEnabled) ## Funktioniert auch mit PySide: trait[1].availableChanged.connect( lambda enable, item=traitItem: item.setEnabled(enable)) trait[1].valueChanged.connect(lambda val, trait=trait[ 1], item=traitItem: self.__setItemValue(trait, item)) self.__model.itemChanged.connect(self.__getItemValue) self.__character.speciesChanged.connect(self.hideOrShowToolPage) self.__character.breedChanged.connect(self.hideOrShowToolPage) self.__character.factionChanged.connect(self.hideOrShowToolPage) def __setItemValue(self, trait, item): """ Setzt den Wert der Angezeigten Items. """ if trait.value == 0: item.setCheckState(Qt.Unchecked) elif trait.value == 1: item.setCheckState(Qt.PartiallyChecked) else: item.setCheckState(Qt.Checked) def __getItemValue(self, item): """ Setzt den Wert der Eigenschaft im Speicher. """ for trait in self.__items.items(): if id(trait[1]) == id(item): trait[0].value = item.checkState() break def hideOrShowToolPage(self, res): """ Alle Eigenschaften, die nicht zur Verfügung stehen, werden verborgen, indem sie in ein anderes Model verschoben werden. \bug Möglicher Fehler in PySide: Der Aufruf von QStandardItem.model() führt beim Beenden des Programms zu einem Segmentation Fault. """ # Versteckt alle Unterkräfte, die zu gewähltem Charakter nicht passen. for item in self.__items.items(): if ((item[0].species and item[0].species != self.__character.species) or (item[0].only and self.__character.breed not in item[0].only and self.__character.faction not in item[0].only)): #self.__view.setRowHidden(item[1].index().row(), item[1].parent().index(), True) #Debug.debug(item[1].model()) ## Hier wird beispielsweise besagter Aufruf getätigt, der zu einem segfault führt. if item[1].model() == self.__model: parent = item[1].parent() itemUnused = parent.takeRow(item[1].index().row()) parentUnused = self.__modelUnused.findItems( parent.text())[0] parentUnused.appendRow(itemUnused) else: #self.__view.setRowHidden(item[1].index().row(), item[1].parent().index(), False) if item[1].model() == self.__modelUnused: parent = item[1].parent() itemUsed = parent.takeRow(item[1].index().row()) parentUsed = self.__model.findItems(parent.text())[0] parentUsed.appendRow(itemUsed) ## Versteckt alle Elternzeilen, wenn sie keine Kinder enthalten. for i in range(self.__model.rowCount()): categoryItem = self.__model.item(i) if categoryItem.hasChildren(): self.__view.setRowHidden(categoryItem.index().row(), self.__rootItem.index(), False) else: self.__view.setRowHidden(categoryItem.index().row(), self.__rootItem.index(), True)
def add_device_to_tree(self, device): # check if device is already added if len(device['uid']) > 0: for row in range(self.model_devices.rowCount()): existing_name = self.model_devices.item(row, 0).text() exisitng_uid = self.tree_devices.indexWidget( self.model_devices.item(row, 1).index()).text() if device['name'] == existing_name and device[ 'uid'] == exisitng_uid: EventLogger.info( 'Ignoring duplicate device "{0}" with UID "{1}"'. format(device['name'], device['uid'])) return # add device name_item = QStandardItem(device['name']) uid_item = QStandardItem('') self.model_devices.appendRow([name_item, uid_item]) edit_uid = QLineEdit() edit_uid.setPlaceholderText('Enter UID') edit_uid.setValidator( QRegExpValidator(QRegExp( '^[{0}]{{1,6}}$'.format(BASE58)))) # FIXME: use stricter logic edit_uid.setText(device['uid']) self.tree_devices.setIndexWidget(uid_item.index(), edit_uid) value_specs = device_specs[device['name']]['values'] parent_item = QStandardItem('Values') name_item.appendRow([parent_item, QStandardItem('')]) self.tree_devices.expand(parent_item.index()) # add values for value_spec in value_specs: value_name_item = QStandardItem(value_spec['name']) value_interval_item = QStandardItem('') parent_item.appendRow([value_name_item, value_interval_item]) spinbox_interval = QSpinBox() spinbox_interval.setRange(0, (1 << 31) - 1) spinbox_interval.setSingleStep(1) spinbox_interval.setValue( device['values'][value_spec['name']]['interval']) spinbox_interval.setSuffix(' seconds') self.tree_devices.setIndexWidget(value_interval_item.index(), spinbox_interval) if value_spec['subvalues'] != None: for subvalue_name in value_spec['subvalues']: subvalue_name_item = QStandardItem(subvalue_name) subvalue_check_item = QStandardItem('') value_name_item.appendRow( [subvalue_name_item, subvalue_check_item]) check_subvalue = QCheckBox() check_subvalue.setChecked(device['values'][ value_spec['name']]['subvalues'][subvalue_name]) self.tree_devices.setIndexWidget( subvalue_check_item.index(), check_subvalue) self.tree_devices.expand(name_item.index()) # add options option_specs = device_specs[device['name']]['options'] if option_specs != None: parent_item = QStandardItem('Options') name_item.appendRow([parent_item, QStandardItem('')]) for option_spec in option_specs: option_name_item = QStandardItem(option_spec['name']) option_widget_item = QStandardItem('') parent_item.appendRow([option_name_item, option_widget_item]) if option_spec['type'] == 'choice': widget_option_value = QComboBox() for option_value_spec in option_spec['values']: widget_option_value.addItem( option_value_spec[0].decode('utf-8'), option_value_spec[1]) widget_option_value.setCurrentIndex( widget_option_value.findText(device['options'][ option_spec['name']]['value'].decode('utf-8'))) elif option_spec['type'] == 'int': widget_option_value = QSpinBox() widget_option_value.setRange(option_spec['minimum'], option_spec['maximum']) widget_option_value.setSuffix(option_spec['suffix']) widget_option_value.setValue( device['options'][option_spec['name']]['value']) elif option_spec['type'] == 'bool': widget_option_value = QCheckBox() widget_option_value.setChecked( device['options'][option_spec['name']]['value']) self.tree_devices.setIndexWidget(option_widget_item.index(), widget_option_value)
def add_device_to_tree(self, device): # check if device is already added if len(device['uid']) > 0: for row in range(self.model_devices.rowCount()): existing_name = self.model_devices.item(row, 0).text() exisitng_uid = self.tree_devices.indexWidget(self.model_devices.item(row, 1).index()).text() if device['name'] == existing_name and device['uid'] == exisitng_uid: EventLogger.info('Ignoring duplicate device "{0}" with UID "{1}"' .format(device['name'], device['uid'])) return # add device name_item = QStandardItem(device['name']) uid_item = QStandardItem('') self.model_devices.appendRow([name_item, uid_item]) edit_uid = QLineEdit() edit_uid.setPlaceholderText('Enter UID') edit_uid.setValidator(QRegExpValidator(QRegExp('^[{0}]{{1,6}}$'.format(BASE58)))) # FIXME: use stricter logic edit_uid.setText(device['uid']) self.tree_devices.setIndexWidget(uid_item.index(), edit_uid) value_specs = device_specs[device['name']]['values'] parent_item = QStandardItem('Values') name_item.appendRow([parent_item, QStandardItem('')]) self.tree_devices.expand(parent_item.index()) # add values for value_spec in value_specs: value_name_item = QStandardItem(value_spec['name']) value_interval_item = QStandardItem('') parent_item.appendRow([value_name_item, value_interval_item]) spinbox_interval = QSpinBox() spinbox_interval.setRange(0, (1 << 31) - 1) spinbox_interval.setSingleStep(1) spinbox_interval.setValue(device['values'][value_spec['name']]['interval']) spinbox_interval.setSuffix(' seconds') self.tree_devices.setIndexWidget(value_interval_item.index(), spinbox_interval) if value_spec['subvalues'] != None: for subvalue_name in value_spec['subvalues']: subvalue_name_item = QStandardItem(subvalue_name) subvalue_check_item = QStandardItem('') value_name_item.appendRow([subvalue_name_item, subvalue_check_item]) check_subvalue = QCheckBox() check_subvalue.setChecked(device['values'][value_spec['name']]['subvalues'][subvalue_name]) self.tree_devices.setIndexWidget(subvalue_check_item.index(), check_subvalue) self.tree_devices.expand(name_item.index()) # add options option_specs = device_specs[device['name']]['options'] if option_specs != None: parent_item = QStandardItem('Options') name_item.appendRow([parent_item, QStandardItem('')]) for option_spec in option_specs: option_name_item = QStandardItem(option_spec['name']) option_widget_item = QStandardItem('') parent_item.appendRow([option_name_item, option_widget_item]) if option_spec['type'] == 'choice': widget_option_value = QComboBox() for option_value_spec in option_spec['values']: widget_option_value.addItem(option_value_spec[0].decode('utf-8'), option_value_spec[1]) widget_option_value.setCurrentIndex(widget_option_value.findText(device['options'][option_spec['name']]['value'].decode('utf-8'))) elif option_spec['type'] == 'int': widget_option_value = QSpinBox() widget_option_value.setRange(option_spec['minimum'], option_spec['maximum']) widget_option_value.setSuffix(option_spec['suffix']) widget_option_value.setValue(device['options'][option_spec['name']]['value']) elif option_spec['type'] == 'bool': widget_option_value = QCheckBox() widget_option_value.setChecked(device['options'][option_spec['name']]['value']) self.tree_devices.setIndexWidget(option_widget_item.index(), widget_option_value)
def fillTree(self): self.items = {} self.model.clear() self.model.setHorizontalHeaderLabels([self.tr('Setting'), self.tr('Value')]) text = unicode(self.searchBox.text()) settings = ProcessingConfig.getSettings() rootItem = self.model.invisibleRootItem() priorityKeys = [self.tr('General'), self.tr('Models'), self.tr('Scripts')] for group in priorityKeys: groupItem = QStandardItem(group) icon = ProcessingConfig.getGroupIcon(group) groupItem.setIcon(icon) groupItem.setEditable(False) emptyItem = QStandardItem() emptyItem.setEditable(False) rootItem.insertRow(0, [groupItem, emptyItem]) for setting in settings[group]: if setting.hidden or setting.name.startswith("MENU_"): continue if text == '' or text.lower() in setting.description.lower(): labelItem = QStandardItem(setting.description) labelItem.setIcon(icon) labelItem.setEditable(False) self.items[setting] = SettingItem(setting) groupItem.insertRow(0, [labelItem, self.items[setting]]) if text != '': self.tree.expand(groupItem.index()) providersItem = QStandardItem(self.tr('Providers')) icon = QIcon(os.path.join(pluginPath, 'images', 'alg.png')) providersItem.setIcon(icon) providersItem.setEditable(False) emptyItem = QStandardItem() emptyItem.setEditable(False) rootItem.insertRow(0, [providersItem, emptyItem]) for group in settings.keys(): if group in priorityKeys or group == menusSettingsGroup: continue groupItem = QStandardItem(group) icon = ProcessingConfig.getGroupIcon(group) groupItem.setIcon(icon) groupItem.setEditable(False) for setting in settings[group]: if setting.hidden: continue if text == '' or text.lower() in setting.description.lower(): labelItem = QStandardItem(setting.description) labelItem.setIcon(icon) labelItem.setEditable(False) self.items[setting] = SettingItem(setting) groupItem.insertRow(0, [labelItem, self.items[setting]]) emptyItem = QStandardItem() emptyItem.setEditable(False) providersItem.appendRow([groupItem, emptyItem]) menusItem = QStandardItem(self.tr('Menus (requires restart)')) icon = QIcon(os.path.join(pluginPath, 'images', 'menu.png')) menusItem.setIcon(icon) menusItem.setEditable(False) emptyItem = QStandardItem() emptyItem.setEditable(False) rootItem.insertRow(0, [menusItem, emptyItem]) providers = Processing.providers for provider in providers: groupItem = QStandardItem(provider.getDescription()) icon = provider.getIcon() groupItem.setIcon(icon) groupItem.setEditable(False) for alg in provider.algs: labelItem = QStandardItem(alg.name) labelItem.setIcon(icon) labelItem.setEditable(False) try: setting = ProcessingConfig.settings["MENU_" + alg.commandLineName()] except: continue self.items[setting] = SettingItem(setting) groupItem.insertRow(0, [labelItem, self.items[setting]]) emptyItem = QStandardItem() emptyItem.setEditable(False) menusItem.appendRow([groupItem, emptyItem]) self.tree.sortByColumn(0, Qt.AscendingOrder) self.adjustColumns()
class SubPowerWidget(QWidget): """ @brief Zeigt alle Unterkräfte in einer Baumstruktur an. """ def __init__(self, template, character, parent=None): super(SubPowerWidget, self).__init__(parent) self.__storage = template self.__character = character self.__model = QStandardItemModel() # Das ungenutzte Model dient dazu, alle Unterkräfte aufzunehmen, die ich nicht darstellen möchte. Ist einfacher, als diese im View zu verstecken. self.__modelUnused = QStandardItemModel() self._layout = QVBoxLayout() self.setLayout( self._layout ) self.__view = QTreeView() self.__view.setHeaderHidden(True) self.__view.setModel( self.__model) self._layout.addWidget(self.__view) self._typ = "Subpower" categories = self.__storage.categories(self._typ) self.__items = {} self.__rootItem = QStandardItem() self.__rootItem = self.__model.invisibleRootItem() self.__rootItemUnused = QStandardItem() self.__rootItemUnused = self.__modelUnused.invisibleRootItem() for item in categories: categoryItem = QStandardItem(item) self.__rootItem.appendRow(categoryItem) ## Ich benötige diese Items auch im ungenutzten Model. categoryItemUnused = QStandardItem(item) self.__rootItemUnused.appendRow(categoryItemUnused) traitList = list( self.__character.traits[self._typ][item].items() ) traitList.sort() for trait in traitList: traitItem = QStandardItem(trait[1].name) traitItem.setCheckable(True) ## Unhashable Type self.__items[trait[1]] = traitItem categoryItem.appendRow(traitItem) ## Funktioniert mit PySide nicht: #trait[1].availableChanged.connect(traitItem.setEnabled) ## Funktioniert auch mit PySide: trait[1].availableChanged.connect( lambda enable, item=traitItem: item.setEnabled(enable) ) trait[1].valueChanged.connect( lambda val, trait=trait[1], item=traitItem: self.__setItemValue(trait, item) ) self.__model.itemChanged.connect(self.__getItemValue) self.__character.speciesChanged.connect(self.hideOrShowToolPage) self.__character.breedChanged.connect(self.hideOrShowToolPage) self.__character.factionChanged.connect(self.hideOrShowToolPage) def __setItemValue(self, trait, item): """ Setzt den Wert der Angezeigten Items. """ if trait.value == 0: item.setCheckState(Qt.Unchecked) elif trait.value == 1: item.setCheckState(Qt.PartiallyChecked) else: item.setCheckState(Qt.Checked) def __getItemValue(self, item): """ Setzt den Wert der Eigenschaft im Speicher. """ for trait in self.__items.items(): if id(trait[1]) == id(item): trait[0].value = item.checkState() break def hideOrShowToolPage(self, res): """ Alle Eigenschaften, die nicht zur Verfügung stehen, werden verborgen, indem sie in ein anderes Model verschoben werden. \bug Möglicher Fehler in PySide: Der Aufruf von QStandardItem.model() führt beim Beenden des Programms zu einem Segmentation Fault. """ # Versteckt alle Unterkräfte, die zu gewähltem Charakter nicht passen. for item in self.__items.items(): if ( ( item[0].species and item[0].species != self.__character.species ) or ( item[0].only and self.__character.breed not in item[0].only and self.__character.faction not in item[0].only ) ): #self.__view.setRowHidden(item[1].index().row(), item[1].parent().index(), True) #Debug.debug(item[1].model()) ## Hier wird beispielsweise besagter Aufruf getätigt, der zu einem segfault führt. if item[1].model() == self.__model: parent = item[1].parent() itemUnused = parent.takeRow(item[1].index().row()) parentUnused = self.__modelUnused.findItems(parent.text())[0] parentUnused.appendRow(itemUnused) else: #self.__view.setRowHidden(item[1].index().row(), item[1].parent().index(), False) if item[1].model() == self.__modelUnused: parent = item[1].parent() itemUsed = parent.takeRow(item[1].index().row()) parentUsed = self.__model.findItems(parent.text())[0] parentUsed.appendRow(itemUsed) ## Versteckt alle Elternzeilen, wenn sie keine Kinder enthalten. for i in range(self.__model.rowCount()): categoryItem = self.__model.item(i) if categoryItem.hasChildren(): self.__view.setRowHidden(categoryItem.index().row(), self.__rootItem.index(), False) else: self.__view.setRowHidden(categoryItem.index().row(), self.__rootItem.index(), True)
def __addNewItemTo(self, tree_view, text): item = QStandardItem(text) tree_view.model().insertRow(0, item) tree_view.selectionModel().select( item.index(), QItemSelectionModel.Rows | QItemSelectionModel.Select)
def __addNewItemTo(self, tree_view, text): item = QStandardItem(text) tree_view.model().insertRow(0, item) tree_view.selectionModel().select(item.index(), QItemSelectionModel.Rows | QItemSelectionModel.Select)