quantumgui 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- quantumgui/__init__.py +9 -0
- quantumgui/__main__.py +6 -0
- quantumgui/cli.py +341 -0
- quantumgui/core/__init__.py +39 -0
- quantumgui/core/animation.py +221 -0
- quantumgui/core/audit.py +323 -0
- quantumgui/core/context.py +54 -0
- quantumgui/core/data_source.py +426 -0
- quantumgui/core/draw.py +428 -0
- quantumgui/core/enterprise.py +402 -0
- quantumgui/core/enterprise_app.py +419 -0
- quantumgui/core/events.py +34 -0
- quantumgui/core/geometry.py +108 -0
- quantumgui/core/native_dialogs.py +276 -0
- quantumgui/core/pdf_report.py +390 -0
- quantumgui/core/rules.py +284 -0
- quantumgui/core/state.py +42 -0
- quantumgui/core/task_runner.py +181 -0
- quantumgui/core/widget.py +498 -0
- quantumgui/core/window.py +920 -0
- quantumgui/core/xlsx_export.py +145 -0
- quantumgui/py.typed +0 -0
- quantumgui/theme/__init__.py +32 -0
- quantumgui/theme/hct.py +191 -0
- quantumgui/theme/theme.py +93 -0
- quantumgui/theme/tokens.py +423 -0
- quantumgui/ui.py +2142 -0
- quantumgui/widgets/__init__.py +168 -0
- quantumgui/widgets/advanced.py +499 -0
- quantumgui/widgets/button.py +224 -0
- quantumgui/widgets/charts.py +625 -0
- quantumgui/widgets/charts_bi.py +528 -0
- quantumgui/widgets/charts_extra.py +430 -0
- quantumgui/widgets/charts_pro.py +568 -0
- quantumgui/widgets/checkbox.py +199 -0
- quantumgui/widgets/chip.py +219 -0
- quantumgui/widgets/containers.py +321 -0
- quantumgui/widgets/data_controls.py +487 -0
- quantumgui/widgets/data_grid.py +956 -0
- quantumgui/widgets/data_table.py +551 -0
- quantumgui/widgets/date_picker.py +407 -0
- quantumgui/widgets/display_media.py +372 -0
- quantumgui/widgets/display_pro.py +407 -0
- quantumgui/widgets/drawer.py +252 -0
- quantumgui/widgets/enterprise_controls.py +934 -0
- quantumgui/widgets/fab.py +261 -0
- quantumgui/widgets/forms_pro.py +703 -0
- quantumgui/widgets/forms_pro_media.py +587 -0
- quantumgui/widgets/inputs.py +870 -0
- quantumgui/widgets/layout_pro.py +162 -0
- quantumgui/widgets/list_view.py +307 -0
- quantumgui/widgets/masked_input.py +131 -0
- quantumgui/widgets/master_detail.py +155 -0
- quantumgui/widgets/navigation.py +382 -0
- quantumgui/widgets/overlays.py +355 -0
- quantumgui/widgets/productivity.py +500 -0
- quantumgui/widgets/productivity_boards.py +544 -0
- quantumgui/widgets/radio.py +209 -0
- quantumgui/widgets/scroll_view.py +129 -0
- quantumgui/widgets/select.py +373 -0
- quantumgui/widgets/sheets.py +249 -0
- quantumgui/widgets/slider.py +209 -0
- quantumgui/widgets/switch.py +196 -0
- quantumgui/widgets/tabs.py +175 -0
- quantumgui/widgets/text.py +101 -0
- quantumgui/widgets/text_field.py +490 -0
- quantumgui/widgets/time_picker.py +499 -0
- quantumgui-1.0.0.dist-info/METADATA +137 -0
- quantumgui-1.0.0.dist-info/RECORD +73 -0
- quantumgui-1.0.0.dist-info/WHEEL +5 -0
- quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
- quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
- quantumgui-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,956 @@
|
|
|
1
|
+
"""Enterprise virtualized DataGrid for 100,000+ rows at 120 FPS with inline editing,
|
|
2
|
+
live filtering, column sorting, aggregate totals row (SUM/AVG/COUNT/MIN/MAX), and CSV export.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import csv
|
|
8
|
+
import io
|
|
9
|
+
import time
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Callable, Literal, Mapping, Sequence
|
|
13
|
+
|
|
14
|
+
import glfw
|
|
15
|
+
import skia
|
|
16
|
+
|
|
17
|
+
from ..core.data_source import DataSource, coerce_data_source
|
|
18
|
+
from ..core.draw import (
|
|
19
|
+
ColorSpec,
|
|
20
|
+
draw_elevation_shadow,
|
|
21
|
+
draw_icon,
|
|
22
|
+
draw_text_centered_y,
|
|
23
|
+
font_for_style,
|
|
24
|
+
make_rrect,
|
|
25
|
+
make_rrect_spec,
|
|
26
|
+
measure_text,
|
|
27
|
+
resolve_color,
|
|
28
|
+
)
|
|
29
|
+
from ..core.events import CharEvent, KeyEvent, MouseEvent, ScrollEvent
|
|
30
|
+
from ..core.geometry import Constraints, Rect, Size
|
|
31
|
+
from ..core.widget import Widget
|
|
32
|
+
from ..theme import Color, Theme
|
|
33
|
+
|
|
34
|
+
GridColType = Literal["text", "int", "float", "currency", "percent", "bool", "badge"]
|
|
35
|
+
GridColAlign = Literal["left", "center", "right"]
|
|
36
|
+
AggregateOp = Literal["sum", "avg", "count", "min", "max"]
|
|
37
|
+
BadgeTone = Literal["primary", "secondary", "tertiary", "error", "neutral"]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class GridColumn:
|
|
42
|
+
"""Column specification for `ui.data_grid`."""
|
|
43
|
+
|
|
44
|
+
title: str
|
|
45
|
+
type: GridColType = "text"
|
|
46
|
+
width: float | None = None
|
|
47
|
+
align: GridColAlign | None = None
|
|
48
|
+
sortable: bool = True
|
|
49
|
+
editable: bool = True
|
|
50
|
+
aggregate: AggregateOp | None = None
|
|
51
|
+
prefix: str = ""
|
|
52
|
+
suffix: str = ""
|
|
53
|
+
decimals: int = 2
|
|
54
|
+
badge_colors: dict[str, BadgeTone] = field(default_factory=dict)
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def effective_align(self) -> GridColAlign:
|
|
58
|
+
if self.align is not None:
|
|
59
|
+
return self.align
|
|
60
|
+
if self.type in ("int", "float", "currency", "percent"):
|
|
61
|
+
return "right"
|
|
62
|
+
if self.type in ("bool", "badge"):
|
|
63
|
+
return "center"
|
|
64
|
+
return "left"
|
|
65
|
+
|
|
66
|
+
def format_value(self, value: Any) -> str:
|
|
67
|
+
if value is None:
|
|
68
|
+
return ""
|
|
69
|
+
if self.type == "int":
|
|
70
|
+
try:
|
|
71
|
+
return f"{self.prefix}{int(round(float(value))):,}{self.suffix}"
|
|
72
|
+
except (TypeError, ValueError):
|
|
73
|
+
return str(value)
|
|
74
|
+
if self.type == "float":
|
|
75
|
+
try:
|
|
76
|
+
return f"{self.prefix}{float(value):,.{self.decimals}f}{self.suffix}"
|
|
77
|
+
except (TypeError, ValueError):
|
|
78
|
+
return str(value)
|
|
79
|
+
if self.type == "currency":
|
|
80
|
+
pref = self.prefix if self.prefix else "$"
|
|
81
|
+
try:
|
|
82
|
+
return f"{pref}{float(value):,.{self.decimals}f}{self.suffix}"
|
|
83
|
+
except (TypeError, ValueError):
|
|
84
|
+
return str(value)
|
|
85
|
+
if self.type == "percent":
|
|
86
|
+
try:
|
|
87
|
+
v = float(value)
|
|
88
|
+
pct = v * 100.0 if abs(v) <= 1.0 else v
|
|
89
|
+
return f"{self.prefix}{pct:.{self.decimals}f}%{self.suffix}"
|
|
90
|
+
except (TypeError, ValueError):
|
|
91
|
+
return str(value)
|
|
92
|
+
if self.type == "bool":
|
|
93
|
+
return "Sí" if bool(value) else "No"
|
|
94
|
+
return f"{self.prefix}{value}{self.suffix}"
|
|
95
|
+
|
|
96
|
+
def parse_edited(self, raw: str, fallback: Any) -> Any:
|
|
97
|
+
s = raw.strip()
|
|
98
|
+
if self.type == "int":
|
|
99
|
+
try:
|
|
100
|
+
return int(float(s.replace(",", "").replace("$", "")))
|
|
101
|
+
except ValueError:
|
|
102
|
+
return fallback
|
|
103
|
+
if self.type in ("float", "currency", "percent"):
|
|
104
|
+
try:
|
|
105
|
+
return float(s.replace(",", "").replace("$", "").replace("%", ""))
|
|
106
|
+
except ValueError:
|
|
107
|
+
return fallback
|
|
108
|
+
if self.type == "bool":
|
|
109
|
+
return s.lower() in ("1", "true", "si", "sí", "yes", "y", "s")
|
|
110
|
+
return raw
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _coerce_grid_col(col: str | GridColumn) -> GridColumn:
|
|
114
|
+
return col if isinstance(col, GridColumn) else GridColumn(title=str(col))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class DataGrid(Widget):
|
|
118
|
+
"""Virtualized Enterprise DataGrid supporting 100,000+ rows at O(1) per frame.
|
|
119
|
+
|
|
120
|
+
Features:
|
|
121
|
+
- O(1) virtualized rendering & hit-testing (only visible rows are painted)
|
|
122
|
+
- Sticky header, optional live search/filter toolbar, and sticky aggregate totals footer
|
|
123
|
+
- Draggable M3 vertical scrollbar + mouse wheel + keyboard navigation
|
|
124
|
+
- Inline cell editing (double-click or F2 / Enter)
|
|
125
|
+
- Native CSV export (`export_csv` / `to_csv`) and Skia PDF export (`ui.export_pdf`)
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
interactive = True
|
|
129
|
+
focusable = True
|
|
130
|
+
manages_own_box = True
|
|
131
|
+
|
|
132
|
+
TOOLBAR_H = 44.0
|
|
133
|
+
HEADER_H = 44.0
|
|
134
|
+
FOOTER_H = 40.0
|
|
135
|
+
PAGER_H = 36.0
|
|
136
|
+
SCROLLBAR_W = 12.0
|
|
137
|
+
|
|
138
|
+
def __init__(
|
|
139
|
+
self,
|
|
140
|
+
columns: Sequence[str | GridColumn] | None = None,
|
|
141
|
+
rows: Sequence[Sequence[Any]] | None = None,
|
|
142
|
+
*,
|
|
143
|
+
source: Any = None,
|
|
144
|
+
column_overrides: Mapping[str, GridColumn] | None = None,
|
|
145
|
+
page_size: int | None = None,
|
|
146
|
+
virtualized: bool = True,
|
|
147
|
+
height: float = 360.0,
|
|
148
|
+
row_height: float = 40.0,
|
|
149
|
+
searchable: bool = True,
|
|
150
|
+
editable: bool = False,
|
|
151
|
+
striped: bool = True,
|
|
152
|
+
show_totals: bool | None = None,
|
|
153
|
+
header_bg: ColorSpec | None = None,
|
|
154
|
+
on_row_click: Callable[[int, list[Any]], None] | None = None,
|
|
155
|
+
on_cell_edit: Callable[[int, int, Any, Any], None] | None = None,
|
|
156
|
+
) -> None:
|
|
157
|
+
super().__init__()
|
|
158
|
+
self._source: DataSource | None = coerce_data_source(source) if source is not None else None
|
|
159
|
+
self._column_overrides: dict[str, GridColumn] = dict(column_overrides) if column_overrides else {}
|
|
160
|
+
self.page_size: int | None = max(1, int(page_size)) if page_size is not None else None
|
|
161
|
+
self.virtualized: bool = bool(virtualized)
|
|
162
|
+
self._page: int = 1
|
|
163
|
+
self._source_total_count: int = 0
|
|
164
|
+
self._field_names: list[str] = []
|
|
165
|
+
|
|
166
|
+
if self._source is not None:
|
|
167
|
+
self._field_names = list(self._source.columns)
|
|
168
|
+
if columns is not None:
|
|
169
|
+
self.columns = [_coerce_grid_col(c) for c in columns]
|
|
170
|
+
else:
|
|
171
|
+
self.columns = self._build_columns_from_source(self._source)
|
|
172
|
+
self._source_total_count = self._source.count()
|
|
173
|
+
if self.page_size is not None:
|
|
174
|
+
self._rows = self._source.fetch(limit=self.page_size, offset=0)
|
|
175
|
+
else:
|
|
176
|
+
self._rows = self._source.fetch()
|
|
177
|
+
self._source.subscribe(self.reload)
|
|
178
|
+
else:
|
|
179
|
+
self.columns = [_coerce_grid_col(c) for c in (columns or ())]
|
|
180
|
+
self._field_names = [c.title for c in self.columns]
|
|
181
|
+
self._rows = [list(r) for r in (rows or ())]
|
|
182
|
+
self._source_total_count = len(self._rows)
|
|
183
|
+
|
|
184
|
+
self.grid_height = float(height)
|
|
185
|
+
self.row_height = float(row_height)
|
|
186
|
+
self.searchable = searchable
|
|
187
|
+
self.editable = editable
|
|
188
|
+
self.striped = striped
|
|
189
|
+
self.show_totals = (
|
|
190
|
+
any(c.aggregate is not None for c in self.columns)
|
|
191
|
+
if show_totals is None
|
|
192
|
+
else bool(show_totals)
|
|
193
|
+
)
|
|
194
|
+
self.header_bg = header_bg
|
|
195
|
+
self.on_row_click = on_row_click
|
|
196
|
+
self.on_cell_edit = on_cell_edit
|
|
197
|
+
|
|
198
|
+
self._fill_width = True
|
|
199
|
+
self._scroll_y: float = 0.0
|
|
200
|
+
self._filter_text: str = ""
|
|
201
|
+
self._search_focused: bool = False
|
|
202
|
+
self._sort_col: int | None = None
|
|
203
|
+
self._sort_asc: bool = True
|
|
204
|
+
|
|
205
|
+
self._view_indices: list[int] = list(range(len(self._rows)))
|
|
206
|
+
self._totals_cache: list[str] = [""] * len(self.columns)
|
|
207
|
+
|
|
208
|
+
self._selected_view_row: int | None = None
|
|
209
|
+
self._selected_col: int = 0
|
|
210
|
+
self._hover_view_row: int = -1
|
|
211
|
+
self._hover_header: int = -1
|
|
212
|
+
|
|
213
|
+
# Inline cell editor state (at most 1 cell active at a time -> zero memory overhead)
|
|
214
|
+
self._editing_cell: tuple[int, int] | None = None # (view_row, col_idx)
|
|
215
|
+
self._edit_buffer: str = ""
|
|
216
|
+
self._last_click_time: float = 0.0
|
|
217
|
+
self._last_click_cell: tuple[int, int] | None = None
|
|
218
|
+
|
|
219
|
+
# Draggable scrollbar & pager rects
|
|
220
|
+
self._dragging_thumb: bool = False
|
|
221
|
+
self._drag_offset_y: float = 0.0
|
|
222
|
+
self._prev_btn_rect: Rect = Rect(0.0, 0.0, 0.0, 0.0)
|
|
223
|
+
self._next_btn_rect: Rect = Rect(0.0, 0.0, 0.0, 0.0)
|
|
224
|
+
|
|
225
|
+
self._recompute_totals()
|
|
226
|
+
|
|
227
|
+
def _build_columns_from_source(self, src: DataSource) -> list[GridColumn]:
|
|
228
|
+
types = src.column_types
|
|
229
|
+
cols: list[GridColumn] = []
|
|
230
|
+
for field_name in src.columns:
|
|
231
|
+
if field_name in self._column_overrides:
|
|
232
|
+
cols.append(self._column_overrides[field_name])
|
|
233
|
+
else:
|
|
234
|
+
inferred = types.get(field_name, "text")
|
|
235
|
+
col_type: GridColType = inferred if inferred in ("text", "int", "float", "bool") else "text" # type: ignore[assignment]
|
|
236
|
+
title = field_name.replace("_", " ").strip().title() or field_name
|
|
237
|
+
cols.append(GridColumn(title=title, type=col_type))
|
|
238
|
+
return cols
|
|
239
|
+
|
|
240
|
+
def reload(self) -> None:
|
|
241
|
+
"""Reload rows from the attached `DataSource` (respecting `page_size` and current `page`)."""
|
|
242
|
+
if self._source is None:
|
|
243
|
+
return
|
|
244
|
+
self._editing_cell = None
|
|
245
|
+
self._source_total_count = self._source.count()
|
|
246
|
+
max_p = self.total_pages
|
|
247
|
+
if self._page > max_p:
|
|
248
|
+
self._page = max_p
|
|
249
|
+
if self.page_size is not None:
|
|
250
|
+
offset = (self._page - 1) * self.page_size
|
|
251
|
+
self._rows = self._source.fetch(limit=self.page_size, offset=offset)
|
|
252
|
+
else:
|
|
253
|
+
self._rows = self._source.fetch()
|
|
254
|
+
self._scroll_y = 0.0
|
|
255
|
+
self._rebuild_view()
|
|
256
|
+
self.invalidate()
|
|
257
|
+
|
|
258
|
+
# --- Pagination API ---
|
|
259
|
+
|
|
260
|
+
@property
|
|
261
|
+
def page(self) -> int:
|
|
262
|
+
return self._page
|
|
263
|
+
|
|
264
|
+
@property
|
|
265
|
+
def total_pages(self) -> int:
|
|
266
|
+
if self.page_size is None:
|
|
267
|
+
return 1
|
|
268
|
+
total = self._source_total_count if self._source is not None else len(self._view_indices)
|
|
269
|
+
return max(1, (total + self.page_size - 1) // self.page_size)
|
|
270
|
+
|
|
271
|
+
def go_to_page(self, page: int) -> None:
|
|
272
|
+
if self.page_size is None:
|
|
273
|
+
return
|
|
274
|
+
target = max(1, min(int(page), self.total_pages))
|
|
275
|
+
if target == self._page:
|
|
276
|
+
return
|
|
277
|
+
self._page = target
|
|
278
|
+
self._scroll_y = 0.0
|
|
279
|
+
self._editing_cell = None
|
|
280
|
+
self._selected_view_row = None
|
|
281
|
+
if self._source is not None:
|
|
282
|
+
self.reload()
|
|
283
|
+
else:
|
|
284
|
+
self.invalidate()
|
|
285
|
+
|
|
286
|
+
def next_page(self) -> None:
|
|
287
|
+
self.go_to_page(self._page + 1)
|
|
288
|
+
|
|
289
|
+
def prev_page(self) -> None:
|
|
290
|
+
self.go_to_page(self._page - 1)
|
|
291
|
+
|
|
292
|
+
def _active_page_indices(self) -> list[int]:
|
|
293
|
+
"""Indices displayed on the current page (all `_view_indices` when unpaginated or SQL-paged)."""
|
|
294
|
+
if self.page_size is None or self._source is not None:
|
|
295
|
+
return self._view_indices
|
|
296
|
+
start = (self._page - 1) * self.page_size
|
|
297
|
+
return self._view_indices[start : start + self.page_size]
|
|
298
|
+
|
|
299
|
+
# --- Public Data & Filter API ---
|
|
300
|
+
|
|
301
|
+
@property
|
|
302
|
+
def rows(self) -> list[list[Any]]:
|
|
303
|
+
return self._rows
|
|
304
|
+
|
|
305
|
+
@rows.setter
|
|
306
|
+
def rows(self, new_rows: Sequence[Sequence[Any]]) -> None:
|
|
307
|
+
self._rows = [list(r) for r in new_rows]
|
|
308
|
+
self._editing_cell = None
|
|
309
|
+
self._selected_view_row = None
|
|
310
|
+
self._scroll_y = 0.0
|
|
311
|
+
self._rebuild_view()
|
|
312
|
+
self.invalidate()
|
|
313
|
+
|
|
314
|
+
@property
|
|
315
|
+
def visible_row_count(self) -> int:
|
|
316
|
+
return len(self._view_indices)
|
|
317
|
+
|
|
318
|
+
@property
|
|
319
|
+
def scroll_y(self) -> float:
|
|
320
|
+
return self._scroll_y
|
|
321
|
+
|
|
322
|
+
@scroll_y.setter
|
|
323
|
+
def scroll_y(self, val: float) -> None:
|
|
324
|
+
clamped = max(0.0, min(float(val), self.max_scroll))
|
|
325
|
+
if abs(clamped - self._scroll_y) > 1e-4:
|
|
326
|
+
self._scroll_y = clamped
|
|
327
|
+
self.invalidate()
|
|
328
|
+
|
|
329
|
+
@property
|
|
330
|
+
def filter_text(self) -> str:
|
|
331
|
+
return self._filter_text
|
|
332
|
+
|
|
333
|
+
@filter_text.setter
|
|
334
|
+
def filter_text(self, text: str) -> None:
|
|
335
|
+
s = str(text)
|
|
336
|
+
if s != self._filter_text:
|
|
337
|
+
self._filter_text = s
|
|
338
|
+
self._scroll_y = 0.0
|
|
339
|
+
self._editing_cell = None
|
|
340
|
+
self._rebuild_view()
|
|
341
|
+
self.invalidate()
|
|
342
|
+
|
|
343
|
+
@property
|
|
344
|
+
def totals(self) -> list[str]:
|
|
345
|
+
return list(self._totals_cache)
|
|
346
|
+
|
|
347
|
+
def sort_by(self, col_idx: int, ascending: bool | None = None) -> None:
|
|
348
|
+
if not (0 <= col_idx < len(self.columns)) or not self.columns[col_idx].sortable:
|
|
349
|
+
return
|
|
350
|
+
if ascending is None:
|
|
351
|
+
if self._sort_col == col_idx:
|
|
352
|
+
self._sort_asc = not self._sort_asc
|
|
353
|
+
else:
|
|
354
|
+
self._sort_col = col_idx
|
|
355
|
+
self._sort_asc = True
|
|
356
|
+
else:
|
|
357
|
+
self._sort_col = col_idx
|
|
358
|
+
self._sort_asc = bool(ascending)
|
|
359
|
+
self._editing_cell = None
|
|
360
|
+
self._rebuild_view()
|
|
361
|
+
self.invalidate()
|
|
362
|
+
|
|
363
|
+
def _rebuild_view(self) -> None:
|
|
364
|
+
q = self._filter_text.strip().lower()
|
|
365
|
+
if not q:
|
|
366
|
+
indices = list(range(len(self._rows)))
|
|
367
|
+
else:
|
|
368
|
+
indices = [
|
|
369
|
+
i
|
|
370
|
+
for i, row in enumerate(self._rows)
|
|
371
|
+
if any(q in str(cell).lower() for cell in row)
|
|
372
|
+
]
|
|
373
|
+
|
|
374
|
+
if self._sort_col is not None:
|
|
375
|
+
col = self._sort_col
|
|
376
|
+
|
|
377
|
+
def _key(row_i: int) -> tuple[int, Any]:
|
|
378
|
+
row = self._rows[row_i]
|
|
379
|
+
v = row[col] if col < len(row) else None
|
|
380
|
+
if v is None:
|
|
381
|
+
return (1, "")
|
|
382
|
+
if isinstance(v, (int, float)):
|
|
383
|
+
return (0, float(v))
|
|
384
|
+
return (0, str(v).lower())
|
|
385
|
+
|
|
386
|
+
indices.sort(key=_key, reverse=not self._sort_asc)
|
|
387
|
+
|
|
388
|
+
self._view_indices = indices
|
|
389
|
+
self._scroll_y = max(0.0, min(self._scroll_y, self.max_scroll))
|
|
390
|
+
self._recompute_totals()
|
|
391
|
+
|
|
392
|
+
def _recompute_totals(self) -> None:
|
|
393
|
+
if not self.show_totals:
|
|
394
|
+
self._totals_cache = [""] * len(self.columns)
|
|
395
|
+
return
|
|
396
|
+
out: list[str] = []
|
|
397
|
+
n = len(self._view_indices)
|
|
398
|
+
for ci, col in enumerate(self.columns):
|
|
399
|
+
op = col.aggregate
|
|
400
|
+
if op is None:
|
|
401
|
+
out.append(f"{n:,} filas" if ci == 0 else "")
|
|
402
|
+
continue
|
|
403
|
+
if op == "count":
|
|
404
|
+
out.append(f"N={n:,}")
|
|
405
|
+
continue
|
|
406
|
+
nums: list[float] = []
|
|
407
|
+
for ri in self._view_indices:
|
|
408
|
+
row = self._rows[ri]
|
|
409
|
+
if ci < len(row) and row[ci] is not None:
|
|
410
|
+
try:
|
|
411
|
+
nums.append(float(row[ci]))
|
|
412
|
+
except (TypeError, ValueError):
|
|
413
|
+
pass
|
|
414
|
+
if not nums:
|
|
415
|
+
out.append("—")
|
|
416
|
+
continue
|
|
417
|
+
if op == "sum":
|
|
418
|
+
val = sum(nums)
|
|
419
|
+
elif op == "avg":
|
|
420
|
+
val = sum(nums) / len(nums)
|
|
421
|
+
elif op == "min":
|
|
422
|
+
val = min(nums)
|
|
423
|
+
else:
|
|
424
|
+
val = max(nums)
|
|
425
|
+
out.append(col.format_value(val))
|
|
426
|
+
self._totals_cache = out
|
|
427
|
+
|
|
428
|
+
# --- CSV Export ---
|
|
429
|
+
|
|
430
|
+
def to_csv(self) -> str:
|
|
431
|
+
"""Export current filtered & sorted rows to a CSV string."""
|
|
432
|
+
buf = io.StringIO()
|
|
433
|
+
writer = csv.writer(buf)
|
|
434
|
+
writer.writerow([c.title for c in self.columns])
|
|
435
|
+
for ri in self._view_indices:
|
|
436
|
+
writer.writerow(self._rows[ri])
|
|
437
|
+
return buf.getvalue()
|
|
438
|
+
|
|
439
|
+
def export_csv(self, path: str | Path) -> Path:
|
|
440
|
+
"""Write current filtered & sorted rows to a UTF-8 CSV file."""
|
|
441
|
+
out = Path(path)
|
|
442
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
443
|
+
out.write_text(self.to_csv(), encoding="utf-8-sig")
|
|
444
|
+
return out
|
|
445
|
+
|
|
446
|
+
def export_xlsx(self, path: str | Path, *, sheet_name: str = "Datos") -> Path:
|
|
447
|
+
"""Write current filtered & sorted rows + totals to a native Excel `.xlsx` file (zero deps)."""
|
|
448
|
+
from ..core.xlsx_export import export_xlsx as _export_xlsx
|
|
449
|
+
|
|
450
|
+
headers = [c.title for c in self.columns]
|
|
451
|
+
rows = [self._rows[ri] for ri in self._view_indices]
|
|
452
|
+
totals = self.totals if self.show_totals else None
|
|
453
|
+
return _export_xlsx(path, headers, rows, sheet_name=sheet_name, totals=totals)
|
|
454
|
+
|
|
455
|
+
# --- Geometry Helpers ---
|
|
456
|
+
|
|
457
|
+
def _toolbar_rect(self) -> Rect:
|
|
458
|
+
h = self.TOOLBAR_H if self.searchable else 0.0
|
|
459
|
+
return Rect(self.rect.x, self.rect.y, self.rect.width, h)
|
|
460
|
+
|
|
461
|
+
def _header_rect(self) -> Rect:
|
|
462
|
+
y = self.rect.y + (self.TOOLBAR_H if self.searchable else 0.0)
|
|
463
|
+
return Rect(self.rect.x, y, self.rect.width, self.HEADER_H)
|
|
464
|
+
|
|
465
|
+
def _pager_rect(self) -> Rect:
|
|
466
|
+
h = self.PAGER_H if self.page_size is not None else 0.0
|
|
467
|
+
return Rect(self.rect.x, self.rect.bottom - h, self.rect.width, h)
|
|
468
|
+
|
|
469
|
+
def _footer_rect(self) -> Rect:
|
|
470
|
+
h = self.FOOTER_H if self.show_totals else 0.0
|
|
471
|
+
pager_h = self.PAGER_H if self.page_size is not None else 0.0
|
|
472
|
+
return Rect(self.rect.x, self.rect.bottom - pager_h - h, self.rect.width, h)
|
|
473
|
+
|
|
474
|
+
def _body_rect(self) -> Rect:
|
|
475
|
+
top = self._header_rect().bottom
|
|
476
|
+
bottom = self._footer_rect().y
|
|
477
|
+
return Rect(self.rect.x, top, self.rect.width, max(0.0, bottom - top))
|
|
478
|
+
|
|
479
|
+
@property
|
|
480
|
+
def max_scroll(self) -> float:
|
|
481
|
+
total_h = len(self._active_page_indices()) * self.row_height
|
|
482
|
+
return max(0.0, total_h - self._body_rect().height)
|
|
483
|
+
|
|
484
|
+
def _col_widths(self) -> list[float]:
|
|
485
|
+
avail = max(40.0, self.rect.width - self.SCROLLBAR_W)
|
|
486
|
+
n = max(1, len(self.columns))
|
|
487
|
+
fixed = sum(c.width for c in self.columns if c.width is not None)
|
|
488
|
+
flex_n = sum(1 for c in self.columns if c.width is None)
|
|
489
|
+
flex_w = max(60.0, (avail - fixed) / flex_n) if flex_n > 0 else 0.0
|
|
490
|
+
return [c.width if c.width is not None else flex_w for c in self.columns]
|
|
491
|
+
|
|
492
|
+
def _col_at(self, x: float) -> int:
|
|
493
|
+
cx = self.rect.x
|
|
494
|
+
for i, w in enumerate(self._col_widths()):
|
|
495
|
+
if cx <= x < cx + w:
|
|
496
|
+
return i
|
|
497
|
+
cx += w
|
|
498
|
+
return -1
|
|
499
|
+
|
|
500
|
+
def _view_row_at(self, y: float) -> int:
|
|
501
|
+
body = self._body_rect()
|
|
502
|
+
if not (body.y <= y < body.bottom):
|
|
503
|
+
return -1
|
|
504
|
+
idx = int((y - body.y + self._scroll_y) // self.row_height)
|
|
505
|
+
return idx if 0 <= idx < len(self._active_page_indices()) else -1
|
|
506
|
+
|
|
507
|
+
def _thumb_rect(self) -> Rect | None:
|
|
508
|
+
body = self._body_rect()
|
|
509
|
+
total_h = len(self._active_page_indices()) * self.row_height
|
|
510
|
+
if total_h <= body.height or body.height < 24.0:
|
|
511
|
+
return None
|
|
512
|
+
track_h = body.height - 8.0
|
|
513
|
+
thumb_h = max(28.0, track_h * (body.height / total_h))
|
|
514
|
+
frac = self._scroll_y / max(1e-4, self.max_scroll)
|
|
515
|
+
ty = body.y + 4.0 + frac * (track_h - thumb_h)
|
|
516
|
+
return Rect(self.rect.right - self.SCROLLBAR_W + 3.0, ty, 6.0, thumb_h)
|
|
517
|
+
|
|
518
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
519
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 640.0
|
|
520
|
+
return Size(w, self.grid_height)
|
|
521
|
+
|
|
522
|
+
# --- Inline Cell Editing ---
|
|
523
|
+
|
|
524
|
+
def start_edit(self, view_row: int, col_idx: int) -> bool:
|
|
525
|
+
if not self.editable:
|
|
526
|
+
return False
|
|
527
|
+
page_idx = self._active_page_indices()
|
|
528
|
+
if not (0 <= view_row < len(page_idx) and 0 <= col_idx < len(self.columns)):
|
|
529
|
+
return False
|
|
530
|
+
if not self.columns[col_idx].editable:
|
|
531
|
+
return False
|
|
532
|
+
real_row = page_idx[view_row]
|
|
533
|
+
cur = self._rows[real_row][col_idx] if col_idx < len(self._rows[real_row]) else ""
|
|
534
|
+
self._editing_cell = (view_row, col_idx)
|
|
535
|
+
self._edit_buffer = "" if cur is None else str(cur)
|
|
536
|
+
self._search_focused = False
|
|
537
|
+
self.invalidate()
|
|
538
|
+
return True
|
|
539
|
+
|
|
540
|
+
def commit_edit(self) -> None:
|
|
541
|
+
if self._editing_cell is None:
|
|
542
|
+
return
|
|
543
|
+
vr, ci = self._editing_cell
|
|
544
|
+
self._editing_cell = None
|
|
545
|
+
page_idx = self._active_page_indices()
|
|
546
|
+
if not (0 <= vr < len(page_idx) and 0 <= ci < len(self.columns)):
|
|
547
|
+
return
|
|
548
|
+
real_row = page_idx[vr]
|
|
549
|
+
row = self._rows[real_row]
|
|
550
|
+
while len(row) <= ci:
|
|
551
|
+
row.append(None)
|
|
552
|
+
old_val = row[ci]
|
|
553
|
+
new_val = self.columns[ci].parse_edited(self._edit_buffer, old_val)
|
|
554
|
+
row[ci] = new_val
|
|
555
|
+
self._recompute_totals()
|
|
556
|
+
if new_val != old_val:
|
|
557
|
+
if self._source is not None:
|
|
558
|
+
field_name = self._field_names[ci] if ci < len(self._field_names) else self.columns[ci].title
|
|
559
|
+
offset = ((self._page - 1) * self.page_size) if self.page_size is not None else 0
|
|
560
|
+
self._source.update_cell(offset + real_row, field_name, new_val, row)
|
|
561
|
+
if self.on_cell_edit is not None:
|
|
562
|
+
self.on_cell_edit(real_row, ci, old_val, new_val)
|
|
563
|
+
self.invalidate()
|
|
564
|
+
|
|
565
|
+
def cancel_edit(self) -> None:
|
|
566
|
+
if self._editing_cell is not None:
|
|
567
|
+
self._editing_cell = None
|
|
568
|
+
self.invalidate()
|
|
569
|
+
|
|
570
|
+
# --- Input Events ---
|
|
571
|
+
|
|
572
|
+
def on_scroll(self, event: ScrollEvent) -> bool:
|
|
573
|
+
if self.max_scroll <= 0.0:
|
|
574
|
+
return False
|
|
575
|
+
prev = self._scroll_y
|
|
576
|
+
self.scroll_y = self._scroll_y - event.dy * self.row_height * 2.5
|
|
577
|
+
return abs(self._scroll_y - prev) > 1e-4
|
|
578
|
+
|
|
579
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
580
|
+
if self._dragging_thumb:
|
|
581
|
+
body = self._body_rect()
|
|
582
|
+
thumb = self._thumb_rect()
|
|
583
|
+
if thumb is not None:
|
|
584
|
+
track_h = max(1.0, body.height - 8.0 - thumb.height)
|
|
585
|
+
frac = max(0.0, min(1.0, (event.y - self._drag_offset_y - (body.y + 4.0)) / track_h))
|
|
586
|
+
self.scroll_y = frac * self.max_scroll
|
|
587
|
+
return
|
|
588
|
+
|
|
589
|
+
hr = self._header_rect()
|
|
590
|
+
new_hh = self._col_at(event.x) if hr.contains(event.x, event.y) else -1
|
|
591
|
+
new_hr = self._view_row_at(event.y)
|
|
592
|
+
if new_hh != self._hover_header or new_hr != self._hover_view_row:
|
|
593
|
+
self._hover_header = new_hh
|
|
594
|
+
self._hover_view_row = new_hr
|
|
595
|
+
self.invalidate()
|
|
596
|
+
|
|
597
|
+
def on_mouse_leave(self) -> None:
|
|
598
|
+
super().on_mouse_leave()
|
|
599
|
+
self._hover_header = -1
|
|
600
|
+
self._hover_view_row = -1
|
|
601
|
+
|
|
602
|
+
def on_mouse_down(self, event: MouseEvent) -> None:
|
|
603
|
+
super().on_mouse_down(event)
|
|
604
|
+
# Check scrollbar thumb drag
|
|
605
|
+
thumb = self._thumb_rect()
|
|
606
|
+
if thumb is not None and event.x >= self.rect.right - self.SCROLLBAR_W - 2.0:
|
|
607
|
+
if thumb.y <= event.y <= thumb.bottom:
|
|
608
|
+
self._dragging_thumb = True
|
|
609
|
+
self._drag_offset_y = event.y - thumb.y
|
|
610
|
+
else:
|
|
611
|
+
body = self._body_rect()
|
|
612
|
+
frac = max(0.0, min(1.0, (event.y - body.y) / max(1.0, body.height)))
|
|
613
|
+
self.scroll_y = frac * self.max_scroll
|
|
614
|
+
return
|
|
615
|
+
|
|
616
|
+
# Check search toolbar click
|
|
617
|
+
tb = self._toolbar_rect()
|
|
618
|
+
if self.searchable and tb.contains(event.x, event.y):
|
|
619
|
+
self.commit_edit()
|
|
620
|
+
# Clear button on right side of search box
|
|
621
|
+
if self._filter_text and event.x >= tb.right - 36.0:
|
|
622
|
+
self.filter_text = ""
|
|
623
|
+
self._search_focused = True
|
|
624
|
+
self.invalidate()
|
|
625
|
+
return
|
|
626
|
+
|
|
627
|
+
self._search_focused = False
|
|
628
|
+
|
|
629
|
+
# Check header sort click
|
|
630
|
+
hr = self._header_rect()
|
|
631
|
+
if hr.contains(event.x, event.y):
|
|
632
|
+
self.commit_edit()
|
|
633
|
+
ci = self._col_at(event.x)
|
|
634
|
+
if ci != -1:
|
|
635
|
+
self.sort_by(ci)
|
|
636
|
+
return
|
|
637
|
+
|
|
638
|
+
# Check pager clicks
|
|
639
|
+
if self.page_size is not None:
|
|
640
|
+
if self._prev_btn_rect.contains(event.x, event.y):
|
|
641
|
+
self.commit_edit()
|
|
642
|
+
self.prev_page()
|
|
643
|
+
return
|
|
644
|
+
if self._next_btn_rect.contains(event.x, event.y):
|
|
645
|
+
self.commit_edit()
|
|
646
|
+
self.next_page()
|
|
647
|
+
return
|
|
648
|
+
|
|
649
|
+
# Body cell click / double-click
|
|
650
|
+
vr = self._view_row_at(event.y)
|
|
651
|
+
ci = self._col_at(event.x)
|
|
652
|
+
if vr != -1 and ci != -1:
|
|
653
|
+
now = time.perf_counter()
|
|
654
|
+
is_double = (
|
|
655
|
+
self._last_click_cell == (vr, ci)
|
|
656
|
+
and (now - self._last_click_time) <= 0.40
|
|
657
|
+
)
|
|
658
|
+
self._last_click_time = now
|
|
659
|
+
self._last_click_cell = (vr, ci)
|
|
660
|
+
|
|
661
|
+
if self._editing_cell is not None and self._editing_cell != (vr, ci):
|
|
662
|
+
self.commit_edit()
|
|
663
|
+
|
|
664
|
+
self._selected_view_row = vr
|
|
665
|
+
self._selected_col = ci
|
|
666
|
+
if is_double and self.editable:
|
|
667
|
+
self.start_edit(vr, ci)
|
|
668
|
+
elif self.on_row_click is not None:
|
|
669
|
+
page_idx = self._active_page_indices()
|
|
670
|
+
if 0 <= vr < len(page_idx):
|
|
671
|
+
real_row = page_idx[vr]
|
|
672
|
+
self.on_row_click(real_row, self._rows[real_row])
|
|
673
|
+
self.invalidate()
|
|
674
|
+
|
|
675
|
+
def on_mouse_up(self, event: MouseEvent) -> None:
|
|
676
|
+
super().on_mouse_up(event)
|
|
677
|
+
self._dragging_thumb = False
|
|
678
|
+
|
|
679
|
+
def on_char(self, event: CharEvent) -> None:
|
|
680
|
+
if self._search_focused:
|
|
681
|
+
self.filter_text = self._filter_text + event.char
|
|
682
|
+
return
|
|
683
|
+
if self._editing_cell is not None:
|
|
684
|
+
self._edit_buffer += event.char
|
|
685
|
+
self.invalidate()
|
|
686
|
+
|
|
687
|
+
def on_key(self, event: KeyEvent) -> None:
|
|
688
|
+
if event.action not in (glfw.PRESS, glfw.REPEAT):
|
|
689
|
+
return
|
|
690
|
+
if self._search_focused:
|
|
691
|
+
if event.key == glfw.KEY_BACKSPACE and self._filter_text:
|
|
692
|
+
self.filter_text = self._filter_text[:-1]
|
|
693
|
+
elif event.key == glfw.KEY_ESCAPE:
|
|
694
|
+
self._search_focused = False
|
|
695
|
+
self.invalidate()
|
|
696
|
+
return
|
|
697
|
+
|
|
698
|
+
if self._editing_cell is not None:
|
|
699
|
+
if event.key == glfw.KEY_BACKSPACE:
|
|
700
|
+
self._edit_buffer = self._edit_buffer[:-1]
|
|
701
|
+
self.invalidate()
|
|
702
|
+
elif event.key in (glfw.KEY_ENTER, glfw.KEY_KP_ENTER):
|
|
703
|
+
self.commit_edit()
|
|
704
|
+
elif event.key == glfw.KEY_ESCAPE:
|
|
705
|
+
self.cancel_edit()
|
|
706
|
+
return
|
|
707
|
+
|
|
708
|
+
# Row keyboard navigation & F2 edit
|
|
709
|
+
n = len(self._view_indices)
|
|
710
|
+
if n == 0:
|
|
711
|
+
return
|
|
712
|
+
if event.key == glfw.KEY_DOWN:
|
|
713
|
+
self._selected_view_row = 0 if self._selected_view_row is None else min(n - 1, self._selected_view_row + 1)
|
|
714
|
+
self._ensure_row_visible(self._selected_view_row)
|
|
715
|
+
self.invalidate()
|
|
716
|
+
elif event.key == glfw.KEY_UP:
|
|
717
|
+
self._selected_view_row = 0 if self._selected_view_row is None else max(0, self._selected_view_row - 1)
|
|
718
|
+
self._ensure_row_visible(self._selected_view_row)
|
|
719
|
+
self.invalidate()
|
|
720
|
+
elif event.key == glfw.KEY_F2 and self._selected_view_row is not None and self.editable:
|
|
721
|
+
self.start_edit(self._selected_view_row, self._selected_col)
|
|
722
|
+
|
|
723
|
+
def _ensure_row_visible(self, vr: int) -> None:
|
|
724
|
+
body_h = self._body_rect().height
|
|
725
|
+
top = vr * self.row_height
|
|
726
|
+
bottom = top + self.row_height
|
|
727
|
+
if top < self._scroll_y:
|
|
728
|
+
self.scroll_y = top
|
|
729
|
+
elif bottom > self._scroll_y + body_h:
|
|
730
|
+
self.scroll_y = bottom - body_h
|
|
731
|
+
|
|
732
|
+
# --- O(1) Virtualized Skia Rendering ---
|
|
733
|
+
|
|
734
|
+
def _badge_colors(self, tone: BadgeTone, theme: Theme) -> tuple[Color, Color]:
|
|
735
|
+
c = theme.colors
|
|
736
|
+
if tone == "primary":
|
|
737
|
+
return (c.primary_container, c.on_primary_container)
|
|
738
|
+
if tone == "secondary":
|
|
739
|
+
return (c.secondary_container, c.on_secondary_container)
|
|
740
|
+
if tone == "tertiary":
|
|
741
|
+
return (c.tertiary_container, c.on_tertiary_container)
|
|
742
|
+
if tone == "error":
|
|
743
|
+
return (c.error_container, c.on_error_container)
|
|
744
|
+
return (c.surface_container_highest, c.on_surface_variant)
|
|
745
|
+
|
|
746
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
747
|
+
c = theme.colors
|
|
748
|
+
rad_spec = self._shape_override if self._shape_override is not None else "medium"
|
|
749
|
+
rad = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
|
|
750
|
+
if self._elevation_override:
|
|
751
|
+
sh = resolve_color(self._shadow_color_override, theme, c.shadow)
|
|
752
|
+
assert sh is not None
|
|
753
|
+
draw_elevation_shadow(canvas, self.rect, rad, self._elevation_override, sh)
|
|
754
|
+
|
|
755
|
+
rrect = make_rrect_spec(self.rect, rad_spec, theme)
|
|
756
|
+
bg = resolve_color(self._bg_override, theme, c.surface) or c.surface
|
|
757
|
+
|
|
758
|
+
canvas.save()
|
|
759
|
+
canvas.clipRRect(rrect, doAntiAlias=True)
|
|
760
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
761
|
+
|
|
762
|
+
widths = self._col_widths()
|
|
763
|
+
hfont = font_for_style(theme.font_family, theme.typography.label_large)
|
|
764
|
+
bfont = font_for_style(theme.font_family, theme.typography.body_medium)
|
|
765
|
+
sfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
766
|
+
div_paint = skia.Paint(AntiAlias=True, Color=c.outline_variant.to_skia(), StrokeWidth=1.0)
|
|
767
|
+
|
|
768
|
+
# 1. Optional live filter toolbar
|
|
769
|
+
if self.searchable:
|
|
770
|
+
tb = self._toolbar_rect()
|
|
771
|
+
canvas.drawRect(
|
|
772
|
+
skia.Rect.MakeXYWH(tb.x, tb.y, tb.width, tb.height),
|
|
773
|
+
skia.Paint(AntiAlias=True, Color=c.surface_container_low.to_skia()),
|
|
774
|
+
)
|
|
775
|
+
draw_icon(canvas, "search", tb.x + 22.0, tb.center_y, 16.0, c.primary if self._search_focused else c.on_surface_variant)
|
|
776
|
+
if self._filter_text:
|
|
777
|
+
draw_text_centered_y(canvas, self._filter_text, tb.x + 40.0, tb.center_y, bfont, c.on_surface)
|
|
778
|
+
draw_icon(canvas, "close", tb.right - 22.0, tb.center_y, 15.0, c.on_surface_variant)
|
|
779
|
+
else:
|
|
780
|
+
draw_text_centered_y(
|
|
781
|
+
canvas,
|
|
782
|
+
f"Filtrar {len(self._rows):,} registros...",
|
|
783
|
+
tb.x + 40.0,
|
|
784
|
+
tb.center_y,
|
|
785
|
+
bfont,
|
|
786
|
+
c.on_surface_variant,
|
|
787
|
+
)
|
|
788
|
+
count_txt = f"{len(self._view_indices):,} / {len(self._rows):,}"
|
|
789
|
+
cw = measure_text(sfont, count_txt)
|
|
790
|
+
draw_text_centered_y(
|
|
791
|
+
canvas, count_txt, tb.right - cw - (42.0 if self._filter_text else 16.0), tb.center_y, sfont, c.on_surface_variant
|
|
792
|
+
)
|
|
793
|
+
canvas.drawLine(tb.x, tb.bottom, tb.right, tb.bottom, div_paint)
|
|
794
|
+
|
|
795
|
+
# 2. Sticky Header
|
|
796
|
+
hr = self._header_rect()
|
|
797
|
+
hdr_bg = resolve_color(self.header_bg, theme, c.surface_container) or c.surface_container
|
|
798
|
+
canvas.drawRect(skia.Rect.MakeXYWH(hr.x, hr.y, hr.width, hr.height), skia.Paint(AntiAlias=True, Color=hdr_bg.to_skia()))
|
|
799
|
+
cx = hr.x
|
|
800
|
+
for ci, col in enumerate(self.columns):
|
|
801
|
+
w = widths[ci]
|
|
802
|
+
if ci == self._hover_header and col.sortable:
|
|
803
|
+
canvas.drawRect(
|
|
804
|
+
skia.Rect.MakeXYWH(cx, hr.y, w, hr.height),
|
|
805
|
+
skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
|
|
806
|
+
)
|
|
807
|
+
label = col.title
|
|
808
|
+
if self._sort_col == ci:
|
|
809
|
+
label += " ▲" if self._sort_asc else " ▼"
|
|
810
|
+
tw = measure_text(hfont, label)
|
|
811
|
+
align = col.effective_align
|
|
812
|
+
tx = cx + 12.0 if align == "left" else (cx + (w - tw) * 0.5 if align == "center" else cx + w - tw - 12.0)
|
|
813
|
+
draw_text_centered_y(canvas, label, tx, hr.center_y, hfont, c.on_surface)
|
|
814
|
+
cx += w
|
|
815
|
+
canvas.drawLine(hr.x, hr.bottom, hr.right, hr.bottom, div_paint)
|
|
816
|
+
|
|
817
|
+
# 3. Virtualized (or full) Body
|
|
818
|
+
body = self._body_rect()
|
|
819
|
+
canvas.save()
|
|
820
|
+
canvas.clipRect(skia.Rect.MakeXYWH(body.x, body.y, body.width, body.height))
|
|
821
|
+
|
|
822
|
+
page_indices = self._active_page_indices()
|
|
823
|
+
total_view = len(page_indices)
|
|
824
|
+
if self.virtualized:
|
|
825
|
+
first_row = max(0, int(self._scroll_y // self.row_height))
|
|
826
|
+
visible_count = int(body.height // self.row_height) + 2
|
|
827
|
+
last_row = min(total_view, first_row + visible_count)
|
|
828
|
+
else:
|
|
829
|
+
first_row = 0
|
|
830
|
+
last_row = total_view
|
|
831
|
+
|
|
832
|
+
for vr in range(first_row, last_row):
|
|
833
|
+
ry = body.y + vr * self.row_height - self._scroll_y
|
|
834
|
+
real_idx = page_indices[vr]
|
|
835
|
+
row = self._rows[real_idx]
|
|
836
|
+
|
|
837
|
+
if vr == self._selected_view_row:
|
|
838
|
+
rbg = c.secondary_container.with_alpha(0.55)
|
|
839
|
+
elif vr == self._hover_view_row:
|
|
840
|
+
rbg = c.on_surface.with_alpha(theme.state_layers.hover)
|
|
841
|
+
elif self.striped and vr % 2 == 1:
|
|
842
|
+
rbg = c.surface_container_low
|
|
843
|
+
else:
|
|
844
|
+
rbg = None
|
|
845
|
+
if rbg is not None:
|
|
846
|
+
canvas.drawRect(
|
|
847
|
+
skia.Rect.MakeXYWH(body.x, ry, body.width - self.SCROLLBAR_W, self.row_height),
|
|
848
|
+
skia.Paint(AntiAlias=True, Color=rbg.to_skia()),
|
|
849
|
+
)
|
|
850
|
+
|
|
851
|
+
cx = body.x
|
|
852
|
+
for ci, col in enumerate(self.columns):
|
|
853
|
+
w = widths[ci]
|
|
854
|
+
val = row[ci] if ci < len(row) else None
|
|
855
|
+
|
|
856
|
+
if self._editing_cell == (vr, ci):
|
|
857
|
+
er = Rect(cx + 4.0, ry + 4.0, max(10.0, w - 8.0), self.row_height - 8.0)
|
|
858
|
+
err = make_rrect(er, 6.0)
|
|
859
|
+
canvas.drawRRect(err, skia.Paint(AntiAlias=True, Color=c.surface.to_skia()))
|
|
860
|
+
canvas.drawRRect(
|
|
861
|
+
err,
|
|
862
|
+
skia.Paint(AntiAlias=True, Color=c.primary.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.0),
|
|
863
|
+
)
|
|
864
|
+
draw_text_centered_y(canvas, self._edit_buffer + "|", er.x + 8.0, er.center_y, bfont, c.on_surface)
|
|
865
|
+
elif col.type == "badge":
|
|
866
|
+
txt = str(val) if val is not None else ""
|
|
867
|
+
tone = col.badge_colors.get(txt, "neutral")
|
|
868
|
+
bbg, bfg = self._badge_colors(tone, theme)
|
|
869
|
+
tw = measure_text(sfont, txt)
|
|
870
|
+
pw = tw + 16.0
|
|
871
|
+
ph = 22.0
|
|
872
|
+
px = cx + (w - pw) * 0.5
|
|
873
|
+
py = ry + (self.row_height - ph) * 0.5
|
|
874
|
+
canvas.drawRRect(make_rrect(Rect(px, py, pw, ph), 11.0), skia.Paint(AntiAlias=True, Color=bbg.to_skia()))
|
|
875
|
+
draw_text_centered_y(canvas, txt, px + 8.0, py + ph * 0.5, sfont, bfg)
|
|
876
|
+
else:
|
|
877
|
+
txt = col.format_value(val)
|
|
878
|
+
tw = measure_text(bfont, txt)
|
|
879
|
+
align = col.effective_align
|
|
880
|
+
tx = cx + 12.0 if align == "left" else (cx + (w - tw) * 0.5 if align == "center" else cx + w - tw - 12.0)
|
|
881
|
+
canvas.save()
|
|
882
|
+
canvas.clipRect(skia.Rect.MakeXYWH(cx + 4.0, ry, max(0.0, w - 8.0), self.row_height))
|
|
883
|
+
draw_text_centered_y(canvas, txt, tx, ry + self.row_height * 0.5, bfont, c.on_surface)
|
|
884
|
+
canvas.restore()
|
|
885
|
+
cx += w
|
|
886
|
+
|
|
887
|
+
canvas.drawLine(body.x, ry + self.row_height, body.right - self.SCROLLBAR_W, ry + self.row_height, div_paint)
|
|
888
|
+
|
|
889
|
+
# Scrollbar track & thumb
|
|
890
|
+
thumb = self._thumb_rect()
|
|
891
|
+
if thumb is not None:
|
|
892
|
+
canvas.drawRect(
|
|
893
|
+
skia.Rect.MakeXYWH(self.rect.right - self.SCROLLBAR_W, body.y, self.SCROLLBAR_W, body.height),
|
|
894
|
+
skia.Paint(AntiAlias=True, Color=c.surface_container_low.to_skia()),
|
|
895
|
+
)
|
|
896
|
+
alpha = 0.65 if self._dragging_thumb else 0.40
|
|
897
|
+
canvas.drawRRect(
|
|
898
|
+
make_rrect(thumb, 3.0),
|
|
899
|
+
skia.Paint(AntiAlias=True, Color=c.on_surface_variant.with_alpha(alpha).to_skia()),
|
|
900
|
+
)
|
|
901
|
+
|
|
902
|
+
canvas.restore()
|
|
903
|
+
|
|
904
|
+
# 4. Sticky Aggregate Totals Footer
|
|
905
|
+
if self.show_totals:
|
|
906
|
+
fr = self._footer_rect()
|
|
907
|
+
canvas.drawRect(
|
|
908
|
+
skia.Rect.MakeXYWH(fr.x, fr.y, fr.width, fr.height),
|
|
909
|
+
skia.Paint(AntiAlias=True, Color=c.surface_container_high.to_skia()),
|
|
910
|
+
)
|
|
911
|
+
canvas.drawLine(fr.x, fr.y, fr.right, fr.y, div_paint)
|
|
912
|
+
cx = fr.x
|
|
913
|
+
for ci, col in enumerate(self.columns):
|
|
914
|
+
w = widths[ci]
|
|
915
|
+
txt = self._totals_cache[ci] if ci < len(self._totals_cache) else ""
|
|
916
|
+
if txt:
|
|
917
|
+
tw = measure_text(hfont, txt)
|
|
918
|
+
align = col.effective_align
|
|
919
|
+
tx = cx + 12.0 if align == "left" else (cx + (w - tw) * 0.5 if align == "center" else cx + w - tw - 12.0)
|
|
920
|
+
draw_text_centered_y(canvas, txt, tx, fr.center_y, hfont, c.primary if col.aggregate else c.on_surface_variant)
|
|
921
|
+
cx += w
|
|
922
|
+
|
|
923
|
+
# 5. Optional Pagination Bar (`page_size=...`)
|
|
924
|
+
if self.page_size is not None:
|
|
925
|
+
pr = self._pager_rect()
|
|
926
|
+
canvas.drawRect(
|
|
927
|
+
skia.Rect.MakeXYWH(pr.x, pr.y, pr.width, pr.height),
|
|
928
|
+
skia.Paint(AntiAlias=True, Color=c.surface_container_low.to_skia()),
|
|
929
|
+
)
|
|
930
|
+
canvas.drawLine(pr.x, pr.y, pr.right, pr.y, div_paint)
|
|
931
|
+
total_all = self._source_total_count if self._source is not None else len(self._view_indices)
|
|
932
|
+
info = f"Pág. {self._page} / {self.total_pages} · {total_all:,} registros ({self.page_size}/pág)"
|
|
933
|
+
draw_text_centered_y(canvas, info, pr.x + 14.0, pr.center_y, sfont, c.on_surface_variant)
|
|
934
|
+
|
|
935
|
+
btn_w, btn_h = 28.0, 24.0
|
|
936
|
+
self._next_btn_rect = Rect(pr.right - btn_w - 10.0, pr.center_y - btn_h * 0.5, btn_w, btn_h)
|
|
937
|
+
self._prev_btn_rect = Rect(self._next_btn_rect.x - btn_w - 6.0, pr.center_y - btn_h * 0.5, btn_w, btn_h)
|
|
938
|
+
for brect, sym, enabled in (
|
|
939
|
+
(self._prev_btn_rect, "◀", self._page > 1),
|
|
940
|
+
(self._next_btn_rect, "▶", self._page < self.total_pages),
|
|
941
|
+
):
|
|
942
|
+
b_bg = c.surface_container_highest if enabled else c.surface_container
|
|
943
|
+
b_fg = c.on_surface if enabled else c.on_surface_variant.with_alpha(0.38)
|
|
944
|
+
canvas.drawRRect(make_rrect(brect, 6.0), skia.Paint(AntiAlias=True, Color=b_bg.to_skia()))
|
|
945
|
+
sw = measure_text(sfont, sym)
|
|
946
|
+
draw_text_centered_y(canvas, sym, brect.center_x - sw * 0.5, brect.center_y, sfont, b_fg)
|
|
947
|
+
|
|
948
|
+
canvas.restore()
|
|
949
|
+
|
|
950
|
+
# Outer border
|
|
951
|
+
bc = resolve_color(self._border_color_override, theme, c.outline_variant) or c.outline_variant
|
|
952
|
+
bw = self._border_width_override if self._border_width_override is not None else 1.0
|
|
953
|
+
canvas.drawRRect(
|
|
954
|
+
rrect,
|
|
955
|
+
skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
|
|
956
|
+
)
|