chiark / gitweb /
Merge branch 'master' into SteamEngine
[cura.git] / Cura / gui / configBase.py
1 from __future__ import absolute_import
2 from __future__ import division
3
4 import platform
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):
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         def CreateConfigTab(self, nb, name):
28                 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
29                 nb.AddPage(configPanel, name)
30                 return leftConfigPanel, rightConfigPanel
31         
32         def CreateConfigPanel(self, parent):
33                 configPanel = wx.Panel(parent);
34                 leftConfigPanel = wx.Panel(configPanel)
35                 rightConfigPanel = wx.Panel(configPanel)
36
37                 sizer = wx.GridBagSizer(2, 2)
38                 leftConfigPanel.SetSizer(sizer)
39                 sizer = wx.GridBagSizer(2, 2)
40                 rightConfigPanel.SetSizer(sizer)
41
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
49
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)
55
56                 sizer = wx.GridBagSizer(2, 2)
57                 leftConfigPanel.SetSizer(sizer)
58                 sizer.AddGrowableCol(1)
59
60                 sizer = wx.GridBagSizer(2, 2)
61                 rightConfigPanel.SetSizer(sizer)
62                 sizer.AddGrowableCol(1)
63
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)
68
69                 configPanel.SetAutoLayout(1)
70                 configPanel.SetupScrolling(scroll_x=False, scroll_y=True)
71
72                 leftConfigPanel.main = self
73                 rightConfigPanel.main = self
74
75                 configPanel.leftPanel = leftConfigPanel
76                 configPanel.rightPanel = rightConfigPanel
77
78                 nb.AddPage(configPanel, name)
79
80                 return leftConfigPanel, rightConfigPanel, configPanel
81
82         def OnPopupDisplay(self, setting):
83                 self.popup.setting = setting
84                 self.UpdatePopup(setting)
85                 self.popup.Show(True)
86                 
87         def OnPopupHide(self, e):
88                 self.popup.Show(False)
89         
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())
94                         else:
95                                 self.popup.text.SetLabel(setting.setting.getTooltip())
96                         self.popup.text.Wrap(350)
97                         self.popup.Fit()
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)
103                         #       x -= wx
104                         #       y -= wy
105                         self.popup.SetPosition((x, y+sy))
106         
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())
111                 self.Update()
112
113         def getLabelColumnWidth(self, panel):
114                 maxWidth = 0
115                 for child in panel.GetChildren():
116                         if isinstance(child, wx.lib.stattext.GenStaticText):
117                                 maxWidth = max(maxWidth, child.GetSize()[0])
118                 return maxWidth
119         
120         def setLabelColumnWidth(self, panel, width):
121                 for child in panel.GetChildren():
122                         if isinstance(child, wx.lib.stattext.GenStaticText):
123                                 size = child.GetSize()
124                                 size[0] = width
125                                 child.SetBestSize(size)
126         
127 class TitleRow():
128         def __init__(self, panel, name):
129                 "Add a title row to the configuration panel"
130                 sizer = panel.GetSizer()
131                 x = sizer.GetRows()
132                 self.title = wx.StaticText(panel, -1, name.replace('&', '&&'))
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)
136                 sizer.SetRows(x + 2)
137
138 class SettingRow():
139         def __init__(self, panel, configName, valueOverride = None):
140                 "Add a setting to the configuration panel"
141                 sizer = panel.GetSizer()
142                 x = sizer.GetRows()
143                 y = 0
144                 flag = 0
145
146                 self.setting = profile.settingsDictionary[configName]
147                 self.validationMsg = ''
148                 self.panel = panel
149
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)
153
154                 if self.setting.getType() is types.FloatType and False:
155                         digits = 0
156                         while 1 / pow(10, digits) > defaultValue:
157                                 digits += 1
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)
160                         flag = wx.EXPAND
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 or valueOverride is not None:
170                         if valueOverride is not None:
171                                 self.ctrl = wx.ComboBox(panel, -1, self.setting.getValue(), choices=valueOverride, style=wx.CB_DROPDOWN|wx.CB_READONLY)
172                         else:
173                                 self.ctrl = wx.ComboBox(panel, -1, self.setting.getValue(), choices=self.setting.getType(), style=wx.CB_DROPDOWN|wx.CB_READONLY)
174                         self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
175                         self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
176                         flag = wx.EXPAND
177                 else:
178                         self.ctrl = wx.TextCtrl(panel, -1, self.setting.getValue())
179                         self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
180                         flag = wx.EXPAND
181
182                 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
183                 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
184                 sizer.SetRows(x+1)
185
186                 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
187                 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
188                 if isinstance(self.ctrl, floatspin.FloatSpin):
189                         self.ctrl.GetTextCtrl().Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
190                         self.ctrl.GetTextCtrl().Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
191                         self.defaultBGColour = self.ctrl.GetTextCtrl().GetBackgroundColour()
192                 else:
193                         self.defaultBGColour = self.ctrl.GetBackgroundColour()
194                 
195                 panel.main.settingControlList.append(self)
196
197         def OnMouseEnter(self, e):
198                 self.panel.main.OnPopupDisplay(self)
199
200         def OnMouseExit(self, e):
201                 self.panel.main.OnPopupHide(self)
202                 e.Skip()
203
204         def OnSettingChange(self, e):
205                 self.setting.setValue(self.GetValue())
206                 result, msg = self.setting.validate()
207
208                 ctrl = self.ctrl
209                 if isinstance(ctrl, floatspin.FloatSpin):
210                         ctrl = ctrl.GetTextCtrl()
211                 if result == validators.ERROR:
212                         ctrl.SetBackgroundColour('Red')
213                 elif result == validators.WARNING:
214                         ctrl.SetBackgroundColour('Yellow')
215                 else:
216                         ctrl.SetBackgroundColour(self.defaultBGColour)
217                 ctrl.Refresh()
218
219                 self.validationMsg = msg
220                 self.panel.main.UpdatePopup(self)
221
222         def GetValue(self):
223                 if isinstance(self.ctrl, wx.ColourPickerCtrl):
224                         return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
225                 else:
226                         return str(self.ctrl.GetValue())
227
228         def SetValue(self, value):
229                 if isinstance(self.ctrl, wx.CheckBox):
230                         self.ctrl.SetValue(str(value) == "True")
231                 elif isinstance(self.ctrl, wx.ColourPickerCtrl):
232                         self.ctrl.SetColour(value)
233                 elif isinstance(self.ctrl, floatspin.FloatSpin):
234                         try:
235                                 self.ctrl.SetValue(float(value))
236                         except ValueError:
237                                 pass
238                 else:
239                         self.ctrl.SetValue(value)
240
241 #Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
242 # A bit hacky, bit it works.
243 class settingNotify():
244         def __init__(self, setting, func):
245                 self.setting = setting
246                 self.setting.validators.append(self)
247                 self.func = func
248         
249         def validate(self):
250                 self.func()
251                 return validators.SUCCESS, ''
252