ferrocad 0.1.4

FerroCAD: a Rust reimplementation of FreeCAD's App core, exposed as the FreeCAD Python package
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
"""Headless drivers for the four remaining Draft forms (Q5).

FerroCAD does not render Qt. ``FreeCADGui.PySideUic.loadUi`` returns a
``FormProxy`` model instead (Q2a), and this module drives that model: it seeds
the widgets from the Draft parameter group and from the active document, then
reads them back on accept. There is no window, no event loop and no Qt import.

The four commands registered at import time are upstream's remaining Draft
forms:

============================ ==========================================
Command                      Form
============================ ==========================================
``Draft_SetStyle``           ``TaskPanel_SetStyle.ui``
``Draft_Hatch``              ``dialogHatch.ui``
``Draft_AnnotationStyleEditor`` ``dialog_AnnotationStyleEditor.ui``
``Draft_Scale``              ``TaskPanel_Scale.ui`` (local, converted)
============================ ==========================================

``TaskPanel_Scale.ui`` is the one panel upstream builds in code
(``drafttaskpanels/task_scale.py``); the local copy is the declarative
equivalent described by ``docs/workbenches/qt-strategy.md`` ยง2.5. The other
three files are read from the pinned upstream checkout, resolved exactly as the
conformance harness resolves it (``FERROCAD_UPSTREAM``, then the sibling
``freecad-upstream`` of this repository). An explicit path always wins.

Programmatic entry point
------------------------

``open_panel(name, path=None)`` loads a form and returns a ``PanelController``:

``panel.form``
    The ``FormProxy``; widget names are the ``.ui`` object names.
``panel.fields()``
    JSON-ready field descriptions for a shell dialog spec.
``panel.values()``
    The current form values, in the same shape ``fields()`` reports.
``panel.apply(values)``
    Write shell values into the form (unknown names raise ``ValueError``).
``panel.accept(values=None)``
    Apply (optionally), commit, and return ``{"status": ..., "value": ...}``.
``panel.reject()``
    Discard the form without writing anything.

The SetStyle panel also exposes ``presets()``, ``save_preset(name)``,
``delete_preset(name)``, ``load_preset(name)`` and ``select_preset(name)``. The
annotation editor exposes ``styles()``, ``style_names()``, ``select_style(name)``,
``save_style(name)`` and ``editor_values()``. The hatch panel exposes
``patterns()``.

What is deliberately *not* modelled
-----------------------------------

* The forms are never rendered; layout/tab/scroll widgets are loaded (so their
  widget counts and names match upstream) but have no behaviour here.
* The annotation editor cannot push a style into live annotation objects: the
  headless ``ViewObject`` models only the shape-view properties
  (``ShapeColor``, ``Transparency``, ``DisplayMode``, ...), not upstream's
  ``FontName``/``TextColor``/``AnnotationStyle``. Accept therefore persists the
  style in the document's ``Meta`` store and in the Draft parameter group, and
  reports that no annotation view objects were updated. This is an explicit
  limitation, not a silent no-op.
* Upstream stores SetStyle presets in ``<user>/Draft/StylePresets.json``. Here
  they live in the Draft parameter group as one JSON string, as Q5 requires.
* ``QFontComboBox`` has no font enumeration in the proxy, so seeding a font
  adds that family to the combo's items before selecting it.
* The SetStyle ``applyButton``/``annotButton`` and the preset ``saveButton``
  are not wired to signals. The former need the view properties upstream's
  ``ViewProvider`` exposes (``LineColor``, ``LineWidth``, ``PointColor``,
  ``DrawStyle``, annotation fonts, ...); the headless ``ViewObject`` models only
  ``ShapeColor``, ``Transparency``, ``ShapeAppearance`` and ``DisplayMode``, so
  a partial application would be worse than an explicit gap. The latter needs
  ``QInputDialog``, which the shim raises on. Use :meth:`SetStylePanel.save_preset`
  and :meth:`SetStylePanel.select_preset` instead.
"""

from __future__ import annotations

import json
import os
from pathlib import Path
from typing import NamedTuple

import FreeCAD
import FreeCADGui

#: The Draft preference group every command reads and writes.
DRAFT_PREFERENCES = "User parameter:BaseApp/Preferences/Mod/Draft"

#: The preferred key of the SetStyle preset store (a JSON object as a string).
PRESET_KEY = "StylePresets"

#: The four commands this module owns.
COMMANDS = (
    "Draft_SetStyle",
    "Draft_Hatch",
    "Draft_AnnotationStyleEditor",
    "Draft_Scale",
)

#: The upstream form each non-local command drives.
UPSTREAM_FORMS = {
    "Draft_SetStyle": "TaskPanel_SetStyle.ui",
    "Draft_Hatch": "dialogHatch.ui",
    "Draft_AnnotationStyleEditor": "dialog_AnnotationStyleEditor.ui",
    # Converted from code; the local copy is authoritative for this one.
    "Draft_Scale": "TaskPanel_Scale.ui",
}

#: Forms shipped with FerroCAD rather than read from the upstream checkout.
LOCAL_FORMS = frozenset({"Draft_Scale"})

_MODULE_DIR = Path(__file__).resolve().parent
_LOCAL_UI_DIR = _MODULE_DIR / "Resources" / "ui"

#: Upstream's arrow/dimension enumerations (``draftutils/utils.py``).
ARROW_TYPES = ("Dot", "Circle", "Arrow", "Tick", "Tick-2", "None")


