mitk-python 2026.12rc1__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. mitk/MitkCore/MitkDICOMImageIO.dll +0 -0
  2. mitk/MitkCore/MitkDICOMPMIO.dll +0 -0
  3. mitk/MitkCore/MitkDICOMRTIO.dll +0 -0
  4. mitk/MitkCore/MitkDICOMSegIO.dll +0 -0
  5. mitk/MitkCore/MitkIOExt.dll +0 -0
  6. mitk/MitkCore/MitkModelFitIOServices.dll +0 -0
  7. mitk/MitkCore/MitkMultilabelIO.dll +0 -0
  8. mitk/MitkCore/MitkPlanarFigureIO.dll +0 -0
  9. mitk/MitkCore/MitkROIIO.dll +0 -0
  10. mitk/MitkCore/MitkSceneSerializationIO.dll +0 -0
  11. mitk/MitkCore/MitkSegmentationIO.dll +0 -0
  12. mitk/MitkDICOM/MitkCESTIO.dll +0 -0
  13. mitk/MitkModelFit/MitkModelFitModelsServices.dll +0 -0
  14. mitk/MitkModelFit/MitkPharmacokineticModelsServices.dll +0 -0
  15. mitk/__init__.py +78 -0
  16. mitk/mitk.cp313-win_amd64.pyd +0 -0
  17. mitk/mxn/__init__.py +17 -0
  18. mitk/mxn/layout/__init__.py +115 -0
  19. mitk/mxn/layout/_builders.py +239 -0
  20. mitk/mxn/layout/_io.py +66 -0
  21. mitk/mxn/layout/_model.py +1204 -0
  22. mitk/mxn/layout/_presets.py +93 -0
  23. mitk/mxn/layout/_repr.py +205 -0
  24. mitk/mxn/layout/_selector.py +363 -0
  25. mitk/mxn/layout/_validation.py +348 -0
  26. mitk/mxn/layout/_vocabulary.py +100 -0
  27. mitk/property_view.py +123 -0
  28. mitk_python-2026.12rc1.dist-info/DELVEWHEEL +2 -0
  29. mitk_python-2026.12rc1.dist-info/METADATA +67 -0
  30. mitk_python-2026.12rc1.dist-info/RECORD +193 -0
  31. mitk_python-2026.12rc1.dist-info/WHEEL +4 -0
  32. mitk_python-2026.12rc1.dist-info/top_level.txt +1 -0
  33. mitk_python.libs/ITKCommon-5.4.dll +0 -0
  34. mitk_python.libs/ITKIOBMP-5.4.dll +0 -0
  35. mitk_python.libs/ITKIOBioRad-5.4.dll +0 -0
  36. mitk_python.libs/ITKIOBruker-5.4.dll +0 -0
  37. mitk_python.libs/ITKIOGDCM-5.4.dll +0 -0
  38. mitk_python.libs/ITKIOGE-5.4.dll +0 -0
  39. mitk_python.libs/ITKIOGIPL-5.4.dll +0 -0
  40. mitk_python.libs/ITKIOHDF5-5.4.dll +0 -0
  41. mitk_python.libs/ITKIOIPL-5.4.dll +0 -0
  42. mitk_python.libs/ITKIOImageBase-5.4.dll +0 -0
  43. mitk_python.libs/ITKIOJPEG-5.4.dll +0 -0
  44. mitk_python.libs/ITKIOJPEG2000-5.4.dll +0 -0
  45. mitk_python.libs/ITKIOLSM-5.4.dll +0 -0
  46. mitk_python.libs/ITKIOMINC-5.4.dll +0 -0
  47. mitk_python.libs/ITKIOMRC-5.4.dll +0 -0
  48. mitk_python.libs/ITKIOMeta-5.4.dll +0 -0
  49. mitk_python.libs/ITKIONIFTI-5.4.dll +0 -0
  50. mitk_python.libs/ITKIONRRD-5.4.dll +0 -0
  51. mitk_python.libs/ITKIOPNG-5.4.dll +0 -0
  52. mitk_python.libs/ITKIOStimulate-5.4.dll +0 -0
  53. mitk_python.libs/ITKIOTIFF-5.4.dll +0 -0
  54. mitk_python.libs/ITKIOVTK-5.4.dll +0 -0
  55. mitk_python.libs/ITKLabelMap-5.4.dll +0 -0
  56. mitk_python.libs/ITKOptimizers-5.4.dll +0 -0
  57. mitk_python.libs/ITKQuadEdgeMesh-5.4.dll +0 -0
  58. mitk_python.libs/ITKRegionGrowing-5.4.dll +0 -0
  59. mitk_python.libs/ITKSmoothing-5.4.dll +0 -0
  60. mitk_python.libs/ITKStatistics-5.4.dll +0 -0
  61. mitk_python.libs/ITKTransform-5.4.dll +0 -0
  62. mitk_python.libs/MitkAlgorithmsExt.dll +0 -0
  63. mitk_python.libs/MitkCEST.dll +0 -0
  64. mitk_python.libs/MitkContourModel.dll +0 -0
  65. mitk_python.libs/MitkCore.dll +0 -0
  66. mitk_python.libs/MitkCppMicroServices.dll +0 -0
  67. mitk_python.libs/MitkDICOM.dll +0 -0
  68. mitk_python.libs/MitkDICOMPM.dll +0 -0
  69. mitk_python.libs/MitkDICOMQI.dll +0 -0
  70. mitk_python.libs/MitkDataTypesExt.dll +0 -0
  71. mitk_python.libs/MitkLog.dll +0 -0
  72. mitk_python.libs/MitkMapperExt.dll +0 -0
  73. mitk_python.libs/MitkModelFit.dll +0 -0
  74. mitk_python.libs/MitkMultilabel.dll +0 -0
  75. mitk_python.libs/MitkPharmacokinetics.dll +0 -0
  76. mitk_python.libs/MitkPlanarFigure.dll +0 -0
  77. mitk_python.libs/MitkROI.dll +0 -0
  78. mitk_python.libs/MitkRT.dll +0 -0
  79. mitk_python.libs/MitkSceneSerialization.dll +0 -0
  80. mitk_python.libs/MitkSceneSerializationBase.dll +0 -0
  81. mitk_python.libs/MitkSegmentation.dll +0 -0
  82. mitk_python.libs/MitkSurfaceInterpolation.dll +0 -0
  83. mitk_python.libs/MitkVolumeVisualization.dll +0 -0
  84. mitk_python.libs/PocoFoundation.dll +0 -0
  85. mitk_python.libs/PocoZip.dll +0 -0
  86. mitk_python.libs/ann.dll +0 -0
  87. mitk_python.libs/dcmdata.dll +0 -0
  88. mitk_python.libs/dcmfg.dll +0 -0
  89. mitk_python.libs/dcmimage.dll +0 -0
  90. mitk_python.libs/dcmimgle.dll +0 -0
  91. mitk_python.libs/dcmiod.dll +0 -0
  92. mitk_python.libs/dcmpmap.dll +0 -0
  93. mitk_python.libs/dcmrt.dll +0 -0
  94. mitk_python.libs/dcmseg.dll +0 -0
  95. mitk_python.libs/dcmsr.dll +0 -0
  96. mitk_python.libs/gdcmCommon.dll +0 -0
  97. mitk_python.libs/gdcmDICT.dll +0 -0
  98. mitk_python.libs/gdcmDSED.dll +0 -0
  99. mitk_python.libs/gdcmIOD.dll +0 -0
  100. mitk_python.libs/gdcmMSFF.dll +0 -0
  101. mitk_python.libs/gdcmcharls.dll +0 -0
  102. mitk_python.libs/gdcmexpat.dll +0 -0
  103. mitk_python.libs/gdcmjpeg12.dll +0 -0
  104. mitk_python.libs/gdcmjpeg16.dll +0 -0
  105. mitk_python.libs/gdcmjpeg8.dll +0 -0
  106. mitk_python.libs/gdcmopenjp2.dll +0 -0
  107. mitk_python.libs/gdcmzlib.dll +0 -0
  108. mitk_python.libs/itkGrowCut-5.4.dll +0 -0
  109. mitk_python.libs/itkhdf5-shared-5.4.dll +0 -0
  110. mitk_python.libs/itkhdf5_cpp-shared-5.4.dll +0 -0
  111. mitk_python.libs/lz4.dll +0 -0
  112. mitk_python.libs/msvcp140.dll +0 -0
  113. mitk_python.libs/oficonv.dll +0 -0
  114. mitk_python.libs/oflog.dll +0 -0
  115. mitk_python.libs/ofstd.dll +0 -0
  116. mitk_python.libs/tbb12.dll +0 -0
  117. mitk_python.libs/vcomp140.dll +0 -0
  118. mitk_python.libs/vtkCommonCache-9.7.dll +0 -0
  119. mitk_python.libs/vtkCommonColor-9.7.dll +0 -0
  120. mitk_python.libs/vtkCommonComputationalGeometry-9.7.dll +0 -0
  121. mitk_python.libs/vtkCommonCore-9.7.dll +4 -0
  122. mitk_python.libs/vtkCommonDataModel-9.7.dll +0 -0
  123. mitk_python.libs/vtkCommonExecutionModel-9.7.dll +0 -0
  124. mitk_python.libs/vtkCommonMath-9.7.dll +0 -0
  125. mitk_python.libs/vtkCommonMisc-9.7.dll +0 -0
  126. mitk_python.libs/vtkCommonSystem-9.7.dll +0 -0
  127. mitk_python.libs/vtkCommonTransforms-9.7.dll +0 -0
  128. mitk_python.libs/vtkDICOMParser-9.7.dll +0 -0
  129. mitk_python.libs/vtkFiltersCellGrid-9.7.dll +0 -0
  130. mitk_python.libs/vtkFiltersCore-9.7.dll +0 -0
  131. mitk_python.libs/vtkFiltersExtraction-9.7.dll +0 -0
  132. mitk_python.libs/vtkFiltersGeneral-9.7.dll +0 -0
  133. mitk_python.libs/vtkFiltersGeneric-9.7.dll +0 -0
  134. mitk_python.libs/vtkFiltersGeometry-9.7.dll +0 -0
  135. mitk_python.libs/vtkFiltersHybrid-9.7.dll +0 -0
  136. mitk_python.libs/vtkFiltersHyperTree-9.7.dll +0 -0
  137. mitk_python.libs/vtkFiltersModeling-9.7.dll +0 -0
  138. mitk_python.libs/vtkFiltersParallel-9.7.dll +0 -0
  139. mitk_python.libs/vtkFiltersReduction-9.7.dll +0 -0
  140. mitk_python.libs/vtkFiltersSources-9.7.dll +0 -0
  141. mitk_python.libs/vtkFiltersStatistics-9.7.dll +0 -0
  142. mitk_python.libs/vtkFiltersTexture-9.7.dll +0 -0
  143. mitk_python.libs/vtkFiltersVerdict-9.7.dll +0 -0
  144. mitk_python.libs/vtkIOCellGrid-9.7.dll +0 -0
  145. mitk_python.libs/vtkIOCore-9.7.dll +0 -0
  146. mitk_python.libs/vtkIOGeometry-9.7.dll +0 -0
  147. mitk_python.libs/vtkIOHDF-9.7.dll +0 -0
  148. mitk_python.libs/vtkIOImage-9.7.dll +0 -0
  149. mitk_python.libs/vtkIOLegacy-9.7.dll +0 -0
  150. mitk_python.libs/vtkIOPLY-9.7.dll +0 -0
  151. mitk_python.libs/vtkIOXML-9.7.dll +0 -0
  152. mitk_python.libs/vtkIOXMLParser-9.7.dll +0 -0
  153. mitk_python.libs/vtkImagingColor-9.7.dll +0 -0
  154. mitk_python.libs/vtkImagingCore-9.7.dll +0 -0
  155. mitk_python.libs/vtkImagingGeneral-9.7.dll +0 -0
  156. mitk_python.libs/vtkImagingMath-9.7.dll +0 -0
  157. mitk_python.libs/vtkImagingSources-9.7.dll +0 -0
  158. mitk_python.libs/vtkImagingStencil-9.7.dll +0 -0
  159. mitk_python.libs/vtkInteractionStyle-9.7.dll +0 -0
  160. mitk_python.libs/vtkParallelCore-9.7.dll +0 -0
  161. mitk_python.libs/vtkParallelDIY-9.7.dll +0 -0
  162. mitk_python.libs/vtkRenderingAnnotation-9.7.dll +0 -0
  163. mitk_python.libs/vtkRenderingContext2D-9.7.dll +0 -0
  164. mitk_python.libs/vtkRenderingContextOpenGL2-9.7.dll +0 -0
  165. mitk_python.libs/vtkRenderingCore-9.7.dll +0 -0
  166. mitk_python.libs/vtkRenderingFreeType-9.7.dll +0 -0
  167. mitk_python.libs/vtkRenderingHyperTreeGrid-9.7.dll +0 -0
  168. mitk_python.libs/vtkRenderingLabel-9.7.dll +0 -0
  169. mitk_python.libs/vtkRenderingOpenGL2-9.7.dll +0 -0
  170. mitk_python.libs/vtkRenderingUI-9.7.dll +0 -0
  171. mitk_python.libs/vtkRenderingVolume-9.7.dll +0 -0
  172. mitk_python.libs/vtkRenderingVolumeOpenGL2-9.7.dll +0 -0
  173. mitk_python.libs/vtkexpat-9.7.dll +0 -0
  174. mitk_python.libs/vtkfmt-9.7.dll +0 -0
  175. mitk_python.libs/vtkfreetype-9.7.dll +0 -0
  176. mitk_python.libs/vtkglad-9.7.dll +0 -0
  177. mitk_python.libs/vtkhdf5-9.7.dll +0 -0
  178. mitk_python.libs/vtkhdf5_hl-9.7.dll +0 -0
  179. mitk_python.libs/vtkjpeg-9.7.dll +0 -0
  180. mitk_python.libs/vtkjsoncpp-9.7.dll +0 -0
  181. mitk_python.libs/vtkkissfft-9.7.dll +0 -0
  182. mitk_python.libs/vtkloguru-9.7.dll +0 -0
  183. mitk_python.libs/vtklz4-9.7.dll +0 -0
  184. mitk_python.libs/vtklzma-9.7.dll +0 -0
  185. mitk_python.libs/vtkmetaio-9.7.dll +0 -0
  186. mitk_python.libs/vtkpng-9.7.dll +0 -0
  187. mitk_python.libs/vtkpugixml-9.7.dll +0 -0
  188. mitk_python.libs/vtkscn-9.7.dll +0 -0
  189. mitk_python.libs/vtksys-9.7.dll +0 -0
  190. mitk_python.libs/vtktiff-9.7.dll +0 -0
  191. mitk_python.libs/vtktoken-9.7.dll +0 -0
  192. mitk_python.libs/vtkverdict-9.7.dll +0 -0
  193. mitk_python.libs/vtkzlib-9.7.dll +0 -0
