def insert_source_index_children(self, source_index): """Recursively add children proxies to the list. """ for row in range(self.sourceModel().rowCount(source_index)): child_index = self.sourceModel().index(row, 0, source_index) proxy_index = self.createIndex( child_index.row(), child_index.column(), child_index.internalPointer(), ) self._index_list.append(proxy_index) child_index = QtCore.QPersistentModelIndex(child_index) # self._index_map.insert( # proxy_index, # QtCore.QPersistentModelIndex(child_index) # ) self._index_to_source_map[proxy_index] = child_index self._source_to_index_map[child_index] = proxy_index self.insert_source_index_children(child_index)
def highlight_item_row(self, item): """ highlight the entire row containing a table item @param item: table item """ try: if not item.index().isValid(): return parent = item.parent() if parent is None: parent = item if not parent.hasChildren(): self.highlight_item(parent) return row = item.row() column_num = parent.columnCount() for column in xrange(0, column_num): if self.functionModel.hasIndex(row, column, parent.index()): cur_index = self.functionModel.index( row, column, parent.index()) self.highlight_item( self.functionModel.itemFromIndex(cur_index)) persistent_index = QtCore.QPersistentModelIndex(cur_index) self.highligthed_items.append(persistent_index) except Exception as ex: print "Error while highlighting item row: %s" % ex
def focusOutEvent(self, event): if self.selectionModel().selectedIndexes(): for index in self.selectionModel().selectedRows(): self._last_indexes.append(QtCore.QPersistentModelIndex(index)) if self._last_indexes: for i in self._last_indexes: self.selectionModel().setCurrentIndex(i, QtGui.QItemSelectionModel.Select) event.accept()
def removeRows(self, rowIndexes): """ Remove multiple rows at once. :param rowIndexes: The rows that will be deleted """ # Make the indexes persistent first to be able to # delete multiple selections at once persistentRowIndexes = [] # Reverse the delete order --> no need to worry about shifting indexes <: for rowIndex in sorted(rowIndexes, reverse=True): persistentRowIndexes.append(QtCore.QPersistentModelIndex(rowIndex)) for persistentRowIndex in persistentRowIndexes: self.removeRow(persistentRowIndex.row())
def delete_selected(self): reply = QtGui.QMessageBox.question( self, "", "Are you sure you want to delete selected entries?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: selected = self.selectedIndexes() persistent = [ QtCore.QPersistentModelIndex(idx) for idx in selected ] self.clearSelection() fail_message = False for index in persistent: success = self.model().deleteEntry(index) if not success and not fail_message: fail_message = True if fail_message: QtGui.QMessageBox.critical( self, "", "Could not delete all selected entries. Make sure you have write permission for the corresponding files.", QtGui.QMessageBox.Ok)
def __init__(self, index, value, role, model, text=None, parent=None): """ Constructor. @param index The QModelIndex. @param value The value to be applied at the index. @param role The role Qt Item Data Role of the index. @param model The model. @param text The QUndoCommand display text. @param parent The QObject parent of the QUndoCommand """ super(SetDataCommand, self).__init__(text, parent) self._model = model self._index = QtCore.QPersistentModelIndex(index) self._role = role self._oldvalue = index.data(role) self._newvalue = value if not text: self.setText("({0},{1}) => [{2}] to [{3}] Role: {4}".format( self._index.row(), self._index.column(), self._oldvalue, self._newvalue, next((n for n, r in QtCore.Qt.ItemDataRole.values.items() if r == role), "<Unknown>")))
def delete_items(self): selection = self.table_view.selectionModel().selectedRows() persistent_indexes = [QtCore.QPersistentModelIndex(index) for index in selection] for index in persistent_indexes: if index.isValid(): self.dependency_model.removeRows(index.row(), 1, index.parent())
def set_index(self, new_index): self.index = QtCore.QPersistentModelIndex(new_index)