@flipova/foundation 1.14.0 → 2.0.5
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.
- package/CHANGELOG.md +181 -0
- package/README.md +53 -178
- package/components/ui/accordion/ANIMATION_CUSTOMIZATION.md +162 -0
- package/components/ui/accordion/AccordionAnimatedHeight.tsx +56 -0
- package/components/ui/accordion/AccordionAnimatedIcon.tsx +41 -0
- package/components/ui/accordion/AnimatedHeight.tsx +56 -0
- package/components/ui/accordion/AnimatedIcon.tsx +41 -0
- package/components/ui/accordion/MIGRATION_SUMMARY.md +250 -0
- package/components/ui/accordion/USAGE_EXAMPLE.tsx +179 -0
- package/components/ui/accordion/animation-config.ts +32 -0
- package/components/ui/accordion/index.tsx +253 -0
- package/components/ui/actionsheet/index.tsx +511 -0
- package/components/ui/alert/index.tsx +158 -0
- package/components/ui/alert-dialog/index.tsx +254 -0
- package/components/ui/avatar/index.tsx +139 -0
- package/components/ui/badge/index.tsx +163 -0
- package/components/ui/bottomsheet/index.tsx +477 -0
- package/components/ui/box/index.tsx +19 -0
- package/components/ui/box/index.web.tsx +19 -0
- package/components/ui/box/styles.tsx +10 -0
- package/components/ui/button/index.tsx +264 -0
- package/components/ui/calendar/creator.tsx +366 -0
- package/components/ui/calendar/index.tsx +462 -0
- package/components/ui/calendar/styles.tsx +140 -0
- package/components/ui/card/index.tsx +23 -0
- package/components/ui/card/index.web.tsx +26 -0
- package/components/ui/card/styles.tsx +16 -0
- package/components/ui/center/index.tsx +22 -0
- package/components/ui/center/index.web.tsx +20 -0
- package/components/ui/center/styles.tsx +8 -0
- package/components/ui/checkbox/index.tsx +184 -0
- package/components/ui/date-time-picker/creator.tsx +109 -0
- package/components/ui/date-time-picker/index.tsx +585 -0
- package/components/ui/date-time-picker/index.web.tsx +217 -0
- package/components/ui/date-time-picker/styles.tsx +56 -0
- package/components/ui/divider/index.tsx +40 -0
- package/components/ui/drawer/index.tsx +362 -0
- package/components/ui/fab/index.tsx +215 -0
- package/components/ui/flat-list/index.tsx +21 -0
- package/components/ui/form-control/index.tsx +352 -0
- package/components/ui/gluestack-ui-provider/config.ts +72 -0
- package/components/ui/gluestack-ui-provider/index.tsx +47 -0
- package/components/ui/gluestack-ui-provider/index.web.tsx +96 -0
- package/components/ui/gluestack-ui-provider/script.ts +19 -0
- package/components/ui/gluestack-ui-provider/tokens.d.ts +17 -0
- package/components/ui/gluestack-ui-provider/tokens.js +188 -0
- package/components/ui/gluestack-ui-provider/useGluestackColors.ts +82 -0
- package/components/ui/grid/index.tsx +283 -0
- package/components/ui/grid/index.web.tsx +66 -0
- package/components/ui/grid/styles.tsx +15 -0
- package/components/ui/heading/index.tsx +220 -0
- package/components/ui/heading/index.web.tsx +202 -0
- package/components/ui/heading/styles.tsx +43 -0
- package/components/ui/hstack/index.tsx +27 -0
- package/components/ui/hstack/index.web.tsx +26 -0
- package/components/ui/hstack/styles.tsx +25 -0
- package/components/ui/icon/index.tsx +1588 -0
- package/components/ui/icon/index.web.tsx +1573 -0
- package/components/ui/image/index.tsx +48 -0
- package/components/ui/image-background/index.tsx +25 -0
- package/components/ui/image-viewer/index.tsx +743 -0
- package/components/ui/image-viewer/index.web.tsx +302 -0
- package/components/ui/index.ts +58 -0
- package/components/ui/input/index.tsx +125 -0
- package/components/ui/input-accessory-view/index.tsx +2 -0
- package/components/ui/keyboard-avoiding-view/index.tsx +2 -0
- package/components/ui/link/index.tsx +102 -0
- package/components/ui/liquid-glass/index.tsx +74 -0
- package/components/ui/menu/index.tsx +186 -0
- package/components/ui/modal/index.tsx +237 -0
- package/components/ui/popover/index.tsx +272 -0
- package/components/ui/portal/index.tsx +17 -0
- package/components/ui/pressable/index.tsx +39 -0
- package/components/ui/progress/index.tsx +83 -0
- package/components/ui/radio/index.tsx +217 -0
- package/components/ui/refresh-control/index.tsx +2 -0
- package/components/ui/safe-area-view/index.tsx +2 -0
- package/components/ui/scroll-view/index.tsx +2 -0
- package/components/ui/section-list/index.tsx +2 -0
- package/components/ui/select/index.tsx +277 -0
- package/components/ui/select/select-actionsheet.tsx +562 -0
- package/components/ui/skeleton/index.tsx +105 -0
- package/components/ui/skeleton/index.web.tsx +103 -0
- package/components/ui/skeleton/styles.tsx +35 -0
- package/components/ui/slider/index.tsx +181 -0
- package/components/ui/spinner/index.tsx +40 -0
- package/components/ui/status-bar/index.tsx +2 -0
- package/components/ui/switch/index.tsx +41 -0
- package/components/ui/table/index.tsx +210 -0
- package/components/ui/table/index.web.tsx +157 -0
- package/components/ui/table/styles.tsx +44 -0
- package/components/ui/tabs/TabsAnimatedIndicator.tsx +160 -0
- package/components/ui/tabs/animation-config.ts +3 -0
- package/components/ui/tabs/creator.tsx +206 -0
- package/components/ui/tabs/index.tsx +484 -0
- package/components/ui/text/index.tsx +48 -0
- package/components/ui/text/index.web.tsx +45 -0
- package/components/ui/text/styles.tsx +47 -0
- package/components/ui/textarea/index.tsx +94 -0
- package/components/ui/toast/index.tsx +252 -0
- package/components/ui/tooltip/index.tsx +131 -0
- package/components/ui/view/index.tsx +2 -0
- package/components/ui/virtualized-list/index.tsx +2 -0
- package/components/ui/vstack/index.tsx +28 -0
- package/components/ui/vstack/index.web.tsx +27 -0
- package/components/ui/vstack/styles.tsx +25 -0
- package/design/canonical.index.txt +53 -0
- package/design/documentation.xml +519 -0
- package/design/manifest.xml +90 -0
- package/design/schema.xsd +639 -0
- package/design/themes.xml +204 -0
- package/design/tokens.xml +510 -0
- package/design/tools/design.js +165 -0
- package/design/tools/npm.js +65 -0
- package/design/tools/requirements.txt +35 -0
- package/design/tools/sources/.checker/__init__.py +81 -0
- package/design/tools/sources/.checker/checks.py +198 -0
- package/design/tools/sources/.checker/cli.py +154 -0
- package/design/tools/sources/.checker/io.py +242 -0
- package/design/tools/sources/.checker/model.py +144 -0
- package/design/tools/sources/.checker/registry.py +115 -0
- package/design/tools/sources/.checker/report.py +21 -0
- package/design/tools/sources/.docgen/__init__.py +45 -0
- package/design/tools/sources/.docgen/cli.py +101 -0
- package/design/tools/sources/.docgen/model.py +147 -0
- package/design/tools/sources/.docgen/reference.py +97 -0
- package/design/tools/sources/.docgen/site.py +145 -0
- package/design/tools/sources/.docgen/xsddoc.py +329 -0
- package/design/tools/sources/.generate/__init__.py +35 -0
- package/design/tools/sources/.generate/cli.py +76 -0
- package/design/tools/sources/.generate/common.py +33 -0
- package/design/tools/sources/.generate/config.py +119 -0
- package/design/tools/sources/.generate/model.py +51 -0
- package/design/tools/sources/.generate/tokens.py +141 -0
- package/design/tools/sources/.linter/__init__.py +9 -0
- package/design/tools/sources/.linter/cli.py +44 -0
- package/design/tools/sources/.linter/discovery.py +30 -0
- package/design/tools/sources/.linter/errors.py +45 -0
- package/design/tools/sources/.linter/linter.py +68 -0
- package/design/tools/sources/.xmleditor/__init__.py +74 -0
- package/design/tools/sources/.xmleditor/blocks.py +185 -0
- package/design/tools/sources/.xmleditor/cli.py +141 -0
- package/design/tools/sources/.xmleditor/editor.py +906 -0
- package/design/tools/sources/.xmleditor/model.py +708 -0
- package/design/tools/sources/.xmleditor/widgets.py +1832 -0
- package/design/tools/sources/.xmleditor/xsdmodel.py +753 -0
- package/design/tools/sources/checker.py +55 -0
- package/design/tools/sources/docgen.py +66 -0
- package/design/tools/sources/generate.py +48 -0
- package/design/tools/sources/linter.py +54 -0
- package/design/tools/sources/ruff.toml +12 -0
- package/design/tools/sources/xmleditor.py +62 -0
- package/design/tools/unix/_venv_python.sh +16 -0
- package/design/tools/unix/check.sh +13 -0
- package/design/tools/unix/doc.sh +14 -0
- package/design/tools/unix/gen.sh +14 -0
- package/design/tools/unix/lint.sh +10 -0
- package/design/tools/unix/run.sh +20 -0
- package/design/tools/unix/verify.sh +12 -0
- package/design/tools/unix/xmledit.sh +12 -0
- package/design/tools/windows/check.cmd +12 -0
- package/design/tools/windows/doc.cmd +12 -0
- package/design/tools/windows/gen.cmd +11 -0
- package/design/tools/windows/lint.cmd +9 -0
- package/design/tools/windows/run.cmd +26 -0
- package/design/tools/windows/verify.cmd +10 -0
- package/design/tools/windows/xmledit.cmd +10 -0
- package/dist/chunk-LV4UIEQN.js +9730 -0
- package/dist/chunk-LV4UIEQN.js.map +1 -0
- package/dist/chunk-WNVU3BM2.mjs +9401 -0
- package/dist/chunk-WNVU3BM2.mjs.map +1 -0
- package/dist/index.d.mts +40 -10
- package/dist/index.d.ts +40 -10
- package/dist/index.js +726 -414
- package/dist/index.mjs +1 -5
- package/dist/ui/index.d.mts +6931 -3070
- package/dist/ui/index.d.ts +6931 -3070
- package/dist/ui/index.js +779 -352
- package/dist/ui/index.mjs +1 -2
- package/package.json +66 -149
- package/dist/chunk-2XORADW6.js +0 -24552
- package/dist/chunk-2XORADW6.js.map +0 -1
- package/dist/chunk-52BHHF5M.js +0 -46
- package/dist/chunk-52BHHF5M.js.map +0 -1
- package/dist/chunk-5647JY7Z.mjs +0 -100
- package/dist/chunk-5647JY7Z.mjs.map +0 -1
- package/dist/chunk-5N5HC4ZA.mjs +0 -3
- package/dist/chunk-5N5HC4ZA.mjs.map +0 -1
- package/dist/chunk-7QFZMGL6.js +0 -8583
- package/dist/chunk-7QFZMGL6.js.map +0 -1
- package/dist/chunk-ASZEP37D.js +0 -423
- package/dist/chunk-ASZEP37D.js.map +0 -1
- package/dist/chunk-BH5SU2JQ.mjs +0 -8282
- package/dist/chunk-BH5SU2JQ.mjs.map +0 -1
- package/dist/chunk-BO65GO53.mjs +0 -422
- package/dist/chunk-BO65GO53.mjs.map +0 -1
- package/dist/chunk-BSVXX52E.mjs +0 -413
- package/dist/chunk-BSVXX52E.mjs.map +0 -1
- package/dist/chunk-DNV5UBNF.mjs +0 -3
- package/dist/chunk-DNV5UBNF.mjs.map +0 -1
- package/dist/chunk-EDTAMUO2.js +0 -8310
- package/dist/chunk-EDTAMUO2.js.map +0 -1
- package/dist/chunk-IJTI6EX4.js +0 -4
- package/dist/chunk-IJTI6EX4.js.map +0 -1
- package/dist/chunk-M3YLASM2.js +0 -428
- package/dist/chunk-M3YLASM2.js.map +0 -1
- package/dist/chunk-OXKOGLXE.js +0 -4
- package/dist/chunk-OXKOGLXE.js.map +0 -1
- package/dist/chunk-RQXQK3ZO.mjs +0 -8486
- package/dist/chunk-RQXQK3ZO.mjs.map +0 -1
- package/dist/chunk-SR25V4EA.mjs +0 -40
- package/dist/chunk-SR25V4EA.mjs.map +0 -1
- package/dist/chunk-UB34WFEQ.js +0 -106
- package/dist/chunk-UB34WFEQ.js.map +0 -1
- package/dist/chunk-ZBRWXKDF.mjs +0 -24431
- package/dist/chunk-ZBRWXKDF.mjs.map +0 -1
- package/dist/cli/ds.d.mts +0 -1
- package/dist/cli/ds.d.ts +0 -1
- package/dist/cli/ds.js +0 -602
- package/dist/cli/ds.js.map +0 -1
- package/dist/cli/ds.mjs +0 -579
- package/dist/cli/ds.mjs.map +0 -1
- package/dist/cli/flipova.d.mts +0 -1
- package/dist/cli/flipova.d.ts +0 -1
- package/dist/cli/flipova.js +0 -161
- package/dist/cli/flipova.js.map +0 -1
- package/dist/cli/flipova.mjs +0 -154
- package/dist/cli/flipova.mjs.map +0 -1
- package/dist/config/index.d.mts +0 -123
- package/dist/config/index.d.ts +0 -123
- package/dist/config/index.js +0 -28
- package/dist/config/index.js.map +0 -1
- package/dist/config/index.mjs +0 -3
- package/dist/config/index.mjs.map +0 -1
- package/dist/createTheme-DQeX3s0I.d.mts +0 -78
- package/dist/createTheme-Y4jfz4-u.d.ts +0 -78
- package/dist/index-CuaJA3gM.d.mts +0 -78
- package/dist/index-CuaJA3gM.d.ts +0 -78
- package/dist/registry/index.d.mts +0 -10160
- package/dist/registry/index.d.ts +0 -10160
- package/dist/registry/index.js +0 -392
- package/dist/registry/index.js.map +0 -1
- package/dist/registry/index.mjs +0 -3
- package/dist/registry/index.mjs.map +0 -1
- package/dist/theme/index.d.mts +0 -35
- package/dist/theme/index.d.ts +0 -35
- package/dist/theme/index.js +0 -28
- package/dist/theme/index.js.map +0 -1
- package/dist/theme/index.mjs +0 -3
- package/dist/theme/index.mjs.map +0 -1
- package/dist/tokens/index.d.mts +0 -423
- package/dist/tokens/index.d.ts +0 -423
- package/dist/tokens/index.js +0 -44
- package/dist/tokens/index.js.map +0 -1
- package/dist/tokens/index.mjs +0 -3
- package/dist/tokens/index.mjs.map +0 -1
- package/dist/types/index.d.mts +0 -202
- package/dist/types/index.d.ts +0 -202
- package/dist/types/index.js +0 -4
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -3
- package/dist/types/index.mjs.map +0 -1
- package/dist/web/index.d.mts +0 -316
- package/dist/web/index.d.ts +0 -316
- package/dist/web/index.js +0 -1500
- package/dist/web/index.js.map +0 -1
- package/dist/web/index.mjs +0 -1396
- package/dist/web/index.mjs.map +0 -1
|
@@ -0,0 +1,906 @@
|
|
|
1
|
+
"""Main window of the visual XML editor.
|
|
2
|
+
|
|
3
|
+
The window is assembled from the XSD-driven widgets: palette on the left
|
|
4
|
+
(schema profile always visible + what is allowed under the selection),
|
|
5
|
+
document blocks / diagram / source in the middle, typed inspector on the
|
|
6
|
+
right, problem list at the bottom. Every action routes through
|
|
7
|
+
``DocumentModel`` so undo/redo and dirty tracking are exact.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import tkinter as tk
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from tkinter import filedialog, messagebox, simpledialog, ttk
|
|
15
|
+
|
|
16
|
+
from .blocks import can_add, contextual_entries, describe
|
|
17
|
+
from .model import DocumentModel, Issue, localname
|
|
18
|
+
from .widgets import (
|
|
19
|
+
ISSUE_MARKS,
|
|
20
|
+
ISSUE_TEXT,
|
|
21
|
+
BlockDiagram,
|
|
22
|
+
BlockPalette,
|
|
23
|
+
Breadcrumb,
|
|
24
|
+
DocumentTree,
|
|
25
|
+
EmptyState,
|
|
26
|
+
FileExplorer,
|
|
27
|
+
PropertyPanel,
|
|
28
|
+
SourceView,
|
|
29
|
+
Tip,
|
|
30
|
+
apply_theme,
|
|
31
|
+
)
|
|
32
|
+
from .xsdmodel import SchemaModel
|
|
33
|
+
|
|
34
|
+
APP_TITLE = "Flipova Foundation — XML block editor"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class XmlEditorApp:
|
|
38
|
+
"""The editor window (schema-driven: it knows no element name by heart)."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, schema: SchemaModel, path: Path | None = None) -> None:
|
|
41
|
+
self.schema = schema
|
|
42
|
+
self.doc: DocumentModel | None = None
|
|
43
|
+
self.issues: list[Issue] = []
|
|
44
|
+
self.selected = None
|
|
45
|
+
self._keep_panel = False # True while an inspector edit is being applied
|
|
46
|
+
self._actions: dict[str, ttk.Button] = {}
|
|
47
|
+
self._issue_map: dict[str, Issue] = {}
|
|
48
|
+
|
|
49
|
+
self.root = tk.Tk()
|
|
50
|
+
apply_theme(self.root)
|
|
51
|
+
self.root.title(APP_TITLE)
|
|
52
|
+
self.root.geometry("1400x900")
|
|
53
|
+
self.root.minsize(1040, 660)
|
|
54
|
+
self.root.protocol("WM_DELETE_WINDOW", self.quit)
|
|
55
|
+
|
|
56
|
+
self._build_menu()
|
|
57
|
+
self._build_toolbar()
|
|
58
|
+
self._build_status()
|
|
59
|
+
self._build_body()
|
|
60
|
+
self._bind_keys()
|
|
61
|
+
|
|
62
|
+
if path is not None:
|
|
63
|
+
self.open_path(path)
|
|
64
|
+
else:
|
|
65
|
+
self.refresh()
|
|
66
|
+
|
|
67
|
+
# -- construction ----------------------------------------------------
|
|
68
|
+
|
|
69
|
+
def _build_menu(self) -> None:
|
|
70
|
+
menubar = tk.Menu(self.root)
|
|
71
|
+
|
|
72
|
+
file_menu = tk.Menu(menubar, tearoff=False)
|
|
73
|
+
file_menu.add_command(label="New from schema…", accelerator="Ctrl+N",
|
|
74
|
+
command=self.choose_root)
|
|
75
|
+
file_menu.add_command(label="Open…", accelerator="Ctrl+O", command=self.open_file)
|
|
76
|
+
file_menu.add_separator()
|
|
77
|
+
file_menu.add_command(label="Save", accelerator="Ctrl+S", command=self.save)
|
|
78
|
+
file_menu.add_command(label="Save as…", command=self.save_as)
|
|
79
|
+
file_menu.add_separator()
|
|
80
|
+
file_menu.add_command(label="Exit", command=self.quit)
|
|
81
|
+
menubar.add_cascade(label="File", menu=file_menu)
|
|
82
|
+
|
|
83
|
+
edit_menu = tk.Menu(menubar, tearoff=False)
|
|
84
|
+
edit_menu.add_command(label="Undo", accelerator="Ctrl+Z", command=self.undo)
|
|
85
|
+
edit_menu.add_command(label="Redo", accelerator="Ctrl+Y", command=self.redo)
|
|
86
|
+
edit_menu.add_separator()
|
|
87
|
+
edit_menu.add_command(label="Auto-fix all issues", accelerator="F2",
|
|
88
|
+
command=self.auto_fix)
|
|
89
|
+
edit_menu.add_separator()
|
|
90
|
+
edit_menu.add_command(label="Add block", accelerator="Insert", command=self.add_block)
|
|
91
|
+
edit_menu.add_command(label="Duplicate block", accelerator="Ctrl+D",
|
|
92
|
+
command=self.duplicate_block)
|
|
93
|
+
edit_menu.add_command(label="Remove block", accelerator="Del", command=self.remove_block)
|
|
94
|
+
edit_menu.add_separator()
|
|
95
|
+
edit_menu.add_command(label="Move up", accelerator="Alt+Up",
|
|
96
|
+
command=lambda: self.move_block(-1))
|
|
97
|
+
edit_menu.add_command(label="Move down", accelerator="Alt+Down",
|
|
98
|
+
command=lambda: self.move_block(1))
|
|
99
|
+
menubar.add_cascade(label="Edit", menu=edit_menu)
|
|
100
|
+
|
|
101
|
+
view_menu = tk.Menu(menubar, tearoff=False)
|
|
102
|
+
view_menu.add_command(label="Blocks view", accelerator="Ctrl+1",
|
|
103
|
+
command=lambda: self.notebook.select(self.tree))
|
|
104
|
+
view_menu.add_command(label="Diagram view", accelerator="Ctrl+2",
|
|
105
|
+
command=lambda: self.notebook.select(self.diagram))
|
|
106
|
+
view_menu.add_command(label="Source view", accelerator="Ctrl+3",
|
|
107
|
+
command=lambda: self.notebook.select(self.source))
|
|
108
|
+
view_menu.add_separator()
|
|
109
|
+
view_menu.add_command(label="Search blocks", accelerator="Ctrl+F",
|
|
110
|
+
command=lambda: self.palette.focus_search())
|
|
111
|
+
menubar.add_cascade(label="View", menu=view_menu)
|
|
112
|
+
|
|
113
|
+
tools_menu = tk.Menu(menubar, tearoff=False)
|
|
114
|
+
tools_menu.add_command(label="Validate (XSD)", accelerator="F5", command=self.validate_all)
|
|
115
|
+
tools_menu.add_command(label="Schema profile summary", command=self.show_schema_summary)
|
|
116
|
+
menubar.add_cascade(label="Tools", menu=tools_menu)
|
|
117
|
+
|
|
118
|
+
help_menu = tk.Menu(menubar, tearoff=False)
|
|
119
|
+
help_menu.add_command(label="About", command=self.show_about)
|
|
120
|
+
menubar.add_cascade(label="Help", menu=help_menu)
|
|
121
|
+
|
|
122
|
+
self.root.configure(menu=menubar)
|
|
123
|
+
|
|
124
|
+
def _build_toolbar(self) -> None:
|
|
125
|
+
bar = ttk.Frame(self.root, padding=(12, 10, 12, 6))
|
|
126
|
+
bar.pack(fill="x")
|
|
127
|
+
left = ttk.Frame(bar)
|
|
128
|
+
left.pack(side="left", fill="x")
|
|
129
|
+
right = ttk.Frame(bar)
|
|
130
|
+
right.pack(side="right")
|
|
131
|
+
|
|
132
|
+
def add(parent, key, label, tip, command,
|
|
133
|
+
style="Toolbar.TButton", padx=(0, 6)):
|
|
134
|
+
button = ttk.Button(parent, text=label, command=command, style=style)
|
|
135
|
+
button.pack(side="left", padx=padx)
|
|
136
|
+
Tip(button, tip)
|
|
137
|
+
self._actions[key] = button
|
|
138
|
+
|
|
139
|
+
# left: document lifecycle + history (block ops live in the menus,
|
|
140
|
+
# the right-click menu and the keyboard to keep the bar calm)
|
|
141
|
+
add(left, "new", "New", "New document from a schema root (Ctrl+N)",
|
|
142
|
+
self.choose_root)
|
|
143
|
+
add(left, "open", "Open", "Open an XML file (Ctrl+O)", self.open_file)
|
|
144
|
+
add(left, "save", "Save", "Save the document (Ctrl+S)", self.save)
|
|
145
|
+
ttk.Separator(left, orient="vertical").pack(side="left", fill="y",
|
|
146
|
+
padx=(4, 12))
|
|
147
|
+
add(left, "undo", "Undo", "Undo (Ctrl+Z)", self.undo)
|
|
148
|
+
add(left, "redo", "Redo", "Redo (Ctrl+Y)", self.redo, padx=(0, 0))
|
|
149
|
+
|
|
150
|
+
# right: the two primary actions
|
|
151
|
+
add(right, "validate", "Validate", "Validate against the XSD (F5)",
|
|
152
|
+
self.validate_all, padx=(0, 8))
|
|
153
|
+
add(right, "fix", "Auto-fix all",
|
|
154
|
+
("Add missing required blocks/attributes and correct "
|
|
155
|
+
"invalid values in one click (F2)"),
|
|
156
|
+
self.auto_fix, style="Accent.TButton", padx=(0, 0))
|
|
157
|
+
|
|
158
|
+
def _build_body(self) -> None:
|
|
159
|
+
outer = ttk.PanedWindow(self.root, orient="vertical")
|
|
160
|
+
outer.pack(fill="both", expand=True, padx=10, pady=(4, 0))
|
|
161
|
+
|
|
162
|
+
top = ttk.PanedWindow(outer, orient="horizontal")
|
|
163
|
+
outer.add(top, weight=5)
|
|
164
|
+
|
|
165
|
+
# far left: file explorer (XML files under the schema's folder = design/)
|
|
166
|
+
self.explorer = FileExplorer(top, self.schema.path.parent,
|
|
167
|
+
self._explorer_open)
|
|
168
|
+
top.add(self.explorer, weight=1)
|
|
169
|
+
|
|
170
|
+
# left: palette (schema profile is permanent, context list updates)
|
|
171
|
+
self.palette = BlockPalette(top, self.on_palette_activate)
|
|
172
|
+
self.palette.set_profile(self.schema.profile())
|
|
173
|
+
top.add(self.palette, weight=1)
|
|
174
|
+
|
|
175
|
+
# centre: breadcrumb + views (+ start screen while no document)
|
|
176
|
+
center = ttk.Frame(top)
|
|
177
|
+
top.add(center, weight=4)
|
|
178
|
+
self.breadcrumb = Breadcrumb(center, self.select)
|
|
179
|
+
self.breadcrumb.pack(fill="x", padx=10, pady=(8, 6))
|
|
180
|
+
self.notebook = ttk.Notebook(center)
|
|
181
|
+
self.notebook.pack(fill="both", expand=True, padx=10, pady=(0, 8))
|
|
182
|
+
self.tree = DocumentTree(self.notebook, self.select, self.popup_menu)
|
|
183
|
+
self.notebook.add(self.tree, text="Blocks")
|
|
184
|
+
self.diagram = BlockDiagram(self.notebook, self.select, self.popup_menu)
|
|
185
|
+
self.notebook.add(self.diagram, text="Diagram")
|
|
186
|
+
self.source = SourceView(self.notebook)
|
|
187
|
+
self.notebook.add(self.source, text="Source")
|
|
188
|
+
self.notebook.bind("<<NotebookTabChanged>>", self._on_tab_changed)
|
|
189
|
+
self.empty = EmptyState(center, list(self.schema.root_names),
|
|
190
|
+
self._new_from_root, self.open_file)
|
|
191
|
+
|
|
192
|
+
# right: inspector
|
|
193
|
+
self.properties = PropertyPanel(top, self.on_property_change)
|
|
194
|
+
top.add(self.properties, weight=2)
|
|
195
|
+
|
|
196
|
+
# bottom: problems
|
|
197
|
+
issues_frame = ttk.Frame(outer)
|
|
198
|
+
outer.add(issues_frame, weight=1)
|
|
199
|
+
head = ttk.Frame(issues_frame)
|
|
200
|
+
head.pack(fill="x", padx=10, pady=(6, 4))
|
|
201
|
+
ttk.Label(head, text="Problems", style="Section.TLabel").pack(side="left")
|
|
202
|
+
self.problem_summary = ttk.Label(head, text="", style="Hint.TLabel")
|
|
203
|
+
self.problem_summary.pack(side="left", padx=10)
|
|
204
|
+
self.show_warnings = tk.BooleanVar(value=True)
|
|
205
|
+
ttk.Checkbutton(head, text="Show warnings", variable=self.show_warnings,
|
|
206
|
+
command=lambda: self._fill_issues(self.issues)).pack(side="right")
|
|
207
|
+
fix_button = ttk.Button(head, text="Auto-fix all", style="Accent.TButton",
|
|
208
|
+
command=self.auto_fix)
|
|
209
|
+
fix_button.pack(side="right", padx=(0, 8))
|
|
210
|
+
Tip(fix_button, "Add every missing required block/attribute and correct "
|
|
211
|
+
"invalid values in one click (F2)")
|
|
212
|
+
Tip(head, "Structural checks run live; press F5 for the full XSD validation")
|
|
213
|
+
|
|
214
|
+
body = ttk.Frame(issues_frame)
|
|
215
|
+
body.pack(fill="both", expand=True)
|
|
216
|
+
self.issues_tree = ttk.Treeview(body, columns=("severity", "block", "message"),
|
|
217
|
+
show="headings", height=6, selectmode="browse")
|
|
218
|
+
for column, width, title in (("severity", 90, "Severity"), ("block", 260, "Block"),
|
|
219
|
+
("message", 800, "Diagnostic")):
|
|
220
|
+
self.issues_tree.heading(column, text=title)
|
|
221
|
+
self.issues_tree.column(column, width=width, stretch=(column == "message"))
|
|
222
|
+
issues_y = ttk.Scrollbar(body, orient="vertical", command=self.issues_tree.yview)
|
|
223
|
+
self.issues_tree.configure(yscrollcommand=issues_y.set)
|
|
224
|
+
self.issues_tree.pack(side="left", fill="both", expand=True)
|
|
225
|
+
issues_y.pack(side="right", fill="y")
|
|
226
|
+
self.issues_tree.tag_configure("error", foreground=ISSUE_TEXT["error"])
|
|
227
|
+
self.issues_tree.tag_configure("warning", foreground=ISSUE_TEXT["warning"])
|
|
228
|
+
self.issues_tree.bind("<<TreeviewSelect>>", self._on_issue_activate)
|
|
229
|
+
|
|
230
|
+
def _build_status(self) -> None:
|
|
231
|
+
holder = ttk.Frame(self.root)
|
|
232
|
+
holder.pack(side="bottom", fill="x")
|
|
233
|
+
ttk.Separator(holder, orient="horizontal").pack(fill="x")
|
|
234
|
+
bar = ttk.Frame(holder, padding=(12, 5))
|
|
235
|
+
bar.pack(fill="x")
|
|
236
|
+
self.status = tk.StringVar(value="Ready — F5: validate · F2: auto-fix")
|
|
237
|
+
self.counts = tk.StringVar(value="")
|
|
238
|
+
ttk.Label(bar, textvariable=self.status, anchor="w").pack(side="left", fill="x",
|
|
239
|
+
expand=True)
|
|
240
|
+
ttk.Label(bar, textvariable=self.counts, style="Hint.TLabel").pack(side="right")
|
|
241
|
+
|
|
242
|
+
def _guard(self, func, views_only: bool = False):
|
|
243
|
+
"""Keyboard shortcut that never fires while the user is typing.
|
|
244
|
+
|
|
245
|
+
``views_only`` restricts it to the block tree / diagram, so Delete in
|
|
246
|
+
a text field or in the palette can no longer remove a block.
|
|
247
|
+
"""
|
|
248
|
+
editing = (tk.Entry, tk.Text, tk.Spinbox)
|
|
249
|
+
|
|
250
|
+
def handler(event):
|
|
251
|
+
widget = event.widget
|
|
252
|
+
if isinstance(widget, editing):
|
|
253
|
+
return None
|
|
254
|
+
if views_only and widget not in (self.tree.tree, self.diagram.canvas):
|
|
255
|
+
return None
|
|
256
|
+
func()
|
|
257
|
+
return "break"
|
|
258
|
+
return handler
|
|
259
|
+
|
|
260
|
+
def _bind_keys(self) -> None:
|
|
261
|
+
r = self.root
|
|
262
|
+
r.bind("<Control-n>", lambda _e: self.choose_root())
|
|
263
|
+
r.bind("<Control-o>", lambda _e: self.open_file())
|
|
264
|
+
r.bind("<Control-s>", lambda _e: self.save())
|
|
265
|
+
r.bind("<Control-f>", lambda _e: self.palette.focus_search())
|
|
266
|
+
r.bind("<F5>", lambda _e: self.validate_all())
|
|
267
|
+
r.bind("<F2>", self._guard(self.auto_fix))
|
|
268
|
+
for number, view in enumerate((self.tree, self.diagram, self.source), 1):
|
|
269
|
+
r.bind(f"<Control-Key-{number}>", lambda _e, v=view: self.notebook.select(v))
|
|
270
|
+
r.bind("<Control-z>", self._guard(self.undo))
|
|
271
|
+
r.bind("<Control-y>", self._guard(self.redo))
|
|
272
|
+
r.bind("<Control-Shift-Z>", self._guard(self.redo))
|
|
273
|
+
r.bind("<Control-d>", self._guard(self.duplicate_block))
|
|
274
|
+
r.bind("<Insert>", self._guard(self.add_block))
|
|
275
|
+
r.bind("<Alt-Up>", self._guard(lambda: self.move_block(-1)))
|
|
276
|
+
r.bind("<Alt-Down>", self._guard(lambda: self.move_block(1)))
|
|
277
|
+
r.bind("<Delete>", self._guard(self.remove_block, views_only=True))
|
|
278
|
+
|
|
279
|
+
# -- document lifecycle ----------------------------------------------
|
|
280
|
+
|
|
281
|
+
def choose_root(self) -> None:
|
|
282
|
+
"""Pick a root element (declared in the schema) and create a document."""
|
|
283
|
+
if not self._confirm_discard():
|
|
284
|
+
return
|
|
285
|
+
if not self.schema.root_names:
|
|
286
|
+
messagebox.showinfo(APP_TITLE, "The schema declares no root element.")
|
|
287
|
+
return
|
|
288
|
+
dialog = tk.Toplevel(self.root)
|
|
289
|
+
dialog.title("New document")
|
|
290
|
+
dialog.transient(self.root)
|
|
291
|
+
dialog.resizable(False, False)
|
|
292
|
+
body = ttk.Frame(dialog, padding=12)
|
|
293
|
+
body.pack(fill="both", expand=True)
|
|
294
|
+
ttk.Label(body, text="Root element (declared in schema.xsd):").pack(anchor="w")
|
|
295
|
+
choice = tk.StringVar(value=self.schema.root_names[0])
|
|
296
|
+
combo = ttk.Combobox(body, textvariable=choice, values=self.schema.root_names,
|
|
297
|
+
state="readonly", width=48)
|
|
298
|
+
combo.pack(fill="x", pady=(4, 0))
|
|
299
|
+
info = tk.Text(body, height=9, width=68, wrap="word", relief="flat",
|
|
300
|
+
background="#ffffff", padx=8, pady=6)
|
|
301
|
+
info.pack(pady=8)
|
|
302
|
+
info.configure(state="disabled")
|
|
303
|
+
|
|
304
|
+
def show_description(*_args) -> None:
|
|
305
|
+
block = self.schema.root(choice.get())
|
|
306
|
+
info.configure(state="normal")
|
|
307
|
+
info.delete("1.0", "end")
|
|
308
|
+
info.insert("1.0", describe(block) if block is not None else "")
|
|
309
|
+
info.configure(state="disabled")
|
|
310
|
+
|
|
311
|
+
combo.bind("<<ComboboxSelected>>", show_description)
|
|
312
|
+
show_description()
|
|
313
|
+
|
|
314
|
+
def confirm(_event=None) -> None:
|
|
315
|
+
dialog.destroy()
|
|
316
|
+
self.new_document(choice.get())
|
|
317
|
+
|
|
318
|
+
row = ttk.Frame(body)
|
|
319
|
+
row.pack(anchor="e")
|
|
320
|
+
ttk.Button(row, text="Cancel", command=dialog.destroy).pack(side="left", padx=4)
|
|
321
|
+
ttk.Button(row, text="Create", style="Accent.TButton", command=confirm).pack(
|
|
322
|
+
side="left", padx=4)
|
|
323
|
+
dialog.bind("<Return>", confirm)
|
|
324
|
+
dialog.bind("<Escape>", lambda _e: dialog.destroy())
|
|
325
|
+
combo.focus_set()
|
|
326
|
+
dialog.wait_visibility()
|
|
327
|
+
dialog.grab_set()
|
|
328
|
+
|
|
329
|
+
def _new_from_root(self, root_name: str) -> None:
|
|
330
|
+
if self._confirm_discard():
|
|
331
|
+
self.new_document(root_name)
|
|
332
|
+
|
|
333
|
+
def new_document(self, root_name: str) -> None:
|
|
334
|
+
try:
|
|
335
|
+
self.doc = DocumentModel.new(self.schema, root_name)
|
|
336
|
+
except Exception as exc: # noqa: BLE001 - reported to the user
|
|
337
|
+
messagebox.showerror(APP_TITLE, f"cannot create <{root_name}>:\n{exc}")
|
|
338
|
+
return
|
|
339
|
+
self.selected = None
|
|
340
|
+
self.refresh(full=False)
|
|
341
|
+
self.select(self.doc.root, force=True)
|
|
342
|
+
self.explorer.mark_current(None)
|
|
343
|
+
self.status.set(f"New <{root_name}> document — the “Required” group lists what to fill in")
|
|
344
|
+
|
|
345
|
+
def _explorer_open(self, path: Path) -> None:
|
|
346
|
+
"""Open *path* picked in the file explorer (usual dirty check)."""
|
|
347
|
+
if (self.doc is not None and self.doc.path is not None
|
|
348
|
+
and self.doc.path.resolve() == path.resolve()):
|
|
349
|
+
self.status.set("That file is already open")
|
|
350
|
+
return
|
|
351
|
+
if not self._confirm_discard():
|
|
352
|
+
return
|
|
353
|
+
self.open_path(path)
|
|
354
|
+
|
|
355
|
+
def open_file(self) -> None:
|
|
356
|
+
if not self._confirm_discard():
|
|
357
|
+
return
|
|
358
|
+
initial = str(self.doc.path.parent) if self.doc is not None and self.doc.path else str(
|
|
359
|
+
Path.cwd())
|
|
360
|
+
path = filedialog.askopenfilename(
|
|
361
|
+
title="Open XML", initialdir=initial,
|
|
362
|
+
filetypes=[("XML files", "*.xml"), ("All files", "*.*")])
|
|
363
|
+
if path:
|
|
364
|
+
self.open_path(Path(path))
|
|
365
|
+
|
|
366
|
+
def open_path(self, path: Path) -> None:
|
|
367
|
+
try:
|
|
368
|
+
self.doc = DocumentModel.load(self.schema, path)
|
|
369
|
+
except Exception as exc: # noqa: BLE001 - reported to the user
|
|
370
|
+
messagebox.showerror(APP_TITLE, f"cannot open {path}:\n{exc}")
|
|
371
|
+
return
|
|
372
|
+
self.selected = None
|
|
373
|
+
self.refresh(full=False)
|
|
374
|
+
self.select(self.doc.root, force=True)
|
|
375
|
+
self.status.set(f"Opened {path}")
|
|
376
|
+
self.explorer.mark_current(Path(path))
|
|
377
|
+
|
|
378
|
+
def save(self) -> None:
|
|
379
|
+
if self.doc is None:
|
|
380
|
+
return
|
|
381
|
+
if self.doc.path is None:
|
|
382
|
+
self.save_as()
|
|
383
|
+
return
|
|
384
|
+
try:
|
|
385
|
+
self.doc.save()
|
|
386
|
+
except Exception as exc: # noqa: BLE001
|
|
387
|
+
messagebox.showerror(APP_TITLE, f"cannot save:\n{exc}")
|
|
388
|
+
return
|
|
389
|
+
self.refresh(full=False)
|
|
390
|
+
self.status.set(f"Saved {self.doc.path}")
|
|
391
|
+
self.explorer.mark_current(self.doc.path)
|
|
392
|
+
|
|
393
|
+
def save_as(self) -> None:
|
|
394
|
+
if self.doc is None:
|
|
395
|
+
return
|
|
396
|
+
path = filedialog.asksaveasfilename(
|
|
397
|
+
title="Save XML as", defaultextension=".xml",
|
|
398
|
+
initialfile=self.doc.path.name if self.doc.path else "document.xml",
|
|
399
|
+
filetypes=[("XML files", "*.xml"), ("All files", "*.*")])
|
|
400
|
+
if path:
|
|
401
|
+
self.doc.path = Path(path)
|
|
402
|
+
self.save()
|
|
403
|
+
|
|
404
|
+
def _confirm_discard(self) -> bool:
|
|
405
|
+
if self.doc is None or not self.doc.dirty:
|
|
406
|
+
return True
|
|
407
|
+
answer = messagebox.askyesnocancel(
|
|
408
|
+
APP_TITLE, "The current document has unsaved changes. Save them?")
|
|
409
|
+
if answer is None:
|
|
410
|
+
return False
|
|
411
|
+
if answer:
|
|
412
|
+
self.save()
|
|
413
|
+
return not self.doc.dirty # save-as cancelled -> stay
|
|
414
|
+
return True
|
|
415
|
+
|
|
416
|
+
def quit(self) -> None:
|
|
417
|
+
if self._confirm_discard():
|
|
418
|
+
self.root.destroy()
|
|
419
|
+
|
|
420
|
+
# -- selection -------------------------------------------------------
|
|
421
|
+
|
|
422
|
+
def _on_tab_changed(self, _event=None) -> None:
|
|
423
|
+
"""Paint deferred widgets when their tab becomes visible.
|
|
424
|
+
|
|
425
|
+
The diagram and source tabs only paint on demand: this is what keeps
|
|
426
|
+
large documents fast — editing in the Tree tab never pays for the
|
|
427
|
+
diagram layout or the source highlighting.
|
|
428
|
+
"""
|
|
429
|
+
try:
|
|
430
|
+
current = self.notebook.nametowidget(self.notebook.select())
|
|
431
|
+
except tk.TclError:
|
|
432
|
+
return
|
|
433
|
+
if current is self.diagram:
|
|
434
|
+
if self.diagram.redraw_if_visible():
|
|
435
|
+
self.root.after_idle(lambda: self.diagram.select_element(self.selected, reveal=False))
|
|
436
|
+
elif current is self.source:
|
|
437
|
+
self.source.refresh_if_visible()
|
|
438
|
+
|
|
439
|
+
def select(self, element, force: bool = False) -> None:
|
|
440
|
+
"""Select *element* everywhere (tree, diagram, breadcrumb, inspector)."""
|
|
441
|
+
if element is None or (element is self.selected and not force):
|
|
442
|
+
return
|
|
443
|
+
self.selected = element
|
|
444
|
+
self._sync_selection()
|
|
445
|
+
|
|
446
|
+
def _sync_selection(self) -> None:
|
|
447
|
+
self.tree.select_element(self.selected)
|
|
448
|
+
self.diagram.select_element(self.selected)
|
|
449
|
+
self._update_breadcrumb()
|
|
450
|
+
self.show_contextual()
|
|
451
|
+
self._load_properties()
|
|
452
|
+
self._update_actions()
|
|
453
|
+
|
|
454
|
+
@staticmethod
|
|
455
|
+
def _chain(element) -> list:
|
|
456
|
+
chain = []
|
|
457
|
+
node = element
|
|
458
|
+
while node is not None:
|
|
459
|
+
chain.append(node)
|
|
460
|
+
node = node.getparent()
|
|
461
|
+
chain.reverse()
|
|
462
|
+
return chain
|
|
463
|
+
|
|
464
|
+
def _update_breadcrumb(self) -> None:
|
|
465
|
+
self.breadcrumb.show(self._chain(self.selected) if self.selected is not None else [])
|
|
466
|
+
|
|
467
|
+
def show_contextual(self) -> None:
|
|
468
|
+
"""Bottom palette list = what the schema allows under the selection.
|
|
469
|
+
|
|
470
|
+
The top list (schema profile) is never replaced by this.
|
|
471
|
+
"""
|
|
472
|
+
if self.doc is None:
|
|
473
|
+
self.palette.set_context("", contextual_entries(self.schema, None, []))
|
|
474
|
+
return
|
|
475
|
+
target = self.selected if self.selected is not None else self.doc.root
|
|
476
|
+
block = self.doc.block_of(target)
|
|
477
|
+
entries = contextual_entries(self.schema, block, self.doc.child_names(target))
|
|
478
|
+
self.palette.set_context(localname(str(target.tag)), entries)
|
|
479
|
+
|
|
480
|
+
# -- inserting blocks ------------------------------------------------
|
|
481
|
+
|
|
482
|
+
def on_palette_activate(self, entry) -> None:
|
|
483
|
+
"""Double-click / button in the palette."""
|
|
484
|
+
if entry.category == "Roots":
|
|
485
|
+
self._new_from_root(entry.block.name)
|
|
486
|
+
return
|
|
487
|
+
if self.doc is None:
|
|
488
|
+
self.status.set("Create or open a document first")
|
|
489
|
+
return
|
|
490
|
+
self.insert_entry(entry)
|
|
491
|
+
|
|
492
|
+
def _find_host(self, start, name: str, free_key: bool):
|
|
493
|
+
"""Nearest element (start, then its ancestors) that may receive *name*."""
|
|
494
|
+
node = start
|
|
495
|
+
while node is not None:
|
|
496
|
+
block = self.doc.block_of(node)
|
|
497
|
+
if block is not None:
|
|
498
|
+
particle = block.child(name)
|
|
499
|
+
if particle is not None:
|
|
500
|
+
if can_add(particle, self.doc.child_names(node).count(name)):
|
|
501
|
+
return node
|
|
502
|
+
elif free_key and block.allows_any and node is start:
|
|
503
|
+
return node
|
|
504
|
+
node = node.getparent()
|
|
505
|
+
return None
|
|
506
|
+
|
|
507
|
+
def insert_entry(self, entry, target=None) -> None:
|
|
508
|
+
"""Insert a palette block, climbing to the nearest ancestor that accepts it."""
|
|
509
|
+
name = entry.block.name
|
|
510
|
+
free_key = name == "*"
|
|
511
|
+
if free_key:
|
|
512
|
+
name = (simpledialog.askstring(
|
|
513
|
+
APP_TITLE, "Element name (this content model is open: xs:any):",
|
|
514
|
+
parent=self.root) or "").strip()
|
|
515
|
+
if not name:
|
|
516
|
+
return
|
|
517
|
+
start = target if target is not None else (
|
|
518
|
+
self.selected if self.selected is not None else self.doc.root)
|
|
519
|
+
host = self._find_host(start, name, free_key)
|
|
520
|
+
element = self.doc.add_child(host, name) if host is not None else None
|
|
521
|
+
if element is None:
|
|
522
|
+
messagebox.showinfo(
|
|
523
|
+
APP_TITLE,
|
|
524
|
+
f"<{name}> cannot be inserted here: the schema does not allow it under "
|
|
525
|
+
f"<{localname(str(start.tag))}> (or its parents), or the maximum number "
|
|
526
|
+
"of occurrences is reached.")
|
|
527
|
+
return
|
|
528
|
+
self.selected = element
|
|
529
|
+
self.refresh()
|
|
530
|
+
self._sync_selection()
|
|
531
|
+
self.status.set(f"Added <{name}> in <{localname(str(host.tag))}>")
|
|
532
|
+
|
|
533
|
+
def on_property_change(self, element, kind: str, name: str, value: str) -> None:
|
|
534
|
+
"""Commit an inspector edit through the document model."""
|
|
535
|
+
if self.doc is None:
|
|
536
|
+
return
|
|
537
|
+
if kind == "attribute":
|
|
538
|
+
changed = self.doc.set_attribute(element, name, value)
|
|
539
|
+
else:
|
|
540
|
+
changed = self.doc.set_text(element, value)
|
|
541
|
+
if changed:
|
|
542
|
+
self.selected = element
|
|
543
|
+
self._keep_panel = True # keep the fields (and Tab focus) alive
|
|
544
|
+
try:
|
|
545
|
+
self.refresh()
|
|
546
|
+
finally:
|
|
547
|
+
self._keep_panel = False
|
|
548
|
+
|
|
549
|
+
# -- context menu ----------------------------------------------------
|
|
550
|
+
|
|
551
|
+
def popup_menu(self, element, event) -> None:
|
|
552
|
+
"""Right-click menu shared by the tree and the diagram."""
|
|
553
|
+
if self.doc is None:
|
|
554
|
+
return
|
|
555
|
+
self.select(element)
|
|
556
|
+
menu = tk.Menu(self.root, tearoff=False)
|
|
557
|
+
block = self.doc.block_of(element)
|
|
558
|
+
entries = contextual_entries(self.schema, block, self.doc.child_names(element))
|
|
559
|
+
if entries:
|
|
560
|
+
add = tk.Menu(menu, tearoff=False)
|
|
561
|
+
previous = None
|
|
562
|
+
for entry in entries:
|
|
563
|
+
if previous is not None and entry.category != previous:
|
|
564
|
+
add.add_separator()
|
|
565
|
+
previous = entry.category
|
|
566
|
+
label = ("★ " if entry.category == "Required" else "") + entry.label
|
|
567
|
+
add.add_command(label=label,
|
|
568
|
+
command=lambda e=entry, t=element: self.insert_entry(e, t))
|
|
569
|
+
menu.add_cascade(label="Add block inside", menu=add)
|
|
570
|
+
else:
|
|
571
|
+
menu.add_command(label="Add block inside", state="disabled")
|
|
572
|
+
menu.add_separator()
|
|
573
|
+
is_root = element.getparent() is None
|
|
574
|
+
menu.add_command(label="Duplicate", accelerator="Ctrl+D", command=self.duplicate_block,
|
|
575
|
+
state="disabled" if is_root else "normal")
|
|
576
|
+
menu.add_command(label="Move up", accelerator="Alt+Up",
|
|
577
|
+
command=lambda: self.move_block(-1))
|
|
578
|
+
menu.add_command(label="Move down", accelerator="Alt+Down",
|
|
579
|
+
command=lambda: self.move_block(1))
|
|
580
|
+
menu.add_separator()
|
|
581
|
+
menu.add_command(label="Remove", accelerator="Del", command=self.remove_block,
|
|
582
|
+
state="normal" if self._can_remove(element)[0] else "disabled")
|
|
583
|
+
try:
|
|
584
|
+
menu.tk_popup(event.x_root, event.y_root)
|
|
585
|
+
finally:
|
|
586
|
+
menu.grab_release()
|
|
587
|
+
|
|
588
|
+
# -- commands --------------------------------------------------------
|
|
589
|
+
|
|
590
|
+
def add_block(self) -> None:
|
|
591
|
+
if self.doc is None:
|
|
592
|
+
return
|
|
593
|
+
entry = self.palette.selected_entry()
|
|
594
|
+
if entry is None:
|
|
595
|
+
self.status.set("Pick a block in the palette first (double-click adds it)")
|
|
596
|
+
return
|
|
597
|
+
self.on_palette_activate(entry)
|
|
598
|
+
|
|
599
|
+
def _can_remove(self, element) -> tuple[bool, str]:
|
|
600
|
+
if self.doc is None or element is None:
|
|
601
|
+
return False, ""
|
|
602
|
+
if element.getparent() is None:
|
|
603
|
+
return False, "the document root cannot be removed"
|
|
604
|
+
return self.doc.can_remove(element)
|
|
605
|
+
|
|
606
|
+
def remove_block(self) -> None:
|
|
607
|
+
if self.doc is None or self.selected is None:
|
|
608
|
+
return
|
|
609
|
+
allowed, reason = self._can_remove(self.selected)
|
|
610
|
+
if not allowed:
|
|
611
|
+
messagebox.showinfo(APP_TITLE, reason)
|
|
612
|
+
return
|
|
613
|
+
parent = self.selected.getparent()
|
|
614
|
+
name = localname(str(self.selected.tag))
|
|
615
|
+
if self.doc.remove(self.selected):
|
|
616
|
+
self.selected = parent
|
|
617
|
+
self.refresh()
|
|
618
|
+
self._sync_selection()
|
|
619
|
+
self.status.set(f"Removed <{name}>")
|
|
620
|
+
|
|
621
|
+
def duplicate_block(self) -> None:
|
|
622
|
+
if self.doc is None or self.selected is None:
|
|
623
|
+
return
|
|
624
|
+
clone = self.doc.duplicate(self.selected)
|
|
625
|
+
if clone is None:
|
|
626
|
+
messagebox.showinfo(APP_TITLE, "The schema allows only one occurrence here "
|
|
627
|
+
"(the root cannot be duplicated).")
|
|
628
|
+
return
|
|
629
|
+
self.selected = clone
|
|
630
|
+
self.refresh()
|
|
631
|
+
self._sync_selection()
|
|
632
|
+
self.status.set(f"Duplicated <{localname(str(clone.tag))}>")
|
|
633
|
+
|
|
634
|
+
def move_block(self, delta: int) -> None:
|
|
635
|
+
if self.doc is None or self.selected is None:
|
|
636
|
+
return
|
|
637
|
+
if self.doc.move(self.selected, delta):
|
|
638
|
+
self.refresh()
|
|
639
|
+
|
|
640
|
+
def undo(self) -> None:
|
|
641
|
+
if self.doc is not None and self.doc.undo():
|
|
642
|
+
self.refresh()
|
|
643
|
+
self.status.set("Undo")
|
|
644
|
+
|
|
645
|
+
def redo(self) -> None:
|
|
646
|
+
if self.doc is not None and self.doc.redo():
|
|
647
|
+
self.refresh()
|
|
648
|
+
self.status.set("Redo")
|
|
649
|
+
|
|
650
|
+
def auto_fix(self) -> None:
|
|
651
|
+
"""One click: repair every issue the block model can fix by itself."""
|
|
652
|
+
if self.doc is None:
|
|
653
|
+
self.status.set("Create or open a document first — "
|
|
654
|
+
"then Auto-fix repairs it in one click")
|
|
655
|
+
return
|
|
656
|
+
stats = self.doc.auto_fix()
|
|
657
|
+
self.refresh(full=True)
|
|
658
|
+
parts = []
|
|
659
|
+
if stats["blocks"]:
|
|
660
|
+
parts.append(f"+{stats['blocks']} required block(s)")
|
|
661
|
+
if stats["attributes"]:
|
|
662
|
+
parts.append(f"+{stats['attributes']} required attribute(s)")
|
|
663
|
+
if stats["values"]:
|
|
664
|
+
parts.append(f"{stats['values']} value(s) corrected")
|
|
665
|
+
if stats["removed"]:
|
|
666
|
+
parts.append(f"-{stats['removed']} extra occurrence(s)")
|
|
667
|
+
message = "Auto-fix: " + (
|
|
668
|
+
" · ".join(parts) if parts else "nothing to fix — already clean")
|
|
669
|
+
if stats["unfixed"]:
|
|
670
|
+
message += (f" · {stats['unfixed']} item(s) need manual attention "
|
|
671
|
+
"(see Problems)")
|
|
672
|
+
self.status.set(message)
|
|
673
|
+
|
|
674
|
+
def validate_all(self) -> None:
|
|
675
|
+
"""Run the authoritative XSD pass (same validator as the pipeline)."""
|
|
676
|
+
if self.doc is None:
|
|
677
|
+
return
|
|
678
|
+
self.issues = self.doc.validate(self.schema.path)
|
|
679
|
+
self.refresh(full=True, keep_issues=True)
|
|
680
|
+
errors = [i for i in self.issues if i.severity == "error"]
|
|
681
|
+
self.status.set(f"XSD validation: {len(errors)} error(s), "
|
|
682
|
+
f"{len(self.issues) - len(errors)} warning(s)")
|
|
683
|
+
|
|
684
|
+
def show_schema_summary(self) -> None:
|
|
685
|
+
profile = self.schema.profile()
|
|
686
|
+
lines = [f"Schema: {self.schema.path}",
|
|
687
|
+
f"target namespace: {self.schema.target_namespace or '(none)'}",
|
|
688
|
+
"roots: {}".format(", ".join(self.schema.root_names)),
|
|
689
|
+
f"blocks: {len(self.schema.blocks)}", "",
|
|
690
|
+
"Profile:"]
|
|
691
|
+
lines.extend(f" {name:<30} {len(blocks)}" for name, blocks in profile.items())
|
|
692
|
+
messagebox.showinfo(APP_TITLE, "\n".join(lines))
|
|
693
|
+
|
|
694
|
+
def show_about(self) -> None:
|
|
695
|
+
messagebox.showinfo(
|
|
696
|
+
APP_TITLE,
|
|
697
|
+
"Visual XML block editor for Flipova Foundation.\n\n"
|
|
698
|
+
"The XSD (design/schema.xsd) is the single source of truth: roots, "
|
|
699
|
+
"blocks, cardinalities, attributes, enumerations and documentation "
|
|
700
|
+
"are all read from it at runtime, so any schema-valid XML is editable "
|
|
701
|
+
"with no code change.\n\n"
|
|
702
|
+
"Blocks nest like the schema content model; validation reuses "
|
|
703
|
+
"design/tools/sources/linter.py, the same validator as the CLI pipeline.")
|
|
704
|
+
|
|
705
|
+
# -- view refresh ----------------------------------------------------
|
|
706
|
+
|
|
707
|
+
def refresh(self, full: bool = False, keep_issues: bool = False) -> None:
|
|
708
|
+
"""Recompute diagnostics and repaint every view from the live buffer."""
|
|
709
|
+
path = self._selection_path()
|
|
710
|
+
if self.doc is None:
|
|
711
|
+
self.issues = []
|
|
712
|
+
elif not keep_issues:
|
|
713
|
+
self.issues = (self.doc.validate(self.schema.path) if full
|
|
714
|
+
else self.doc.structural_issues())
|
|
715
|
+
self._refresh_views(path)
|
|
716
|
+
|
|
717
|
+
def _selection_path(self):
|
|
718
|
+
"""Positional path of the selection: None = nothing, () = the root."""
|
|
719
|
+
node = self.selected
|
|
720
|
+
if self.doc is None or node is None:
|
|
721
|
+
return None
|
|
722
|
+
parts = []
|
|
723
|
+
while node is not None and node.getparent() is not None:
|
|
724
|
+
siblings = [c for c in node.getparent() if isinstance(c.tag, str)]
|
|
725
|
+
if node not in siblings:
|
|
726
|
+
return None
|
|
727
|
+
parts.append(siblings.index(node))
|
|
728
|
+
node = node.getparent()
|
|
729
|
+
return tuple(reversed(parts))
|
|
730
|
+
|
|
731
|
+
def _resolve_path(self, path):
|
|
732
|
+
if self.doc is None or path is None:
|
|
733
|
+
return None
|
|
734
|
+
node = self.doc.root
|
|
735
|
+
for index in path:
|
|
736
|
+
children = [c for c in node if isinstance(c.tag, str)]
|
|
737
|
+
if index >= len(children):
|
|
738
|
+
return None
|
|
739
|
+
node = children[index]
|
|
740
|
+
return node
|
|
741
|
+
|
|
742
|
+
def _refresh_views(self, path) -> None:
|
|
743
|
+
doc = self.doc
|
|
744
|
+
self.tree.refresh(doc, self.issues)
|
|
745
|
+
self.diagram.render(doc, self.issues)
|
|
746
|
+
self.source.show(doc.to_string() if doc is not None else "")
|
|
747
|
+
self._fill_issues(self.issues)
|
|
748
|
+
self.selected = self._resolve_path(path)
|
|
749
|
+
if self.selected is not None:
|
|
750
|
+
self.tree.select_element(self.selected)
|
|
751
|
+
self.diagram.select_element(self.selected, reveal=False)
|
|
752
|
+
self._update_breadcrumb()
|
|
753
|
+
self.show_contextual()
|
|
754
|
+
if self._keep_panel:
|
|
755
|
+
self.properties.set_issues(self._relevant_issues())
|
|
756
|
+
else:
|
|
757
|
+
self._load_properties()
|
|
758
|
+
self._update_status()
|
|
759
|
+
self._update_actions()
|
|
760
|
+
self._update_empty_state()
|
|
761
|
+
|
|
762
|
+
def _on_tab_changed(self, _event=None) -> None:
|
|
763
|
+
"""Paint whatever the newly visible tab deferred while it was hidden."""
|
|
764
|
+
try:
|
|
765
|
+
widget = self.root.nametowidget(str(self.notebook.select()))
|
|
766
|
+
except (tk.TclError, KeyError):
|
|
767
|
+
return
|
|
768
|
+
if widget is self.diagram:
|
|
769
|
+
if self.diagram.redraw_if_visible() and self.selected is not None:
|
|
770
|
+
self.diagram.select_element(self.selected)
|
|
771
|
+
elif widget is self.source:
|
|
772
|
+
self.source.refresh_if_visible()
|
|
773
|
+
|
|
774
|
+
def _relevant_issues(self) -> list[Issue]:
|
|
775
|
+
if self.selected is None:
|
|
776
|
+
return []
|
|
777
|
+
return [issue for issue in self.issues if issue.element is self.selected]
|
|
778
|
+
|
|
779
|
+
def _load_properties(self) -> None:
|
|
780
|
+
self.properties.load(self.doc, self.selected, self._relevant_issues())
|
|
781
|
+
|
|
782
|
+
def _path_label(self, element) -> str:
|
|
783
|
+
names = [localname(str(node.tag)) for node in self._chain(element)]
|
|
784
|
+
return " › ".join(names if len(names) <= 3 else ["…"] + names[-3:])
|
|
785
|
+
|
|
786
|
+
def _on_tab_changed(self, _event=None) -> None:
|
|
787
|
+
"""Paint whatever the newly visible tab has been waiting for.
|
|
788
|
+
|
|
789
|
+
Heavy first-fits run while the user is looking at a fast view, not
|
|
790
|
+
while the window is still opening — the tab event is the earliest
|
|
791
|
+
moment we know the widget has a real size.
|
|
792
|
+
"""
|
|
793
|
+
try:
|
|
794
|
+
selected = self.notebook.nametowidget(self.notebook.select())
|
|
795
|
+
except tk.TclError:
|
|
796
|
+
return
|
|
797
|
+
if selected is self.diagram:
|
|
798
|
+
self.diagram.redraw_if_visible()
|
|
799
|
+
elif selected is self.source:
|
|
800
|
+
self.source.refresh_if_visible()
|
|
801
|
+
|
|
802
|
+
def _fill_issues(self, issues: list[Issue]) -> None:
|
|
803
|
+
self.issues_tree.delete(*self.issues_tree.get_children())
|
|
804
|
+
self._issue_map.clear()
|
|
805
|
+
errors = sum(1 for i in issues if i.severity == "error")
|
|
806
|
+
warnings = len(issues) - errors
|
|
807
|
+
if self.doc is None:
|
|
808
|
+
self.problem_summary.configure(text="")
|
|
809
|
+
elif not issues:
|
|
810
|
+
self.problem_summary.configure(text="✔ No problem detected", foreground="#248a3d")
|
|
811
|
+
else:
|
|
812
|
+
self.problem_summary.configure(
|
|
813
|
+
text=f"{errors} error(s) · {warnings} warning(s) — click a row to jump to the block",
|
|
814
|
+
foreground=ISSUE_TEXT["error"] if errors else ISSUE_TEXT["warning"])
|
|
815
|
+
for index, issue in enumerate(issues):
|
|
816
|
+
if issue.severity != "error" and not self.show_warnings.get():
|
|
817
|
+
continue
|
|
818
|
+
iid = f"i{index}"
|
|
819
|
+
if issue.element is not None:
|
|
820
|
+
where = self._path_label(issue.element)
|
|
821
|
+
elif issue.line is not None:
|
|
822
|
+
where = f"line {issue.line}"
|
|
823
|
+
else:
|
|
824
|
+
where = ""
|
|
825
|
+
self.issues_tree.insert(
|
|
826
|
+
"", "end", iid=iid,
|
|
827
|
+
values=(f"{ISSUE_MARKS.get(issue.severity, '•')} {issue.severity}",
|
|
828
|
+
where, issue.message),
|
|
829
|
+
tags=(issue.severity,))
|
|
830
|
+
self._issue_map[iid] = issue
|
|
831
|
+
|
|
832
|
+
def _on_issue_activate(self, _event=None) -> None:
|
|
833
|
+
selection = self.issues_tree.selection()
|
|
834
|
+
if not selection:
|
|
835
|
+
return
|
|
836
|
+
issue = self._issue_map.get(selection[0])
|
|
837
|
+
if issue is not None and issue.element is not None:
|
|
838
|
+
self.select(issue.element, force=True)
|
|
839
|
+
|
|
840
|
+
def _update_status(self) -> None:
|
|
841
|
+
if self.doc is None:
|
|
842
|
+
self.root.title(APP_TITLE)
|
|
843
|
+
self.counts.set("")
|
|
844
|
+
return
|
|
845
|
+
location = str(self.doc.path) if self.doc.path else "(unsaved)"
|
|
846
|
+
self.root.title("{} — {}{}".format(APP_TITLE, location, " *" if self.doc.dirty else ""))
|
|
847
|
+
blocks = len(self.doc.elements_by_line())
|
|
848
|
+
errors = sum(1 for issue in self.issues if issue.severity == "error")
|
|
849
|
+
self.counts.set(f"{blocks} block(s) · {errors} error(s) · "
|
|
850
|
+
f"{len(self.issues) - errors} warning(s)")
|
|
851
|
+
|
|
852
|
+
def _update_actions(self) -> None:
|
|
853
|
+
"""Enable only the toolbar buttons that make sense right now."""
|
|
854
|
+
doc, sel = self.doc, self.selected
|
|
855
|
+
|
|
856
|
+
def state(key: str, enabled: bool) -> None:
|
|
857
|
+
button = self._actions.get(key)
|
|
858
|
+
if button is not None:
|
|
859
|
+
button.configure(state="normal" if enabled else "disabled")
|
|
860
|
+
|
|
861
|
+
has_sel = doc is not None and sel is not None
|
|
862
|
+
parent = sel.getparent() if has_sel else None
|
|
863
|
+
index = count = 0
|
|
864
|
+
if parent is not None:
|
|
865
|
+
siblings = [c for c in parent if isinstance(c.tag, str)]
|
|
866
|
+
if sel in siblings:
|
|
867
|
+
index, count = siblings.index(sel), len(siblings)
|
|
868
|
+
state("save", doc is not None)
|
|
869
|
+
state("undo", doc is not None and doc.can_undo)
|
|
870
|
+
state("redo", doc is not None and doc.can_redo)
|
|
871
|
+
state("add", doc is not None)
|
|
872
|
+
state("dup", has_sel and parent is not None)
|
|
873
|
+
state("remove", has_sel and self._can_remove(sel)[0])
|
|
874
|
+
state("up", has_sel and parent is not None and index > 0)
|
|
875
|
+
state("down", has_sel and parent is not None and index < count - 1)
|
|
876
|
+
state("fix", doc is not None)
|
|
877
|
+
state("validate", doc is not None)
|
|
878
|
+
|
|
879
|
+
def _on_tab_changed(self, _event=None) -> None:
|
|
880
|
+
"""Paint whatever the newly selected centre tab deferred while hidden."""
|
|
881
|
+
try:
|
|
882
|
+
selected = self.notebook.select()
|
|
883
|
+
except tk.TclError:
|
|
884
|
+
return
|
|
885
|
+
if self.notebook.index(selected) == 1: # Diagram tab
|
|
886
|
+
self.diagram.redraw_if_visible()
|
|
887
|
+
elif self.notebook.index(selected) == 2: # Source tab
|
|
888
|
+
self.source.refresh_if_visible()
|
|
889
|
+
|
|
890
|
+
def _update_empty_state(self) -> None:
|
|
891
|
+
if self.doc is None:
|
|
892
|
+
self.empty.place(in_=self.notebook, x=0, y=0, relwidth=1, relheight=1)
|
|
893
|
+
self.empty.lift()
|
|
894
|
+
else:
|
|
895
|
+
self.empty.place_forget()
|
|
896
|
+
|
|
897
|
+
# -- entry point -----------------------------------------------------
|
|
898
|
+
|
|
899
|
+
def run(self) -> int:
|
|
900
|
+
self.root.mainloop()
|
|
901
|
+
return 0
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
def launch(schema: SchemaModel, path: Path | None = None) -> int:
|
|
905
|
+
"""Create the application and enter its main loop."""
|
|
906
|
+
return XmlEditorApp(schema, path).run()
|