@@ -0,0 +1,93 @@
1
+ # ============================================================================
2
+ #
3
+ # The Medical Imaging Interaction Toolkit (MITK)
4
+ #
5
+ # Copyright (c) German Cancer Research Center (DKFZ)
6
+ # All rights reserved.
7
+ #
8
+ # Use of this source code is governed by a 3-clause BSD license that can be
9
+ # found in the LICENSE file.
10
+ #
11
+ # ============================================================================
12
+
13
+ """Named preset registry.
14
+
15
+ Presets are *named, complete documents* registered for lookup. Builders
16
+ return :class:`Split`; presets wrap the result with
17
+ ``MxNLayoutDocument.create(...)`` and are returned ready-to-use.
18
+
19
+ Named factories (rather than lambdas) so tracebacks point to a real
20
+ qualified name when a preset blows up.
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ from collections.abc import Callable, Mapping
26
+
27
+ from ._builders import single_window, three_up, two_rows_each_direction
28
+ from ._model import DEFAULT_GROUP, Group, LayoutWindow, Link, MxNLayoutDocument, Split
29
+ from ._vocabulary import ViewDirection
30
+
31
+
32
+ def _preset_movie_frame() -> MxNLayoutDocument:
33
+ """Three axial windows on one navigation group, at slice offsets
34
+ -1 / 0 / +1: the previous, the current and the next slice."""
35
+ navigation = Group("nav")
36
+
37
+ def frame(index: int, offset: int) -> LayoutWindow:
38
+ return LayoutWindow.create(
39
+ f"mxn__widget{index}",
40
+ ViewDirection.AXIAL,
41
+ slice=Link(navigation.name, offset=offset),
42
+ orientation=navigation,
43
+ )
44
+
45
+ return MxNLayoutDocument.create(
46
+ root=Split.horizontal(frame(0, -1), frame(1, 0), frame(2, 1)),
47
+ name="Movie Frame",
48
+ groups=[DEFAULT_GROUP, navigation],
49
+ )
50
+
51
+
52
+ def _preset_three_up() -> MxNLayoutDocument:
53
+ return MxNLayoutDocument.create(root=three_up(), name="Three Views")
54
+
55
+
56
+ def _preset_two_rows_each_direction() -> MxNLayoutDocument:
57
+ return MxNLayoutDocument.create(
58
+ root=two_rows_each_direction(),
59
+ name="Two Rows Each Direction",
60
+ )
61
+
62
+
63
+ def _preset_single() -> MxNLayoutDocument:
64
+ return MxNLayoutDocument.create(root=single_window())
65
+
66
+
67
+ PRESETS: Mapping[str, Callable[[], MxNLayoutDocument]] = {
68
+ "movie-frame": _preset_movie_frame,
69
+ "three-up": _preset_three_up,
70
+ "two-rows-each-direction": _preset_two_rows_each_direction,
71
+ "single": _preset_single,
72
+ }
73
+
74
+
75
+ def preset(name: str) -> MxNLayoutDocument:
76
+ """Return a fresh ``MxNLayoutDocument`` for the named preset.
77
+
78
+ Each call invokes the registered factory, so callers receive an
79
+ independent, mutation-safe document.
80
+ """
81
+ try:
82
+ factory = PRESETS[name]
83
+ except KeyError:
84
+ available = ", ".join(sorted(PRESETS.keys()))
85
+ raise KeyError(
86
+ f"unknown preset {name!r}; available presets: [{available}]"
87
+ ) from None
88
+ return factory()
89
+
90
+
91
+ def list_presets() -> list[str]:
92
+ """Names of all registered presets, sorted lexicographically."""
93
+ return sorted(PRESETS.keys())
@@ -0,0 +1,205 @@
1
+ # ============================================================================
2
+ #
3
+ # The Medical Imaging Interaction Toolkit (MITK)
4
+ #
5
+ # Copyright (c) German Cancer Research Center (DKFZ)
6
+ # All rights reserved.
7
+ #
8
+ # Use of this source code is governed by a 3-clause BSD license that can be
9
+ # found in the LICENSE file.
10
+ #
11
+ # ============================================================================
12
+
13
+ """Plain-text and HTML representations for the dataclass model.
14
+
15
+ HTML trust boundary: every string sourced from user-controlled content
16
+ (window id, window name, group name, view direction enum value, document
17
+ name, link dimension keys) passes through :func:`_html_escape` before
18
+ being interpolated. Inline styles only -- no JavaScript, no external
19
+ assets, no network.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ from html import escape as _stdlib_html_escape
25
+
26
+ from ._model import (
27
+ LayoutWindow,
28
+ LinkValue,
29
+ MxNLayoutDocument,
30
+ Split,
31
+ _in_dimension_order,
32
+ _link_only_groups,
33
+ )
34
+ from ._vocabulary import LinkDimension, RenderMode
35
+
36
+
37
+ _SELECTION = LinkDimension.SELECTION.value
38
+ _TIME = LinkDimension.TIME.value
39
+
40
+
41
+ def _navigation_links(window: LayoutWindow) -> list[tuple[str, LinkValue]]:
42
+ """The window's links other than selection and time. Every window links
43
+ time to the reserved group, so showing it tells nothing."""
44
+ return [
45
+ (dim, value)
46
+ for dim, value in _in_dimension_order(window._links)
47
+ if dim not in (_SELECTION, _TIME)
48
+ ]
49
+
50
+
51
+ def _link_text(value: LinkValue) -> str:
52
+ if isinstance(value, str):
53
+ return value
54
+ offset = value.offset
55
+ offset_text = ", ".join(str(v) for v in offset) if isinstance(offset, tuple) else str(offset)
56
+ return f"{value.group} ({offset_text})"
57
+
58
+
59
+ def _html_escape(text: str) -> str:
60
+ """Escape HTML metacharacters including single and double quotes.
61
+
62
+ Centralised so a future audit can grep for ``f"...{...}..."`` in
63
+ this module and confirm every interpolation runs through here.
64
+ """
65
+ return _stdlib_html_escape(text, quote=True)
66
+
67
+
68
+ # --------------------------------------------------------------------------- #
69
+ # Plain-text repr
70
+ # --------------------------------------------------------------------------- #
71
+
72
+
73
+ def window_repr(window: LayoutWindow) -> str:
74
+ parts = [
75
+ f"id={window.id!r}",
76
+ f"view_direction={window.view_direction.name}",
77
+ ]
78
+ if window.render_mode is RenderMode.THREE_D:
79
+ parts.append(f"render_mode={window.render_mode.name}")
80
+ parts.append(f"selection={window.selection!r}")
81
+ parts.extend(f"{dim}={value!r}" for dim, value in _navigation_links(window))
82
+ if window.name is not None:
83
+ parts.append(f"name={window.name!r}")
84
+ if window.size != 1:
85
+ parts.append(f"size={window.size}")
86
+ return f"LayoutWindow({', '.join(parts)})"
87
+
88
+
89
+ def split_repr(split: Split) -> str:
90
+ return (
91
+ f"Split(orientation={split.orientation!r}, "
92
+ f"children=<{len(split.children)} nodes>, size={split.size})"
93
+ )
94
+
95
+
96
+ def document_repr(doc: MxNLayoutDocument) -> str:
97
+ label = f"name={doc.name!r}, " if doc.name is not None else ""
98
+ group_count = len(doc.groups)
99
+ window_count = sum(1 for _ in doc.windows())
100
+ return (
101
+ f"MxNLayoutDocument({label}version={doc.version!r}, "
102
+ f"windows={window_count}, groups={group_count})"
103
+ )
104
+
105
+
106
+ # --------------------------------------------------------------------------- #
107
+ # HTML repr (notebook)
108
+ # --------------------------------------------------------------------------- #
109
+
110
+
111
+ _WINDOW_STYLE = (
112
+ "border:1px solid #999; padding:6px 8px; margin:2px; "
113
+ "font-family:sans-serif; min-width:120px; min-height:60px; "
114
+ "display:inline-block; vertical-align:top; background:#fafafa;"
115
+ )
116
+ _TABLE_STYLE = (
117
+ "border-collapse:collapse; margin:2px;"
118
+ )
119
+ _CELL_STYLE = (
120
+ "padding:0; vertical-align:top;"
121
+ )
122
+ _DOC_HEADER_STYLE = (
123
+ "font-family:sans-serif; padding:4px 0; color:#333;"
124
+ )
125
+
126
+
127
+ def window_html(window: LayoutWindow) -> str:
128
+ """Render a single window as a bordered ``<div>``."""
129
+ parts: list[str] = []
130
+ parts.append(f'<div style="{_WINDOW_STYLE}">')
131
+ if window.name is not None:
132
+ parts.append(
133
+ f'<div style="font-weight:600; margin-bottom:2px;">'
134
+ f"{_html_escape(window.name)}</div>"
135
+ )
136
+ parts.append(
137
+ f'<div style="font-family:monospace; font-size:0.85em; color:#666;">'
138
+ f"{_html_escape(window.id)}</div>"
139
+ )
140
+ view = window.view_direction.value
141
+ if window.render_mode is RenderMode.THREE_D:
142
+ view += f", {window.render_mode.value}"
143
+ parts.append(
144
+ f'<div style="font-size:0.9em;">view: '
145
+ f"{_html_escape(view)}</div>"
146
+ )
147
+ parts.append(
148
+ f'<div style="font-size:0.9em;">selection: '
149
+ f"{_html_escape(window.selection)}</div>"
150
+ )
151
+ for dim, value in _navigation_links(window):
152
+ parts.append(
153
+ f'<div style="font-size:0.9em;">'
154
+ f"{_html_escape(dim)}: {_html_escape(_link_text(value))}</div>"
155
+ )
156
+ parts.append("</div>")
157
+ return "".join(parts)
158
+
159
+
160
+ def _node_html(node: Split | LayoutWindow) -> str:
161
+ if isinstance(node, LayoutWindow):
162
+ return window_html(node)
163
+ return split_html(node)
164
+
165
+
166
+ def split_html(split: Split) -> str:
167
+ """Render a split as a tiny ``<table>`` whose layout (rows for
168
+ vertical, single row for horizontal) mirrors the splitter orientation.
169
+ """
170
+ children_html = [_node_html(c) for c in split.children]
171
+ if split.orientation == "horizontal":
172
+ cells = "".join(f'<td style="{_CELL_STYLE}">{html}</td>' for html in children_html)
173
+ return f'<table style="{_TABLE_STYLE}"><tr>{cells}</tr></table>'
174
+ rows = "".join(
175
+ f'<tr><td style="{_CELL_STYLE}">{html}</td></tr>' for html in children_html
176
+ )
177
+ return f'<table style="{_TABLE_STYLE}">{rows}</table>'
178
+
179
+
180
+ def document_html(doc: MxNLayoutDocument) -> str:
181
+ parts: list[str] = []
182
+ if doc.name is not None:
183
+ parts.append(
184
+ f'<div style="{_DOC_HEADER_STYLE} font-weight:600;">'
185
+ f"{_html_escape(doc.name)}</div>"
186
+ )
187
+ link_only = _link_only_groups(doc)
188
+
189
+ def role(name: str, select_all: bool | None) -> str:
190
+ if name in link_only:
191
+ return "link-only"
192
+ return "curated" if select_all is False else "select-all"
193
+
194
+ group_summary = ", ".join(
195
+ f"{_html_escape(name)} ({role(name, g.select_all)})"
196
+ for name, g in sorted(doc.groups.items())
197
+ )
198
+ parts.append(
199
+ f'<div style="{_DOC_HEADER_STYLE} font-size:0.9em; color:#555;">'
200
+ f"version {_html_escape(doc.version)} &middot; groups: "
201
+ f"{group_summary or '(none)'}"
202
+ f"</div>"
203
+ )
204
+ parts.append(split_html(doc.root))
205
+ return "".join(parts)
@@ -0,0 +1,363 @@
1
+ # ============================================================================
2
+ #
3
+ # The Medical Imaging Interaction Toolkit (MITK)
4
+ #
5
+ # Copyright (c) German Cancer Research Center (DKFZ)
6
+ # All rights reserved.
7
+ #
8
+ # Use of this source code is governed by a 3-clause BSD license that can be
9
+ # found in the LICENSE file.
10
+ #
11
+ # ============================================================================
12
+
13
+ """``MxNWindowSelector`` -- pandas-style filter + bulk transform over a layout tree.
14
+
15
+ A selector is bound to a *source* tree (``MxNLayoutDocument`` or ``Split``).
16
+ Filters return a new selector; terminals return either a value (read) or a
17
+ new instance of the source type (update). All immutable -- the source tree
18
+ is never mutated.
19
+
20
+ The selector is local-only: it knows nothing about HTTP, transports, or
21
+ workbench state. It operates on layout *documents* as values.
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ from collections.abc import Callable, Iterable, Iterator
27
+ from dataclasses import replace
28
+ from typing import Generic, TypeVar, Union
29
+
30
+ from . import _validation as _v
31
+ from ._model import (
32
+ Group,
33
+ LayoutWindow,
34
+ LinkLike,
35
+ MxNLayoutDocument,
36
+ Split,
37
+ _coerce_render_mode,
38
+ _default_group,
39
+ _link_only_groups,
40
+ _link_target,
41
+ _normalise_link_value,
42
+ _referenced_groups,
43
+ )
44
+ from ._vocabulary import LinkDimension, RenderMode, ViewDirection
45
+
46
+
47
+ _Source = TypeVar("_Source", MxNLayoutDocument, Split)
48
+
49
+
50
+ def _coerce_view_direction(value: ViewDirection | str) -> ViewDirection:
51
+ if isinstance(value, ViewDirection):
52
+ return value
53
+ if isinstance(value, str):
54
+ return ViewDirection(value)
55
+ raise ValueError(f"view direction must be ViewDirection or str, got {type(value).__name__}")
56
+
57
+
58
+ def _root_of(source: _Source) -> Split:
59
+ return source.root if isinstance(source, MxNLayoutDocument) else source
60
+
61
+
62
+ def _rewrap(source: _Source, new_root: Split) -> _Source:
63
+ """Rebuild the source with a new root, preserving document metadata."""
64
+ if isinstance(source, MxNLayoutDocument):
65
+ return replace(source, root=new_root) # type: ignore[return-value]
66
+ return new_root # type: ignore[return-value]
67
+
68
+
69
+ def _rewrite_tree(
70
+ node: Split | LayoutWindow,
71
+ transform: Callable[[LayoutWindow], LayoutWindow],
72
+ keep: Callable[[LayoutWindow], bool],
73
+ ) -> Split | LayoutWindow:
74
+ """Walk the tree and replace each leaf for which ``keep`` returns True
75
+ with ``transform(leaf)``; pass through other leaves. Splits are
76
+ rebuilt structurally (size/orientation preserved).
77
+ """
78
+ if isinstance(node, LayoutWindow):
79
+ return transform(node) if keep(node) else node
80
+ new_children = tuple(_rewrite_tree(c, transform, keep) for c in node.children)
81
+ if new_children == node.children:
82
+ return node
83
+ return replace(node, children=new_children)
84
+
85
+
86
+ def _ensure_group(source: _Source, group_name: str, *, named_by_selection: bool) -> _Source:
87
+ """When the source is a document and ``group_name`` is new to it, give
88
+ it the entry ``groups=None`` would materialise. On ``Split`` sources
89
+ this is a no-op (groups live on the document). The reserved group never
90
+ takes an entry.
91
+ """
92
+ if not isinstance(source, MxNLayoutDocument) or _v.names_global_group(group_name):
93
+ return source
94
+ if group_name in source.groups:
95
+ return source
96
+ new_groups = dict(source.groups)
97
+ new_groups[group_name] = _default_group(group_name, named_by_selection=named_by_selection)
98
+ new_storage = tuple(sorted(new_groups.items()))
99
+ return replace(source, _groups=new_storage) # type: ignore[return-value]
100
+
101
+
102
+ def _keep_group_roles(before: _Source, after: _Source) -> _Source:
103
+ """``after`` with the group entries the editor holds after it loaded
104
+ ``before`` and made the same edit. The editor fixes a group's role at
105
+ load: it registers a group a selection link names, keeps it registered
106
+ when its last selection link moves away, and writes ``select_all`` for
107
+ it, so an entry without ``select_all`` gets it when a selection link
108
+ starts or stops naming its group. It drops a link-only group with its
109
+ last link, so the entry of a group that was link-only in ``before`` and
110
+ that no link of ``after`` names goes. A ``Split`` has no groups."""
111
+ if not isinstance(before, MxNLayoutDocument) or not isinstance(after, MxNLayoutDocument):
112
+ return after
113
+ link_only_before = _link_only_groups(before)
114
+ named_before = _referenced_groups(before.root)
115
+ named_after = _referenced_groups(after.root)
116
+ entries = []
117
+ for name, group in after._groups:
118
+ if name in link_only_before and name not in named_after:
119
+ continue
120
+ by_selection_before = named_before.get(name) is True
121
+ by_selection_after = named_after.get(name) is True
122
+ if group.select_all is None and by_selection_before != by_selection_after:
123
+ group = replace(group, select_all=True)
124
+ entries.append((name, group))
125
+ return replace(after, _groups=tuple(entries)) # type: ignore[return-value]
126
+
127
+
128
+ class MxNWindowSelector(Generic[_Source]):
129
+ """Filter + bulk-transform over a tree's windows.
130
+
131
+ Each filter call returns a new selector; each terminal call returns
132
+ a value (read) or a new ``T`` (update). All operations are immutable.
133
+ """
134
+
135
+ __slots__ = ("_source", "_predicate")
136
+
137
+ def __init__(
138
+ self,
139
+ source: _Source,
140
+ predicate: Callable[[LayoutWindow], bool] | None = None,
141
+ ) -> None:
142
+ self._source = source
143
+ self._predicate: Callable[[LayoutWindow], bool] = predicate or (lambda _w: True)
144
+
145
+ # ---- internals ------------------------------------------------------
146
+
147
+ def _and(
148
+ self, extra: Callable[[LayoutWindow], bool]
149
+ ) -> "MxNWindowSelector[_Source]":
150
+ prior = self._predicate
151
+ return MxNWindowSelector(self._source, lambda w: prior(w) and extra(w))
152
+
153
+ def _selected_windows(self) -> list[LayoutWindow]:
154
+ return [w for w in _root_of(self._source).windows() if self._predicate(w)]
155
+
156
+ # ---- filtering ------------------------------------------------------
157
+
158
+ def where(
159
+ self,
160
+ *,
161
+ view_direction: ViewDirection | str | None = None,
162
+ ids: Iterable[str] | None = None,
163
+ display_names: Iterable[str | None] | None = None,
164
+ predicate: Callable[[LayoutWindow], bool] | None = None,
165
+ group: str | Group | None = None,
166
+ dim: LinkDimension | str = LinkDimension.SELECTION,
167
+ ) -> "MxNWindowSelector[_Source]":
168
+ """Compose multiple filter predicates AND-style.
169
+
170
+ All keyword arguments are optional; the resulting selector keeps
171
+ only windows that satisfy every supplied predicate.
172
+ """
173
+ result: MxNWindowSelector[_Source] = self
174
+ if view_direction is not None:
175
+ target_view = _coerce_view_direction(view_direction)
176
+ result = result._and(lambda w, v=target_view: w.view_direction == v)
177
+ if ids is not None:
178
+ id_set = set(ids)
179
+ result = result._and(lambda w, s=id_set: w.id in s)
180
+ if display_names is not None:
181
+ name_set = set(display_names)
182
+ result = result._and(lambda w, s=name_set: w.name in s)
183
+ if group is not None:
184
+ target_group = group.name if isinstance(group, Group) else group
185
+ dim_key = _v.validate_dimension_key(dim)
186
+ result = result._and(
187
+ lambda w, g=target_group, d=dim_key: w.linked_to(g, dim=d)
188
+ )
189
+ if predicate is not None:
190
+ result = result._and(predicate)
191
+ return result
192
+
193
+ def by_id(self, *ids: str) -> "MxNWindowSelector[_Source]":
194
+ """Keep only windows whose ids match any of ``ids``."""
195
+ return self.where(ids=ids)
196
+
197
+ def by_display_name(
198
+ self, *names: str | None
199
+ ) -> "MxNWindowSelector[_Source]":
200
+ """Keep only windows whose display names match any of ``names``.
201
+
202
+ ``None`` matches windows that have no display label set.
203
+ """
204
+ return self.where(display_names=names)
205
+
206
+ def by_view(
207
+ self, *view_directions: ViewDirection | str
208
+ ) -> "MxNWindowSelector[_Source]":
209
+ """Keep only windows in any of the supplied view directions."""
210
+ coerced = [_coerce_view_direction(v) for v in view_directions]
211
+ coerced_set = set(coerced)
212
+ return self._and(lambda w, s=coerced_set: w.view_direction in s)
213
+
214
+ # ---- terminals: read ------------------------------------------------
215
+
216
+ def to_list(self) -> list[LayoutWindow]:
217
+ return self._selected_windows()
218
+
219
+ def ids(self) -> list[str]:
220
+ return [w.id for w in self._selected_windows()]
221
+
222
+ def __iter__(self) -> Iterator[LayoutWindow]:
223
+ return iter(self._selected_windows())
224
+
225
+ def __len__(self) -> int:
226
+ return len(self._selected_windows())
227
+
228
+ # ---- terminals: bulk update ----------------------------------------
229
+
230
+ def link_to(
231
+ self,
232
+ link: LinkLike,
233
+ *,
234
+ dim: LinkDimension | str = LinkDimension.SELECTION,
235
+ ) -> _Source:
236
+ """Re-link every selected window to ``link`` on dimension ``dim``.
237
+
238
+ ``link`` may carry an offset (``Link``) on slice, zoom and pan, and
239
+ may be the reserved group :data:`GLOBAL_GROUP_NAME` on slice and
240
+ crosshair. On a document source, keeps the groups in step with the
241
+ links, as the editor does: a group name new to the document gets a
242
+ default ``Group`` entry, a group a selection link names gets
243
+ ``select_all`` and keeps it when its last selection link moves away,
244
+ and the entry of a link-only group goes with its last link. A
245
+ declared group without ``select_all`` that no window links becomes
246
+ link-only once only navigation links name it. On a ``Split`` source
247
+ there is no group registry to update.
248
+ """
249
+ dim_key = _v.validate_dimension_key(dim)
250
+ new_value = _normalise_link_value(dim_key, link)
251
+
252
+ ids = {w.id for w in self._selected_windows()}
253
+
254
+ def transform(window: LayoutWindow) -> LayoutWindow:
255
+ return window.with_link(dim_key, new_value)
256
+
257
+ new_root = _rewrite_tree(_root_of(self._source), transform, lambda w: w.id in ids)
258
+ assert isinstance(new_root, Split)
259
+ new_source = _ensure_group(
260
+ _rewrap(self._source, new_root),
261
+ _link_target(new_value),
262
+ named_by_selection=dim_key == LinkDimension.SELECTION.value,
263
+ )
264
+ return _keep_group_roles(self._source, new_source)
265
+
266
+ def unlink(self, *, dim: LinkDimension | str) -> _Source:
267
+ """Unlink every selected window on dimension ``dim``, offset
268
+ included; a window not linked on ``dim`` stays as it is.
269
+
270
+ Unlinked, a window keeps its own value on orientation, pan, zoom,
271
+ slice and crosshair; on slice and crosshair it then no longer
272
+ follows the application's position, as a link to
273
+ :data:`GLOBAL_GROUP_NAME` does. On windowing and lut it shows the
274
+ data node's own level/window and colormap, which the application's
275
+ controls change. Raises ``ValueError`` for ``selection`` and
276
+ ``time``, which every window links, also when nothing is selected.
277
+
278
+ On a document source, the entry of a link-only group goes with its
279
+ last link, as the editor drops such a group. On a ``Split`` source
280
+ there is no group registry to update.
281
+ """
282
+ dim_key = _v.validate_unlinkable_dimension(dim)
283
+ ids = {w.id for w in self._selected_windows()}
284
+ new_root = _rewrite_tree(
285
+ _root_of(self._source), lambda w: w.without_link(dim_key), lambda w: w.id in ids
286
+ )
287
+ assert isinstance(new_root, Split)
288
+ return _keep_group_roles(self._source, _rewrap(self._source, new_root))
289
+
290
+ def with_view_direction(self, vd: ViewDirection | str) -> _Source:
291
+ """Give every selected window the view direction ``vd``; ``free``
292
+ only on 3D windows. To turn a 2D window into a ``free`` 3D view in
293
+ one step, use :meth:`with_render_mode`."""
294
+ target = _coerce_view_direction(vd)
295
+ ids = {w.id for w in self._selected_windows()}
296
+
297
+ def transform(window: LayoutWindow) -> LayoutWindow:
298
+ _v.validate_view_for_render_mode(window.id, target, window.render_mode)
299
+ return replace(window, view_direction=target)
300
+
301
+ new_root = _rewrite_tree(_root_of(self._source), transform, lambda w: w.id in ids)
302
+ assert isinstance(new_root, Split)
303
+ return _rewrap(self._source, new_root)
304
+
305
+ def with_render_mode(
306
+ self,
307
+ mode: RenderMode | str,
308
+ *,
309
+ view_direction: ViewDirection | str | None = None,
310
+ ) -> _Source:
311
+ """Turn every selected window into a 3D view, or back into a 2D one,
312
+ as :meth:`LayoutWindow.with_render_mode` does; without
313
+ ``view_direction`` each window keeps its own direction. Raises
314
+ ``ValueError`` for ``free`` in 2D, also when nothing is selected."""
315
+ target_mode = _coerce_render_mode(mode)
316
+ target_view = None if view_direction is None else _coerce_view_direction(view_direction)
317
+ if target_view is ViewDirection.FREE and target_mode is not RenderMode.THREE_D:
318
+ raise ValueError("view_direction 'free' needs render_mode '3d'")
319
+ ids = {w.id for w in self._selected_windows()}
320
+ new_root = _rewrite_tree(
321
+ _root_of(self._source),
322
+ lambda w: w.with_render_mode(target_mode, view_direction=target_view),
323
+ lambda w: w.id in ids,
324
+ )
325
+ assert isinstance(new_root, Split)
326
+ return _rewrap(self._source, new_root)
327
+
328
+ def with_display_name(
329
+ self,
330
+ name: Union[str, None, Callable[[LayoutWindow], str | None]],
331
+ ) -> _Source:
332
+ ids = {w.id for w in self._selected_windows()}
333
+
334
+ if callable(name):
335
+ fn = name
336
+
337
+ def transform(window: LayoutWindow) -> LayoutWindow:
338
+ return window.with_name(fn(window))
339
+
340
+ else:
341
+ value: str | None = name
342
+
343
+ def transform(window: LayoutWindow) -> LayoutWindow:
344
+ return window.with_name(value)
345
+
346
+ new_root = _rewrite_tree(_root_of(self._source), transform, lambda w: w.id in ids)
347
+ assert isinstance(new_root, Split)
348
+ return _rewrap(self._source, new_root)
349
+
350
+ def map(
351
+ self, fn: Callable[[LayoutWindow], LayoutWindow]
352
+ ) -> _Source:
353
+ """Escape hatch -- replace each selected window with ``fn(window)``.
354
+
355
+ The selector preserves no invariants beyond what
356
+ ``MxNLayoutDocument.create`` enforces on construction; if the
357
+ caller introduces a duplicate id, ``MxNLayoutDocument.validate()``
358
+ catches it on the next ``validate()`` or ``to_json()`` call.
359
+ """
360
+ ids = {w.id for w in self._selected_windows()}
361
+ new_root = _rewrite_tree(_root_of(self._source), fn, lambda w: w.id in ids)
362
+ assert isinstance(new_root, Split)
363
+ return _rewrap(self._source, new_root)