# ---------------------------------------------------------------------------
# Locating the .ui files
# ---------------------------------------------------------------------------


def _find_ui_dir(root: Path):
    """Return the Draft ``Resources/ui`` directory under ``root``, or ``None``."""
    for candidate in (
        root / "src" / "Mod" / "Draft",
        root / "Mod" / "Draft",
        root,
    ):
        ui_dir = candidate / "Resources" / "ui"
        if ui_dir.is_dir():
            return ui_dir
    return None


def _candidate_upstream_roots():
    """The checkout locations the conformance harness also understands."""
    override = os.environ.get("FERROCAD_UPSTREAM")
    if override:
        yield Path(override).expanduser()
    # ``python/FerroCADGui/forms.py`` -> repository root.
    repository = _MODULE_DIR.parents[1]
    yield repository.parent / "freecad-upstream"
    yield repository.parent.parent / "freecad-upstream"
    yield repository / "freecad-upstream"


def ui_directory(path=None) -> Path:
    """Resolve a Draft ``Resources/ui`` directory.

    ``path`` wins when given: it may name the directory, the Draft module or
    the upstream checkout root. Otherwise ``FERROCAD_UPSTREAM`` and the sibling
    ``freecad-upstream`` of this repository are tried. A missing directory is an
    actionable ``FileNotFoundError``, never an empty form.
    """
    if path is not None:
        candidate = Path(path).expanduser()
        if candidate.is_file():
            candidate = candidate.parent
        # A Draft module directory, the ui directory itself, or a checkout.
        for check in (candidate / "Resources" / "ui", candidate):
            if check.is_dir() and any(check.glob("*.ui")):
                return check
        found = _find_ui_dir(candidate)
        if found is not None:
            return found
        raise FileNotFoundError(
            f"Draft forms: {path!r} is not a Draft Resources/ui directory "
            "and contains no Draft .ui files"
        )

    tried = []
    for root in _candidate_upstream_roots():
        tried.append(str(root))
        found = _find_ui_dir(root)
        if found is not None:
            return found
    raise FileNotFoundError(
        "Draft forms: cannot find the upstream Draft .ui resources. "
        "Set FERROCAD_UPSTREAM to the freecad-upstream checkout, or pass an "
        "explicit path. Tried: " + ", ".join(tried)
    )


def ui_path(name: str, path=None) -> Path:
    """Resolve one form's ``.ui`` file.

    ``name`` is a command id (``"Draft_Hatch"``) or a file name. ``path`` may be
    the file itself, its directory, or the upstream checkout; it always wins.
    ``TaskPanel_Scale.ui`` resolves against the local ``Resources/ui`` unless an
    explicit path is given.
    """
    filename = UPSTREAM_FORMS.get(name, name)

    if path is not None:
        candidate = Path(path).expanduser()
        if candidate.is_dir():
            direct = candidate / filename
            if direct.is_file():
                return direct
            candidate = ui_directory(candidate) / filename
        if not candidate.is_file():
            raise FileNotFoundError(
                f"Draft forms: no form {filename!r} at {candidate}"
            )
        return candidate

    if name in LOCAL_FORMS or name == filename:
        local = _LOCAL_UI_DIR / filename
        if local.is_file():
            return local

    directory = ui_directory()
    candidate = directory / filename
    if not candidate.is_file():
        raise FileNotFoundError(
            f"Draft forms: {filename!r} is missing from {directory}; "
            "the upstream checkout may be incomplete"
        )
    return candidate


# ---------------------------------------------------------------------------
# Parameter and colour helpers
# ---------------------------------------------------------------------------


def _draft_group():
    """A fresh handle on the Draft preference group."""
    return FreeCAD.ParamGet(DRAFT_PREFERENCES)


def _rgba_int(value) -> int:
    """Pack an ``(r, g, b, a)`` proxy colour into a FreeCAD RGBA integer."""
    red, green, blue, alpha = (int(component) & 0xFF for component in value)
    return (red << 24) | (green << 16) | (blue << 8) | alpha


def _rgba_tuple(value) -> tuple:
    """Unpack a FreeCAD RGBA integer into the proxy's tuple form."""
    value = int(value) & 0xFFFFFFFF
    return (
        (value >> 24) & 0xFF,
        (value >> 16) & 0xFF,
        (value >> 8) & 0xFF,
        value & 0xFF,
    )


def _parse_hex_colour(text: str) -> int:
    text = str(text).strip().lstrip("#")
    if len(text) == 6:
        text += "ff"
    if len(text) != 8:
        raise ValueError(f"expected a #rrggbb or #rrggbbaa colour, got {text!r}")
    return int(text, 16)


def _hex_colour(value: int) -> str:
    return "#%08x" % (int(value) & 0xFFFFFFFF)


class _Field(NamedTuple):
    """One form field: widget name, label, value kind and parameter key."""

    name: str
    label: str
    kind: str
    param: str = ""
    default: object = None


#: Field kinds map onto the shell's dialog kinds in ``fields()``.
_SHELL_KIND = {
    "color": "text",
    "font": "text",
    "file": "text",
}


# ---------------------------------------------------------------------------
# Generic form access
# ---------------------------------------------------------------------------


