1 from __future__ import absolute_import
2 from __future__ import division
5 import wx, wx.lib.stattext, types
6 from wx.lib.agw import floatspin
8 from Cura.util import validators
9 from Cura.util import profile
11 class configPanelBase(wx.Panel):
12 "A base class for configuration dialogs. Handles creation of settings, and popups"
13 def __init__(self, parent):
14 super(configPanelBase, self).__init__(parent)
16 self.settingControlList = []
18 #Create the popup window
19 self.popup = wx.PopupWindow(self, flags=wx.BORDER_SIMPLE)
20 self.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
21 self.popup.setting = None
22 self.popup.text = wx.StaticText(self.popup, -1, '');
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 def CreateConfigTab(self, nb, name):
28 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
29 nb.AddPage(configPanel, name)
30 return leftConfigPanel, rightConfigPanel
32 def CreateConfigPanel(self, parent):
33 configPanel = wx.Panel(parent);
34 leftConfigPanel = wx.Panel(configPanel)
35 rightConfigPanel = wx.Panel(configPanel)
37 sizer = wx.GridBagSizer(2, 2)
38 leftConfigPanel.SetSizer(sizer)
39 sizer = wx.GridBagSizer(2, 2)
40 rightConfigPanel.SetSizer(sizer)
42 sizer = wx.BoxSizer(wx.HORIZONTAL)
43 configPanel.SetSizer(sizer)
44 sizer.Add(leftConfigPanel, border=35, flag=wx.RIGHT)
45 sizer.Add(rightConfigPanel)
46 leftConfigPanel.main = self
47 rightConfigPanel.main = self
48 return leftConfigPanel, rightConfigPanel, configPanel
50 def CreateDynamicConfigTab(self, nb, name):
51 configPanel = wx.lib.scrolledpanel.ScrolledPanel(nb)
52 #configPanel = wx.Panel(nb);
53 leftConfigPanel = wx.Panel(configPanel)
54 rightConfigPanel = wx.Panel(configPanel)
56 sizer = wx.GridBagSizer(2, 2)
57 leftConfigPanel.SetSizer(sizer)
58 sizer.AddGrowableCol(1)
60 sizer = wx.GridBagSizer(2, 2)
61 rightConfigPanel.SetSizer(sizer)
62 sizer.AddGrowableCol(1)
64 sizer = wx.BoxSizer(wx.HORIZONTAL)
65 sizer.Add(leftConfigPanel, proportion=1, border=35, flag=wx.EXPAND)
66 sizer.Add(rightConfigPanel, proportion=1, flag=wx.EXPAND)
67 configPanel.SetSizer(sizer)
69 configPanel.SetAutoLayout(1)
70 configPanel.SetupScrolling(scroll_x=False, scroll_y=True)
72 leftConfigPanel.main = self
73 rightConfigPanel.main = self
75 configPanel.leftPanel = leftConfigPanel
76 configPanel.rightPanel = rightConfigPanel
78 nb.AddPage(configPanel, name)
80 return leftConfigPanel, rightConfigPanel, configPanel
82 def OnPopupDisplay(self, setting):
83 self.popup.setting = setting
84 self.UpdatePopup(setting)
87 def OnPopupHide(self, e):
88 self.popup.Show(False)
90 def UpdatePopup(self, setting):
91 if self.popup.setting == setting:
92 if setting.validationMsg != '':
93 self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.setting.getTooltip())
95 self.popup.text.SetLabel(setting.setting.getTooltip())
96 self.popup.text.Wrap(350)
98 x, y = setting.ctrl.ClientToScreenXY(0, 0)
99 sx, sy = setting.ctrl.GetSizeTuple()
100 #if platform.system() == "Windows":
101 # for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
102 # wx, wy = self.ClientToScreenXY(0, 0)
105 self.popup.SetPosition((x, y+sy))
107 def updateProfileToControls(self):
108 "Update the configuration wx controls to show the new configuration settings"
109 for setting in self.settingControlList:
110 setting.SetValue(setting.setting.getValue())
113 def getLabelColumnWidth(self, panel):
115 for child in panel.GetChildren():
116 if isinstance(child, wx.lib.stattext.GenStaticText):
117 maxWidth = max(maxWidth, child.GetSize()[0])
120 def setLabelColumnWidth(self, panel, width):
121 for child in panel.GetChildren():
122 if isinstance(child, wx.lib.stattext.GenStaticText):
123 size = child.GetSize()
125 child.SetBestSize(size)
128 def __init__(self, panel, name):
129 "Add a title row to the configuration panel"
130 sizer = panel.GetSizer()
132 self.title = wx.StaticText(panel, -1, name)
133 self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
134 sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
135 sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
139 def __init__(self, panel, configName):
140 "Add a setting to the configuration panel"
141 sizer = panel.GetSizer()
146 self.setting = profile.settingsDictionary[configName]
147 self.validationMsg = ''
150 self.label = wx.lib.stattext.GenStaticText(panel, -1, self.setting.getLabel())
151 self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
152 self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
154 if self.setting.getType() is types.FloatType and False:
156 while 1 / pow(10, digits) > defaultValue:
158 self.ctrl = floatspin.FloatSpin(panel, -1, value=float(getSettingFunc(configName)), increment=defaultValue, digits=digits, min_val=0.0)
159 self.ctrl.Bind(floatspin.EVT_FLOATSPIN, self.OnSettingChange)
161 elif self.setting.getType() is types.BooleanType:
162 self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
163 self.SetValue(self.setting.getValue())
164 self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
165 elif self.setting.getType() is wx.Colour:
166 self.ctrl = wx.ColourPickerCtrl(panel, -1)
167 self.SetValue(self.setting.getValue())
168 self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
169 elif type(self.setting.getType()) is list:
170 self.ctrl = wx.ComboBox(panel, -1, self.setting.getValue(), choices=self.setting.getType(), style=wx.CB_DROPDOWN|wx.CB_READONLY)
171 self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
172 self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
175 self.ctrl = wx.TextCtrl(panel, -1, self.setting.getValue())
176 self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
179 # Set the minimum size of control to something other than the humungous default
180 minSize = self.ctrl.GetMinSize()
182 ##if platform.system() == "Darwin":
183 ## # Under MacOS, it appears that the minSize is used for the actual size, so give the field a bit more room...
187 ##self.ctrl.SetMinSize(minSize)
189 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
190 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
193 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
194 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
195 if isinstance(self.ctrl, floatspin.FloatSpin):
196 self.ctrl.GetTextCtrl().Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
197 self.ctrl.GetTextCtrl().Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
198 self.defaultBGColour = self.ctrl.GetTextCtrl().GetBackgroundColour()
200 self.defaultBGColour = self.ctrl.GetBackgroundColour()
202 panel.main.settingControlList.append(self)
204 def OnMouseEnter(self, e):
205 self.panel.main.OnPopupDisplay(self)
207 def OnMouseExit(self, e):
208 self.panel.main.OnPopupHide(self)
211 def OnSettingChange(self, e):
212 self.setting.setValue(self.GetValue())
213 result, msg = self.setting.validate()
216 if isinstance(ctrl, floatspin.FloatSpin):
217 ctrl = ctrl.GetTextCtrl()
218 if result == validators.ERROR:
219 ctrl.SetBackgroundColour('Red')
220 elif result == validators.WARNING:
221 ctrl.SetBackgroundColour('Yellow')
223 ctrl.SetBackgroundColour(self.defaultBGColour)
226 self.validationMsg = msg
227 self.panel.main.UpdatePopup(self)
230 if isinstance(self.ctrl, wx.ColourPickerCtrl):
231 return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
233 return str(self.ctrl.GetValue())
235 def SetValue(self, value):
236 if isinstance(self.ctrl, wx.CheckBox):
237 self.ctrl.SetValue(str(value) == "True")
238 elif isinstance(self.ctrl, wx.ColourPickerCtrl):
239 self.ctrl.SetColour(value)
240 elif isinstance(self.ctrl, floatspin.FloatSpin):
242 self.ctrl.SetValue(float(value))
246 self.ctrl.SetValue(value)
248 #Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
249 # A bit hacky, bit it works.
250 class settingNotify():
251 def __init__(self, setting, func):
252 self.setting = setting
253 self.setting.validators.append(self)
258 return validators.SUCCESS, ''