Sat, 08 Sep 2018 12:00:12 +0200
bad idea :(
15 | 1 | # This file is part of the Printrun suite. |
2 | # | |
3 | # Printrun is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation, either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # Printrun is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with Printrun. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | import wx | |
17 | import re | |
18 | ||
19 | class MacroEditor(wx.Dialog): | |
20 | """Really simple editor to edit macro definitions""" | |
21 | ||
22 | def __init__(self, macro_name, definition, callback, gcode = False): | |
23 | self.indent_chars = " " | |
24 | title = " macro %s" | |
25 | if gcode: | |
26 | title = " %s" | |
27 | self.gcode = gcode | |
28 | wx.Dialog.__init__(self, None, title = title % macro_name, | |
29 | style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) | |
30 | self.callback = callback | |
31 | self.panel = wx.Panel(self, -1) | |
32 | titlesizer = wx.BoxSizer(wx.HORIZONTAL) | |
33 | self.titletext = wx.StaticText(self.panel, -1, " _") # title%macro_name) | |
34 | titlesizer.Add(self.titletext, 1) | |
35 | self.findb = wx.Button(self.panel, -1, _("Find"), style = wx.BU_EXACTFIT) # New button for "Find" (Jezmy) | |
36 | self.findb.Bind(wx.EVT_BUTTON, self.find) | |
37 | self.okb = wx.Button(self.panel, -1, _("Save"), style = wx.BU_EXACTFIT) | |
38 | self.okb.Bind(wx.EVT_BUTTON, self.save) | |
39 | self.Bind(wx.EVT_CLOSE, self.close) | |
40 | titlesizer.Add(self.findb) | |
41 | titlesizer.Add(self.okb) | |
42 | self.cancelb = wx.Button(self.panel, -1, _("Cancel"), style = wx.BU_EXACTFIT) | |
43 | self.cancelb.Bind(wx.EVT_BUTTON, self.close) | |
44 | titlesizer.Add(self.cancelb) | |
45 | topsizer = wx.BoxSizer(wx.VERTICAL) | |
46 | topsizer.Add(titlesizer, 0, wx.EXPAND) | |
47 | self.e = wx.TextCtrl(self.panel, style = wx.HSCROLL | wx.TE_MULTILINE | wx.TE_RICH2, size = (400, 400)) | |
48 | if not self.gcode: | |
49 | self.e.SetValue(self.unindent(definition)) | |
50 | else: | |
51 | self.e.SetValue("\n".join(definition)) | |
52 | topsizer.Add(self.e, 1, wx.ALL | wx.EXPAND) | |
53 | self.panel.SetSizer(topsizer) | |
54 | topsizer.Layout() | |
55 | topsizer.Fit(self) | |
56 | self.Show() | |
57 | self.e.SetFocus() | |
58 | ||
59 | def find(self, ev): | |
60 | # Ask user what to look for, find it and point at it ... (Jezmy) | |
61 | S = self.e.GetStringSelection() | |
62 | if not S: | |
63 | S = "Z" | |
64 | FindValue = wx.GetTextFromUser('Please enter a search string:', caption = "Search", default_value = S, parent = None) | |
65 | somecode = self.e.GetValue() | |
66 | position = somecode.find(FindValue, self.e.GetInsertionPoint()) | |
67 | if position == -1: | |
68 | self.titletext.SetLabel(_("Not Found!")) | |
69 | else: | |
70 | self.titletext.SetLabel(str(position)) | |
71 | ||
72 | # ananswer = wx.MessageBox(str(numLines)+" Lines detected in file\n"+str(position), "OK") | |
73 | self.e.SetFocus() | |
74 | self.e.SetInsertionPoint(position) | |
75 | self.e.SetSelection(position, position + len(FindValue)) | |
76 | self.e.ShowPosition(position) | |
77 | ||
78 | def ShowMessage(self, ev, message): | |
79 | dlg = wx.MessageDialog(self, message, | |
80 | "Info!", wx.OK | wx.ICON_INFORMATION) | |
81 | dlg.ShowModal() | |
82 | dlg.Destroy() | |
83 | ||
84 | def save(self, ev): | |
85 | self.Destroy() | |
86 | if not self.gcode: | |
87 | self.callback(self.reindent(self.e.GetValue())) | |
88 | else: | |
89 | self.callback(self.e.GetValue().split("\n")) | |
90 | ||
91 | def close(self, ev): | |
92 | self.Destroy() | |
93 | ||
94 | def unindent(self, text): | |
95 | self.indent_chars = text[:len(text) - len(text.lstrip())] | |
96 | if len(self.indent_chars) == 0: | |
97 | self.indent_chars = " " | |
98 | unindented = "" | |
99 | lines = re.split(r"(?:\r\n?|\n)", text) | |
100 | if len(lines) <= 1: | |
101 | return text | |
102 | for line in lines: | |
103 | if line.startswith(self.indent_chars): | |
104 | unindented += line[len(self.indent_chars):] + "\n" | |
105 | else: | |
106 | unindented += line + "\n" | |
107 | return unindented | |
108 | ||
109 | def reindent(self, text): | |
110 | lines = re.split(r"(?:\r\n?|\n)", text) | |
111 | if len(lines) <= 1: | |
112 | return text | |
113 | reindented = "" | |
114 | for line in lines: | |
115 | if line.strip() != "": | |
116 | reindented += self.indent_chars + line + "\n" | |
117 | return reindented | |
118 | ||
119 | SETTINGS_GROUPS = {"Printer": _("Printer settings"), | |
120 | "UI": _("User interface"), | |
121 | "Viewer": _("Viewer"), | |
122 | "Colors": _("Colors"), | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
15
diff
changeset
|
123 | "External": _("External commands"), |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
15
diff
changeset
|
124 | "Laser": "Lasercut options"} |
15 | 125 | |
126 | class PronterOptionsDialog(wx.Dialog): | |
127 | """Options editor""" | |
128 | def __init__(self, pronterface): | |
129 | wx.Dialog.__init__(self, parent = None, title = _("Edit settings"), | |
130 | size = (400, 500), style = wx.DEFAULT_DIALOG_STYLE) | |
131 | panel = wx.Panel(self) | |
132 | header = wx.StaticBox(panel, label = _("Settings")) | |
133 | sbox = wx.StaticBoxSizer(header, wx.VERTICAL) | |
134 | notebook = wx.Notebook(panel) | |
135 | all_settings = pronterface.settings._all_settings() | |
136 | group_list = [] | |
137 | groups = {} | |
26 | 138 | for group in ["Printer", "UI", "Viewer", "Colors", "External", "Laser"]: |
15 | 139 | group_list.append(group) |
140 | groups[group] = [] | |
141 | for setting in all_settings: | |
142 | if setting.group not in group_list: | |
143 | group_list.append(setting.group) | |
144 | groups[setting.group] = [] | |
145 | groups[setting.group].append(setting) | |
146 | for group in group_list: | |
147 | grouppanel = wx.Panel(notebook, -1) | |
148 | notebook.AddPage(grouppanel, SETTINGS_GROUPS[group]) | |
149 | settings = groups[group] | |
150 | grid = wx.GridBagSizer(hgap = 8, vgap = 2) | |
151 | current_row = 0 | |
152 | for setting in settings: | |
153 | if setting.name.startswith("separator_"): | |
154 | sep = wx.StaticLine(grouppanel, size = (-1, 5), style = wx.LI_HORIZONTAL) | |
155 | grid.Add(sep, pos = (current_row, 0), span = (1, 2), | |
156 | border = 3, flag = wx.ALIGN_CENTER | wx.ALL | wx.EXPAND) | |
157 | current_row += 1 | |
158 | label, widget = setting.get_label(grouppanel), setting.get_widget(grouppanel) | |
159 | if setting.name.startswith("separator_"): | |
160 | font = label.GetFont() | |
161 | font.SetWeight(wx.BOLD) | |
162 | label.SetFont(font) | |
163 | grid.Add(label, pos = (current_row, 0), | |
164 | flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT) | |
165 | grid.Add(widget, pos = (current_row, 1), | |
166 | flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND) | |
167 | if hasattr(label, "set_default"): | |
168 | label.Bind(wx.EVT_MOUSE_EVENTS, label.set_default) | |
169 | if hasattr(widget, "Bind"): | |
170 | widget.Bind(wx.EVT_MOUSE_EVENTS, label.set_default) | |
171 | current_row += 1 | |
172 | grid.AddGrowableCol(1) | |
173 | grouppanel.SetSizer(grid) | |
174 | sbox.Add(notebook, 1, wx.EXPAND) | |
175 | panel.SetSizer(sbox) | |
176 | topsizer = wx.BoxSizer(wx.VERTICAL) | |
177 | topsizer.Add(panel, 1, wx.ALL | wx.EXPAND) | |
178 | topsizer.Add(self.CreateButtonSizer(wx.OK | wx.CANCEL), 0, wx.ALIGN_RIGHT) | |
179 | self.SetSizerAndFit(topsizer) | |
180 | self.SetMinSize(self.GetSize()) | |
181 | ||
26 | 182 | self.notebook = notebook |
183 | self.group_list = group_list | |
184 | ||
185 | def setPage(self, name): | |
186 | self.notebook.ChangeSelection(self.group_list.index(name)) | |
187 | ||
188 | def PronterOptions(pronterface, defaulttab = None): | |
15 | 189 | dialog = PronterOptionsDialog(pronterface) |
26 | 190 | if defaulttab: |
191 | # set the active tab before open dialog | |
192 | dialog.setPage(defaulttab) | |
193 | ||
15 | 194 | if dialog.ShowModal() == wx.ID_OK: |
195 | for setting in pronterface.settings._all_settings(): | |
196 | old_value = setting.value | |
197 | setting.update() | |
198 | if setting.value != old_value: | |
199 | pronterface.set(setting.name, setting.value) | |
200 | dialog.Destroy() | |
201 | ||
202 | class ButtonEdit(wx.Dialog): | |
203 | """Custom button edit dialog""" | |
204 | def __init__(self, pronterface): | |
205 | wx.Dialog.__init__(self, None, title = _("Custom button"), | |
206 | style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) | |
207 | self.pronterface = pronterface | |
208 | topsizer = wx.BoxSizer(wx.VERTICAL) | |
209 | grid = wx.FlexGridSizer(rows = 0, cols = 2, hgap = 4, vgap = 2) | |
210 | grid.AddGrowableCol(1, 1) | |
211 | grid.Add(wx.StaticText(self, -1, _("Button title")), 0, wx.BOTTOM | wx.RIGHT) | |
212 | self.name = wx.TextCtrl(self, -1, "") | |
213 | grid.Add(self.name, 1, wx.EXPAND) | |
214 | grid.Add(wx.StaticText(self, -1, _("Command")), 0, wx.BOTTOM | wx.RIGHT) | |
215 | self.command = wx.TextCtrl(self, -1, "") | |
216 | xbox = wx.BoxSizer(wx.HORIZONTAL) | |
217 | xbox.Add(self.command, 1, wx.EXPAND) | |
218 | self.command.Bind(wx.EVT_TEXT, self.macrob_enabler) | |
219 | self.macrob = wx.Button(self, -1, "..", style = wx.BU_EXACTFIT) | |
220 | self.macrob.Bind(wx.EVT_BUTTON, self.macrob_handler) | |
221 | xbox.Add(self.macrob, 0) | |
222 | grid.Add(xbox, 1, wx.EXPAND) | |
223 | grid.Add(wx.StaticText(self, -1, _("Color")), 0, wx.BOTTOM | wx.RIGHT) | |
224 | self.color = wx.TextCtrl(self, -1, "") | |
225 | grid.Add(self.color, 1, wx.EXPAND) | |
226 | topsizer.Add(grid, 0, wx.EXPAND) | |
227 | topsizer.Add((0, 0), 1) | |
228 | topsizer.Add(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL), 0, wx.ALIGN_CENTER) | |
229 | self.SetSizer(topsizer) | |
230 | ||
231 | def macrob_enabler(self, e): | |
232 | macro = self.command.GetValue() | |
233 | valid = False | |
234 | try: | |
235 | if macro == "": | |
236 | valid = True | |
237 | elif macro in self.pronterface.macros: | |
238 | valid = True | |
239 | elif hasattr(self.pronterface.__class__, u"do_" + macro): | |
240 | valid = False | |
241 | elif len([c for c in macro if not c.isalnum() and c != "_"]): | |
242 | valid = False | |
243 | else: | |
244 | valid = True | |
245 | except: | |
246 | if macro == "": | |
247 | valid = True | |
248 | elif macro in self.pronterface.macros: | |
249 | valid = True | |
250 | elif len([c for c in macro if not c.isalnum() and c != "_"]): | |
251 | valid = False | |
252 | else: | |
253 | valid = True | |
254 | self.macrob.Enable(valid) | |
255 | ||
256 | def macrob_handler(self, e): | |
257 | macro = self.command.GetValue() | |
258 | macro = self.pronterface.edit_macro(macro) | |
259 | self.command.SetValue(macro) | |
260 | if self.name.GetValue() == "": | |
261 | self.name.SetValue(macro) | |
262 | ||
263 | class TempGauge(wx.Panel): | |
264 | ||
265 | def __init__(self, parent, size = (200, 22), title = "", | |
266 | maxval = 240, gaugeColour = None, bgcolor = "#FFFFFF"): | |
267 | wx.Panel.__init__(self, parent, -1, size = size) | |
268 | self.Bind(wx.EVT_PAINT, self.paint) | |
269 | self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) | |
270 | self.bgcolor = wx.Colour() | |
271 | self.bgcolor.SetFromName(bgcolor) | |
272 | self.width, self.height = size | |
273 | self.title = title | |
274 | self.max = maxval | |
275 | self.gaugeColour = gaugeColour | |
276 | self.value = 0 | |
277 | self.setpoint = 0 | |
278 | self.recalc() | |
279 | ||
280 | def recalc(self): | |
281 | mmax = max(int(self.setpoint * 1.05), self.max) | |
282 | self.scale = float(self.width - 2) / float(mmax) | |
283 | self.ypt = max(16, int(self.scale * max(self.setpoint, self.max / 6))) | |
284 | ||
285 | def SetValue(self, value): | |
286 | self.value = value | |
287 | wx.CallAfter(self.Refresh) | |
288 | ||
289 | def SetTarget(self, value): | |
290 | self.setpoint = value | |
291 | wx.CallAfter(self.Refresh) | |
292 | ||
293 | def interpolatedColour(self, val, vmin, vmid, vmax, cmin, cmid, cmax): | |
294 | if val < vmin: return cmin | |
295 | if val > vmax: return cmax | |
296 | if val <= vmid: | |
297 | lo, hi, val, valhi = cmin, cmid, val - vmin, vmid - vmin | |
298 | else: | |
299 | lo, hi, val, valhi = cmid, cmax, val - vmid, vmax - vmid | |
300 | vv = float(val) / valhi | |
301 | rgb = lo.Red() + (hi.Red() - lo.Red()) * vv, lo.Green() + (hi.Green() - lo.Green()) * vv, lo.Blue() + (hi.Blue() - lo.Blue()) * vv | |
302 | rgb = map(lambda x: x * 0.8, rgb) | |
303 | return wx.Colour(*map(int, rgb)) | |
304 | ||
305 | def paint(self, ev): | |
306 | self.width, self.height = self.GetClientSizeTuple() | |
307 | self.recalc() | |
308 | x0, y0, x1, y1, xE, yE = 1, 1, self.ypt + 1, 1, self.width + 1 - 2, 20 | |
309 | dc = wx.PaintDC(self) | |
310 | dc.SetBackground(wx.Brush(self.bgcolor)) | |
311 | dc.Clear() | |
312 | cold, medium, hot = wx.Colour(0, 167, 223), wx.Colour(239, 233, 119), wx.Colour(210, 50.100) | |
313 | # gauge1, gauge2 = wx.Colour(255, 255, 210), (self.gaugeColour or wx.Colour(234, 82, 0)) | |
314 | gauge1 = wx.Colour(255, 255, 210) | |
315 | shadow1, shadow2 = wx.Colour(110, 110, 110), self.bgcolor | |
316 | gc = wx.GraphicsContext.Create(dc) | |
317 | # draw shadow first | |
318 | # corners | |
319 | gc.SetBrush(gc.CreateRadialGradientBrush(xE - 7, 9, xE - 7, 9, 8, shadow1, shadow2)) | |
320 | gc.DrawRectangle(xE - 7, 1, 8, 8) | |
321 | gc.SetBrush(gc.CreateRadialGradientBrush(xE - 7, 17, xE - 7, 17, 8, shadow1, shadow2)) | |
322 | gc.DrawRectangle(xE - 7, 17, 8, 8) | |
323 | gc.SetBrush(gc.CreateRadialGradientBrush(x0 + 6, 17, x0 + 6, 17, 8, shadow1, shadow2)) | |
324 | gc.DrawRectangle(0, 17, x0 + 6, 8) | |
325 | # edges | |
326 | gc.SetBrush(gc.CreateLinearGradientBrush(xE - 6, 0, xE + 1, 0, shadow1, shadow2)) | |
327 | gc.DrawRectangle(xE - 7, 9, 8, 8) | |
328 | gc.SetBrush(gc.CreateLinearGradientBrush(x0, yE - 2, x0, yE + 5, shadow1, shadow2)) | |
329 | gc.DrawRectangle(x0 + 6, yE - 2, xE - 12, 7) | |
330 | # draw gauge background | |
331 | gc.SetBrush(gc.CreateLinearGradientBrush(x0, y0, x1 + 1, y1, cold, medium)) | |
332 | gc.DrawRoundedRectangle(x0, y0, x1 + 4, yE, 6) | |
333 | gc.SetBrush(gc.CreateLinearGradientBrush(x1 - 2, y1, xE, y1, medium, hot)) | |
334 | gc.DrawRoundedRectangle(x1 - 2, y1, xE - x1, yE, 6) | |
335 | # draw gauge | |
336 | width = 12 | |
337 | w1 = y0 + 9 - width / 2 | |
338 | w2 = w1 + width | |
339 | value = x0 + max(10, min(self.width + 1 - 2, int(self.value * self.scale))) | |
340 | # gc.SetBrush(gc.CreateLinearGradientBrush(x0, y0 + 3, x0, y0 + 15, gauge1, gauge2)) | |
341 | # gc.SetBrush(gc.CreateLinearGradientBrush(0, 3, 0, 15, wx.Colour(255, 255, 255), wx.Colour(255, 90, 32))) | |
342 | gc.SetBrush(gc.CreateLinearGradientBrush(x0, y0 + 3, x0, y0 + 15, gauge1, self.interpolatedColour(value, x0, x1, xE, cold, medium, hot))) | |
343 | val_path = gc.CreatePath() | |
344 | val_path.MoveToPoint(x0, w1) | |
345 | val_path.AddLineToPoint(value, w1) | |
346 | val_path.AddLineToPoint(value + 2, w1 + width / 4) | |
347 | val_path.AddLineToPoint(value + 2, w2 - width / 4) | |
348 | val_path.AddLineToPoint(value, w2) | |
349 | # val_path.AddLineToPoint(value-4, 10) | |
350 | val_path.AddLineToPoint(x0, w2) | |
351 | gc.DrawPath(val_path) | |
352 | # draw setpoint markers | |
353 | setpoint = x0 + max(10, int(self.setpoint * self.scale)) | |
354 | gc.SetBrush(gc.CreateBrush(wx.Brush(wx.Colour(0, 0, 0)))) | |
355 | setp_path = gc.CreatePath() | |
356 | setp_path.MoveToPoint(setpoint - 4, y0) | |
357 | setp_path.AddLineToPoint(setpoint + 4, y0) | |
358 | setp_path.AddLineToPoint(setpoint, y0 + 5) | |
359 | setp_path.MoveToPoint(setpoint - 4, yE) | |
360 | setp_path.AddLineToPoint(setpoint + 4, yE) | |
361 | setp_path.AddLineToPoint(setpoint, yE - 5) | |
362 | gc.DrawPath(setp_path) | |
363 | # draw readout | |
364 | text = u"T\u00B0 %u/%u" % (self.value, self.setpoint) | |
365 | # gc.SetFont(gc.CreateFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD), wx.WHITE)) | |
366 | # gc.DrawText(text, 29,-2) | |
367 | gc.SetFont(gc.CreateFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD), wx.WHITE)) | |
368 | gc.DrawText(self.title, x0 + 19, y0 + 4) | |
369 | gc.DrawText(text, x0 + 119, y0 + 4) | |
370 | gc.SetFont(gc.CreateFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))) | |
371 | gc.DrawText(self.title, x0 + 18, y0 + 3) | |
372 | gc.DrawText(text, x0 + 118, y0 + 3) | |
373 | ||
374 | class SpecialButton(object): | |
375 | ||
376 | label = None | |
377 | command = None | |
378 | background = None | |
379 | tooltip = None | |
380 | custom = None | |
381 | ||
382 | def __init__(self, label, command, background = None, | |
383 | tooltip = None, custom = False): | |
384 | self.label = label | |
385 | self.command = command | |
386 | self.background = background | |
387 | self.tooltip = tooltip | |
388 | self.custom = custom |