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.
Files changed (73) hide show
  1. quantumgui/__init__.py +9 -0
  2. quantumgui/__main__.py +6 -0
  3. quantumgui/cli.py +341 -0
  4. quantumgui/core/__init__.py +39 -0
  5. quantumgui/core/animation.py +221 -0
  6. quantumgui/core/audit.py +323 -0
  7. quantumgui/core/context.py +54 -0
  8. quantumgui/core/data_source.py +426 -0
  9. quantumgui/core/draw.py +428 -0
  10. quantumgui/core/enterprise.py +402 -0
  11. quantumgui/core/enterprise_app.py +419 -0
  12. quantumgui/core/events.py +34 -0
  13. quantumgui/core/geometry.py +108 -0
  14. quantumgui/core/native_dialogs.py +276 -0
  15. quantumgui/core/pdf_report.py +390 -0
  16. quantumgui/core/rules.py +284 -0
  17. quantumgui/core/state.py +42 -0
  18. quantumgui/core/task_runner.py +181 -0
  19. quantumgui/core/widget.py +498 -0
  20. quantumgui/core/window.py +920 -0
  21. quantumgui/core/xlsx_export.py +145 -0
  22. quantumgui/py.typed +0 -0
  23. quantumgui/theme/__init__.py +32 -0
  24. quantumgui/theme/hct.py +191 -0
  25. quantumgui/theme/theme.py +93 -0
  26. quantumgui/theme/tokens.py +423 -0
  27. quantumgui/ui.py +2142 -0
  28. quantumgui/widgets/__init__.py +168 -0
  29. quantumgui/widgets/advanced.py +499 -0
  30. quantumgui/widgets/button.py +224 -0
  31. quantumgui/widgets/charts.py +625 -0
  32. quantumgui/widgets/charts_bi.py +528 -0
  33. quantumgui/widgets/charts_extra.py +430 -0
  34. quantumgui/widgets/charts_pro.py +568 -0
  35. quantumgui/widgets/checkbox.py +199 -0
  36. quantumgui/widgets/chip.py +219 -0
  37. quantumgui/widgets/containers.py +321 -0
  38. quantumgui/widgets/data_controls.py +487 -0
  39. quantumgui/widgets/data_grid.py +956 -0
  40. quantumgui/widgets/data_table.py +551 -0
  41. quantumgui/widgets/date_picker.py +407 -0
  42. quantumgui/widgets/display_media.py +372 -0
  43. quantumgui/widgets/display_pro.py +407 -0
  44. quantumgui/widgets/drawer.py +252 -0
  45. quantumgui/widgets/enterprise_controls.py +934 -0
  46. quantumgui/widgets/fab.py +261 -0
  47. quantumgui/widgets/forms_pro.py +703 -0
  48. quantumgui/widgets/forms_pro_media.py +587 -0
  49. quantumgui/widgets/inputs.py +870 -0
  50. quantumgui/widgets/layout_pro.py +162 -0
  51. quantumgui/widgets/list_view.py +307 -0
  52. quantumgui/widgets/masked_input.py +131 -0
  53. quantumgui/widgets/master_detail.py +155 -0
  54. quantumgui/widgets/navigation.py +382 -0
  55. quantumgui/widgets/overlays.py +355 -0
  56. quantumgui/widgets/productivity.py +500 -0
  57. quantumgui/widgets/productivity_boards.py +544 -0
  58. quantumgui/widgets/radio.py +209 -0
  59. quantumgui/widgets/scroll_view.py +129 -0
  60. quantumgui/widgets/select.py +373 -0
  61. quantumgui/widgets/sheets.py +249 -0
  62. quantumgui/widgets/slider.py +209 -0
  63. quantumgui/widgets/switch.py +196 -0
  64. quantumgui/widgets/tabs.py +175 -0
  65. quantumgui/widgets/text.py +101 -0
  66. quantumgui/widgets/text_field.py +490 -0
  67. quantumgui/widgets/time_picker.py +499 -0
  68. quantumgui-1.0.0.dist-info/METADATA +137 -0
  69. quantumgui-1.0.0.dist-info/RECORD +73 -0
  70. quantumgui-1.0.0.dist-info/WHEEL +5 -0
  71. quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
  72. quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
  73. quantumgui-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,498 @@
