printrun-src/printrun/settings.py

Wed, 20 Jan 2021 11:37:03 +0100

author
mdd
date
Wed, 20 Jan 2021 11:37:03 +0100
changeset 48
3c27b4ee6fec
parent 46
cce0af6351f0
permissions
-rw-r--r--

reimplemented lasercutter changes

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 import traceback
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
18 import os
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
19 import sys
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 from functools import wraps
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 from .utils import parse_build_dimensions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 def setting_add_tooltip(func):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 @wraps(func)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 def decorator(self, *args, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 widget = func(self, *args, **kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 helptxt = self.help or ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 sep, deftxt = "", ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 if len(helptxt):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 sep = "\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 if helptxt.find("\n") >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 sep = "\n\n"
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
35 if self.default != "":
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 deftxt = _("Default: ")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 resethelp = _("(Control-doubleclick to reset to default value)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 if len(repr(self.default)) > 10:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 deftxt += "\n " + repr(self.default).strip("'") + "\n" + resethelp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 deftxt += repr(self.default) + " " + resethelp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 helptxt += sep + deftxt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 if len(helptxt):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
44 widget.SetToolTip(helptxt)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 return widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 return decorator
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
48 class Setting:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 DEFAULT_GROUP = "Printer"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 hidden = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 def __init__(self, name, default, label = None, help = None, group = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self.name = name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 self.default = default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 self._value = default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 self.label = label
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 self.help = help
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 self.group = group if group else Setting.DEFAULT_GROUP
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 def _get_value(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 return self._value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 def _set_value(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 raise NotImplementedError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 value = property(_get_value, _set_value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 @setting_add_tooltip
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 def get_label(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 widget = wx.StaticText(parent, -1, self.label or self.name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 widget.set_default = self.set_default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 return widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 @setting_add_tooltip
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 def get_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 return self.get_specific_widget(parent)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def get_specific_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 raise NotImplementedError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 def update(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 raise NotImplementedError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
86 def validate(self, value): pass
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
87
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 def __str__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 return self.name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 def __repr__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 return self.name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 class HiddenSetting(Setting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 hidden = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 def _set_value(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 self._value = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 value = property(Setting._get_value, _set_value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 class wxSetting(Setting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 widget = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 def _set_value(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 self._value = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 if self.widget:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.widget.SetValue(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 value = property(Setting._get_value, _set_value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 def update(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.value = self.widget.GetValue()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
115 def set_default(self, e):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
116 if e.CmdDown() and e.ButtonDClick() and self.default != "":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
117 self.widget.SetValue(self.default)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
118 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
119 e.Skip()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
120
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 class StringSetting(wxSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 def get_specific_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 self.widget = wx.TextCtrl(parent, -1, str(self.value))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
128 def wxColorToStr(color, withAlpha = True):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
129 # including Alpha seems to be non standard in CSS
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
130 format = '#{0.red:02X}{0.green:02X}{0.blue:02X}' \
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
131 + ('{0.alpha:02X}' if withAlpha else '')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
132 return format.format(color)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
133
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
134 class ColorSetting(wxSetting):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
135 def __init__(self, name, default, label = None, help = None, group = None, isRGBA=True):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
136 super().__init__(name, default, label, help, group)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
137 self.isRGBA = isRGBA
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
138
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
139 def validate(self, value):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
140 from .utils import check_rgb_color, check_rgba_color
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
141 validate = check_rgba_color if self.isRGBA else check_rgb_color
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
142 validate(value)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
143
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
144 def get_specific_widget(self, parent):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
145 import wx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
146 self.widget = wx.ColourPickerCtrl(parent, colour=wx.Colour(self.value), style=wx.CLRP_USE_TEXTCTRL)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
147 self.widget.SetValue = self.widget.SetColour
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
148 self.widget.LayoutDirection = wx.Layout_RightToLeft
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
149 return self.widget
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
150 def update(self):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
151 self._value = wxColorToStr(self.widget.Colour, self.isRGBA)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
152
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 class ComboSetting(wxSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 def __init__(self, name, default, choices, label = None, help = None, group = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 super(ComboSetting, self).__init__(name, default, label, help, group)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 self.choices = choices
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 def get_specific_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 import wx
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
161 readonly = isinstance(self.choices, tuple)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
162 if readonly:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
163 # wx.Choice drops its list on click, no need to click down arrow
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
164 # which is far to the right because of wx.EXPAND
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
165 self.widget = wx.Choice(parent, -1, choices = self.choices)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
166 self.widget.GetValue = lambda: self.choices[self.widget.Selection]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
167 self.widget.SetValue = lambda v: self.widget.SetSelection(self.choices.index(v))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
168 self.widget.SetValue(self.value)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
169 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
170 self.widget = wx.ComboBox(parent, -1, str(self.value), choices = self.choices, style = wx.CB_DROPDOWN)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 class SpinSetting(wxSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 def __init__(self, name, default, min, max, label = None, help = None, group = None, increment = 0.1):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
176 super().__init__(name, default, label, help, group)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 self.min = min
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 self.max = max
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 self.increment = increment
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 def get_specific_widget(self, parent):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
182 import wx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
183 self.widget = wx.SpinCtrlDouble(parent, -1, min = self.min, max = self.max)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
184 self.widget.SetDigits(0)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 self.widget.SetValue(self.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 orig = self.widget.GetValue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 self.widget.GetValue = lambda: int(orig())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
190 def MySpin(parent, digits, *args, **kw):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
191 # in GTK 3.[01], spinner is not large enough to fit text
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
192 # Could be a class, but use function to avoid load errors if wx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
193 # not installed
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
194 # If native wx.SpinCtrlDouble has problems in different platforms
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
195 # try agw
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
196 # from wx.lib.agw.floatspin import FloatSpin
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
197 import wx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
198 sp = wx.SpinCtrlDouble(parent, *args, **kw)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
199 # sp = FloatSpin(parent)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
200 sp.SetDigits(digits)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
201 # sp.SetValue(kw['initial'])
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
202 def fitValue(ev):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
203 text = '%%.%df'% digits % sp.Max
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
204 # native wx.SpinCtrlDouble does not return good size
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
205 # in GTK 3.0
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
206 tex = sp.GetTextExtent(text)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
207 tsz = sp.GetSizeFromTextSize(tex.x)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
208
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
209 if sp.MinSize.x < tsz.x:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
210 # print('fitValue', getattr(sp, 'setting', None), sp.Value, sp.Digits, tsz.x)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
211 sp.MinSize = tsz
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
212 # sp.Size = tsz
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
213 # sp.Bind(wx.EVT_TEXT, fitValue)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
214 fitValue(None)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
215 return sp
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
216
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 class FloatSpinSetting(SpinSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 def get_specific_widget(self, parent):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
220 self.widget = MySpin(parent, 2, initial = self.value, min = self.min, max = self.max, inc = self.increment)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 class BooleanSetting(wxSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 def _get_value(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 return bool(self._value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 def _set_value(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 self._value = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 if self.widget:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 self.widget.SetValue(bool(value))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 value = property(_get_value, _set_value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 def get_specific_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 self.widget = wx.CheckBox(parent, -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 self.widget.SetValue(bool(self.value))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 class StaticTextSetting(wxSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 def __init__(self, name, label = " ", text = "", help = None, group = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 super(StaticTextSetting, self).__init__(name, "", label, help, group)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 self.text = text
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 def update(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 def _get_value(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 return ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 def _set_value(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 def get_specific_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.widget = wx.StaticText(parent, -1, self.text)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 class BuildDimensionsSetting(wxSetting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 widgets = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 def _set_value(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 self._value = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 if self.widgets:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 self._set_widgets_values(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 value = property(wxSetting._get_value, _set_value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 def _set_widgets_values(self, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 build_dimensions_list = parse_build_dimensions(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 for i in range(len(self.widgets)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 self.widgets[i].SetValue(build_dimensions_list[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 def get_widget(self, parent):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 from wx.lib.agw.floatspin import FloatSpin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 build_dimensions = parse_build_dimensions(self.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 self.widgets = []
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
281 def w(val, m, M):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
282 self.widgets.append(MySpin(parent, 2, initial = val, min = m, max = M))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
283 def addlabel(name, pos):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
284 self.widget.Add(wx.StaticText(parent, -1, name), pos = pos, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
285 def addwidget(*pos):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
286 self.widget.Add(self.widgets[-1], pos = pos, flag = wx.RIGHT | wx.EXPAND, border = 5)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 self.widget = wx.GridBagSizer()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 addlabel(_("Width"), (0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 w(build_dimensions[0], 0, 2000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 addwidget(0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 addlabel(_("Depth"), (0, 2))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 w(build_dimensions[1], 0, 2000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 addwidget(0, 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 addlabel(_("Height"), (0, 4))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 w(build_dimensions[2], 0, 2000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 addwidget(0, 5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 addlabel(_("X offset"), (1, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 w(build_dimensions[3], -2000, 2000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 addwidget(1, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 addlabel(_("Y offset"), (1, 2))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 w(build_dimensions[4], -2000, 2000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 addwidget(1, 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 addlabel(_("Z offset"), (1, 4))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 w(build_dimensions[5], -2000, 2000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 addwidget(1, 5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 addlabel(_("X home pos."), (2, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 w(build_dimensions[6], -2000, 2000)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
308 addwidget(2, 1)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 addlabel(_("Y home pos."), (2, 2))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 w(build_dimensions[7], -2000, 2000)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
311 addwidget(2, 3)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 addlabel(_("Z home pos."), (2, 4))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 w(build_dimensions[8], -2000, 2000)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
314 addwidget(2, 5)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 return self.widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 def update(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 values = [float(w.GetValue()) for w in self.widgets]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 self.value = "%.02fx%.02fx%.02f%+.02f%+.02f%+.02f%+.02f%+.02f%+.02f" % tuple(values)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
321 class Settings:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 def __baudrate_list(self): return ["2400", "9600", "19200", "38400", "57600", "115200", "250000"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 def __init__(self, root):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 # defaults here.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 # the initial value determines the type
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 self._add(StringSetting("port", "", _("Serial port"), _("Port used to communicate with printer")))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 self._add(ComboSetting("baudrate", 115200, self.__baudrate_list(), _("Baud rate"), _("Communications Speed")))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 self._add(BooleanSetting("tcp_streaming_mode", False, _("TCP streaming mode"), _("When using a TCP connection to the printer, the streaming mode will not wait for acks from the printer to send new commands. This will break things such as ETA prediction, but can result in smoother prints.")), root.update_tcp_streaming_mode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 self._add(BooleanSetting("rpc_server", True, _("RPC server"), _("Enable RPC server to allow remotely querying print status")), root.update_rpc_server)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 self._add(BooleanSetting("dtr", True, _("DTR"), _("Disabling DTR would prevent Arduino (RAMPS) from resetting upon connection"), "Printer"))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
332 if sys.platform != "win32":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
333 self._add(StringSetting("devicepath", "", _("Device name pattern"), _("Custom device pattern: for example /dev/3DP_* "), "Printer"))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
334 self._add(SpinSetting("bedtemp_abs", 110, 0, 400, _("Bed temperature for ABS"), _("Heated Build Platform temp for ABS (deg C)"), "Printer"), root.set_temp_preset)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
335 self._add(SpinSetting("bedtemp_pla", 60, 0, 400, _("Bed temperature for PLA"), _("Heated Build Platform temp for PLA (deg C)"), "Printer"), root.set_temp_preset)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
336 self._add(SpinSetting("temperature_abs", 230, 0, 400, _("Extruder temperature for ABS"), _("Extruder temp for ABS (deg C)"), "Printer"), root.set_temp_preset)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
337 self._add(SpinSetting("temperature_pla", 185, 0, 400, _("Extruder temperature for PLA"), _("Extruder temp for PLA (deg C)"), "Printer"), root.set_temp_preset)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 self._add(SpinSetting("xy_feedrate", 3000, 0, 50000, _("X && Y manual feedrate"), _("Feedrate for Control Panel Moves in X and Y (mm/min)"), "Printer"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 self._add(SpinSetting("z_feedrate", 100, 0, 50000, _("Z manual feedrate"), _("Feedrate for Control Panel Moves in Z (mm/min)"), "Printer"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 self._add(SpinSetting("e_feedrate", 100, 0, 1000, _("E manual feedrate"), _("Feedrate for Control Panel Moves in Extrusions (mm/min)"), "Printer"))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
341 defaultslicerpath = ""
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
342 if getattr(sys, 'frozen', False):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
343 if sys.platform == "darwin":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
344 defaultslicerpath = "/Applications/Slic3r.app/Contents/MacOS/"
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
345 elif sys.platform == "win32":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
346 defaultslicerpath = ".\\slic3r\\"
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
347 self._add(StringSetting("slicecommandpath", defaultslicerpath, _("Path to slicer"), _("Path to slicer"), "External"))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
348 slicer = 'slic3r-console' if sys.platform == 'win32' else 'slic3r'
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
349 self._add(StringSetting("slicecommand", slicer + ' $s --output $o', _("Slice command"), _("Slice command"), "External"))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
350 self._add(StringSetting("sliceoptscommand", "slic3r", _("Slicer options command"), _("Slice settings command"), "External"))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 self._add(StringSetting("start_command", "", _("Start command"), _("Executable to run when the print is started"), "External"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 self._add(StringSetting("final_command", "", _("Final command"), _("Executable to run when the print is finished"), "External"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 self._add(StringSetting("error_command", "", _("Error command"), _("Executable to run when an error occurs"), "External"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 self._add(StringSetting("log_path", "", _("Log path"), _("Path to the log file. An empty path will log to the console."), "UI"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 self._add(HiddenSetting("project_offset_x", 0.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 self._add(HiddenSetting("project_offset_y", 0.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 self._add(HiddenSetting("project_interval", 2.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 self._add(HiddenSetting("project_pause", 2.5))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 self._add(HiddenSetting("project_scale", 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 self._add(HiddenSetting("project_x", 1024))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 self._add(HiddenSetting("project_y", 768))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 self._add(HiddenSetting("project_projected_x", 150.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 self._add(HiddenSetting("project_direction", "Top Down"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 self._add(HiddenSetting("project_overshoot", 3.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 self._add(HiddenSetting("project_z_axis_rate", 200))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 self._add(HiddenSetting("project_layer", 0.1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 self._add(HiddenSetting("project_prelift_gcode", ""))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 self._add(HiddenSetting("project_postlift_gcode", ""))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 self._add(HiddenSetting("pause_between_prints", True))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 self._add(HiddenSetting("default_extrusion", 5.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 self._add(HiddenSetting("last_extrusion", 5.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 self._add(HiddenSetting("total_filament_used", 0.0))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
374 self._add(HiddenSetting("spool_list", ""))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 _settings = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 def __setattr__(self, name, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 if name.startswith("_"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 return object.__setattr__(self, name, value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 if isinstance(value, Setting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 if not value.hidden:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 self._settings.append(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 object.__setattr__(self, "_" + name, value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 elif hasattr(self, "_" + name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 getattr(self, "_" + name).value = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 setattr(self, name, StringSetting(name = name, default = value))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 def __getattr__(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 if name.startswith("_"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 return object.__getattribute__(self, name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 return getattr(self, "_" + name).value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
395 def _add(self, setting, callback = None,
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 alias = None, autocomplete_list = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 setattr(self, setting.name, setting)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 if callback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 setattr(self, "__" + setting.name + "_cb", callback)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 if alias:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 setattr(self, "__" + setting.name + "_alias", alias)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 if autocomplete_list:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 setattr(self, "__" + setting.name + "_list", autocomplete_list)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 def _set(self, key, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 value = getattr(self, "__%s_alias" % key)()[value]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 except KeyError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 except AttributeError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 pass
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
412 setting = getattr(self, '_'+key)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
413 setting.validate(value)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 t = type(getattr(self, key))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
415 if t == bool and value == "False":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
416 value = False
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
417 setattr(self, key, t(value))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 try:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
419 cb = getattr(self, "__%s_cb" % key, None)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
420 if cb is not None:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
421 cb(key, value)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 logging.warning((_("Failed to run callback after setting \"%s\":") % key) +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 return value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 def _tabcomplete(self, key):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 return getattr(self, "__%s_list" % key)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 except AttributeError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 try:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
433 return list(getattr(self, "__%s_alias" % key)().keys())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 except AttributeError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 def _all_settings(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 return self._settings

mercurial