From 7ae910c40df15e3f65043cf8c1c1980f342ffaba Mon Sep 17 00:00:00 2001 From: viktorvanwijk Date: Tue, 30 Apr 2024 16:20:29 +0200 Subject: [PATCH 1/2] Modify PandasModel.removeRows() to work with an arbitrary row selection --- gui.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/gui.py b/gui.py index ae6600b..e950245 100644 --- a/gui.py +++ b/gui.py @@ -557,11 +557,14 @@ def headerData(self, section, orientation, role): else: return super().headerData(section, orientation, role) - def removeRows(self, row, count, parent=QModelIndex()): - self._l.debug(f"Deleting rows from '{row}' to '{row + count - 1}'.") - self.beginRemoveRows(parent, row, row + count - 1) - for _ in range(count): - self._data.drop(self._data.index[row], inplace=True) + # TODO: modifying this signature is not the nicest thing to do probably, as + # it violates the L in SOLID. It is no longer interchangable with it parent + # class QAbstractTableModel. + def removeRows(self, rows, count, parent=QModelIndex()): + self._l.debug(f"Deleting rows {rows}.") + self.beginRemoveRows(parent, rows[0], rows[-1]) + indices = [self._data.index[r] for r in rows] + self._data.drop(indices, inplace=True) self.endRemoveRows() self.layoutChanged.emit() self._l.debug(f"Dataframe length after deleting: {self._data.shape[0]}") @@ -601,9 +604,7 @@ def _init_ui(self) -> None: self.setSizeAdjustPolicy(QHeaderView.AdjustToContents) self.setSizePolicy(SIZE_MIN_EXPANDING, SIZE_MIN_EXPANDING) self.setSelectionBehavior(QAbstractItemView.SelectRows) - # NOTE: ContiguousSelection is needed as PandasModel.removeRows() does - # not support arbitrary row selection - self.setSelectionMode(QAbstractItemView.ContiguousSelection) + self.setSelectionMode(QAbstractItemView.ExtendedSelection) self.setEditTriggers(QAbstractItemView.NoEditTriggers) self.setSortingEnabled(True) @@ -651,7 +652,7 @@ def keyPressEvent(self, e: Optional[QKeyEvent]) -> None: rows = self.selectionModel().selectedRows() if len(rows) == 0: return - self.model().removeRows(rows[0].row(), len(rows), rows[0]) + self.model().removeRows([r.row() for r in rows], len(rows), rows[0]) self.selectionModel().clearSelection() super().keyPressEvent(e) From 795072ca033c09f59a00957542adf9d1ca43fc80 Mon Sep 17 00:00:00 2001 From: viktorvanwijk Date: Thu, 2 May 2024 19:30:27 +0200 Subject: [PATCH 2/2] Remove TODO --- gui.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/gui.py b/gui.py index e950245..1c3f421 100644 --- a/gui.py +++ b/gui.py @@ -557,9 +557,6 @@ def headerData(self, section, orientation, role): else: return super().headerData(section, orientation, role) - # TODO: modifying this signature is not the nicest thing to do probably, as - # it violates the L in SOLID. It is no longer interchangable with it parent - # class QAbstractTableModel. def removeRows(self, rows, count, parent=QModelIndex()): self._l.debug(f"Deleting rows {rows}.") self.beginRemoveRows(parent, rows[0], rows[-1])