Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify PandasModel.removeRows() to work with an arbitrary row selection #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ 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)
def removeRows(self, rows, count, parent=QModelIndex()):
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the nicest thing to do maybe, as it differs from the behaviour of QAbstractTableModel

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]}")
Expand Down Expand Up @@ -642,9 +642,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)

Expand Down Expand Up @@ -692,7 +690,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)

Expand Down