Skip to content

Commit

Permalink
don't call extra bound fn for add/del if using undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
ragardner committed Feb 21, 2025
1 parent 0ce3574 commit 567d22c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `show_top_left` parameter now defaults to `None` to represent tksheet handling of top left visibility
- Provide `natural_sort_key` as importable from `tksheet.natural_sort_key`
- `dropdown_search_function` now uses an iterable of objects rather than an iterable of Sequences of objects
- Rename internal parameter `restored_state` -> `from_undo`

### Version 7.4.2
#### Fixed:
Expand Down
6 changes: 4 additions & 2 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,18 +855,20 @@ Notes:
- In this case the arg `func` is totally ignored.
- For `"end_..."` events the bound function is run before the value is set.
- **To unbind** a function either set `func` argument to `None` or leave it as default e.g. `extra_bindings("begin_copy")` to unbind `"begin_copy"`.
- Even though undo/redo edits or adds or deletes rows/columns the bound functions for those actions will not be called. Undo/redo must be specifically bound in order for a function to be called.

Parameters:
`bindings` (`str`) options:
- Undo/Redo:
- `"begin_undo", "begin_ctrl_z"`
- `"ctrl_z", "end_undo", "end_ctrl_z", "undo"`
- Editing:
- `"begin_copy", "begin_ctrl_c"`
- `"ctrl_c", "end_copy", "end_ctrl_c", "copy"`
- `"begin_cut", "begin_ctrl_x"`
- `"ctrl_x", "end_cut", "end_ctrl_x", "cut"`
- `"begin_paste", "begin_ctrl_v"`
- `"ctrl_v", "end_paste", "end_ctrl_v", "paste"`
- `"begin_undo", "begin_ctrl_z"`
- `"ctrl_z", "end_undo", "end_ctrl_z", "undo"`
- `"begin_delete_key", "begin_delete"`
- `"delete_key", "end_delete", "end_delete_key", "delete"`
- `"begin_edit_cell", "begin_edit_table"`
Expand Down
42 changes: 22 additions & 20 deletions tksheet/main_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ def undo_modification_invert_event(self, modification: EventDataDict, name: str
event_data = self.delete_rows_displayed(
rows=tuple(reversed(modification["added"]["rows"]["row_heights"])),
event_data=event_data,
restored_state=True,
from_undo=True,
)

if modification["added"]["columns"]:
Expand All @@ -1985,7 +1985,7 @@ def undo_modification_invert_event(self, modification: EventDataDict, name: str
event_data = self.delete_columns_displayed(
cols=tuple(reversed(modification["added"]["columns"]["column_widths"])),
event_data=event_data,
restored_state=True,
from_undo=True,
)

if modification["deleted"]["rows"] or modification["deleted"]["row_heights"]:
Expand All @@ -1998,7 +1998,7 @@ def undo_modification_invert_event(self, modification: EventDataDict, name: str
create_ops=False,
create_selections=False,
add_col_positions=False,
restored_state=True,
from_undo=True,
)

if modification["deleted"]["columns"] or modification["deleted"]["column_widths"]:
Expand All @@ -2010,7 +2010,7 @@ def undo_modification_invert_event(self, modification: EventDataDict, name: str
create_ops=False,
create_selections=False,
add_row_positions=False,
restored_state=True,
from_undo=True,
)

if modification["eventname"].startswith(("edit", "move")):
Expand Down Expand Up @@ -4893,18 +4893,18 @@ def add_columns(
add_row_positions: bool = True,
push_ops: bool = True,
mod_event_boxes: bool = True,
restored_state: bool = False,
from_undo: bool = False,
) -> EventDataDict | None:
if not try_binding(self.extra_begin_insert_cols_rc_func, event_data, "begin_add_columns"):
if not from_undo and not try_binding(self.extra_begin_insert_cols_rc_func, event_data, "begin_add_columns"):
return
self.saved_column_widths = {}
if not restored_state and not self.all_columns_displayed:
if not from_undo and not self.all_columns_displayed:
self.displayed_columns = add_to_displayed(self.displayed_columns, columns)
cws = self.get_column_widths()
if column_widths and next(reversed(column_widths)) > len(cws):
for i in reversed(range(len(cws), len(cws) + next(reversed(column_widths)) - len(cws))):
column_widths[i] = self.PAR.ops.default_column_width
if not restored_state:
if not from_undo:
self.set_col_positions(
itr=insert_items(
cws,
Expand All @@ -4930,7 +4930,7 @@ def add_columns(
"index": {},
"row_heights": {rn: default_height for rn in range(len(self.row_positions) - 1, maxrn + 1)},
}
if not restored_state:
if not from_undo:
self.set_row_positions(
itr=chain(
self.gen_row_heights(),
Expand Down Expand Up @@ -4963,7 +4963,8 @@ def add_columns(
"header": header,
"column_widths": column_widths,
}
try_binding(self.extra_end_insert_cols_rc_func, event_data, "end_add_columns")
if not from_undo:
try_binding(self.extra_end_insert_cols_rc_func, event_data, "end_add_columns")
return event_data

def rc_add_columns(self, event: object = None) -> None:
Expand Down Expand Up @@ -5025,19 +5026,19 @@ def add_rows(
push_ops: bool = True,
tree: bool = True,
mod_event_boxes: bool = True,
restored_state: bool = False,
from_undo: bool = False,
) -> EventDataDict | None:
if not try_binding(self.extra_begin_insert_rows_rc_func, event_data, "begin_add_rows"):
if not from_undo and not try_binding(self.extra_begin_insert_rows_rc_func, event_data, "begin_add_rows"):
return
self.saved_row_heights = {}
if not restored_state and not self.all_rows_displayed:
if not from_undo and not self.all_rows_displayed:
self.displayed_rows = add_to_displayed(self.displayed_rows, rows)
rhs = self.get_row_heights()
if row_heights and next(reversed(row_heights)) > len(rhs):
default_row_height = self.get_default_row_height()
for i in reversed(range(len(rhs), len(rhs) + next(reversed(row_heights)) - len(rhs))):
row_heights[i] = default_row_height
if not restored_state:
if not from_undo:
self.set_row_positions(
itr=insert_items(
rhs,
Expand All @@ -5063,7 +5064,7 @@ def add_rows(
"header": {},
"column_widths": {cn: default_width for cn in range(len(self.col_positions) - 1, maxcn + 1)},
}
if not restored_state:
if not from_undo:
self.set_col_positions(
itr=chain(
self.gen_column_widths(),
Expand Down Expand Up @@ -5096,7 +5097,8 @@ def add_rows(
}
if tree and self.PAR.ops.treeview:
event_data = self.RI.tree_add_rows(event_data=event_data)
try_binding(self.extra_end_insert_rows_rc_func, event_data, "end_add_rows")
if not from_undo:
try_binding(self.extra_end_insert_rows_rc_func, event_data, "end_add_rows")
return event_data

def rc_add_rows(self, event: object = None) -> None:
Expand Down Expand Up @@ -5313,7 +5315,7 @@ def delete_columns_displayed(
self,
cols: list[int],
event_data: EventDataDict | None = None,
restored_state: bool = False,
from_undo: bool = False,
) -> EventDataDict:
if not event_data:
event_data = self.new_event_dict("delete_columns", state=True)
Expand All @@ -5322,7 +5324,7 @@ def delete_columns_displayed(
for c in reversed(cols):
if len(self.col_positions) > c + 1:
event_data["deleted"]["column_widths"][c] = self.col_positions[c + 1] - self.col_positions[c]
if not restored_state:
if not from_undo:
cols_set = set(cols)
self.set_col_positions(
itr=(width for c, width in enumerate(self.gen_column_widths()) if c not in cols_set)
Expand Down Expand Up @@ -5400,7 +5402,7 @@ def delete_rows_displayed(
self,
rows: list[int],
event_data: EventDataDict | None = None,
restored_state: bool = False,
from_undo: bool = False,
) -> EventDataDict:
if not event_data:
event_data = self.new_event_dict("delete_rows", state=True)
Expand All @@ -5409,7 +5411,7 @@ def delete_rows_displayed(
for r in reversed(rows):
if len(self.row_positions) > r + 1:
event_data["deleted"]["row_heights"][r] = self.row_positions[r + 1] - self.row_positions[r]
if not restored_state:
if not from_undo:
rows_set = set(rows)
self.set_row_positions(
itr=(height for r, height in enumerate(self.gen_row_heights()) if r not in rows_set)
Expand Down

0 comments on commit 567d22c

Please sign in to comment.