def index(self, row: int, column: int, parent: QModelIndex = None) -> QModelIndex: """ Function create QModelIndex from the parent item and row and column index. Parameters ---------- row Row of the child column Column of th child - not used since we have only one column. parent Parent of the child we are making index of. Returns ------- Index of the child at row-th row of the parent. """ if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parent_item = self.rootItem else: parent_item = self.node_from_index(parent) child_item = parent_item.child(row) if child_item: return self.createIndex(row, column, child_item) else: return QModelIndex()
def data(self, index: QModelIndex, role=None) -> Optional[str]: """ Function returns the index's data that are shown in the view. """ if not index.isValid() or role != Qt.DisplayRole: return None item = self.node_from_index(index) rd = item.name return rd
def rowCount(self, parent: QModelIndex = None) -> int: # noqa: N802 """ Function returns the number children of an item. """ if not parent or not parent.isValid(): parent_item = self.rootItem else: parent_item = self.node_from_index(parent) count = parent_item.children_count() return count
def parent(self, index: QModelIndex) -> QModelIndex: """ Function returns parent of the item in index. """ if not index.isValid(): return QModelIndex() child_item = self.node_from_index(index) parent_item = child_item.parentItem if child_item else None if not parent_item or parent_item == self.rootItem: return QModelIndex() return self.createIndex(parent_item.row(), 0, parent_item)
def __update(self): """Update the current description. """ if self.__currentIndex != -1: index = self.model().index(self.__currentIndex, 0) else: index = QModelIndex() if not index.isValid(): description = "" name = "" path = "" svg = NO_PREVIEW_SVG else: description = qtcompat.qunwrap(index.data(Qt.WhatsThisRole)) if description: description = six.text_type(description) else: description = u"No description." description = escape(description) description = description.replace("\n", "<br/>") name = qtcompat.qunwrap(index.data(Qt.DisplayRole)) if name: name = six.text_type(name) else: name = "Untitled" name = escape(name) path = qtcompat.qunwrap(index.data(Qt.StatusTipRole)) path = six.text_type(path) svg = qtcompat.qunwrap(index.data(previewmodel.ThumbnailSVGRole)) svg = six.text_type(svg) desc_text = self.__template.format(description=description, name=name) self.__label.setText(desc_text) self.__path.setText(path) if not svg: svg = NO_PREVIEW_SVG if svg: self.__image.load(QByteArray(svg.encode("utf-8")))
def node_from_index(self, index: QModelIndex) -> TreeItem: # noqa: N802 """ Function returns a TreeItem saved in the index. Parameters ---------- index Index of the TreeItem Returns ------- Tree item from the index. """ if index.isValid(): return index.internalPointer() else: return self.rootItem
def __update(self): # type: () -> None """Update the current description. """ if self.__currentIndex != -1 and self.__model is not None: index = self.__model.index(self.__currentIndex, 0) else: index = QModelIndex() if not index.isValid(): description = "" name = "" path = "" svg = NO_PREVIEW_SVG else: description = index.data(Qt.WhatsThisRole) if description: description = description else: description = "没有说明。" description = escape(description) description = description.replace("\n", "<br/>") name = index.data(Qt.DisplayRole) if name: name = name else: name = "Untitled" name = escape(name) path = str(index.data(Qt.StatusTipRole)) svg = str(index.data(previewmodel.ThumbnailSVGRole)) desc_text = self.__template.format(description=description, name=name) self.__label.setText(desc_text) self.__path.setText(contractuser(path)) if not svg: svg = NO_PREVIEW_SVG if svg: self.__image.load(QByteArray(svg.encode("utf-8")))
def __update(self): """Update the current description. """ if self.__currentIndex != -1: index = self.model().index(self.__currentIndex, 0) else: index = QModelIndex() if not index.isValid(): description = "" name = "" path = "" svg = NO_PREVIEW_SVG else: description = index.data(Qt.WhatsThisRole) if description: description = description else: description = "No description." description = escape(description) description = description.replace("\n", "<br/>") name = index.data(Qt.DisplayRole) if name: name = name else: name = "Untitled" name = escape(name) path = str(index.data(Qt.StatusTipRole)) svg = str(index.data(previewmodel.ThumbnailSVGRole)) desc_text = self.__template.format(description=description, name=name) self.__label.setText(desc_text) self.__path.setText(contractuser(path)) if not svg: svg = NO_PREVIEW_SVG if svg: self.__image.load(QByteArray(svg.encode("utf-8")))
def selectionCommand( self, index: QModelIndex, event: QEvent = None) -> QItemSelectionModel.SelectionFlags: flags = super().selectionCommand(index, event) selmodel = self.selectionModel() if not index.isValid(): # Click on empty viewport; don't clear return QItemSelectionModel.NoUpdate if selmodel.isSelected(index): currsel = selmodel.selectedIndexes() if len(currsel) == 1 and index == currsel[0]: # Is the last selected index; do not deselect it return QItemSelectionModel.NoUpdate if (event is not None and event.type() == QEvent.MouseMove and flags & QItemSelectionModel.ToggleCurrent): # Disable ctrl drag 'toggle'; can be made to deselect the last # index, would need to keep track of the current selection # (selectionModel does this but does not expose it) flags &= ~QItemSelectionModel.Toggle flags |= QItemSelectionModel.Select return flags
def flags(self, index: QModelIndex) -> Qt.ItemFlag: if not index.isValid(): return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled | Qt.ItemIsDragEnabled