def select(self, selection, flags): if isinstance(selection, QModelIndex): selection = QItemSelection(selection, selection) model = self.model() indexes = selection.indexes() sel_inds = {ind.row() for ind in indexes} | \ {ind.column() for ind in indexes} if flags == QItemSelectionModel.ClearAndSelect: selected = set() else: selected = {ind.row() for ind in self.selectedIndexes()} if flags & QItemSelectionModel.Select: selected |= sel_inds elif flags & QItemSelectionModel.Deselect: selected -= sel_inds new_selection = QItemSelection() regions = list(ranges(sorted(selected))) for r_start, r_end in regions: for c_start, c_end in regions: top_left = model.index(r_start, c_start) bottom_right = model.index(r_end - 1, c_end - 1) new_selection.select(top_left, bottom_right) QItemSelectionModel.select(self, new_selection, QItemSelectionModel.ClearAndSelect)
def select(self, selection, flags): """Reimplemented.""" if isinstance(selection, QModelIndex): selection = QItemSelection(selection, selection) model = self.model() indexes = self.selectedIndexes() rows = set(ind.row() for ind in indexes) cols = set(ind.column() for ind in indexes) if flags & QItemSelectionModel.Select and \ not flags & QItemSelectionModel.Clear and self.__selectBlocks: indexes = selection.indexes() sel_rows = set(ind.row() for ind in indexes).union(rows) sel_cols = set(ind.column() for ind in indexes).union(cols) selection = QItemSelection() for r_start, r_end in ranges(sorted(sel_rows)): for c_start, c_end in ranges(sorted(sel_cols)): top_left = model.index(r_start, c_start) bottom_right = model.index(r_end - 1, c_end - 1) selection.select(top_left, bottom_right) elif self.__selectBlocks and flags & QItemSelectionModel.Deselect: indexes = selection.indexes() def to_ranges(indices): return list(range(*r) for r in ranges(indices)) selected_rows = to_ranges(sorted(rows)) selected_cols = to_ranges(sorted(cols)) desel_rows = to_ranges(set(ind.row() for ind in indexes)) desel_cols = to_ranges(set(ind.column() for ind in indexes)) selection = QItemSelection() # deselection extended vertically for row_range, col_range in \ itertools.product(selected_rows, desel_cols): selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) # deselection extended horizontally for row_range, col_range in \ itertools.product(desel_rows, selected_cols): selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) QItemSelectionModel.select(self, selection, flags)
def _tree_to_model( tree: Dict, root: QStandardItem, sel_model: QItemSelectionModel ) -> None: # tuple of subtree and selection flag if isinstance(tree, tuple): tree, _ = tree # read from .json if isinstance(tree, list): tree = {t: {} for t in tree} for word, words in tree.items(): item = QStandardItem(word) root.appendRow(item) if isinstance(words, tuple): _, selected = words if selected: sel_model.select(item.index(), QItemSelectionModel.Select) if len(words): _tree_to_model(words, item, sel_model)
def select(self, index, flags=QItemSelectionModel.ClearAndSelect): if isinstance(index, int): index = self.model().index(index) return QItemSelectionModel.select(self, index, flags)
def __init__(self, model, parent=None): QItemSelectionModel.__init__(self, model, parent) self.selectionChanged.connect(self.onSelectionChanged)
def test_header(self): model = QStandardItemModel() hheader = HeaderView(Qt.Horizontal) vheader = HeaderView(Qt.Vertical) hheader.setSortIndicatorShown(True) # paint with no model. vheader.grab() hheader.grab() hheader.setModel(model) vheader.setModel(model) hheader.adjustSize() vheader.adjustSize() # paint with an empty model vheader.grab() hheader.grab() model.setRowCount(1) model.setColumnCount(1) icon = QIcon(StampIconEngine("A", Qt.red)) model.setHeaderData(0, Qt.Horizontal, icon, Qt.DecorationRole) model.setHeaderData(0, Qt.Vertical, icon, Qt.DecorationRole) model.setHeaderData(0, Qt.Horizontal, QColor(Qt.blue), Qt.ForegroundRole) model.setHeaderData(0, Qt.Vertical, QColor(Qt.blue), Qt.ForegroundRole) model.setHeaderData(0, Qt.Horizontal, QColor(Qt.white), Qt.BackgroundRole) model.setHeaderData(0, Qt.Vertical, QColor(Qt.white), Qt.BackgroundRole) # paint with single col/row model vheader.grab() hheader.grab() model.setRowCount(3) model.setColumnCount(3) hheader.adjustSize() vheader.adjustSize() # paint with single col/row model vheader.grab() hheader.grab() hheader.setSortIndicator(0, Qt.AscendingOrder) vheader.setHighlightSections(True) hheader.setHighlightSections(True) vheader.grab() hheader.grab() vheader.setSectionsClickable(True) hheader.setSectionsClickable(True) vheader.grab() hheader.grab() vheader.setTextElideMode(Qt.ElideRight) hheader.setTextElideMode(Qt.ElideRight) selmodel = QItemSelectionModel(model, model) vheader.setSelectionModel(selmodel) hheader.setSelectionModel(selmodel) selmodel.select(model.index(1, 1), QItemSelectionModel.Rows | QItemSelectionModel.Select) selmodel.select(model.index(1, 1), QItemSelectionModel.Columns | QItemSelectionModel.Select) vheader.grab() vheader.grab()
def selected(sel: QItemSelectionModel) -> Set[Tuple[int, int]]: return set((r.row(), r.column()) for r in sel.selectedIndexes())