def _read_storage(form, field: _Field):
    """Read a field from the form in its storage form (colours as ints)."""
    widget = getattr(form, field.name)
    kind = field.kind
    if kind == "color":
        return _rgba_int(widget.property("color"))
    if kind == "int":
        return int(widget.value())
    if kind == "float":
        return float(widget.value())
    if kind == "quantity":
        return float(widget.property("rawValue"))
    if kind == "bool":
        return bool(widget.isChecked())
    if kind == "text":
        return str(widget.text())
    if kind == "font":
        return str(widget.currentText())
    if kind == "enum":
        return int(widget.currentIndex())
    if kind == "file":
        return str(widget.fileName())
    raise ValueError(f"unknown field kind {kind!r} for {field.name!r}")


def _write_storage(form, field: _Field, value, *, strict: bool = False) -> None:
    """Write a storage-form value into the form.

    ``strict`` refuses an enum index outside the combo's items (a shell value);
    the seeding path clamps instead, so a stale preference still loads.
    """
    widget = getattr(form, field.name)
    kind = field.kind
    if kind == "color":
        widget.setProperty("color", _rgba_tuple(int(value)))
    elif kind == "quantity":
        widget.setProperty("rawValue", float(value))
    elif kind in ("int", "float"):
        widget.setValue(value)
    elif kind == "bool":
        widget.setChecked(bool(value))
    elif kind == "text":
        widget.setText(str(value))
    elif kind == "font":
        _set_combo_text(widget, str(value))
    elif kind == "enum":
        _set_combo_index(widget, int(value), strict=strict)
    elif kind == "file":
        widget.setFileName(str(value))
    else:  # pragma: no cover - the table above is closed
        raise ValueError(f"unknown field kind {kind!r} for {field.name!r}")


def _set_combo_text(widget, text: str) -> None:
    """Select ``text`` in a font combo, adding it first when necessary.

    The proxy has no font enumeration, so the preferred family must be present
    as an item before ``setCurrentText`` can select it.
    """
    items = [widget.itemText(index) for index in range(widget.count())]
    if text not in items:
        widget.addItem(text)
    widget.setCurrentText(text)


def _set_combo_index(widget, index: int, *, strict: bool) -> None:
    count = widget.count()
    if count == 0:
        if strict and index >= 0:
            raise ValueError(f"{widget.objectName()}: the combo box has no items")
        return
    if index < -1 or index >= count:
        if strict:
            raise ValueError(
                f"{widget.objectName()}: index {index} is outside 0..{count - 1}"
            )
        index = count - 1 if index >= count else -1
    widget.setCurrentIndex(index)


def _combo_text(widget) -> str:
    index = widget.currentIndex()
    if 0 <= index < widget.count():
        return widget.itemText(index)
    return widget.currentText()


def _combo_items(widget) -> list:
    return [widget.itemText(index) for index in range(widget.count())]


def _set_combo_items(widget, items) -> None:
    current = _combo_text(widget)
    widget.clear()
    widget.addItems(list(items))
    if current in items:
        widget.setCurrentText(current)


def _json_value(form, field: _Field):
    """A shell-shaped value: enum as text, colour as ``#rrggbbaa``."""
    value = _read_storage(form, field)
    if field.kind == "color":
        return _hex_colour(value)
    if field.kind == "enum":
        widget = getattr(form, field.name)
        index = int(value)
        if 0 <= index < widget.count():
            return widget.itemText(index)
        return widget.currentText()
    return value


def _from_json(form, field: _Field, value):
    """Convert a shell-shaped value into storage form."""
    kind = field.kind
    if kind == "color":
        return value if isinstance(value, int) else _parse_hex_colour(value)
    if kind == "enum":
        widget = getattr(form, field.name)
        wanted = str(value)
        for index, item in enumerate(_combo_items(widget)):
            if item == wanted:
                return index
        if wanted == "":
            return -1
        raise ValueError(
            f"{field.name}: {wanted!r} is not one of {_combo_items(widget)!r}"
        )
    if kind == "int":
        return int(value)
    if kind in ("float", "quantity"):
        return float(value)
    if kind == "bool":
        if isinstance(value, str):
            return value.strip().lower() in ("1", "true", "yes", "on")
        return bool(value)
    return str(value)


def _field_json(form, field: _Field) -> dict:
    item = {
        "name": field.name,
        "label": field.label,
        "kind": _SHELL_KIND.get(field.kind, field.kind),
        "value": _json_value(form, field),
    }
    if field.kind == "enum":
        item["values"] = _combo_items(getattr(form, field.name))
    elif field.kind in ("int", "float"):
        widget = getattr(form, field.name)
        item["min"] = widget.minimum()
        item["max"] = widget.maximum()
    return item


def _read_parameter(field: _Field):
    group = _draft_group()
    if field.kind == "color":
        return group.GetUnsigned(field.param, int(field.default))
    if field.kind in ("int", "enum"):
        return group.GetInt(field.param, int(field.default))
    if field.kind in ("float", "quantity"):
        return group.GetFloat(field.param, float(field.default))
    if field.kind == "bool":
        return group.GetBool(field.param, bool(field.default))
    return group.GetString(field.param, str(field.default))


def _write_parameter(field: _Field, value) -> None:
    group = _draft_group()
    if field.kind == "color":
        group.SetUnsigned(field.param, int(value))
    elif field.kind in ("int", "enum"):
        group.SetInt(field.param, int(value))
    elif field.kind in ("float", "quantity"):
        group.SetFloat(field.param, float(value))
    elif field.kind == "bool":
        group.SetBool(field.param, bool(value))
    else:
        group.SetString(field.param, str(value))


