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,551 @@
|
|
|
1
|
+
"""Material Design 3 DataTable with rich typed formatting and image/avatar cells."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from datetime import date, datetime, time as dt_time
|
|
7
|
+
from functools import lru_cache
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any, Callable, Literal, Mapping, Sequence
|
|
10
|
+
|
|
11
|
+
import skia
|
|
12
|
+
|
|
13
|
+
from ..core.data_source import DataSource, coerce_data_source
|
|
14
|
+
from ..core.draw import (
|
|
15
|
+
ColorSpec,
|
|
16
|
+
draw_elevation_shadow,
|
|
17
|
+
draw_icon,
|
|
18
|
+
draw_text_centered_y,
|
|
19
|
+
font_for_style,
|
|
20
|
+
make_rrect,
|
|
21
|
+
make_rrect_spec,
|
|
22
|
+
measure_text,
|
|
23
|
+
resolve_color,
|
|
24
|
+
)
|
|
25
|
+
from ..core.events import MouseEvent
|
|
26
|
+
from ..core.geometry import Constraints, Rect, Size
|
|
27
|
+
from ..core.widget import Widget
|
|
28
|
+
from ..theme import Color, Theme
|
|
29
|
+
|
|
30
|
+
ColumnType = Literal[
|
|
31
|
+
"text",
|
|
32
|
+
"number",
|
|
33
|
+
"currency",
|
|
34
|
+
"percent",
|
|
35
|
+
"boolean",
|
|
36
|
+
"date",
|
|
37
|
+
"time",
|
|
38
|
+
"badge",
|
|
39
|
+
"progress",
|
|
40
|
+
"image",
|
|
41
|
+
"avatar",
|
|
42
|
+
"rating",
|
|
43
|
+
]
|
|
44
|
+
ColumnAlign = Literal["left", "center", "right"]
|
|
45
|
+
BadgeTone = Literal["primary", "secondary", "tertiary", "error", "neutral"]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass
|
|
49
|
+
class TableColumn:
|
|
50
|
+
"""Column definition for Material Design 3 `DataTable`."""
|
|
51
|
+
|
|
52
|
+
title: str
|
|
53
|
+
type: ColumnType = "text"
|
|
54
|
+
align: ColumnAlign | None = None
|
|
55
|
+
width: float | None = None
|
|
56
|
+
sortable: bool = True
|
|
57
|
+
prefix: str = ""
|
|
58
|
+
suffix: str = ""
|
|
59
|
+
decimals: int = 2
|
|
60
|
+
date_format: str = "%d/%m/%Y"
|
|
61
|
+
time_format: str = "%H:%M"
|
|
62
|
+
badge_colors: dict[str, BadgeTone] = field(default_factory=dict)
|
|
63
|
+
formatter: Callable[[Any], str] | None = None
|
|
64
|
+
|
|
65
|
+
def resolved_align(self) -> ColumnAlign:
|
|
66
|
+
if self.align is not None:
|
|
67
|
+
return self.align
|
|
68
|
+
if self.type in ("number", "currency", "percent"):
|
|
69
|
+
return "right"
|
|
70
|
+
if self.type in ("boolean", "image", "avatar", "badge", "rating"):
|
|
71
|
+
return "center"
|
|
72
|
+
return "left"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@lru_cache(maxsize=128)
|
|
76
|
+
def _load_image_from_path(path_str: str) -> skia.Image | None:
|
|
77
|
+
p = Path(path_str)
|
|
78
|
+
if not p.is_file():
|
|
79
|
+
return None
|
|
80
|
+
data = skia.Data.MakeFromFileName(str(p))
|
|
81
|
+
if data is None:
|
|
82
|
+
return None
|
|
83
|
+
return skia.Image.MakeFromEncoded(data)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _resolve_skia_image(value: Any) -> skia.Image | None:
|
|
87
|
+
if isinstance(value, skia.Image):
|
|
88
|
+
return value
|
|
89
|
+
if isinstance(value, (bytes, bytearray)):
|
|
90
|
+
return skia.Image.MakeFromEncoded(skia.Data.MakeWithCopy(bytes(value)))
|
|
91
|
+
if isinstance(value, (str, Path)):
|
|
92
|
+
return _load_image_from_path(str(value))
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _draw_image_cover(canvas: skia.Canvas, img: skia.Image, dest: Rect) -> None:
|
|
97
|
+
"""Draw `img` aspect-fill (object-fit: cover) inside `dest`."""
|
|
98
|
+
iw, ih = float(img.width()), float(img.height())
|
|
99
|
+
if iw <= 0 or ih <= 0 or dest.width <= 0 or dest.height <= 0:
|
|
100
|
+
return
|
|
101
|
+
src_ratio = iw / ih
|
|
102
|
+
dst_ratio = dest.width / dest.height
|
|
103
|
+
if src_ratio > dst_ratio:
|
|
104
|
+
crop_w = ih * dst_ratio
|
|
105
|
+
sx = (iw - crop_w) * 0.5
|
|
106
|
+
src = skia.Rect.MakeXYWH(sx, 0.0, crop_w, ih)
|
|
107
|
+
else:
|
|
108
|
+
crop_h = iw / dst_ratio
|
|
109
|
+
sy = (ih - crop_h) * 0.5
|
|
110
|
+
src = skia.Rect.MakeXYWH(0.0, sy, iw, crop_h)
|
|
111
|
+
dst = skia.Rect.MakeXYWH(dest.x, dest.y, dest.width, dest.height)
|
|
112
|
+
sampling = skia.SamplingOptions(skia.FilterMode.kLinear, skia.MipmapMode.kLinear)
|
|
113
|
+
canvas.drawImageRect(img, src, dst, sampling, skia.Paint(AntiAlias=True))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class DataTable(Widget):
|
|
117
|
+
"""Material Design 3 DataTable with typed formatting, sorting, selection, and images."""
|
|
118
|
+
|
|
119
|
+
interactive = True
|
|
120
|
+
manages_own_box = True
|
|
121
|
+
cursor = "pointer"
|
|
122
|
+
|
|
123
|
+
HEADER_H = 48.0
|
|
124
|
+
|
|
125
|
+
def __init__(
|
|
126
|
+
self,
|
|
127
|
+
columns: Sequence[str | TableColumn] | None = None,
|
|
128
|
+
rows: Sequence[Sequence[Any]] | None = None,
|
|
129
|
+
*,
|
|
130
|
+
source: Any = None,
|
|
131
|
+
column_overrides: Mapping[str, TableColumn] | None = None,
|
|
132
|
+
sortable: bool = True,
|
|
133
|
+
selectable: bool = True,
|
|
134
|
+
striped: bool = False,
|
|
135
|
+
show_dividers: bool = True,
|
|
136
|
+
header_bg: ColorSpec | None = None,
|
|
137
|
+
row_height: float | None = None,
|
|
138
|
+
on_row_click: Callable[[int, list[Any]], None] | None = None,
|
|
139
|
+
) -> None:
|
|
140
|
+
super().__init__()
|
|
141
|
+
self._source: DataSource | None = coerce_data_source(source) if source is not None else None
|
|
142
|
+
self._column_overrides: dict[str, TableColumn] = dict(column_overrides) if column_overrides else {}
|
|
143
|
+
if self._source is not None:
|
|
144
|
+
if columns is not None:
|
|
145
|
+
self.columns = [c if isinstance(c, TableColumn) else TableColumn(str(c)) for c in columns]
|
|
146
|
+
else:
|
|
147
|
+
types = self._source.column_types
|
|
148
|
+
self.columns = [
|
|
149
|
+
self._column_overrides[name]
|
|
150
|
+
if name in self._column_overrides
|
|
151
|
+
else TableColumn(
|
|
152
|
+
name.replace("_", " ").strip().title() or name,
|
|
153
|
+
type=types.get(name, "text") if types.get(name, "text") in ("text", "int", "float", "bool") else "text", # type: ignore[arg-type]
|
|
154
|
+
)
|
|
155
|
+
for name in self._source.columns
|
|
156
|
+
]
|
|
157
|
+
self._rows = self._source.fetch()
|
|
158
|
+
self._source.subscribe(self.reload)
|
|
159
|
+
else:
|
|
160
|
+
self.columns = [
|
|
161
|
+
c if isinstance(c, TableColumn) else TableColumn(str(c))
|
|
162
|
+
for c in (columns or ())
|
|
163
|
+
]
|
|
164
|
+
self._rows = [list(r) for r in (rows or ())]
|
|
165
|
+
self.sortable = sortable
|
|
166
|
+
self.selectable = selectable
|
|
167
|
+
self.striped = striped
|
|
168
|
+
self.show_dividers = show_dividers
|
|
169
|
+
self.header_bg = header_bg
|
|
170
|
+
has_media = any(c.type in ("image", "avatar") for c in self.columns)
|
|
171
|
+
self.row_height: float = float(row_height) if row_height is not None else (54.0 if has_media else 46.0)
|
|
172
|
+
self.on_row_click = on_row_click
|
|
173
|
+
|
|
174
|
+
self._sort_col: int | None = None
|
|
175
|
+
self._sort_asc: bool = True
|
|
176
|
+
self._hover_row: int = -1 # -2 = header, 0..n-1 = data row
|
|
177
|
+
self._selected_row: int | None = None
|
|
178
|
+
self._fill_width = True
|
|
179
|
+
|
|
180
|
+
def reload(self) -> None:
|
|
181
|
+
"""Reload rows from the attached `DataSource`."""
|
|
182
|
+
if self._source is None:
|
|
183
|
+
return
|
|
184
|
+
self._rows = self._source.fetch()
|
|
185
|
+
self.invalidate()
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def rows(self) -> list[list[Any]]:
|
|
189
|
+
return self._rows
|
|
190
|
+
|
|
191
|
+
@rows.setter
|
|
192
|
+
def rows(self, new_rows: Sequence[Sequence[Any]]) -> None:
|
|
193
|
+
self._rows = [list(r) for r in new_rows]
|
|
194
|
+
self._selected_row = None
|
|
195
|
+
self.invalidate()
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def selected_row(self) -> int | None:
|
|
199
|
+
return self._selected_row
|
|
200
|
+
|
|
201
|
+
def _compute_col_widths(self, total_w: float) -> list[float]:
|
|
202
|
+
if not self.columns:
|
|
203
|
+
return []
|
|
204
|
+
fixed = sum(c.width for c in self.columns if c.width is not None)
|
|
205
|
+
flex_count = sum(1 for c in self.columns if c.width is None)
|
|
206
|
+
flex_w = max(60.0, (total_w - fixed) / flex_count) if flex_count > 0 else 0.0
|
|
207
|
+
return [float(c.width) if c.width is not None else flex_w for c in self.columns]
|
|
208
|
+
|
|
209
|
+
def sort_by(self, col_idx: int) -> None:
|
|
210
|
+
if not (0 <= col_idx < len(self.columns)):
|
|
211
|
+
return
|
|
212
|
+
col = self.columns[col_idx]
|
|
213
|
+
if not col.sortable or col.type in ("image", "avatar"):
|
|
214
|
+
return
|
|
215
|
+
if self._sort_col == col_idx:
|
|
216
|
+
self._sort_asc = not self._sort_asc
|
|
217
|
+
else:
|
|
218
|
+
self._sort_col = col_idx
|
|
219
|
+
self._sort_asc = True
|
|
220
|
+
|
|
221
|
+
def sort_key(row: list[Any]):
|
|
222
|
+
val = row[col_idx] if col_idx < len(row) else None
|
|
223
|
+
if val is None:
|
|
224
|
+
return (2, 0.0, "")
|
|
225
|
+
if isinstance(val, bool):
|
|
226
|
+
return (0, 1.0 if val else 0.0, "")
|
|
227
|
+
if isinstance(val, (int, float)):
|
|
228
|
+
return (0, float(val), "")
|
|
229
|
+
if isinstance(val, (date, datetime, dt_time)):
|
|
230
|
+
return (1, 0.0, val.isoformat())
|
|
231
|
+
return (1, 0.0, str(val).lower())
|
|
232
|
+
|
|
233
|
+
self._rows.sort(key=sort_key, reverse=not self._sort_asc)
|
|
234
|
+
self.invalidate()
|
|
235
|
+
|
|
236
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
237
|
+
default_w = sum(c.width if c.width is not None else 130.0 for c in self.columns)
|
|
238
|
+
w = constraints.max_width if constraints.max_width != float("inf") else max(360.0, default_w)
|
|
239
|
+
h = self.HEADER_H + max(1, len(self._rows)) * self.row_height
|
|
240
|
+
return Size(w, h)
|
|
241
|
+
|
|
242
|
+
def _row_at(self, y: float) -> int:
|
|
243
|
+
if y < self.rect.y + self.HEADER_H:
|
|
244
|
+
return -2
|
|
245
|
+
idx = int((y - (self.rect.y + self.HEADER_H)) // self.row_height)
|
|
246
|
+
return idx if 0 <= idx < len(self._rows) else -1
|
|
247
|
+
|
|
248
|
+
def _col_at(self, x: float) -> int:
|
|
249
|
+
widths = self._compute_col_widths(self.rect.width)
|
|
250
|
+
cur_x = self.rect.x
|
|
251
|
+
for i, cw in enumerate(widths):
|
|
252
|
+
if cur_x <= x <= cur_x + cw:
|
|
253
|
+
return i
|
|
254
|
+
cur_x += cw
|
|
255
|
+
return -1
|
|
256
|
+
|
|
257
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
258
|
+
r = self._row_at(event.y)
|
|
259
|
+
if r != self._hover_row:
|
|
260
|
+
self._hover_row = r
|
|
261
|
+
self.invalidate()
|
|
262
|
+
|
|
263
|
+
def on_mouse_leave(self) -> None:
|
|
264
|
+
super().on_mouse_leave()
|
|
265
|
+
self._hover_row = -1
|
|
266
|
+
|
|
267
|
+
def on_mouse_up(self, event: MouseEvent) -> None:
|
|
268
|
+
was_pressed = self.pressed
|
|
269
|
+
super().on_mouse_up(event)
|
|
270
|
+
if not was_pressed or not self.rect.contains(event.x, event.y):
|
|
271
|
+
return
|
|
272
|
+
r = self._row_at(event.y)
|
|
273
|
+
if r == -2 and self.sortable:
|
|
274
|
+
col = self._col_at(event.x)
|
|
275
|
+
if col != -1:
|
|
276
|
+
self.sort_by(col)
|
|
277
|
+
elif 0 <= r < len(self._rows):
|
|
278
|
+
if self.selectable:
|
|
279
|
+
self._selected_row = r
|
|
280
|
+
self.invalidate()
|
|
281
|
+
if self.on_row_click is not None:
|
|
282
|
+
self.on_row_click(r, self._rows[r])
|
|
283
|
+
|
|
284
|
+
# --- Cell formatting & rendering ---
|
|
285
|
+
|
|
286
|
+
def format_value(self, col: TableColumn, value: Any) -> str:
|
|
287
|
+
if col.formatter is not None:
|
|
288
|
+
return str(col.formatter(value))
|
|
289
|
+
if value is None:
|
|
290
|
+
return "—"
|
|
291
|
+
if col.type == "currency":
|
|
292
|
+
prefix = col.prefix or "$"
|
|
293
|
+
try:
|
|
294
|
+
num = float(value)
|
|
295
|
+
return f"{prefix}{num:,.{col.decimals}f}{col.suffix}"
|
|
296
|
+
except (TypeError, ValueError):
|
|
297
|
+
return str(value)
|
|
298
|
+
if col.type == "number":
|
|
299
|
+
try:
|
|
300
|
+
num = float(value)
|
|
301
|
+
if col.decimals == 0 or (isinstance(value, int) and not isinstance(value, bool)):
|
|
302
|
+
return f"{col.prefix}{int(round(num)):,}{col.suffix}"
|
|
303
|
+
return f"{col.prefix}{num:,.{col.decimals}f}{col.suffix}"
|
|
304
|
+
except (TypeError, ValueError):
|
|
305
|
+
return str(value)
|
|
306
|
+
if col.type == "percent":
|
|
307
|
+
try:
|
|
308
|
+
num = float(value)
|
|
309
|
+
pct = num * 100.0 if 0.0 <= num <= 1.0 else num
|
|
310
|
+
return f"{pct:.{min(col.decimals, 1)}f}%"
|
|
311
|
+
except (TypeError, ValueError):
|
|
312
|
+
return str(value)
|
|
313
|
+
if col.type == "date":
|
|
314
|
+
if isinstance(value, (date, datetime)):
|
|
315
|
+
return value.strftime(col.date_format)
|
|
316
|
+
return str(value)
|
|
317
|
+
if col.type == "time":
|
|
318
|
+
if isinstance(value, (dt_time, datetime)):
|
|
319
|
+
return value.strftime(col.time_format)
|
|
320
|
+
return str(value)
|
|
321
|
+
return f"{col.prefix}{value}{col.suffix}"
|
|
322
|
+
|
|
323
|
+
def _badge_palette(self, tone: BadgeTone, theme: Theme) -> tuple[Color, Color]:
|
|
324
|
+
c = theme.colors
|
|
325
|
+
if tone == "primary":
|
|
326
|
+
return (c.primary_container, c.on_primary_container)
|
|
327
|
+
if tone == "tertiary":
|
|
328
|
+
return (c.tertiary_container, c.on_tertiary_container)
|
|
329
|
+
if tone == "error":
|
|
330
|
+
return (c.error_container, c.on_error_container)
|
|
331
|
+
if tone == "neutral":
|
|
332
|
+
return (c.surface_container_highest, c.on_surface_variant)
|
|
333
|
+
return (c.secondary_container, c.on_secondary_container)
|
|
334
|
+
|
|
335
|
+
def _paint_cell(
|
|
336
|
+
self,
|
|
337
|
+
canvas: skia.Canvas,
|
|
338
|
+
col: TableColumn,
|
|
339
|
+
value: Any,
|
|
340
|
+
cell: Rect,
|
|
341
|
+
font: skia.Font,
|
|
342
|
+
fg: Color,
|
|
343
|
+
theme: Theme,
|
|
344
|
+
) -> None:
|
|
345
|
+
c = theme.colors
|
|
346
|
+
align = col.resolved_align()
|
|
347
|
+
pad_x = 14.0
|
|
348
|
+
|
|
349
|
+
if col.type == "boolean":
|
|
350
|
+
truthy = bool(value)
|
|
351
|
+
bg = c.primary_container if truthy else c.surface_container_highest
|
|
352
|
+
ic = c.on_primary_container if truthy else c.on_surface_variant
|
|
353
|
+
canvas.drawCircle(cell.center_x, cell.center_y, 12.0, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
354
|
+
draw_icon(canvas, "check" if truthy else "close", cell.center_x, cell.center_y, 13.0, ic)
|
|
355
|
+
return
|
|
356
|
+
|
|
357
|
+
if col.type == "badge":
|
|
358
|
+
txt = self.format_value(col, value)
|
|
359
|
+
tone: BadgeTone = col.badge_colors.get(txt, "secondary")
|
|
360
|
+
bg, bfg = self._badge_palette(tone, theme)
|
|
361
|
+
bfont = font_for_style(theme.font_family, theme.typography.label_medium)
|
|
362
|
+
tw = measure_text(bfont, txt)
|
|
363
|
+
pw = tw + 18.0
|
|
364
|
+
ph = 24.0
|
|
365
|
+
px = cell.center_x - pw * 0.5 if align == "center" else (
|
|
366
|
+
cell.right - pad_x - pw if align == "right" else cell.x + pad_x
|
|
367
|
+
)
|
|
368
|
+
brect = Rect(px, cell.center_y - ph * 0.5, pw, ph)
|
|
369
|
+
canvas.drawRRect(make_rrect(brect, ph * 0.5), skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
370
|
+
draw_text_centered_y(canvas, txt, brect.center_x - tw * 0.5, brect.center_y, bfont, bfg)
|
|
371
|
+
return
|
|
372
|
+
|
|
373
|
+
if col.type == "progress":
|
|
374
|
+
try:
|
|
375
|
+
num = float(value)
|
|
376
|
+
ratio = max(0.0, min(1.0, num / 100.0 if num > 1.0 else num))
|
|
377
|
+
except (TypeError, ValueError):
|
|
378
|
+
ratio = 0.0
|
|
379
|
+
lbl = f"{int(round(ratio * 100))}%"
|
|
380
|
+
lfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
381
|
+
lw = measure_text(lfont, lbl)
|
|
382
|
+
bar_w = max(36.0, cell.width - pad_x * 2.0 - lw - 8.0)
|
|
383
|
+
bx = cell.x + pad_x
|
|
384
|
+
track = Rect(bx, cell.center_y - 3.0, bar_w, 6.0)
|
|
385
|
+
canvas.drawRRect(make_rrect(track, 3.0), skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()))
|
|
386
|
+
if ratio > 0.0:
|
|
387
|
+
fill_r = Rect(bx, cell.center_y - 3.0, bar_w * ratio, 6.0)
|
|
388
|
+
canvas.drawRRect(make_rrect(fill_r, 3.0), skia.Paint(AntiAlias=True, Color=c.primary.to_skia()))
|
|
389
|
+
draw_text_centered_y(canvas, lbl, bx + bar_w + 8.0, cell.center_y, lfont, c.on_surface_variant)
|
|
390
|
+
return
|
|
391
|
+
|
|
392
|
+
if col.type == "rating":
|
|
393
|
+
try:
|
|
394
|
+
stars = max(0, min(5, int(round(float(value)))))
|
|
395
|
+
except (TypeError, ValueError):
|
|
396
|
+
stars = 0
|
|
397
|
+
step = 16.0
|
|
398
|
+
start_x = cell.center_x - (step * 4) * 0.5
|
|
399
|
+
for i in range(5):
|
|
400
|
+
sc = c.primary if i < stars else c.outline_variant
|
|
401
|
+
draw_icon(canvas, "star", start_x + i * step, cell.center_y, 13.0, sc)
|
|
402
|
+
return
|
|
403
|
+
|
|
404
|
+
if col.type in ("image", "avatar"):
|
|
405
|
+
img = _resolve_skia_image(value)
|
|
406
|
+
sz = min(38.0, self.row_height - 12.0)
|
|
407
|
+
dest = Rect(cell.center_x - sz * 0.5, cell.center_y - sz * 0.5, sz, sz)
|
|
408
|
+
rad = sz * 0.5 if col.type == "avatar" else theme.shape.small
|
|
409
|
+
if img is not None:
|
|
410
|
+
canvas.save()
|
|
411
|
+
canvas.clipRRect(make_rrect(dest, rad), doAntiAlias=True)
|
|
412
|
+
_draw_image_cover(canvas, img, dest)
|
|
413
|
+
canvas.restore()
|
|
414
|
+
canvas.drawRRect(
|
|
415
|
+
make_rrect(dest, rad),
|
|
416
|
+
skia.Paint(
|
|
417
|
+
AntiAlias=True,
|
|
418
|
+
Color=c.outline_variant.to_skia(),
|
|
419
|
+
Style=skia.Paint.kStroke_Style,
|
|
420
|
+
StrokeWidth=1.0,
|
|
421
|
+
),
|
|
422
|
+
)
|
|
423
|
+
else:
|
|
424
|
+
# Fallback initials avatar or placeholder icon
|
|
425
|
+
canvas.drawRRect(make_rrect(dest, rad), skia.Paint(AntiAlias=True, Color=c.primary_container.to_skia()))
|
|
426
|
+
initials = "".join(w[0].upper() for w in str(value or "?").split()[:2]) or "?"
|
|
427
|
+
ifont = font_for_style(theme.font_family, theme.typography.label_medium)
|
|
428
|
+
iw = measure_text(ifont, initials)
|
|
429
|
+
draw_text_centered_y(canvas, initials, dest.center_x - iw * 0.5, dest.center_y, ifont, c.on_primary_container)
|
|
430
|
+
return
|
|
431
|
+
|
|
432
|
+
# Default text-based cell (text, number, currency, percent, date, time)
|
|
433
|
+
txt = self.format_value(col, value)
|
|
434
|
+
tw = measure_text(font, txt)
|
|
435
|
+
if align == "right":
|
|
436
|
+
tx = cell.right - pad_x - tw
|
|
437
|
+
elif align == "center":
|
|
438
|
+
tx = cell.center_x - tw * 0.5
|
|
439
|
+
else:
|
|
440
|
+
tx = cell.x + pad_x
|
|
441
|
+
canvas.save()
|
|
442
|
+
canvas.clipRect(skia.Rect.MakeXYWH(cell.x + 6.0, cell.y, max(0.0, cell.width - 12.0), cell.height))
|
|
443
|
+
draw_text_centered_y(canvas, txt, tx, cell.center_y, font, fg)
|
|
444
|
+
canvas.restore()
|
|
445
|
+
|
|
446
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
447
|
+
c = theme.colors
|
|
448
|
+
rad_spec = self._shape_override if self._shape_override is not None else "medium"
|
|
449
|
+
rad_num = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
|
|
450
|
+
if self._elevation_override:
|
|
451
|
+
draw_elevation_shadow(canvas, self.rect, rad_num, self._elevation_override, c.shadow)
|
|
452
|
+
rrect = make_rrect_spec(self.rect, rad_spec, theme)
|
|
453
|
+
|
|
454
|
+
canvas.save()
|
|
455
|
+
canvas.clipRRect(rrect, doAntiAlias=True)
|
|
456
|
+
|
|
457
|
+
table_bg = resolve_color(self._bg_override, theme, c.surface) or c.surface
|
|
458
|
+
hdr_bg = resolve_color(self.header_bg, theme, c.surface_container) or c.surface_container
|
|
459
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=table_bg.to_skia()))
|
|
460
|
+
canvas.drawRect(
|
|
461
|
+
skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.HEADER_H),
|
|
462
|
+
skia.Paint(AntiAlias=True, Color=hdr_bg.to_skia()),
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
widths = self._compute_col_widths(self.rect.width)
|
|
466
|
+
header_font = font_for_style(theme.font_family, theme.typography.title_small)
|
|
467
|
+
cell_font = font_for_style(theme.font_family, theme.typography.body_medium)
|
|
468
|
+
divider_paint = skia.Paint(AntiAlias=True, Color=c.outline_variant.to_skia(), StrokeWidth=1.0)
|
|
469
|
+
|
|
470
|
+
cur_x = self.rect.x
|
|
471
|
+
for ci, (col, cw) in enumerate(zip(self.columns, widths)):
|
|
472
|
+
align = col.resolved_align()
|
|
473
|
+
tw = measure_text(header_font, col.title)
|
|
474
|
+
cy = self.rect.y + self.HEADER_H * 0.5
|
|
475
|
+
if align == "right":
|
|
476
|
+
tx = cur_x + cw - 14.0 - tw - (18.0 if self._sort_col == ci else 0.0)
|
|
477
|
+
elif align == "center":
|
|
478
|
+
tx = cur_x + (cw - tw) * 0.5
|
|
479
|
+
else:
|
|
480
|
+
tx = cur_x + 14.0
|
|
481
|
+
draw_text_centered_y(canvas, col.title, tx, cy, header_font, c.on_surface)
|
|
482
|
+
if self._sort_col == ci:
|
|
483
|
+
draw_icon(
|
|
484
|
+
canvas,
|
|
485
|
+
"chevron_up" if self._sort_asc else "chevron_down",
|
|
486
|
+
tx + tw + 12.0,
|
|
487
|
+
cy,
|
|
488
|
+
14.0,
|
|
489
|
+
c.primary,
|
|
490
|
+
)
|
|
491
|
+
cur_x += cw
|
|
492
|
+
|
|
493
|
+
canvas.drawLine(
|
|
494
|
+
self.rect.x,
|
|
495
|
+
self.rect.y + self.HEADER_H,
|
|
496
|
+
self.rect.right,
|
|
497
|
+
self.rect.y + self.HEADER_H,
|
|
498
|
+
divider_paint,
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
if not self._rows:
|
|
502
|
+
draw_text_centered_y(
|
|
503
|
+
canvas,
|
|
504
|
+
"Sin datos",
|
|
505
|
+
self.rect.x + 16.0,
|
|
506
|
+
self.rect.y + self.HEADER_H + self.row_height * 0.5,
|
|
507
|
+
cell_font,
|
|
508
|
+
c.on_surface_variant,
|
|
509
|
+
)
|
|
510
|
+
else:
|
|
511
|
+
for ri, row in enumerate(self._rows):
|
|
512
|
+
ry = self.rect.y + self.HEADER_H + ri * self.row_height
|
|
513
|
+
if self._selected_row == ri:
|
|
514
|
+
canvas.drawRect(
|
|
515
|
+
skia.Rect.MakeXYWH(self.rect.x, ry, self.rect.width, self.row_height),
|
|
516
|
+
skia.Paint(AntiAlias=True, Color=c.secondary_container.with_alpha(0.55).to_skia()),
|
|
517
|
+
)
|
|
518
|
+
elif self._hover_row == ri:
|
|
519
|
+
canvas.drawRect(
|
|
520
|
+
skia.Rect.MakeXYWH(self.rect.x, ry, self.rect.width, self.row_height),
|
|
521
|
+
skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
|
|
522
|
+
)
|
|
523
|
+
elif self.striped and ri % 2 == 1:
|
|
524
|
+
canvas.drawRect(
|
|
525
|
+
skia.Rect.MakeXYWH(self.rect.x, ry, self.rect.width, self.row_height),
|
|
526
|
+
skia.Paint(AntiAlias=True, Color=c.surface_container_low.to_skia()),
|
|
527
|
+
)
|
|
528
|
+
|
|
529
|
+
fg = c.on_secondary_container if self._selected_row == ri else c.on_surface
|
|
530
|
+
cx = self.rect.x
|
|
531
|
+
for ci, (col, cw) in enumerate(zip(self.columns, widths)):
|
|
532
|
+
val = row[ci] if ci < len(row) else None
|
|
533
|
+
self._paint_cell(canvas, col, val, Rect(cx, ry, cw, self.row_height), cell_font, fg, theme)
|
|
534
|
+
cx += cw
|
|
535
|
+
|
|
536
|
+
if self.show_dividers and ri < len(self._rows) - 1:
|
|
537
|
+
canvas.drawLine(self.rect.x, ry + self.row_height, self.rect.right, ry + self.row_height, divider_paint)
|
|
538
|
+
|
|
539
|
+
canvas.restore()
|
|
540
|
+
|
|
541
|
+
bc = resolve_color(self._border_color_override, theme, c.outline_variant) or c.outline_variant
|
|
542
|
+
bw = self._border_width_override if self._border_width_override is not None else 1.0
|
|
543
|
+
canvas.drawRRect(
|
|
544
|
+
rrect,
|
|
545
|
+
skia.Paint(
|
|
546
|
+
AntiAlias=True,
|
|
547
|
+
Color=bc.to_skia(),
|
|
548
|
+
Style=skia.Paint.kStroke_Style,
|
|
549
|
+
StrokeWidth=bw,
|
|
550
|
+
),
|
|
551
|
+
)
|