1 from __future__ import division
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
4 import wx, wx.lib.stattext, types
5 from wx.lib.agw import floatspin
7 from Cura.util import validators
8 from Cura.util import profile
10 class configPanelBase(wx.Panel):
11 "A base class for configuration dialogs. Handles creation of settings, and popups"
12 def __init__(self, parent, changeCallback = None):
13 super(configPanelBase, self).__init__(parent)
15 self.settingControlList = []
17 #Create the popup window
18 self.popup = wx.PopupWindow(self, flags=wx.BORDER_SIMPLE)
19 self.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
20 self.popup.setting = None
21 self.popup.text = wx.StaticText(self.popup, -1, '')
22 self.popup.text.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOTEXT))
23 self.popup.sizer = wx.BoxSizer()
24 self.popup.sizer.Add(self.popup.text, flag=wx.EXPAND|wx.ALL, border=1)
25 self.popup.SetSizer(self.popup.sizer)
27 self._callback = changeCallback
29 def CreateConfigTab(self, nb, name):
30 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
31 nb.AddPage(configPanel, name)
32 return leftConfigPanel, rightConfigPanel
34 def CreateConfigPanel(self, parent):
35 configPanel = wx.Panel(parent);
36 leftConfigPanel = wx.Panel(configPanel)
37 rightConfigPanel = wx.Panel(configPanel)
39 sizer = wx.GridBagSizer(2, 2)
40 leftConfigPanel.SetSizer(sizer)
41 sizer = wx.GridBagSizer(2, 2)
42 rightConfigPanel.SetSizer(sizer)
44 sizer = wx.BoxSizer(wx.HORIZONTAL)
45 configPanel.SetSizer(sizer)
46 sizer.Add(leftConfigPanel, border=35, flag=wx.RIGHT)
47 sizer.Add(rightConfigPanel)
48 leftConfigPanel.main = self
49 rightConfigPanel.main = self
50 return leftConfigPanel, rightConfigPanel, configPanel
52 def CreateDynamicConfigTab(self, nb, name):
53 configPanel = wx.lib.scrolledpanel.ScrolledPanel(nb)
54 #configPanel = wx.Panel(nb);
55 leftConfigPanel = wx.Panel(configPanel)
56 rightConfigPanel = wx.Panel(configPanel)
58 sizer = wx.GridBagSizer(2, 2)
59 leftConfigPanel.SetSizer(sizer)
60 #sizer.AddGrowableCol(1)
62 sizer = wx.GridBagSizer(2, 2)
63 rightConfigPanel.SetSizer(sizer)
64 #sizer.AddGrowableCol(1)
66 sizer = wx.BoxSizer(wx.HORIZONTAL)
67 sizer.Add(leftConfigPanel, proportion=1, border=35, flag=wx.EXPAND)
68 sizer.Add(rightConfigPanel, proportion=1, flag=wx.EXPAND)
69 configPanel.SetSizer(sizer)
71 configPanel.SetAutoLayout(1)
72 configPanel.SetupScrolling(scroll_x=False, scroll_y=True)
74 leftConfigPanel.main = self
75 rightConfigPanel.main = self
77 configPanel.leftPanel = leftConfigPanel
78 configPanel.rightPanel = rightConfigPanel
80 nb.AddPage(configPanel, name)
82 return leftConfigPanel, rightConfigPanel, configPanel
84 def OnPopupDisplay(self, setting):
85 self.popup.setting = setting
86 self.UpdatePopup(setting)
89 def OnPopupHide(self, e):
90 self.popup.Show(False)
92 def UpdatePopup(self, setting):
93 if self.popup.setting == setting:
94 if setting.validationMsg != '':
95 self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.setting.getTooltip())
97 self.popup.text.SetLabel(setting.setting.getTooltip())
98 self.popup.text.Wrap(350)
100 x, y = setting.ctrl.ClientToScreenXY(0, 0)
101 sx, sy = setting.ctrl.GetSizeTuple()
102 #if platform.system() == "Windows":
103 # for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
104 # wx, wy = self.ClientToScreenXY(0, 0)
107 self.popup.SetPosition((x, y+sy))
109 def updateProfileToControls(self):
110 "Update the configuration wx controls to show the new configuration settings"
111 for setting in self.settingControlList:
112 setting.SetValue(setting.setting.getValue())
116 for setting in self.settingControlList:
118 if self._callback is not None:
121 def getLabelColumnWidth(self, panel):
123 for child in panel.GetChildren():
124 if isinstance(child, wx.lib.stattext.GenStaticText):
125 maxWidth = max(maxWidth, child.GetSize()[0])
128 def setLabelColumnWidth(self, panel, width):
129 for child in panel.GetChildren():
130 if isinstance(child, wx.lib.stattext.GenStaticText):
131 size = child.GetSize()
133 child.SetBestSize(size)
135 class TitleRow(object):
136 def __init__(self, panel, name):
137 "Add a title row to the configuration panel"
138 sizer = panel.GetSizer()
140 self.title = wx.StaticText(panel, -1, name.replace('&', '&&'))
141 self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
142 sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
143 sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
146 class SettingRow(object):
147 def __init__(self, panel, configName, valueOverride = None, index = None):
148 "Add a setting to the configuration panel"
149 sizer = panel.GetSizer()
154 self.setting = profile.settingsDictionary[configName]
155 self.settingIndex = index
156 self.validationMsg = ''
159 self.label = wx.lib.stattext.GenStaticText(panel, -1, self.setting.getLabel())
160 self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
161 self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
163 #if self.setting.getType() is types.FloatType and False:
165 # while 1 / pow(10, digits) > defaultValue:
167 # self.ctrl = floatspin.FloatSpin(panel, -1, value=float(getSettingFunc(configName)), increment=defaultValue, digits=digits, min_val=0.0)
168 # self.ctrl.Bind(floatspin.EVT_FLOATSPIN, self.OnSettingChange)
170 if self.setting.getType() is types.BooleanType:
171 self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
172 self.SetValue(self.setting.getValue(self.settingIndex))
173 self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
174 elif valueOverride is not None and valueOverride is wx.Colour:
175 self.ctrl = wx.ColourPickerCtrl(panel, -1)
176 self.SetValue(self.setting.getValue(self.settingIndex))
177 self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
178 elif type(self.setting.getType()) is list or valueOverride is not None:
179 value = self.setting.getValue(self.settingIndex)
180 choices = self.setting.getType()
181 if valueOverride is not None:
182 choices = valueOverride
183 self._englishChoices = choices[:]
184 if value not in choices and len(choices) > 0:
186 for n in xrange(0, len(choices)):
187 choices[n] = _(choices[n])
189 self.ctrl = wx.ComboBox(panel, -1, value, choices=choices, style=wx.CB_DROPDOWN|wx.CB_READONLY)
190 self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
191 self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
194 self.ctrl = wx.TextCtrl(panel, -1, self.setting.getValue(self.settingIndex))
195 self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
198 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
199 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
202 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
203 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
204 if isinstance(self.ctrl, floatspin.FloatSpin):
205 self.ctrl.GetTextCtrl().Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
206 self.ctrl.GetTextCtrl().Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
207 self.defaultBGColour = self.ctrl.GetTextCtrl().GetBackgroundColour()
209 self.defaultBGColour = self.ctrl.GetBackgroundColour()
211 panel.main.settingControlList.append(self)
213 def OnMouseEnter(self, e):
214 self.panel.main.OnPopupDisplay(self)
216 def OnMouseExit(self, e):
217 self.panel.main.OnPopupHide(self)
220 def OnSettingChange(self, e):
221 self.setting.setValue(self.GetValue(), self.settingIndex)
222 self.panel.main._validate()
225 result, msg = self.setting.validate()
228 if isinstance(ctrl, floatspin.FloatSpin):
229 ctrl = ctrl.GetTextCtrl()
230 if result == validators.ERROR:
231 ctrl.SetBackgroundColour('Red')
232 elif result == validators.WARNING:
233 ctrl.SetBackgroundColour('Yellow')
235 ctrl.SetBackgroundColour(self.defaultBGColour)
238 self.validationMsg = msg
239 self.panel.main.UpdatePopup(self)
242 if isinstance(self.ctrl, wx.ColourPickerCtrl):
243 return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
244 elif isinstance(self.ctrl, wx.ComboBox):
245 value = str(self.ctrl.GetValue())
246 for ret in self._englishChoices:
251 return str(self.ctrl.GetValue())
253 def SetValue(self, value):
254 if isinstance(self.ctrl, wx.CheckBox):
255 self.ctrl.SetValue(str(value) == "True")
256 elif isinstance(self.ctrl, wx.ColourPickerCtrl):
257 self.ctrl.SetColour(value)
258 elif isinstance(self.ctrl, floatspin.FloatSpin):
260 self.ctrl.SetValue(float(value))
263 elif isinstance(self.ctrl, wx.ComboBox):
264 self.ctrl.SetValue(_(value))
266 self.ctrl.SetValue(value)