# ---------------------------------------------------------------------------
# The controller
# ---------------------------------------------------------------------------


class PanelController:
    """A loaded form plus its accept/reject path.

    Subclasses supply ``_field_specs()`` (the fields this panel reads and
    writes), ``_seed()`` (fill the form) and ``_commit()`` (persist the form).
    """

    command: str = ""
    title: str = ""

    def __init__(self, path) -> None:
        self.ui_path = Path(path)
        self.form = FreeCADGui.PySideUic.loadUi(str(self.ui_path))
        self._seed()

    # -- what a shell dialog spec needs ------------------------------------

    def _field_specs(self):
        raise NotImplementedError

    def fields(self) -> list:
        """JSON-ready descriptions of every field this panel drives."""
        return [_field_json(self.form, field) for field in self._field_specs()]

    def values(self) -> dict:
        """The current form values, keyed by field name."""
        return {
            field.name: _json_value(self.form, field)
            for field in self._field_specs()
        }

    def apply(self, values: dict) -> None:
        """Write shell values into the form; unknown names are an error."""
        specs = {field.name: field for field in self._field_specs()}
        for name, value in values.items():
            field = specs.get(name)
            if field is None:
                raise ValueError(
                    f"{self.command}: unknown field {name!r}; "
                    f"expected one of {sorted(specs)}"
                )
            _write_storage(self.form, field, _from_json(self.form, field, value),
                           strict=True)

    # -- the two exits -----------------------------------------------------

    def accept(self, values=None) -> dict:
        """Apply optional shell values, commit, and close the form."""
        if values:
            self.apply(values)
        result = self._commit()
        self._finish(True)
        return result

    def reject(self) -> dict:
        """Discard the form; nothing is written."""
        self._finish(False)
        return {"status": f"{self.command}: cancelled; nothing was written"}

    def _finish(self, accepted: bool) -> None:
        """Mirror QDialog accept/reject when the form root is a dialog."""
        if self.form.findChild("QDialog") is not None:
            if accepted:
                self.form.accept()
            else:
                self.form.reject()

    # -- subclass hooks ----------------------------------------------------

    def _seed(self) -> None:
        raise NotImplementedError

    def _commit(self) -> dict:
        raise NotImplementedError

    def _storage_values(self) -> dict:
        return {
            field.name: _read_storage(self.form, field)
            for field in self._field_specs()
        }

    def _json_values(self) -> dict:
        return {
            field.name: _json_value(self.form, field)
            for field in self._field_specs()
        }


# ---------------------------------------------------------------------------
# Draft_SetStyle
# ---------------------------------------------------------------------------


_STYLE_FIELDS = (
    _Field("ShapeColor", "Shape color", "color", "DefaultShapeColor", 0xCCCCCCFF),
    _Field("AmbientColor", "Ambient shape color", "color",
           "DefaultAmbientColor", 0x333333FF),
    _Field("EmissiveColor", "Emissive shape color", "color",
           "DefaultEmissiveColor", 0x000000FF),
    _Field("SpecularColor", "Specular shape color", "color",
           "DefaultSpecularColor", 0x000000FF),
    _Field("Transparency", "Shape transparency", "int",
           "DefaultShapeTransparency", 0),
    _Field("Shininess", "Shape shininess", "int", "DefaultShapeShininess", 0),
    _Field("LineColor", "Line color", "color", "DefaultShapeLineColor", 0x000000FF),
    _Field("LineWidth", "Line width", "int", "DefaultShapeLineWidth", 2),
    _Field("PointColor", "Point color", "color", "DefaultShapeVertexColor",
           0x000000FF),
    _Field("PointSize", "Point size", "int", "DefaultShapePointSize", 2),
    _Field("DrawStyle", "Draw style", "enum", "DefaultDrawStyle", 0),
    _Field("DisplayMode", "Display mode", "enum", "DefaultDisplayMode", 0),
    _Field("TextColor", "Text color", "color", "DefaultTextColor", 0x000000FF),
    _Field("TextFont", "Font name", "font", "textfont", "sans"),
    _Field("TextSize", "Font size", "quantity", "textheight", 3.5),
    _Field("LineSpacing", "Line spacing factor", "float", "LineSpacing", 1.0),
    _Field("ScaleMultiplier", "Scale multiplier", "float",
           "DefaultAnnoScaleMultiplier", 1.0),
    _Field("AnnoLineColor", "Line and arrow color", "color",
           "DefaultAnnoLineColor", 0x000000FF),
    _Field("AnnoLineWidth", "Line width", "int", "DefaultAnnoLineWidth", 1),
    _Field("ArrowStyleStart", "Start arrow type", "enum", "dimsymbolstart", 2),
    _Field("ArrowSizeStart", "Start arrow size", "quantity", "arrowsizestart", 1.0),
    _Field("ArrowStyleEnd", "End arrow type", "enum", "dimsymbolend", 2),
    _Field("ArrowSizeEnd", "End arrow size", "quantity", "arrowsizeend", 1.0),
    _Field("ShowUnit", "Show unit", "bool", "showUnit", False),
    _Field("UnitOverride", "Unit override", "text", "overrideUnit", ""),
    _Field("DimOvershoot", "Dimension line overshoot", "quantity",
           "dimovershoot", 0.0),
    _Field("ExtLines", "Extension line length", "quantity", "extlines", 0.0),
    _Field("ExtOvershoot", "Extension line overshoot", "quantity",
           "extovershoot", 0.0),
    _Field("TextSpacing", "Text spacing", "quantity", "dimspacing", 1.0),
)