1
+ """Base Widget class with NiceGUI-style `with` support and fluent modifiers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Literal, Self
6
+
7
+ import skia
8
+
9
+ from ..theme import Color, ShapeToken, Theme
10
+ from .context import context
11
+ from .draw import ColorSpec, draw_elevation_shadow, make_rrect_spec, resolve_color
12
+ from .events import CharEvent, KeyEvent, MouseEvent, ScrollEvent
13
+ from .geometry import Constraints, EdgeInsets, Rect, Size
14
+
15
+ if TYPE_CHECKING:
16
+ from .window import Window
17
+
18
+ SelfAlign = Literal["auto", "start", "center", "end", "stretch"]
19
+ CursorType = Literal["default", "pointer", "ibeam", "ew_resize"]
20
+ RadiusSpec = ShapeToken | float | tuple[float, float, float, float]
21
+
22
+
23
+ class Widget:
24
+ """Base class for all QuantumGUI Material Design 3 widgets."""
25
+
26
+ focusable: bool = False
27
+ interactive: bool = False
28
+ auto_attach: bool = True
29
+ dismiss_on_outside_click: bool = True
30
+ manages_own_box: bool = False
31
+ manages_own_elevation: bool = False
32
+ cursor: CursorType = "default"
33
+
34
+ def __init__(self) -> None:
35
+ self.parent: Widget | None = None
36
+ self.children: list[Widget] = []
37
+ self._window: Window | None = None
38
+
39
+ # Layout & universal style modifier properties
40
+ self._fixed_width: float | None = None
41
+ self._fixed_height: float | None = None
42
+ self._min_width: float | None = None
43
+ self._max_width: float | None = None
44
+ self._min_height: float | None = None
45
+ self._max_height: float | None = None
46
+ self._fill_width: bool = False
47
+ self._fill_height: bool = False
48
+ self._flex: float = 0.0
49
+ self._padding: EdgeInsets = EdgeInsets()
50
+ self._margin: EdgeInsets = EdgeInsets()
51
+ self._align_self: SelfAlign = "auto"
52
+ self._visible: bool = True
53
+ self._shape_override: RadiusSpec | None = None
54
+ self._elevation_override: int | None = None
55
+ self._shadow_color_override: ColorSpec | None = None
56
+ self._bg_override: ColorSpec | None = None
57
+ self._fg_override: ColorSpec | None = None
58
+ self._border_color_override: ColorSpec | None = None
59
+ self._border_width_override: float | None = None
60
+ self._opacity: float = 1.0
61
+ self._tooltip_text: str | None = None
62
+
63
+ # Computed layout rect (outer box excluding margin, in window coordinates)
64
+ self.rect: Rect = Rect(0.0, 0.0, 0.0, 0.0)
65
+
66
+ # Interaction state
67
+ self.hovered: bool = False
68
+ self.pressed: bool = False
69
+ self.focused: bool = False
70
+
71
+ # Auto-attach to active `with` container (NiceGUI pattern)
72
+ if self.auto_attach and not context.is_detached:
73
+ parent = context.current_container
74
+ if parent is not None:
75
+ parent.add_child(self)
76
+ elif context.current_window is not None and context.current_window.root is None:
77
+ context.current_window.set_root(self)
78
+
79
+ # --- NiceGUI-style `with` context manager ---
80
+
81
+ def __enter__(self) -> Self:
82
+ context.push_container(self)
83
+ return self
84
+
85
+ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
86
+ context.pop_container()
87
+
88
+ # --- Tree management ---
89
+
90
+ def add_child(self, child: Widget) -> Self:
91
+ child.parent = self
92
+ child._set_window_recursive(self.window)
93
+ self.children.append(child)
94
+ self.invalidate()
95
+ return self
96
+
97
+ def _set_window_recursive(self, window: Window | None) -> None:
98
+ self._window = window
99
+ for child in self.children:
100
+ child._set_window_recursive(window)
101
+
102
+ @property
103
+ def window(self) -> Window | None:
104
+ if self._window is not None:
105
+ return self._window
106
+ if self.parent is not None:
107
+ return self.parent.window
108
+ return None
109
+
110
+ def invalidate(self) -> None:
111
+ win = self.window
112
+ if win is not None:
113
+ win.request_redraw()
114
+
115
+ # --- Fluent layout & M3 token modifiers ---
116
+
117
+ def width(self, px: float) -> Self:
118
+ self._fixed_width = float(px)
119
+ self.invalidate()
120
+ return self
121
+
122
+ def height(self, px: float) -> Self:
123
+ self._fixed_height = float(px)
124
+ self.invalidate()
125
+ return self
126
+
127
+ def size(self, width: float, height: float) -> Self:
128
+ self._fixed_width = float(width)
129
+ self._fixed_height = float(height)
130
+ self.invalidate()
131
+ return self
132
+
133
+ def fill_width(self, enabled: bool = True) -> Self:
134
+ self._fill_width = enabled
135
+ self.invalidate()
136
+ return self
137
+
138
+ def fill_height(self, enabled: bool = True) -> Self:
139
+ self._fill_height = enabled
140
+ self.invalidate()
141
+ return self
142
+
143
+ def expand(self, flex: float = 1.0) -> Self:
144
+ self._flex = float(flex)
145
+ self.invalidate()
146
+ return self
147
+
148
+ def padding(
149
+ self,
150
+ value: float | tuple[float, float] | tuple[float, float, float, float] | EdgeInsets,
151
+ ) -> Self:
152
+ self._padding = EdgeInsets.parse(value)
153
+ self.invalidate()
154
+ return self
155
+
156
+ def margin(
157
+ self,
158
+ value: float | tuple[float, float] | tuple[float, float, float, float] | EdgeInsets,
159
+ ) -> Self:
160
+ self._margin = EdgeInsets.parse(value)
161
+ self.invalidate()
162
+ return self
163
+
164
+ def align_self(self, align: SelfAlign) -> Self:
165
+ self._align_self = align
166
+ self.invalidate()
167
+ return self
168
+
169
+ def visible(self, is_visible: bool) -> Self:
170
+ self._visible = bool(is_visible)
171
+ self.invalidate()
172
+ return self
173
+
174
+ def shape(self, token: RadiusSpec) -> Self:
175
+ self._shape_override = token
176
+ self.invalidate()
177
+ return self
178
+
179
+ def radius(self, value: RadiusSpec) -> Self:
180
+ """Alias for `.shape()`: accepts M3 token, uniform float, or `(tl, tr, br, bl)`."""
181
+ return self.shape(value)
182
+
183
+ def elevation(self, level: int, shadow_color: ColorSpec | None = None) -> Self:
184
+ self._elevation_override = max(0, min(5, int(level)))
185
+ if shadow_color is not None:
186
+ self._shadow_color_override = shadow_color
187
+ self.invalidate()
188
+ return self
189
+
190
+ def bg(self, color: ColorSpec | None) -> Self:
191
+ """Override container/background color (`#hex`, `Color`, or M3 token name)."""
192
+ self._bg_override = color
193
+ self.invalidate()
194
+ return self
195
+
196
+ def fg(self, color: ColorSpec | None) -> Self:
197
+ """Override foreground / content / active accent color."""
198
+ self._fg_override = color
199
+ self.invalidate()
200
+ return self
201
+
202
+ def accent(self, color: ColorSpec | None) -> Self:
203
+ """Alias for `.fg()` — sets custom active/accent color."""
204
+ return self.fg(color)
205
+
206
+ def border(self, color: ColorSpec | None, width: float = 1.0) -> Self:
207
+ """Set custom border color and stroke width in dp."""
208
+ self._border_color_override = color
209
+ self._border_width_override = float(width)
210
+ self.invalidate()
211
+ return self
212
+
213
+ def opacity(self, alpha: float) -> Self:
214
+ """Set widget (and subtree) opacity in `[0.0, 1.0]`."""
215
+ self._opacity = max(0.0, min(1.0, float(alpha)))
216
+ self.invalidate()
217
+ return self
218
+
219
+ def min_width(self, px: float) -> Self:
220
+ self._min_width = float(px)
221
+ self.invalidate()
222
+ return self
223
+
224
+ def max_width(self, px: float) -> Self:
225
+ self._max_width = float(px)
226
+ self.invalidate()
227
+ return self
228
+
229
+ def min_height(self, px: float) -> Self:
230
+ self._min_height = float(px)
231
+ self.invalidate()
232
+ return self
233
+
234
+ def max_height(self, px: float) -> Self:
235
+ self._max_height = float(px)
236
+ self.invalidate()
237
+ return self
238
+
239
+ def min_size(self, width: float, height: float) -> Self:
240
+ self._min_width = float(width)
241
+ self._min_height = float(height)
242
+ self.invalidate()
243
+ return self
244
+
245
+ def max_size(self, width: float, height: float) -> Self:
246
+ self._max_width = float(width)
247
+ self._max_height = float(height)
248
+ self.invalidate()
249
+ return self
250
+
251
+ def tooltip(self, text: str | None) -> Self:
252
+ """Attach an official Material Design 3 plain tooltip on hover."""
253
+ self._tooltip_text = text
254
+ return self
255
+
256
+ def require_role(
257
+ self,
258
+ role: str | tuple[str, ...] | list[str],
259
+ *,
260
+ mode: Literal["hide", "disable"] = "hide",
261
+ auth: object | None = None,
262
+ ) -> Self:
263
+ """Bind this widget reactively to an RBAC role (hides or disables if unauthorized)."""
264
+ from .enterprise_app import AuthContext, default_auth
265
+
266
+ ctx: AuthContext = auth if isinstance(auth, AuthContext) else default_auth
267
+
268
+ def _apply(a: AuthContext) -> None:
269
+ allowed = a.has_role(role)
270
+ if mode == "hide":
271
+ self.visible(allowed)
272
+ elif hasattr(self, "disabled"):
273
+ setattr(self, "disabled", not allowed)
274
+ self.invalidate()
275
+ elif hasattr(self, "_disabled"):
276
+ setattr(self, "_disabled", not allowed)
277
+ self.invalidate()
278
+
279
+ _apply(ctx)
280
+ ctx.subscribe(_apply)
281
+ return self
282
+
283
+ def require_permission(
284
+ self,
285
+ permission: str | tuple[str, ...] | list[str],
286
+ *,
287
+ mode: Literal["hide", "disable"] = "hide",
288
+ auth: object | None = None,
289
+ ) -> Self:
290
+ """Bind this widget reactively to an RBAC permission (hides or disables if unauthorized)."""
291
+ from .enterprise_app import AuthContext, default_auth
292
+
293
+ ctx: AuthContext = auth if isinstance(auth, AuthContext) else default_auth
294
+
295
+ def _apply(a: AuthContext) -> None:
296
+ allowed = a.has_permission(permission)
297
+ if mode == "hide":
298
+ self.visible(allowed)
299
+ elif hasattr(self, "disabled"):
300
+ setattr(self, "disabled", not allowed)
301
+ self.invalidate()
302
+ elif hasattr(self, "_disabled"):
303
+ setattr(self, "_disabled", not allowed)
304
+ self.invalidate()
305
+
306
+ _apply(ctx)
307
+ ctx.subscribe(_apply)
308
+ return self
309
+
310
+ # --- Layout protocol ---
311
+
312
+ def measure(self, constraints: Constraints, theme: Theme) -> Size:
313
+ """Compute desired size (excluding margin) given layout constraints."""
314
+ if not self._visible:
315
+ return Size(0.0, 0.0)
316
+ inner_c = self._apply_size_constraints(constraints)
317
+ natural = self._measure_content(inner_c, theme)
318
+ w = self._fixed_width if self._fixed_width is not None else natural.width
319
+ h = self._fixed_height if self._fixed_height is not None else natural.height
320
+ if self._fill_width and constraints.max_width != float("inf"):
321
+ w = constraints.max_width
322
+ if self._fill_height and constraints.max_height != float("inf"):
323
+ h = constraints.max_height
324
+ if self._min_width is not None:
325
+ w = max(w, self._min_width)
326
+ if self._max_width is not None:
327
+ w = min(w, self._max_width)
328
+ if self._min_height is not None:
329
+ h = max(h, self._min_height)
330
+ if self._max_height is not None:
331
+ h = min(h, self._max_height)
332
+ return Size(constraints.clamp_width(w), constraints.clamp_height(h))
333
+
334
+ def _apply_size_constraints(self, constraints: Constraints) -> Constraints:
335
+ max_w = self._fixed_width if self._fixed_width is not None else constraints.max_width
336
+ max_h = self._fixed_height if self._fixed_height is not None else constraints.max_height
337
+ min_w = self._fixed_width if self._fixed_width is not None else constraints.min_width
338
+ min_h = self._fixed_height if self._fixed_height is not None else constraints.min_height
339
+ return Constraints(min_w, max_w, min_h, max_h)
340
+
341
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
342
+ return Size(self._padding.horizontal, self._padding.vertical)
343
+
344
+ def layout(self, rect: Rect, theme: Theme) -> None:
345
+ self.rect = rect
346
+ self._layout_children(rect.inset(self._padding), theme)
347
+
348
+ def _layout_children(self, content_rect: Rect, theme: Theme) -> None:
349
+ for child in self.children:
350
+ if not child._visible:
351
+ continue
352
+ c = Constraints(0.0, content_rect.width, 0.0, content_rect.height)
353
+ sz = child.measure(c, theme)
354
+ child.layout(
355
+ Rect(
356
+ content_rect.x + child._margin.left,
357
+ content_rect.y + child._margin.top,
358
+ sz.width,
359
+ sz.height,
360
+ ),
361
+ theme,
362
+ )
363
+
364
+ # --- Rendering & Animation ---
365
+
366
+ def tick(self, now: float) -> bool:
367
+ """Advance widget animations; return True if another frame is needed."""
368
+ active = False
369
+ for child in self.children:
370
+ if child._visible and child.tick(now):
371
+ active = True
372
+ return active
373
+
374
+ def _paint_custom_border(
375
+ self,
376
+ canvas: skia.Canvas,
377
+ theme: Theme,
378
+ default_radius: RadiusSpec = 0.0,
379
+ ) -> None:
380
+ """Paint custom `.border(color, width)` inset inward so stroke never overflows `self.rect`."""
381
+ if self._border_color_override is None:
382
+ return
383
+ bc = resolve_color(self._border_color_override, theme)
384
+ if bc is None:
385
+ return
386
+ bw = self._border_width_override if self._border_width_override is not None else 1.0
387
+ if bw <= 0.0:
388
+ return
389
+ rad_spec = self._shape_override if self._shape_override is not None else default_radius
390
+ inset_rect = self.rect.inset(EdgeInsets.all(bw * 0.5))
391
+ if inset_rect.width <= 0.0 or inset_rect.height <= 0.0:
392
+ inset_rect = self.rect
393
+ rrect = make_rrect_spec(inset_rect, rad_spec, theme)
394
+ canvas.drawRRect(
395
+ rrect,
396
+ skia.Paint(
397
+ AntiAlias=True,
398
+ Color=bc.to_skia(),
399
+ Style=skia.Paint.kStroke_Style,
400
+ StrokeWidth=bw,
401
+ ),
402
+ )
403
+
404
+ def _paint_universal_bg(self, canvas: skia.Canvas, theme: Theme) -> None:
405
+ """Paint optional elevation shadow and background before `_paint_self`."""
406
+ if self._elevation_override and not self.manages_own_elevation:
407
+ rad_spec = self._shape_override if self._shape_override is not None else (
408
+ 12.0 if self.manages_own_box else 0.0
409
+ )
410
+ rad_num = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
411
+ sh_col = resolve_color(self._shadow_color_override, theme, theme.colors.shadow)
412
+ assert sh_col is not None
413
+ draw_elevation_shadow(canvas, self.rect, rad_num, self._elevation_override, sh_col)
414
+ if self.manages_own_box:
415
+ return
416
+ if self._bg_override is not None:
417
+ bg_c = resolve_color(self._bg_override, theme)
418
+ if bg_c is not None:
419
+ rad_spec = self._shape_override if self._shape_override is not None else 0.0
420
+ rrect = make_rrect_spec(self.rect, rad_spec, theme)
421
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg_c.to_skia()))
422
+
423
+ def _paint_universal_border(self, canvas: skia.Canvas, theme: Theme) -> None:
424
+ """Paint optional `.border()` after `_paint_self` so content never obscures the stroke."""
425
+ if self.manages_own_box:
426
+ return
427
+ self._paint_custom_border(canvas, theme, 0.0)
428
+
429
+ def paint(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
430
+ if not self._visible or self._opacity <= 0.0:
431
+ return
432
+ use_layer = self._opacity < 0.999
433
+ if use_layer:
434
+ canvas.saveLayerAlpha(
435
+ skia.Rect.MakeXYWH(self.rect.x - 24.0, self.rect.y - 24.0, self.rect.width + 48.0, self.rect.height + 48.0),
436
+ round(self._opacity * 255),
437
+ )
438
+ self._paint_universal_bg(canvas, theme)
439
+ self._paint_self(canvas, theme, now)
440
+ for child in self.children:
441
+ if child._visible:
442
+ child.paint(canvas, theme, now)
443
+ self._paint_universal_border(canvas, theme)
444
+ if use_layer:
445
+ canvas.restore()
446
+
447
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
448
+ pass
449
+
450
+ # --- Hit testing & Input events ---
451
+
452
+ def hit_test(self, x: float, y: float) -> Widget | None:
453
+ if not self._visible or not self.rect.contains(x, y):
454
+ return None
455
+ for child in reversed(self.children):
456
+ hit = child.hit_test(x, y)
457
+ if hit is not None:
458
+ return hit
459
+ if self.interactive or self.focusable:
460
+ return self
461
+ return None
462
+
463
+ def on_mouse_enter(self) -> None:
464
+ self.hovered = True
465
+ self.invalidate()
466
+
467
+ def on_mouse_leave(self) -> None:
468
+ self.hovered = False
469
+ self.invalidate()
470
+
471
+ def on_mouse_down(self, event: MouseEvent) -> None:
472
+ self.pressed = True
473
+ self.invalidate()
474
+
475
+ def on_mouse_move(self, event: MouseEvent) -> None:
476
+ pass
477
+
478
+ def on_mouse_up(self, event: MouseEvent) -> None:
479
+ self.pressed = False
480
+ self.invalidate()
481
+
482
+ def on_focus(self) -> None:
483
+ self.focused = True
484
+ self.invalidate()
485
+
486
+ def on_blur(self) -> None:
487
+ self.focused = False
488
+ self.invalidate()
489
+
490
+ def on_key(self, event: KeyEvent) -> None:
491
+ pass
492
+
493
+ def on_char(self, event: CharEvent) -> None:
494
+ pass
495
+
496
+ def on_scroll(self, event: ScrollEvent) -> bool:
497
+ """Handle mouse-wheel scroll; return True if consumed."""
498
+ return False