-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_ui.py
More file actions
457 lines (376 loc) · 20.3 KB
/
template_ui.py
File metadata and controls
457 lines (376 loc) · 20.3 KB
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
"""
Template UI - Template editor dialog and helper functions
Contains the TemplateEditorDialog and template management functions
"""
import wx
import wx.lib.scrolledpanel
class TemplateEditorDialog(wx.Dialog):
"""Full-featured template editor with all CLI parameters."""
def __init__(self, parent, template=None, template_type="static", is_new=True):
title = "Create New Template" if is_new else f"Edit Template"
super().__init__(parent, title=title, size=(580, 720),
style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
self.template_type = template_type
self.is_new = is_new
self.result = None
self._updating_aspect = False # Prevent recursive updates
# Get board dimensions and aspect ratio from parent dialog if available
self._board_aspect = 1.0
self._board_width = 100.0 # Default mm
self._board_height = 100.0
if hasattr(parent, 'pcb_path') and parent.pcb_path:
try:
from .board_utils import get_board_aspect_ratio, get_board_dimensions
self._board_aspect = get_board_aspect_ratio(parent.pcb_path)
dims = get_board_dimensions(parent.pcb_path)
if dims[0] and dims[1]:
self._board_width, self._board_height = dims
except Exception:
pass # Use defaults
# Initialize with template data or defaults
from .template_manager import get_empty_template
if template:
self.template = template.copy()
self.template["cli_params"] = template.get("cli_params", {}).copy()
else:
self.template = get_empty_template(template_type)
self._create_ui()
self._load_values()
self.Centre()
def _create_ui(self):
"""Create the editor UI."""
panel = wx.Panel(self)
main_sizer = wx.BoxSizer(wx.VERTICAL)
# Scrolled area for all controls
scroll = wx.lib.scrolledpanel.ScrolledPanel(panel, style=wx.VSCROLL)
scroll.SetupScrolling(scroll_x=False)
sizer = wx.BoxSizer(wx.VERTICAL)
# === Basic Info ===
info_box = wx.StaticBox(scroll, label="Template Info")
info_sizer = wx.StaticBoxSizer(info_box, wx.VERTICAL)
grid = wx.FlexGridSizer(3, 2, 5, 10)
grid.AddGrowableCol(1)
grid.Add(wx.StaticText(scroll, label="Name:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.name_ctrl = wx.TextCtrl(scroll, value="")
grid.Add(self.name_ctrl, 1, wx.EXPAND)
grid.Add(wx.StaticText(scroll, label="Description:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.desc_ctrl = wx.TextCtrl(scroll, value="")
grid.Add(self.desc_ctrl, 1, wx.EXPAND)
grid.Add(wx.StaticText(scroll, label="Type:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.type_ctrl = wx.Choice(scroll, choices=["spinrender", "static", "pinout"])
self.type_ctrl.SetStringSelection(self.template_type)
self.type_ctrl.Bind(wx.EVT_CHOICE, self._on_type_change)
grid.Add(self.type_ctrl, 1, wx.EXPAND)
info_sizer.Add(grid, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(info_sizer, 0, wx.EXPAND | wx.ALL, 5)
# === Image Settings ===
img_box = wx.StaticBox(scroll, label="Image Settings")
img_sizer = wx.StaticBoxSizer(img_box, wx.VERTICAL)
img_grid = wx.FlexGridSizer(5, 4, 5, 10)
img_grid.AddGrowableCol(1)
img_grid.AddGrowableCol(3)
# Width
img_grid.Add(wx.StaticText(scroll, label="Width:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.width_ctrl = wx.SpinCtrl(scroll, min=100, max=8192, initial=1920)
img_grid.Add(self.width_ctrl, 1, wx.EXPAND)
# Height
img_grid.Add(wx.StaticText(scroll, label="Height:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.height_ctrl = wx.SpinCtrl(scroll, min=100, max=8192, initial=1080)
img_grid.Add(self.height_ctrl, 1, wx.EXPAND)
# Lock aspect ratio
self.lock_aspect_ctrl = wx.CheckBox(scroll, label="🔗 Lock to board ratio")
self.lock_aspect_ctrl.SetValue(True)
self.lock_aspect_ctrl.SetToolTip(f"Auto-adjust dimensions to match board ratio ({self._board_aspect:.2f})")
img_grid.Add(self.lock_aspect_ctrl, 0, wx.ALIGN_CENTER_VERTICAL)
img_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
# Bind width/height change events for aspect ratio lock
self.width_ctrl.Bind(wx.EVT_SPINCTRL, self._on_width_changed)
self.height_ctrl.Bind(wx.EVT_SPINCTRL, self._on_height_changed)
# Side
img_grid.Add(wx.StaticText(scroll, label="Side:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.side_ctrl = wx.Choice(scroll, choices=["top", "bottom", "left", "right", "front", "back"])
self.side_ctrl.SetSelection(0)
img_grid.Add(self.side_ctrl, 1, wx.EXPAND)
# Background
img_grid.Add(wx.StaticText(scroll, label="Background:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.bg_ctrl = wx.Choice(scroll, choices=["transparent", "opaque"])
self.bg_ctrl.SetSelection(0)
img_grid.Add(self.bg_ctrl, 1, wx.EXPAND)
# Quality
img_grid.Add(wx.StaticText(scroll, label="Quality:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.quality_ctrl = wx.Choice(scroll, choices=["basic", "high", "user"])
self.quality_ctrl.SetSelection(1)
img_grid.Add(self.quality_ctrl, 1, wx.EXPAND)
# Preset
img_grid.Add(wx.StaticText(scroll, label="Preset:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.preset_ctrl = wx.TextCtrl(scroll, value="follow_plot_settings")
img_grid.Add(self.preset_ctrl, 1, wx.EXPAND)
img_sizer.Add(img_grid, 0, wx.EXPAND | wx.ALL, 5)
# Checkboxes row
self.stackup_ctrl = wx.CheckBox(scroll, label="Use board stackup colors")
self.perspective_ctrl = wx.CheckBox(scroll, label="Use perspective projection")
cb_sizer = wx.BoxSizer(wx.HORIZONTAL)
cb_sizer.Add(self.stackup_ctrl, 0, wx.RIGHT, 15)
cb_sizer.Add(self.perspective_ctrl, 0)
img_sizer.Add(cb_sizer, 0, wx.ALL, 5)
sizer.Add(img_sizer, 0, wx.EXPAND | wx.ALL, 5)
# === Safe Zoom Settings (for spinrender) ===
self.safe_zoom_box = wx.StaticBox(scroll, label="Safe Zoom Settings (Animation)")
safe_zoom_sizer = wx.StaticBoxSizer(self.safe_zoom_box, wx.VERTICAL)
safe_grid = wx.FlexGridSizer(2, 4, 5, 10)
safe_grid.AddGrowableCol(1)
safe_grid.AddGrowableCol(3)
# Auto-safe checkbox
self.auto_safe_ctrl = wx.CheckBox(scroll, label="🔒 Auto-safe zoom")
self.auto_safe_ctrl.SetValue(True)
self.auto_safe_ctrl.SetToolTip("Automatically limit zoom to prevent corner clipping during rotation")
self.auto_safe_ctrl.Bind(wx.EVT_CHECKBOX, self._on_safe_zoom_settings_changed)
safe_grid.Add(self.auto_safe_ctrl, 0, wx.ALIGN_CENTER_VERTICAL)
safe_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
safe_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
safe_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
# Safety margin
safe_grid.Add(wx.StaticText(scroll, label="Safety margin:"), 0, wx.ALIGN_CENTER_VERTICAL)
margin_row = wx.BoxSizer(wx.HORIZONTAL)
self.safety_margin_ctrl = wx.SpinCtrl(scroll, min=0, max=50, initial=5, size=(60, -1))
self.safety_margin_ctrl.SetToolTip("Extra buffer % to prevent clipping (5-10% recommended)")
self.safety_margin_ctrl.Bind(wx.EVT_SPINCTRL, self._on_safe_zoom_settings_changed)
margin_row.Add(self.safety_margin_ctrl, 0, wx.RIGHT, 2)
margin_row.Add(wx.StaticText(scroll, label="%"), 0, wx.ALIGN_CENTER_VERTICAL)
safe_grid.Add(margin_row, 1, wx.EXPAND)
# Component height
safe_grid.Add(wx.StaticText(scroll, label="Component height:"), 0, wx.ALIGN_CENTER_VERTICAL)
height_row = wx.BoxSizer(wx.HORIZONTAL)
self.component_height_ctrl = wx.SpinCtrl(scroll, min=0, max=100, initial=10, size=(60, -1))
self.component_height_ctrl.SetToolTip("Max height of tallest component (mm). Include connectors, caps, heatsinks, etc.")
self.component_height_ctrl.Bind(wx.EVT_SPINCTRL, self._on_safe_zoom_settings_changed)
height_row.Add(self.component_height_ctrl, 0, wx.RIGHT, 2)
height_row.Add(wx.StaticText(scroll, label="mm"), 0, wx.ALIGN_CENTER_VERTICAL)
safe_grid.Add(height_row, 1, wx.EXPAND)
safe_zoom_sizer.Add(safe_grid, 0, wx.EXPAND | wx.ALL, 5)
self.safe_zoom_sizer = safe_zoom_sizer
sizer.Add(safe_zoom_sizer, 0, wx.EXPAND | wx.ALL, 5)
# === Camera Settings ===
cam_box = wx.StaticBox(scroll, label="Camera Settings")
cam_sizer = wx.StaticBoxSizer(cam_box, wx.VERTICAL)
cam_grid = wx.FlexGridSizer(4, 4, 5, 10)
cam_grid.AddGrowableCol(1)
cam_grid.AddGrowableCol(3)
# Zoom with slider
cam_grid.Add(wx.StaticText(scroll, label="Zoom:"), 0, wx.ALIGN_CENTER_VERTICAL)
zoom_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.zoom_slider = wx.Slider(scroll, value=100, minValue=10, maxValue=500, size=(120, -1))
self.zoom_ctrl = wx.SpinCtrlDouble(scroll, min=0.1, max=5.0, initial=1.0, inc=0.1, size=(70, -1))
self.zoom_slider.Bind(wx.EVT_SLIDER, self._on_zoom_slider)
self.zoom_ctrl.Bind(wx.EVT_SPINCTRLDOUBLE, self._on_zoom_spin)
zoom_sizer.Add(self.zoom_slider, 1, wx.EXPAND | wx.RIGHT, 5)
zoom_sizer.Add(self.zoom_ctrl, 0)
cam_grid.Add(zoom_sizer, 1, wx.EXPAND)
# Safe zoom info label (spans 2 columns)
cam_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
self.zoom_info_label = wx.StaticText(scroll, label="")
self.zoom_info_label.SetForegroundColour(wx.Colour(180, 120, 0)) # Warning orange
cam_grid.Add(self.zoom_info_label, 0, wx.ALIGN_CENTER_VERTICAL)
# Pan
cam_grid.Add(wx.StaticText(scroll, label="Pan (X,Y,Z):"), 0, wx.ALIGN_CENTER_VERTICAL)
self.pan_ctrl = wx.TextCtrl(scroll, value="")
self.pan_ctrl.SetToolTip("Camera pan offset, e.g. '3,0,0'")
cam_grid.Add(self.pan_ctrl, 1, wx.EXPAND)
# Pivot
cam_grid.Add(wx.StaticText(scroll, label="Pivot (X,Y,Z):"), 0, wx.ALIGN_CENTER_VERTICAL)
self.pivot_ctrl = wx.TextCtrl(scroll, value="")
self.pivot_ctrl.SetToolTip("Pivot point relative to board center in cm")
cam_grid.Add(self.pivot_ctrl, 1, wx.EXPAND)
# Rotate
cam_grid.Add(wx.StaticText(scroll, label="Rotate (X,Y,Z):"), 0, wx.ALIGN_CENTER_VERTICAL)
self.rotate_ctrl = wx.TextCtrl(scroll, value="")
self.rotate_ctrl.SetToolTip("Rotation in degrees, e.g. '-45,0,45' for isometric")
self.rotate_ctrl.Bind(wx.EVT_TEXT, self._on_rotate_changed)
cam_grid.Add(self.rotate_ctrl, 1, wx.EXPAND)
cam_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
cam_grid.Add(wx.StaticText(scroll, label=""), 0) # Spacer
cam_sizer.Add(cam_grid, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(cam_sizer, 0, wx.EXPAND | wx.ALL, 5)
scroll.SetSizer(sizer)
main_sizer.Add(scroll, 1, wx.EXPAND)
# === Buttons ===
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
save_btn = wx.Button(panel, wx.ID_OK, "Save")
cancel_btn = wx.Button(panel, wx.ID_CANCEL, "Cancel")
btn_sizer.Add(save_btn, 0, wx.RIGHT, 10)
btn_sizer.Add(cancel_btn, 0)
main_sizer.Add(btn_sizer, 0, wx.ALIGN_CENTER | wx.ALL, 10)
panel.SetSizer(main_sizer)
save_btn.Bind(wx.EVT_BUTTON, self._on_save)
cancel_btn.Bind(wx.EVT_BUTTON, lambda e: self.EndModal(wx.ID_CANCEL))
def _on_zoom_slider(self, event):
"""Sync zoom slider to spin control."""
self.zoom_ctrl.SetValue(self.zoom_slider.GetValue() / 100.0)
self._update_zoom_info()
def _on_zoom_spin(self, event):
"""Sync zoom spin to slider."""
self.zoom_slider.SetValue(int(self.zoom_ctrl.GetValue() * 100))
self._update_zoom_info()
def _update_zoom_info(self):
"""Update the safe zoom warning display."""
user_zoom = self.zoom_ctrl.GetValue()
rotate_str = self.rotate_ctrl.GetValue().strip()
# Parse rotation values
tilt_x, tilt_y, tilt_z = 0, 0, 0
if rotate_str:
try:
parts = [float(p.strip()) for p in rotate_str.split(',')]
if len(parts) >= 1: tilt_x = parts[0]
if len(parts) >= 2: tilt_y = parts[1]
if len(parts) >= 3: tilt_z = parts[2]
except ValueError:
pass
# Only show safe zoom info if auto-safe is enabled and we have rotation
if not self.auto_safe_ctrl.GetValue() or (tilt_x == 0 and tilt_y == 0 and tilt_z == 0):
self.zoom_info_label.SetLabel("")
return
try:
# For animation templates, use animation safe zoom (considers 360° rotation)
template_type = self.type_ctrl.GetStringSelection()
component_height = self.component_height_ctrl.GetValue()
margin = self.safety_margin_ctrl.GetValue() / 100.0
if template_type == "spinrender":
from .rotation_geometry import calculate_safe_zoom_for_animation
safe_zoom = calculate_safe_zoom_for_animation(
self._board_width, self._board_height, tilt_x, tilt_y,
component_height, margin
)
else:
from .rotation_geometry import calculate_safe_zoom
safe_zoom = calculate_safe_zoom(
self._board_width, self._board_height, tilt_x, tilt_y, tilt_z,
component_height, margin
)
if user_zoom > safe_zoom:
self.zoom_info_label.SetLabel(f"⚠️ Will adjust to {safe_zoom:.2f} (prevents clipping)")
self.zoom_info_label.SetForegroundColour(wx.Colour(180, 120, 0)) # Warning orange
else:
self.zoom_info_label.SetLabel(f"✓ Safe (max: {safe_zoom:.2f})")
self.zoom_info_label.SetForegroundColour(wx.Colour(0, 140, 0)) # Green
except Exception:
self.zoom_info_label.SetLabel("")
def _on_width_changed(self, event):
"""Auto-adjust height when width changes (if locked)."""
if self._updating_aspect or not self.lock_aspect_ctrl.GetValue():
event.Skip()
return
self._updating_aspect = True
try:
from .board_utils import calculate_dimensions_for_width
new_width = self.width_ctrl.GetValue()
new_height = calculate_dimensions_for_width(new_width, self._board_aspect)
self.height_ctrl.SetValue(new_height)
finally:
self._updating_aspect = False
event.Skip()
def _on_height_changed(self, event):
"""Auto-adjust width when height changes (if locked)."""
if self._updating_aspect or not self.lock_aspect_ctrl.GetValue():
event.Skip()
return
self._updating_aspect = True
try:
from .board_utils import calculate_dimensions_for_height
new_height = self.height_ctrl.GetValue()
new_width = calculate_dimensions_for_height(new_height, self._board_aspect)
self.width_ctrl.SetValue(new_width)
finally:
self._updating_aspect = False
event.Skip()
def _on_rotate_changed(self, event):
"""Update safe zoom info when rotation changes."""
self._update_zoom_info()
event.Skip()
def _on_safe_zoom_settings_changed(self, event):
"""Update safe zoom info when auto-safe settings change."""
self._update_zoom_info()
event.Skip()
def _on_type_change(self, event):
"""Show/hide type-specific controls."""
template_type = self.type_ctrl.GetStringSelection()
# Safe zoom settings only apply to spinrender (animation)
show_safe_zoom = (template_type == "spinrender")
self.safe_zoom_box.Show(show_safe_zoom)
self._update_zoom_info() # Recalculate for new template type
self.Layout()
self.Refresh()
def _load_values(self):
"""Load template values into controls."""
self.name_ctrl.SetValue(self.template.get("name", ""))
self.desc_ctrl.SetValue(self.template.get("description", ""))
params = self.template.get("cli_params", {})
# Image settings
self.width_ctrl.SetValue(params.get("width", 1920))
self.height_ctrl.SetValue(params.get("height", 1080))
self.lock_aspect_ctrl.SetValue(params.get("lock_aspect", True))
side = params.get("side", "top")
idx = self.side_ctrl.FindString(side)
if idx != wx.NOT_FOUND:
self.side_ctrl.SetSelection(idx)
bg = params.get("background", "transparent")
idx = self.bg_ctrl.FindString(bg)
if idx != wx.NOT_FOUND:
self.bg_ctrl.SetSelection(idx)
quality = params.get("quality", "high")
idx = self.quality_ctrl.FindString(quality)
if idx != wx.NOT_FOUND:
self.quality_ctrl.SetSelection(idx)
self.preset_ctrl.SetValue(params.get("preset", "follow_plot_settings"))
self.stackup_ctrl.SetValue(params.get("use_board_stackup_colors", False))
self.perspective_ctrl.SetValue(params.get("perspective", False))
# Safe zoom settings (for spinrender)
self.auto_safe_ctrl.SetValue(params.get("auto_safe_zoom", True))
self.safety_margin_ctrl.SetValue(params.get("safety_margin", 5))
self.component_height_ctrl.SetValue(params.get("component_height", 10))
# Camera
zoom = params.get("zoom", 1.0)
self.zoom_ctrl.SetValue(zoom)
self.zoom_slider.SetValue(int(zoom * 100))
self.pan_ctrl.SetValue(params.get("pan", ""))
self.pivot_ctrl.SetValue(params.get("pivot", ""))
self.rotate_ctrl.SetValue(params.get("rotate", ""))
# Show/hide type-specific controls and update zoom info
self._on_type_change(None)
self._update_zoom_info()
def _on_save(self, event):
"""Save the template."""
from .template_manager import save_template
from datetime import datetime
name = self.name_ctrl.GetValue().strip()
if not name:
wx.MessageBox("Template name is required.", "Error", wx.OK | wx.ICON_ERROR)
return
# Build template
self.template["name"] = name
self.template["description"] = self.desc_ctrl.GetValue()
self.template["template_type"] = self.type_ctrl.GetStringSelection()
self.template["modified"] = datetime.now().isoformat()
# CLI params
params = self.template["cli_params"]
params["width"] = self.width_ctrl.GetValue()
params["height"] = self.height_ctrl.GetValue()
params["lock_aspect"] = self.lock_aspect_ctrl.GetValue()
params["side"] = self.side_ctrl.GetStringSelection()
params["background"] = self.bg_ctrl.GetStringSelection()
params["quality"] = self.quality_ctrl.GetStringSelection()
params["preset"] = self.preset_ctrl.GetValue()
params["use_board_stackup_colors"] = self.stackup_ctrl.GetValue()
params["perspective"] = self.perspective_ctrl.GetValue()
# Safe zoom settings (for spinrender)
params["auto_safe_zoom"] = self.auto_safe_ctrl.GetValue()
params["safety_margin"] = self.safety_margin_ctrl.GetValue()
params["component_height"] = self.component_height_ctrl.GetValue()
# Camera
params["zoom"] = self.zoom_ctrl.GetValue()
params["pan"] = self.pan_ctrl.GetValue()
params["pivot"] = self.pivot_ctrl.GetValue()
params["rotate"] = self.rotate_ctrl.GetValue()
# Save
filepath = save_template(self.template, name)
self.result = self.template
self.EndModal(wx.ID_OK)