class SetStylePanel(PanelController):
    """``Draft_SetStyle``: the Draft defaults plus named presets.

    Accept writes every field into the Draft parameter group. Presets are a JSON
    object stored in that same group (``StylePresets``), so they survive a
    process restart without the upstream ``StylePresets.json`` file.
    """

    command = "Draft_SetStyle"
    title = "Set Style"

    def _field_specs(self):
        return _STYLE_FIELDS

    def _seed(self) -> None:
        for field in _STYLE_FIELDS:
            _write_storage(self.form, field, _read_parameter(field), strict=False)
        self._sync_preset_combo()

    # -- presets -----------------------------------------------------------

    def presets(self) -> dict:
        """The stored presets, ``{name: {field: value}}``."""
        raw = _draft_group().GetString(PRESET_KEY, "")
        if not raw:
            return {}
        try:
            data = json.loads(raw)
        except json.JSONDecodeError as error:
            raise ValueError(
                f"Draft style presets in {DRAFT_PREFERENCES}: "
                f"{PRESET_KEY!r} is not valid JSON: {error}"
            ) from error
        if not isinstance(data, dict):
            raise ValueError(
                f"Draft style presets in {DRAFT_PREFERENCES}: "
                f"{PRESET_KEY!r} must be a JSON object"
            )
        return data

    def _store_presets(self, presets: dict) -> None:
        _draft_group().SetString(PRESET_KEY, json.dumps(presets, sort_keys=True))
        self._sync_preset_combo()

    def _sync_preset_combo(self) -> None:
        _set_combo_items(self.form.comboPresets, ["Load preset"] + list(self.presets()))

    def save_preset(self, name: str) -> dict:
        """Store the current form values under ``name`` and return them."""
        name = str(name).strip()
        if not name:
            raise ValueError("a preset name must not be empty")
        presets = self.presets()
        presets[name] = self._storage_values()
        self._store_presets(presets)
        return dict(presets[name])

    def delete_preset(self, name: str) -> bool:
        """Remove ``name``; ``False`` when it was not stored."""
        presets = self.presets()
        if name not in presets:
            return False
        del presets[name]
        self._store_presets(presets)
        return True

    def load_preset(self, name: str) -> dict:
        """Fill the form from a stored preset and return its values."""
        presets = self.presets()
        if name not in presets:
            raise KeyError(f"no Draft style preset {name!r}")
        preset = presets[name]
        for field in _STYLE_FIELDS:
            _write_storage(
                self.form,
                field,
                preset.get(field.name, _read_parameter(field)),
                strict=False,
            )
        return {field.name: _read_storage(self.form, field) for field in _STYLE_FIELDS}

    def select_preset(self, name: str) -> dict:
        """Load a preset and select it in the preset combo."""
        values = self.load_preset(name)
        self.form.comboPresets.setCurrentText(name)
        return values

    # -- accept ------------------------------------------------------------

    def _commit(self) -> dict:
        values = self._storage_values()
        for field in _STYLE_FIELDS:
            _write_parameter(field, values[field.name])
        return {
            "status": (
                f"style applied: {len(values)} values written to "
                f"{DRAFT_PREFERENCES}"
            ),
            "value": self._json_values(),
        }


# ---------------------------------------------------------------------------
# Draft_Hatch
# ---------------------------------------------------------------------------


_HATCH_FIELDS = (
    _Field("File", "PAT file", "file"),
    _Field("Pattern", "Pattern", "enum"),
    _Field("Scale", "Scale", "float"),
    _Field("Rotation", "Rotation", "float"),
    _Field("Translate", "Align to face", "bool"),
)


def _patterns_in(filename: str) -> list:
    """The ``*NAME, description`` lines of a PAT file, as upstream reads them."""
    if not filename or not os.path.isfile(filename):
        return []
    names = []
    with open(filename, encoding="utf-8", errors="replace") as handle:
        for line in handle:
            if line.startswith("*"):
                name = line[1:].split(",")[0].strip()
                if name:
                    names.append(name)
    return names


