chiark / gitweb /
Merge branch 'master' of github.com:daid/Cura
[cura.git] / Cura / gui / configBase.py
1 from __future__ import absolute_import
2
3 import wx, wx.lib.stattext, types
4
5 from Cura.util import validators
6 from Cura.util import profile
7
8 class configPanelBase(wx.Panel):
9         "A base class for configuration dialogs. Handles creation of settings, and popups"
10         def __init__(self, parent):
11                 super(configPanelBase, self).__init__(parent)
12                 
13                 self.settingControlList = []
14                 
15                 #Create the popup window
16                 self.popup = wx.PopupWindow(self, flags=wx.BORDER_SIMPLE)
17                 self.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
18                 self.popup.setting = None
19                 self.popup.text = wx.StaticText(self.popup, -1, '');
20                 self.popup.sizer = wx.BoxSizer()
21                 self.popup.sizer.Add(self.popup.text, flag=wx.EXPAND|wx.ALL, border=1)
22                 self.popup.SetSizer(self.popup.sizer)
23         
24         def CreateConfigTab(self, nb, name):
25                 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
26                 nb.AddPage(configPanel, name)
27                 return leftConfigPanel, rightConfigPanel
28         
29         def CreateConfigPanel(self, parent):
30                 configPanel = wx.Panel(parent);
31                 leftConfigPanel = wx.Panel(configPanel)
32                 rightConfigPanel = wx.Panel(configPanel)
33                 sizer = wx.GridBagSizer(2, 2)
34                 leftConfigPanel.SetSizer(sizer)
35                 sizer = wx.GridBagSizer(2, 2)
36                 rightConfigPanel.SetSizer(sizer)
37                 sizer = wx.BoxSizer(wx.HORIZONTAL)
38                 configPanel.SetSizer(sizer)
39                 sizer.Add(leftConfigPanel, border=35, flag=wx.RIGHT)
40                 sizer.Add(rightConfigPanel)
41                 leftConfigPanel.main = self
42                 rightConfigPanel.main = self
43                 return leftConfigPanel, rightConfigPanel, configPanel
44
45         def CreateSimpleConfigTab(self, nb, name):
46                 leftConfigPanel, configPanel = self.CreateSimpleConfigPanel(nb)
47                 nb.AddPage(configPanel, name)
48                 return leftConfigPanel
49         
50         def CreateSimpleConfigPanel(self, parent):
51                 configPanel = wx.Panel(parent);
52                 leftConfigPanel = wx.Panel(configPanel)
53                 
54                 sizer = wx.GridBagSizer(2, 2)
55                 leftConfigPanel.SetSizer(sizer)
56                 sizer.AddGrowableCol(1)
57
58                 sizer = wx.BoxSizer(wx.HORIZONTAL)
59                 configPanel.SetSizer(sizer)
60                 sizer.Add(leftConfigPanel, 1, wx.EXPAND)
61
62                 leftConfigPanel.main = self
63                 return leftConfigPanel, configPanel
64
65         def OnPopupDisplay(self, setting):
66                 self.popup.setting = setting
67                 self.UpdatePopup(setting)
68                 self.popup.Show(True)
69                 
70         def OnPopupHide(self, e):
71                 self.popup.Show(False)
72         
73         def UpdatePopup(self, setting):
74                 if self.popup.setting == setting:
75                         if setting.validationMsg != '':
76                                 self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.helpText)
77                         else:
78                                 self.popup.text.SetLabel(setting.helpText)
79                         self.popup.text.Wrap(350)
80                         self.popup.Fit()
81                         x, y = setting.ctrl.ClientToScreenXY(0, 0)
82                         sx, sy = setting.ctrl.GetSizeTuple()
83                         #if platform.system() == "Windows":
84                         #       for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
85                         #       wx, wy = self.ClientToScreenXY(0, 0)
86                         #       x -= wx
87                         #       y -= wy
88                         self.popup.SetPosition((x, y+sy))
89         
90         def updateProfileToControls(self):
91                 "Update the configuration wx controls to show the new configuration settings"
92                 for setting in self.settingControlList:
93                         if setting.type == 'profile':
94                                 setting.SetValue(profile.getProfileSetting(setting.configName))
95                         else:
96                                 setting.SetValue(profile.getPreference(setting.configName))
97                 self.Update()
98
99 class TitleRow():
100         def __init__(self, panel, name):
101                 "Add a title row to the configuration panel"
102                 sizer = panel.GetSizer()
103                 x = sizer.GetRows()
104                 self.title = wx.StaticText(panel, -1, name)
105                 self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
106                 sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
107                 sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
108                 sizer.SetRows(x + 2)
109
110 class SettingRow():
111         def __init__(self, panel, label, configName, defaultValue = '', helpText = 'Help: TODO', type = 'profile'):
112                 "Add a setting to the configuration panel"
113                 sizer = panel.GetSizer()
114                 x = sizer.GetRows()
115                 y = 0
116                 flag = 0
117                 
118                 self.validators = []
119                 self.validationMsg = ''
120                 self.helpText = helpText
121                 self.configName = configName
122                 self.panel = panel
123                 self.type = type
124
125                 self.label = wx.lib.stattext.GenStaticText(panel, -1, label)
126                 self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
127                 self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
128
129                 getSettingFunc = profile.getPreference
130                 if self.type == 'profile':
131                         getSettingFunc = profile.getProfileSetting
132                 if isinstance(defaultValue, types.StringTypes):
133                         self.ctrl = wx.TextCtrl(panel, -1, getSettingFunc(configName))
134                         self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
135                         flag = wx.EXPAND
136                 elif isinstance(defaultValue, types.BooleanType):
137                         self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
138                         self.SetValue(getSettingFunc(configName))
139                         self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
140                 elif isinstance(defaultValue, wx.Colour):
141                         self.ctrl = wx.ColourPickerCtrl(panel, -1)
142                         self.SetValue(getSettingFunc(configName))
143                         self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
144                 else:
145                         self.ctrl = wx.ComboBox(panel, -1, getSettingFunc(configName), choices=defaultValue, style=wx.CB_DROPDOWN|wx.CB_READONLY)
146                         self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
147                         self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
148                         flag = wx.EXPAND
149
150                 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
151                 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
152                 sizer.SetRows(x+1)
153
154                 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
155                 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
156                 
157                 self.defaultBGColour = self.ctrl.GetBackgroundColour()
158                 
159                 panel.main.settingControlList.append(self)
160
161         def OnMouseEnter(self, e):
162                 self.panel.main.OnPopupDisplay(self)
163
164         def OnMouseExit(self, e):
165                 self.panel.main.OnPopupHide(self)
166                 e.Skip()
167
168         def OnSettingChange(self, e):
169                 if self.type == 'profile':
170                         profile.putProfileSetting(self.configName, self.GetValue())
171                 else:
172                         profile.putPreference(self.configName, self.GetValue())
173                 result = validators.SUCCESS
174                 msgs = []
175                 for validator in self.validators:
176                         res, err = validator.validate()
177                         if res == validators.ERROR:
178                                 result = res
179                         elif res == validators.WARNING and result != validators.ERROR:
180                                 result = res
181                         if res != validators.SUCCESS:
182                                 msgs.append(err)
183                 if result == validators.ERROR:
184                         self.ctrl.SetBackgroundColour('Red')
185                 elif result == validators.WARNING:
186                         self.ctrl.SetBackgroundColour('Yellow')
187                 else:
188                         self.ctrl.SetBackgroundColour(self.defaultBGColour)
189                 self.ctrl.Refresh()
190
191                 self.validationMsg = '\n'.join(msgs)
192                 self.panel.main.UpdatePopup(self)
193
194         def GetValue(self):
195                 if isinstance(self.ctrl, wx.ColourPickerCtrl):
196                         return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
197                 else:
198                         return str(self.ctrl.GetValue())
199
200         def SetValue(self, value):
201                 if isinstance(self.ctrl, wx.CheckBox):
202                         self.ctrl.SetValue(str(value) == "True")
203                 elif isinstance(self.ctrl, wx.ColourPickerCtrl):
204                         self.ctrl.SetColour(value)
205                 else:
206                         self.ctrl.SetValue(value)
207
208 #Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
209 # A bit hacky, bit it works.
210 class settingNotify():
211         def __init__(self, setting, func):
212                 self.setting = setting
213                 self.setting.validators.append(self)
214                 self.func = func
215         
216         def validate(self):
217                 self.func()
218                 return validators.SUCCESS, ''
219