chiark / gitweb /
Copyright message needs to be after the __future__ imports.
[cura.git] / Cura / gui / configBase.py
1 from __future__ import absolute_import
2 from __future__ import division
3 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
4
5 import platform
6 import wx, wx.lib.stattext, types
7 from wx.lib.agw import floatspin
8
9 from Cura.util import validators
10 from Cura.util import profile
11
12 class configPanelBase(wx.Panel):
13         "A base class for configuration dialogs. Handles creation of settings, and popups"
14         def __init__(self, parent, changeCallback = None):
15                 super(configPanelBase, self).__init__(parent)
16                 
17                 self.settingControlList = []
18                 
19                 #Create the popup window
20                 self.popup = wx.PopupWindow(self, flags=wx.BORDER_SIMPLE)
21                 self.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
22                 self.popup.setting = None
23                 self.popup.text = wx.StaticText(self.popup, -1, '');
24                 self.popup.sizer = wx.BoxSizer()
25                 self.popup.sizer.Add(self.popup.text, flag=wx.EXPAND|wx.ALL, border=1)
26                 self.popup.SetSizer(self.popup.sizer)
27
28                 self._callback = changeCallback
29         
30         def CreateConfigTab(self, nb, name):
31                 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
32                 nb.AddPage(configPanel, name)
33                 return leftConfigPanel, rightConfigPanel
34         
35         def CreateConfigPanel(self, parent):
36                 configPanel = wx.Panel(parent);
37                 leftConfigPanel = wx.Panel(configPanel)
38                 rightConfigPanel = wx.Panel(configPanel)
39
40                 sizer = wx.GridBagSizer(2, 2)
41                 leftConfigPanel.SetSizer(sizer)
42                 sizer = wx.GridBagSizer(2, 2)
43                 rightConfigPanel.SetSizer(sizer)
44
45                 sizer = wx.BoxSizer(wx.HORIZONTAL)
46                 configPanel.SetSizer(sizer)
47                 sizer.Add(leftConfigPanel, border=35, flag=wx.RIGHT)
48                 sizer.Add(rightConfigPanel)
49                 leftConfigPanel.main = self
50                 rightConfigPanel.main = self
51                 return leftConfigPanel, rightConfigPanel, configPanel
52
53         def CreateDynamicConfigTab(self, nb, name):
54                 configPanel = wx.lib.scrolledpanel.ScrolledPanel(nb)    
55                 #configPanel = wx.Panel(nb);
56                 leftConfigPanel = wx.Panel(configPanel)
57                 rightConfigPanel = wx.Panel(configPanel)
58
59                 sizer = wx.GridBagSizer(2, 2)
60                 leftConfigPanel.SetSizer(sizer)
61                 #sizer.AddGrowableCol(1)
62
63                 sizer = wx.GridBagSizer(2, 2)
64                 rightConfigPanel.SetSizer(sizer)
65                 #sizer.AddGrowableCol(1)
66
67                 sizer = wx.BoxSizer(wx.HORIZONTAL)
68                 sizer.Add(leftConfigPanel, proportion=1, border=35, flag=wx.EXPAND)
69                 sizer.Add(rightConfigPanel, proportion=1, flag=wx.EXPAND)
70                 configPanel.SetSizer(sizer)
71
72                 configPanel.SetAutoLayout(1)
73                 configPanel.SetupScrolling(scroll_x=False, scroll_y=True)
74
75                 leftConfigPanel.main = self
76                 rightConfigPanel.main = self
77
78                 configPanel.leftPanel = leftConfigPanel
79                 configPanel.rightPanel = rightConfigPanel
80
81                 nb.AddPage(configPanel, name)
82
83                 return leftConfigPanel, rightConfigPanel, configPanel
84
85         def OnPopupDisplay(self, setting):
86                 self.popup.setting = setting
87                 self.UpdatePopup(setting)
88                 self.popup.Show(True)
89                 
90         def OnPopupHide(self, e):
91                 self.popup.Show(False)
92         
93         def UpdatePopup(self, setting):
94                 if self.popup.setting == setting:
95                         if setting.validationMsg != '':
96                                 self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.setting.getTooltip())
97                         else:
98                                 self.popup.text.SetLabel(setting.setting.getTooltip())
99                         self.popup.text.Wrap(350)
100                         self.popup.Fit()
101                         x, y = setting.ctrl.ClientToScreenXY(0, 0)
102                         sx, sy = setting.ctrl.GetSizeTuple()
103                         #if platform.system() == "Windows":
104                         #       for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
105                         #       wx, wy = self.ClientToScreenXY(0, 0)
106                         #       x -= wx
107                         #       y -= wy
108                         self.popup.SetPosition((x, y+sy))
109         
110         def updateProfileToControls(self):
111                 "Update the configuration wx controls to show the new configuration settings"
112                 for setting in self.settingControlList:
113                         setting.SetValue(setting.setting.getValue())
114                 self.Update()
115
116         def _validate(self):
117                 for setting in self.settingControlList:
118                         setting._validate()
119                 if self._callback is not None:
120                         self._callback()
121
122         def getLabelColumnWidth(self, panel):
123                 maxWidth = 0
124                 for child in panel.GetChildren():
125                         if isinstance(child, wx.lib.stattext.GenStaticText):
126                                 maxWidth = max(maxWidth, child.GetSize()[0])
127                 return maxWidth
128         
129         def setLabelColumnWidth(self, panel, width):
130                 for child in panel.GetChildren():
131                         if isinstance(child, wx.lib.stattext.GenStaticText):
132                                 size = child.GetSize()
133                                 size[0] = width
134                                 child.SetBestSize(size)
135         
136 class TitleRow():
137         def __init__(self, panel, name):
138                 "Add a title row to the configuration panel"
139                 sizer = panel.GetSizer()
140                 x = sizer.GetRows()
141                 self.title = wx.StaticText(panel, -1, name.replace('&', '&&'))
142                 self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
143                 sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
144                 sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
145                 sizer.SetRows(x + 2)
146
147 class SettingRow():
148         def __init__(self, panel, configName, valueOverride = None):
149                 "Add a setting to the configuration panel"
150                 sizer = panel.GetSizer()
151                 x = sizer.GetRows()
152                 y = 0
153                 flag = 0
154
155                 self.setting = profile.settingsDictionary[configName]
156                 self.validationMsg = ''
157                 self.panel = panel
158
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)
162
163                 #if self.setting.getType() is types.FloatType and False:
164                 #       digits = 0
165                 #       while 1 / pow(10, digits) > defaultValue:
166                 #               digits += 1
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)
169                 #       flag = wx.EXPAND
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())
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())
177                         self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
178                 elif type(self.setting.getType()) is list or valueOverride is not None:
179                         if valueOverride is not None:
180                                 self.ctrl = wx.ComboBox(panel, -1, self.setting.getValue(), choices=valueOverride, style=wx.CB_DROPDOWN|wx.CB_READONLY)
181                         else:
182                                 self.ctrl = wx.ComboBox(panel, -1, self.setting.getValue(), choices=self.setting.getType(), style=wx.CB_DROPDOWN|wx.CB_READONLY)
183                         self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
184                         self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
185                         flag = wx.EXPAND
186                 else:
187                         self.ctrl = wx.TextCtrl(panel, -1, self.setting.getValue())
188                         self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
189                         flag = wx.EXPAND
190
191                 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
192                 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
193                 sizer.SetRows(x+1)
194
195                 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
196                 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
197                 if isinstance(self.ctrl, floatspin.FloatSpin):
198                         self.ctrl.GetTextCtrl().Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
199                         self.ctrl.GetTextCtrl().Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
200                         self.defaultBGColour = self.ctrl.GetTextCtrl().GetBackgroundColour()
201                 else:
202                         self.defaultBGColour = self.ctrl.GetBackgroundColour()
203                 
204                 panel.main.settingControlList.append(self)
205
206         def OnMouseEnter(self, e):
207                 self.panel.main.OnPopupDisplay(self)
208
209         def OnMouseExit(self, e):
210                 self.panel.main.OnPopupHide(self)
211                 e.Skip()
212
213         def OnSettingChange(self, e):
214                 self.setting.setValue(self.GetValue())
215                 self.panel.main._validate()
216
217         def _validate(self):
218                 result, msg = self.setting.validate()
219
220                 ctrl = self.ctrl
221                 if isinstance(ctrl, floatspin.FloatSpin):
222                         ctrl = ctrl.GetTextCtrl()
223                 if result == validators.ERROR:
224                         ctrl.SetBackgroundColour('Red')
225                 elif result == validators.WARNING:
226                         ctrl.SetBackgroundColour('Yellow')
227                 else:
228                         ctrl.SetBackgroundColour(self.defaultBGColour)
229                 ctrl.Refresh()
230
231                 self.validationMsg = msg
232                 self.panel.main.UpdatePopup(self)
233
234         def GetValue(self):
235                 if isinstance(self.ctrl, wx.ColourPickerCtrl):
236                         return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
237                 else:
238                         return str(self.ctrl.GetValue())
239
240         def SetValue(self, value):
241                 if isinstance(self.ctrl, wx.CheckBox):
242                         self.ctrl.SetValue(str(value) == "True")
243                 elif isinstance(self.ctrl, wx.ColourPickerCtrl):
244                         self.ctrl.SetColour(value)
245                 elif isinstance(self.ctrl, floatspin.FloatSpin):
246                         try:
247                                 self.ctrl.SetValue(float(value))
248                         except ValueError:
249                                 pass
250                 else:
251                         self.ctrl.SetValue(value)
252
253 #Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
254 # A bit hacky, bit it works.
255 class settingNotify():
256         def __init__(self, setting, func):
257                 self.setting = setting
258                 self.setting.validators.append(self)
259                 self.func = func
260         
261         def validate(self):
262                 self.func()
263                 return validators.SUCCESS, ''
264