class HatchPanel(PanelController):
    """``Draft_Hatch``: hatch parameters, optionally applied to a selection."""

    command = "Draft_Hatch"
    title = "Hatch"

    def _field_specs(self):
        return _HATCH_FIELDS

    def _seed(self) -> None:
        group = _draft_group()
        self.form.Scale.setValue(group.GetFloat("HatchPatternScale", 1000.0))
        self.form.Rotation.setValue(group.GetFloat("HatchPatternRotation", 0.0))
        self.form.Translate.setChecked(
            group.GetBool("HatchPatternTranslate", False)
        )
        self.form.File.setFileName(group.GetString("HatchPatternFile", ""))
        self._refresh_patterns()
        name = group.GetString("HatchPatternName", "")
        if name:
            self.form.Pattern.setCurrentText(name)
        # Connect after seeding: the initial name must not erase the pattern.
        self.form.File.fileNameChanged.connect(self._on_file_changed)

    def _on_file_changed(self, filename) -> None:
        self._refresh_patterns(filename)

    def patterns(self) -> list:
        """The patterns in the form's current PAT file."""
        return _patterns_in(self.form.File.fileName())

    def _refresh_patterns(self, filename=None) -> None:
        filename = self.form.File.fileName() if filename is None else filename
        patterns = _patterns_in(filename)
        current = _combo_text(self.form.Pattern)
        self.form.Pattern.clear()
        self.form.Pattern.addItems(patterns)
        if current in patterns:
            self.form.Pattern.setCurrentText(current)

    def apply(self, values: dict) -> None:
        # The file reloads the pattern list, so it must be applied first.
        remaining = dict(values)
        if "File" in remaining:
            field = next(item for item in _HATCH_FIELDS if item.name == "File")
            _write_storage(
                self.form,
                field,
                _from_json(self.form, field, remaining.pop("File")),
                strict=True,
            )
        super().apply(remaining)

    def _commit(self) -> dict:
        form = self.form
        filename = form.File.fileName()
        pattern = _combo_text(form.Pattern)
        scale = float(form.Scale.value())
        rotation = float(form.Rotation.value())
        translate = bool(form.Translate.isChecked())

        group = _draft_group()
        group.SetString("HatchPatternFile", filename)
        group.SetString("HatchPatternName", pattern)
        group.SetFloat("HatchPatternScale", scale)
        group.SetFloat("HatchPatternRotation", rotation)
        group.SetBool("HatchPatternTranslate", translate)

        applied = []
        for obj in FreeCADGui.Selection.getSelection():
            properties = list(getattr(obj, "PropertiesList", ()) or ())
            # Upstream decides from File/Pattern; the object is a hatch.
            if "File" not in properties or "Pattern" not in properties:
                continue
            obj.File = filename
            obj.Pattern = pattern
            if "Scale" in properties:
                obj.Scale = scale
            if "Rotation" in properties:
                obj.Rotation = rotation
            if "Translate" in properties:
                obj.Translate = translate
            applied.append(obj.Name)

        if applied:
            status = (
                "hatch settings saved and applied to "
                + ", ".join(applied)
            )
        else:
            status = (
                "hatch settings saved; no supported object selected "
                "(an object needs File and Pattern properties)"
            )
        return {
            "status": status,
            "value": {
                "File": filename,
                "Pattern": pattern,
                "Scale": scale,
                "Rotation": rotation,
                "Translate": translate,
                "applied": applied,
            },
        }


# ---------------------------------------------------------------------------
# Draft_AnnotationStyleEditor
# ---------------------------------------------------------------------------


_ANNOTATION_FIELDS = (
    _Field("ArrowSizeStart", "Start arrow size", "quantity", "arrowsizestart", 1.0),
    _Field("ArrowSizeEnd", "End arrow size", "quantity", "arrowsizeend", 1.0),
    _Field("ArrowTypeStart", "Start arrow type", "enum", "dimsymbolstart", 2),
    _Field("ArrowTypeEnd", "End arrow type", "enum", "dimsymbolend", 2),
    _Field("Decimals", "Number of decimals", "int", "dimPrecision", 2),
    _Field("DimOvershoot", "Dimension line overshoot", "quantity",
           "dimovershoot", 0.0),
    _Field("ExtLines", "Extension line length", "quantity", "extlines", 0.0),
    _Field("ExtOvershoot", "Extension line overshoot", "quantity",
           "extovershoot", 0.0),
    _Field("FontName", "Font name", "font", "textfont", "sans"),
    _Field("FontSize", "Font size", "quantity", "textheight", 3.5),
    _Field("LineColor", "Line and arrow color", "color",
           "DefaultAnnoLineColor", 0x000000FF),
    _Field("LineSpacing", "Line spacing factor", "float", "LineSpacing", 1.0),
    _Field("LineWidth", "Line width", "int", "DefaultAnnoLineWidth", 1),
    _Field("ScaleMultiplier", "Scale multiplier", "float",
           "DefaultAnnoScaleMultiplier", 1.0),
    _Field("ShowLine", "Show dimension line", "bool", "DimShowLine", True),
    _Field("ShowUnit", "Show unit", "bool", "showUnit", False),
    _Field("TextColor", "Text color", "color", "DefaultTextColor", 0x000000FF),
    _Field("TextSpacing", "Text spacing", "quantity", "dimspacing", 1.0),
    _Field("UnitOverride", "Unit override", "text", "overrideUnit", ""),
)

_STYLE_PREFIX = "Draft_Style_"


