def moveRows(self, sourceParent: PySide6.QtCore.QModelIndex, sourceRow: int, count: int, destinationParent: PySide6.QtCore.QModelIndex, destinationChild: int) -> bool: if sourceRow < 0 or sourceRow + count - 1 >= self.rowCount( sourceParent ) or destinationChild < 0 or destinationChild > self.rowCount( destinationParent ) or sourceRow == destinationChild - 1 or count <= 0: return False if not self.beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild): return False fromRow = sourceRow if destinationChild < sourceRow: fromRow += count - 1 else: destinationChild -= 1 while count > 0: self.layouts.insert(destinationChild, self.layouts.pop(fromRow)) count -= 1 self.endMoveRows() # fromRow = (sourceRow + count - # 1) if destinationChild < sourceRow else sourceRow # while count > 0: # count -= 1 # print(f'{fromRow} <-> {destinationChild}') # self.layouts.insert(destinationChild, self.layouts.pop(fromRow)) # self.endMoveRows() return True
def editEntry(self): """ Edit an entry in the addressbook. """ tableView = self.currentWidget() proxyModel = tableView.model() selectionModel = tableView.selectionModel() # Get the name and address of the currently selected row. indexes = selectionModel.selectedRows() for index in indexes: row = proxyModel.mapToSource(index).row() ix = self.tableModel.index(row, 0, QModelIndex()) name = self.tableModel.data(ix, Qt.DisplayRole) ix = self.tableModel.index(row, 1, QModelIndex()) address = self.tableModel.data(ix, Qt.DisplayRole) # Open an addDialogWidget, and only allow the user to edit the address. addDialog = AddDialogWidget() addDialog.setWindowTitle("Edit a Contact") addDialog.nameText.setReadOnly(True) addDialog.nameText.setText(name) addDialog.addressText.setText(address) # If the address is different, add it to the model. if addDialog.exec_(): newAddress = addDialog.address if newAddress != address: ix = self.tableModel.index(row, 1, QModelIndex()) self.tableModel.setData(ix, newAddress, Qt.EditRole)
def parent(self, index=None): if not index.isValid(): return QModelIndex() child_item = index.internalPointer() parent_item = child_item.getParent() if parent_item == self._root: return QModelIndex() return self.createIndex(0, 0, parent_item)
def removeRows(self, position, rows=1, index=QModelIndex()): """ Remove a row from the model. """ self.beginRemoveRows(QModelIndex(), position, position + rows - 1) del self.addresses[position:position + rows] self.endRemoveRows() return True
def index(self, row, col, parent): if parent.isValid(): return QModelIndex() if row >= len(self.entries): return QModelIndex() if col >= self.total_cols: return QModelIndex() return self.createIndex(row, col)
def columnCount(self, parent: Qt = QModelIndex()) -> int: """Override method from QAbstractTableModel Return column count of the pandas DataFrame """ if parent == QModelIndex(): return len(self._dataframe.columns) return 0
def insertRows(self, position, rows=1, index=QModelIndex()): """ Insert a row into the model. """ self.beginInsertRows(QModelIndex(), position, position + rows - 1) for row in range(rows): self.addresses.insert(position + row, {"name": "", "address": ""}) self.endInsertRows() return True
def depends(self): if self.ui.depends.currentIndex() == 0: return QModelIndex() index = self.indexList[self.ui.depends.currentIndex() - 1] if index.isValid(): return index return QModelIndex()
def moveRow(self, from_, to): if from_ == to: return if from_ >= len(self.m_tasks) or to >= len(self.m_tasks) + 1: return if self.beginMoveRows(QModelIndex(), from_, from_, QModelIndex(), to): self.m_tasks.insert(to, self.m_tasks.pop(from_)) self.endMoveRows() else: return
def parent(self, child: QModelIndex = ...) -> QModelIndex: parent_arg_in = {"child": child} logging.debug("parent") logging.debug(parent_arg_in) if not child.isValid(): return QModelIndex() child_item = child.internalPointer() parent_item = child_item.parent_item() if parent_item == self.__root: return QModelIndex() logging.debug({"return": self.createIndex(parent_item.row(), 0, parent_item)}) return self.createIndex(parent_item.row(), 0, parent_item)
def index(self, row, column, parent=None): if parent is None: return QModelIndex() if not parent.isValid(): parent_id = self.ROOT_PID else: parent_id = parent.internalId() child_id = readSQL( f"SELECT id FROM {self._table} WHERE pid=:pid LIMIT 1 OFFSET :row_n", [(":pid", parent_id), (":row_n", row)]) if child_id: return self.createIndex(row, column, id=child_id) return QModelIndex()
def parent(self, index): if not index.isValid(): return QModelIndex() child_id = index.internalId() parent_id = readSQL(f"SELECT pid FROM {self._table} WHERE id=:id", [(":id", child_id)]) if parent_id == self.ROOT_PID: return QModelIndex() row = readSQL( f"SELECT row_number FROM (" f"SELECT ROW_NUMBER() OVER (ORDER BY id) AS row_number, id, pid " f"FROM {self._table} WHERE pid IN (SELECT pid FROM {self._table} WHERE id=:id)) " f"WHERE id=:id", [(":id", parent_id)]) return self.createIndex(row - 1, 0, id=parent_id)
def index(self, row: int, column: int, parent: QModelIndex = QModelIndex()) -> QModelIndex: if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parent_item = self.rootItem else: parent_item = parent.internalPointer() child_item = parent_item.child(row) if child_item: return self.createIndex(row, column, child_item) else: return QModelIndex()
def locateItem(self, item_id, use_filter=''): row = readSQL( f"SELECT row_number FROM (SELECT ROW_NUMBER() OVER (ORDER BY tag) AS row_number, id " f"FROM {self._table}) WHERE id=:id", [(":id", item_id)]) if row is None: return QModelIndex() return self.index(row - 1, 0)
def dropMimeData(self, data, action, row, column, parent): # If any albums were dropped into another album, # add all the songs in the dropped album # but not the album item itself if parent.isValid(): dummy_model = QStandardItemModel() dummy_model.dropMimeData(data, action, 0, 0, QModelIndex()) indexes = [] for r in range(dummy_model.rowCount()): for c in range(dummy_model.columnCount()): index = dummy_model.index(r, c) # QStandardItemModel doesn't recognize our items as our custom classes # so we have to treat them as QStandardItems item = dummy_model.item(r, c) if item.data( CustomDataRole.ITEMTYPE) == TreeWidgetType.ALBUM: indexes += [ child.index() for child in AlbumTreeWidgetItem. getChildrenFromStandardItem(item) ] elif item.data( CustomDataRole.ITEMTYPE) == TreeWidgetType.SONG: indexes.append(index) data = dummy_model.mimeData(indexes) ret = super().dropMimeData(data, action, row, column, parent) return ret
def addEntry(self, name=None, address=None): """ Add an entry to the addressbook. """ if name is None and address is None: addDialog = AddDialogWidget() if addDialog.exec_(): name = addDialog.name address = addDialog.address address = {"name": name, "address": address} addresses = self.tableModel.addresses[:] # The QT docs for this example state that what we're doing here # is checking if the entered name already exists. What they # (and we here) are actually doing is checking if the whole # name/address pair exists already - ok for the purposes of this # example, but obviously not how a real addressbook application # should behave. try: addresses.remove(address) QMessageBox.information(self, "Duplicate Name", "The name \"%s\" already exists." % name) except ValueError: # The address didn't already exist, so let's add it to the model. # Step 1: create the row self.tableModel.insertRows(0) # Step 2: get the index of the newly created row and use it. # to set the name ix = self.tableModel.index(0, 0, QModelIndex()) self.tableModel.setData(ix, address["name"], Qt.EditRole) # Step 3: lather, rinse, repeat for the address. ix = self.tableModel.index(0, 1, QModelIndex()) self.tableModel.setData(ix, address["address"], Qt.EditRole) # Remove the newAddressTab, as we now have at least one # address in the model. self.removeTab(self.indexOf(self.newAddressTab)) # The screenshot for the QT example shows nicely formatted # multiline cells, but the actual application doesn't behave # quite so nicely, at least on Ubuntu. Here we resize the newly # created row so that multiline addresses look reasonable. tableView = self.currentWidget() tableView.resizeRowToContents(ix.row())
def index(self, row: int, column: int, parent: QModelIndex = ...) -> QModelIndex: index_arg_in = {"row": row, "column": column, "parent": parent} logging.debug("index") logging.debug(index_arg_in) if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parent_item = self.__root else: parent_item = parent.internalPointer() res = parent_item.child(row) if res is None: return QModelIndex() logging.debug({"return": self.createIndex(row, column, res)}) return self.createIndex(row, column, res)
def on_devices_setitem( self, devices: DeviceDict, device_id: DeviceUID, _device: Device ) -> None: row_index = self.get_row_index(device_id) self.beginInsertRows(QModelIndex(), row_index, row_index) self.endInsertRows()
def data(self, idx=QModelIndex(), role=None): c = idx.column() r = idx.row() if c == 0 and role == Qt.DisplayRole: return self.name_list[r] if c == 1 and role == Qt.DisplayRole: return self.reqCount_list[r] if c == 2 and self.counter != None and role == Qt.DisplayRole: return '+' * self.counter[r]
def index(self, row, column, parent=None): if not parent.isValid(): parent = self._root else: parent = parent.internalPointer() child = parent.getChild(row) if child: return self.createIndex(row, column, child) return QModelIndex()
def removeEntry(self): selectedIndexes = self.ui.ganttView.selectionModel().selectedIndexes() if len(selectedIndexes) > 0: index = selectedIndexes[0] else: index = QModelIndex() if not index.isValid(): return self.model.removeRow(index.row(), index.parent())
def removeRows(self, row: int, count: int, parent: PySide6.QtCore.QModelIndex) -> bool: if count <= 0 or row < 0 or (row + count) > self.rowCount(parent): return False if count == 1: self.beginRemoveRows(QModelIndex(), row, row + count - 1) self.layouts.pop(row) self.endRemoveRows() return True return False
def add_new_goods(self, name: str, gtype: GoodsType._Type, comment: Optional[str] = None): self.beginInsertRows(QModelIndex(), self.rowCount() + 1, self.rowCount() + 1) g = Record(self._driver) g.name = name g.type = gtype g.comment = comment g.save() self._goods = self._load() self.endInsertRows()
def initFrom(self, index, constraintModel): row = index.row() parent = index.parent() self.ui.name.setText( self.model.data(self.model.index(row, 0, parent)).toString()) self.ui.legend.setText( self.model.data(self.model.index(row, 5, parent)).toString()) idx = self.ui.type.findData( self.model.data(self.model.index(row, 1, parent)).toInt()) self.ui.type.setCurrentIndex(idx) self.ui.startDate.setDateTime( self.model.data(self.model.index(row, 2, parent)).toDateTime()) self.ui.endDate.setDateTime( self.model.data(self.model.index(row, 3, parent)).toDateTime()) self.ui.completion.setValue( self.model.data(self.model.index(row, 4, parent)).toInt()) self.ui.readOnly.setChecked(not ( self.model.flags(self.model.index(row, 0, parent)) & Qt.ItemIsEditable)) constraints = constraintModel.constraintsForIndex( self.model.index(row, 0, parent)) if constraints.isEmpty(): return constraintIndex = QModelIndex() for i in range(0, constraints.size()): constraint = constraints[i] if constraint.endIndex() == index: constraintIndex = constraint.startIndex() break if not constraintIndex.isValid(): return self.ui.depends.setCurrentIndex(indexList.indexOf(constraintIndex) + 1)
def addNewEntry(self): dialog = EntryDialog(self.model) dialog.setWindowTitle("New Entry") if dialog.exec_() == QDialog.Rejected: dialog = None return selectedIndexes = self.ui.ganttView.selectionModel().selectedIndexes() if len(selectedIndexes) > 0: parent = selectedIndexes[0] else: parent = QModelIndex() if not self.model.insertRow(self.model.rowCount(parent), parent): return row = self.model.rowCount(parent) - 1 if row == 0 and parent.isValid(): self.model.insertColumns(self.model.columnCount(paren), 5, parent) self.model.setData(self.model.index(row, 0, parent), dialog.name()) self.model.setData(self.model.index(row, 1, parent), dialog.type()) if dialog.type() != KDGantt.TypeSummary: self.model.setData(self.model.index(row, 2, parent), dialog.startDate(), KDGantt.StartTimeRole) self.model.setData(self.model.index(row, 3, parent), dialog.endDate(), KDGantt.EndTimeRole) self.model.setData(self.model.index(row, 4, parent), dialog.completion()) self.model.setData(self.model.index(row, 5, parent), dialog.legend()) self.addConstraint(dialog.depends(), self.model.index(row, 0, parent)) self.setReadOnly(self.model.index(row, 0, parent), dialog.readOnly()) dialog = None
def parent(self, child): return QModelIndex()
def index(self, row, column, parent): if parent.isValid() or column > len(self.columns) or row >= len( self.rows): return QModelIndex() return self.createIndex(row, column)
def index(self, row, column, _=QModelIndex()): """Provides index for the item (Overriden)""" return self.createIndex(row, column, self.get_play(row).id)
def parent(self, _): """Provides parent for the item (Overriden)""" return QModelIndex()
def columnCount(self, _=QModelIndex()): """Provides number of columns (Overriden)""" return 7