| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import tkinter as tk |
| 4 |
from collections import defaultdict |
| 5 |
from collections.abc import Callable, Hashable, Iterator, Sequence |
| 6 |
from functools import partial |
| 7 |
from itertools import cycle, islice, repeat |
| 8 |
from math import ceil |
| 9 |
from operator import itemgetter |
| 10 |
from typing import Any, Literal |
| 11 |
|
| 12 |
from .colors import color_map |
| 13 |
from .constants import ( |
| 14 |
USER_OS, |
| 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 |
consecutive_ranges, |
| 24 |
event_dict, |
| 25 |
event_has_char_key, |
| 26 |
event_opens_dropdown_or_checkbox, |
| 27 |
get_n2a, |
| 28 |
int_x_tuple, |
| 29 |
is_contiguous, |
| 30 |
mod_event_val, |
| 31 |
new_tk_event, |
| 32 |
rounded_box_coords, |
| 33 |
stored_event_dict, |
| 34 |
try_b_index, |
| 35 |
try_binding, |
| 36 |
wrap_text, |
| 37 |
) |
| 38 |
from .other_classes import DotDict, DraggedRowColumn, DropdownStorage, EventDataDict, TextEditorStorage |
| 39 |
from .row_index import RowIndex |
| 40 |
from .sorting import sort_column, sort_rows_by_column, sort_tree_rows_by_column |
| 41 |
from .text_editor import TextEditor |
| 42 |
|
| 43 |
|
| 44 |
class ColumnHeaders(tk.Canvas): |
| 45 |
def __init__(self, parent, **kwargs): |
| 46 |
super().__init__( |
| 47 |
parent, |
| 48 |
background=parent.ops.header_bg, |
| 49 |
highlightthickness=0, |
| 50 |
) |
| 51 |
self.PAR = parent |
| 52 |
self.ops = self.PAR.ops |
| 53 |
self.current_height = None |
| 54 |
self.MT = None |
| 55 |
self.RI: RowIndex | None = None |
| 56 |
self.TL = None |
| 57 |
self.popup_menu_loc = None |
| 58 |
self.extra_begin_edit_cell_func = None |
| 59 |
self.extra_end_edit_cell_func = None |
| 60 |
self.centre_alignment_text_mod_indexes = (slice(1, None), slice(None, -1)) |
| 61 |
self.c_align_cyc = cycle(self.centre_alignment_text_mod_indexes) |
| 62 |
self.b1_pressed_loc = None |
| 63 |
self.closed_dropdown = None |
| 64 |
self.being_drawn_item = None |
| 65 |
self.extra_motion_func = None |
| 66 |
self.extra_b1_press_func = None |
| 67 |
self.extra_b1_motion_func = None |
| 68 |
self.extra_b1_release_func = None |
| 69 |
self.extra_double_b1_func = None |
| 70 |
self.ch_extra_begin_drag_drop_func = None |
| 71 |
self.ch_extra_end_drag_drop_func = None |
| 72 |
self.ch_extra_begin_sort_rows_func = None |
| 73 |
self.ch_extra_end_sort_rows_func = None |
| 74 |
self.extra_rc_func = None |
| 75 |
self.selection_binding_func = None |
| 76 |
self.shift_selection_binding_func = None |
| 77 |
self.ctrl_selection_binding_func = None |
| 78 |
self.drag_selection_binding_func = None |
| 79 |
self.column_width_resize_func = None |
| 80 |
self.width_resizing_enabled = False |
| 81 |
self.height_resizing_enabled = False |
| 82 |
self.double_click_resizing_enabled = False |
| 83 |
self.col_selection_enabled = False |
| 84 |
self.drag_and_drop_enabled = False |
| 85 |
self.rc_delete_col_enabled = False |
| 86 |
self.rc_insert_col_enabled = False |
| 87 |
self.hide_columns_enabled = False |
| 88 |
self.edit_cell_enabled = False |
| 89 |
self.dragged_col = None |
| 90 |
self.visible_col_dividers = {} |
| 91 |
self.col_height_resize_bbox = () |
| 92 |
self.cell_options = {} |
| 93 |
self.rsz_w = None |
| 94 |
self.rsz_h = None |
| 95 |
self.lines_start_at = 0 |
| 96 |
self.currently_resizing_width = False |
| 97 |
self.currently_resizing_height = False |
| 98 |
self.ch_rc_popup_menu = None |
| 99 |
self.dropdown = DropdownStorage() |
| 100 |
self.text_editor = TextEditorStorage() |
| 101 |
|
| 102 |
self.disp_text = {} |
| 103 |
self.disp_high = {} |
| 104 |
self.disp_grid = {} |
| 105 |
self.disp_fill_sels = {} |
| 106 |
self.disp_resize_lines = {} |
| 107 |
self.disp_dropdown = {} |
| 108 |
self.disp_checkbox = {} |
| 109 |
self.disp_boxes = set() |
| 110 |
self.hidd_text = {} |
| 111 |
self.hidd_high = {} |
| 112 |
self.hidd_grid = {} |
| 113 |
self.hidd_fill_sels = {} |
| 114 |
self.hidd_resize_lines = {} |
| 115 |
self.hidd_dropdown = {} |
| 116 |
self.hidd_checkbox = {} |
| 117 |
self.hidd_boxes = set() |
| 118 |
|
| 119 |
self.align = kwargs["header_align"] |
| 120 |
self.basic_bindings() |
| 121 |
|
| 122 |
def event_generate(self, *args, **kwargs) -> None: |
| 123 |
for arg in args: |
| 124 |
if self.MT and arg in self.MT.event_linker: |
| 125 |
self.MT.event_linker[arg]() |
| 126 |
else: |
| 127 |
super().event_generate(*args, **kwargs) |
| 128 |
|
| 129 |
def basic_bindings(self, enable: bool = True) -> None: |
| 130 |
if enable: |
| 131 |
self.bind("<Motion>", self.mouse_motion) |
| 132 |
self.bind("<ButtonPress-1>", self.b1_press) |
| 133 |
self.bind("<B1-Motion>", self.b1_motion) |
| 134 |
self.bind("<ButtonRelease-1>", self.b1_release) |
| 135 |
self.bind("<Double-Button-1>", self.double_b1) |
| 136 |
self.bind(rc_binding, self.rc) |
| 137 |
self.bind("<MouseWheel>", self.mousewheel) |
| 138 |
if USER_OS == "linux": |
| 139 |
self.bind("<Button-4>", self.mousewheel) |
| 140 |
self.bind("<Button-5>", self.mousewheel) |
| 141 |
else: |
| 142 |
self.unbind("<Motion>") |
| 143 |
self.unbind("<ButtonPress-1>") |
| 144 |
self.unbind("<B1-Motion>") |
| 145 |
self.unbind("<ButtonRelease-1>") |
| 146 |
self.unbind("<Double-Button-1>") |
| 147 |
self.unbind(rc_binding) |
| 148 |
self.unbind("<MouseWheel>") |
| 149 |
if USER_OS == "linux": |
| 150 |
self.unbind("<Button-4>") |
| 151 |
self.unbind("<Button-5>") |
| 152 |
|
| 153 |
def mousewheel(self, event: tk.Event) -> None: |
| 154 |
self.MT.mousewheel(event) |
| 155 |
|
| 156 |
def set_height(self, new_height: int, set_TL: bool = False) -> bool: |
| 157 |
try: |
| 158 |
self.config(height=new_height) |
| 159 |
except Exception: |
| 160 |
return False |
| 161 |
if set_TL and self.TL is not None: |
| 162 |
self.TL.set_dimensions(new_h=new_height) |
| 163 |
if expanded := isinstance(self.current_height, int) and new_height > self.current_height: |
| 164 |
self.MT.recreate_all_selection_boxes() |
| 165 |
self.current_height = new_height |
| 166 |
return expanded |
| 167 |
|
| 168 |
def rc(self, event: Any) -> None: |
| 169 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 170 |
self.focus_set() |
| 171 |
popup_menu = None |
| 172 |
if self.MT.identify_col(x=event.x, allow_end=False) is None: |
| 173 |
self.MT.deselect("all") |
| 174 |
c = len(self.MT.col_positions) - 1 |
| 175 |
if self.MT.rc_popup_menus_enabled: |
| 176 |
popup_menu = self.MT.empty_rc_popup_menu |
| 177 |
elif self.col_selection_enabled and not self.currently_resizing_width and not self.currently_resizing_height: |
| 178 |
c = self.MT.identify_col(x=event.x) |
| 179 |
if c < len(self.MT.col_positions) - 1: |
| 180 |
if self.MT.col_selected(c): |
| 181 |
if self.MT.rc_popup_menus_enabled: |
| 182 |
popup_menu = self.ch_rc_popup_menu |
| 183 |
else: |
| 184 |
if self.MT.single_selection_enabled and self.MT.rc_select_enabled: |
| 185 |
self.select_col(c, redraw=True) |
| 186 |
elif self.MT.toggle_selection_enabled and self.MT.rc_select_enabled: |
| 187 |
self.toggle_select_col(c, redraw=True) |
| 188 |
if self.MT.rc_popup_menus_enabled: |
| 189 |
popup_menu = self.ch_rc_popup_menu |
| 190 |
try_binding(self.extra_rc_func, event) |
| 191 |
if popup_menu is not None: |
| 192 |
self.popup_menu_loc = c |
| 193 |
popup_menu.tk_popup(event.x_root, event.y_root) |
| 194 |
|
| 195 |
def ctrl_b1_press(self, event: Any) -> None: |
| 196 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 197 |
if ( |
| 198 |
(self.drag_and_drop_enabled or self.col_selection_enabled) |
| 199 |
and self.MT.ctrl_select_enabled |
| 200 |
and self.rsz_h is None |
| 201 |
and self.rsz_w is None |
| 202 |
): |
| 203 |
c = self.MT.identify_col(x=event.x) |
| 204 |
if c < len(self.MT.col_positions) - 1: |
| 205 |
c_selected = self.MT.col_selected(c) |
| 206 |
if not c_selected and self.col_selection_enabled: |
| 207 |
self.being_drawn_item = True |
| 208 |
self.being_drawn_item = self.add_selection(c, set_as_current=True, run_binding_func=False) |
| 209 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 210 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 211 |
try_binding(self.ctrl_selection_binding_func, sel_event) |
| 212 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 213 |
elif c_selected: |
| 214 |
self.MT.deselect(c=c) |
| 215 |
elif not self.MT.ctrl_select_enabled: |
| 216 |
self.b1_press(event) |
| 217 |
|
| 218 |
def ctrl_shift_b1_press(self, event: Any) -> None: |
| 219 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 220 |
x = event.x |
| 221 |
c = self.MT.identify_col(x=x) |
| 222 |
if ( |
| 223 |
(self.drag_and_drop_enabled or self.col_selection_enabled) |
| 224 |
and self.MT.ctrl_select_enabled |
| 225 |
and self.rsz_h is None |
| 226 |
and self.rsz_w is None |
| 227 |
): |
| 228 |
if c < len(self.MT.col_positions) - 1: |
| 229 |
c_selected = self.MT.col_selected(c) |
| 230 |
if not c_selected and self.col_selection_enabled: |
| 231 |
if self.MT.selected and self.MT.selected.type_ == "columns": |
| 232 |
self.being_drawn_item = self.MT.recreate_selection_box( |
| 233 |
*self.get_shift_select_box(c, self.MT.selected.column), |
| 234 |
fill_iid=self.MT.selected.fill_iid, |
| 235 |
) |
| 236 |
else: |
| 237 |
self.being_drawn_item = self.add_selection( |
| 238 |
c, |
| 239 |
run_binding_func=False, |
| 240 |
set_as_current=True, |
| 241 |
) |
| 242 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 243 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 244 |
try_binding(self.ctrl_selection_binding_func, sel_event) |
| 245 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 246 |
elif c_selected: |
| 247 |
self.dragged_col = DraggedRowColumn( |
| 248 |
dragged=c, |
| 249 |
to_move=sorted(self.MT.get_selected_cols()), |
| 250 |
) |
| 251 |
elif not self.MT.ctrl_select_enabled: |
| 252 |
self.shift_b1_press(event) |
| 253 |
|
| 254 |
def shift_b1_press(self, event: Any) -> None: |
| 255 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 256 |
c = self.MT.identify_col(x=event.x) |
| 257 |
if ( |
| 258 |
(self.drag_and_drop_enabled or self.col_selection_enabled) |
| 259 |
and self.rsz_h is None |
| 260 |
and self.rsz_w is None |
| 261 |
and c < len(self.MT.col_positions) - 1 |
| 262 |
): |
| 263 |
c_selected = self.MT.col_selected(c) |
| 264 |
if not c_selected and self.col_selection_enabled: |
| 265 |
if self.MT.selected and self.MT.selected.type_ == "columns": |
| 266 |
r_to_sel, c_to_sel = self.MT.selected.row, self.MT.selected.column |
| 267 |
self.MT.deselect("all", redraw=False) |
| 268 |
self.being_drawn_item = self.MT.create_selection_box( |
| 269 |
*self.get_shift_select_box(c, c_to_sel), "columns" |
| 270 |
) |
| 271 |
self.MT.set_currently_selected(r_to_sel, c_to_sel, self.being_drawn_item) |
| 272 |
else: |
| 273 |
self.being_drawn_item = self.select_col(c, run_binding_func=False) |
| 274 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 275 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 276 |
try_binding(self.shift_selection_binding_func, sel_event) |
| 277 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 278 |
elif c_selected: |
| 279 |
self.dragged_col = DraggedRowColumn( |
| 280 |
dragged=c, |
| 281 |
to_move=sorted(self.MT.get_selected_cols()), |
| 282 |
) |
| 283 |
|
| 284 |
def get_shift_select_box(self, c: int, min_c: int) -> tuple[int, int, int, int, str]: |
| 285 |
if c >= min_c: |
| 286 |
return 0, min_c, len(self.MT.row_positions) - 1, c + 1 |
| 287 |
elif c < min_c: |
| 288 |
return 0, c, len(self.MT.row_positions) - 1, min_c + 1 |
| 289 |
|
| 290 |
def create_resize_line( |
| 291 |
self, |
| 292 |
x1: int, |
| 293 |
y1: int, |
| 294 |
x2: int, |
| 295 |
y2: int, |
| 296 |
width: int, |
| 297 |
fill: str, |
| 298 |
tag: str | tuple[str], |
| 299 |
) -> None: |
| 300 |
if self.hidd_resize_lines: |
| 301 |
t, sh = self.hidd_resize_lines.popitem() |
| 302 |
self.coords(t, x1, y1, x2, y2) |
| 303 |
if sh: |
| 304 |
self.itemconfig(t, width=width, fill=fill, tag=tag) |
| 305 |
else: |
| 306 |
self.itemconfig(t, width=width, fill=fill, tag=tag, state="normal") |
| 307 |
self.lift(t) |
| 308 |
else: |
| 309 |
t = self.create_line(x1, y1, x2, y2, width=width, fill=fill, tag=tag) |
| 310 |
self.disp_resize_lines[t] = True |
| 311 |
|
| 312 |
def delete_resize_lines(self) -> None: |
| 313 |
self.hidd_resize_lines.update(self.disp_resize_lines) |
| 314 |
self.disp_resize_lines = {} |
| 315 |
for t, sh in self.hidd_resize_lines.items(): |
| 316 |
if sh: |
| 317 |
self.itemconfig(t, tags=("",), state="hidden") |
| 318 |
self.hidd_resize_lines[t] = False |
| 319 |
|
| 320 |
def check_mouse_position_width_resizers(self, x: int, y: int) -> int | None: |
| 321 |
for c, (x1, y1, x2, y2) in self.visible_col_dividers.items(): |
| 322 |
if x >= x1 and y >= y1 and x <= x2 and y <= y2: |
| 323 |
return c |
| 324 |
|
| 325 |
def mouse_motion(self, event: Any) -> None: |
| 326 |
if not self.currently_resizing_height and not self.currently_resizing_width: |
| 327 |
x = self.canvasx(event.x) |
| 328 |
y = self.canvasy(event.y) |
| 329 |
mouse_over_resize = False |
| 330 |
mouse_over_selected = False |
| 331 |
if self.width_resizing_enabled: |
| 332 |
c = self.check_mouse_position_width_resizers(x, y) |
| 333 |
if c is not None: |
| 334 |
self.rsz_w, mouse_over_resize = c, True |
| 335 |
if self.MT.current_cursor != "sb_h_double_arrow": |
| 336 |
self.config(cursor="sb_h_double_arrow") |
| 337 |
self.MT.current_cursor = "sb_h_double_arrow" |
| 338 |
else: |
| 339 |
self.rsz_w = None |
| 340 |
if self.height_resizing_enabled and not mouse_over_resize: |
| 341 |
try: |
| 342 |
x1, y1, x2, y2 = ( |
| 343 |
self.col_height_resize_bbox[0], |
| 344 |
self.col_height_resize_bbox[1], |
| 345 |
self.col_height_resize_bbox[2], |
| 346 |
self.col_height_resize_bbox[3], |
| 347 |
) |
| 348 |
if x >= x1 and y >= y1 and x <= x2 and y <= y2: |
| 349 |
self.rsz_h, mouse_over_resize = True, True |
| 350 |
if self.MT.current_cursor != "sb_v_double_arrow": |
| 351 |
self.config(cursor="sb_v_double_arrow") |
| 352 |
self.MT.current_cursor = "sb_v_double_arrow" |
| 353 |
else: |
| 354 |
self.rsz_h = None |
| 355 |
except Exception: |
| 356 |
self.rsz_h = None |
| 357 |
if not mouse_over_resize and self.MT.col_selected(self.MT.identify_col(event, allow_end=False)): |
| 358 |
mouse_over_selected = True |
| 359 |
if self.MT.current_cursor != "hand2": |
| 360 |
self.config(cursor="hand2") |
| 361 |
self.MT.current_cursor = "hand2" |
| 362 |
if not mouse_over_resize and not mouse_over_selected: |
| 363 |
self.MT.reset_mouse_motion_creations() |
| 364 |
try_binding(self.extra_motion_func, event) |
| 365 |
|
| 366 |
def double_b1(self, event: Any) -> None: |
| 367 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 368 |
self.focus_set() |
| 369 |
if ( |
| 370 |
self.double_click_resizing_enabled |
| 371 |
and self.width_resizing_enabled |
| 372 |
and self.rsz_w is not None |
| 373 |
and not self.currently_resizing_width |
| 374 |
): |
| 375 |
col = self.rsz_w - 1 |
| 376 |
old_width = self.MT.col_positions[self.rsz_w] - self.MT.col_positions[self.rsz_w - 1] |
| 377 |
new_width = self.set_col_width(col) |
| 378 |
self.MT.allow_auto_resize_columns = False |
| 379 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 380 |
if self.column_width_resize_func is not None and old_width != new_width: |
| 381 |
self.column_width_resize_func( |
| 382 |
event_dict( |
| 383 |
name="resize", |
| 384 |
sheet=self.PAR.name, |
| 385 |
resized_columns={col: {"old_size": old_width, "new_size": new_width}}, |
| 386 |
) |
| 387 |
) |
| 388 |
elif self.col_selection_enabled and self.rsz_h is None and self.rsz_w is None: |
| 389 |
c = self.MT.identify_col(x=event.x) |
| 390 |
if c < len(self.MT.col_positions) - 1: |
| 391 |
if self.MT.single_selection_enabled: |
| 392 |
self.select_col(c, redraw=True) |
| 393 |
elif self.MT.toggle_selection_enabled: |
| 394 |
self.toggle_select_col(c, redraw=True) |
| 395 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 396 |
if ( |
| 397 |
self.get_cell_kwargs(datacn, key="dropdown") |
| 398 |
or self.get_cell_kwargs(datacn, key="checkbox") |
| 399 |
or self.edit_cell_enabled |
| 400 |
): |
| 401 |
self.open_cell(event) |
| 402 |
self.rsz_w = None |
| 403 |
self.mouse_motion(event) |
| 404 |
try_binding(self.extra_double_b1_func, event) |
| 405 |
|
| 406 |
def b1_press(self, event: Any) -> None: |
| 407 |
self.MT.unbind("<MouseWheel>") |
| 408 |
self.focus_set() |
| 409 |
self.closed_dropdown = self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 410 |
x = self.canvasx(event.x) |
| 411 |
y = self.canvasy(event.y) |
| 412 |
c = self.MT.identify_col(x=event.x) |
| 413 |
self.b1_pressed_loc = c |
| 414 |
if self.check_mouse_position_width_resizers(x, y) is None: |
| 415 |
self.rsz_w = None |
| 416 |
if self.width_resizing_enabled and self.rsz_w is not None: |
| 417 |
x1, y1, x2, y2 = self.MT.get_canvas_visible_area() |
| 418 |
self.currently_resizing_width = True |
| 419 |
x = self.MT.col_positions[self.rsz_w] |
| 420 |
line2x = self.MT.col_positions[self.rsz_w - 1] |
| 421 |
self.create_resize_line( |
| 422 |
x, |
| 423 |
0, |
| 424 |
x, |
| 425 |
self.current_height, |
| 426 |
width=1, |
| 427 |
fill=self.ops.resizing_line_fg, |
| 428 |
tag=("rw", "rwl"), |
| 429 |
) |
| 430 |
self.MT.create_resize_line(x, y1, x, y2, width=1, fill=self.ops.resizing_line_fg, tag=("rw", "rwl")) |
| 431 |
self.create_resize_line( |
| 432 |
line2x, |
| 433 |
0, |
| 434 |
line2x, |
| 435 |
self.current_height, |
| 436 |
width=1, |
| 437 |
fill=self.ops.resizing_line_fg, |
| 438 |
tag=("rw", "rwl2"), |
| 439 |
) |
| 440 |
self.MT.create_resize_line( |
| 441 |
line2x, y1, line2x, y2, width=1, fill=self.ops.resizing_line_fg, tag=("rw", "rwl2") |
| 442 |
) |
| 443 |
elif self.height_resizing_enabled and self.rsz_w is None and self.rsz_h is not None: |
| 444 |
self.currently_resizing_height = True |
| 445 |
elif self.MT.identify_col(x=event.x, allow_end=False) is None: |
| 446 |
self.MT.deselect("all") |
| 447 |
elif self.col_selection_enabled and self.rsz_w is None and self.rsz_h is None: |
| 448 |
if c < len(self.MT.col_positions) - 1: |
| 449 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 450 |
if ( |
| 451 |
self.MT.col_selected(c) |
| 452 |
and not self.event_over_dropdown(c, datacn, event, x) |
| 453 |
and not self.event_over_checkbox(c, datacn, event, x) |
| 454 |
): |
| 455 |
self.dragged_col = DraggedRowColumn( |
| 456 |
dragged=c, |
| 457 |
to_move=sorted(self.MT.get_selected_cols()), |
| 458 |
) |
| 459 |
else: |
| 460 |
if self.MT.single_selection_enabled: |
| 461 |
self.being_drawn_item = True |
| 462 |
self.being_drawn_item = self.select_col(c, redraw=True) |
| 463 |
elif self.MT.toggle_selection_enabled: |
| 464 |
self.toggle_select_col(c, redraw=True) |
| 465 |
try_binding(self.extra_b1_press_func, event) |
| 466 |
|
| 467 |
def b1_motion(self, event: Any) -> None: |
| 468 |
x1, y1, x2, y2 = self.MT.get_canvas_visible_area() |
| 469 |
if self.width_resizing_enabled and self.rsz_w is not None and self.currently_resizing_width: |
| 470 |
x = self.canvasx(event.x) |
| 471 |
size = x - self.MT.col_positions[self.rsz_w - 1] |
| 472 |
if size >= self.ops.min_column_width and size < self.ops.max_column_width: |
| 473 |
self.hide_resize_and_ctrl_lines(ctrl_lines=False) |
| 474 |
line2x = self.MT.col_positions[self.rsz_w - 1] |
| 475 |
self.create_resize_line( |
| 476 |
x, |
| 477 |
0, |
| 478 |
x, |
| 479 |
self.current_height, |
| 480 |
width=1, |
| 481 |
fill=self.ops.resizing_line_fg, |
| 482 |
tag=("rw", "rwl"), |
| 483 |
) |
| 484 |
self.MT.create_resize_line(x, y1, x, y2, width=1, fill=self.ops.resizing_line_fg, tag=("rw", "rwl")) |
| 485 |
self.create_resize_line( |
| 486 |
line2x, |
| 487 |
0, |
| 488 |
line2x, |
| 489 |
self.current_height, |
| 490 |
width=1, |
| 491 |
fill=self.ops.resizing_line_fg, |
| 492 |
tag=("rw", "rwl2"), |
| 493 |
) |
| 494 |
self.MT.create_resize_line( |
| 495 |
line2x, |
| 496 |
y1, |
| 497 |
line2x, |
| 498 |
y2, |
| 499 |
width=1, |
| 500 |
fill=self.ops.resizing_line_fg, |
| 501 |
tag=("rw", "rwl2"), |
| 502 |
) |
| 503 |
self.drag_width_resize() |
| 504 |
elif self.height_resizing_enabled and self.rsz_h is not None and self.currently_resizing_height: |
| 505 |
evy = event.y |
| 506 |
if evy > self.current_height: |
| 507 |
if evy > self.ops.max_header_height: |
| 508 |
evy = int(self.ops.max_header_height) |
| 509 |
self.drag_height_resize(evy) |
| 510 |
else: |
| 511 |
if evy < self.MT.min_header_height: |
| 512 |
evy = int(self.MT.min_header_height) |
| 513 |
self.drag_height_resize(evy) |
| 514 |
elif ( |
| 515 |
self.drag_and_drop_enabled |
| 516 |
and self.col_selection_enabled |
| 517 |
and self.MT.anything_selected(exclude_cells=True, exclude_rows=True) |
| 518 |
and self.rsz_h is None |
| 519 |
and self.rsz_w is None |
| 520 |
and self.dragged_col is not None |
| 521 |
): |
| 522 |
x = self.canvasx(event.x) |
| 523 |
if x > 0: |
| 524 |
self.show_drag_and_drop_indicators( |
| 525 |
self.drag_and_drop_motion(event), |
| 526 |
y1, |
| 527 |
y2, |
| 528 |
self.dragged_col.to_move, |
| 529 |
) |
| 530 |
elif ( |
| 531 |
self.MT.drag_selection_enabled and self.col_selection_enabled and self.rsz_h is None and self.rsz_w is None |
| 532 |
): |
| 533 |
need_redraw = False |
| 534 |
end_col = self.MT.identify_col(x=event.x) |
| 535 |
if end_col < len(self.MT.col_positions) - 1 and self.MT.selected: |
| 536 |
if self.MT.selected.type_ == "columns": |
| 537 |
box = self.get_b1_motion_box(self.MT.selected.column, end_col) |
| 538 |
if ( |
| 539 |
box is not None |
| 540 |
and self.being_drawn_item is not None |
| 541 |
and self.MT.coords_and_type(self.being_drawn_item) != box |
| 542 |
): |
| 543 |
if box[3] - box[1] != 1: |
| 544 |
self.being_drawn_item = self.MT.recreate_selection_box( |
| 545 |
*box[:-1], |
| 546 |
fill_iid=self.MT.selected.fill_iid, |
| 547 |
) |
| 548 |
else: |
| 549 |
self.being_drawn_item = self.select_col(self.MT.selected.column, run_binding_func=False) |
| 550 |
need_redraw = True |
| 551 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 552 |
try_binding(self.drag_selection_binding_func, sel_event) |
| 553 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 554 |
if self.scroll_if_event_offscreen(event): |
| 555 |
need_redraw = True |
| 556 |
if need_redraw: |
| 557 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=False) |
| 558 |
try_binding(self.extra_b1_motion_func, event) |
| 559 |
|
| 560 |
def drag_height_resize(self, height: int) -> None: |
| 561 |
if self.set_height(height, set_TL=True): |
| 562 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=False, redraw_table=False) |
| 563 |
|
| 564 |
def get_b1_motion_box(self, start_col: int, end_col: int) -> tuple[int, int, int, int, Literal["columns"]]: |
| 565 |
if end_col >= start_col: |
| 566 |
return 0, start_col, len(self.MT.row_positions) - 1, end_col + 1, "columns" |
| 567 |
elif end_col < start_col: |
| 568 |
return 0, end_col, len(self.MT.row_positions) - 1, start_col + 1, "columns" |
| 569 |
|
| 570 |
def ctrl_b1_motion(self, event: Any) -> None: |
| 571 |
x1, y1, x2, y2 = self.MT.get_canvas_visible_area() |
| 572 |
if ( |
| 573 |
self.drag_and_drop_enabled |
| 574 |
and self.col_selection_enabled |
| 575 |
and self.MT.anything_selected(exclude_cells=True, exclude_rows=True) |
| 576 |
and self.rsz_h is None |
| 577 |
and self.rsz_w is None |
| 578 |
and self.dragged_col is not None |
| 579 |
): |
| 580 |
x = self.canvasx(event.x) |
| 581 |
if x > 0: |
| 582 |
self.show_drag_and_drop_indicators( |
| 583 |
self.drag_and_drop_motion(event), |
| 584 |
y1, |
| 585 |
y2, |
| 586 |
self.dragged_col.to_move, |
| 587 |
) |
| 588 |
elif ( |
| 589 |
self.MT.ctrl_select_enabled |
| 590 |
and self.MT.drag_selection_enabled |
| 591 |
and self.col_selection_enabled |
| 592 |
and self.rsz_h is None |
| 593 |
and self.rsz_w is None |
| 594 |
): |
| 595 |
need_redraw = False |
| 596 |
end_col = self.MT.identify_col(x=event.x) |
| 597 |
if end_col < len(self.MT.col_positions) - 1 and self.MT.selected: |
| 598 |
if self.MT.selected.type_ == "columns": |
| 599 |
box = self.get_b1_motion_box(self.MT.selected.column, end_col) |
| 600 |
if ( |
| 601 |
box is not None |
| 602 |
and self.being_drawn_item is not None |
| 603 |
and self.MT.coords_and_type(self.being_drawn_item) != box |
| 604 |
): |
| 605 |
if box[3] - box[1] != 1: |
| 606 |
self.being_drawn_item = self.MT.recreate_selection_box( |
| 607 |
*box[:-1], |
| 608 |
self.MT.selected.fill_iid, |
| 609 |
) |
| 610 |
else: |
| 611 |
self.MT.hide_selection_box(self.MT.selected.fill_iid) |
| 612 |
self.being_drawn_item = self.add_selection(box[1], run_binding_func=False) |
| 613 |
need_redraw = True |
| 614 |
sel_event = self.MT.get_select_event(being_drawn_item=self.being_drawn_item) |
| 615 |
try_binding(self.drag_selection_binding_func, sel_event) |
| 616 |
self.PAR.emit_event("<<SheetSelect>>", data=sel_event) |
| 617 |
if self.scroll_if_event_offscreen(event): |
| 618 |
need_redraw = True |
| 619 |
if need_redraw: |
| 620 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=False) |
| 621 |
elif not self.MT.ctrl_select_enabled: |
| 622 |
self.b1_motion(event) |
| 623 |
|
| 624 |
def drag_and_drop_motion(self, event: Any) -> float: |
| 625 |
x = event.x |
| 626 |
wend = self.winfo_width() |
| 627 |
xcheck = self.xview() |
| 628 |
if x >= wend - 0 and len(xcheck) > 1 and xcheck[1] < 1: |
| 629 |
if x >= wend + 15: |
| 630 |
self.MT.xview_scroll(2, "units") |
| 631 |
self.xview_scroll(2, "units") |
| 632 |
else: |
| 633 |
self.MT.xview_scroll(1, "units") |
| 634 |
self.xview_scroll(1, "units") |
| 635 |
self.fix_xview() |
| 636 |
self.MT.x_move_synced_scrolls("moveto", self.MT.xview()[0]) |
| 637 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True) |
| 638 |
elif x <= 0 and len(xcheck) > 1 and xcheck[0] > 0: |
| 639 |
if x >= -15: |
| 640 |
self.MT.xview_scroll(-1, "units") |
| 641 |
self.xview_scroll(-1, "units") |
| 642 |
else: |
| 643 |
self.MT.xview_scroll(-2, "units") |
| 644 |
self.xview_scroll(-2, "units") |
| 645 |
self.fix_xview() |
| 646 |
self.MT.x_move_synced_scrolls("moveto", self.MT.xview()[0]) |
| 647 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True) |
| 648 |
col = self.MT.identify_col(x=x) |
| 649 |
if col == len(self.MT.col_positions) - 1: |
| 650 |
col -= 1 |
| 651 |
if col >= self.dragged_col.to_move[0] and col <= self.dragged_col.to_move[-1]: |
| 652 |
if is_contiguous(self.dragged_col.to_move): |
| 653 |
return self.MT.col_positions[self.dragged_col.to_move[0]] |
| 654 |
return self.MT.col_positions[col] |
| 655 |
elif col > self.dragged_col.to_move[-1]: |
| 656 |
return self.MT.col_positions[col + 1] |
| 657 |
return self.MT.col_positions[col] |
| 658 |
|
| 659 |
def show_drag_and_drop_indicators( |
| 660 |
self, |
| 661 |
xpos: float, |
| 662 |
y1: float, |
| 663 |
y2: float, |
| 664 |
cols: Sequence[int], |
| 665 |
) -> None: |
| 666 |
self.hide_resize_and_ctrl_lines() |
| 667 |
self.create_resize_line( |
| 668 |
xpos, |
| 669 |
0, |
| 670 |
xpos, |
| 671 |
self.current_height, |
| 672 |
width=3, |
| 673 |
fill=self.ops.drag_and_drop_bg, |
| 674 |
tag="move_columns", |
| 675 |
) |
| 676 |
self.MT.create_resize_line(xpos, y1, xpos, y2, width=3, fill=self.ops.drag_and_drop_bg, tag="move_columns") |
| 677 |
for boxst, boxend in consecutive_ranges(cols): |
| 678 |
self.MT.show_ctrl_outline( |
| 679 |
start_cell=(boxst, 0), |
| 680 |
end_cell=(boxend, len(self.MT.row_positions) - 1), |
| 681 |
dash=(), |
| 682 |
outline=self.ops.drag_and_drop_bg, |
| 683 |
delete_on_timer=False, |
| 684 |
) |
| 685 |
|
| 686 |
def hide_resize_and_ctrl_lines(self, ctrl_lines: bool = True) -> None: |
| 687 |
self.delete_resize_lines() |
| 688 |
self.MT.delete_resize_lines() |
| 689 |
if ctrl_lines: |
| 690 |
self.MT.delete_ctrl_outlines() |
| 691 |
|
| 692 |
def scroll_if_event_offscreen(self, event: Any) -> bool: |
| 693 |
xcheck = self.xview() |
| 694 |
need_redraw = False |
| 695 |
if event.x > self.winfo_width() and len(xcheck) > 1 and xcheck[1] < 1: |
| 696 |
try: |
| 697 |
self.MT.xview_scroll(1, "units") |
| 698 |
self.xview_scroll(1, "units") |
| 699 |
except Exception: |
| 700 |
pass |
| 701 |
self.fix_xview() |
| 702 |
self.MT.x_move_synced_scrolls("moveto", self.MT.xview()[0]) |
| 703 |
need_redraw = True |
| 704 |
elif event.x < 0 and self.canvasx(self.winfo_width()) > 0 and xcheck and xcheck[0] > 0: |
| 705 |
try: |
| 706 |
self.xview_scroll(-1, "units") |
| 707 |
self.MT.xview_scroll(-1, "units") |
| 708 |
except Exception: |
| 709 |
pass |
| 710 |
self.fix_xview() |
| 711 |
self.MT.x_move_synced_scrolls("moveto", self.MT.xview()[0]) |
| 712 |
need_redraw = True |
| 713 |
return need_redraw |
| 714 |
|
| 715 |
def fix_xview(self) -> None: |
| 716 |
xcheck = self.xview() |
| 717 |
if xcheck and xcheck[0] < 0: |
| 718 |
self.MT.set_xviews("moveto", 0) |
| 719 |
elif len(xcheck) > 1 and xcheck[1] > 1: |
| 720 |
self.MT.set_xviews("moveto", 1) |
| 721 |
|
| 722 |
def event_over_dropdown(self, c: int, datacn: int, event: Any, canvasx: float) -> bool: |
| 723 |
return ( |
| 724 |
event.y < self.MT.header_txt_height + 5 |
| 725 |
and self.get_cell_kwargs(datacn, key="dropdown") |
| 726 |
and canvasx < self.MT.col_positions[c + 1] |
| 727 |
and canvasx > self.MT.col_positions[c + 1] - self.MT.header_txt_height - 4 |
| 728 |
) |
| 729 |
|
| 730 |
def event_over_checkbox(self, c: int, datacn: int, event: Any, canvasx: float) -> bool: |
| 731 |
return ( |
| 732 |
event.y < self.MT.header_txt_height + 5 |
| 733 |
and self.get_cell_kwargs(datacn, key="checkbox") |
| 734 |
and canvasx < self.MT.col_positions[c] + self.MT.header_txt_height + 4 |
| 735 |
) |
| 736 |
|
| 737 |
def drag_width_resize(self) -> None: |
| 738 |
new_col_pos = int(self.coords("rwl")[0]) |
| 739 |
old_width = self.MT.col_positions[self.rsz_w] - self.MT.col_positions[self.rsz_w - 1] |
| 740 |
size = new_col_pos - self.MT.col_positions[self.rsz_w - 1] |
| 741 |
if size < self.ops.min_column_width: |
| 742 |
new_col_pos = ceil(self.MT.col_positions[self.rsz_w - 1] + self.ops.min_column_width) |
| 743 |
elif size > self.ops.max_column_width: |
| 744 |
new_col_pos = int(self.MT.col_positions[self.rsz_w - 1] + self.ops.max_column_width) |
| 745 |
increment = new_col_pos - self.MT.col_positions[self.rsz_w] |
| 746 |
self.MT.col_positions[self.rsz_w + 1 :] = [ |
| 747 |
e + increment for e in islice(self.MT.col_positions, self.rsz_w + 1, None) |
| 748 |
] |
| 749 |
self.MT.col_positions[self.rsz_w] = new_col_pos |
| 750 |
new_width = self.MT.col_positions[self.rsz_w] - self.MT.col_positions[self.rsz_w - 1] |
| 751 |
self.MT.allow_auto_resize_columns = False |
| 752 |
self.MT.recreate_all_selection_boxes() |
| 753 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True, set_scrollregion=False) |
| 754 |
if self.column_width_resize_func is not None and old_width != new_width: |
| 755 |
self.column_width_resize_func( |
| 756 |
event_dict( |
| 757 |
name="resize", |
| 758 |
sheet=self.PAR.name, |
| 759 |
resized_columns={self.rsz_w - 1: {"old_size": old_width, "new_size": new_width}}, |
| 760 |
) |
| 761 |
) |
| 762 |
|
| 763 |
def b1_release(self, event: Any) -> None: |
| 764 |
to_hide = self.being_drawn_item |
| 765 |
if self.being_drawn_item is not None and (to_sel := self.MT.coords_and_type(self.being_drawn_item)): |
| 766 |
r_to_sel, c_to_sel = self.MT.selected.row, self.MT.selected.column |
| 767 |
self.being_drawn_item = None |
| 768 |
self.MT.set_currently_selected( |
| 769 |
r_to_sel, |
| 770 |
c_to_sel, |
| 771 |
item=self.MT.create_selection_box(*to_sel, set_current=False), |
| 772 |
run_binding=False, |
| 773 |
) |
| 774 |
self.MT.hide_selection_box(to_hide) |
| 775 |
self.MT.bind("<MouseWheel>", self.MT.mousewheel) |
| 776 |
if self.width_resizing_enabled and self.rsz_w is not None and self.currently_resizing_width: |
| 777 |
self.drag_width_resize() |
| 778 |
self.hide_resize_and_ctrl_lines(ctrl_lines=False) |
| 779 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 780 |
elif ( |
| 781 |
self.drag_and_drop_enabled |
| 782 |
and self.col_selection_enabled |
| 783 |
and self.MT.anything_selected(exclude_cells=True, exclude_rows=True) |
| 784 |
and self.rsz_h is None |
| 785 |
and self.rsz_w is None |
| 786 |
and self.dragged_col is not None |
| 787 |
and self.find_withtag("move_columns") |
| 788 |
): |
| 789 |
self.hide_resize_and_ctrl_lines() |
| 790 |
c = self.MT.identify_col(x=event.x) |
| 791 |
totalcols = len(self.dragged_col.to_move) |
| 792 |
if ( |
| 793 |
c is not None |
| 794 |
and totalcols != len(self.MT.col_positions) - 1 |
| 795 |
and not ( |
| 796 |
c >= self.dragged_col.to_move[0] |
| 797 |
and c <= self.dragged_col.to_move[-1] |
| 798 |
and is_contiguous(self.dragged_col.to_move) |
| 799 |
) |
| 800 |
): |
| 801 |
if c > self.dragged_col.to_move[-1]: |
| 802 |
c += 1 |
| 803 |
if c > len(self.MT.col_positions) - 1: |
| 804 |
c = len(self.MT.col_positions) - 1 |
| 805 |
event_data = self.MT.new_event_dict("move_columns", state=True) |
| 806 |
event_data["value"] = c |
| 807 |
if try_binding(self.ch_extra_begin_drag_drop_func, event_data, "begin_move_columns"): |
| 808 |
data_new_idxs, disp_new_idxs, event_data = self.MT.move_columns_adjust_options_dict( |
| 809 |
*self.MT.get_args_for_move_columns( |
| 810 |
move_to=c, |
| 811 |
to_move=self.dragged_col.to_move, |
| 812 |
), |
| 813 |
move_data=self.ops.column_drag_and_drop_perform, |
| 814 |
move_widths=self.ops.column_drag_and_drop_perform, |
| 815 |
event_data=event_data, |
| 816 |
) |
| 817 |
event_data["moved"]["columns"] = { |
| 818 |
"data": data_new_idxs, |
| 819 |
"displayed": disp_new_idxs, |
| 820 |
} |
| 821 |
if self.MT.undo_enabled: |
| 822 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 823 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 824 |
try_binding(self.ch_extra_end_drag_drop_func, event_data, "end_move_columns") |
| 825 |
self.MT.sheet_modified(event_data) |
| 826 |
elif self.b1_pressed_loc is not None and self.rsz_w is None and self.rsz_h is None: |
| 827 |
c = self.MT.identify_col(x=event.x) |
| 828 |
if ( |
| 829 |
c is not None |
| 830 |
and c < len(self.MT.col_positions) - 1 |
| 831 |
and c == self.b1_pressed_loc |
| 832 |
and self.b1_pressed_loc != self.closed_dropdown |
| 833 |
): |
| 834 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 835 |
canvasx = self.canvasx(event.x) |
| 836 |
if self.event_over_dropdown( |
| 837 |
c, |
| 838 |
datacn, |
| 839 |
event, |
| 840 |
canvasx, |
| 841 |
) or self.event_over_checkbox( |
| 842 |
c, |
| 843 |
datacn, |
| 844 |
event, |
| 845 |
canvasx, |
| 846 |
): |
| 847 |
self.open_cell(event) |
| 848 |
else: |
| 849 |
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True) |
| 850 |
self.b1_pressed_loc = None |
| 851 |
self.closed_dropdown = None |
| 852 |
self.dragged_col = None |
| 853 |
self.currently_resizing_width = False |
| 854 |
self.currently_resizing_height = False |
| 855 |
self.rsz_w = None |
| 856 |
self.rsz_h = None |
| 857 |
self.mouse_motion(event) |
| 858 |
try_binding(self.extra_b1_release_func, event) |
| 859 |
|
| 860 |
def _sort_columns( |
| 861 |
self, |
| 862 |
event: tk.Event | None = None, |
| 863 |
columns: Iterator[int] | None = None, |
| 864 |
reverse: bool = False, |
| 865 |
validation: bool = True, |
| 866 |
key: Callable | None = None, |
| 867 |
undo: bool = True, |
| 868 |
) -> EventDataDict: |
| 869 |
if columns is None: |
| 870 |
columns = self.MT.get_selected_cols() |
| 871 |
if not columns: |
| 872 |
columns = list(range(0, len(self.MT.col_positions) - 1)) |
| 873 |
event_data = self.MT.new_event_dict("edit_table") |
| 874 |
try_binding(self.MT.extra_begin_sort_cells_func, event_data) |
| 875 |
if key is None: |
| 876 |
key = self.PAR.ops.sort_key |
| 877 |
for c in columns: |
| 878 |
datacn = self.MT.datacn(c) |
| 879 |
for r, val in enumerate( |
| 880 |
sort_column( |
| 881 |
(self.MT.get_cell_data(row, datacn) for row in range(len(self.MT.data))), |
| 882 |
reverse=reverse, |
| 883 |
key=key, |
| 884 |
) |
| 885 |
): |
| 886 |
if ( |
| 887 |
not self.MT.edit_validation_func |
| 888 |
or not validation |
| 889 |
or ( |
| 890 |
self.MT.edit_validation_func |
| 891 |
and (val := self.MT.edit_validation_func(mod_event_val(event_data, val, (r, datacn)))) |
| 892 |
is not None |
| 893 |
) |
| 894 |
): |
| 895 |
event_data = self.MT.event_data_set_cell( |
| 896 |
datarn=r, |
| 897 |
datacn=datacn, |
| 898 |
value=val, |
| 899 |
event_data=event_data, |
| 900 |
) |
| 901 |
event_data = self.MT.bulk_edit_validation(event_data) |
| 902 |
if event_data["cells"]["table"]: |
| 903 |
if undo and self.MT.undo_enabled: |
| 904 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 905 |
try_binding(self.MT.extra_end_sort_cells_func, event_data, "end_edit_table") |
| 906 |
self.MT.sheet_modified(event_data) |
| 907 |
self.PAR.emit_event("<<SheetModified>>", event_data) |
| 908 |
self.MT.refresh() |
| 909 |
return event_data |
| 910 |
|
| 911 |
def _sort_rows_by_column( |
| 912 |
self, |
| 913 |
event: tk.Event | None = None, |
| 914 |
column: int | None = None, |
| 915 |
reverse: bool = False, |
| 916 |
key: Callable | None = None, |
| 917 |
undo: bool = True, |
| 918 |
) -> EventDataDict: |
| 919 |
event_data = self.MT.new_event_dict("move_rows", state=True) |
| 920 |
if not self.MT.data: |
| 921 |
return event_data |
| 922 |
if column is None: |
| 923 |
if not self.MT.selected: |
| 924 |
return event_data |
| 925 |
column = self.MT.datacn(self.MT.selected.column) |
| 926 |
if try_binding(self.ch_extra_begin_sort_rows_func, event_data, "begin_move_rows"): |
| 927 |
if key is None: |
| 928 |
key = self.PAR.ops.sort_key |
| 929 |
disp_new_idxs, disp_row_ctr = {}, 0 |
| 930 |
if self.ops.treeview: |
| 931 |
new_nodes_order, data_new_idxs = sort_tree_rows_by_column( |
| 932 |
data=self.MT.data, |
| 933 |
column=column, |
| 934 |
index=self.MT._row_index, |
| 935 |
rns=self.RI.rns, |
| 936 |
reverse=reverse, |
| 937 |
key=key, |
| 938 |
) |
| 939 |
for node in new_nodes_order: |
| 940 |
if (idx := try_b_index(self.MT.displayed_rows, self.RI.rns[node.iid])) is not None: |
| 941 |
disp_new_idxs[idx] = disp_row_ctr |
| 942 |
disp_row_ctr += 1 |
| 943 |
else: |
| 944 |
new_rows_order, data_new_idxs = sort_rows_by_column( |
| 945 |
self.MT.data, |
| 946 |
column=column, |
| 947 |
reverse=reverse, |
| 948 |
key=key, |
| 949 |
) |
| 950 |
if self.MT.all_rows_displayed: |
| 951 |
disp_new_idxs = data_new_idxs |
| 952 |
else: |
| 953 |
for old_idx, _ in new_rows_order: |
| 954 |
if (idx := try_b_index(self.MT.displayed_rows, old_idx)) is not None: |
| 955 |
disp_new_idxs[idx] = disp_row_ctr |
| 956 |
disp_row_ctr += 1 |
| 957 |
if self.PAR.ops.treeview: |
| 958 |
data_new_idxs, disp_new_idxs, event_data = self.MT.move_rows_adjust_options_dict( |
| 959 |
data_new_idxs=data_new_idxs, |
| 960 |
data_old_idxs=dict(zip(data_new_idxs.values(), data_new_idxs)), |
| 961 |
totalrows=None, |
| 962 |
disp_new_idxs=disp_new_idxs, |
| 963 |
move_data=True, |
| 964 |
create_selections=False, |
| 965 |
event_data=event_data, |
| 966 |
manage_tree=False, |
| 967 |
) |
| 968 |
else: |
| 969 |
data_new_idxs, disp_new_idxs, _ = self.PAR.mapping_move_rows( |
| 970 |
data_new_idxs=data_new_idxs, |
| 971 |
disp_new_idxs=disp_new_idxs, |
| 972 |
move_data=True, |
| 973 |
create_selections=False, |
| 974 |
undo=False, |
| 975 |
emit_event=False, |
| 976 |
redraw=True, |
| 977 |
) |
| 978 |
event_data["moved"]["rows"] = { |
| 979 |
"data": data_new_idxs, |
| 980 |
"displayed": disp_new_idxs, |
| 981 |
} |
| 982 |
if undo and self.MT.undo_enabled: |
| 983 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 984 |
try_binding(self.ch_extra_end_sort_rows_func, event_data, "end_move_rows") |
| 985 |
self.MT.sheet_modified(event_data) |
| 986 |
self.PAR.emit_event("<<SheetModified>>", event_data) |
| 987 |
self.MT.refresh() |
| 988 |
return event_data |
| 989 |
|
| 990 |
def toggle_select_col( |
| 991 |
self, |
| 992 |
column: int, |
| 993 |
add_selection: bool = True, |
| 994 |
redraw: bool = True, |
| 995 |
run_binding_func: bool = True, |
| 996 |
set_as_current: bool = True, |
| 997 |
ext: bool = False, |
| 998 |
) -> int: |
| 999 |
if add_selection: |
| 1000 |
if self.MT.col_selected(column): |
| 1001 |
fill_iid = self.MT.deselect(c=column, redraw=redraw) |
| 1002 |
else: |
| 1003 |
fill_iid = self.add_selection( |
| 1004 |
c=column, |
| 1005 |
redraw=redraw, |
| 1006 |
run_binding_func=run_binding_func, |
| 1007 |
set_as_current=set_as_current, |
| 1008 |
ext=ext, |
| 1009 |
) |
| 1010 |
else: |
| 1011 |
if self.MT.col_selected(column): |
| 1012 |
fill_iid = self.MT.deselect(c=column, redraw=redraw) |
| 1013 |
else: |
| 1014 |
fill_iid = self.select_col(column, redraw=redraw, ext=ext) |
| 1015 |
return fill_iid |
| 1016 |
|
| 1017 |
def select_col( |
| 1018 |
self, |
| 1019 |
c: int | Iterator[int], |
| 1020 |
redraw: bool = False, |
| 1021 |
run_binding_func: bool = True, |
| 1022 |
ext: bool = False, |
| 1023 |
) -> int | list[int]: |
| 1024 |
boxes_to_hide = tuple(self.MT.selection_boxes) |
| 1025 |
fill_iids = [ |
| 1026 |
self.MT.create_selection_box( |
| 1027 |
0, |
| 1028 |
start, |
| 1029 |
len(self.MT.row_positions) - 1, |
| 1030 |
end, |
| 1031 |
"columns", |
| 1032 |
set_current=True, |
| 1033 |
ext=ext, |
| 1034 |
) |
| 1035 |
for start, end in consecutive_ranges(int_x_tuple(c)) |
| 1036 |
] |
| 1037 |
for iid in boxes_to_hide: |
| 1038 |
self.MT.hide_selection_box(iid) |
| 1039 |
if redraw: |
| 1040 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 1041 |
if run_binding_func: |
| 1042 |
self.MT.run_selection_binding("columns") |
| 1043 |
return fill_iids[0] if len(fill_iids) == 1 else fill_iids |
| 1044 |
|
| 1045 |
def add_selection( |
| 1046 |
self, |
| 1047 |
c: int, |
| 1048 |
redraw: bool = False, |
| 1049 |
run_binding_func: bool = True, |
| 1050 |
set_as_current: bool = True, |
| 1051 |
ext: bool = False, |
| 1052 |
) -> int: |
| 1053 |
box = (0, c, len(self.MT.row_positions) - 1, c + 1, "columns") |
| 1054 |
fill_iid = self.MT.create_selection_box(*box, set_current=set_as_current, ext=ext) |
| 1055 |
if redraw: |
| 1056 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 1057 |
if run_binding_func: |
| 1058 |
self.MT.run_selection_binding("columns") |
| 1059 |
return fill_iid |
| 1060 |
|
| 1061 |
def get_cell_dimensions(self, datacn: int) -> tuple[int, int]: |
| 1062 |
txt = self.cell_str(datacn, fix=False) |
| 1063 |
if txt: |
| 1064 |
self.MT.txt_measure_canvas.itemconfig( |
| 1065 |
self.MT.txt_measure_canvas_text, |
| 1066 |
text=txt, |
| 1067 |
font=self.ops.header_font, |
| 1068 |
) |
| 1069 |
b = self.MT.txt_measure_canvas.bbox(self.MT.txt_measure_canvas_text) |
| 1070 |
w = b[2] - b[0] + 7 |
| 1071 |
h = b[3] - b[1] + 5 |
| 1072 |
else: |
| 1073 |
w = self.ops.min_column_width |
| 1074 |
h = self.MT.min_header_height |
| 1075 |
if datacn in self.cell_options and ( |
| 1076 |
self.get_cell_kwargs(datacn, key="dropdown") or self.get_cell_kwargs(datacn, key="checkbox") |
| 1077 |
): |
| 1078 |
return w + self.MT.header_txt_height, h |
| 1079 |
return w, h |
| 1080 |
|
| 1081 |
def set_height_of_header_to_text( |
| 1082 |
self, |
| 1083 |
text: None | str = None, |
| 1084 |
only_if_too_small: bool = False, |
| 1085 |
) -> int: |
| 1086 |
h = self.MT.min_header_height |
| 1087 |
if (text is None and not self.MT._headers and isinstance(self.MT._headers, list)) or ( |
| 1088 |
isinstance(self.MT._headers, int) and self.MT._headers >= len(self.MT.data) |
| 1089 |
): |
| 1090 |
return h |
| 1091 |
self.fix_header() |
| 1092 |
qconf = self.MT.txt_measure_canvas.itemconfig |
| 1093 |
qbbox = self.MT.txt_measure_canvas.bbox |
| 1094 |
qtxtm = self.MT.txt_measure_canvas_text |
| 1095 |
qfont = self.ops.header_font |
| 1096 |
default_header_height = self.MT.get_default_header_height() |
| 1097 |
if text is not None and text: |
| 1098 |
qconf(qtxtm, text=text, font=qfont) |
| 1099 |
b = qbbox(qtxtm) |
| 1100 |
if (th := b[3] - b[1] + 5) > h: |
| 1101 |
h = th |
| 1102 |
elif text is None: |
| 1103 |
if self.MT.all_columns_displayed: |
| 1104 |
if isinstance(self.MT._headers, list): |
| 1105 |
iterable = range(len(self.MT._headers)) |
| 1106 |
else: |
| 1107 |
iterable = range(len(self.MT.data[self.MT._headers])) |
| 1108 |
else: |
| 1109 |
iterable = self.MT.displayed_columns |
| 1110 |
if ( |
| 1111 |
isinstance(self.MT._headers, list) |
| 1112 |
and (th := max(map(itemgetter(0), map(self.get_cell_dimensions, iterable)), default=h)) > h |
| 1113 |
): |
| 1114 |
h = th |
| 1115 |
elif isinstance(self.MT._headers, int): |
| 1116 |
datarn = self.MT._headers |
| 1117 |
for datacn in iterable: |
| 1118 |
if txt := self.MT.cell_str(datarn, datacn, get_displayed=True): |
| 1119 |
qconf(qtxtm, text=txt, font=qfont) |
| 1120 |
b = qbbox(qtxtm) |
| 1121 |
th = b[3] - b[1] + 5 |
| 1122 |
else: |
| 1123 |
th = default_header_height |
| 1124 |
if th > h: |
| 1125 |
h = th |
| 1126 |
space_bot = self.MT.get_space_bot(0) |
| 1127 |
if h > space_bot and space_bot > self.MT.min_header_height: |
| 1128 |
h = space_bot |
| 1129 |
if h < self.MT.min_header_height: |
| 1130 |
h = int(self.MT.min_header_height) |
| 1131 |
elif h > self.ops.max_header_height: |
| 1132 |
h = int(self.ops.max_header_height) |
| 1133 |
if not only_if_too_small or (only_if_too_small and h > self.current_height): |
| 1134 |
self.set_height(h, set_TL=True) |
| 1135 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True) |
| 1136 |
return h |
| 1137 |
|
| 1138 |
def get_col_text_width( |
| 1139 |
self, |
| 1140 |
col: int, |
| 1141 |
visible_only: bool = False, |
| 1142 |
only_if_too_small: bool = False, |
| 1143 |
) -> int: |
| 1144 |
self.fix_header() |
| 1145 |
w = self.ops.min_column_width |
| 1146 |
datacn = col if self.MT.all_columns_displayed else self.MT.displayed_columns[col] |
| 1147 |
|
| 1148 |
hw, hh_ = self.get_cell_dimensions(datacn) |
| 1149 |
|
| 1150 |
if self.MT.data: |
| 1151 |
if self.MT.all_rows_displayed: |
| 1152 |
iterable = range(*self.MT.visible_text_rows) if visible_only else range(0, len(self.MT.data)) |
| 1153 |
else: |
| 1154 |
if visible_only: |
| 1155 |
start_row, end_row = self.MT.visible_text_rows |
| 1156 |
else: |
| 1157 |
start_row, end_row = 0, len(self.MT.displayed_rows) |
| 1158 |
iterable = self.MT.displayed_rows[start_row:end_row] |
| 1159 |
qconf = self.MT.txt_measure_canvas.itemconfig |
| 1160 |
qbbox = self.MT.txt_measure_canvas.bbox |
| 1161 |
qtxtm = self.MT.txt_measure_canvas_text |
| 1162 |
qtxth = self.MT.table_txt_height |
| 1163 |
qfont = self.ops.table_font |
| 1164 |
for datarn in iterable: |
| 1165 |
if txt := self.MT.cell_str(datarn, datacn, get_displayed=True): |
| 1166 |
qconf(qtxtm, text=txt, font=qfont) |
| 1167 |
b = qbbox(qtxtm) |
| 1168 |
if ( |
| 1169 |
( |
| 1170 |
self.MT.get_cell_kwargs(datarn, datacn, key="dropdown") |
| 1171 |
or self.MT.get_cell_kwargs(datarn, datacn, key="checkbox") |
| 1172 |
) |
| 1173 |
and (tw := b[2] - b[0] + qtxth + 7) > w |
| 1174 |
or (tw := b[2] - b[0] + 7) > w |
| 1175 |
): |
| 1176 |
w = tw |
| 1177 |
if hw > w: |
| 1178 |
w = hw |
| 1179 |
if only_if_too_small and w < self.MT.col_positions[col + 1] - self.MT.col_positions[col]: |
| 1180 |
w = self.MT.col_positions[col + 1] - self.MT.col_positions[col] |
| 1181 |
if w <= self.ops.min_column_width: |
| 1182 |
w = self.ops.min_column_width |
| 1183 |
elif w > self.ops.max_column_width: |
| 1184 |
w = int(self.ops.max_column_width) |
| 1185 |
return w |
| 1186 |
|
| 1187 |
def set_col_width( |
| 1188 |
self, |
| 1189 |
col: int, |
| 1190 |
width: None | int = None, |
| 1191 |
only_if_too_small: bool = False, |
| 1192 |
visible_only: bool = False, |
| 1193 |
recreate: bool = True, |
| 1194 |
) -> int: |
| 1195 |
if width is None: |
| 1196 |
width = self.get_col_text_width(col=col, visible_only=visible_only) |
| 1197 |
if width <= self.ops.min_column_width: |
| 1198 |
width = self.ops.min_column_width |
| 1199 |
elif width > self.ops.max_column_width: |
| 1200 |
width = int(self.ops.max_column_width) |
| 1201 |
if only_if_too_small and width <= self.MT.col_positions[col + 1] - self.MT.col_positions[col]: |
| 1202 |
return self.MT.col_positions[col + 1] - self.MT.col_positions[col] |
| 1203 |
new_col_pos = self.MT.col_positions[col] + width |
| 1204 |
increment = new_col_pos - self.MT.col_positions[col + 1] |
| 1205 |
self.MT.col_positions[col + 2 :] = [ |
| 1206 |
e + increment for e in islice(self.MT.col_positions, col + 2, len(self.MT.col_positions)) |
| 1207 |
] |
| 1208 |
self.MT.col_positions[col + 1] = new_col_pos |
| 1209 |
if recreate: |
| 1210 |
self.MT.recreate_all_selection_boxes() |
| 1211 |
return width |
| 1212 |
|
| 1213 |
def set_width_of_all_cols( |
| 1214 |
self, |
| 1215 |
width: None | int = None, |
| 1216 |
only_if_too_small: bool = False, |
| 1217 |
recreate: bool = True, |
| 1218 |
) -> None: |
| 1219 |
if width is None: |
| 1220 |
if self.MT.all_columns_displayed: |
| 1221 |
iterable = range(self.MT.total_data_cols()) |
| 1222 |
else: |
| 1223 |
iterable = range(len(self.MT.displayed_columns)) |
| 1224 |
self.MT.set_col_positions( |
| 1225 |
itr=(self.get_col_text_width(cn, only_if_too_small=only_if_too_small) for cn in iterable) |
| 1226 |
) |
| 1227 |
elif width is not None: |
| 1228 |
if self.MT.all_columns_displayed: |
| 1229 |
self.MT.set_col_positions(itr=repeat(width, self.MT.total_data_cols())) |
| 1230 |
else: |
| 1231 |
self.MT.set_col_positions(itr=repeat(width, len(self.MT.displayed_columns))) |
| 1232 |
if recreate: |
| 1233 |
self.MT.recreate_all_selection_boxes() |
| 1234 |
|
| 1235 |
def redraw_highlight_get_text_fg( |
| 1236 |
self, |
| 1237 |
fc: float, |
| 1238 |
sc: float, |
| 1239 |
c: int, |
| 1240 |
sel_cells_bg: str, |
| 1241 |
sel_cols_bg: str, |
| 1242 |
selections: dict, |
| 1243 |
datacn: int, |
| 1244 |
has_dd: bool, |
| 1245 |
) -> tuple[str, bool]: |
| 1246 |
redrawn = False |
| 1247 |
kwargs = self.get_cell_kwargs(datacn, key="highlight") |
| 1248 |
if kwargs: |
| 1249 |
fill = kwargs[0] |
| 1250 |
if fill and not fill.startswith("#"): |
| 1251 |
fill = color_map[fill] |
| 1252 |
if "columns" in selections and c in selections["columns"]: |
| 1253 |
txtfg = ( |
| 1254 |
self.ops.header_selected_columns_fg |
| 1255 |
if kwargs[1] is None or self.ops.display_selected_fg_over_highlights |
| 1256 |
else kwargs[1] |
| 1257 |
) |
| 1258 |
if fill: |
| 1259 |
fill = ( |
| 1260 |
f"#{int((int(fill[1:3], 16) + int(sel_cols_bg[1:3], 16)) / 2):02X}" |
| 1261 |
+ f"{int((int(fill[3:5], 16) + int(sel_cols_bg[3:5], 16)) / 2):02X}" |
| 1262 |
+ f"{int((int(fill[5:], 16) + int(sel_cols_bg[5:], 16)) / 2):02X}" |
| 1263 |
) |
| 1264 |
elif "cells" in selections and c in selections["cells"]: |
| 1265 |
txtfg = ( |
| 1266 |
self.ops.header_selected_cells_fg |
| 1267 |
if kwargs[1] is None or self.ops.display_selected_fg_over_highlights |
| 1268 |
else kwargs[1] |
| 1269 |
) |
| 1270 |
if fill: |
| 1271 |
fill = ( |
| 1272 |
f"#{int((int(fill[1:3], 16) + int(sel_cells_bg[1:3], 16)) / 2):02X}" |
| 1273 |
+ f"{int((int(fill[3:5], 16) + int(sel_cells_bg[3:5], 16)) / 2):02X}" |
| 1274 |
+ f"{int((int(fill[5:], 16) + int(sel_cells_bg[5:], 16)) / 2):02X}" |
| 1275 |
) |
| 1276 |
else: |
| 1277 |
txtfg = self.ops.header_fg if kwargs[1] is None else kwargs[1] |
| 1278 |
if fill: |
| 1279 |
redrawn = self.redraw_highlight( |
| 1280 |
fc + 1, |
| 1281 |
0, |
| 1282 |
sc, |
| 1283 |
self.current_height - 1, |
| 1284 |
fill=fill, |
| 1285 |
outline=self.ops.header_fg if has_dd and self.ops.show_dropdown_borders else "", |
| 1286 |
) |
| 1287 |
elif not kwargs: |
| 1288 |
if "columns" in selections and c in selections["columns"]: |
| 1289 |
txtfg = self.ops.header_selected_columns_fg |
| 1290 |
redrawn = self.redraw_highlight( |
| 1291 |
fc + 1, |
| 1292 |
0, |
| 1293 |
sc, |
| 1294 |
self.current_height - 1, |
| 1295 |
fill=self.ops.header_selected_columns_bg, |
| 1296 |
outline=self.ops.header_fg if has_dd and self.ops.show_dropdown_borders else "", |
| 1297 |
) |
| 1298 |
elif "cells" in selections and c in selections["cells"]: |
| 1299 |
txtfg = self.ops.header_selected_cells_fg |
| 1300 |
redrawn = self.redraw_highlight( |
| 1301 |
fc + 1, |
| 1302 |
0, |
| 1303 |
sc, |
| 1304 |
self.current_height - 1, |
| 1305 |
fill=self.ops.header_selected_cells_bg, |
| 1306 |
outline=self.ops.header_fg if has_dd and self.ops.show_dropdown_borders else "", |
| 1307 |
) |
| 1308 |
else: |
| 1309 |
txtfg = self.ops.header_fg |
| 1310 |
return txtfg, redrawn |
| 1311 |
|
| 1312 |
def redraw_highlight( |
| 1313 |
self, |
| 1314 |
x1: float, |
| 1315 |
y1: float, |
| 1316 |
x2: float, |
| 1317 |
y2: float, |
| 1318 |
fill: str, |
| 1319 |
outline: str, |
| 1320 |
) -> bool: |
| 1321 |
coords = (x1, y1, x2, y2) |
| 1322 |
if self.hidd_high: |
| 1323 |
iid, showing = self.hidd_high.popitem() |
| 1324 |
self.coords(iid, coords) |
| 1325 |
if showing: |
| 1326 |
self.itemconfig(iid, fill=fill, outline=outline) |
| 1327 |
else: |
| 1328 |
self.itemconfig(iid, fill=fill, outline=outline, state="normal") |
| 1329 |
else: |
| 1330 |
iid = self.create_rectangle(coords, fill=fill, outline=outline) |
| 1331 |
self.disp_high[iid] = True |
| 1332 |
return True |
| 1333 |
|
| 1334 |
def redraw_gridline( |
| 1335 |
self, |
| 1336 |
points: Sequence[float], |
| 1337 |
fill: str, |
| 1338 |
width: int, |
| 1339 |
) -> None: |
| 1340 |
if self.hidd_grid: |
| 1341 |
t, sh = self.hidd_grid.popitem() |
| 1342 |
self.coords(t, points) |
| 1343 |
if sh: |
| 1344 |
self.itemconfig(t, fill=fill, width=width) |
| 1345 |
else: |
| 1346 |
self.itemconfig(t, fill=fill, width=width, state="normal") |
| 1347 |
self.disp_grid[t] = True |
| 1348 |
else: |
| 1349 |
self.disp_grid[self.create_line(points, fill=fill, width=width)] = True |
| 1350 |
|
| 1351 |
def redraw_dropdown( |
| 1352 |
self, |
| 1353 |
x1: float, |
| 1354 |
y1: float, |
| 1355 |
x2: float, |
| 1356 |
y2: float, |
| 1357 |
fill: str, |
| 1358 |
outline: str, |
| 1359 |
draw_outline: bool = True, |
| 1360 |
draw_arrow: bool = True, |
| 1361 |
open_: bool = False, |
| 1362 |
) -> None: |
| 1363 |
if draw_outline and self.ops.show_dropdown_borders: |
| 1364 |
self.redraw_highlight(x1 + 1, y1 + 1, x2, y2, fill="", outline=self.ops.header_fg) |
| 1365 |
if draw_arrow: |
| 1366 |
mod = (self.MT.header_txt_height - 1) if self.MT.header_txt_height % 2 else self.MT.header_txt_height |
| 1367 |
small_mod = int(mod / 5) |
| 1368 |
mid_y = int(self.MT.min_header_height / 2) |
| 1369 |
if open_: |
| 1370 |
|
| 1371 |
points = ( |
| 1372 |
x2 - 4 - small_mod - small_mod - small_mod - small_mod, |
| 1373 |
y1 + mid_y + small_mod, |
| 1374 |
x2 - 4 - small_mod - small_mod, |
| 1375 |
y1 + mid_y - small_mod, |
| 1376 |
x2 - 4, |
| 1377 |
y1 + mid_y + small_mod, |
| 1378 |
) |
| 1379 |
else: |
| 1380 |
|
| 1381 |
points = ( |
| 1382 |
x2 - 4 - small_mod - small_mod - small_mod - small_mod, |
| 1383 |
y1 + mid_y - small_mod, |
| 1384 |
x2 - 4 - small_mod - small_mod, |
| 1385 |
y1 + mid_y + small_mod, |
| 1386 |
x2 - 4, |
| 1387 |
y1 + mid_y - small_mod, |
| 1388 |
) |
| 1389 |
if self.hidd_dropdown: |
| 1390 |
t, sh = self.hidd_dropdown.popitem() |
| 1391 |
self.coords(t, points) |
| 1392 |
if sh: |
| 1393 |
self.itemconfig(t, fill=fill) |
| 1394 |
else: |
| 1395 |
self.itemconfig(t, fill=fill, state="normal") |
| 1396 |
else: |
| 1397 |
t = self.create_line(points, fill=fill, width=2, capstyle=tk.ROUND, joinstyle=tk.BEVEL, tag="lift") |
| 1398 |
self.disp_dropdown[t] = True |
| 1399 |
|
| 1400 |
def redraw_checkbox( |
| 1401 |
self, |
| 1402 |
x1: float, |
| 1403 |
y1: float, |
| 1404 |
x2: float, |
| 1405 |
y2: float, |
| 1406 |
fill: str, |
| 1407 |
outline: str, |
| 1408 |
draw_check: bool = False, |
| 1409 |
) -> None: |
| 1410 |
points = rounded_box_coords(x1, y1, x2, y2) |
| 1411 |
if self.hidd_checkbox: |
| 1412 |
t, sh = self.hidd_checkbox.popitem() |
| 1413 |
self.coords(t, points) |
| 1414 |
if sh: |
| 1415 |
self.itemconfig(t, fill=outline, outline=fill) |
| 1416 |
else: |
| 1417 |
self.itemconfig(t, fill=outline, outline=fill, state="normal") |
| 1418 |
else: |
| 1419 |
t = self.create_polygon(points, fill=outline, outline=fill, smooth=True, tag="lift") |
| 1420 |
self.disp_checkbox[t] = True |
| 1421 |
if draw_check: |
| 1422 |
|
| 1423 |
x1 = x1 + 4 |
| 1424 |
y1 = y1 + 4 |
| 1425 |
x2 = x2 - 3 |
| 1426 |
y2 = y2 - 3 |
| 1427 |
points = rounded_box_coords(x1, y1, x2, y2, radius=4) |
| 1428 |
if self.hidd_checkbox: |
| 1429 |
t, sh = self.hidd_checkbox.popitem() |
| 1430 |
self.coords(t, points) |
| 1431 |
if sh: |
| 1432 |
self.itemconfig(t, fill=fill, outline=outline) |
| 1433 |
else: |
| 1434 |
self.itemconfig(t, fill=fill, outline=outline, state="normal") |
| 1435 |
else: |
| 1436 |
t = self.create_polygon(points, fill=fill, outline=outline, smooth=True, tag="lift") |
| 1437 |
self.disp_checkbox[t] = True |
| 1438 |
|
| 1439 |
def configure_scrollregion(self, last_col_line_pos: float) -> bool: |
| 1440 |
try: |
| 1441 |
self.configure( |
| 1442 |
scrollregion=( |
| 1443 |
0, |
| 1444 |
0, |
| 1445 |
last_col_line_pos + self.ops.empty_horizontal + 2, |
| 1446 |
self.current_height, |
| 1447 |
) |
| 1448 |
) |
| 1449 |
return True |
| 1450 |
except Exception: |
| 1451 |
return False |
| 1452 |
|
| 1453 |
def wrap_get_char_w(self, c: str) -> int: |
| 1454 |
if c in self.MT.char_widths[self.header_font]: |
| 1455 |
return self.MT.char_widths[self.header_font][c] |
| 1456 |
else: |
| 1457 |
self.MT.txt_measure_canvas.itemconfig( |
| 1458 |
self.MT.txt_measure_canvas_text, |
| 1459 |
text=_test_str + c, |
| 1460 |
font=self.header_font, |
| 1461 |
) |
| 1462 |
b = self.MT.txt_measure_canvas.bbox(self.MT.txt_measure_canvas_text) |
| 1463 |
wd = b[2] - b[0] - self.header_test_str_w |
| 1464 |
self.MT.char_widths[self.header_font][c] = wd |
| 1465 |
return wd |
| 1466 |
|
| 1467 |
def redraw_grid_and_text( |
| 1468 |
self, |
| 1469 |
last_col_line_pos: float, |
| 1470 |
scrollpos_left: float, |
| 1471 |
x_stop: float, |
| 1472 |
grid_start_col: int, |
| 1473 |
grid_end_col: int, |
| 1474 |
text_start_col: int, |
| 1475 |
text_end_col: int, |
| 1476 |
scrollpos_right: float, |
| 1477 |
col_pos_exists: bool, |
| 1478 |
set_scrollregion: bool, |
| 1479 |
) -> bool: |
| 1480 |
if set_scrollregion and not self.configure_scrollregion(last_col_line_pos=last_col_line_pos): |
| 1481 |
return False |
| 1482 |
self.hidd_text.update(self.disp_text) |
| 1483 |
self.disp_text = {} |
| 1484 |
self.hidd_high.update(self.disp_high) |
| 1485 |
self.disp_high = {} |
| 1486 |
self.hidd_grid.update(self.disp_grid) |
| 1487 |
self.disp_grid = {} |
| 1488 |
self.hidd_dropdown.update(self.disp_dropdown) |
| 1489 |
self.disp_dropdown = {} |
| 1490 |
self.hidd_checkbox.update(self.disp_checkbox) |
| 1491 |
self.disp_checkbox = {} |
| 1492 |
self.visible_col_dividers = {} |
| 1493 |
self.col_height_resize_bbox = ( |
| 1494 |
scrollpos_left, |
| 1495 |
self.current_height - 2, |
| 1496 |
x_stop, |
| 1497 |
self.current_height, |
| 1498 |
) |
| 1499 |
top = self.canvasy(0) |
| 1500 |
|
| 1501 |
if (self.ops.show_vertical_grid or self.width_resizing_enabled) and col_pos_exists: |
| 1502 |
yend = self.current_height - 5 |
| 1503 |
points = [ |
| 1504 |
x_stop - 1, |
| 1505 |
self.current_height - 1, |
| 1506 |
scrollpos_left - 1, |
| 1507 |
self.current_height - 1, |
| 1508 |
scrollpos_left - 1, |
| 1509 |
-1, |
| 1510 |
] |
| 1511 |
for c in range(grid_start_col, grid_end_col): |
| 1512 |
draw_x = self.MT.col_positions[c] |
| 1513 |
if c and self.width_resizing_enabled: |
| 1514 |
self.visible_col_dividers[c] = (draw_x - 2, 1, draw_x + 2, yend) |
| 1515 |
points.extend( |
| 1516 |
( |
| 1517 |
draw_x, |
| 1518 |
-1, |
| 1519 |
draw_x, |
| 1520 |
self.current_height, |
| 1521 |
draw_x, |
| 1522 |
-1, |
| 1523 |
self.MT.col_positions[c + 1] if len(self.MT.col_positions) - 1 > c else draw_x, |
| 1524 |
-1, |
| 1525 |
) |
| 1526 |
) |
| 1527 |
self.redraw_gridline(points=points, fill=self.ops.header_grid_fg, width=1) |
| 1528 |
|
| 1529 |
sel_cols_bg = ( |
| 1530 |
self.ops.header_selected_cells_bg |
| 1531 |
if self.ops.header_selected_cells_bg.startswith("#") |
| 1532 |
else color_map[self.ops.header_selected_cells_bg] |
| 1533 |
) |
| 1534 |
sel_cells_bg = ( |
| 1535 |
self.ops.header_selected_columns_bg |
| 1536 |
if self.ops.header_selected_columns_bg.startswith("#") |
| 1537 |
else color_map[self.ops.header_selected_columns_bg] |
| 1538 |
) |
| 1539 |
font = self.ops.header_font |
| 1540 |
selections = self.get_redraw_selections(text_start_col, grid_end_col) |
| 1541 |
dd_coords = self.dropdown.get_coords() |
| 1542 |
wrap = self.ops.header_wrap |
| 1543 |
txt_h = self.MT.header_txt_height |
| 1544 |
for c in range(text_start_col, text_end_col): |
| 1545 |
draw_y = 3 |
| 1546 |
cleftgridln = self.MT.col_positions[c] |
| 1547 |
crightgridln = self.MT.col_positions[c + 1] |
| 1548 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 1549 |
kwargs = self.get_cell_kwargs(datacn, key="dropdown") |
| 1550 |
fill, dd_drawn = self.redraw_highlight_get_text_fg( |
| 1551 |
fc=cleftgridln, |
| 1552 |
sc=crightgridln, |
| 1553 |
c=c, |
| 1554 |
sel_cells_bg=sel_cells_bg, |
| 1555 |
sel_cols_bg=sel_cols_bg, |
| 1556 |
selections=selections, |
| 1557 |
datacn=datacn, |
| 1558 |
has_dd=bool(kwargs), |
| 1559 |
) |
| 1560 |
if datacn in self.cell_options and "align" in self.cell_options[datacn]: |
| 1561 |
align = self.cell_options[datacn]["align"] |
| 1562 |
else: |
| 1563 |
align = self.align |
| 1564 |
if kwargs: |
| 1565 |
max_width = crightgridln - cleftgridln - txt_h - 5 |
| 1566 |
if align[-1] == "w": |
| 1567 |
draw_x = cleftgridln + 2 |
| 1568 |
elif align[-1] == "e": |
| 1569 |
draw_x = crightgridln - 5 - txt_h |
| 1570 |
elif align[-1] == "n": |
| 1571 |
draw_x = cleftgridln + (crightgridln - cleftgridln - txt_h) / 2 |
| 1572 |
self.redraw_dropdown( |
| 1573 |
cleftgridln, |
| 1574 |
0, |
| 1575 |
crightgridln, |
| 1576 |
self.current_height - 1, |
| 1577 |
fill=fill if kwargs["state"] != "disabled" else self.ops.header_grid_fg, |
| 1578 |
outline=fill, |
| 1579 |
draw_outline=not dd_drawn, |
| 1580 |
draw_arrow=max_width >= 5, |
| 1581 |
open_=dd_coords == c, |
| 1582 |
) |
| 1583 |
else: |
| 1584 |
max_width = crightgridln - cleftgridln - 2 |
| 1585 |
if align[-1] == "w": |
| 1586 |
draw_x = cleftgridln + 2 |
| 1587 |
elif align[-1] == "e": |
| 1588 |
draw_x = crightgridln - 2 |
| 1589 |
elif align[-1] == "n": |
| 1590 |
draw_x = cleftgridln + (crightgridln - cleftgridln) / 2 |
| 1591 |
if (kwargs := self.get_cell_kwargs(datacn, key="checkbox")) and max_width > txt_h + 1: |
| 1592 |
box_w = txt_h + 1 |
| 1593 |
if align[-1] == "w": |
| 1594 |
draw_x += box_w + 3 |
| 1595 |
elif align[-1] == "n": |
| 1596 |
draw_x += box_w / 2 + 1 |
| 1597 |
max_width -= box_w + 4 |
| 1598 |
try: |
| 1599 |
draw_check = ( |
| 1600 |
self.MT._headers[datacn] |
| 1601 |
if isinstance(self.MT._headers, (list, tuple)) |
| 1602 |
else self.MT.data[self.MT._headers][datacn] |
| 1603 |
) |
| 1604 |
except Exception: |
| 1605 |
draw_check = False |
| 1606 |
self.redraw_checkbox( |
| 1607 |
cleftgridln + 2, |
| 1608 |
2, |
| 1609 |
cleftgridln + txt_h + 3, |
| 1610 |
txt_h + 3, |
| 1611 |
fill=fill if kwargs["state"] == "normal" else self.ops.header_grid_fg, |
| 1612 |
outline="", |
| 1613 |
draw_check=draw_check, |
| 1614 |
) |
| 1615 |
if ( |
| 1616 |
max_width < self.MT.header_txt_width |
| 1617 |
or (align[-1] == "w" and draw_x > scrollpos_right) |
| 1618 |
or (align[-1] == "e" and cleftgridln + 5 > scrollpos_right) |
| 1619 |
or (align[-1] == "n" and cleftgridln + 5 > scrollpos_right) |
| 1620 |
): |
| 1621 |
continue |
| 1622 |
text = self.cell_str(datacn, fix=False) |
| 1623 |
if not text: |
| 1624 |
continue |
| 1625 |
gen_lines = wrap_text( |
| 1626 |
text=text, |
| 1627 |
max_width=max_width, |
| 1628 |
max_lines=int((self.current_height - top - 2) / txt_h), |
| 1629 |
char_width_fn=self.wrap_get_char_w, |
| 1630 |
widths=self.MT.char_widths[font], |
| 1631 |
wrap=wrap, |
| 1632 |
) |
| 1633 |
if align[-1] == "w" or align[-1] == "e": |
| 1634 |
if self.hidd_text: |
| 1635 |
iid, showing = self.hidd_text.popitem() |
| 1636 |
self.coords(iid, draw_x, draw_y) |
| 1637 |
if showing: |
| 1638 |
self.itemconfig( |
| 1639 |
iid, |
| 1640 |
text="\n".join(gen_lines), |
| 1641 |
fill=fill, |
| 1642 |
font=font, |
| 1643 |
anchor=align, |
| 1644 |
) |
| 1645 |
else: |
| 1646 |
self.itemconfig( |
| 1647 |
iid, |
| 1648 |
text="\n".join(gen_lines), |
| 1649 |
fill=fill, |
| 1650 |
font=font, |
| 1651 |
anchor=align, |
| 1652 |
state="normal", |
| 1653 |
) |
| 1654 |
else: |
| 1655 |
iid = self.create_text( |
| 1656 |
draw_x, |
| 1657 |
draw_y, |
| 1658 |
text="\n".join(gen_lines), |
| 1659 |
fill=fill, |
| 1660 |
font=font, |
| 1661 |
anchor=align, |
| 1662 |
tag="lift", |
| 1663 |
) |
| 1664 |
self.disp_text[iid] = True |
| 1665 |
else: |
| 1666 |
for line in gen_lines: |
| 1667 |
if self.hidd_text: |
| 1668 |
iid, showing = self.hidd_text.popitem() |
| 1669 |
self.coords(iid, draw_x, draw_y) |
| 1670 |
if showing: |
| 1671 |
self.itemconfig( |
| 1672 |
iid, |
| 1673 |
text=line, |
| 1674 |
fill=fill, |
| 1675 |
font=font, |
| 1676 |
anchor=align, |
| 1677 |
) |
| 1678 |
else: |
| 1679 |
self.itemconfig( |
| 1680 |
iid, |
| 1681 |
text=line, |
| 1682 |
fill=fill, |
| 1683 |
font=font, |
| 1684 |
anchor=align, |
| 1685 |
state="normal", |
| 1686 |
) |
| 1687 |
else: |
| 1688 |
iid = self.create_text( |
| 1689 |
draw_x, |
| 1690 |
draw_y, |
| 1691 |
text=line, |
| 1692 |
fill=fill, |
| 1693 |
font=font, |
| 1694 |
anchor=align, |
| 1695 |
tag="lift", |
| 1696 |
) |
| 1697 |
self.disp_text[iid] = True |
| 1698 |
draw_y += self.MT.header_txt_height |
| 1699 |
|
| 1700 |
for dct in (self.hidd_text, self.hidd_high, self.hidd_grid, self.hidd_dropdown, self.hidd_checkbox): |
| 1701 |
for iid, showing in dct.items(): |
| 1702 |
if showing: |
| 1703 |
self.itemconfig(iid, state="hidden") |
| 1704 |
dct[iid] = False |
| 1705 |
self.tag_raise("lift") |
| 1706 |
if self.disp_resize_lines: |
| 1707 |
self.tag_raise("rw") |
| 1708 |
return True |
| 1709 |
|
| 1710 |
def get_redraw_selections(self, startc: int, endc: int) -> dict[str, set[int]]: |
| 1711 |
d = defaultdict(set) |
| 1712 |
for _, box in self.MT.get_selection_items(): |
| 1713 |
_, c1, _, c2 = box.coords |
| 1714 |
for c in range(startc, endc): |
| 1715 |
if c1 <= c and c2 > c: |
| 1716 |
d[box.type_ if box.type_ != "rows" else "cells"].add(c) |
| 1717 |
return d |
| 1718 |
|
| 1719 |
def open_cell(self, event: Any = None, ignore_existing_editor: bool = False) -> None: |
| 1720 |
if not self.MT.anything_selected() or (not ignore_existing_editor and self.text_editor.open): |
| 1721 |
return |
| 1722 |
if not self.MT.selected: |
| 1723 |
return |
| 1724 |
c = self.MT.selected.column |
| 1725 |
datacn = self.MT.datacn(c) |
| 1726 |
if self.get_cell_kwargs(datacn, key="readonly"): |
| 1727 |
return |
| 1728 |
elif self.get_cell_kwargs(datacn, key="dropdown") or self.get_cell_kwargs(datacn, key="checkbox"): |
| 1729 |
if event_opens_dropdown_or_checkbox(event): |
| 1730 |
if self.get_cell_kwargs(datacn, key="dropdown"): |
| 1731 |
self.open_dropdown_window(c, event=event) |
| 1732 |
elif self.get_cell_kwargs(datacn, key="checkbox"): |
| 1733 |
self.click_checkbox(c, datacn) |
| 1734 |
elif self.edit_cell_enabled: |
| 1735 |
self.open_text_editor(event=event, c=c, dropdown=False) |
| 1736 |
|
| 1737 |
|
| 1738 |
def get_cell_align(self, c: int) -> str: |
| 1739 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 1740 |
if datacn in self.cell_options and "align" in self.cell_options[datacn]: |
| 1741 |
align = self.cell_options[datacn]["align"] |
| 1742 |
else: |
| 1743 |
align = self.align |
| 1744 |
return align |
| 1745 |
|
| 1746 |
|
| 1747 |
def open_text_editor( |
| 1748 |
self, |
| 1749 |
event: Any = None, |
| 1750 |
c: int = 0, |
| 1751 |
text: Any = None, |
| 1752 |
state: str = "normal", |
| 1753 |
dropdown: bool = False, |
| 1754 |
) -> bool: |
| 1755 |
text = f"{self.get_cell_data(self.MT.datacn(c), none_to_empty_str=True, redirect_int=True)}" |
| 1756 |
extra_func_key = "??" |
| 1757 |
if event_opens_dropdown_or_checkbox(event): |
| 1758 |
if hasattr(event, "keysym") and event.keysym in ("Return", "F2", "BackSpace"): |
| 1759 |
extra_func_key = event.keysym |
| 1760 |
if event.keysym == "BackSpace": |
| 1761 |
text = "" |
| 1762 |
elif event_has_char_key(event): |
| 1763 |
extra_func_key = text = event.char |
| 1764 |
elif event is not None: |
| 1765 |
return False |
| 1766 |
if self.extra_begin_edit_cell_func: |
| 1767 |
try: |
| 1768 |
text = self.extra_begin_edit_cell_func( |
| 1769 |
event_dict( |
| 1770 |
name="begin_edit_header", |
| 1771 |
sheet=self.PAR.name, |
| 1772 |
key=extra_func_key, |
| 1773 |
value=text, |
| 1774 |
loc=c, |
| 1775 |
column=c, |
| 1776 |
boxes=self.MT.get_boxes(), |
| 1777 |
selected=self.MT.selected, |
| 1778 |
) |
| 1779 |
) |
| 1780 |
except Exception: |
| 1781 |
return False |
| 1782 |
if text is None: |
| 1783 |
return False |
| 1784 |
else: |
| 1785 |
text = text if isinstance(text, str) else f"{text}" |
| 1786 |
if self.ops.cell_auto_resize_enabled: |
| 1787 |
if self.height_resizing_enabled: |
| 1788 |
self.set_height_of_header_to_text(text) |
| 1789 |
self.set_col_width_run_binding(c) |
| 1790 |
if self.text_editor.open and c == self.text_editor.column: |
| 1791 |
self.text_editor.set_text(self.text_editor.get() + "" if not isinstance(text, str) else text) |
| 1792 |
return False |
| 1793 |
self.hide_text_editor() |
| 1794 |
if not self.MT.see(0, c, keep_yscroll=True): |
| 1795 |
self.MT.main_table_redraw_grid_and_text(True, True) |
| 1796 |
x = self.MT.col_positions[c] + 1 |
| 1797 |
y = 0 |
| 1798 |
w = self.MT.col_positions[c + 1] - x |
| 1799 |
h = self.current_height + 1 |
| 1800 |
kwargs = { |
| 1801 |
"menu_kwargs": DotDict( |
| 1802 |
{ |
| 1803 |
"font": self.ops.table_font, |
| 1804 |
"foreground": self.ops.popup_menu_fg, |
| 1805 |
"background": self.ops.popup_menu_bg, |
| 1806 |
"activebackground": self.ops.popup_menu_highlight_bg, |
| 1807 |
"activeforeground": self.ops.popup_menu_highlight_fg, |
| 1808 |
} |
| 1809 |
), |
| 1810 |
"sheet_ops": self.ops, |
| 1811 |
"border_color": self.ops.header_selected_columns_bg, |
| 1812 |
"text": text, |
| 1813 |
"state": state, |
| 1814 |
"width": w, |
| 1815 |
"height": h, |
| 1816 |
"show_border": True, |
| 1817 |
"bg": self.ops.header_editor_bg, |
| 1818 |
"fg": self.ops.header_editor_fg, |
| 1819 |
"select_bg": self.ops.header_editor_select_bg, |
| 1820 |
"select_fg": self.ops.header_editor_select_fg, |
| 1821 |
"align": self.get_cell_align(c), |
| 1822 |
"c": c, |
| 1823 |
} |
| 1824 |
if not self.text_editor.window: |
| 1825 |
self.text_editor.window = TextEditor(self, newline_binding=self.text_editor_newline_binding) |
| 1826 |
self.text_editor.canvas_id = self.create_window((x, y), window=self.text_editor.window, anchor="nw") |
| 1827 |
self.text_editor.window.reset(**kwargs) |
| 1828 |
if not self.text_editor.open: |
| 1829 |
self.itemconfig(self.text_editor.canvas_id, state="normal") |
| 1830 |
self.text_editor.open = True |
| 1831 |
self.coords(self.text_editor.canvas_id, x, y) |
| 1832 |
for b in text_editor_newline_bindings: |
| 1833 |
self.text_editor.tktext.bind(b, self.text_editor_newline_binding) |
| 1834 |
for b in text_editor_close_bindings: |
| 1835 |
self.text_editor.tktext.bind(b, self.close_text_editor) |
| 1836 |
if not dropdown: |
| 1837 |
self.text_editor.tktext.focus_set() |
| 1838 |
self.text_editor.window.scroll_to_bottom() |
| 1839 |
self.text_editor.tktext.bind("<FocusOut>", self.close_text_editor) |
| 1840 |
for key, func in self.MT.text_editor_user_bound_keys.items(): |
| 1841 |
self.text_editor.tktext.bind(key, func) |
| 1842 |
return True |
| 1843 |
|
| 1844 |
|
| 1845 |
def text_editor_has_wrapped( |
| 1846 |
self, |
| 1847 |
r: int = 0, |
| 1848 |
c: int = 0, |
| 1849 |
check_lines: None = None, |
| 1850 |
) -> None: |
| 1851 |
if self.width_resizing_enabled: |
| 1852 |
curr_width = self.text_editor.window.winfo_width() |
| 1853 |
new_width = curr_width + (self.MT.header_txt_height * 2) |
| 1854 |
if new_width != curr_width: |
| 1855 |
self.text_editor.window.config(width=new_width) |
| 1856 |
self.set_col_width_run_binding(c, width=new_width, only_if_too_small=False) |
| 1857 |
if self.dropdown.open and self.dropdown.get_coords() == c: |
| 1858 |
self.itemconfig(self.dropdown.canvas_id, width=new_width) |
| 1859 |
self.dropdown.window.update_idletasks() |
| 1860 |
self.dropdown.window._reselect() |
| 1861 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=False, redraw_table=True) |
| 1862 |
self.coords(self.text_editor.canvas_id, self.MT.col_positions[c] + 1, 0) |
| 1863 |
|
| 1864 |
|
| 1865 |
def text_editor_newline_binding(self, event: Any = None, check_lines: bool = True) -> None: |
| 1866 |
if not self.height_resizing_enabled: |
| 1867 |
return |
| 1868 |
curr_height = self.text_editor.window.winfo_height() |
| 1869 |
if curr_height < self.MT.min_header_height: |
| 1870 |
return |
| 1871 |
if ( |
| 1872 |
not check_lines |
| 1873 |
or self.MT.get_lines_cell_height( |
| 1874 |
self.text_editor.window.get_num_lines() + 1, |
| 1875 |
font=self.text_editor.tktext.cget("font"), |
| 1876 |
) |
| 1877 |
> curr_height |
| 1878 |
): |
| 1879 |
c = self.text_editor.column |
| 1880 |
new_height = curr_height + self.MT.header_txt_height |
| 1881 |
space_bot = self.MT.get_space_bot(0) |
| 1882 |
if new_height > space_bot: |
| 1883 |
new_height = space_bot |
| 1884 |
if new_height != curr_height: |
| 1885 |
self.text_editor.window.config(height=new_height) |
| 1886 |
self.set_height(new_height, set_TL=True) |
| 1887 |
if self.dropdown.open and self.dropdown.get_coords() == c: |
| 1888 |
win_h, anchor = self.get_dropdown_height_anchor(c, new_height) |
| 1889 |
self.coords( |
| 1890 |
self.dropdown.canvas_id, |
| 1891 |
self.MT.col_positions[c], |
| 1892 |
new_height - 1, |
| 1893 |
) |
| 1894 |
self.itemconfig(self.dropdown.canvas_id, anchor=anchor, height=win_h) |
| 1895 |
|
| 1896 |
def refresh_open_window_positions(self, zoom: Literal["in", "out"]) -> None: |
| 1897 |
if self.text_editor.open: |
| 1898 |
c = self.text_editor.column |
| 1899 |
self.text_editor.window.config( |
| 1900 |
height=self.current_height, |
| 1901 |
width=self.MT.col_positions[c + 1] - self.MT.col_positions[c] + 1, |
| 1902 |
) |
| 1903 |
self.text_editor.tktext.config(font=self.ops.header_font) |
| 1904 |
self.coords( |
| 1905 |
self.text_editor.canvas_id, |
| 1906 |
self.MT.col_positions[c], |
| 1907 |
0, |
| 1908 |
) |
| 1909 |
if self.dropdown.open: |
| 1910 |
if zoom == "in": |
| 1911 |
self.dropdown.window.zoom_in() |
| 1912 |
elif zoom == "out": |
| 1913 |
self.dropdown.window.zoom_out() |
| 1914 |
c = self.dropdown.get_coords() |
| 1915 |
if self.text_editor.open: |
| 1916 |
text_editor_h = self.text_editor.window.winfo_height() |
| 1917 |
win_h, anchor = self.get_dropdown_height_anchor(c, text_editor_h) |
| 1918 |
else: |
| 1919 |
text_editor_h = self.current_height |
| 1920 |
anchor = self.itemcget(self.dropdown.canvas_id, "anchor") |
| 1921 |
|
| 1922 |
self.dropdown.window.config(width=self.MT.col_positions[c + 1] - self.MT.col_positions[c] + 1) |
| 1923 |
if anchor == "nw": |
| 1924 |
self.coords( |
| 1925 |
self.dropdown.canvas_id, |
| 1926 |
self.MT.col_positions[c], |
| 1927 |
text_editor_h - 1, |
| 1928 |
) |
| 1929 |
|
| 1930 |
elif anchor == "sw": |
| 1931 |
self.coords( |
| 1932 |
self.dropdown.canvas_id, |
| 1933 |
self.MT.col_positions[c], |
| 1934 |
0, |
| 1935 |
) |
| 1936 |
|
| 1937 |
|
| 1938 |
def hide_text_editor(self) -> None: |
| 1939 |
if self.text_editor.open: |
| 1940 |
for binding in text_editor_to_unbind: |
| 1941 |
self.text_editor.tktext.unbind(binding) |
| 1942 |
self.itemconfig(self.text_editor.canvas_id, state="hidden") |
| 1943 |
self.text_editor.open = False |
| 1944 |
|
| 1945 |
|
| 1946 |
def close_text_editor(self, event: tk.Event) -> Literal["break"] | None: |
| 1947 |
|
| 1948 |
|
| 1949 |
try: |
| 1950 |
focused = self.focus_get() |
| 1951 |
except Exception: |
| 1952 |
focused = None |
| 1953 |
try: |
| 1954 |
if focused == self.text_editor.tktext.rc_popup_menu: |
| 1955 |
return "break" |
| 1956 |
except Exception: |
| 1957 |
pass |
| 1958 |
if focused is None: |
| 1959 |
return "break" |
| 1960 |
if event.keysym == "Escape": |
| 1961 |
self.hide_text_editor_and_dropdown() |
| 1962 |
self.focus_set() |
| 1963 |
return |
| 1964 |
|
| 1965 |
text_editor_value = self.text_editor.get() |
| 1966 |
c = self.text_editor.column |
| 1967 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 1968 |
event_data = event_dict( |
| 1969 |
name="end_edit_header", |
| 1970 |
sheet=self.PAR.name, |
| 1971 |
widget=self, |
| 1972 |
cells_header={datacn: self.get_cell_data(datacn)}, |
| 1973 |
key=event.keysym, |
| 1974 |
value=text_editor_value, |
| 1975 |
loc=c, |
| 1976 |
column=c, |
| 1977 |
boxes=self.MT.get_boxes(), |
| 1978 |
selected=self.MT.selected, |
| 1979 |
) |
| 1980 |
edited = False |
| 1981 |
set_data = partial( |
| 1982 |
self.set_cell_data_undo, |
| 1983 |
c=c, |
| 1984 |
datacn=datacn, |
| 1985 |
check_input_valid=False, |
| 1986 |
) |
| 1987 |
if self.MT.edit_validation_func: |
| 1988 |
text_editor_value = self.MT.edit_validation_func(event_data) |
| 1989 |
if text_editor_value is not None and self.input_valid_for_cell(datacn, text_editor_value): |
| 1990 |
edited = set_data(value=text_editor_value) |
| 1991 |
elif self.input_valid_for_cell(datacn, text_editor_value): |
| 1992 |
edited = set_data(value=text_editor_value) |
| 1993 |
if edited: |
| 1994 |
try_binding(self.extra_end_edit_cell_func, event_data) |
| 1995 |
self.MT.recreate_all_selection_boxes() |
| 1996 |
self.hide_text_editor_and_dropdown() |
| 1997 |
if event.keysym != "FocusOut": |
| 1998 |
self.focus_set() |
| 1999 |
return "break" |
| 2000 |
|
| 2001 |
def get_dropdown_height_anchor( |
| 2002 |
self, |
| 2003 |
c: int, |
| 2004 |
text_editor_h: None | int = None, |
| 2005 |
) -> tuple[int, Literal["nw"]]: |
| 2006 |
win_h = 5 |
| 2007 |
datacn = self.MT.datacn(c) |
| 2008 |
for i, v in enumerate(self.get_cell_kwargs(datacn, key="dropdown")["values"]): |
| 2009 |
v_numlines = len(v.split("\n") if isinstance(v, str) else f"{v}".split("\n")) |
| 2010 |
if v_numlines > 1: |
| 2011 |
win_h += self.MT.header_txt_height + (v_numlines * self.MT.header_txt_height) + 5 |
| 2012 |
else: |
| 2013 |
win_h += self.MT.min_header_height |
| 2014 |
if i == 5: |
| 2015 |
break |
| 2016 |
win_h = min(win_h, 500) |
| 2017 |
space_bot = self.MT.get_space_bot(0, text_editor_h) |
| 2018 |
win_h2 = int(win_h) |
| 2019 |
if win_h > space_bot: |
| 2020 |
win_h = space_bot - 1 |
| 2021 |
if win_h < self.MT.header_txt_height + 5: |
| 2022 |
win_h = self.MT.header_txt_height + 5 |
| 2023 |
elif win_h > win_h2: |
| 2024 |
win_h = win_h2 |
| 2025 |
return win_h, "nw" |
| 2026 |
|
| 2027 |
def dropdown_text_editor_modified( |
| 2028 |
self, |
| 2029 |
event: tk.Misc, |
| 2030 |
) -> None: |
| 2031 |
c = self.dropdown.get_coords() |
| 2032 |
event_data = event_dict( |
| 2033 |
name="table_dropdown_modified", |
| 2034 |
sheet=self.PAR.name, |
| 2035 |
value=self.text_editor.get(), |
| 2036 |
loc=c, |
| 2037 |
row=0, |
| 2038 |
column=c, |
| 2039 |
boxes=self.MT.get_boxes(), |
| 2040 |
selected=self.MT.selected, |
| 2041 |
) |
| 2042 |
try_binding(self.dropdown.window.modified_function, event_data) |
| 2043 |
|
| 2044 |
|
| 2045 |
if (hasattr(event, "state") and event.state & (0x0004 | 0x00000010)) or ( |
| 2046 |
hasattr(event, "keysym") and len(event.keysym) > 2 and event.keysym != "space" |
| 2047 |
): |
| 2048 |
return |
| 2049 |
self.text_editor.autocomplete(self.dropdown.window.search_and_see(event_data)) |
| 2050 |
return "break" |
| 2051 |
|
| 2052 |
def open_dropdown_window(self, c: int, event: Any = None) -> None: |
| 2053 |
self.hide_text_editor() |
| 2054 |
kwargs = self.get_cell_kwargs(self.MT.datacn(c), key="dropdown") |
| 2055 |
if kwargs["state"] == "disabled": |
| 2056 |
return |
| 2057 |
if kwargs["state"] == "normal" and not self.open_text_editor(event=event, c=c, dropdown=True): |
| 2058 |
return |
| 2059 |
win_h, anchor = self.get_dropdown_height_anchor(c) |
| 2060 |
win_w = self.MT.col_positions[c + 1] - self.MT.col_positions[c] + 1 |
| 2061 |
ypos = self.current_height - 1 |
| 2062 |
reset_kwargs = { |
| 2063 |
"r": 0, |
| 2064 |
"c": c, |
| 2065 |
"bg": self.ops.header_editor_bg, |
| 2066 |
"fg": self.ops.header_editor_fg, |
| 2067 |
"select_bg": self.ops.header_editor_select_bg, |
| 2068 |
"select_fg": self.ops.header_editor_select_fg, |
| 2069 |
"width": win_w, |
| 2070 |
"height": win_h, |
| 2071 |
"font": self.ops.header_font, |
| 2072 |
"ops": self.ops, |
| 2073 |
"outline_color": self.ops.header_selected_columns_bg, |
| 2074 |
"align": self.get_cell_align(c), |
| 2075 |
"values": kwargs["values"], |
| 2076 |
"search_function": kwargs["search_function"], |
| 2077 |
"modified_function": kwargs["modified_function"], |
| 2078 |
} |
| 2079 |
if self.dropdown.window: |
| 2080 |
self.dropdown.window.reset(**reset_kwargs) |
| 2081 |
self.itemconfig(self.dropdown.canvas_id, state="normal") |
| 2082 |
self.coords(self.dropdown.canvas_id, self.MT.col_positions[c], ypos) |
| 2083 |
self.dropdown.window.tkraise() |
| 2084 |
else: |
| 2085 |
self.dropdown.window = self.PAR._dropdown_cls( |
| 2086 |
self.winfo_toplevel(), |
| 2087 |
**reset_kwargs, |
| 2088 |
single_index="c", |
| 2089 |
close_dropdown_window=self.close_dropdown_window, |
| 2090 |
arrowkey_RIGHT=self.MT.arrowkey_RIGHT, |
| 2091 |
arrowkey_LEFT=self.MT.arrowkey_LEFT, |
| 2092 |
) |
| 2093 |
self.dropdown.canvas_id = self.create_window( |
| 2094 |
(self.MT.col_positions[c], ypos), |
| 2095 |
window=self.dropdown.window, |
| 2096 |
anchor=anchor, |
| 2097 |
) |
| 2098 |
self.update_idletasks() |
| 2099 |
if kwargs["state"] == "normal": |
| 2100 |
self.text_editor.tktext.bind( |
| 2101 |
"<KeyRelease>", |
| 2102 |
self.dropdown_text_editor_modified, |
| 2103 |
) |
| 2104 |
try: |
| 2105 |
self.after(1, lambda: self.text_editor.tktext.focus()) |
| 2106 |
self.after(2, self.text_editor.window.scroll_to_bottom()) |
| 2107 |
except Exception: |
| 2108 |
return |
| 2109 |
redraw = False |
| 2110 |
else: |
| 2111 |
self.dropdown.window.bind("<FocusOut>", lambda _x: self.close_dropdown_window(c)) |
| 2112 |
self.dropdown.window.bind("<Escape>", self.close_dropdown_window) |
| 2113 |
self.dropdown.window.focus_set() |
| 2114 |
redraw = True |
| 2115 |
self.dropdown.open = True |
| 2116 |
if redraw: |
| 2117 |
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=False, redraw_table=False) |
| 2118 |
|
| 2119 |
def close_dropdown_window( |
| 2120 |
self, |
| 2121 |
c: None | int = None, |
| 2122 |
selection: Any = None, |
| 2123 |
redraw: bool = True, |
| 2124 |
) -> None: |
| 2125 |
if c is not None and selection is not None: |
| 2126 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 2127 |
kwargs = self.get_cell_kwargs(datacn, key="dropdown") |
| 2128 |
event_data = event_dict( |
| 2129 |
name="end_edit_header", |
| 2130 |
sheet=self.PAR.name, |
| 2131 |
widget=self, |
| 2132 |
cells_header={datacn: self.get_cell_data(datacn)}, |
| 2133 |
key="??", |
| 2134 |
value=selection, |
| 2135 |
loc=c, |
| 2136 |
column=c, |
| 2137 |
boxes=self.MT.get_boxes(), |
| 2138 |
selected=self.MT.selected, |
| 2139 |
) |
| 2140 |
try_binding(kwargs["select_function"], event_data) |
| 2141 |
selection = selection if not self.MT.edit_validation_func else self.MT.edit_validation_func(event_data) |
| 2142 |
if selection is not None: |
| 2143 |
edited = self.set_cell_data_undo(c, datacn=datacn, value=selection, redraw=not redraw) |
| 2144 |
if edited: |
| 2145 |
try_binding(self.extra_end_edit_cell_func, event_data) |
| 2146 |
self.MT.recreate_all_selection_boxes() |
| 2147 |
self.focus_set() |
| 2148 |
self.hide_text_editor_and_dropdown(redraw=redraw) |
| 2149 |
|
| 2150 |
def hide_text_editor_and_dropdown(self, redraw: bool = True) -> None: |
| 2151 |
self.hide_text_editor() |
| 2152 |
self.hide_dropdown_window() |
| 2153 |
if redraw: |
| 2154 |
self.MT.refresh() |
| 2155 |
|
| 2156 |
def mouseclick_outside_editor_or_dropdown(self, inside: bool = False) -> int | None: |
| 2157 |
closed_dd_coords = self.dropdown.get_coords() |
| 2158 |
if self.text_editor.open: |
| 2159 |
self.close_text_editor(new_tk_event("ButtonPress-1")) |
| 2160 |
if closed_dd_coords is not None: |
| 2161 |
self.hide_dropdown_window() |
| 2162 |
if inside: |
| 2163 |
self.MT.main_table_redraw_grid_and_text( |
| 2164 |
redraw_header=True, |
| 2165 |
redraw_row_index=False, |
| 2166 |
redraw_table=False, |
| 2167 |
) |
| 2168 |
return closed_dd_coords |
| 2169 |
|
| 2170 |
def mouseclick_outside_editor_or_dropdown_all_canvases(self, inside: bool = False) -> int | None: |
| 2171 |
self.RI.mouseclick_outside_editor_or_dropdown() |
| 2172 |
self.MT.mouseclick_outside_editor_or_dropdown() |
| 2173 |
return self.mouseclick_outside_editor_or_dropdown(inside) |
| 2174 |
|
| 2175 |
def hide_dropdown_window(self) -> None: |
| 2176 |
if self.dropdown.open: |
| 2177 |
self.dropdown.window.unbind("<FocusOut>") |
| 2178 |
self.itemconfig(self.dropdown.canvas_id, state="hidden") |
| 2179 |
self.dropdown.open = False |
| 2180 |
|
| 2181 |
|
| 2182 |
def set_cell_data_undo( |
| 2183 |
self, |
| 2184 |
c: int = 0, |
| 2185 |
datacn: int | None = None, |
| 2186 |
value: Any = "", |
| 2187 |
cell_resize: bool = True, |
| 2188 |
undo: bool = True, |
| 2189 |
redraw: bool = True, |
| 2190 |
check_input_valid: bool = True, |
| 2191 |
) -> bool: |
| 2192 |
if datacn is None: |
| 2193 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 2194 |
event_data = event_dict( |
| 2195 |
name="edit_header", |
| 2196 |
sheet=self.PAR.name, |
| 2197 |
widget=self, |
| 2198 |
cells_header={datacn: self.get_cell_data(datacn)}, |
| 2199 |
boxes=self.MT.get_boxes(), |
| 2200 |
selected=self.MT.selected, |
| 2201 |
) |
| 2202 |
edited = False |
| 2203 |
if isinstance(self.MT._headers, int): |
| 2204 |
disprn = self.MT.try_disprn(self.MT._headers) |
| 2205 |
edited = self.MT.set_cell_data_undo( |
| 2206 |
r=disprn if isinstance(disprn, int) else 0, |
| 2207 |
c=c, |
| 2208 |
datarn=self.MT._headers, |
| 2209 |
datacn=datacn, |
| 2210 |
value=value, |
| 2211 |
undo=True, |
| 2212 |
cell_resize=isinstance(disprn, int), |
| 2213 |
) |
| 2214 |
else: |
| 2215 |
self.fix_header(datacn) |
| 2216 |
if not check_input_valid or self.input_valid_for_cell(datacn, value): |
| 2217 |
if self.MT.undo_enabled and undo: |
| 2218 |
self.MT.undo_stack.append(stored_event_dict(event_data)) |
| 2219 |
self.set_cell_data(datacn=datacn, value=value) |
| 2220 |
edited = True |
| 2221 |
if edited and cell_resize and self.ops.cell_auto_resize_enabled: |
| 2222 |
if self.height_resizing_enabled: |
| 2223 |
self.set_height_of_header_to_text(self.cell_str(datacn, fix=False)) |
| 2224 |
self.set_col_width_run_binding(c) |
| 2225 |
if redraw: |
| 2226 |
self.MT.refresh() |
| 2227 |
if edited: |
| 2228 |
self.MT.sheet_modified(event_data) |
| 2229 |
return edited |
| 2230 |
|
| 2231 |
def set_cell_data(self, datacn: int | None = None, value: Any = "") -> None: |
| 2232 |
if isinstance(self.MT._headers, int): |
| 2233 |
self.MT.set_cell_data(datarn=self.MT._headers, datacn=datacn, value=value) |
| 2234 |
else: |
| 2235 |
self.fix_header(datacn) |
| 2236 |
if self.get_cell_kwargs(datacn, key="checkbox"): |
| 2237 |
self.MT._headers[datacn] = try_to_bool(value) |
| 2238 |
else: |
| 2239 |
self.MT._headers[datacn] = value |
| 2240 |
|
| 2241 |
def input_valid_for_cell(self, datacn: int, value: Any, check_readonly: bool = True) -> bool: |
| 2242 |
kwargs = self.get_cell_kwargs(datacn, key=None) |
| 2243 |
if check_readonly and "readonly" in kwargs: |
| 2244 |
return False |
| 2245 |
elif "checkbox" in kwargs: |
| 2246 |
return is_bool_like(value) |
| 2247 |
else: |
| 2248 |
return not ( |
| 2249 |
self.cell_equal_to(datacn, value) |
| 2250 |
or (kwargs := kwargs.get("dropdown", {})) |
| 2251 |
and kwargs["validate_input"] |
| 2252 |
and value not in kwargs["values"] |
| 2253 |
) |
| 2254 |
|
| 2255 |
def cell_equal_to(self, datacn: int, value: Any) -> bool: |
| 2256 |
self.fix_header(datacn) |
| 2257 |
if isinstance(self.MT._headers, list): |
| 2258 |
return self.MT._headers[datacn] == value |
| 2259 |
elif isinstance(self.MT._headers, int): |
| 2260 |
return self.MT.cell_equal_to(self.MT._headers, datacn, value) |
| 2261 |
|
| 2262 |
def get_cell_data( |
| 2263 |
self, |
| 2264 |
datacn: int, |
| 2265 |
get_displayed: bool = False, |
| 2266 |
none_to_empty_str: bool = False, |
| 2267 |
redirect_int: bool = False, |
| 2268 |
) -> Any: |
| 2269 |
if get_displayed: |
| 2270 |
return self.cell_str(datacn, fix=False) |
| 2271 |
if redirect_int and isinstance(self.MT._headers, int): |
| 2272 |
return self.MT.get_cell_data(self.MT._headers, datacn, none_to_empty_str=True) |
| 2273 |
if ( |
| 2274 |
isinstance(self.MT._headers, int) |
| 2275 |
or not self.MT._headers |
| 2276 |
or datacn >= len(self.MT._headers) |
| 2277 |
or (self.MT._headers[datacn] is None and none_to_empty_str) |
| 2278 |
): |
| 2279 |
return "" |
| 2280 |
return self.MT._headers[datacn] |
| 2281 |
|
| 2282 |
def cell_str(self, datacn: int, fix: bool = True) -> str: |
| 2283 |
kwargs = self.get_cell_kwargs(datacn, key="dropdown") |
| 2284 |
if kwargs: |
| 2285 |
if kwargs["text"] is not None: |
| 2286 |
return f"{kwargs['text']}" |
| 2287 |
else: |
| 2288 |
kwargs = self.get_cell_kwargs(datacn, key="checkbox") |
| 2289 |
if kwargs: |
| 2290 |
return f"{kwargs['text']}" |
| 2291 |
if isinstance(self.MT._headers, int): |
| 2292 |
return self.MT.cell_str(self.MT._headers, datacn, get_displayed=True) |
| 2293 |
if fix: |
| 2294 |
self.fix_header(datacn) |
| 2295 |
try: |
| 2296 |
value = self.MT._headers[datacn] |
| 2297 |
if value is None: |
| 2298 |
value = "" |
| 2299 |
elif not isinstance(value, str): |
| 2300 |
value = f"{value}" |
| 2301 |
except Exception: |
| 2302 |
value = "" |
| 2303 |
if not value and self.ops.show_default_header_for_empty: |
| 2304 |
value = get_n2a(datacn, self.ops.default_header) |
| 2305 |
return value |
| 2306 |
|
| 2307 |
def get_value_for_empty_cell(self, datacn: int, c_ops: bool = True) -> Any: |
| 2308 |
kwargs = self.get_cell_kwargs(datacn, key=None, cell=c_ops) |
| 2309 |
if "checkbox" in kwargs: |
| 2310 |
return False |
| 2311 |
elif "dropdown" in kwargs and kwargs["dropdown"]["validate_input"] and kwargs["dropdown"]["values"]: |
| 2312 |
return kwargs["dropdown"]["values"][0] |
| 2313 |
else: |
| 2314 |
return "" |
| 2315 |
|
| 2316 |
def get_empty_header_seq(self, end: int, start: int = 0, c_ops: bool = True) -> list[Any]: |
| 2317 |
return [self.get_value_for_empty_cell(datacn, c_ops=c_ops) for datacn in range(start, end)] |
| 2318 |
|
| 2319 |
def fix_header(self, datacn: None | int = None) -> None: |
| 2320 |
if isinstance(self.MT._headers, int): |
| 2321 |
return |
| 2322 |
if isinstance(self.MT._headers, float): |
| 2323 |
self.MT._headers = int(self.MT._headers) |
| 2324 |
return |
| 2325 |
if not isinstance(self.MT._headers, list): |
| 2326 |
try: |
| 2327 |
self.MT._headers = list(self.MT._headers) |
| 2328 |
except Exception: |
| 2329 |
self.MT._headers = [] |
| 2330 |
if isinstance(datacn, int) and datacn >= len(self.MT._headers): |
| 2331 |
self.MT._headers.extend(self.get_empty_header_seq(end=datacn + 1, start=len(self.MT._headers))) |
| 2332 |
|
| 2333 |
|
| 2334 |
def set_col_width_run_binding(self, c: int, width: int | None = None, only_if_too_small: bool = True) -> None: |
| 2335 |
old_width = self.MT.col_positions[c + 1] - self.MT.col_positions[c] |
| 2336 |
new_width = self.set_col_width(c, width=width, only_if_too_small=only_if_too_small) |
| 2337 |
if self.column_width_resize_func is not None and old_width != new_width: |
| 2338 |
self.column_width_resize_func( |
| 2339 |
event_dict( |
| 2340 |
name="resize", |
| 2341 |
sheet=self.PAR.name, |
| 2342 |
resized_columns={c: {"old_size": old_width, "new_size": new_width}}, |
| 2343 |
) |
| 2344 |
) |
| 2345 |
|
| 2346 |
|
| 2347 |
def click_checkbox(self, c: int, datacn: int | None = None, undo: bool = True, redraw: bool = True) -> None: |
| 2348 |
if datacn is None: |
| 2349 |
datacn = c if self.MT.all_columns_displayed else self.MT.displayed_columns[c] |
| 2350 |
kwargs = self.get_cell_kwargs(datacn, key="checkbox") |
| 2351 |
if kwargs["state"] == "normal": |
| 2352 |
pre_edit_value = self.get_cell_data(datacn) |
| 2353 |
if isinstance(self.MT._headers, list): |
| 2354 |
value = not self.MT._headers[datacn] if isinstance(self.MT._headers[datacn], bool) else False |
| 2355 |
elif isinstance(self.MT._headers, int): |
| 2356 |
value = ( |
| 2357 |
not self.MT.data[self.MT._headers][datacn] |
| 2358 |
if isinstance(self.MT.data[self.MT._headers][datacn], bool) |
| 2359 |
else False |
| 2360 |
) |
| 2361 |
else: |
| 2362 |
value = False |
| 2363 |
self.set_cell_data_undo(c, datacn=datacn, value=value, cell_resize=False) |
| 2364 |
event_data = event_dict( |
| 2365 |
name="end_edit_header", |
| 2366 |
sheet=self.PAR.name, |
| 2367 |
widget=self, |
| 2368 |
cells_header={datacn: pre_edit_value}, |
| 2369 |
key="??", |
| 2370 |
value=value, |
| 2371 |
loc=c, |
| 2372 |
column=c, |
| 2373 |
boxes=self.MT.get_boxes(), |
| 2374 |
selected=self.MT.selected, |
| 2375 |
) |
| 2376 |
if kwargs["check_function"] is not None: |
| 2377 |
kwargs["check_function"](event_data) |
| 2378 |
try_binding(self.extra_end_edit_cell_func, event_data) |
| 2379 |
if redraw: |
| 2380 |
self.MT.refresh() |
| 2381 |
|
| 2382 |
def get_cell_kwargs(self, datacn: int, key: Hashable | None = "dropdown", cell: bool = True) -> dict: |
| 2383 |
if cell and datacn in self.cell_options: |
| 2384 |
return self.cell_options[datacn] if key is None else self.cell_options[datacn].get(key, {}) |
| 2385 |
else: |
| 2386 |
return {} |