| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import tkinter as tk |
| 4 |
from collections import defaultdict |
| 5 |
from collections.abc import Callable, Generator, Hashable, Iterator, Sequence |
| 6 |
from contextlib import suppress |
| 7 |
from functools import partial |
| 8 |
from itertools import cycle, islice, repeat |
| 9 |
from math import ceil |
| 10 |
from re import findall |
| 11 |
from typing import Any, Literal |
| 12 |
|
| 13 |
from .colors import color_map |
| 14 |
from .constants import ( |
| 15 |
_test_str, |
| 16 |
rc_binding, |
| 17 |
text_editor_close_bindings, |
| 18 |
text_editor_newline_bindings, |
| 19 |
text_editor_to_unbind, |
| 20 |
) |
| 21 |
from .formatters import is_bool_like, try_to_bool |
| 22 |
from .functions import ( |
| 23 |
add_to_displayed, |
| 24 |
consecutive_ranges, |
| 25 |
del_placeholder_dict_key, |
| 26 |
event_dict, |
| 27 |
event_has_char_key, |
| 28 |
event_opens_dropdown_or_checkbox, |
| 29 |
get_n2a, |
| 30 |
get_new_indexes, |
| 31 |
int_x_tuple, |
| 32 |
is_contiguous, |
| 33 |
mod_event_val, |
| 34 |
new_tk_event, |
| 35 |
num2alpha, |
| 36 |
rounded_box_coords, |
| 37 |
stored_event_dict, |
| 38 |
try_b_index, |
| 39 |
try_binding, |
| 40 |
wrap_text, |
| 41 |
) |
| 42 |
from .other_classes import DotDict, DraggedRowColumn, DropdownStorage, EventDataDict, Node, TextEditorStorage |
| 43 |
from .sorting import sort_columns_by_row, sort_row |
| 44 |
from .text_editor import TextEditor |
| 45 |
|
| 46 |
|
| 47 |
class RowIndex(tk.Canvas): |
| 48 |
def __init__(self, parent, **kwargs): |
| 49 |
super().__init__( |
| 50 |
parent, |
| 51 |
background=parent.ops.index_bg, |
| 52 |
highlightthickness=0, |
| 53 |
) |
| 54 |
self.PAR = parent |
| 55 |
self.ops = self.PAR.ops |
| 56 |
self.MT = None |
| 57 |
self.CH = None |
| 58 |
self.TL = None |
| 59 |
self.new_iid_ctr = -1 |
| 60 |
self.current_width = None |
| 61 |
self.popup_menu_loc = None |
| 62 |
self.extra_begin_edit_cell_func = None |
| 63 |
self.extra_end_edit_cell_func = None |
| 64 |
self.b1_pressed_loc = None |
| 65 |
self.closed_dropdown = None |
| 66 |
self.centre_alignment_text_mod_indexes = (slice(1, None), slice(None, -1)) |
| 67 |
self.c_align_cyc = cycle(self.centre_alignment_text_mod_indexes) |
| 68 |
self.being_drawn_item = None |
| 69 |
self.extra_motion_func = None |
| 70 |
self.extra_b1_press_func = None |
| 71 |
self.extra_b1_motion_func = None |
| 72 |
self.extra_b1_release_func = None |
| 73 |
self.extra_rc_func = None |
| 74 |
self.selection_binding_func = None |
| 75 |
self.shift_selection_binding_func = None |
| 76 |
self.ctrl_selection_binding_func = None |
| 77 |
self.drag_selection_binding_func = None |
| 78 |
self.ri_extra_begin_drag_drop_func = None |
| 79 |
self.ri_extra_end_drag_drop_func = None |
| 80 |
self.ri_extra_begin_sort_cols_func = None |
| 81 |
self.ri_extra_end_sort_cols_func = None |
| 82 |
self.extra_double_b1_func = None |
| 83 |
self.row_height_resize_func = None |
| 84 |
self.cell_options = {} |
| 85 |
self.drag_and_drop_enabled = False |
| 86 |
self.dragged_row = None |
| 87 |
self.width_resizing_enabled = False |
| 88 |
self.height_resizing_enabled = False |
| 89 |
self.double_click_resizing_enabled = False |
| 90 |
self.row_selection_enabled = False |
| 91 |
self.rc_insert_row_enabled = False |
| 92 |
self.rc_delete_row_enabled = False |
| 93 |
self.edit_cell_enabled = False |
| 94 |
self.visible_row_dividers = {} |
| 95 |
self.row_width_resize_bbox = () |
| 96 |
self.rsz_w = None |
| 97 |
self.rsz_h = None |
| 98 |
self.currently_resizing_width = False |
| 99 |
self.currently_resizing_height = False |
| 100 |
self.ri_rc_popup_menu = None |
| 101 |
self.dropdown = DropdownStorage() |
| 102 |
self.text_editor = TextEditorStorage() |
| 103 |
|
| 104 |
self.disp_text = {} |
| 105 |
self.disp_high = {} |
| 106 |
self.disp_grid = {} |
| 107 |
self.disp_fill_sels = {} |
| 108 |
self.disp_bord_sels = {} |
| 109 |
self.disp_resize_lines = {} |
| 110 |
self.disp_dropdown = {} |
| 111 |
self.disp_checkbox = {} |
| 112 |
self.disp_tree_arrow = {} |
| 113 |
self.disp_boxes = set() |
| 114 |
self.hidd_text = {} |
| 115 |
self.hidd_high = {} |
| 116 |
self.hidd_grid = {} |
| 117 |
self.hidd_fill_sels = {} |
| 118 |
self.hidd_bord_sels = {} |
| 119 |
self.hidd_resize_lines = {} |
| 120 |
self.hidd_dropdown = {} |
| 121 |
self.hidd_checkbox = {} |
| 122 |
self.hidd_tree_arrow = {} |
| 123 |
self.hidd_boxes = set() |
| 124 |
|
| 125 |
self.align = kwargs["row_index_align"] |
| 126 |
|
| 127 |
self.tree_reset() |
| 128 |
self.basic_bindings() |
| 129 |
|
| 130 |
def event_generate(self, *args, **kwargs) -> None: |
| 131 |
for arg in args: |
| 132 |
if self.MT and arg in self.MT.event_linker: |
| 133 |
self.MT.event_linker[arg]() |
| 134 |
else: |
| 135 |
super().event_generate(*args, **kwargs) |
| 136 |
|
| 137 |
def basic_bindings(self, enable: bool = True) -> None: |
| 138 |
if enable: |
| 139 |
self.bind("<Motion>", self.mouse_motion) |
| 140 |
self.bind("<ButtonPress-1>", self.b1_press) |
| 141 |
self.bind("<B1-Motion>", self.b1_motion) |
| 142 |
self.bind("<ButtonRelease-1>", self.b1_release) |
| 143 |
self.bind("<Double-Button-1>", self.double_b1) |
| 144 |
self.bind(rc_binding, self.rc) |
| 145 |
else: |
| 146 |
self.unbind("<Motion>") |
| 147 |
self.unbind("<ButtonPress-1>") |
| 148 |
self.unbind("<B1-Motion>") |
| 149 |
self.unbind("<ButtonRelease-1>") |
| 150 |
self.unbind("<Double-Button-1>") |
| 151 |
self.unbind(rc_binding) |
| 152 |
|
| 153 |
def set_width(self, new_width: int, set_TL: bool = False, recreate_selection_boxes: bool = True) -> None: |
| 154 |
try: |
| 155 |
self.config(width=new_width) |
| 156 |
except Exception: |
| 157 |
return |
| 158 |
if set_TL: |
| 159 |
self.TL.set_dimensions(new_w=new_width) |
| 160 |
if isinstance(self.current_width, int) and new_width > self.current_width and recreate_selection_boxes: |
| 161 |
self.MT.recreate_all_selection_boxes() |
| 162 |
self.current_width = new_width |
| 163 |
|
| 164 |
def rc(self, event: Any) -> None: |
| 165 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 166 |
self.focus_set() |
| 167 |
popup_menu = None |
| 168 |
if self.MT.identify_row(y=event.y, allow_end=False) is None: |
| 169 |
self.MT.deselect("all") |
| 170 |
r = len(self.MT.col_positions) - 1 |
| 171 |
if self.MT.rc_popup_menus_enabled: |
| 172 |
popup_menu = self.MT.empty_rc_popup_menu |
| 173 |
elif self.row_selection_enabled and not self.currently_resizing_width and not self.currently_resizing_height: |
| 174 |
r = self.MT.identify_row(y=event.y) |
| 175 |
if r < len(self.MT.row_positions) - 1: |
| 176 |
if self.MT.row_selected(r): |
| 177 |
if self.MT.rc_popup_menus_enabled: |
| 178 |
popup_menu = self.ri_rc_popup_menu |
| 179 |
else: |
| 180 |
if self.MT.single_selection_enabled and self.MT.rc_select_enabled: |
| 181 |
self.select_row(r, redraw=True) |
| 182 |
elif self.MT.toggle_selection_enabled and self.MT.rc_select_enabled: |
| 183 |
self.toggle_select_row(r, redraw=True) |
| 184 |
if self.MT.rc_popup_menus_enabled: |
| 185 |
popup_menu = self.ri_rc_popup_menu |
| 186 |
try_binding(self.extra_rc_func, event) |
| 187 |
if popup_menu is not None: |
| 188 |
self.popup_menu_loc = r |
| 189 |
popup_menu.tk_popup(event.x_root, event.y_root) |
| 190 |
|
| 191 |
def ctrl_b1_press(self, event: Any) -> None: |
| 192 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 193 |
if ( |
| 194 |
(self.drag_and_drop_enabled or self.row_selection_enabled) |
| 195 |
and self.MT.ctrl_select_enabled |
| 196 |
and self.rsz_h is None |
| 197 |
and self.rsz_w is None |
| 198 |
): |
| 199 |
r = self.MT.identify_row(y=event.y) |
| 200 |
if r < len(self.MT.row_positions) - 1: |
| 201 |
r_selected = self.MT.row_selected(r) |
| 202 |
if not r_selected and self.row_selection_enabled: |
| 203 |
self.being_drawn_item = True |
| 204 |
self.being_drawn_item = self.add_selection(r, set_as_current=True, run_binding_func=False) |
| 205 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 206 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 207 |
try_binding(self.ctrl_selection_binding_func, sel_event) |
| 208 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 209 |
elif r_selected: |
| 210 |
self.MT.deselect(r=r) |
| 211 |
elif not self.MT.ctrl_select_enabled: |
| 212 |
self.b1_press(event) |
| 213 |
|
| 214 |
def ctrl_shift_b1_press(self, event: Any) -> None: |
| 215 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 216 |
y = event.y |
| 217 |
r = self.MT.identify_row(y=y) |
| 218 |
if ( |
| 219 |
(self.drag_and_drop_enabled or self.row_selection_enabled) |
| 220 |
and self.MT.ctrl_select_enabled |
| 221 |
and self.rsz_h is None |
| 222 |
and self.rsz_w is None |
| 223 |
): |
| 224 |
if r < len(self.MT.row_positions) - 1: |
| 225 |
r_selected = self.MT.row_selected(r) |
| 226 |
if not r_selected and self.row_selection_enabled: |
| 227 |
if self.MT.selected and self.MT.selected.type_ == "rows": |
| 228 |
self.being_drawn_item = self.MT.recreate_selection_box( |
| 229 |
*self.get_shift_select_box(r, self.MT.selected.row), |
| 230 |
fill_iid=self.MT.selected.fill_iid, |
| 231 |
) |
| 232 |
else: |
| 233 |
self.being_drawn_item = self.add_selection( |
| 234 |
r, |
| 235 |
run_binding_func=False, |
| 236 |
set_as_current=True, |
| 237 |
) |
| 238 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 239 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 240 |
try_binding(self.ctrl_selection_binding_func, sel_event) |
| 241 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 242 |
elif r_selected: |
| 243 |
self.dragged_row = DraggedRowColumn( |
| 244 |
dragged=r, |
| 245 |
to_move=sorted(self.MT.get_selected_rows()), |
| 246 |
) |
| 247 |
elif not self.MT.ctrl_select_enabled: |
| 248 |
self.shift_b1_press(event) |
| 249 |
|
| 250 |
def shift_b1_press(self, event: Any) -> None: |
| 251 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 252 |
r = self.MT.identify_row(y=event.y) |
| 253 |
if ( |
| 254 |
(self.drag_and_drop_enabled or self.row_selection_enabled) |
| 255 |
and self.rsz_h is None |
| 256 |
and self.rsz_w is None |
| 257 |
and r < len(self.MT.row_positions) - 1 |
| 258 |
): |
| 259 |
r_selected = self.MT.row_selected(r) |
| 260 |
if not r_selected and self.row_selection_enabled: |
| 261 |
if self.MT.selected and self.MT.selected.type_ == "rows": |
| 262 |
r_to_sel, c_to_sel = self.MT.selected.row, self.MT.selected.column |
| 263 |
self.MT.deselect("all", redraw=False) |
| 264 |
self.being_drawn_item = self.MT.create_selection_box( |
| 265 |
*self.get_shift_select_box(r, r_to_sel), "rows" |
| 266 |
) |
| 267 |
self.MT.set_currently_selected(r_to_sel, c_to_sel, self.being_drawn_item) |
| 268 |
else: |
| 269 |
self.being_drawn_item = self.select_row(r, run_binding_func=False) |
| 270 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 271 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 272 |
try_binding(self.shift_selection_binding_func, sel_event) |
| 273 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 274 |
elif r_selected: |
| 275 |
self.dragged_row = DraggedRowColumn( |
| 276 |
dragged=r, |
| 277 |
to_move=sorted(self.MT.get_selected_rows()), |
| 278 |
) |
| 279 |
|
| 280 |
def get_shift_select_box(self, r: int, min_r: int) -> tuple[int, int, int, int, str]: |
| 281 |
if r >= min_r: |
| 282 |
return min_r, 0, r + 1, len(self.MT.col_positions) - 1 |
| 283 |
elif r < min_r: |
| 284 |
return r, 0, min_r + 1, len(self.MT.col_positions) - 1 |
| 285 |
|
| 286 |
def create_resize_line( |
| 287 |
self, |
| 288 |
x1: int, |
| 289 |
y1: int, |
| 290 |
x2: int, |
| 291 |
y2: int, |
| 292 |
width: int, |
| 293 |
fill: str, |
| 294 |
tag: str | tuple[str], |
| 295 |
) -> None: |
| 296 |
if self.hidd_resize_lines: |
| 297 |
t, sh = self.hidd_resize_lines.popitem() |
| 298 |
self.coords(t, x1, y1, x2, y2) |
| 299 |
if sh: |
| 300 |
self.itemconfig(t, width=width, fill=fill, tag=tag) |
| 301 |
else: |
| 302 |
self.itemconfig(t, width=width, fill=fill, tag=tag, state="normal") |
| 303 |
self.lift(t) |
| 304 |
else: |
| 305 |
t = self.create_line(x1, y1, x2, y2, width=width, fill=fill, tag=tag) |
| 306 |
self.disp_resize_lines[t] = True |
| 307 |
|
| 308 |
def delete_resize_lines(self) -> None: |
| 309 |
self.hidd_resize_lines.update(self.disp_resize_lines) |
| 310 |
self.disp_resize_lines = {} |
| 311 |
for t, sh in self.hidd_resize_lines.items(): |
| 312 |
if sh: |
| 313 |
self.itemconfig(t, tags=("",), state="hidden") |
| 314 |
self.hidd_resize_lines[t] = False |
| 315 |
|
| 316 |
def check_mouse_position_height_resizers(self, x: int, y: int) -> int | None: |
| 317 |
for r, (x1, y1, x2, y2) in self.visible_row_dividers.items(): |
| 318 |
if x >= x1 and y >= y1 and x <= x2 and y <= y2: |
| 319 |
return r |
| 320 |
|
| 321 |
def mouse_motion(self, event: Any) -> None: |
| 322 |
if not self.currently_resizing_height and not self.currently_resizing_width: |
| 323 |
x = self.canvasx(event.x) |
| 324 |
y = self.canvasy(event.y) |
| 325 |
mouse_over_resize = False |
| 326 |
mouse_over_selected = False |
| 327 |
if self.height_resizing_enabled and not mouse_over_resize: |
| 328 |
r = self.check_mouse_position_height_resizers(x, y) |
| 329 |
if r is not None: |
| 330 |
self.rsz_h, mouse_over_resize = r, True |
| 331 |
if self.MT.current_cursor != "sb_v_double_arrow": |
| 332 |
self.config(cursor="sb_v_double_arrow") |
| 333 |
self.MT.current_cursor = "sb_v_double_arrow" |
| 334 |
else: |
| 335 |
self.rsz_h = None |
| 336 |
if ( |
| 337 |
self.width_resizing_enabled |
| 338 |
and not mouse_over_resize |
| 339 |
and self.ops.auto_resize_row_index is not True |
| 340 |
and not ( |
| 341 |
self.ops.auto_resize_row_index == "empty" |
| 342 |
and not isinstance(self.MT._row_index, int) |
| 343 |
and not self.MT._row_index |
| 344 |
) |
| 345 |
): |
| 346 |
try: |
| 347 |
x1, y1, x2, y2 = ( |
| 348 |
self.row_width_resize_bbox[0], |
| 349 |
self.row_width_resize_bbox[1], |
| 350 |
self.row_width_resize_bbox[2], |
| 351 |
self.row_width_resize_bbox[3], |
| 352 |
) |
| 353 |
if x >= x1 and y >= y1 and x <= x2 and y <= y2: |
| 354 |
self.rsz_w, mouse_over_resize = True, True |
| 355 |
if self.MT.current_cursor != "sb_h_double_arrow": |
| 356 |
self.config(cursor="sb_h_double_arrow") |
| 357 |
self.MT.current_cursor = "sb_h_double_arrow" |
| 358 |
else: |
| 359 |
self.rsz_w = None |
| 360 |
except Exception: |
| 361 |
self.rsz_w = None |
| 362 |
if not mouse_over_resize and self.MT.row_selected(self.MT.identify_row(event, allow_end=False)): |
| 363 |
mouse_over_selected = True |
| 364 |
if self.MT.current_cursor != "hand2": |
| 365 |
self.config(cursor="hand2") |
| 366 |
self.MT.current_cursor = "hand2" |
| 367 |
if not mouse_over_resize and not mouse_over_selected: |
| 368 |
self.MT.reset_mouse_motion_creations() |
| 369 |
try_binding(self.extra_motion_func, event) |
| 370 |
|
| 371 |
def double_b1(self, event: Any): |
| 372 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 373 |
self.focus_set() |
| 374 |
if ( |
| 375 |
self.double_click_resizing_enabled |
| 376 |
and self.height_resizing_enabled |
| 377 |
and self.rsz_h is not None |
| 378 |
and not self.currently_resizing_height |
| 379 |
): |
| 380 |
row = self.rsz_h - 1 |
| 381 |
old_height = self.MT.row_positions[self.rsz_h] - self.MT.row_positions[self.rsz_h - 1] |
| 382 |
new_height = self.set_row_height(row) |
| 383 |
self.MT.allow_auto_resize_rows = False |
| 384 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 385 |
if self.row_height_resize_func is not None and old_height != new_height: |
| 386 |
self.row_height_resize_func( |
| 387 |
event_dict( |
| 388 |
name="resize", |
| 389 |
sheet=self.PAR.name, |
| 390 |
resized_rows={row: {"old_size": old_height, "new_size": new_height}}, |
| 391 |
) |
| 392 |
) |
| 393 |
elif self.width_resizing_enabled and self.rsz_h is None and self.rsz_w is True: |
| 394 |
self.set_width_of_index_to_text() |
| 395 |
elif (self.row_selection_enabled or self.ops.treeview) and self.rsz_h is None and self.rsz_w is None: |
| 396 |
r = self.MT.identify_row(y=event.y) |
| 397 |
if r < len(self.MT.row_positions) - 1: |
| 398 |
iid = self.event_over_tree_arrow(r, self.canvasy(event.y), event.x) |
| 399 |
if self.row_selection_enabled: |
| 400 |
if self.MT.single_selection_enabled: |
| 401 |
self.select_row(r, redraw=iid is None) |
| 402 |
elif self.MT.toggle_selection_enabled: |
| 403 |
self.toggle_select_row(r, redraw=iid is None) |
| 404 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 405 |
if ( |
| 406 |
self.get_cell_kwargs(datarn, key="dropdown") |
| 407 |
or self.get_cell_kwargs(datarn, key="checkbox") |
| 408 |
or self.edit_cell_enabled |
| 409 |
): |
| 410 |
self.open_cell(event) |
| 411 |
elif iid is not None: |
| 412 |
self.PAR.item(iid, open_=iid not in self.tree_open_ids) |
| 413 |
self.rsz_h = None |
| 414 |
self.mouse_motion(event) |
| 415 |
try_binding(self.extra_double_b1_func, event) |
| 416 |
|
| 417 |
def b1_press(self, event: Any): |
| 418 |
self.MT.unbind("<MouseWheel>") |
| 419 |
self.focus_set() |
| 420 |
self.closed_dropdown = self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 421 |
x = self.canvasx(event.x) |
| 422 |
y = self.canvasy(event.y) |
| 423 |
r = self.MT.identify_row(y=event.y) |
| 424 |
self.b1_pressed_loc = r |
| 425 |
if self.check_mouse_position_height_resizers(x, y) is None: |
| 426 |
self.rsz_h = None |
| 427 |
if ( |
| 428 |
not x >= self.row_width_resize_bbox[0] |
| 429 |
and y >= self.row_width_resize_bbox[1] |
| 430 |
and x <= self.row_width_resize_bbox[2] |
| 431 |
and y <= self.row_width_resize_bbox[3] |
| 432 |
): |
| 433 |
self.rsz_w = None |
| 434 |
if self.height_resizing_enabled and self.rsz_h is not None: |
| 435 |
self.currently_resizing_height = True |
| 436 |
y = self.MT.row_positions[self.rsz_h] |
| 437 |
line2y = self.MT.row_positions[self.rsz_h - 1] |
| 438 |
x1, y1, x2, y2 = self.MT.get_canvas_visible_area() |
| 439 |
self.create_resize_line( |
| 440 |
0, |
| 441 |
y, |
| 442 |
self.current_width, |
| 443 |
y, |
| 444 |
width=1, |
| 445 |
fill=self.ops.resizing_line_fg, |
| 446 |
tag=("rh", "rhl"), |
| 447 |
) |
| 448 |
self.MT.create_resize_line(x1, y, x2, y, width=1, fill=self.ops.resizing_line_fg, tag=("rh", "rhl")) |
| 449 |
self.create_resize_line( |
| 450 |
0, |
| 451 |
line2y, |
| 452 |
self.current_width, |
| 453 |
line2y, |
| 454 |
width=1, |
| 455 |
fill=self.ops.resizing_line_fg, |
| 456 |
tag=("rh", "rhl2"), |
| 457 |
) |
| 458 |
self.MT.create_resize_line( |
| 459 |
x1, line2y, x2, line2y, width=1, fill=self.ops.resizing_line_fg, tag=("rh", "rhl2") |
| 460 |
) |
| 461 |
elif self.width_resizing_enabled and self.rsz_h is None and self.rsz_w is True: |
| 462 |
self.currently_resizing_width = True |
| 463 |
elif self.MT.identify_row(y=event.y, allow_end=False) is None: |
| 464 |
self.MT.deselect("all") |
| 465 |
elif self.row_selection_enabled and self.rsz_h is None and self.rsz_w is None: |
| 466 |
r = self.MT.identify_row(y=event.y) |
| 467 |
if r < len(self.MT.row_positions) - 1: |
| 468 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 469 |
if ( |
| 470 |
self.MT.row_selected(r) |
| 471 |
and not self.event_over_dropdown(r, datarn, event, y) |
| 472 |
and not self.event_over_checkbox(r, datarn, event, y) |
| 473 |
): |
| 474 |
self.dragged_row = DraggedRowColumn( |
| 475 |
dragged=r, |
| 476 |
to_move=sorted(self.MT.get_selected_rows()), |
| 477 |
) |
| 478 |
else: |
| 479 |
if self.MT.single_selection_enabled: |
| 480 |
self.being_drawn_item = True |
| 481 |
self.being_drawn_item = self.select_row(r, redraw=True) |
| 482 |
elif self.MT.toggle_selection_enabled: |
| 483 |
self.toggle_select_row(r, redraw=True) |
| 484 |
try_binding(self.extra_b1_press_func, event) |
| 485 |
|
| 486 |
def b1_motion(self, event: Any): |
| 487 |
x1, y1, x2, y2 = self.MT.get_canvas_visible_area() |
| 488 |
if self.height_resizing_enabled and self.rsz_h is not None and self.currently_resizing_height: |
| 489 |
y = self.canvasy(event.y) |
| 490 |
size = y - self.MT.row_positions[self.rsz_h - 1] |
| 491 |
if size >= self.MT.min_row_height and size < self.ops.max_row_height: |
| 492 |
self.hide_resize_and_ctrl_lines(ctrl_lines=False) |
| 493 |
line2y = self.MT.row_positions[self.rsz_h - 1] |
| 494 |
self.create_resize_line( |
| 495 |
0, |
| 496 |
y, |
| 497 |
self.current_width, |
| 498 |
y, |
| 499 |
width=1, |
| 500 |
fill=self.ops.resizing_line_fg, |
| 501 |
tag=("rh", "rhl"), |
| 502 |
) |
| 503 |
self.MT.create_resize_line(x1, y, x2, y, width=1, fill=self.ops.resizing_line_fg, tag=("rh", "rhl")) |
| 504 |
self.create_resize_line( |
| 505 |
0, |
| 506 |
line2y, |
| 507 |
self.current_width, |
| 508 |
line2y, |
| 509 |
width=1, |
| 510 |
fill=self.ops.resizing_line_fg, |
| 511 |
tag=("rh", "rhl2"), |
| 512 |
) |
| 513 |
self.MT.create_resize_line( |
| 514 |
x1, |
| 515 |
line2y, |
| 516 |
x2, |
| 517 |
line2y, |
| 518 |
width=1, |
| 519 |
fill=self.ops.resizing_line_fg, |
| 520 |
tag=("rh", "rhl2"), |
| 521 |
) |
| 522 |
self.drag_height_resize() |
| 523 |
elif self.width_resizing_enabled and self.rsz_w is not None and self.currently_resizing_width: |
| 524 |
evx = event.x |
| 525 |
if evx > self.current_width: |
| 526 |
if evx > self.ops.max_index_width: |
| 527 |
evx = int(self.ops.max_index_width) |
| 528 |
self.drag_width_resize(evx) |
| 529 |
else: |
| 530 |
if evx < self.ops.min_column_width: |
| 531 |
evx = self.ops.min_column_width |
| 532 |
self.drag_width_resize(evx) |
| 533 |
elif ( |
| 534 |
self.drag_and_drop_enabled |
| 535 |
and self.row_selection_enabled |
| 536 |
and self.MT.anything_selected(exclude_cells=True, exclude_columns=True) |
| 537 |
and self.rsz_h is None |
| 538 |
and self.rsz_w is None |
| 539 |
and self.dragged_row is not None |
| 540 |
): |
| 541 |
y = self.canvasy(event.y) |
| 542 |
if y > 0: |
| 543 |
self.show_drag_and_drop_indicators( |
| 544 |
self.drag_and_drop_motion(event), |
| 545 |
x1, |
| 546 |
x2, |
| 547 |
self.dragged_row.to_move, |
| 548 |
) |
| 549 |
elif ( |
| 550 |
self.MT.drag_selection_enabled and self.row_selection_enabled and self.rsz_h is None and self.rsz_w is None |
| 551 |
): |
| 552 |
need_redraw = False |
| 553 |
end_row = self.MT.identify_row(y=event.y) |
| 554 |
if end_row < len(self.MT.row_positions) - 1 and self.MT.selected: |
| 555 |
if self.MT.selected.type_ == "rows": |
| 556 |
box = self.get_b1_motion_box(self.MT.selected.row, end_row) |
| 557 |
if ( |
| 558 |
box is not None |
| 559 |
and self.being_drawn_item is not None |
| 560 |
and self.MT.coords_and_type(self.being_drawn_item) != box |
| 561 |
): |
| 562 |
if box[2] - box[0] != 1: |
| 563 |
self.being_drawn_item = self.MT.recreate_selection_box( |
| 564 |
*box[:-1], |
| 565 |
fill_iid=self.MT.selected.fill_iid, |
| 566 |
) |
| 567 |
else: |
| 568 |
self.being_drawn_item = self.select_row(self.MT.selected.row, run_binding_func=False) |
| 569 |
need_redraw = True |
| 570 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 571 |
try_binding(self.drag_selection_binding_func, sel_event) |
| 572 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 573 |
if self.scroll_if_event_offscreen(event): |
| 574 |
need_redraw = True |
| 575 |
if need_redraw: |
| 576 |
self.MT.main_table_redraw_grid_and_text(redraw_header=False, redraw_row_index=True) |
| 577 |
try_binding(self.extra_b1_motion_func, event) |
| 578 |
|
| 579 |
def get_b1_motion_box(self, start_row: int, end_row: int) -> tuple[int, int, int, int, Literal["rows"]]: |
| 580 |
if end_row >= start_row: |
| 581 |
return start_row, 0, end_row + 1, len(self.MT.col_positions) - 1, "rows" |
| 582 |
elif end_row < start_row: |
| 583 |
return end_row, 0, start_row + 1, len(self.MT.col_positions) - 1, "rows" |
| 584 |
|
| 585 |
def ctrl_b1_motion(self, event: Any) -> None: |
| 586 |
x1, y1, x2, y2 = self.MT.get_canvas_visible_area() |
| 587 |
if ( |
| 588 |
self.drag_and_drop_enabled |
| 589 |
and self.row_selection_enabled |
| 590 |
and self.rsz_h is None |
| 591 |
and self.rsz_w is None |
| 592 |
and self.dragged_row is not None |
| 593 |
and self.MT.anything_selected(exclude_cells=True, exclude_columns=True) |
| 594 |
): |
| 595 |
y = self.canvasy(event.y) |
| 596 |
if y > 0: |
| 597 |
self.show_drag_and_drop_indicators( |
| 598 |
self.drag_and_drop_motion(event), |
| 599 |
x1, |
| 600 |
x2, |
| 601 |
self.dragged_row.to_move, |
| 602 |
) |
| 603 |
elif ( |
| 604 |
self.MT.ctrl_select_enabled |
| 605 |
and self.row_selection_enabled |
| 606 |
and self.MT.drag_selection_enabled |
| 607 |
and self.rsz_h is None |
| 608 |
and self.rsz_w is None |
| 609 |
): |
| 610 |
need_redraw = False |
| 611 |
end_row = self.MT.identify_row(y=event.y) |
| 612 |
if end_row < len(self.MT.row_positions) - 1 and self.MT.selected: |
| 613 |
if self.MT.selected.type_ == "rows": |
| 614 |
box = self.get_b1_motion_box(self.MT.selected.row, end_row) |
| 615 |
if ( |
| 616 |
box is not None |
| 617 |
and self.being_drawn_item is not None |
| 618 |
and self.MT.coords_and_type(self.being_drawn_item) != box |
| 619 |
): |
| 620 |
if box[2] - box[0] != 1: |
| 621 |
self.being_drawn_item = self.MT.recreate_selection_box( |
| 622 |
*box[:-1], |
| 623 |
self.MT.selected.fill_iid, |
| 624 |
) |
| 625 |
else: |
| 626 |
self.MT.hide_selection_box(self.MT.selected.fill_iid) |
| 627 |
self.being_drawn_item = self.add_selection(box[0], run_binding_func=False) |
| 628 |
need_redraw = True |
| 629 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 630 |
try_binding(self.drag_selection_binding_func, sel_event) |
| 631 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 632 |
if self.scroll_if_event_offscreen(event): |
| 633 |
need_redraw = True |
| 634 |
if need_redraw: |
| 635 |
self.MT.main_table_redraw_grid_and_text(redraw_header=False, redraw_row_index=True) |
| 636 |
elif not self.MT.ctrl_select_enabled: |
| 637 |
self.b1_motion(event) |
| 638 |
|
| 639 |
def drag_and_drop_motion(self, event: Any) -> float: |
| 640 |
y = event.y |
| 641 |
hend = self.winfo_height() |
| 642 |
ycheck = self.yview() |
| 643 |
if y >= hend - 0 and len(ycheck) > 1 and ycheck[1] < 1: |
| 644 |
if y >= hend + 15: |
| 645 |
self.MT.yview_scroll(2, "units") |
| 646 |
self.yview_scroll(2, "units") |
| 647 |
else: |
| 648 |
self.MT.yview_scroll(1, "units") |
| 649 |
self.yview_scroll(1, "units") |
| 650 |
self.fix_yview() |
| 651 |
self.MT.y_move_synced_scrolls("moveto", self.MT.yview()[0]) |
| 652 |
self.MT.main_table_redraw_grid_and_text(redraw_row_index=True) |
| 653 |
elif y <= 0 and len(ycheck) > 1 and ycheck[0] > 0: |
| 654 |
if y >= -15: |
| 655 |
self.MT.yview_scroll(-1, "units") |
| 656 |
self.yview_scroll(-1, "units") |
| 657 |
else: |
| 658 |
self.MT.yview_scroll(-2, "units") |
| 659 |
self.yview_scroll(-2, "units") |
| 660 |
self.fix_yview() |
| 661 |
self.MT.y_move_synced_scrolls("moveto", self.MT.yview()[0]) |
| 662 |
self.MT.main_table_redraw_grid_and_text(redraw_row_index=True) |
| 663 |
row = self.MT.identify_row(y=y) |
| 664 |
if row == len(self.MT.row_positions) - 1: |
| 665 |
row -= 1 |
| 666 |
if row >= self.dragged_row.to_move[0] and row <= self.dragged_row.to_move[-1]: |
| 667 |
if is_contiguous(self.dragged_row.to_move): |
| 668 |
return self.MT.row_positions[self.dragged_row.to_move[0]] |
| 669 |
return self.MT.row_positions[row] |
| 670 |
elif row > self.dragged_row.to_move[-1]: |
| 671 |
return self.MT.row_positions[row + 1] |
| 672 |
return self.MT.row_positions[row] |
| 673 |
|
| 674 |
def show_drag_and_drop_indicators( |
| 675 |
self, |
| 676 |
ypos: float, |
| 677 |
x1: float, |
| 678 |
x2: float, |
| 679 |
rows: Sequence[int], |
| 680 |
) -> None: |
| 681 |
self.hide_resize_and_ctrl_lines() |
| 682 |
self.create_resize_line( |
| 683 |
0, |
| 684 |
ypos, |
| 685 |
self.current_width, |
| 686 |
ypos, |
| 687 |
width=3, |
| 688 |
fill=self.ops.drag_and_drop_bg, |
| 689 |
tag="move_rows", |
| 690 |
) |
| 691 |
self.MT.create_resize_line(x1, ypos, x2, ypos, width=3, fill=self.ops.drag_and_drop_bg, tag="move_rows") |
| 692 |
for boxst, boxend in consecutive_ranges(rows): |
| 693 |
self.MT.show_ctrl_outline( |
| 694 |
start_cell=(0, boxst), |
| 695 |
end_cell=(len(self.MT.col_positions) - 1, boxend), |
| 696 |
dash=(), |
| 697 |
outline=self.ops.drag_and_drop_bg, |
| 698 |
delete_on_timer=False, |
| 699 |
) |
| 700 |
|
| 701 |
def hide_resize_and_ctrl_lines(self, ctrl_lines: bool = True) -> None: |
| 702 |
self.delete_resize_lines() |
| 703 |
self.MT.delete_resize_lines() |
| 704 |
if ctrl_lines: |
| 705 |
self.MT.delete_ctrl_outlines() |
| 706 |
|
| 707 |
def scroll_if_event_offscreen(self, event: Any) -> bool: |
| 708 |
ycheck = self.yview() |
| 709 |
need_redraw = False |
| 710 |
if event.y > self.winfo_height() and len(ycheck) > 1 and ycheck[1] < 1: |
| 711 |
try: |
| 712 |
self.MT.yview_scroll(1, "units") |
| 713 |
self.yview_scroll(1, "units") |
| 714 |
except Exception: |
| 715 |
pass |
| 716 |
self.fix_yview() |
| 717 |
self.MT.y_move_synced_scrolls("moveto", self.MT.yview()[0]) |
| 718 |
need_redraw = True |
| 719 |
elif event.y < 0 and self.canvasy(self.winfo_height()) > 0 and ycheck and ycheck[0] > 0: |
| 720 |
try: |
| 721 |
self.yview_scroll(-1, "units") |
| 722 |
self.MT.yview_scroll(-1, "units") |
| 723 |
except Exception: |
| 724 |
pass |
| 725 |
self.fix_yview() |
| 726 |
self.MT.y_move_synced_scrolls("moveto", self.MT.yview()[0]) |
| 727 |
need_redraw = True |
| 728 |
return need_redraw |
| 729 |
|
| 730 |
def fix_yview(self) -> None: |
| 731 |
ycheck = self.yview() |
| 732 |
if ycheck and ycheck[0] < 0: |
| 733 |
self.MT.set_yviews("moveto", 0) |
| 734 |
if len(ycheck) > 1 and ycheck[1] > 1: |
| 735 |
self.MT.set_yviews("moveto", 1) |
| 736 |
|
| 737 |
def event_over_dropdown(self, r: int, datarn: int, event: Any, canvasy: float) -> bool: |
| 738 |
return ( |
| 739 |
canvasy < self.MT.row_positions[r] + self.MT.index_txt_height |
| 740 |
and self.get_cell_kwargs(datarn, key="dropdown") |
| 741 |
and event.x > self.current_width - self.MT.index_txt_height - 4 |
| 742 |
) |
| 743 |
|
| 744 |
def event_over_checkbox(self, r: int, datarn: int, event: Any, canvasy: float) -> bool: |
| 745 |
return ( |
| 746 |
canvasy < self.MT.row_positions[r] + self.MT.index_txt_height |
| 747 |
and self.get_cell_kwargs(datarn, key="checkbox") |
| 748 |
and event.x < self.MT.index_txt_height + 4 |
| 749 |
) |
| 750 |
|
| 751 |
def drag_width_resize(self, width: int) -> None: |
| 752 |
self.set_width(width, set_TL=True) |
| 753 |
self.MT.main_table_redraw_grid_and_text(redraw_header=False, redraw_row_index=True, redraw_table=False) |
| 754 |
|
| 755 |
def drag_height_resize(self) -> None: |
| 756 |
new_row_pos = int(self.coords("rhl")[1]) |
| 757 |
old_height = self.MT.row_positions[self.rsz_h] - self.MT.row_positions[self.rsz_h - 1] |
| 758 |
size = new_row_pos - self.MT.row_positions[self.rsz_h - 1] |
| 759 |
if size < self.MT.min_row_height: |
| 760 |
new_row_pos = ceil(self.MT.row_positions[self.rsz_h - 1] + self.MT.min_row_height) |
| 761 |
elif size > self.ops.max_row_height: |
| 762 |
new_row_pos = int(self.MT.row_positions[self.rsz_h - 1] + self.ops.max_row_height) |
| 763 |
increment = new_row_pos - self.MT.row_positions[self.rsz_h] |
| 764 |
self.MT.row_positions[self.rsz_h + 1 :] = [ |
| 765 |
e + increment for e in islice(self.MT.row_positions, self.rsz_h + 1, None) |
| 766 |
] |
| 767 |
self.MT.row_positions[self.rsz_h] = new_row_pos |
| 768 |
new_height = self.MT.row_positions[self.rsz_h] - self.MT.row_positions[self.rsz_h - 1] |
| 769 |
self.MT.allow_auto_resize_rows = False |
| 770 |
self.MT.recreate_all_selection_boxes() |
| 771 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True, set_scrollregion=False) |
| 772 |
if self.row_height_resize_func is not None and old_height != new_height: |
| 773 |
self.row_height_resize_func( |
| 774 |
event_dict( |
| 775 |
name="resize", |
| 776 |
sheet=self.PAR.name, |
| 777 |
resized_rows={self.rsz_h - 1: {"old_size": old_height, "new_size": new_height}}, |
| 778 |
) |
| 779 |
) |
| 780 |
|
| 781 |
def b1_release(self, event: Any) -> None: |
| 782 |
to_hide = self.being_drawn_item |
| 783 |
if self.being_drawn_item is not None and (to_sel := self.MT.coords_and_type(self.being_drawn_item)): |
| 784 |
r_to_sel, c_to_sel = self.MT.selected.row, self.MT.selected.column |
| 785 |
self.being_drawn_item = None |
| 786 |
self.MT.set_currently_selected( |
| 787 |
r_to_sel, |
| 788 |
c_to_sel, |
| 789 |
item=self.MT.create_selection_box(*to_sel, set_current=False), |
| 790 |
run_binding=False, |
| 791 |
) |
| 792 |
self.MT.hide_selection_box(to_hide) |
| 793 |
self.MT.bind("<MouseWheel>", self.MT.mousewheel) |
| 794 |
if self.height_resizing_enabled and self.rsz_h is not None and self.currently_resizing_height: |
| 795 |
self.drag_height_resize() |
| 796 |
self.hide_resize_and_ctrl_lines(ctrl_lines=False) |
| 797 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 798 |
elif ( |
| 799 |
self.drag_and_drop_enabled |
| 800 |
and self.MT.anything_selected(exclude_cells=True, exclude_columns=True) |
| 801 |
and self.row_selection_enabled |
| 802 |
and self.rsz_h is None |
| 803 |
and self.rsz_w is None |
| 804 |
and self.dragged_row is not None |
| 805 |
and self.find_withtag("move_rows") |
| 806 |
): |
| 807 |
self.hide_resize_and_ctrl_lines() |
| 808 |
r = self.MT.identify_row(y=event.y) |
| 809 |
totalrows = len(self.dragged_row.to_move) |
| 810 |
if ( |
| 811 |
r is not None |
| 812 |
and totalrows != len(self.MT.row_positions) - 1 |
| 813 |
and not ( |
| 814 |
r >= self.dragged_row.to_move[0] |
| 815 |
and r <= self.dragged_row.to_move[-1] |
| 816 |
and is_contiguous(self.dragged_row.to_move) |
| 817 |
) |
| 818 |
): |
| 819 |
if r > self.dragged_row.to_move[-1]: |
| 820 |
r += 1 |
| 821 |
if r > len(self.MT.row_positions) - 1: |
| 822 |
r = len(self.MT.row_positions) - 1 |
| 823 |
event_data = self.MT.new_event_dict("move_rows", state=True) |
| 824 |
event_data["value"] = r |
| 825 |
if try_binding(self.ri_extra_begin_drag_drop_func, event_data, "begin_move_rows"): |
| 826 |
data_new_idxs, disp_new_idxs, event_data = self.MT.move_rows_adjust_options_dict( |
| 827 |
*self.MT.get_args_for_move_rows( |
| 828 |
move_to=r, |
| 829 |
to_move=self.dragged_row.to_move, |
| 830 |
), |
| 831 |
move_data=self.ops.row_drag_and_drop_perform, |
| 832 |
move_heights=self.ops.row_drag_and_drop_perform, |
| 833 |
event_data=event_data, |
| 834 |
) |
| 835 |
if data_new_idxs and disp_new_idxs: |
| 836 |
event_data["moved"]["rows"] = { |
| 837 |
"data": data_new_idxs, |
| 838 |
"displayed": disp_new_idxs, |
| 839 |
} |
| 840 |
if self.MT.undo_enabled: |
| 841 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 842 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 843 |
try_binding(self.ri_extra_end_drag_drop_func, event_data, "end_move_rows") |
| 844 |
self.MT.sheet_modified(event_data) |
| 845 |
elif self.b1_pressed_loc is not None and self.rsz_w is None and self.rsz_h is None: |
| 846 |
r = self.MT.identify_row(y=event.y) |
| 847 |
if ( |
| 848 |
r is not None |
| 849 |
and r < len(self.MT.row_positions) - 1 |
| 850 |
and r == self.b1_pressed_loc |
| 851 |
and self.b1_pressed_loc != self.closed_dropdown |
| 852 |
): |
| 853 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 854 |
canvasy = self.canvasy(event.y) |
| 855 |
if self.event_over_dropdown(r, datarn, event, canvasy) or self.event_over_checkbox( |
| 856 |
r, datarn, event, canvasy |
| 857 |
): |
| 858 |
self.open_cell(event) |
| 859 |
elif (iid := self.event_over_tree_arrow(r, canvasy, event.x)) is not None: |
| 860 |
if self.MT.selection_boxes: |
| 861 |
self.select_row(r, ext=True, redraw=False) |
| 862 |
self.PAR.item(iid, open_=iid not in self.tree_open_ids) |
| 863 |
else: |
| 864 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 865 |
self.b1_pressed_loc = None |
| 866 |
self.closed_dropdown = None |
| 867 |
self.dragged_row = None |
| 868 |
self.currently_resizing_width = False |
| 869 |
self.currently_resizing_height = False |
| 870 |
self.rsz_w = None |
| 871 |
self.rsz_h = None |
| 872 |
self.mouse_motion(event) |
| 873 |
try_binding(self.extra_b1_release_func, event) |
| 874 |
|
| 875 |
def event_over_tree_arrow( |
| 876 |
self, |
| 877 |
r: int, |
| 878 |
canvasy: float, |
| 879 |
eventx: int, |
| 880 |
) -> bool: |
| 881 |
if self.ops.treeview and ( |
| 882 |
canvasy < self.MT.row_positions[r] + self.MT.index_txt_height + 5 |
| 883 |
and isinstance(self.MT._row_index, list) |
| 884 |
and (datarn := self.MT.datarn(r)) < len(self.MT._row_index) |
| 885 |
and eventx |
| 886 |
< (indent := self.get_iid_indent((iid := self.MT._row_index[datarn].iid))) + self.MT.index_txt_height + 4 |
| 887 |
and eventx >= indent + 1 |
| 888 |
): |
| 889 |
return iid |
| 890 |
return None |
| 891 |
|
| 892 |
def _sort_rows( |
| 893 |
self, |
| 894 |
event: tk.Event | None = None, |
| 895 |
rows: Iterator[int] | None = None, |
| 896 |
reverse: bool = False, |
| 897 |
validation: bool = True, |
| 898 |
key: Callable | None = None, |
| 899 |
undo: bool = True, |
| 900 |
) -> EventDataDict: |
| 901 |
if rows is None: |
| 902 |
rows = self.MT.get_selected_rows() |
| 903 |
if not rows: |
| 904 |
rows = list(range(0, len(self.MT.row_positions) - 1)) |
| 905 |
event_data = self.MT.new_event_dict("edit_table") |
| 906 |
try_binding(self.MT.extra_begin_sort_cells_func, event_data) |
| 907 |
if key is None: |
| 908 |
key = self.PAR.ops.sort_key |
| 909 |
for r in rows: |
| 910 |
datarn = self.MT.datarn(r) |
| 911 |
for c, val in enumerate(sort_row(self.MT.data[datarn], reverse=reverse, key=key)): |
| 912 |
if ( |
| 913 |
not self.MT.edit_validation_func |
| 914 |
or not validation |
| 915 |
or ( |
| 916 |
self.MT.edit_validation_func |
| 917 |
and (val := self.MT.edit_validation_func(mod_event_val(event_data, val, (datarn, c)))) |
| 918 |
is not None |
| 919 |
) |
| 920 |
): |
| 921 |
event_data = self.MT.event_data_set_cell( |
| 922 |
datarn=datarn, |
| 923 |
datacn=c, |
| 924 |
value=val, |
| 925 |
event_data=event_data, |
| 926 |
) |
| 927 |
event_data = self.MT.bulk_edit_validation(event_data) |
| 928 |
if event_data["cells"]["table"]: |
| 929 |
if undo and self.MT.undo_enabled: |
| 930 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 931 |
try_binding(self.MT.extra_end_sort_cells_func, event_data, "end_edit_table") |
| 932 |
self.MT.sheet_modified(event_data) |
| 933 |
self.PAR.emit_event("<<SheetModified>>", event_data) |
| 934 |
self.MT.refresh() |
| 935 |
return event_data |
| 936 |
|
| 937 |
def _sort_columns_by_row( |
| 938 |
self, |
| 939 |
event: tk.Event | None = None, |
| 940 |
row: int | None = None, |
| 941 |
reverse: bool = False, |
| 942 |
key: Callable | None = None, |
| 943 |
undo: bool = True, |
| 944 |
) -> EventDataDict: |
| 945 |
event_data = self.MT.new_event_dict("move_columns", state=True) |
| 946 |
if not self.MT.data: |
| 947 |
return event_data |
| 948 |
if row is None: |
| 949 |
if not self.MT.selected: |
| 950 |
return event_data |
| 951 |
row = self.MT.datarn(self.MT.selected.row) |
| 952 |
if try_binding(self.ri_extra_begin_sort_cols_func, event_data, "begin_move_columns"): |
| 953 |
if key is None: |
| 954 |
key = self.PAR.ops.sort_key |
| 955 |
sorted_indices, data_new_idxs = sort_columns_by_row(self.MT.data, row=row, reverse=reverse, key=key) |
| 956 |
disp_new_idxs = {} |
| 957 |
if self.MT.all_columns_displayed: |
| 958 |
disp_new_idxs = data_new_idxs |
| 959 |
else: |
| 960 |
col_ctr = 0 |
| 961 |
|
| 962 |
for old_idx in sorted_indices: |
| 963 |
if (idx := try_b_index(self.MT.displayed_columns, old_idx)) is not None: |
| 964 |
disp_new_idxs[idx] = col_ctr |
| 965 |
col_ctr += 1 |
| 966 |
data_new_idxs, disp_new_idxs, _ = self.PAR.mapping_move_columns( |
| 967 |
data_new_idxs=data_new_idxs, |
| 968 |
disp_new_idxs=disp_new_idxs, |
| 969 |
move_data=True, |
| 970 |
create_selections=False, |
| 971 |
undo=False, |
| 972 |
emit_event=False, |
| 973 |
redraw=True, |
| 974 |
) |
| 975 |
event_data["moved"]["columns"] = { |
| 976 |
"data": data_new_idxs, |
| 977 |
"displayed": disp_new_idxs, |
| 978 |
} |
| 979 |
if undo and self.MT.undo_enabled: |
| 980 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 981 |
try_binding(self.ri_extra_end_sort_cols_func, event_data, "end_move_columns") |
| 982 |
self.MT.sheet_modified(event_data) |
| 983 |
self.PAR.emit_event("<<SheetModified>>", event_data) |
| 984 |
self.MT.refresh() |
| 985 |
return event_data |
| 986 |
|
| 987 |
def toggle_select_row( |
| 988 |
self, |
| 989 |
row: int, |
| 990 |
add_selection: bool = True, |
| 991 |
redraw: bool = True, |
| 992 |
run_binding_func: bool = True, |
| 993 |
set_as_current: bool = True, |
| 994 |
ext: bool = False, |
| 995 |
) -> int | None: |
| 996 |
if add_selection: |
| 997 |
if self.MT.row_selected(row): |
| 998 |
fill_iid = self.MT.deselect(r=row, redraw=redraw) |
| 999 |
else: |
| 1000 |
fill_iid = self.add_selection( |
| 1001 |
r=row, |
| 1002 |
redraw=redraw, |
| 1003 |
run_binding_func=run_binding_func, |
| 1004 |
set_as_current=set_as_current, |
| 1005 |
ext=ext, |
| 1006 |
) |
| 1007 |
else: |
| 1008 |
if self.MT.row_selected(row): |
| 1009 |
fill_iid = self.MT.deselect(r=row, redraw=redraw) |
| 1010 |
else: |
| 1011 |
fill_iid = self.select_row(row, redraw=redraw, ext=ext) |
| 1012 |
return fill_iid |
| 1013 |
|
| 1014 |
def select_row( |
| 1015 |
self, |
| 1016 |
r: int | Iterator[int], |
| 1017 |
redraw: bool = False, |
| 1018 |
run_binding_func: bool = True, |
| 1019 |
ext: bool = False, |
| 1020 |
) -> int: |
| 1021 |
boxes_to_hide = tuple(self.MT.selection_boxes) |
| 1022 |
fill_iids = [ |
| 1023 |
self.MT.create_selection_box( |
| 1024 |
start, |
| 1025 |
0, |
| 1026 |
end, |
| 1027 |
len(self.MT.col_positions) - 1, |
| 1028 |
"rows", |
| 1029 |
set_current=True, |
| 1030 |
ext=ext, |
| 1031 |
) |
| 1032 |
for start, end in consecutive_ranges(int_x_tuple(r)) |
| 1033 |
] |
| 1034 |
for iid in boxes_to_hide: |
| 1035 |
self.MT.hide_selection_box(iid) |
| 1036 |
if redraw: |
| 1037 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 1038 |
if run_binding_func: |
| 1039 |
self.MT.run_selection_binding("rows") |
| 1040 |
return fill_iids[0] if len(fill_iids) == 1 else fill_iids |
| 1041 |
|
| 1042 |
def add_selection( |
| 1043 |
self, |
| 1044 |
r: int, |
| 1045 |
redraw: bool = False, |
| 1046 |
run_binding_func: bool = True, |
| 1047 |
set_as_current: bool = True, |
| 1048 |
ext: bool = False, |
| 1049 |
) -> int: |
| 1050 |
box = (r, 0, r + 1, len(self.MT.col_positions) - 1, "rows") |
| 1051 |
fill_iid = self.MT.create_selection_box(*box, set_current=set_as_current, ext=ext) |
| 1052 |
if redraw: |
| 1053 |
self.MT.main_table_redraw_grid_and_text(redraw_header=False, redraw_row_index=True) |
| 1054 |
if run_binding_func: |
| 1055 |
self.MT.run_selection_binding("rows") |
| 1056 |
return fill_iid |
| 1057 |
|
| 1058 |
def get_cell_dimensions(self, datarn: int) -> tuple[int, int]: |
| 1059 |
txt = self.cell_str(datarn, fix=False) |
| 1060 |
if txt: |
| 1061 |
lines = findall(r"[^\n]+", txt) |
| 1062 |
h = self.MT.index_txt_height * len(lines) + 5 |
| 1063 |
w = max(sum(self.wrap_get_char_w(c) for c in line) for line in lines) + 8 |
| 1064 |
else: |
| 1065 |
w = self.ops.default_row_index_width |
| 1066 |
h = self.MT.min_row_height |
| 1067 |
|
| 1068 |
if (datarn in self.cell_options and "dropdown" in self.cell_options[datarn]) or ( |
| 1069 |
datarn in self.cell_options and "checkbox" in self.cell_options[datarn] |
| 1070 |
): |
| 1071 |
w += self.MT.index_txt_height + 2 |
| 1072 |
if self.ops.treeview: |
| 1073 |
if datarn in self.cell_options and "align" in self.cell_options[datarn]: |
| 1074 |
align = self.cell_options[datarn]["align"] |
| 1075 |
else: |
| 1076 |
align = self.align |
| 1077 |
if align[-1] == "w": |
| 1078 |
w += self.MT.index_txt_height |
| 1079 |
w += self.get_iid_indent(self.MT._row_index[datarn].iid) + 10 |
| 1080 |
return w, h |
| 1081 |
|
| 1082 |
def get_cell_width(self, datarn: int) -> int: |
| 1083 |
txt = self.cell_str(datarn, fix=False) |
| 1084 |
if txt: |
| 1085 |
w = max(sum(self.wrap_get_char_w(c) for c in line) for line in findall(r"[^\n]+", txt)) + 8 |
| 1086 |
else: |
| 1087 |
w = self.ops.default_row_index_width |
| 1088 |
|
| 1089 |
if (datarn in self.cell_options and "dropdown" in self.cell_options[datarn]) or ( |
| 1090 |
datarn in self.cell_options and "checkbox" in self.cell_options[datarn] |
| 1091 |
): |
| 1092 |
w += self.MT.index_txt_height + 2 |
| 1093 |
if self.ops.treeview: |
| 1094 |
if datarn in self.cell_options and "align" in self.cell_options[datarn]: |
| 1095 |
align = self.cell_options[datarn]["align"] |
| 1096 |
else: |
| 1097 |
align = self.align |
| 1098 |
if align[-1] == "w": |
| 1099 |
w += self.MT.index_txt_height |
| 1100 |
w += self.get_iid_indent(self.MT._row_index[datarn].iid) + 10 |
| 1101 |
return w |
| 1102 |
|
| 1103 |
def get_wrapped_cell_height(self, datarn: int) -> int: |
| 1104 |
n_lines = max( |
| 1105 |
1, |
| 1106 |
sum( |
| 1107 |
1 |
| 1108 |
for _ in wrap_text( |
| 1109 |
text=self.cell_str(datarn, fix=False), |
| 1110 |
max_width=self.current_width, |
| 1111 |
max_lines=float("inf"), |
| 1112 |
char_width_fn=self.wrap_get_char_w, |
| 1113 |
widths=self.MT.char_widths[self.index_font], |
| 1114 |
wrap=self.ops.index_wrap, |
| 1115 |
) |
| 1116 |
), |
| 1117 |
) |
| 1118 |
return 3 + (n_lines * self.MT.index_txt_height) |
| 1119 |
|
| 1120 |
def get_row_text_height( |
| 1121 |
self, |
| 1122 |
row: int, |
| 1123 |
visible_only: bool = False, |
| 1124 |
only_if_too_small: bool = False, |
| 1125 |
) -> int: |
| 1126 |
h = self.MT.min_row_height |
| 1127 |
datarn = row if self.MT.all_rows_displayed else self.MT.displayed_rows[row] |
| 1128 |
|
| 1129 |
ih = self.get_wrapped_cell_height(datarn) |
| 1130 |
|
| 1131 |
if self.MT.data: |
| 1132 |
if self.MT.all_columns_displayed: |
| 1133 |
if visible_only: |
| 1134 |
iterable = range(*self.MT.visible_text_columns) |
| 1135 |
else: |
| 1136 |
if not self.MT.data or datarn >= len(self.MT.data): |
| 1137 |
iterable = range(0, 0) |
| 1138 |
else: |
| 1139 |
iterable = range(0, len(self.MT.data[datarn])) |
| 1140 |
else: |
| 1141 |
if visible_only: |
| 1142 |
start_col, end_col = self.MT.visible_text_columns |
| 1143 |
else: |
| 1144 |
start_col, end_col = 0, len(self.MT.displayed_columns) |
| 1145 |
iterable = self.MT.displayed_columns[start_col:end_col] |
| 1146 |
cell_heights = ( |
| 1147 |
self.MT.get_wrapped_cell_height( |
| 1148 |
datarn, |
| 1149 |
datacn, |
| 1150 |
) |
| 1151 |
for datacn in iterable |
| 1152 |
) |
| 1153 |
h = max(h, max(cell_heights, default=h)) |
| 1154 |
self.MT.cells_cache = None |
| 1155 |
h = max(h, ih) |
| 1156 |
if only_if_too_small and h < self.MT.row_positions[row + 1] - self.MT.row_positions[row]: |
| 1157 |
return self.MT.row_positions[row + 1] - self.MT.row_positions[row] |
| 1158 |
else: |
| 1159 |
return max(int(min(h, self.ops.max_row_height)), self.MT.min_row_height) |
| 1160 |
|
| 1161 |
def set_row_height( |
| 1162 |
self, |
| 1163 |
row: int, |
| 1164 |
height: None | int = None, |
| 1165 |
only_if_too_small: bool = False, |
| 1166 |
visible_only: bool = False, |
| 1167 |
recreate: bool = True, |
| 1168 |
) -> int: |
| 1169 |
if height is None: |
| 1170 |
height = self.get_row_text_height(row=row, visible_only=visible_only) |
| 1171 |
if height < self.MT.min_row_height: |
| 1172 |
height = int(self.MT.min_row_height) |
| 1173 |
elif height > self.ops.max_row_height: |
| 1174 |
height = int(self.ops.max_row_height) |
| 1175 |
if only_if_too_small and height <= self.MT.row_positions[row + 1] - self.MT.row_positions[row]: |
| 1176 |
return self.MT.row_positions[row + 1] - self.MT.row_positions[row] |
| 1177 |
new_row_pos = self.MT.row_positions[row] + height |
| 1178 |
increment = new_row_pos - self.MT.row_positions[row + 1] |
| 1179 |
self.MT.row_positions[row + 2 :] = [ |
| 1180 |
e + increment for e in islice(self.MT.row_positions, row + 2, len(self.MT.row_positions)) |
| 1181 |
] |
| 1182 |
self.MT.row_positions[row + 1] = new_row_pos |
| 1183 |
if recreate: |
| 1184 |
self.MT.recreate_all_selection_boxes() |
| 1185 |
return height |
| 1186 |
|
| 1187 |
def get_index_text_width(self, only_rows: Iterator[int] | None = None) -> int: |
| 1188 |
self.fix_index() |
| 1189 |
w = self.ops.default_row_index_width |
| 1190 |
if (not self.MT._row_index and isinstance(self.MT._row_index, list)) or ( |
| 1191 |
isinstance(self.MT._row_index, int) and self.MT._row_index >= len(self.MT.data) |
| 1192 |
): |
| 1193 |
return w |
| 1194 |
if only_rows: |
| 1195 |
iterable = only_rows |
| 1196 |
elif self.MT.all_rows_displayed: |
| 1197 |
if isinstance(self.MT._row_index, list): |
| 1198 |
iterable = range(len(self.MT._row_index)) |
| 1199 |
else: |
| 1200 |
iterable = range(len(self.MT.data)) |
| 1201 |
else: |
| 1202 |
iterable = self.MT.displayed_rows |
| 1203 |
if (new_w := max(map(self.get_cell_width, iterable), default=w)) > w: |
| 1204 |
w = new_w |
| 1205 |
if w > self.ops.max_index_width: |
| 1206 |
w = int(self.ops.max_index_width) |
| 1207 |
return w |
| 1208 |
|
| 1209 |
def set_width_of_index_to_text( |
| 1210 |
self, |
| 1211 |
text: None | str = None, |
| 1212 |
only_rows: list[int] | None = None, |
| 1213 |
) -> int: |
| 1214 |
self.fix_index() |
| 1215 |
w = self.ops.default_row_index_width |
| 1216 |
if (text is None and isinstance(self.MT._row_index, list) and not self.MT._row_index) or ( |
| 1217 |
isinstance(self.MT._row_index, int) and self.MT._row_index >= len(self.MT.data) |
| 1218 |
): |
| 1219 |
return w |
| 1220 |
if text is not None and text: |
| 1221 |
self.MT.txt_measure_canvas.itemconfig(self.MT.txt_measure_canvas_text, text=text) |
| 1222 |
b = self.MT.txt_measure_canvas.bbox(self.MT.txt_measure_canvas_text) |
| 1223 |
if (tw := b[2] - b[0] + 10) > w: |
| 1224 |
w = tw |
| 1225 |
elif text is None: |
| 1226 |
w = self.get_index_text_width(only_rows=[] if only_rows is None else only_rows) |
| 1227 |
if w > self.ops.max_index_width: |
| 1228 |
w = int(self.ops.max_index_width) |
| 1229 |
self.set_width(w, set_TL=True) |
| 1230 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 1231 |
return w |
| 1232 |
|
| 1233 |
def set_height_of_all_rows( |
| 1234 |
self, |
| 1235 |
height: int | None = None, |
| 1236 |
only_if_too_small: bool = False, |
| 1237 |
recreate: bool = True, |
| 1238 |
) -> None: |
| 1239 |
if height is None: |
| 1240 |
if self.MT.all_columns_displayed: |
| 1241 |
iterable = range(self.MT.total_data_rows()) |
| 1242 |
else: |
| 1243 |
iterable = range(len(self.MT.displayed_rows)) |
| 1244 |
self.MT.set_row_positions( |
| 1245 |
itr=(self.get_row_text_height(rn, only_if_too_small=only_if_too_small) for rn in iterable) |
| 1246 |
) |
| 1247 |
elif height is not None: |
| 1248 |
if self.MT.all_rows_displayed: |
| 1249 |
self.MT.set_row_positions(itr=repeat(height, len(self.MT.data))) |
| 1250 |
else: |
| 1251 |
self.MT.set_row_positions(itr=repeat(height, len(self.MT.displayed_rows))) |
| 1252 |
if recreate: |
| 1253 |
self.MT.recreate_all_selection_boxes() |
| 1254 |
|
| 1255 |
def auto_set_index_width(self, end_row: int, only_rows: list) -> bool: |
| 1256 |
if not isinstance(self.MT._row_index, int) and not self.MT._row_index: |
| 1257 |
if self.ops.default_row_index == "letters": |
| 1258 |
new_w = sum(self.wrap_get_char_w(c) for c in num2alpha(end_row)) + 20 |
| 1259 |
elif self.ops.default_row_index == "numbers": |
| 1260 |
new_w = sum(self.wrap_get_char_w(c) for c in str(end_row)) + 20 |
| 1261 |
elif self.ops.default_row_index == "both": |
| 1262 |
new_w = sum(self.wrap_get_char_w(c) for c in f"{end_row + 1} {num2alpha(end_row)}") + 20 |
| 1263 |
elif self.ops.default_row_index is None: |
| 1264 |
new_w = 20 |
| 1265 |
elif self.ops.auto_resize_row_index is True: |
| 1266 |
new_w = self.get_index_text_width(only_rows=only_rows) |
| 1267 |
else: |
| 1268 |
new_w = None |
| 1269 |
if new_w is not None and (sheet_w_x := int(self.PAR.winfo_width() * 0.7)) < new_w: |
| 1270 |
new_w = sheet_w_x |
| 1271 |
if new_w and (self.current_width - new_w > 20 or new_w - self.current_width > 3): |
| 1272 |
if self.MT.find_window.open: |
| 1273 |
self.MT.itemconfig(self.MT.find_window.canvas_id, state="hidden") |
| 1274 |
self.set_width(new_w, set_TL=True, recreate_selection_boxes=False) |
| 1275 |
return True |
| 1276 |
return False |
| 1277 |
|
| 1278 |
def redraw_highlight_get_text_fg( |
| 1279 |
self, |
| 1280 |
fr: float, |
| 1281 |
sr: float, |
| 1282 |
r: int, |
| 1283 |
sel_cells_bg: str, |
| 1284 |
sel_rows_bg: str, |
| 1285 |
selections: dict, |
| 1286 |
datarn: int, |
| 1287 |
has_dd: bool, |
| 1288 |
) -> tuple[str, str, bool]: |
| 1289 |
redrawn = False |
| 1290 |
kwargs = self.get_cell_kwargs(datarn, key="highlight") |
| 1291 |
if kwargs: |
| 1292 |
fill = kwargs[0] |
| 1293 |
if fill and not fill.startswith("#"): |
| 1294 |
fill = color_map[fill] |
| 1295 |
if "rows" in selections and r in selections["rows"]: |
| 1296 |
txtfg = ( |
| 1297 |
self.ops.index_selected_rows_fg |
| 1298 |
if kwargs[1] is None or self.ops.display_selected_fg_over_highlights |
| 1299 |
else kwargs[1] |
| 1300 |
) |
| 1301 |
if fill: |
| 1302 |
fill = ( |
| 1303 |
f"#{int((int(fill[1:3], 16) + int(sel_rows_bg[1:3], 16)) / 2):02X}" |
| 1304 |
+ f"{int((int(fill[3:5], 16) + int(sel_rows_bg[3:5], 16)) / 2):02X}" |
| 1305 |
+ f"{int((int(fill[5:], 16) + int(sel_rows_bg[5:], 16)) / 2):02X}" |
| 1306 |
) |
| 1307 |
elif "cells" in selections and r in selections["cells"]: |
| 1308 |
txtfg = ( |
| 1309 |
self.ops.index_selected_cells_fg |
| 1310 |
if kwargs[1] is None or self.ops.display_selected_fg_over_highlights |
| 1311 |
else kwargs[1] |
| 1312 |
) |
| 1313 |
if fill: |
| 1314 |
fill = ( |
| 1315 |
f"#{int((int(fill[1:3], 16) + int(sel_cells_bg[1:3], 16)) / 2):02X}" |
| 1316 |
+ f"{int((int(fill[3:5], 16) + int(sel_cells_bg[3:5], 16)) / 2):02X}" |
| 1317 |
+ f"{int((int(fill[5:], 16) + int(sel_cells_bg[5:], 16)) / 2):02X}" |
| 1318 |
) |
| 1319 |
else: |
| 1320 |
txtfg = self.ops.index_fg if kwargs[1] is None else kwargs[1] |
| 1321 |
if fill: |
| 1322 |
redrawn = self.redraw_highlight( |
| 1323 |
0, |
| 1324 |
fr + 1, |
| 1325 |
self.current_width - 1, |
| 1326 |
sr, |
| 1327 |
fill=fill, |
| 1328 |
outline=self.ops.index_fg if has_dd and self.ops.show_dropdown_borders else "", |
| 1329 |
) |
| 1330 |
tree_arrow_fg = txtfg |
| 1331 |
elif not kwargs: |
| 1332 |
if "rows" in selections and r in selections["rows"]: |
| 1333 |
txtfg = self.ops.index_selected_rows_fg |
| 1334 |
tree_arrow_fg = self.ops.selected_rows_tree_arrow_fg |
| 1335 |
redrawn = self.redraw_highlight( |
| 1336 |
0, |
| 1337 |
fr + 1, |
| 1338 |
self.current_width - 1, |
| 1339 |
sr, |
| 1340 |
fill=self.ops.index_selected_rows_bg, |
| 1341 |
outline=self.ops.index_fg if has_dd and self.ops.show_dropdown_borders else "", |
| 1342 |
) |
| 1343 |
elif "cells" in selections and r in selections["cells"]: |
| 1344 |
txtfg = self.ops.index_selected_cells_fg |
| 1345 |
tree_arrow_fg = self.ops.selected_cells_tree_arrow_fg |
| 1346 |
redrawn = self.redraw_highlight( |
| 1347 |
0, |
| 1348 |
fr + 1, |
| 1349 |
self.current_width - 1, |
| 1350 |
sr, |
| 1351 |
fill=self.ops.index_selected_cells_bg, |
| 1352 |
outline=self.ops.index_fg if has_dd and self.ops.show_dropdown_borders else "", |
| 1353 |
) |
| 1354 |
else: |
| 1355 |
txtfg = self.ops.index_fg |
| 1356 |
tree_arrow_fg = self.ops.tree_arrow_fg |
| 1357 |
return txtfg, tree_arrow_fg, redrawn |
| 1358 |
|
| 1359 |
def redraw_highlight( |
| 1360 |
self, |
| 1361 |
x1: float, |
| 1362 |
y1: float, |
| 1363 |
x2: float, |
| 1364 |
y2: float, |
| 1365 |
fill: str, |
| 1366 |
outline: str, |
| 1367 |
) -> bool: |
| 1368 |
coords = (x1, y1, x2, y2) |
| 1369 |
if self.hidd_high: |
| 1370 |
iid, showing = self.hidd_high.popitem() |
| 1371 |
self.coords(iid, coords) |
| 1372 |
if showing: |
| 1373 |
self.itemconfig(iid, fill=fill, outline=outline) |
| 1374 |
else: |
| 1375 |
self.itemconfig(iid, fill=fill, outline=outline, state="normal") |
| 1376 |
else: |
| 1377 |
iid = self.create_rectangle(coords, fill=fill, outline=outline) |
| 1378 |
self.disp_high[iid] = True |
| 1379 |
return True |
| 1380 |
|
| 1381 |
def redraw_gridline( |
| 1382 |
self, |
| 1383 |
points: tuple[float], |
| 1384 |
fill: str, |
| 1385 |
width: int, |
| 1386 |
) -> None: |
| 1387 |
if self.hidd_grid: |
| 1388 |
t, sh = self.hidd_grid.popitem() |
| 1389 |
self.coords(t, points) |
| 1390 |
if sh: |
| 1391 |
self.itemconfig(t, fill=fill, width=width) |
| 1392 |
else: |
| 1393 |
self.itemconfig(t, fill=fill, width=width, state="normal") |
| 1394 |
self.disp_grid[t] = True |
| 1395 |
else: |
| 1396 |
self.disp_grid[self.create_line(points, fill=fill, width=width)] = True |
| 1397 |
|
| 1398 |
def redraw_tree_arrow( |
| 1399 |
self, |
| 1400 |
x1: float, |
| 1401 |
y1: float, |
| 1402 |
y2: float, |
| 1403 |
fill: str, |
| 1404 |
indent: float, |
| 1405 |
has_children: bool = False, |
| 1406 |
open_: bool = False, |
| 1407 |
level: int = 1, |
| 1408 |
) -> None: |
| 1409 |
mod = (self.MT.index_txt_height - 1) if self.MT.index_txt_height % 2 else self.MT.index_txt_height |
| 1410 |
small_mod = int(mod / 5) |
| 1411 |
mid_y = int(self.MT.min_row_height / 2) |
| 1412 |
if has_children: |
| 1413 |
|
| 1414 |
if open_: |
| 1415 |
points = ( |
| 1416 |
|
| 1417 |
x1 + 5 + indent, |
| 1418 |
y1 + mid_y + small_mod, |
| 1419 |
|
| 1420 |
x1 + 5 + indent + small_mod + small_mod, |
| 1421 |
y1 + mid_y - small_mod, |
| 1422 |
|
| 1423 |
x1 + 5 + indent + small_mod + small_mod + small_mod + small_mod, |
| 1424 |
y1 + mid_y + small_mod, |
| 1425 |
) |
| 1426 |
|
| 1427 |
else: |
| 1428 |
points = ( |
| 1429 |
|
| 1430 |
x1 + 5 + indent + small_mod + small_mod, |
| 1431 |
y1 + mid_y - small_mod - small_mod, |
| 1432 |
|
| 1433 |
x1 + 5 + indent + small_mod + small_mod + small_mod + small_mod, |
| 1434 |
y1 + mid_y, |
| 1435 |
|
| 1436 |
x1 + 5 + indent + small_mod + small_mod, |
| 1437 |
y1 + mid_y + small_mod + small_mod, |
| 1438 |
) |
| 1439 |
else: |
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
points = ( |
| 1462 |
|
| 1463 |
x1 + 5 + indent, |
| 1464 |
y1 + mid_y, |
| 1465 |
|
| 1466 |
x1 + 5 + indent + small_mod + small_mod + small_mod + small_mod, |
| 1467 |
y1 + mid_y, |
| 1468 |
) |
| 1469 |
|
| 1470 |
if self.hidd_tree_arrow: |
| 1471 |
t, sh = self.hidd_tree_arrow.popitem() |
| 1472 |
self.coords(t, points) |
| 1473 |
if sh: |
| 1474 |
self.itemconfig(t, fill=fill if has_children else self.ops.index_grid_fg) |
| 1475 |
else: |
| 1476 |
self.itemconfig(t, fill=fill if has_children else self.ops.index_grid_fg, state="normal") |
| 1477 |
else: |
| 1478 |
t = self.create_line( |
| 1479 |
points, |
| 1480 |
fill=fill if has_children else self.ops.index_grid_fg, |
| 1481 |
width=2, |
| 1482 |
capstyle=tk.ROUND, |
| 1483 |
joinstyle=tk.BEVEL, |
| 1484 |
tag="lift", |
| 1485 |
) |
| 1486 |
self.disp_tree_arrow[t] = True |
| 1487 |
|
| 1488 |
def redraw_dropdown( |
| 1489 |
self, |
| 1490 |
x1: float, |
| 1491 |
y1: float, |
| 1492 |
x2: float, |
| 1493 |
y2: float, |
| 1494 |
fill: str, |
| 1495 |
outline: str, |
| 1496 |
draw_outline: bool = True, |
| 1497 |
draw_arrow: bool = True, |
| 1498 |
open_: bool = False, |
| 1499 |
) -> None: |
| 1500 |
if draw_outline and self.ops.show_dropdown_borders: |
| 1501 |
self.redraw_highlight(x1 + 1, y1 + 1, x2, y2, fill="", outline=self.ops.index_fg) |
| 1502 |
if draw_arrow: |
| 1503 |
mod = (self.MT.index_txt_height - 1) if self.MT.index_txt_height % 2 else self.MT.index_txt_height |
| 1504 |
small_mod = int(mod / 5) |
| 1505 |
mid_y = int(self.MT.min_row_height / 2) |
| 1506 |
if open_: |
| 1507 |
|
| 1508 |
points = ( |
| 1509 |
x2 - 4 - small_mod - small_mod - small_mod - small_mod, |
| 1510 |
y1 + mid_y + small_mod, |
| 1511 |
x2 - 4 - small_mod - small_mod, |
| 1512 |
y1 + mid_y - small_mod, |
| 1513 |
x2 - 4, |
| 1514 |
y1 + mid_y + small_mod, |
| 1515 |
) |
| 1516 |
else: |
| 1517 |
|
| 1518 |
points = ( |
| 1519 |
x2 - 4 - small_mod - small_mod - small_mod - small_mod, |
| 1520 |
y1 + mid_y - small_mod, |
| 1521 |
x2 - 4 - small_mod - small_mod, |
| 1522 |
y1 + mid_y + small_mod, |
| 1523 |
x2 - 4, |
| 1524 |
y1 + mid_y - small_mod, |
| 1525 |
) |
| 1526 |
if self.hidd_dropdown: |
| 1527 |
t, sh = self.hidd_dropdown.popitem() |
| 1528 |
self.coords(t, points) |
| 1529 |
if sh: |
| 1530 |
self.itemconfig(t, fill=fill) |
| 1531 |
else: |
| 1532 |
self.itemconfig(t, fill=fill, state="normal") |
| 1533 |
else: |
| 1534 |
t = self.create_line( |
| 1535 |
points, |
| 1536 |
fill=fill, |
| 1537 |
width=2, |
| 1538 |
capstyle=tk.ROUND, |
| 1539 |
joinstyle=tk.BEVEL, |
| 1540 |
tag="lift", |
| 1541 |
) |
| 1542 |
self.disp_dropdown[t] = True |
| 1543 |
|
| 1544 |
def redraw_checkbox( |
| 1545 |
self, |
| 1546 |
x1: float, |
| 1547 |
y1: float, |
| 1548 |
x2: float, |
| 1549 |
y2: float, |
| 1550 |
fill: str, |
| 1551 |
outline: str, |
| 1552 |
draw_check: bool = False, |
| 1553 |
) -> None: |
| 1554 |
points = rounded_box_coords(x1, y1, x2, y2) |
| 1555 |
if self.hidd_checkbox: |
| 1556 |
t, sh = self.hidd_checkbox.popitem() |
| 1557 |
self.coords(t, points) |
| 1558 |
if sh: |
| 1559 |
self.itemconfig(t, fill=outline, outline=fill) |
| 1560 |
else: |
| 1561 |
self.itemconfig(t, fill=outline, outline=fill, state="normal") |
| 1562 |
else: |
| 1563 |
t = self.create_polygon(points, fill=outline, outline=fill, smooth=True, tag="lift") |
| 1564 |
self.disp_checkbox[t] = True |
| 1565 |
if draw_check: |
| 1566 |
|
| 1567 |
x1 = x1 + 4 |
| 1568 |
y1 = y1 + 4 |
| 1569 |
x2 = x2 - 3 |
| 1570 |
y2 = y2 - 3 |
| 1571 |
points = rounded_box_coords(x1, y1, x2, y2, radius=4) |
| 1572 |
if self.hidd_checkbox: |
| 1573 |
t, sh = self.hidd_checkbox.popitem() |
| 1574 |
self.coords(t, points) |
| 1575 |
if sh: |
| 1576 |
self.itemconfig(t, fill=fill, outline=outline) |
| 1577 |
else: |
| 1578 |
self.itemconfig(t, fill=fill, outline=outline, state="normal") |
| 1579 |
else: |
| 1580 |
t = self.create_polygon(points, fill=fill, outline=outline, smooth=True, tag="lift") |
| 1581 |
self.disp_checkbox[t] = True |
| 1582 |
|
| 1583 |
def configure_scrollregion(self, last_row_line_pos: float) -> bool: |
| 1584 |
try: |
| 1585 |
self.configure( |
| 1586 |
scrollregion=( |
| 1587 |
0, |
| 1588 |
0, |
| 1589 |
self.current_width, |
| 1590 |
last_row_line_pos + self.ops.empty_vertical + 2, |
| 1591 |
) |
| 1592 |
) |
| 1593 |
return True |
| 1594 |
except Exception: |
| 1595 |
return False |
| 1596 |
|
| 1597 |
def wrap_get_char_w(self, c: str) -> int: |
| 1598 |
if c in self.MT.char_widths[self.index_font]: |
| 1599 |
return self.MT.char_widths[self.index_font][c] |
| 1600 |
else: |
| 1601 |
self.MT.txt_measure_canvas.itemconfig( |
| 1602 |
self.MT.txt_measure_canvas_text, |
| 1603 |
text=_test_str + c, |
| 1604 |
font=self.index_font, |
| 1605 |
) |
| 1606 |
b = self.MT.txt_measure_canvas.bbox(self.MT.txt_measure_canvas_text) |
| 1607 |
wd = b[2] - b[0] - self.index_test_str_w |
| 1608 |
self.MT.char_widths[self.index_font][c] = wd |
| 1609 |
return wd |
| 1610 |
|
| 1611 |
def redraw_grid_and_text( |
| 1612 |
self, |
| 1613 |
last_row_line_pos: float, |
| 1614 |
scrollpos_top: int, |
| 1615 |
y_stop: int, |
| 1616 |
grid_start_row: int, |
| 1617 |
grid_end_row: int, |
| 1618 |
text_start_row: int, |
| 1619 |
text_end_row: int, |
| 1620 |
scrollpos_bot: int, |
| 1621 |
row_pos_exists: bool, |
| 1622 |
set_scrollregion: bool, |
| 1623 |
) -> bool: |
| 1624 |
if set_scrollregion and not self.configure_scrollregion(last_row_line_pos=last_row_line_pos): |
| 1625 |
return False |
| 1626 |
self.hidd_text.update(self.disp_text) |
| 1627 |
self.disp_text = {} |
| 1628 |
self.hidd_high.update(self.disp_high) |
| 1629 |
self.disp_high = {} |
| 1630 |
self.hidd_grid.update(self.disp_grid) |
| 1631 |
self.disp_grid = {} |
| 1632 |
self.hidd_dropdown.update(self.disp_dropdown) |
| 1633 |
self.disp_dropdown = {} |
| 1634 |
self.hidd_checkbox.update(self.disp_checkbox) |
| 1635 |
self.disp_checkbox = {} |
| 1636 |
self.hidd_tree_arrow.update(self.disp_tree_arrow) |
| 1637 |
self.disp_tree_arrow = {} |
| 1638 |
self.visible_row_dividers = {} |
| 1639 |
self.row_width_resize_bbox = ( |
| 1640 |
self.current_width - 2, |
| 1641 |
scrollpos_top, |
| 1642 |
self.current_width, |
| 1643 |
scrollpos_bot, |
| 1644 |
) |
| 1645 |
|
| 1646 |
if (self.ops.show_horizontal_grid or self.height_resizing_enabled) and row_pos_exists: |
| 1647 |
xend = self.current_width - 6 |
| 1648 |
points = [ |
| 1649 |
self.current_width - 1, |
| 1650 |
y_stop - 1, |
| 1651 |
self.current_width - 1, |
| 1652 |
scrollpos_top - 1, |
| 1653 |
-1, |
| 1654 |
scrollpos_top - 1, |
| 1655 |
] |
| 1656 |
for r in range(grid_start_row, grid_end_row): |
| 1657 |
draw_y = self.MT.row_positions[r] |
| 1658 |
if r and self.height_resizing_enabled: |
| 1659 |
self.visible_row_dividers[r] = (1, draw_y - 2, xend, draw_y + 2) |
| 1660 |
points.extend( |
| 1661 |
( |
| 1662 |
-1, |
| 1663 |
draw_y, |
| 1664 |
self.current_width, |
| 1665 |
draw_y, |
| 1666 |
-1, |
| 1667 |
draw_y, |
| 1668 |
-1, |
| 1669 |
self.MT.row_positions[r + 1] if len(self.MT.row_positions) - 1 > r else draw_y, |
| 1670 |
) |
| 1671 |
) |
| 1672 |
self.redraw_gridline(points=points, fill=self.ops.index_grid_fg, width=1) |
| 1673 |
|
| 1674 |
sel_cells_bg = ( |
| 1675 |
self.ops.index_selected_cells_bg |
| 1676 |
if self.ops.index_selected_cells_bg.startswith("#") |
| 1677 |
else color_map[self.ops.index_selected_cells_bg] |
| 1678 |
) |
| 1679 |
sel_rows_bg = ( |
| 1680 |
self.ops.index_selected_rows_bg |
| 1681 |
if self.ops.index_selected_rows_bg.startswith("#") |
| 1682 |
else color_map[self.ops.index_selected_rows_bg] |
| 1683 |
) |
| 1684 |
font = self.ops.index_font |
| 1685 |
selections = self.get_redraw_selections(text_start_row, grid_end_row) |
| 1686 |
dd_coords = self.dropdown.get_coords() |
| 1687 |
treeview = self.ops.treeview |
| 1688 |
wrap = self.ops.index_wrap |
| 1689 |
for r in range(text_start_row, text_end_row): |
| 1690 |
rtopgridln = self.MT.row_positions[r] |
| 1691 |
rbotgridln = self.MT.row_positions[r + 1] |
| 1692 |
if rbotgridln - rtopgridln < self.MT.index_txt_height: |
| 1693 |
continue |
| 1694 |
checkbox_kwargs = {} |
| 1695 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 1696 |
dropdown_kwargs = self.get_cell_kwargs(datarn, key="dropdown") |
| 1697 |
fill, tree_arrow_fg, dd_drawn = self.redraw_highlight_get_text_fg( |
| 1698 |
fr=rtopgridln, |
| 1699 |
sr=rbotgridln, |
| 1700 |
r=r, |
| 1701 |
sel_cells_bg=sel_cells_bg, |
| 1702 |
sel_rows_bg=sel_rows_bg, |
| 1703 |
selections=selections, |
| 1704 |
datarn=datarn, |
| 1705 |
has_dd=bool(dropdown_kwargs), |
| 1706 |
) |
| 1707 |
|
| 1708 |
if datarn in self.cell_options and "align" in self.cell_options[datarn]: |
| 1709 |
align = self.cell_options[datarn]["align"] |
| 1710 |
else: |
| 1711 |
align = self.align |
| 1712 |
if dropdown_kwargs: |
| 1713 |
max_width = self.current_width - self.MT.index_txt_height - 5 |
| 1714 |
if align[-1] == "w": |
| 1715 |
draw_x = 3 |
| 1716 |
elif align[-1] == "e": |
| 1717 |
draw_x = self.current_width - 5 - self.MT.index_txt_height |
| 1718 |
elif align[-1] == "n": |
| 1719 |
draw_x = (self.current_width - self.MT.index_txt_height) / 2 |
| 1720 |
self.redraw_dropdown( |
| 1721 |
0, |
| 1722 |
rtopgridln, |
| 1723 |
self.current_width - 1, |
| 1724 |
rbotgridln - 1, |
| 1725 |
fill=fill if dropdown_kwargs["state"] != "disabled" else self.ops.index_grid_fg, |
| 1726 |
outline=fill, |
| 1727 |
draw_outline=not dd_drawn, |
| 1728 |
draw_arrow=True, |
| 1729 |
open_=dd_coords == r, |
| 1730 |
) |
| 1731 |
else: |
| 1732 |
max_width = self.current_width - 2 |
| 1733 |
if align[-1] == "w": |
| 1734 |
draw_x = 3 |
| 1735 |
elif align[-1] == "e": |
| 1736 |
draw_x = self.current_width - 3 |
| 1737 |
elif align[-1] == "n": |
| 1738 |
draw_x = self.current_width / 2 |
| 1739 |
if ( |
| 1740 |
(checkbox_kwargs := self.get_cell_kwargs(datarn, key="checkbox")) |
| 1741 |
and not dropdown_kwargs |
| 1742 |
and max_width > self.MT.index_txt_height + 1 |
| 1743 |
): |
| 1744 |
box_w = self.MT.index_txt_height + 1 |
| 1745 |
if align[-1] == "w": |
| 1746 |
draw_x += box_w + 3 |
| 1747 |
elif align[-1] == "n": |
| 1748 |
draw_x += box_w / 2 + 1 |
| 1749 |
max_width -= box_w + 4 |
| 1750 |
try: |
| 1751 |
draw_check = ( |
| 1752 |
self.MT._row_index[datarn] |
| 1753 |
if isinstance(self.MT._row_index, (list, tuple)) |
| 1754 |
else self.MT.data[datarn][self.MT._row_index] |
| 1755 |
) |
| 1756 |
except Exception: |
| 1757 |
draw_check = False |
| 1758 |
self.redraw_checkbox( |
| 1759 |
2, |
| 1760 |
rtopgridln + 2, |
| 1761 |
self.MT.index_txt_height + 3, |
| 1762 |
rtopgridln + self.MT.index_txt_height + 3, |
| 1763 |
fill=fill if checkbox_kwargs["state"] == "normal" else self.ops.index_grid_fg, |
| 1764 |
outline="", |
| 1765 |
draw_check=draw_check, |
| 1766 |
) |
| 1767 |
if treeview and isinstance(self.MT._row_index, list) and len(self.MT._row_index) > datarn: |
| 1768 |
iid = self.MT._row_index[datarn].iid |
| 1769 |
max_width -= self.MT.index_txt_height |
| 1770 |
if align[-1] == "w": |
| 1771 |
draw_x += self.MT.index_txt_height + 3 |
| 1772 |
level, indent = self.get_iid_level_indent(iid) |
| 1773 |
draw_x += indent + 5 |
| 1774 |
self.redraw_tree_arrow( |
| 1775 |
2, |
| 1776 |
rtopgridln, |
| 1777 |
rbotgridln - 1, |
| 1778 |
fill=tree_arrow_fg, |
| 1779 |
indent=indent, |
| 1780 |
has_children=bool(self.MT._row_index[datarn].children), |
| 1781 |
open_=self.MT._row_index[datarn].iid in self.tree_open_ids, |
| 1782 |
level=level, |
| 1783 |
) |
| 1784 |
if max_width <= 1: |
| 1785 |
continue |
| 1786 |
text = self.cell_str(datarn, fix=False) |
| 1787 |
if not text: |
| 1788 |
continue |
| 1789 |
start_line = max(0, int((scrollpos_top - rtopgridln) / self.MT.index_txt_height)) |
| 1790 |
draw_y = rtopgridln + 3 + (start_line * self.MT.index_txt_height) |
| 1791 |
gen_lines = wrap_text( |
| 1792 |
text=text, |
| 1793 |
max_width=max_width, |
| 1794 |
max_lines=int((rbotgridln - rtopgridln - 2) / self.MT.index_txt_height), |
| 1795 |
char_width_fn=self.wrap_get_char_w, |
| 1796 |
widths=self.MT.char_widths[font], |
| 1797 |
wrap=wrap, |
| 1798 |
start_line=start_line, |
| 1799 |
) |
| 1800 |
if align[-1] == "w" or align[-1] == "e": |
| 1801 |
if self.hidd_text: |
| 1802 |
iid, showing = self.hidd_text.popitem() |
| 1803 |
self.coords(iid, draw_x, draw_y) |
| 1804 |
if showing: |
| 1805 |
self.itemconfig( |
| 1806 |
iid, |
| 1807 |
text="\n".join(gen_lines), |
| 1808 |
fill=fill, |
| 1809 |
font=font, |
| 1810 |
anchor=align, |
| 1811 |
) |
| 1812 |
else: |
| 1813 |
self.itemconfig( |
| 1814 |
iid, |
| 1815 |
text="\n".join(gen_lines), |
| 1816 |
fill=fill, |
| 1817 |
font=font, |
| 1818 |
anchor=align, |
| 1819 |
state="normal", |
| 1820 |
) |
| 1821 |
else: |
| 1822 |
iid = self.create_text( |
| 1823 |
draw_x, |
| 1824 |
draw_y, |
| 1825 |
text="\n".join(gen_lines), |
| 1826 |
fill=fill, |
| 1827 |
font=font, |
| 1828 |
anchor=align, |
| 1829 |
tag="lift", |
| 1830 |
) |
| 1831 |
self.disp_text[iid] = True |
| 1832 |
else: |
| 1833 |
for line in gen_lines: |
| 1834 |
if self.hidd_text: |
| 1835 |
iid, showing = self.hidd_text.popitem() |
| 1836 |
self.coords(iid, draw_x, draw_y) |
| 1837 |
if showing: |
| 1838 |
self.itemconfig( |
| 1839 |
iid, |
| 1840 |
text=line, |
| 1841 |
fill=fill, |
| 1842 |
font=font, |
| 1843 |
anchor=align, |
| 1844 |
) |
| 1845 |
else: |
| 1846 |
self.itemconfig( |
| 1847 |
iid, |
| 1848 |
text=line, |
| 1849 |
fill=fill, |
| 1850 |
font=font, |
| 1851 |
anchor=align, |
| 1852 |
state="normal", |
| 1853 |
) |
| 1854 |
else: |
| 1855 |
iid = self.create_text( |
| 1856 |
draw_x, |
| 1857 |
draw_y, |
| 1858 |
text=line, |
| 1859 |
fill=fill, |
| 1860 |
font=font, |
| 1861 |
anchor=align, |
| 1862 |
tag="lift", |
| 1863 |
) |
| 1864 |
self.disp_text[iid] = True |
| 1865 |
draw_y += self.MT.header_txt_height |
| 1866 |
|
| 1867 |
for dct in ( |
| 1868 |
self.hidd_text, |
| 1869 |
self.hidd_high, |
| 1870 |
self.hidd_grid, |
| 1871 |
self.hidd_dropdown, |
| 1872 |
self.hidd_checkbox, |
| 1873 |
self.hidd_tree_arrow, |
| 1874 |
): |
| 1875 |
for iid, showing in dct.items(): |
| 1876 |
if showing: |
| 1877 |
self.itemconfig(iid, state="hidden") |
| 1878 |
dct[iid] = False |
| 1879 |
self.tag_raise("lift") |
| 1880 |
if self.disp_resize_lines: |
| 1881 |
self.tag_raise("rh") |
| 1882 |
return True |
| 1883 |
|
| 1884 |
def get_redraw_selections(self, startr: int, endr: int) -> dict[str, set[int]]: |
| 1885 |
d = defaultdict(set) |
| 1886 |
for _, box in self.MT.get_selection_items(): |
| 1887 |
r1, _, r2, _ = box.coords |
| 1888 |
for r in range(startr, endr): |
| 1889 |
if r1 <= r and r2 > r: |
| 1890 |
d[box.type_ if box.type_ != "columns" else "cells"].add(r) |
| 1891 |
return d |
| 1892 |
|
| 1893 |
def open_cell(self, event: Any = None, ignore_existing_editor: bool = False) -> None: |
| 1894 |
if not self.MT.anything_selected() or (not ignore_existing_editor and self.text_editor.open): |
| 1895 |
return |
| 1896 |
if not self.MT.selected: |
| 1897 |
return |
| 1898 |
r = self.MT.selected.row |
| 1899 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 1900 |
if self.get_cell_kwargs(datarn, key="readonly"): |
| 1901 |
return |
| 1902 |
elif self.get_cell_kwargs(datarn, key="dropdown") or self.get_cell_kwargs(datarn, key="checkbox"): |
| 1903 |
if event_opens_dropdown_or_checkbox(event): |
| 1904 |
if self.get_cell_kwargs(datarn, key="dropdown"): |
| 1905 |
self.open_dropdown_window(r, event=event) |
| 1906 |
elif self.get_cell_kwargs(datarn, key="checkbox"): |
| 1907 |
self.click_checkbox(r, datarn) |
| 1908 |
elif self.edit_cell_enabled: |
| 1909 |
self.open_text_editor(event=event, r=r, dropdown=False) |
| 1910 |
|
| 1911 |
|
| 1912 |
def get_cell_align(self, r: int) -> str: |
| 1913 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 1914 |
if datarn in self.cell_options and "align" in self.cell_options[datarn]: |
| 1915 |
align = self.cell_options[datarn]["align"] |
| 1916 |
else: |
| 1917 |
align = self.align |
| 1918 |
return align |
| 1919 |
|
| 1920 |
|
| 1921 |
def open_text_editor( |
| 1922 |
self, |
| 1923 |
event: Any = None, |
| 1924 |
r: int = 0, |
| 1925 |
text: None | str = None, |
| 1926 |
state: str = "normal", |
| 1927 |
dropdown: bool = False, |
| 1928 |
) -> bool: |
| 1929 |
text = f"{self.get_cell_data(self.MT.datarn(r), none_to_empty_str=True, redirect_int=True)}" |
| 1930 |
extra_func_key = "??" |
| 1931 |
if event_opens_dropdown_or_checkbox(event): |
| 1932 |
if hasattr(event, "keysym") and event.keysym in ("Return", "F2", "BackSpace"): |
| 1933 |
extra_func_key = event.keysym |
| 1934 |
if event.keysym == "BackSpace": |
| 1935 |
text = "" |
| 1936 |
elif event_has_char_key(event): |
| 1937 |
extra_func_key = text = event.char |
| 1938 |
elif event is not None: |
| 1939 |
return False |
| 1940 |
if self.extra_begin_edit_cell_func: |
| 1941 |
try: |
| 1942 |
text = self.extra_begin_edit_cell_func( |
| 1943 |
event_dict( |
| 1944 |
name="begin_edit_index", |
| 1945 |
sheet=self.PAR.name, |
| 1946 |
key=extra_func_key, |
| 1947 |
value=text, |
| 1948 |
loc=r, |
| 1949 |
row=r, |
| 1950 |
boxes=self.MT.get_boxes(), |
| 1951 |
selected=self.MT.selected, |
| 1952 |
) |
| 1953 |
) |
| 1954 |
except Exception: |
| 1955 |
return False |
| 1956 |
if text is None: |
| 1957 |
return False |
| 1958 |
else: |
| 1959 |
text = text if isinstance(text, str) else f"{text}" |
| 1960 |
if self.ops.cell_auto_resize_enabled: |
| 1961 |
self.set_row_height_run_binding(r) |
| 1962 |
if self.text_editor.open and r == self.text_editor.row: |
| 1963 |
self.text_editor.set_text(self.text_editor.get() + "" if not isinstance(text, str) else text) |
| 1964 |
return False |
| 1965 |
self.hide_text_editor() |
| 1966 |
if not self.MT.see(r, 0, keep_yscroll=True): |
| 1967 |
self.MT.main_table_redraw_grid_and_text(True, True) |
| 1968 |
x = 0 |
| 1969 |
y = self.MT.row_positions[r] |
| 1970 |
w = self.current_width + 1 |
| 1971 |
h = self.MT.row_positions[r + 1] - y + 1 |
| 1972 |
kwargs = { |
| 1973 |
"menu_kwargs": DotDict( |
| 1974 |
{ |
| 1975 |
"font": self.ops.index_font, |
| 1976 |
"foreground": self.ops.popup_menu_fg, |
| 1977 |
"background": self.ops.popup_menu_bg, |
| 1978 |
"activebackground": self.ops.popup_menu_highlight_bg, |
| 1979 |
"activeforeground": self.ops.popup_menu_highlight_fg, |
| 1980 |
} |
| 1981 |
), |
| 1982 |
"sheet_ops": self.ops, |
| 1983 |
"border_color": self.ops.index_selected_rows_bg, |
| 1984 |
"text": text, |
| 1985 |
"state": state, |
| 1986 |
"width": w, |
| 1987 |
"height": h, |
| 1988 |
"show_border": True, |
| 1989 |
"bg": self.ops.index_editor_bg, |
| 1990 |
"fg": self.ops.index_editor_fg, |
| 1991 |
"select_bg": self.ops.index_editor_select_bg, |
| 1992 |
"select_fg": self.ops.index_editor_select_fg, |
| 1993 |
"align": self.get_cell_align(r), |
| 1994 |
"r": r, |
| 1995 |
} |
| 1996 |
if not self.text_editor.window: |
| 1997 |
self.text_editor.window = TextEditor(self, newline_binding=self.text_editor_newline_binding) |
| 1998 |
self.text_editor.canvas_id = self.create_window((x, y), window=self.text_editor.window, anchor="nw") |
| 1999 |
self.text_editor.window.reset(**kwargs) |
| 2000 |
if not self.text_editor.open: |
| 2001 |
self.itemconfig(self.text_editor.canvas_id, state="normal") |
| 2002 |
self.text_editor.open = True |
| 2003 |
self.coords(self.text_editor.canvas_id, x, y) |
| 2004 |
for b in text_editor_newline_bindings: |
| 2005 |
self.text_editor.tktext.bind(b, self.text_editor_newline_binding) |
| 2006 |
for b in text_editor_close_bindings: |
| 2007 |
self.text_editor.tktext.bind(b, self.close_text_editor) |
| 2008 |
if not dropdown: |
| 2009 |
self.text_editor.tktext.focus_set() |
| 2010 |
self.text_editor.window.scroll_to_bottom() |
| 2011 |
self.text_editor.tktext.bind("<FocusOut>", self.close_text_editor) |
| 2012 |
for key, func in self.MT.text_editor_user_bound_keys.items(): |
| 2013 |
self.text_editor.tktext.bind(key, func) |
| 2014 |
return True |
| 2015 |
|
| 2016 |
def text_editor_newline_binding(self, event: Any = None, check_lines: bool = True) -> None: |
| 2017 |
if not self.height_resizing_enabled: |
| 2018 |
return |
| 2019 |
curr_height = self.text_editor.window.winfo_height() |
| 2020 |
if curr_height < self.MT.min_row_height: |
| 2021 |
return |
| 2022 |
if ( |
| 2023 |
not check_lines |
| 2024 |
or self.MT.get_lines_cell_height( |
| 2025 |
self.text_editor.window.get_num_lines() + 1, |
| 2026 |
font=self.text_editor.tktext.cget("font"), |
| 2027 |
) |
| 2028 |
> curr_height |
| 2029 |
): |
| 2030 |
r = self.text_editor.row |
| 2031 |
new_height = curr_height + self.MT.index_txt_height |
| 2032 |
space_bot = self.MT.get_space_bot(r) |
| 2033 |
if new_height > space_bot: |
| 2034 |
new_height = space_bot |
| 2035 |
if new_height != curr_height: |
| 2036 |
self.set_row_height(r, new_height) |
| 2037 |
self.MT.main_table_redraw_grid_and_text(True, True) |
| 2038 |
self.text_editor.window.config(height=new_height) |
| 2039 |
self.coords(self.text_editor.canvas_id, 0, self.MT.row_positions[r] + 1) |
| 2040 |
if self.dropdown.open and self.dropdown.get_coords() == r: |
| 2041 |
text_editor_h = self.text_editor.window.winfo_height() |
| 2042 |
win_h, anchor = self.get_dropdown_height_anchor(r, text_editor_h) |
| 2043 |
if anchor == "nw": |
| 2044 |
self.coords( |
| 2045 |
self.dropdown.canvas_id, |
| 2046 |
0, |
| 2047 |
self.MT.row_positions[r] + text_editor_h - 1, |
| 2048 |
) |
| 2049 |
self.itemconfig(self.dropdown.canvas_id, anchor=anchor, height=win_h) |
| 2050 |
elif anchor == "sw": |
| 2051 |
self.coords( |
| 2052 |
self.dropdown.canvas_id, |
| 2053 |
0, |
| 2054 |
self.MT.row_positions[r], |
| 2055 |
) |
| 2056 |
self.itemconfig(self.dropdown.canvas_id, anchor=anchor, height=win_h) |
| 2057 |
|
| 2058 |
def refresh_open_window_positions(self, zoom: Literal["in", "out"]) -> None: |
| 2059 |
if self.text_editor.open: |
| 2060 |
r = self.text_editor.row |
| 2061 |
self.text_editor.window.config(height=self.MT.row_positions[r + 1] - self.MT.row_positions[r]) |
| 2062 |
self.text_editor.tktext.config(font=self.ops.index_font) |
| 2063 |
self.coords( |
| 2064 |
self.text_editor.canvas_id, |
| 2065 |
0, |
| 2066 |
self.MT.row_positions[r], |
| 2067 |
) |
| 2068 |
if self.dropdown.open: |
| 2069 |
if zoom == "in": |
| 2070 |
self.dropdown.window.zoom_in() |
| 2071 |
elif zoom == "out": |
| 2072 |
self.dropdown.window.zoom_out() |
| 2073 |
r = self.dropdown.get_coords() |
| 2074 |
if self.text_editor.open: |
| 2075 |
text_editor_h = self.text_editor.window.winfo_height() |
| 2076 |
win_h, anchor = self.get_dropdown_height_anchor(r, text_editor_h) |
| 2077 |
else: |
| 2078 |
text_editor_h = self.MT.row_positions[r + 1] - self.MT.row_positions[r] + 1 |
| 2079 |
anchor = self.itemcget(self.dropdown.canvas_id, "anchor") |
| 2080 |
|
| 2081 |
self.dropdown.window.config(width=self.current_width + 1) |
| 2082 |
if anchor == "nw": |
| 2083 |
self.coords( |
| 2084 |
self.dropdown.canvas_id, |
| 2085 |
0, |
| 2086 |
self.MT.row_positions[r] + text_editor_h - 1, |
| 2087 |
) |
| 2088 |
|
| 2089 |
elif anchor == "sw": |
| 2090 |
self.coords( |
| 2091 |
self.dropdown.canvas_id, |
| 2092 |
0, |
| 2093 |
self.MT.row_positions[r], |
| 2094 |
) |
| 2095 |
|
| 2096 |
|
| 2097 |
def hide_text_editor(self) -> None: |
| 2098 |
if self.text_editor.open: |
| 2099 |
for binding in text_editor_to_unbind: |
| 2100 |
self.text_editor.tktext.unbind(binding) |
| 2101 |
self.itemconfig(self.text_editor.canvas_id, state="hidden") |
| 2102 |
self.text_editor.open = False |
| 2103 |
|
| 2104 |
|
| 2105 |
def close_text_editor(self, event: tk.Event) -> Literal["break"] | None: |
| 2106 |
|
| 2107 |
|
| 2108 |
try: |
| 2109 |
focused = self.focus_get() |
| 2110 |
except Exception: |
| 2111 |
focused = None |
| 2112 |
try: |
| 2113 |
if focused == self.text_editor.tktext.rc_popup_menu: |
| 2114 |
return "break" |
| 2115 |
except Exception: |
| 2116 |
pass |
| 2117 |
if focused is None: |
| 2118 |
return "break" |
| 2119 |
if event.keysym == "Escape": |
| 2120 |
self.hide_text_editor_and_dropdown() |
| 2121 |
self.focus_set() |
| 2122 |
return |
| 2123 |
text_editor_value = self.text_editor.get() |
| 2124 |
r = self.text_editor.row |
| 2125 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 2126 |
event_data = event_dict( |
| 2127 |
name="end_edit_index", |
| 2128 |
sheet=self.PAR.name, |
| 2129 |
widget=self, |
| 2130 |
cells_index={datarn: self.get_cell_data(datarn)}, |
| 2131 |
key=event.keysym, |
| 2132 |
value=text_editor_value, |
| 2133 |
loc=r, |
| 2134 |
row=r, |
| 2135 |
boxes=self.MT.get_boxes(), |
| 2136 |
selected=self.MT.selected, |
| 2137 |
) |
| 2138 |
edited = False |
| 2139 |
set_data = partial( |
| 2140 |
self.set_cell_data_undo, |
| 2141 |
r=r, |
| 2142 |
datarn=datarn, |
| 2143 |
check_input_valid=False, |
| 2144 |
) |
| 2145 |
if self.MT.edit_validation_func: |
| 2146 |
text_editor_value = self.MT.edit_validation_func(event_data) |
| 2147 |
if text_editor_value is not None and self.input_valid_for_cell(datarn, text_editor_value): |
| 2148 |
edited = set_data(value=text_editor_value) |
| 2149 |
elif self.input_valid_for_cell(datarn, text_editor_value): |
| 2150 |
edited = set_data(value=text_editor_value) |
| 2151 |
if edited: |
| 2152 |
try_binding(self.extra_end_edit_cell_func, event_data) |
| 2153 |
self.MT.recreate_all_selection_boxes() |
| 2154 |
self.hide_text_editor_and_dropdown() |
| 2155 |
if event.keysym != "FocusOut": |
| 2156 |
self.focus_set() |
| 2157 |
return "break" |
| 2158 |
|
| 2159 |
def get_dropdown_height_anchor(self, r: int, text_editor_h: None | int = None) -> tuple[int, str]: |
| 2160 |
win_h = 5 |
| 2161 |
datarn = self.MT.datarn(r) |
| 2162 |
for i, v in enumerate(self.get_cell_kwargs(datarn, key="dropdown")["values"]): |
| 2163 |
v_numlines = len(v.split("\n") if isinstance(v, str) else f"{v}".split("\n")) |
| 2164 |
if v_numlines > 1: |
| 2165 |
win_h += self.MT.index_txt_height + (v_numlines * self.MT.index_txt_height) + 5 |
| 2166 |
else: |
| 2167 |
win_h += self.MT.min_row_height |
| 2168 |
if i == 5: |
| 2169 |
break |
| 2170 |
win_h = min(win_h, 500) |
| 2171 |
space_bot = self.MT.get_space_bot(r, text_editor_h) |
| 2172 |
space_top = int(self.MT.row_positions[r]) |
| 2173 |
anchor = "nw" |
| 2174 |
win_h2 = int(win_h) |
| 2175 |
if win_h > space_bot: |
| 2176 |
if space_bot >= space_top: |
| 2177 |
anchor = "nw" |
| 2178 |
win_h = space_bot - 1 |
| 2179 |
elif space_top > space_bot: |
| 2180 |
anchor = "sw" |
| 2181 |
win_h = space_top - 1 |
| 2182 |
if win_h < self.MT.index_txt_height + 5: |
| 2183 |
win_h = self.MT.index_txt_height + 5 |
| 2184 |
elif win_h > win_h2: |
| 2185 |
win_h = win_h2 |
| 2186 |
return win_h, anchor |
| 2187 |
|
| 2188 |
def dropdown_text_editor_modified( |
| 2189 |
self, |
| 2190 |
event: tk.Misc, |
| 2191 |
) -> None: |
| 2192 |
r = self.dropdown.get_coords() |
| 2193 |
event_data = event_dict( |
| 2194 |
name="table_dropdown_modified", |
| 2195 |
sheet=self.PAR.name, |
| 2196 |
value=self.text_editor.get(), |
| 2197 |
loc=r, |
| 2198 |
row=r, |
| 2199 |
column=0, |
| 2200 |
boxes=self.MT.get_boxes(), |
| 2201 |
selected=self.MT.selected, |
| 2202 |
) |
| 2203 |
try_binding(self.dropdown.window.modified_function, event_data) |
| 2204 |
|
| 2205 |
|
| 2206 |
if (hasattr(event, "state") and event.state & (0x0004 | 0x00000010)) or ( |
| 2207 |
hasattr(event, "keysym") and len(event.keysym) > 2 and event.keysym != "space" |
| 2208 |
): |
| 2209 |
return |
| 2210 |
self.text_editor.autocomplete(self.dropdown.window.search_and_see(event_data)) |
| 2211 |
return "break" |
| 2212 |
|
| 2213 |
def open_dropdown_window(self, r: int, event: Any = None) -> None: |
| 2214 |
self.hide_text_editor() |
| 2215 |
kwargs = self.get_cell_kwargs(self.MT.datarn(r), key="dropdown") |
| 2216 |
if kwargs["state"] == "disabled": |
| 2217 |
return |
| 2218 |
if kwargs["state"] == "normal" and not self.open_text_editor(event=event, r=r, dropdown=True): |
| 2219 |
return |
| 2220 |
win_h, anchor = self.get_dropdown_height_anchor(r) |
| 2221 |
win_w = self.current_width + 1 |
| 2222 |
if anchor == "nw": |
| 2223 |
if kwargs["state"] == "normal": |
| 2224 |
self.text_editor.window.update_idletasks() |
| 2225 |
ypos = self.MT.row_positions[r] + self.text_editor.window.winfo_height() - 1 |
| 2226 |
else: |
| 2227 |
ypos = self.MT.row_positions[r + 1] |
| 2228 |
else: |
| 2229 |
ypos = self.MT.row_positions[r] |
| 2230 |
reset_kwargs = { |
| 2231 |
"r": r, |
| 2232 |
"c": 0, |
| 2233 |
"bg": self.ops.index_editor_bg, |
| 2234 |
"fg": self.ops.index_editor_fg, |
| 2235 |
"select_bg": self.ops.index_editor_select_bg, |
| 2236 |
"select_fg": self.ops.index_editor_select_fg, |
| 2237 |
"width": win_w, |
| 2238 |
"height": win_h, |
| 2239 |
"font": self.ops.index_font, |
| 2240 |
"ops": self.ops, |
| 2241 |
"outline_color": self.ops.index_selected_rows_bg, |
| 2242 |
"align": self.get_cell_align(r), |
| 2243 |
"values": kwargs["values"], |
| 2244 |
"search_function": kwargs["search_function"], |
| 2245 |
"modified_function": kwargs["modified_function"], |
| 2246 |
} |
| 2247 |
if self.dropdown.window: |
| 2248 |
self.dropdown.window.reset(**reset_kwargs) |
| 2249 |
self.itemconfig(self.dropdown.canvas_id, state="normal", anchor=anchor) |
| 2250 |
self.coords(self.dropdown.canvas_id, 0, ypos) |
| 2251 |
self.dropdown.window.tkraise() |
| 2252 |
else: |
| 2253 |
self.dropdown.window = self.PAR._dropdown_cls( |
| 2254 |
self.winfo_toplevel(), |
| 2255 |
**reset_kwargs, |
| 2256 |
single_index="r", |
| 2257 |
close_dropdown_window=self.close_dropdown_window, |
| 2258 |
arrowkey_RIGHT=self.MT.arrowkey_RIGHT, |
| 2259 |
arrowkey_LEFT=self.MT.arrowkey_LEFT, |
| 2260 |
) |
| 2261 |
self.dropdown.canvas_id = self.create_window( |
| 2262 |
(0, ypos), |
| 2263 |
window=self.dropdown.window, |
| 2264 |
anchor=anchor, |
| 2265 |
) |
| 2266 |
self.update_idletasks() |
| 2267 |
if kwargs["state"] == "normal": |
| 2268 |
self.text_editor.tktext.bind( |
| 2269 |
"<KeyRelease>", |
| 2270 |
self.dropdown_text_editor_modified, |
| 2271 |
) |
| 2272 |
try: |
| 2273 |
self.after(1, lambda: self.text_editor.tktext.focus()) |
| 2274 |
self.after(2, self.text_editor.window.scroll_to_bottom()) |
| 2275 |
except Exception: |
| 2276 |
return |
| 2277 |
redraw = False |
| 2278 |
else: |
| 2279 |
self.dropdown.window.bind("<FocusOut>", lambda _x: self.close_dropdown_window(r)) |
| 2280 |
self.dropdown.window.bind("<Escape>", self.close_dropdown_window) |
| 2281 |
self.dropdown.window.focus_set() |
| 2282 |
redraw = True |
| 2283 |
self.dropdown.open = True |
| 2284 |
if redraw: |
| 2285 |
self.MT.main_table_redraw_grid_and_text(redraw_header=False, redraw_row_index=True, redraw_table=False) |
| 2286 |
|
| 2287 |
|
| 2288 |
def close_dropdown_window( |
| 2289 |
self, |
| 2290 |
r: int | None = None, |
| 2291 |
selection: Any = None, |
| 2292 |
redraw: bool = True, |
| 2293 |
) -> None: |
| 2294 |
if r is not None and selection is not None: |
| 2295 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 2296 |
kwargs = self.get_cell_kwargs(datarn, key="dropdown") |
| 2297 |
event_data = event_dict( |
| 2298 |
name="end_edit_index", |
| 2299 |
sheet=self.PAR.name, |
| 2300 |
widget=self, |
| 2301 |
cells_header={datarn: self.get_cell_data(datarn)}, |
| 2302 |
key="??", |
| 2303 |
value=selection, |
| 2304 |
loc=r, |
| 2305 |
row=r, |
| 2306 |
boxes=self.MT.get_boxes(), |
| 2307 |
selected=self.MT.selected, |
| 2308 |
) |
| 2309 |
try_binding(kwargs["select_function"], event_data) |
| 2310 |
selection = selection if not self.MT.edit_validation_func else self.MT.edit_validation_func(event_data) |
| 2311 |
if selection is not None: |
| 2312 |
edited = self.set_cell_data_undo(r, datarn=datarn, value=selection, redraw=not redraw) |
| 2313 |
if edited: |
| 2314 |
try_binding(self.extra_end_edit_cell_func, event_data) |
| 2315 |
self.MT.recreate_all_selection_boxes() |
| 2316 |
self.focus_set() |
| 2317 |
self.hide_text_editor_and_dropdown(redraw=redraw) |
| 2318 |
|
| 2319 |
def hide_text_editor_and_dropdown(self, redraw: bool = True) -> None: |
| 2320 |
self.hide_text_editor() |
| 2321 |
self.hide_dropdown_window() |
| 2322 |
if redraw: |
| 2323 |
self.MT.refresh() |
| 2324 |
|
| 2325 |
def mouseclick_outside_editor_or_dropdown(self, inside: bool = False) -> int | None: |
| 2326 |
closed_dd_coords = self.dropdown.get_coords() |
| 2327 |
if self.text_editor.open: |
| 2328 |
self.close_text_editor(new_tk_event("ButtonPress-1")) |
| 2329 |
if closed_dd_coords is not None: |
| 2330 |
self.hide_dropdown_window() |
| 2331 |
if inside: |
| 2332 |
self.MT.main_table_redraw_grid_and_text( |
| 2333 |
redraw_header=False, |
| 2334 |
redraw_row_index=True, |
| 2335 |
redraw_table=False, |
| 2336 |
) |
| 2337 |
return closed_dd_coords |
| 2338 |
|
| 2339 |
def mouseclick_outside_editor_or_dropdown_all_canvases(self, inside: bool = False) -> int | None: |
| 2340 |
self.CH.mouseclick_outside_editor_or_dropdown() |
| 2341 |
self.MT.mouseclick_outside_editor_or_dropdown() |
| 2342 |
return self.mouseclick_outside_editor_or_dropdown(inside) |
| 2343 |
|
| 2344 |
def hide_dropdown_window(self) -> None: |
| 2345 |
if self.dropdown.open: |
| 2346 |
self.dropdown.window.unbind("<FocusOut>") |
| 2347 |
self.itemconfig(self.dropdown.canvas_id, state="hidden") |
| 2348 |
self.dropdown.open = False |
| 2349 |
|
| 2350 |
|
| 2351 |
def set_cell_data_undo( |
| 2352 |
self, |
| 2353 |
r: int = 0, |
| 2354 |
datarn: None | int = None, |
| 2355 |
value: str = "", |
| 2356 |
cell_resize: bool = True, |
| 2357 |
undo: bool = True, |
| 2358 |
redraw: bool = True, |
| 2359 |
check_input_valid: bool = True, |
| 2360 |
) -> bool: |
| 2361 |
if datarn is None: |
| 2362 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 2363 |
event_data = event_dict( |
| 2364 |
name="edit_index", |
| 2365 |
sheet=self.PAR.name, |
| 2366 |
widget=self, |
| 2367 |
cells_index={datarn: self.get_cell_data(datarn)}, |
| 2368 |
boxes=self.MT.get_boxes(), |
| 2369 |
selected=self.MT.selected, |
| 2370 |
) |
| 2371 |
edited = False |
| 2372 |
if isinstance(self.MT._row_index, int): |
| 2373 |
dispcn = self.MT.try_dispcn(self.MT._row_index) |
| 2374 |
edited = self.MT.set_cell_data_undo( |
| 2375 |
r=r, |
| 2376 |
c=dispcn if isinstance(dispcn, int) else 0, |
| 2377 |
datarn=datarn, |
| 2378 |
datacn=self.MT._row_index, |
| 2379 |
value=value, |
| 2380 |
undo=True, |
| 2381 |
cell_resize=isinstance(dispcn, int), |
| 2382 |
) |
| 2383 |
else: |
| 2384 |
self.fix_index(datarn) |
| 2385 |
if not check_input_valid or self.input_valid_for_cell(datarn, value): |
| 2386 |
if self.MT.undo_enabled and undo: |
| 2387 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 2388 |
self.set_cell_data(datarn=datarn, value=value) |
| 2389 |
edited = True |
| 2390 |
if edited and cell_resize and self.ops.cell_auto_resize_enabled: |
| 2391 |
self.set_row_height_run_binding(r, only_if_too_small=False) |
| 2392 |
if redraw: |
| 2393 |
self.MT.refresh() |
| 2394 |
if edited: |
| 2395 |
self.MT.sheet_modified(event_data) |
| 2396 |
return edited |
| 2397 |
|
| 2398 |
def set_cell_data(self, datarn: int | None = None, value: Any = "") -> None: |
| 2399 |
if isinstance(self.MT._row_index, int): |
| 2400 |
self.MT.set_cell_data(datarn=datarn, datacn=self.MT._row_index, value=value) |
| 2401 |
else: |
| 2402 |
self.fix_index(datarn) |
| 2403 |
if self.get_cell_kwargs(datarn, key="checkbox"): |
| 2404 |
self.MT._row_index[datarn] = try_to_bool(value) |
| 2405 |
elif self.ops.treeview: |
| 2406 |
self.MT._row_index[datarn].text = value |
| 2407 |
else: |
| 2408 |
self.MT._row_index[datarn] = value |
| 2409 |
|
| 2410 |
def input_valid_for_cell(self, datarn: int, value: Any, check_readonly: bool = True) -> bool: |
| 2411 |
kwargs = self.get_cell_kwargs(datarn, key=None) |
| 2412 |
if check_readonly and "readonly" in kwargs: |
| 2413 |
return False |
| 2414 |
elif "checkbox" in kwargs: |
| 2415 |
return is_bool_like(value) |
| 2416 |
else: |
| 2417 |
return not ( |
| 2418 |
self.cell_equal_to(datarn, value) |
| 2419 |
or (kwargs := kwargs.get("dropdown", {})) |
| 2420 |
and kwargs["validate_input"] |
| 2421 |
and value not in kwargs["values"] |
| 2422 |
) |
| 2423 |
|
| 2424 |
def cell_equal_to(self, datarn: int, value: Any) -> bool: |
| 2425 |
self.fix_index(datarn) |
| 2426 |
if isinstance(self.MT._row_index, list): |
| 2427 |
return self.MT._row_index[datarn] == value |
| 2428 |
elif isinstance(self.MT._row_index, int): |
| 2429 |
return self.MT.cell_equal_to(datarn, self.MT._row_index, value) |
| 2430 |
|
| 2431 |
def get_cell_data( |
| 2432 |
self, |
| 2433 |
datarn: int, |
| 2434 |
get_displayed: bool = False, |
| 2435 |
none_to_empty_str: bool = False, |
| 2436 |
redirect_int: bool = False, |
| 2437 |
) -> Any: |
| 2438 |
if get_displayed: |
| 2439 |
return self.cell_str(datarn, fix=False) |
| 2440 |
if redirect_int and isinstance(self.MT._row_index, int): |
| 2441 |
return self.MT.get_cell_data(datarn, self.MT._row_index, none_to_empty_str=True) |
| 2442 |
if ( |
| 2443 |
isinstance(self.MT._row_index, int) |
| 2444 |
or not self.MT._row_index |
| 2445 |
or datarn >= len(self.MT._row_index) |
| 2446 |
or (self.MT._row_index[datarn] is None and none_to_empty_str) |
| 2447 |
): |
| 2448 |
return "" |
| 2449 |
if self.ops.treeview: |
| 2450 |
return self.MT._row_index[datarn].text |
| 2451 |
return self.MT._row_index[datarn] |
| 2452 |
|
| 2453 |
def cell_str(self, datarn: int, fix: bool = True) -> str: |
| 2454 |
kwargs = self.get_cell_kwargs(datarn, key="dropdown") |
| 2455 |
if kwargs: |
| 2456 |
if kwargs["text"] is not None: |
| 2457 |
return f"{kwargs['text']}" |
| 2458 |
else: |
| 2459 |
kwargs = self.get_cell_kwargs(datarn, key="checkbox") |
| 2460 |
if kwargs: |
| 2461 |
return f"{kwargs['text']}" |
| 2462 |
if isinstance(self.MT._row_index, int): |
| 2463 |
return self.MT.cell_str(datarn, self.MT._row_index, get_displayed=True) |
| 2464 |
if fix: |
| 2465 |
self.fix_index(datarn) |
| 2466 |
try: |
| 2467 |
value = self.MT._row_index[datarn] |
| 2468 |
if value is None: |
| 2469 |
value = "" |
| 2470 |
elif isinstance(value, Node): |
| 2471 |
value = value.text |
| 2472 |
elif not isinstance(value, str): |
| 2473 |
value = f"{value}" |
| 2474 |
except Exception: |
| 2475 |
value = "" |
| 2476 |
if not value and self.ops.show_default_index_for_empty: |
| 2477 |
value = get_n2a(datarn, self.ops.default_row_index) |
| 2478 |
return value |
| 2479 |
|
| 2480 |
def get_value_for_empty_cell(self, datarn: int, r_ops: bool = True) -> Any: |
| 2481 |
if self.ops.treeview: |
| 2482 |
iid = self.new_iid() |
| 2483 |
return Node(text=iid, iid=iid, parent=self.get_row_parent(datarn)) |
| 2484 |
kwargs = self.get_cell_kwargs(datarn, key=None, cell=r_ops) |
| 2485 |
if "checkbox" in kwargs: |
| 2486 |
return False |
| 2487 |
elif "dropdown" in kwargs and kwargs["dropdown"]["validate_input"] and kwargs["dropdown"]["values"]: |
| 2488 |
return kwargs["dropdown"]["values"][0] |
| 2489 |
else: |
| 2490 |
return "" |
| 2491 |
|
| 2492 |
def get_empty_index_seq(self, end: int, start: int = 0, r_ops: bool = True) -> list[Any]: |
| 2493 |
return [self.get_value_for_empty_cell(datarn, r_ops=r_ops) for datarn in range(start, end)] |
| 2494 |
|
| 2495 |
def fix_index(self, datarn: int | None = None) -> None: |
| 2496 |
if isinstance(self.MT._row_index, int): |
| 2497 |
return |
| 2498 |
if isinstance(self.MT._row_index, float): |
| 2499 |
self.MT._row_index = int(self.MT._row_index) |
| 2500 |
return |
| 2501 |
if not isinstance(self.MT._row_index, list): |
| 2502 |
try: |
| 2503 |
self.MT._row_index = list(self.MT._row_index) |
| 2504 |
except Exception: |
| 2505 |
self.MT._row_index = [] |
| 2506 |
if isinstance(datarn, int) and datarn >= len(self.MT._row_index): |
| 2507 |
self.MT._row_index.extend(self.get_empty_index_seq(end=datarn + 1, start=len(self.MT._row_index))) |
| 2508 |
|
| 2509 |
def set_row_height_run_binding(self, r: int, only_if_too_small: bool = True) -> None: |
| 2510 |
old_height = self.MT.row_positions[r + 1] - self.MT.row_positions[r] |
| 2511 |
new_height = self.set_row_height(r, only_if_too_small=only_if_too_small) |
| 2512 |
if self.row_height_resize_func is not None and old_height != new_height: |
| 2513 |
self.row_height_resize_func( |
| 2514 |
event_dict( |
| 2515 |
name="resize", |
| 2516 |
sheet=self.PAR.name, |
| 2517 |
resized_rows={r: {"old_size": old_height, "new_size": new_height}}, |
| 2518 |
) |
| 2519 |
) |
| 2520 |
|
| 2521 |
|
| 2522 |
def click_checkbox(self, r: int, datarn: int | None = None, undo: bool = True, redraw: bool = True) -> None: |
| 2523 |
if datarn is None: |
| 2524 |
datarn = r if self.MT.all_rows_displayed else self.MT.displayed_rows[r] |
| 2525 |
kwargs = self.get_cell_kwargs(datarn, key="checkbox") |
| 2526 |
if kwargs["state"] == "normal": |
| 2527 |
pre_edit_value = self.get_cell_data(datarn) |
| 2528 |
if isinstance(self.MT._row_index, list): |
| 2529 |
value = not self.MT._row_index[datarn] if isinstance(self.MT._row_index[datarn], bool) else False |
| 2530 |
elif isinstance(self.MT._row_index, int): |
| 2531 |
value = ( |
| 2532 |
not self.MT.data[datarn][self.MT._row_index] |
| 2533 |
if isinstance(self.MT.data[datarn][self.MT._row_index], bool) |
| 2534 |
else False |
| 2535 |
) |
| 2536 |
else: |
| 2537 |
value = False |
| 2538 |
self.set_cell_data_undo(r, datarn=datarn, value=value, cell_resize=False) |
| 2539 |
event_data = event_dict( |
| 2540 |
name="end_edit_index", |
| 2541 |
sheet=self.PAR.name, |
| 2542 |
widget=self, |
| 2543 |
cells_index={datarn: pre_edit_value}, |
| 2544 |
key="??", |
| 2545 |
value=value, |
| 2546 |
loc=r, |
| 2547 |
row=r, |
| 2548 |
boxes=self.MT.get_boxes(), |
| 2549 |
selected=self.MT.selected, |
| 2550 |
) |
| 2551 |
if kwargs["check_function"] is not None: |
| 2552 |
kwargs["check_function"](event_data) |
| 2553 |
try_binding(self.extra_end_edit_cell_func, event_data) |
| 2554 |
if redraw: |
| 2555 |
self.MT.refresh() |
| 2556 |
|
| 2557 |
def get_cell_kwargs(self, datarn: int, key: Hashable | None = "dropdown", cell: bool = True) -> dict: |
| 2558 |
if cell and datarn in self.cell_options: |
| 2559 |
return self.cell_options[datarn] if key is None else self.cell_options[datarn].get(key, {}) |
| 2560 |
else: |
| 2561 |
return {} |
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
def tree_reset(self) -> None: |
| 2566 |
self.tree_open_ids = set() |
| 2567 |
self.rns = {} |
| 2568 |
if self.MT: |
| 2569 |
self.MT.displayed_rows = [] |
| 2570 |
self.MT._row_index = [] |
| 2571 |
self.MT.data = [] |
| 2572 |
self.MT.row_positions = [0] |
| 2573 |
self.MT.saved_row_heights = {} |
| 2574 |
|
| 2575 |
def new_iid(self) -> str: |
| 2576 |
self.new_iid_ctr += 1 |
| 2577 |
while (iid := f"{num2alpha(self.new_iid_ctr)}") in self.rns: |
| 2578 |
self.new_iid_ctr += 1 |
| 2579 |
return iid |
| 2580 |
|
| 2581 |
def get_row_parent(self, r: int) -> str: |
| 2582 |
if r >= len(self.MT._row_index): |
| 2583 |
return "" |
| 2584 |
else: |
| 2585 |
return self.MT._row_index[r].parent |
| 2586 |
|
| 2587 |
def tree_del_rows(self, event_data: EventDataDict) -> EventDataDict: |
| 2588 |
event_data["treeview"]["nodes"] = {} |
| 2589 |
for node in event_data["deleted"]["index"].values(): |
| 2590 |
iid = node.iid |
| 2591 |
if parent_node := self.parent_node(iid): |
| 2592 |
if parent_node.iid not in event_data["treeview"]["nodes"]: |
| 2593 |
event_data["treeview"]["nodes"][parent_node.iid] = Node( |
| 2594 |
text=parent_node.text, |
| 2595 |
iid=parent_node.iid, |
| 2596 |
parent=parent_node.parent, |
| 2597 |
children=parent_node.children.copy(), |
| 2598 |
) |
| 2599 |
self.remove_iid_from_parents_children(iid) |
| 2600 |
for node in event_data["deleted"]["index"].values(): |
| 2601 |
iid = node.iid |
| 2602 |
for did in self.get_iid_descendants(iid): |
| 2603 |
self.tree_open_ids.discard(did) |
| 2604 |
self.tree_open_ids.discard(iid) |
| 2605 |
|
| 2606 |
for datarn in reversed(event_data["deleted"]["index"]): |
| 2607 |
del self.MT._row_index[datarn] |
| 2608 |
|
| 2609 |
return event_data |
| 2610 |
|
| 2611 |
def tree_add_rows(self, event_data: EventDataDict) -> EventDataDict: |
| 2612 |
for rn, node in event_data["added"]["rows"]["index"].items(): |
| 2613 |
self.rns[node.iid] = rn |
| 2614 |
|
| 2615 |
if event_data["treeview"]["nodes"]: |
| 2616 |
self.restore_nodes(event_data=event_data) |
| 2617 |
else: |
| 2618 |
row, a_node = next(iter(event_data["added"]["rows"]["index"].items())) |
| 2619 |
if parent := a_node.parent: |
| 2620 |
if self.iid_children(parent): |
| 2621 |
index = next( |
| 2622 |
(i for i, cid in enumerate(self.iid_children(parent)) if self.rns[cid] >= row), |
| 2623 |
len(self.iid_children(parent)), |
| 2624 |
) |
| 2625 |
self.iid_children(parent)[index:index] = [ |
| 2626 |
n.iid for n in event_data["added"]["rows"]["index"].values() |
| 2627 |
] |
| 2628 |
else: |
| 2629 |
self.iid_children(parent).extend(n.iid for n in event_data["added"]["rows"]["index"].values()) |
| 2630 |
|
| 2631 |
|
| 2632 |
event_data["added"]["rows"]["row_heights"] = {} |
| 2633 |
if parent and self.PAR.item_displayed(parent) and parent in self.tree_open_ids: |
| 2634 |
self.MT.displayed_rows = add_to_displayed(self.MT.displayed_rows, event_data["added"]["rows"]["index"]) |
| 2635 |
disp_idx = self.MT.disprn(self.rns[a_node.iid]) |
| 2636 |
h = self.MT.get_default_row_height() |
| 2637 |
for i in range(len(event_data["added"]["rows"]["index"])): |
| 2638 |
event_data["added"]["rows"]["row_heights"][disp_idx + i] = h |
| 2639 |
|
| 2640 |
elif not parent: |
| 2641 |
self.MT.displayed_rows = add_to_displayed(self.MT.displayed_rows, event_data["added"]["rows"]["index"]) |
| 2642 |
h = self.MT.get_default_row_height() |
| 2643 |
for datarn in event_data["added"]["rows"]["index"]: |
| 2644 |
event_data["added"]["rows"]["row_heights"][self.MT.disprn(datarn)] = h |
| 2645 |
|
| 2646 |
return event_data |
| 2647 |
|
| 2648 |
def move_rows_mod_nodes( |
| 2649 |
self, |
| 2650 |
data_new_idxs: dict[int, int], |
| 2651 |
data_old_idxs: dict[int, int], |
| 2652 |
disp_new_idxs: dict[int, int], |
| 2653 |
maxidx: int, |
| 2654 |
event_data: EventDataDict, |
| 2655 |
undo_modification: EventDataDict | None = None, |
| 2656 |
node_change: tuple[str, str, int] | None = None, |
| 2657 |
) -> Generator[tuple[dict[int, int], dict[int, int], dict[str, int], EventDataDict]] | None: |
| 2658 |
|
| 2659 |
|
| 2660 |
if not event_data["treeview"]["nodes"]: |
| 2661 |
if undo_modification: |
| 2662 |
""" |
| 2663 |
Used by undo/redo |
| 2664 |
""" |
| 2665 |
event_data = self.copy_nodes(undo_modification["treeview"]["nodes"], event_data) |
| 2666 |
self.restore_nodes(undo_modification) |
| 2667 |
|
| 2668 |
elif event_data["moved"]["rows"]: |
| 2669 |
if node_change: |
| 2670 |
item = node_change[0] |
| 2671 |
moved_rows = [self.rns[item]] |
| 2672 |
new_parent = node_change[1] |
| 2673 |
move_to_index = node_change[2] |
| 2674 |
if new_parent: |
| 2675 |
if isinstance(move_to_index, int): |
| 2676 |
move_to_index = min(move_to_index, len(self.iid_children(new_parent))) |
| 2677 |
else: |
| 2678 |
move_to_index = len(self.iid_children(new_parent)) |
| 2679 |
move_to_row = self.rns[new_parent] |
| 2680 |
_find = move_to_index + 1 if new_parent == self.iid_parent(item) else move_to_index |
| 2681 |
move_to_row += _find + sum( |
| 2682 |
self.num_descendants(cid) for cid in islice(self.iid_children(new_parent), _find) |
| 2683 |
) |
| 2684 |
insert_row = move_to_row + 1 |
| 2685 |
else: |
| 2686 |
num_top_nodes = sum(1 for _ in self.gen_top_nodes()) |
| 2687 |
if move_to_index is None: |
| 2688 |
move_to_row = self.PAR.top_index_row(num_top_nodes - 1) |
| 2689 |
move_to_index = num_top_nodes |
| 2690 |
insert_row = move_to_row |
| 2691 |
else: |
| 2692 |
move_to_row = self.PAR.top_index_row(move_to_index) |
| 2693 |
insert_row = move_to_row |
| 2694 |
if move_to_row is None: |
| 2695 |
move_to_row = self.PAR.top_index_row(num_top_nodes - 1) |
| 2696 |
move_to_index = num_top_nodes |
| 2697 |
insert_row = move_to_row + 1 |
| 2698 |
|
| 2699 |
move_to_iid = self.MT._row_index[move_to_row].iid |
| 2700 |
disp_insert_row = None |
| 2701 |
|
| 2702 |
else: |
| 2703 |
iids = {self.MT._row_index[r].iid for r in event_data["moved"]["rows"]["data"]} |
| 2704 |
iids_descendants = {iid: set(self.get_iid_descendants(iid)) for iid in iids} |
| 2705 |
|
| 2706 |
|
| 2707 |
iids -= set.union(*iids_descendants.values()) & iids |
| 2708 |
moved_rows = sorted(map(self.rns.__getitem__, iids)) |
| 2709 |
item = self.MT._row_index[moved_rows[0]].iid |
| 2710 |
|
| 2711 |
if isinstance(event_data.value, int): |
| 2712 |
disp_insert_row = event_data.value |
| 2713 |
if disp_insert_row >= len(self.MT.displayed_rows): |
| 2714 |
insert_row = len(self.MT._row_index) |
| 2715 |
else: |
| 2716 |
insert_row = self.MT.datarn(disp_insert_row) |
| 2717 |
move_to_iid = self.MT._row_index[min(insert_row, len(self.MT._row_index) - 1)].iid |
| 2718 |
|
| 2719 |
else: |
| 2720 |
disp_insert_row = None |
| 2721 |
min_from = min(event_data["moved"]["rows"]["data"]) |
| 2722 |
|
| 2723 |
min_to = min(event_data["moved"]["rows"]["data"].values()) |
| 2724 |
max_to = max(event_data["moved"]["rows"]["data"].values()) |
| 2725 |
insert_row = max_to if min_from <= min_to else min_to |
| 2726 |
move_to_row = insert_row |
| 2727 |
move_to_iid = self.MT._row_index[insert_row].iid |
| 2728 |
|
| 2729 |
move_to_index = self.PAR.index(move_to_iid) |
| 2730 |
new_parent = self.iid_parent(move_to_iid) |
| 2731 |
|
| 2732 |
event_data["moved"]["rows"]["data"] = {moved_rows[0]: insert_row} |
| 2733 |
|
| 2734 |
new_loc_is_displayed = not new_parent or ( |
| 2735 |
new_parent and new_parent in self.tree_open_ids and self.PAR.item_displayed(new_parent) |
| 2736 |
) |
| 2737 |
|
| 2738 |
event_data["moved"]["rows"]["displayed"] = {} |
| 2739 |
with suppress(Exception): |
| 2740 |
if new_loc_is_displayed: |
| 2741 |
if disp_insert_row is None: |
| 2742 |
if new_parent or insert_row > move_to_row: |
| 2743 |
disp_insert_row = self.MT.disprn(self.rns[move_to_iid]) + 1 |
| 2744 |
else: |
| 2745 |
disp_insert_row = self.MT.disprn(self.rns[move_to_iid]) |
| 2746 |
if (disp_from_row := self.MT.try_disprn(self.rns[item])) is not None: |
| 2747 |
event_data["moved"]["rows"]["displayed"] = {disp_from_row: disp_insert_row} |
| 2748 |
else: |
| 2749 |
event_data["moved"]["rows"]["displayed"] = {(): disp_insert_row} |
| 2750 |
|
| 2751 |
if any(self.move_pid_causes_recursive_loop(self.MT._row_index[r].iid, new_parent) for r in moved_rows): |
| 2752 |
event_data["moved"]["rows"] = {} |
| 2753 |
data_new_idxs, data_old_idxs, disp_new_idxs = {}, {}, {} |
| 2754 |
|
| 2755 |
else: |
| 2756 |
for r in moved_rows: |
| 2757 |
iid = self.MT._row_index[r].iid |
| 2758 |
event_data = self.move_node( |
| 2759 |
event_data=event_data, |
| 2760 |
item=iid, |
| 2761 |
parent=new_parent, |
| 2762 |
index=move_to_index, |
| 2763 |
) |
| 2764 |
move_to_index += 1 |
| 2765 |
event_data["moved"]["rows"]["data"] = get_new_indexes( |
| 2766 |
insert_row, |
| 2767 |
event_data["moved"]["rows"]["data"], |
| 2768 |
) |
| 2769 |
data_new_idxs = event_data["moved"]["rows"]["data"] |
| 2770 |
data_old_idxs = {v: k for k, v in data_new_idxs.items()} |
| 2771 |
|
| 2772 |
if () in event_data["moved"]["rows"]["displayed"]: |
| 2773 |
del event_data["moved"]["rows"]["displayed"][()] |
| 2774 |
|
| 2775 |
if event_data["moved"]["rows"]["displayed"]: |
| 2776 |
event_data["moved"]["rows"]["displayed"] = get_new_indexes( |
| 2777 |
disp_insert_row, |
| 2778 |
event_data["moved"]["rows"]["displayed"], |
| 2779 |
) |
| 2780 |
disp_new_idxs = event_data["moved"]["rows"]["displayed"] |
| 2781 |
|
| 2782 |
if data_new_idxs: |
| 2783 |
self.MT.move_rows_data(data_new_idxs, data_old_idxs, maxidx) |
| 2784 |
|
| 2785 |
yield data_new_idxs, data_old_idxs, disp_new_idxs, event_data |
| 2786 |
|
| 2787 |
if not undo_modification and data_new_idxs: |
| 2788 |
if new_parent and (not self.PAR.item_displayed(new_parent) or new_parent not in self.tree_open_ids): |
| 2789 |
self.PAR.hide_rows(set(data_new_idxs.values()), data_indexes=True) |
| 2790 |
if new_loc_is_displayed: |
| 2791 |
self.PAR.show_rows( |
| 2792 |
(r for r in data_new_idxs.values() if self.ancestors_all_open(self.MT._row_index[r].iid)) |
| 2793 |
) |
| 2794 |
|
| 2795 |
yield None |
| 2796 |
|
| 2797 |
def move_node( |
| 2798 |
self, |
| 2799 |
event_data: EventDataDict, |
| 2800 |
item: str, |
| 2801 |
parent: str | None = None, |
| 2802 |
index: int = 0, |
| 2803 |
) -> EventDataDict: |
| 2804 |
|
| 2805 |
if parent is None: |
| 2806 |
parent = self.iid_parent(item) |
| 2807 |
|
| 2808 |
item_node = self.iid_node(item) |
| 2809 |
|
| 2810 |
|
| 2811 |
if parent: |
| 2812 |
parent_node = self.iid_node(parent) |
| 2813 |
|
| 2814 |
if parent == item_node.parent: |
| 2815 |
event_data = self.copy_nodes((item, parent), event_data) |
| 2816 |
pop_index = parent_node.children.index(item) |
| 2817 |
parent_node.children.insert(index, parent_node.children.pop(pop_index)) |
| 2818 |
|
| 2819 |
else: |
| 2820 |
if item_node.parent: |
| 2821 |
event_data = self.copy_nodes((item, item_node.parent, parent), event_data) |
| 2822 |
else: |
| 2823 |
event_data = self.copy_nodes((item, parent), event_data) |
| 2824 |
self.remove_iid_from_parents_children(item) |
| 2825 |
item_node.parent = parent_node.iid |
| 2826 |
parent_node.children.insert(index, item) |
| 2827 |
|
| 2828 |
|
| 2829 |
else: |
| 2830 |
if item_node.parent: |
| 2831 |
event_data = self.copy_nodes((item, item_node.parent), event_data) |
| 2832 |
else: |
| 2833 |
event_data = self.copy_nodes((item,), event_data) |
| 2834 |
self.remove_iid_from_parents_children(item) |
| 2835 |
self.MT._row_index[self.rns[item]].parent = "" |
| 2836 |
|
| 2837 |
|
| 2838 |
mapping = event_data["moved"]["rows"]["data"] |
| 2839 |
row_ctr = next(reversed(mapping.values())) + 1 |
| 2840 |
|
| 2841 |
if disp_mapping := event_data["moved"]["rows"]["displayed"]: |
| 2842 |
if () in disp_mapping: |
| 2843 |
disp_row_ctr = next(reversed(disp_mapping.values())) |
| 2844 |
else: |
| 2845 |
disp_row_ctr = next(reversed(disp_mapping.values())) + 1 |
| 2846 |
|
| 2847 |
rn = self.rns[item] |
| 2848 |
if rn not in mapping: |
| 2849 |
mapping[rn] = row_ctr |
| 2850 |
row_ctr += 1 |
| 2851 |
if disp_mapping and (disp_from := self.MT.try_disprn(rn)) is not None: |
| 2852 |
disp_mapping = del_placeholder_dict_key(disp_mapping, disp_from, disp_row_ctr) |
| 2853 |
disp_row_ctr += 1 |
| 2854 |
|
| 2855 |
for did in self.get_iid_descendants(item): |
| 2856 |
mapping[self.rns[did]] = row_ctr |
| 2857 |
row_ctr += 1 |
| 2858 |
if disp_mapping and (disp_from := self.MT.try_disprn(self.rns[did])) is not None: |
| 2859 |
disp_mapping = del_placeholder_dict_key(disp_mapping, disp_from, disp_row_ctr) |
| 2860 |
disp_row_ctr += 1 |
| 2861 |
|
| 2862 |
event_data["moved"]["rows"]["data"] = mapping |
| 2863 |
event_data["moved"]["rows"]["displayed"] = disp_mapping |
| 2864 |
|
| 2865 |
return event_data |
| 2866 |
|
| 2867 |
def restore_nodes(self, event_data: EventDataDict) -> None: |
| 2868 |
for iid, node in event_data["treeview"]["nodes"].items(): |
| 2869 |
self.MT._row_index[self.rns[iid]] = node |
| 2870 |
|
| 2871 |
def copy_node(self, item: str) -> Node: |
| 2872 |
n = self.iid_node(item) |
| 2873 |
return Node( |
| 2874 |
text=n.text, |
| 2875 |
iid=n.iid, |
| 2876 |
parent=n.parent, |
| 2877 |
children=n.children.copy(), |
| 2878 |
) |
| 2879 |
|
| 2880 |
def copy_nodes(self, items: Iterator[str], event_data: EventDataDict) -> EventDataDict: |
| 2881 |
nodes = event_data["treeview"]["nodes"] |
| 2882 |
for iid in items: |
| 2883 |
if iid not in nodes: |
| 2884 |
n = self.iid_node(iid) |
| 2885 |
nodes[iid] = Node( |
| 2886 |
text=n.text, |
| 2887 |
iid=n.iid, |
| 2888 |
parent=n.parent, |
| 2889 |
children=n.children.copy(), |
| 2890 |
) |
| 2891 |
return event_data |
| 2892 |
|
| 2893 |
def get_node_level(self, node: Node) -> int: |
| 2894 |
level = 0 |
| 2895 |
while node.parent: |
| 2896 |
level += 1 |
| 2897 |
node = self.MT._row_index[self.rns[node.parent]] |
| 2898 |
return level |
| 2899 |
|
| 2900 |
def ancestors_all_open(self, iid: str, stop_at: str = "") -> bool: |
| 2901 |
if stop_at: |
| 2902 |
for i in self.get_iid_ancestors(iid): |
| 2903 |
if i == stop_at: |
| 2904 |
return True |
| 2905 |
elif i not in self.tree_open_ids: |
| 2906 |
return False |
| 2907 |
return True |
| 2908 |
return all(map(self.tree_open_ids.__contains__, self.get_iid_ancestors(iid))) |
| 2909 |
|
| 2910 |
def get_iid_ancestors(self, iid: str) -> Generator[str]: |
| 2911 |
current_iid = iid |
| 2912 |
while self.MT._row_index[self.rns[current_iid]].parent: |
| 2913 |
parent_iid = self.MT._row_index[self.rns[current_iid]].parent |
| 2914 |
yield parent_iid |
| 2915 |
current_iid = parent_iid |
| 2916 |
|
| 2917 |
def get_iid_descendants(self, iid: str, check_open: bool = False) -> Generator[str]: |
| 2918 |
index = self.MT._row_index |
| 2919 |
stack = [iter(index[self.rns[iid]].children)] |
| 2920 |
while stack: |
| 2921 |
top_iterator = stack[-1] |
| 2922 |
try: |
| 2923 |
ciid = next(top_iterator) |
| 2924 |
yield ciid |
| 2925 |
if index[self.rns[ciid]].children and (not check_open or ciid in self.tree_open_ids): |
| 2926 |
stack.append(iter(index[self.rns[ciid]].children)) |
| 2927 |
except StopIteration: |
| 2928 |
stack.pop() |
| 2929 |
|
| 2930 |
def num_descendants(self, iid: str) -> int: |
| 2931 |
index = self.MT._row_index |
| 2932 |
stack = [iter(index[self.rns[iid]].children)] |
| 2933 |
num = 0 |
| 2934 |
while stack: |
| 2935 |
top_iterator = stack[-1] |
| 2936 |
try: |
| 2937 |
ciid = next(top_iterator) |
| 2938 |
num += 1 |
| 2939 |
if index[self.rns[ciid]].children: |
| 2940 |
stack.append(iter(index[self.rns[ciid]].children)) |
| 2941 |
except StopIteration: |
| 2942 |
stack.pop() |
| 2943 |
return num |
| 2944 |
|
| 2945 |
def iid_node(self, iid: str) -> Node: |
| 2946 |
return self.MT._row_index[self.rns[iid]] |
| 2947 |
|
| 2948 |
def iid_parent(self, iid: str) -> str: |
| 2949 |
return self.MT._row_index[self.rns[iid]].parent |
| 2950 |
|
| 2951 |
def iid_children(self, iid: str) -> list[str]: |
| 2952 |
return self.MT._row_index[self.rns[iid]].children |
| 2953 |
|
| 2954 |
def parent_node(self, iid: str) -> Node | Literal[""]: |
| 2955 |
if self.MT._row_index[self.rns[iid]].parent: |
| 2956 |
return self.MT._row_index[self.rns[self.MT._row_index[self.rns[iid]].parent]] |
| 2957 |
else: |
| 2958 |
return "" |
| 2959 |
|
| 2960 |
def rename_iid(self, old: str, new: str, event_data: EventDataDict) -> EventDataDict: |
| 2961 |
event_data.treeview.renamed[old] = new |
| 2962 |
for ciid in self.iid_children(old): |
| 2963 |
self.MT._row_index[self.rns[ciid]].parent = new |
| 2964 |
if self.iid_parent(old): |
| 2965 |
parent_node = self.parent_node(old) |
| 2966 |
item_index = parent_node.children.index(old) |
| 2967 |
parent_node.children[item_index] = new |
| 2968 |
self.MT._row_index[self.rns[old]].iid = new |
| 2969 |
self.rns[new] = self.rns.pop(old) |
| 2970 |
if old in self.tree_open_ids: |
| 2971 |
self.tree_open_ids.discard(old) |
| 2972 |
self.tree_open_ids.add(new) |
| 2973 |
return event_data |
| 2974 |
|
| 2975 |
def gen_top_nodes(self) -> Generator[Node]: |
| 2976 |
yield from (node for node in self.MT._row_index if node.parent == "") |
| 2977 |
|
| 2978 |
def get_iid_indent(self, iid: str) -> int: |
| 2979 |
if isinstance(self.ops.treeview_indent, str): |
| 2980 |
indent = self.MT.index_txt_width * int(self.ops.treeview_indent) |
| 2981 |
else: |
| 2982 |
indent = self.ops.treeview_indent |
| 2983 |
return indent * self.get_node_level(self.iid_node(iid)) |
| 2984 |
|
| 2985 |
def get_iid_level_indent(self, iid: str) -> tuple[int, int]: |
| 2986 |
if isinstance(self.ops.treeview_indent, str): |
| 2987 |
indent = self.MT.index_txt_width * int(self.ops.treeview_indent) |
| 2988 |
else: |
| 2989 |
indent = self.ops.treeview_indent |
| 2990 |
level = self.get_node_level(self.iid_node(iid)) |
| 2991 |
return level, indent * level |
| 2992 |
|
| 2993 |
def remove_iid_from_parents_children(self, iid: str) -> None: |
| 2994 |
if parent_node := self.parent_node(iid): |
| 2995 |
parent_node.children.remove(iid) |
| 2996 |
if not parent_node.children: |
| 2997 |
self.tree_open_ids.discard(parent_node.iid) |
| 2998 |
|
| 2999 |
def move_pid_causes_recursive_loop(self, to_move_iid: str, move_to_parent: str) -> bool: |
| 3000 |
|
| 3001 |
|
| 3002 |
return any(move_to_parent == diid for diid in self.get_iid_descendants(to_move_iid)) |
| 3003 |
|
| 3004 |
def tree_build( |
| 3005 |
self, |
| 3006 |
data: list[list[Any]], |
| 3007 |
iid_column: int, |
| 3008 |
parent_column: int, |
| 3009 |
text_column: None | int | list[str] = None, |
| 3010 |
push_ops: bool = False, |
| 3011 |
row_heights: Sequence[int] | None | False = None, |
| 3012 |
open_ids: Iterator[str] | None = None, |
| 3013 |
ncols: int | None = None, |
| 3014 |
lower: bool = False, |
| 3015 |
include_iid_column: bool = True, |
| 3016 |
include_parent_column: bool = True, |
| 3017 |
include_text_column: bool = True, |
| 3018 |
) -> None: |
| 3019 |
data_rns = {} |
| 3020 |
tree = {} |
| 3021 |
if text_column is None: |
| 3022 |
text_column = iid_column |
| 3023 |
if not isinstance(ncols, int): |
| 3024 |
ncols = max(map(len, data), default=0) |
| 3025 |
for rn, row in enumerate(data): |
| 3026 |
if lower: |
| 3027 |
iid = row[iid_column].lower() |
| 3028 |
pid = row[parent_column].lower() |
| 3029 |
else: |
| 3030 |
iid = row[iid_column] |
| 3031 |
pid = row[parent_column] |
| 3032 |
if iid in tree: |
| 3033 |
tree[iid].text = row[text_column] if isinstance(text_column, int) else text_column[rn] |
| 3034 |
else: |
| 3035 |
tree[iid] = Node( |
| 3036 |
text=row[text_column] if isinstance(text_column, int) else text_column[rn], |
| 3037 |
iid=iid, |
| 3038 |
parent="", |
| 3039 |
) |
| 3040 |
data_rns[iid] = rn |
| 3041 |
if pid: |
| 3042 |
if pid in tree: |
| 3043 |
tree[pid].children.append(iid) |
| 3044 |
else: |
| 3045 |
tree[pid] = Node( |
| 3046 |
text=pid, |
| 3047 |
iid=pid, |
| 3048 |
children=[iid], |
| 3049 |
) |
| 3050 |
tree[iid].parent = pid |
| 3051 |
else: |
| 3052 |
tree[iid].parent = "" |
| 3053 |
exclude = set() |
| 3054 |
if not include_iid_column: |
| 3055 |
exclude.add(iid_column) |
| 3056 |
if not include_parent_column: |
| 3057 |
exclude.add(parent_column) |
| 3058 |
if isinstance(text_column, int) and not include_text_column: |
| 3059 |
exclude.add(text_column) |
| 3060 |
rows = [] |
| 3061 |
ctr = 0 |
| 3062 |
if exclude: |
| 3063 |
for iid, node in tree.items(): |
| 3064 |
if node.parent == "": |
| 3065 |
row = [tree[iid]] |
| 3066 |
row.extend(e for i, e in enumerate(data[data_rns[iid]]) if i not in exclude) |
| 3067 |
rows.append(row) |
| 3068 |
self.rns[iid] = ctr |
| 3069 |
ctr += 1 |
| 3070 |
for diid in self._build_get_descendants(iid, tree): |
| 3071 |
row = [tree[diid]] |
| 3072 |
row.extend(e for i, e in enumerate(data[data_rns[diid]]) if i not in exclude) |
| 3073 |
rows.append(row) |
| 3074 |
self.rns[diid] = ctr |
| 3075 |
ctr += 1 |
| 3076 |
else: |
| 3077 |
for iid, node in tree.items(): |
| 3078 |
if node.parent == "": |
| 3079 |
row = [tree[iid]] |
| 3080 |
row.extend(data[data_rns[iid]]) |
| 3081 |
rows.append(row) |
| 3082 |
self.rns[iid] = ctr |
| 3083 |
ctr += 1 |
| 3084 |
for diid in self._build_get_descendants(iid, tree): |
| 3085 |
row = [tree[diid]] |
| 3086 |
row.extend(data[data_rns[diid]]) |
| 3087 |
rows.append(row) |
| 3088 |
self.rns[diid] = ctr |
| 3089 |
ctr += 1 |
| 3090 |
self.PAR.insert_rows( |
| 3091 |
rows=rows, |
| 3092 |
idx=0, |
| 3093 |
heights=[] if row_heights is False else row_heights, |
| 3094 |
row_index=True, |
| 3095 |
create_selections=False, |
| 3096 |
fill=False, |
| 3097 |
undo=False, |
| 3098 |
push_ops=push_ops, |
| 3099 |
redraw=False, |
| 3100 |
tree=False, |
| 3101 |
) |
| 3102 |
self.MT.all_rows_displayed = False |
| 3103 |
self.MT.displayed_rows = list(range(len(self.MT._row_index))) |
| 3104 |
if open_ids: |
| 3105 |
self.PAR.tree_set_open(open_ids=open_ids) |
| 3106 |
else: |
| 3107 |
index = self.MT._row_index |
| 3108 |
self.PAR.hide_rows( |
| 3109 |
{self.rns[iid] for iid in self.PAR.get_children() if index[self.rns[iid]].parent}, |
| 3110 |
deselect_all=False, |
| 3111 |
data_indexes=True, |
| 3112 |
row_heights=row_heights is not False, |
| 3113 |
) |
| 3114 |
|
| 3115 |
def safe_tree_build( |
| 3116 |
self, |
| 3117 |
data: list[list[Any]], |
| 3118 |
iid_column: int, |
| 3119 |
parent_column: int, |
| 3120 |
text_column: None | int | list[str] = None, |
| 3121 |
push_ops: bool = False, |
| 3122 |
row_heights: Sequence[int] | None | False = None, |
| 3123 |
open_ids: Iterator[str] | None = None, |
| 3124 |
ncols: int | None = None, |
| 3125 |
lower: bool = False, |
| 3126 |
include_iid_column: bool = True, |
| 3127 |
include_parent_column: bool = True, |
| 3128 |
include_text_column: bool = True, |
| 3129 |
) -> None: |
| 3130 |
data_rns = {} |
| 3131 |
tree = {} |
| 3132 |
iids_missing_rows = set() |
| 3133 |
if text_column is None: |
| 3134 |
text_column = iid_column |
| 3135 |
tally_of_ids = defaultdict(lambda: -1) |
| 3136 |
if not isinstance(ncols, int): |
| 3137 |
ncols = max(map(len, data), default=0) |
| 3138 |
for rn, row in enumerate(data): |
| 3139 |
if ncols > (lnr := len(row)): |
| 3140 |
row += self.MT.get_empty_row_seq(rn, end=ncols, start=lnr) |
| 3141 |
if lower: |
| 3142 |
iid = row[iid_column].lower() |
| 3143 |
pid = row[parent_column].lower() |
| 3144 |
else: |
| 3145 |
iid = row[iid_column] |
| 3146 |
pid = row[parent_column] |
| 3147 |
if not iid: |
| 3148 |
continue |
| 3149 |
tally_of_ids[iid] += 1 |
| 3150 |
if tally_of_ids[iid]: |
| 3151 |
x = 1 |
| 3152 |
while iid in tally_of_ids: |
| 3153 |
new = f"{row[iid_column]}_DUPLICATED_{x}" |
| 3154 |
iid = new.lower() if lower else new |
| 3155 |
x += 1 |
| 3156 |
tally_of_ids[iid] += 1 |
| 3157 |
row[iid_column] = new |
| 3158 |
if iid in tree: |
| 3159 |
tree[iid].text = row[text_column] if isinstance(text_column, int) else text_column[rn] |
| 3160 |
else: |
| 3161 |
tree[iid] = Node( |
| 3162 |
text=row[text_column] if isinstance(text_column, int) else text_column[rn], |
| 3163 |
iid=iid, |
| 3164 |
parent="", |
| 3165 |
) |
| 3166 |
if iid in iids_missing_rows: |
| 3167 |
iids_missing_rows.discard(iid) |
| 3168 |
data_rns[iid] = rn |
| 3169 |
if iid == pid or self.build_pid_causes_recursive_loop(iid, pid, tree): |
| 3170 |
row[parent_column] = "" |
| 3171 |
pid = "" |
| 3172 |
if pid: |
| 3173 |
if pid in tree: |
| 3174 |
tree[pid].children.append(iid) |
| 3175 |
else: |
| 3176 |
tree[pid] = Node( |
| 3177 |
text=pid, |
| 3178 |
iid=pid, |
| 3179 |
children=[iid], |
| 3180 |
) |
| 3181 |
iids_missing_rows.add(pid) |
| 3182 |
tree[iid].parent = pid |
| 3183 |
else: |
| 3184 |
tree[iid].parent = "" |
| 3185 |
empty_rows = {} |
| 3186 |
for iid in iids_missing_rows: |
| 3187 |
node = tree[iid] |
| 3188 |
newrow = self.MT.get_empty_row_seq(len(data), ncols) |
| 3189 |
newrow[iid_column] = node.iid |
| 3190 |
empty_rows[node.iid] = newrow |
| 3191 |
exclude = set() |
| 3192 |
if not include_iid_column: |
| 3193 |
exclude.add(iid_column) |
| 3194 |
if not include_parent_column: |
| 3195 |
exclude.add(parent_column) |
| 3196 |
if isinstance(text_column, int) and not include_text_column: |
| 3197 |
exclude.add(text_column) |
| 3198 |
rows = [] |
| 3199 |
ctr = 0 |
| 3200 |
if exclude: |
| 3201 |
for iid, node in tree.items(): |
| 3202 |
if node.parent == "": |
| 3203 |
row = [tree[iid]] |
| 3204 |
if iid in empty_rows: |
| 3205 |
row.extend(e for i, e in enumerate(empty_rows[iid]) if i not in exclude) |
| 3206 |
else: |
| 3207 |
row.extend(e for i, e in enumerate(data[data_rns[iid]]) if i not in exclude) |
| 3208 |
rows.append(row) |
| 3209 |
self.rns[iid] = ctr |
| 3210 |
ctr += 1 |
| 3211 |
for diid in self._build_get_descendants(iid, tree): |
| 3212 |
row = [tree[diid]] |
| 3213 |
row.extend(e for i, e in enumerate(data[data_rns[diid]]) if i not in exclude) |
| 3214 |
rows.append(row) |
| 3215 |
self.rns[diid] = ctr |
| 3216 |
ctr += 1 |
| 3217 |
else: |
| 3218 |
for iid, node in tree.items(): |
| 3219 |
if node.parent == "": |
| 3220 |
row = [tree[iid]] |
| 3221 |
if iid in empty_rows: |
| 3222 |
row.extend(empty_rows[iid]) |
| 3223 |
else: |
| 3224 |
row.extend(data[data_rns[iid]]) |
| 3225 |
rows.append(row) |
| 3226 |
self.rns[iid] = ctr |
| 3227 |
ctr += 1 |
| 3228 |
for diid in self._build_get_descendants(iid, tree): |
| 3229 |
row = [tree[diid]] |
| 3230 |
row.extend(data[data_rns[diid]]) |
| 3231 |
rows.append(row) |
| 3232 |
self.rns[diid] = ctr |
| 3233 |
ctr += 1 |
| 3234 |
self.PAR.insert_rows( |
| 3235 |
rows=rows, |
| 3236 |
idx=0, |
| 3237 |
heights=[] if row_heights is False else row_heights, |
| 3238 |
row_index=True, |
| 3239 |
create_selections=False, |
| 3240 |
fill=False, |
| 3241 |
undo=False, |
| 3242 |
push_ops=push_ops, |
| 3243 |
redraw=False, |
| 3244 |
tree=False, |
| 3245 |
) |
| 3246 |
self.MT.all_rows_displayed = False |
| 3247 |
self.MT.displayed_rows = list(range(len(self.MT._row_index))) |
| 3248 |
if open_ids: |
| 3249 |
self.PAR.tree_set_open(open_ids=open_ids) |
| 3250 |
else: |
| 3251 |
index = self.MT._row_index |
| 3252 |
self.PAR.hide_rows( |
| 3253 |
{self.rns[iid] for iid in self.PAR.get_children() if index[self.rns[iid]].parent}, |
| 3254 |
deselect_all=False, |
| 3255 |
data_indexes=True, |
| 3256 |
row_heights=row_heights is not False, |
| 3257 |
) |
| 3258 |
|
| 3259 |
def _build_get_descendants(self, iid: str, tree: dict[str, Node]) -> Generator[str]: |
| 3260 |
stack = [iter(tree[iid].children)] |
| 3261 |
while stack: |
| 3262 |
top_iterator = stack[-1] |
| 3263 |
try: |
| 3264 |
ciid = next(top_iterator) |
| 3265 |
yield ciid |
| 3266 |
if tree[ciid].children: |
| 3267 |
stack.append(iter(tree[ciid].children)) |
| 3268 |
except StopIteration: |
| 3269 |
stack.pop() |
| 3270 |
|
| 3271 |
def build_pid_causes_recursive_loop(self, iid: str, pid: str, tree: dict[str, Node]) -> bool: |
| 3272 |
|
| 3273 |
for diid in self._build_get_descendants(iid, tree): |
| 3274 |
if diid == pid: |
| 3275 |
return True |
| 3276 |
|
| 3277 |
current_iid = iid |
| 3278 |
while tree[current_iid].parent: |
| 3279 |
parent_iid = tree[current_iid].parent |
| 3280 |
if parent_iid == pid: |
| 3281 |
return True |
| 3282 |
current_iid = parent_iid |
| 3283 |
return False |