class AnnotationStylePanel(PanelController):
    """``Draft_AnnotationStyleEditor``: annotation styles stored in ``Doc.Meta``.

    Styles live under ``Draft_Style_<name>`` keys, exactly as upstream stores
    them. Accept also writes the edited values into the Draft parameter group
    (the headless ``ViewObject`` has no annotation properties to push them to);
    the returned ``value`` names the objects that were updated, which is empty
    in FerroCAD today.
    """

    command = "Draft_AnnotationStyleEditor"
    title = "Annotation Styles"

    def _field_specs(self):
        return _ANNOTATION_FIELDS

    def _seed(self) -> None:
        combo = self.form.comboBoxStyles
        self._styles = self._read_styles()
        combo.clear()
        combo.addItems(["", "Add newโ€ฆ"] + list(self._styles))
        self.current_style = None
        self._fill_editor(self._default_values())
        combo.currentIndexChanged.connect(self._on_style_changed)
        if self._styles:
            combo.setCurrentIndex(2)
        else:
            self._update_buttons()

    def _default_values(self) -> dict:
        return {field.name: _read_parameter(field) for field in _ANNOTATION_FIELDS}

    def _fill_editor(self, style: dict) -> None:
        for field in _ANNOTATION_FIELDS:
            _write_storage(
                self.form,
                field,
                style.get(field.name, _read_parameter(field)),
                strict=False,
            )
        self._update_buttons()

    def _update_buttons(self) -> None:
        enabled = bool(getattr(self, "current_style", None))
        self.form.pushButtonRename.setEnabled(enabled)
        self.form.pushButtonDelete.setEnabled(enabled)

    # -- the style table ---------------------------------------------------

    def editor_values(self) -> dict:
        """The editor's values in storage form."""
        return self._storage_values()

    def styles(self) -> dict:
        """A shallow copy of the loaded style table."""
        return {name: dict(values) for name, values in self._styles.items()}

    def style_names(self) -> list:
        return list(self._styles)

    def _style_index(self, name: str) -> int:
        for index, item in enumerate(_combo_items(self.form.comboBoxStyles)):
            if item == name:
                return index
        return -1

    def select_style(self, name: str) -> dict:
        """Select ``name`` in the combo (adding it when absent) and fill the form."""
        combo = self.form.comboBoxStyles
        index = self._style_index(name)
        if index < 0:
            self._styles.setdefault(name, self.editor_values())
            combo.addItem(name)
            index = combo.count() - 1
        combo.setCurrentIndex(index)
        return self.editor_values()

    def _on_style_changed(self, index) -> None:
        if getattr(self, "_loading", False):
            return
        if self.current_style:
            self._styles[self.current_style] = self.editor_values()
        if index <= 1:
            self.current_style = None
            self._fill_editor(self._default_values())
        else:
            name = self.form.comboBoxStyles.itemText(index)
            self.current_style = name
            self._fill_editor(self._styles[name])
        self._update_buttons()

    def save_style(self, name: str) -> dict:
        """Store the editor values as ``name`` in the document and return them."""
        name = str(name).strip()
        if not name:
            raise ValueError("a style name must not be empty")
        self._styles[name] = self.editor_values()
        self.current_style = name
        combo = self.form.comboBoxStyles
        index = self._style_index(name)
        if index < 0:
            combo.addItem(name)
            index = combo.count() - 1
        combo.setCurrentIndex(index)
        self._save_styles()
        return dict(self._styles[name])

    def _read_styles(self) -> dict:
        document = FreeCAD.ActiveDocument
        if document is None:
            return {}
        styles = {}
        for key, value in (document.Meta or {}).items():
            if not key.startswith(_STYLE_PREFIX):
                continue
            name = key[len(_STYLE_PREFIX):]
            try:
                data = json.loads(value)
            except (TypeError, json.JSONDecodeError) as error:
                raise ValueError(
                    f"document {document.Name!r}: style {name!r} is not valid "
                    f"JSON: {error}"
                ) from error
            if not isinstance(data, dict):
                raise ValueError(
                    f"document {document.Name!r}: style {name!r} must be a "
                    "JSON object"
                )
            styles[name] = self._repair_style(data)
        return styles

    def _repair_style(self, style: dict) -> dict:
        """Fill missing keys from the current defaults (upstream's repair)."""
        source = dict(style)
        # V1.0 -> V1.1: one ArrowSize/ArrowType became Start/End pairs.
        for old in ("ArrowSize", "ArrowType"):
            if (
                old in source
                and old + "Start" not in source
                and old + "End" not in source
            ):
                source[old + "Start"] = source[old]
                source[old + "End"] = source[old]

        repaired = {}
        for field in _ANNOTATION_FIELDS:
            default = _read_parameter(field)
            raw = source.get(field.name)
            if raw is None:
                repaired[field.name] = default
                continue
            try:
                if field.kind == "bool":
                    repaired[field.name] = bool(raw)
                elif field.kind in ("int", "color"):
                    repaired[field.name] = int(raw)
                elif field.kind == "enum":
                    if isinstance(raw, str):
                        repaired[field.name] = ARROW_TYPES.index(raw)
                    else:
                        repaired[field.name] = int(raw)
                elif field.kind in ("float", "quantity"):
                    repaired[field.name] = float(raw)
                else:
                    repaired[field.name] = str(raw)
            except (TypeError, ValueError):
                repaired[field.name] = default
        return repaired

    def _save_styles(self) -> bool:
        document = FreeCAD.ActiveDocument
        if document is None:
            return False
        meta = dict(document.Meta or {})
        for key in list(meta):
            if key.startswith(_STYLE_PREFIX) and key[len(_STYLE_PREFIX):] not in self._styles:
                del meta[key]
        for name, values in self._styles.items():
            meta[_STYLE_PREFIX + name] = json.dumps(values, sort_keys=True)
        document.Meta = meta
        return True

    # -- accept ------------------------------------------------------------

    def _commit(self) -> dict:
        editor = self.editor_values()
        for field in _ANNOTATION_FIELDS:
            _write_parameter(field, editor[field.name])

        if self.current_style:
            self._styles[self.current_style] = editor
        saved = self._save_styles()

        if self.current_style:
            status = (
                f"annotation style {self.current_style!r} updated in "
                f"{DRAFT_PREFERENCES}"
            )
        else:
            status = (
                "annotation parameters written to "
                f"{DRAFT_PREFERENCES}; no named style was selected"
            )
        if not saved:
            status += "; no active document, so styles were not stored"
        status += "; no annotation view objects exist headlessly"

        return {
            "status": status,
            "value": {
                "style": self.current_style,
                "styles": sorted(self._styles),
                "document": saved,
                "values": self._json_values(),
            },
        }


