-
Notifications
You must be signed in to change notification settings - Fork 1
Do and Undo (mainly) #84
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
325da86
Very fist Undo Command working
PaulJonasJost b804daa
Completed column alteration
PaulJonasJost 2360c2d
Merge branch 'develop' into do_undo
PaulJonasJost c9d147a
More stable undo redo of a singular column
PaulJonasJost f842d05
adding and deleting rows is now reversible
PaulJonasJost f794231
Undo Redo provisourically complete, testing remains
PaulJonasJost fb39f8d
More unified petab linting
PaulJonasJost a18938b
Casting into correct datatypes now. Slight changes to error messages
PaulJonasJost 3cc7c9b
Various fixes:
PaulJonasJost b096365
Worked in suggestions
PaulJonasJost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| """Store commands for the do/undo functionality.""" | ||
| from PySide6.QtGui import QUndoCommand | ||
| from PySide6.QtCore import QModelIndex, Qt | ||
| import pandas as pd | ||
| import numpy as np | ||
|
|
||
|
|
||
| pd.set_option('future.no_silent_downcasting', True) | ||
|
|
||
|
|
||
| class ModifyColumnCommand(QUndoCommand): | ||
| """Command to add a column to the table.""" | ||
|
|
||
| def __init__(self, model, column_name, add_mode: bool = True): | ||
| action = "Add" if add_mode else "Remove" | ||
| super().__init__( | ||
| f"{action} column {column_name} in table {model.table_type}" | ||
| ) | ||
| self.model = model | ||
| self.column_name = column_name | ||
| self.add_mode = add_mode | ||
| self.old_values = None | ||
| self.position = None | ||
|
|
||
| if not add_mode and column_name in model._data_frame.columns: | ||
| self.position = model._data_frame.columns.get_loc(column_name) | ||
| self.old_values = model._data_frame[column_name].copy() | ||
|
|
||
| def redo(self): | ||
| if self.add_mode: | ||
| position = self.model._data_frame.shape[1] | ||
| self.model.beginInsertColumns(QModelIndex(), position, position) | ||
| self.model._data_frame[self.column_name] = "" | ||
| self.model.endInsertColumns() | ||
| else: | ||
| self.position = self.model._data_frame.columns.get_loc(self.column_name) | ||
| self.model.beginRemoveColumns(QModelIndex(), self.position, self.position) | ||
| self.model._data_frame.drop(columns=self.column_name, inplace=True) | ||
| self.model.endRemoveColumns() | ||
|
|
||
| def undo(self): | ||
| if self.add_mode: | ||
| position = self.model._data_frame.columns.get_loc(self.column_name) | ||
| self.model.beginRemoveColumns(QModelIndex(), position, position) | ||
| self.model._data_frame.drop(columns=self.column_name, inplace=True) | ||
| self.model.endRemoveColumns() | ||
| else: | ||
| self.model.beginInsertColumns(QModelIndex(), self.position, self.position) | ||
| self.model._data_frame.insert(self.position, self.column_name, self.old_values) | ||
| self.model.endInsertColumns() | ||
|
|
||
|
|
||
| class ModifyRowCommand(QUndoCommand): | ||
| """Command to add a row to the table.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| model, | ||
| row_indices: list[int] | int, | ||
| add_mode: bool = True | ||
| ): | ||
| action = "Add" if add_mode else "Remove" | ||
| super().__init__(f"{action} row(s) in table {model.table_type}") | ||
| self.model = model | ||
| self.add_mode = add_mode | ||
| self.old_rows = None | ||
| self.old_ind_names = None | ||
|
|
||
| df = self.model._data_frame | ||
|
|
||
| if add_mode: | ||
| # Adding: interpret input as count of new rows | ||
| self.row_indices = self._generate_new_indices(row_indices) | ||
| else: | ||
| # Deleting: interpret input as specific index labels | ||
| self.row_indices = row_indices if isinstance(row_indices, list) else [row_indices] | ||
| self.old_rows = df.iloc[self.row_indices].copy() | ||
| self.old_ind_names = [df.index[idx] for idx in self.row_indices] | ||
|
|
||
| def _generate_new_indices(self, count): | ||
| """Generate default row indices based on table type and index type.""" | ||
| df = self.model._data_frame | ||
| base = 0 | ||
| existing = set(df.index.astype(str)) | ||
|
|
||
| indices = [] | ||
| while len(indices) < count: | ||
| idx = f"new_{self.model.table_type}_{base}" | ||
| if idx not in existing: | ||
| indices.append(idx) | ||
| base += 1 | ||
| self.old_ind_names = indices | ||
| return indices | ||
|
|
||
| def redo(self): | ||
| df = self.model._data_frame | ||
|
|
||
| if self.add_mode: | ||
| position = df.shape[0] - 1 # insert *before* the auto-row | ||
| self.model.beginInsertRows(QModelIndex(), position, position + len(self.row_indices) - 1) | ||
| for i, idx in enumerate(self.row_indices): | ||
| df.loc[idx] = [""] * df.shape[1] | ||
| self.model.endInsertRows() | ||
| else: | ||
| self.model.beginRemoveRows(QModelIndex(), min(self.row_indices), max(self.row_indices)) | ||
| df.drop(index=self.old_ind_names, inplace=True) | ||
| self.model.endRemoveRows() | ||
|
|
||
| def undo(self): | ||
| df = self.model._data_frame | ||
|
|
||
| if self.add_mode: | ||
| positions = [df.index.get_loc(idx) for idx in self.row_indices] | ||
| self.model.beginRemoveRows(QModelIndex(), min(positions), max(positions)) | ||
| df.drop(index=self.old_ind_names, inplace=True) | ||
| self.model.endRemoveRows() | ||
| else: | ||
| self.model.beginInsertRows(QModelIndex(), min(self.row_indices), max(self.row_indices)) | ||
| restore_index_order = df.index | ||
| for pos, index_name, row in zip( | ||
| self.row_indices, self.old_ind_names, self.old_rows.values | ||
| ): | ||
| restore_index_order = restore_index_order.insert( | ||
| pos, index_name | ||
| ) | ||
| df.loc[index_name] = row | ||
| df.sort_index( | ||
| inplace=True, | ||
| key=lambda x: x.map(restore_index_order.get_loc) | ||
| ) | ||
| self.model.endInsertRows() | ||
|
|
||
|
|
||
| class ModifyDataFrameCommand(QUndoCommand): | ||
| def __init__(self, model, changes: dict[tuple, tuple], description="Modify values"): | ||
| super().__init__(description) | ||
| self.model = model | ||
| self.changes = changes # {(row_key, column_name): (old_val, new_val)} | ||
|
|
||
| def redo(self): | ||
| self._apply_changes(use_new=True) | ||
|
|
||
| def undo(self): | ||
| self._apply_changes(use_new=False) | ||
|
|
||
| def _apply_changes(self, use_new: bool): | ||
| df = self.model._data_frame | ||
| col_offset = 1 if self.model._has_named_index else 0 | ||
| original_dtypes = df.dtypes.copy() | ||
|
|
||
| # Apply changes | ||
| update_vals = { | ||
| (row, col): val[1 if use_new else 0] | ||
| for (row, col), val in self.changes.items() | ||
| } | ||
| update_df = pd.Series(update_vals).unstack() | ||
| for col in update_df.columns: | ||
| if col in df.columns: | ||
| df[col] = df[col].astype('object') | ||
| update_df.replace({None: "Placeholder_temp"}, inplace=True) | ||
| df.update(update_df) | ||
| df.replace({"Placeholder_temp": ""}, inplace=True) | ||
| for col, dtype in original_dtypes.items(): | ||
| if col not in update_df.columns: | ||
| continue | ||
| if np.issubdtype(dtype, np.number): | ||
| df[col] = pd.to_numeric(df[col], errors="coerce") | ||
| else: | ||
| df[col] = df[col].astype(dtype) | ||
|
|
||
| rows = [df.index.get_loc(row_key) for (row_key, _) in | ||
| self.changes.keys()] | ||
| cols = [df.columns.get_loc(col) + col_offset for (_, col) in | ||
| self.changes.keys()] | ||
|
|
||
| top_left = self.model.index(min(rows), min(cols)) | ||
| bottom_right = self.model.index(max(rows), max(cols)) | ||
| self.model.dataChanged.emit(top_left, bottom_right, [Qt.DisplayRole]) | ||
|
|
||
|
|
||
| class RenameIndexCommand(QUndoCommand): | ||
| def __init__(self, model, old_index, new_index, model_index): | ||
| super().__init__(f"Rename index {old_index} → {new_index}") | ||
| self.model = model | ||
| self.model_index = model_index | ||
| self.old_index = old_index | ||
| self.new_index = new_index | ||
|
|
||
| def redo(self): | ||
| self._apply(self.old_index, self.new_index) | ||
|
|
||
| def undo(self): | ||
| self._apply(self.new_index, self.old_index) | ||
|
|
||
| def _apply(self, src, dst): | ||
| df = self.model._data_frame | ||
| df.rename(index={src: dst}, inplace=True) | ||
| self.model.dataChanged.emit( | ||
| self.model_index, self.model_index, [Qt.DisplayRole] | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.