1 from __future__ import absolute_import
3 import wx, wx.lib.stattext, types
5 from Cura.util import validators
6 from Cura.util import profile
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)
13 self.settingControlList = []
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)
24 def CreateConfigTab(self, nb, name):
25 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
26 nb.AddPage(configPanel, name)
27 return leftConfigPanel, rightConfigPanel
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
45 def CreateSimpleConfigTab(self, nb, name):
46 leftConfigPanel, configPanel = self.CreateSimpleConfigPanel(nb)
47 nb.AddPage(configPanel, name)
48 return leftConfigPanel
50 def CreateSimpleConfigPanel(self, parent):
51 configPanel = wx.Panel(parent);
52 leftConfigPanel = wx.Panel(configPanel)
54 sizer = wx.GridBagSizer(2, 2)
55 leftConfigPanel.SetSizer(sizer)
56 sizer.AddGrowableCol(1)
58 sizer = wx.BoxSizer(wx.HORIZONTAL)
59 configPanel.SetSizer(sizer)
60 sizer.Add(leftConfigPanel, 1, wx.EXPAND)
62 leftConfigPanel.main = self
63 return leftConfigPanel, configPanel
65 def OnPopupDisplay(self, setting):
66 self.popup.setting = setting
67 self.UpdatePopup(setting)
70 def OnPopupHide(self, e):
71 self.popup.Show(False)
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)
78 self.popup.text.SetLabel(setting.helpText)
79 self.popup.text.Wrap(350)
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)
88 self.popup.SetPosition((x, y+sy))
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))
96 setting.SetValue(profile.getPreference(setting.configName))
100 def __init__(self, panel, name):
101 "Add a title row to the configuration panel"
102 sizer = panel.GetSizer()
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)
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()
119 self.validationMsg = ''
120 self.helpText = helpText
121 self.configName = configName
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)
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)
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)
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)
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)
154 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
155 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
157 self.defaultBGColour = self.ctrl.GetBackgroundColour()
159 panel.main.settingControlList.append(self)
161 def OnMouseEnter(self, e):
162 self.panel.main.OnPopupDisplay(self)
164 def OnMouseExit(self, e):
165 self.panel.main.OnPopupHide(self)
168 def OnSettingChange(self, e):
169 if self.type == 'profile':
170 profile.putProfileSetting(self.configName, self.GetValue())
172 profile.putPreference(self.configName, self.GetValue())
173 result = validators.SUCCESS
175 for validator in self.validators:
176 res, err = validator.validate()
177 if res == validators.ERROR:
179 elif res == validators.WARNING and result != validators.ERROR:
181 if res != validators.SUCCESS:
183 if result == validators.ERROR:
184 self.ctrl.SetBackgroundColour('Red')
185 elif result == validators.WARNING:
186 self.ctrl.SetBackgroundColour('Yellow')
188 self.ctrl.SetBackgroundColour(self.defaultBGColour)
191 self.validationMsg = '\n'.join(msgs)
192 self.panel.main.UpdatePopup(self)
195 if isinstance(self.ctrl, wx.ColourPickerCtrl):
196 return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
198 return str(self.ctrl.GetValue())
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)
206 self.ctrl.SetValue(value)
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)
218 return validators.SUCCESS, ''