chiark / gitweb /
Add "Reload platform" to refresh all the objects on the platform from their files
[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 wx, wx.lib.stattext, types
6 from wx.lib.agw import floatspin
7
8 from Cura.util import validators
9 from Cura.util import profile
10
11 class configPanelBase(wx.Panel):
12         "A base class for configuration dialogs. Handles creation of settings, and popups"
13         def __init__(self, parent, changeCallback = None):
14                 super(configPanelBase, self).__init__(parent)
15                 
16                 self.settingControlList = []
17                 
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)
26
27                 self._callback = changeCallback
28         
29         def CreateConfigTab(self, nb, name):
30                 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
31                 nb.AddPage(configPanel, name)
32                 return leftConfigPanel, rightConfigPanel
33         
34         def CreateConfigPanel(self, parent):
35                 configPanel = wx.Panel(parent);
36                 leftConfigPanel = wx.Panel(configPanel)
37                 rightConfigPanel = wx.Panel(configPanel)
38
39                 sizer = wx.GridBagSizer(2, 2)
40                 leftConfigPanel.SetSizer(sizer)
41                 sizer = wx.GridBagSizer(2, 2)
42                 rightConfigPanel.SetSizer(sizer)
43
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
51
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)
57
58                 sizer = wx.GridBagSizer(2, 2)
59                 leftConfigPanel.SetSizer(sizer)
60                 #sizer.AddGrowableCol(1)
61
62                 sizer = wx.GridBagSizer(2, 2)
63                 rightConfigPanel.SetSizer(sizer)
64                 #sizer.AddGrowableCol(1)
65
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)
70
71                 configPanel.SetAutoLayout(1)
72                 configPanel.SetupScrolling(scroll_x=False, scroll_y=True)
73
74                 leftConfigPanel.main = self
75                 rightConfigPanel.main = self
76
77                 configPanel.leftPanel = leftConfigPanel
78                 configPanel.rightPanel = rightConfigPanel
79
80                 nb.AddPage(configPanel, name)
81
82                 return leftConfigPanel, rightConfigPanel, configPanel
83
84         def OnPopupDisplay(self, setting):
85                 self.popup.setting = setting
86                 self.UpdatePopup(setting)
87                 self.popup.Show(True)
88                 
89         def OnPopupHide(self, e):
90                 self.popup.Show(False)
91         
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())
96                         else:
97                                 self.popup.text.SetLabel(setting.setting.getTooltip())
98                         self.popup.text.Wrap(350)
99                         self.popup.Fit()
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)
105                         #       x -= wx
106                         #       y -= wy
107                         self.popup.SetPosition((x, y+sy))
108         
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())
113                 self.Update()
114
115         def _validate(self):
116                 for setting in self.settingControlList:
117                         setting._validate()
118                 if self._callback is not None:
119                         self._callback()
120
121         def getLabelColumnWidth(self, panel):
122                 maxWidth = 0
123                 for child in panel.GetChildren():
124                         if isinstance(child, wx.lib.stattext.GenStaticText):
125                                 maxWidth = max(maxWidth, child.GetSize()[0])
126                 return maxWidth
127         
128         def setLabelColumnWidth(self, panel, width):
129                 for child in panel.GetChildren():
130                         if isinstance(child, wx.lib.stattext.GenStaticText):
131                                 size = child.GetSize()
132                                 size[0] = width
133                                 child.SetBestSize(size)
134         
135 class TitleRow(object):
136         def __init__(self, panel, name):
137                 "Add a title row to the configuration panel"
138                 sizer = panel.GetSizer()
139                 x = sizer.GetRows()
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)
144                 sizer.SetRows(x + 2)
145
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()
150                 x = sizer.GetRows()
151                 y = 0
152                 flag = 0
153
154                 self.setting = profile.settingsDictionary[configName]
155                 self.settingIndex = index
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(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:
185                                 value = choices[0]
186                         for n in xrange(0, len(choices)):
187                                 choices[n] = _(choices[n])
188                         value = _(value)
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)
192                         flag = wx.EXPAND
193                 else:
194                         self.ctrl = wx.TextCtrl(panel, -1, self.setting.getValue(self.settingIndex))
195                         self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
196                         flag = wx.EXPAND
197
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)
200                 sizer.SetRows(x+1)
201
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()
208                 else:
209                         self.defaultBGColour = self.ctrl.GetBackgroundColour()
210                 
211                 panel.main.settingControlList.append(self)
212
213         def OnMouseEnter(self, e):
214                 self.panel.main.OnPopupDisplay(self)
215
216         def OnMouseExit(self, e):
217                 self.panel.main.OnPopupHide(self)
218                 e.Skip()
219
220         def OnSettingChange(self, e):
221                 self.setting.setValue(self.GetValue(), self.settingIndex)
222                 self.panel.main._validate()
223
224         def _validate(self):
225                 result, msg = self.setting.validate()
226
227                 ctrl = self.ctrl
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')
234                 else:
235                         ctrl.SetBackgroundColour(self.defaultBGColour)
236                 ctrl.Refresh()
237
238                 self.validationMsg = msg
239                 self.panel.main.UpdatePopup(self)
240
241         def GetValue(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:
247                                 if _(ret) == value:
248                                         return ret
249                         return value
250                 else:
251                         return str(self.ctrl.GetValue())
252
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):
259                         try:
260                                 self.ctrl.SetValue(float(value))
261                         except ValueError:
262                                 pass
263                 elif isinstance(self.ctrl, wx.ComboBox):
264                         self.ctrl.SetValue(_(value))
265                 else:
266                         self.ctrl.SetValue(value)