# ---------------------------------------------------------------------------
# Draft_Scale
# ---------------------------------------------------------------------------


_SCALE_FIELDS = (
    _Field("xValue", "X-factor", "float"),
    _Field("yValue", "Y-factor", "float"),
    _Field("zValue", "Z-factor", "float"),
    _Field("lock", "Uniform scaling", "bool"),
    _Field("isCopy", "Copy", "bool"),
)


class ScalePanel(PanelController):
    """``Draft_Scale``: the scale factors and the copy option."""

    command = "Draft_Scale"
    title = "Scale"

    def _field_specs(self):
        return _SCALE_FIELDS

    def _seed(self) -> None:
        group = _draft_group()
        self.form.lock.setChecked(group.GetBool("ScaleUniform", False))
        self.form.isCopy.setChecked(group.GetBool("ScaleCopy", False))

    def _commit(self) -> dict:
        form = self.form
        x = float(form.xValue.value())
        y = float(form.yValue.value())
        z = float(form.zValue.value())
        uniform = bool(form.lock.isChecked())
        if uniform:
            y = z = x
            form.yValue.setValue(y)
            form.zValue.setValue(z)

        group = _draft_group()
        group.SetBool("ScaleUniform", uniform)
        group.SetBool("ScaleCopy", bool(form.isCopy.isChecked()))

        applied = []
        for obj in FreeCADGui.Selection.getSelection():
            properties = list(getattr(obj, "PropertiesList", ()) or ())
            if "ScaleVector" in properties:
                obj.ScaleVector = FreeCAD.Vector(x, y, z)
                if "Scale" in properties and x == y == z:
                    obj.Scale = x
                applied.append(obj.Name)
            elif "Scale" in properties and x == y == z:
                obj.Scale = x
                applied.append(obj.Name)

        if applied:
            status = "scale applied to " + ", ".join(applied)
        else:
            status = (
                "scale factors saved; no supported object selected "
                "(an object needs a ScaleVector property)"
            )

        return {
            "status": status,
            "value": {
                "x": x,
                "y": y,
                "z": z,
                "uniform": uniform,
                "copy": bool(form.isCopy.isChecked()),
                "applied": applied,
            },
        }


# ---------------------------------------------------------------------------
# open_panel and the commands
# ---------------------------------------------------------------------------


_PANELS = {
    "Draft_SetStyle": SetStylePanel,
    "Draft_Hatch": HatchPanel,
    "Draft_AnnotationStyleEditor": AnnotationStylePanel,
    "Draft_Scale": ScalePanel,
}


def open_panel(name: str, path=None) -> PanelController:
    """Load ``name`` headlessly and return its controller.

    ``name`` is one of :data:`COMMANDS`; ``path`` overrides the form location.
    A missing extension propagates ``FreeCADGui.PySideUic.loadUi``'s actionable
    ``ImportError``; a missing ``.ui`` propagates ``FileNotFoundError``.
    """
    panel_class = _PANELS.get(name)
    if panel_class is None:
        raise KeyError(
            f"unknown Draft form {name!r}; expected one of {sorted(_PANELS)}"
        )
    return panel_class(ui_path(name, path))


def _dialog_spec(panel: PanelController) -> dict:
    """Turn a controller into the dialog spec ``FreeCADGui._payload`` accepts."""
    return {
        "title": panel.title,
        "fields": panel.fields(),
        "accept": "OK",
        "cancel": "Cancel",
        "on_accept": panel.accept,
    }


class _FormCommand:
    """One Draft command, driving its headless form."""

    command = ""
    menu = ""
    tooltip = ""
    accel = ""

    def GetResources(self) -> dict:
        return {
            "MenuText": self.menu or self.command,
            "ToolTip": self.tooltip,
            "Accel": self.accel,
            "Group": "Draft",
        }

    def IsActive(self) -> bool:
        # Every accept path has a parameter-store effect, so the command can
        # run without a document or a selection.
        return True

    def Activated(self) -> dict:
        return _dialog_spec(open_panel(self.command))


class _SetStyleCommand(_FormCommand):
    command = "Draft_SetStyle"
    menu = "Set Style"
    tooltip = "Sets the default Draft style and manages style presets"
    accel = "S, S"


class _HatchCommand(_FormCommand):
    command = "Draft_Hatch"
    menu = "Hatch"
    tooltip = "Creates hatches on the faces of a selected object"
    accel = "H, A"


class _AnnotationStyleCommand(_FormCommand):
    command = "Draft_AnnotationStyleEditor"
    menu = "Annotation Styles"
    tooltip = "Opens an editor to manage or create annotation styles"


class _ScaleCommand(_FormCommand):
    command = "Draft_Scale"
    menu = "Scale"
    tooltip = "Scales the selected objects"


_COMMAND_INSTANCES = (
    _SetStyleCommand(),
    _HatchCommand(),
    _AnnotationStyleCommand(),
    _ScaleCommand(),
)

for _instance in _COMMAND_INSTANCES:
    FreeCADGui.addCommand(_instance.command, _instance)
del _instance


__all__ = [
    "ARROW_TYPES",
    "COMMANDS",
    "DRAFT_PREFERENCES",
    "PRESET_KEY",
    "UPSTREAM_FORMS",
    "AnnotationStylePanel",
    "HatchPanel",
    "PanelController",
    "ScalePanel",
    "SetStylePanel",
    "open_panel",
    "ui_directory",
    "ui_path",
]