-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinout_tab.py
More file actions
275 lines (224 loc) · 13 KB
/
pinout_tab.py
File metadata and controls
275 lines (224 loc) · 13 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
"""
Pinout Tab - 2D Documentation/Pinout generation UI
"""
import os
import json
import wx
import wx.lib.scrolledpanel as scrolled
def create_pinout_tab(parent, dialog):
"""Create the Pinout/2D Documentation tab."""
import time
t0 = time.time()
# OPTIMIZATION: Freeze updates during widget creation
parent.Freeze()
try:
from .pinout import check_pil
from .tabs import (_on_template_selected, _show_manage_menu, _save_as_template,
_refresh_template_dropdown, _show_preview, _on_pinout_format_change,
_open_template_folder)
outer = wx.Panel(parent)
outer_sizer = wx.BoxSizer(wx.VERTICAL)
# Template selector at top
tmpl_sizer = wx.BoxSizer(wx.HORIZONTAL)
tmpl_sizer.Add(wx.StaticText(outer, label="Template:"), 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)
dialog.pinout_template_choice = wx.Choice(outer, size=(180, -1))
dialog.pinout_template_choice.Bind(wx.EVT_CHOICE, lambda e: _on_template_selected(dialog, "pinout"))
tmpl_sizer.Add(dialog.pinout_template_choice, 0, wx.RIGHT, 5)
manage_btn = wx.Button(outer, label="⚙", size=(30, -1))
manage_btn.SetToolTip("Manage templates")
manage_btn.Bind(wx.EVT_BUTTON, lambda e: _show_manage_menu(dialog, manage_btn, "pinout"))
tmpl_sizer.Add(manage_btn, 0, wx.RIGHT, 2)
folder_btn = wx.Button(outer, label="📁", size=(30, -1))
folder_btn.SetToolTip("Open template folder")
folder_btn.Bind(wx.EVT_BUTTON, lambda e: _open_template_folder())
tmpl_sizer.Add(folder_btn, 0, wx.RIGHT, 2)
create_btn = wx.Button(outer, label="+", size=(30, -1))
create_btn.SetToolTip("Create new template")
create_btn.Bind(wx.EVT_BUTTON, lambda e: _save_as_template(dialog, "pinout"))
tmpl_sizer.Add(create_btn, 0, wx.RIGHT, 5)
# CLI Validation status icon (just after +)
dialog.pinout_validation_status = wx.StaticText(outer, label="")
dialog.pinout_validation_status.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
dialog.pinout_validation_status.SetToolTip("CLI parameter validation status")
tmpl_sizer.Add(dialog.pinout_validation_status, 0, wx.ALIGN_CENTER_VERTICAL)
outer_sizer.Add(tmpl_sizer, 0, wx.ALL, 10)
# Defer template refresh
wx.CallAfter(_refresh_template_dropdown, dialog, "pinout")
# Use SplitterWindow for resizable JSON preview
splitter = wx.SplitterWindow(outer, style=wx.SP_LIVE_UPDATE | wx.SP_3DSASH)
splitter.SetMinimumPaneSize(80) # Minimum height for either pane
# Top pane: JSON Preview (and PIL warning if needed)
json_panel = wx.Panel(splitter)
json_panel_sizer = wx.BoxSizer(wx.VERTICAL)
# PIL Status warning
if not check_pil():
warn = wx.StaticText(json_panel, label="⚠ PIL/Pillow not installed - labels will be disabled. Install with: pip install Pillow")
warn.SetForegroundColour(wx.Colour(200, 100, 0))
json_panel_sizer.Add(warn, 0, wx.EXPAND | wx.ALL, 5)
json_box = wx.StaticBox(json_panel, label="Template Parameters (edit via Manage → Edit)")
json_sizer = wx.StaticBoxSizer(json_box, wx.VERTICAL)
dialog.pinout_json_preview = wx.TextCtrl(json_panel, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
dialog.pinout_json_preview.SetFont(wx.Font(9, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
dialog.pinout_json_preview.SetBackgroundColour(wx.Colour(245, 245, 245))
json_sizer.Add(dialog.pinout_json_preview, 1, wx.EXPAND | wx.ALL, 5)
json_panel_sizer.Add(json_sizer, 1, wx.EXPAND | wx.ALL, 5)
json_panel.SetSizer(json_panel_sizer)
# Bottom pane: Scrolled settings panel
scroll = scrolled.ScrolledPanel(splitter, style=wx.VSCROLL)
sizer = wx.BoxSizer(wx.VERTICAL)
# Render-time settings (not in template)
render_box = wx.StaticBox(scroll, label="Render Options")
render_sizer = wx.StaticBoxSizer(render_box, wx.VERTICAL)
grid = wx.FlexGridSizer(2, 2, 5, 10)
grid.AddGrowableCol(1)
grid.Add(wx.StaticText(scroll, label="Side:"), 0, wx.ALIGN_CENTER_VERTICAL)
dialog.pinout_side = wx.Choice(scroll, choices=["top", "bottom"])
dialog.pinout_side.SetSelection(0)
grid.Add(dialog.pinout_side, 1, wx.EXPAND)
grid.Add(wx.StaticText(scroll, label="Output Format:"), 0, wx.ALIGN_CENTER_VERTICAL)
dialog.pinout_format = wx.Choice(scroll, choices=["PNG (3D Render)", "SVG (3D + Vector Labels)"])
dialog.pinout_format.SetSelection(0)
dialog.pinout_format.Bind(wx.EVT_CHOICE, lambda e: _on_pinout_format_change(dialog))
grid.Add(dialog.pinout_format, 1, wx.EXPAND)
render_sizer.Add(grid, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(render_sizer, 0, wx.EXPAND | wx.ALL, 5)
# SVG Layer Settings (hidden by default)
svg_layer_box = wx.StaticBox(scroll, label="SVG Layer Selection")
svg_layer_sizer = wx.StaticBoxSizer(svg_layer_box, wx.VERTICAL)
dialog.pinout_svg_panel = svg_layer_box.GetParent() # For show/hide
svg_help = wx.StaticText(scroll, label="Select which layers to include in the SVG export:")
svg_layer_sizer.Add(svg_help, 0, wx.ALL, 5)
svg_layer_grid = wx.FlexGridSizer(3, 3, 3, 10)
dialog.pinout_svg_layers = {}
svg_layers = [
("F.Cu", "Front Copper"),
("B.Cu", "Back Copper"),
("F.SilkS", "Front Silkscreen"),
("B.SilkS", "Back Silkscreen"),
("F.Mask", "Front Soldermask"),
("B.Mask", "Back Soldermask"),
("Edge.Cuts", "Board Outline"),
("F.Fab", "Front Fabrication"),
("B.Fab", "Back Fabrication"),
]
for layer_id, layer_name in svg_layers:
cb = wx.CheckBox(scroll, label=layer_name)
cb.SetValue(layer_id in ["F.Cu", "F.SilkS", "Edge.Cuts"]) # Default selection
dialog.pinout_svg_layers[layer_id] = cb
svg_layer_grid.Add(cb, 0)
svg_layer_sizer.Add(svg_layer_grid, 0, wx.ALL, 5)
dialog.pinout_svg_box = svg_layer_box
dialog.pinout_svg_sizer = svg_layer_sizer
sizer.Add(svg_layer_sizer, 0, wx.EXPAND | wx.ALL, 5)
svg_layer_box.Hide()
# Label Settings
label_box = wx.StaticBox(scroll, label="Component Labels")
label_sizer = wx.StaticBoxSizer(label_box, wx.VERTICAL)
dialog.pinout_show_labels = wx.CheckBox(scroll, label="Show component labels")
dialog.pinout_show_labels.SetValue(True)
label_sizer.Add(dialog.pinout_show_labels, 0, wx.ALL, 5)
label_grid = wx.FlexGridSizer(4, 2, 5, 10)
label_grid.AddGrowableCol(1)
label_grid.Add(wx.StaticText(scroll, label="Show:"), 0, wx.ALIGN_CENTER_VERTICAL)
show_sizer = wx.BoxSizer(wx.HORIZONTAL)
dialog.pinout_show_refs = wx.CheckBox(scroll, label="References")
dialog.pinout_show_refs.SetValue(True)
dialog.pinout_show_values = wx.CheckBox(scroll, label="Values")
show_sizer.Add(dialog.pinout_show_refs, 0, wx.RIGHT, 10)
show_sizer.Add(dialog.pinout_show_values, 0)
label_grid.Add(show_sizer, 1, wx.EXPAND)
label_grid.Add(wx.StaticText(scroll, label="Label Size:"), 0, wx.ALIGN_CENTER_VERTICAL)
dialog.pinout_label_size = wx.SpinCtrl(scroll, value="14", min=6, max=72)
label_grid.Add(dialog.pinout_label_size, 1, wx.EXPAND)
label_grid.Add(wx.StaticText(scroll, label="Filter Pattern:"), 0, wx.ALIGN_CENTER_VERTICAL)
dialog.pinout_filter = wx.TextCtrl(scroll, value="")
dialog.pinout_filter.SetToolTip("Only show matching components, e.g., 'U*' for ICs, 'J*' for connectors. Empty = all")
label_grid.Add(dialog.pinout_filter, 1, wx.EXPAND)
label_grid.Add(wx.StaticText(scroll, label="Highlight Pattern:"), 0, wx.ALIGN_CENTER_VERTICAL)
dialog.pinout_highlight = wx.TextCtrl(scroll, value="")
dialog.pinout_highlight.SetToolTip("Highlight matching components in yellow, e.g., 'LED*' or 'J1,J2'")
label_grid.Add(dialog.pinout_highlight, 1, wx.EXPAND)
label_sizer.Add(label_grid, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(label_sizer, 0, wx.EXPAND | wx.ALL, 5)
# Output
out_box = wx.StaticBox(scroll, label="Output")
out_sizer = wx.StaticBoxSizer(out_box, wx.VERTICAL)
dir_sizer = wx.BoxSizer(wx.HORIZONTAL)
dir_sizer.Add(wx.StaticText(scroll, label="Output File:"), 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)
default_output = os.path.join(dialog.output_dirs['Pinout'], f"{dialog.project_name}_pinout.png")
dialog.pinout_output = wx.TextCtrl(scroll, value=default_output)
dialog._pinout_project_dir = dialog.output_dirs['Pinout'] # Store for format change
dialog._pinout_project_name = dialog.project_name
browse_btn = wx.Button(scroll, label="Browse...")
browse_btn.Bind(wx.EVT_BUTTON, dialog.on_browse_pinout)
dir_sizer.Add(dialog.pinout_output, 1, wx.EXPAND | wx.RIGHT, 5)
dir_sizer.Add(browse_btn, 0)
out_sizer.Add(dir_sizer, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(out_sizer, 0, wx.EXPAND | wx.ALL, 5)
# Setup scrolling AFTER populating sizer
scroll.SetSizer(sizer)
scroll.SetupScrolling(scroll_x=False)
# Split horizontally: JSON preview on top, settings below
initial_pos = dialog.config.get('pinout_json_splitter_pos', 150)
splitter.SplitHorizontally(json_panel, scroll, initial_pos)
outer_sizer.Add(splitter, 1, wx.EXPAND)
# Store splitter reference for saving position later
dialog.pinout_json_splitter = splitter
# Button row
btn_row = wx.BoxSizer(wx.HORIZONTAL)
preview_btn = wx.Button(outer, label="Preview", size=(80, 40))
preview_btn.SetToolTip("Quick 320x240 preview render")
preview_btn.Bind(wx.EVT_BUTTON, lambda e: _show_preview(dialog, "pinout"))
render_btn = wx.Button(outer, label="Generate Pinout", size=(-1, 40))
render_btn.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
render_btn.Bind(wx.EVT_BUTTON, dialog.on_render_pinout)
dialog.pinout_cancel_btn = wx.Button(outer, label="Cancel", size=(80, 40))
dialog.pinout_cancel_btn.Enable(False)
dialog.pinout_cancel_btn.Bind(wx.EVT_BUTTON, dialog.on_cancel)
btn_row.Add(preview_btn, 0, wx.RIGHT, 5)
btn_row.Add(render_btn, 1, wx.EXPAND | wx.RIGHT, 5)
btn_row.Add(dialog.pinout_cancel_btn, 0)
outer_sizer.Add(btn_row, 0, wx.EXPAND | wx.ALL, 10)
outer.SetSizer(outer_sizer)
finally:
parent.Thaw()
dialog.log(f" [Pinout] UI created in {(time.time()-t0)*1000:.0f}ms")
return outer
def update_pinout_json_preview(dialog, template=None):
"""
Update the JSON preview for the selected pinout template.
Called when template selection changes. Shows raw template JSON.
"""
if not hasattr(dialog, 'pinout_json_preview'):
return
if template is None:
dialog.pinout_json_preview.SetValue("No template selected")
return
try:
# Show raw template JSON as-is
json_str = json.dumps(template, indent=2)
dialog.pinout_json_preview.SetValue(json_str)
except Exception as e:
dialog.pinout_json_preview.SetValue(f"Error: {e}")
def _on_pinout_width_change(dialog):
"""Handle width change - update height if aspect lock is enabled."""
if not hasattr(dialog, 'pinout_lock_aspect') or not dialog.pinout_lock_aspect.GetValue():
return
if not hasattr(dialog, '_pinout_updating') or not dialog._pinout_updating:
from .board_utils import calculate_dimensions_for_width
dialog._pinout_updating = True
new_width = dialog.pinout_width.GetValue()
new_height = calculate_dimensions_for_width(new_width, dialog._pinout_board_aspect)
dialog.pinout_height.SetValue(new_height)
dialog._pinout_updating = False
def _on_pinout_height_change(dialog):
"""Handle height change - update width if aspect lock is enabled."""
if not hasattr(dialog, 'pinout_lock_aspect') or not dialog.pinout_lock_aspect.GetValue():
return
if not hasattr(dialog, '_pinout_updating') or not dialog._pinout_updating:
from .board_utils import calculate_dimensions_for_height
dialog._pinout_updating = True
new_height = dialog.pinout_height.GetValue()
new_width = calculate_dimensions_for_height(new_height, dialog._pinout_board_aspect)
dialog.pinout_width.SetValue(new_width)
dialog._pinout_updating = False