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 configWindowBase(wx.Frame):
9 "A base class for configuration dialogs. Handles creation of settings, and popups"
10 def __init__(self, title, style=wx.DEFAULT_FRAME_STYLE):
11 super(configWindowBase, self).__init__(None, title=title, style=style)
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 OnPopupDisplay(self, setting):
46 self.popup.setting = setting
47 self.UpdatePopup(setting)
50 def OnPopupHide(self, e):
51 self.popup.Show(False)
53 def UpdatePopup(self, setting):
54 if self.popup.setting == setting:
55 if setting.validationMsg != '':
56 self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.helpText)
58 self.popup.text.SetLabel(setting.helpText)
59 self.popup.text.Wrap(350)
61 x, y = setting.ctrl.ClientToScreenXY(0, 0)
62 sx, sy = setting.ctrl.GetSizeTuple()
63 #if platform.system() == "Windows":
64 # for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
65 # wx, wy = self.ClientToScreenXY(0, 0)
68 self.popup.SetPosition((x, y+sy))
70 def updateProfileToControls(self):
71 "Update the configuration wx controls to show the new configuration settings"
72 for setting in self.settingControlList:
73 if setting.type == 'profile':
74 setting.SetValue(profile.getProfileSetting(setting.configName))
76 setting.SetValue(profile.getPreference(setting.configName))
80 def __init__(self, panel, name):
81 "Add a title row to the configuration panel"
82 sizer = panel.GetSizer()
84 self.title = wx.StaticText(panel, -1, name)
85 self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
86 sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
87 sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
91 def __init__(self, panel, label, configName, defaultValue = '', helpText = 'Help: TODO', type = 'profile'):
92 "Add a setting to the configuration panel"
93 sizer = panel.GetSizer()
99 self.validationMsg = ''
100 self.helpText = helpText
101 self.configName = configName
105 self.label = wx.lib.stattext.GenStaticText(panel, -1, label)
106 self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
107 self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
109 getSettingFunc = profile.getPreference
110 if self.type == 'profile':
111 getSettingFunc = profile.getProfileSetting
112 if isinstance(defaultValue, types.StringTypes):
113 self.ctrl = wx.TextCtrl(panel, -1, getSettingFunc(configName))
114 self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
116 elif isinstance(defaultValue, types.BooleanType):
117 self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
118 self.SetValue(getSettingFunc(configName))
119 self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
120 elif isinstance(defaultValue, wx.Colour):
121 self.ctrl = wx.ColourPickerCtrl(panel, -1)
122 self.SetValue(getSettingFunc(configName))
123 self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
125 self.ctrl = wx.ComboBox(panel, -1, getSettingFunc(configName), choices=defaultValue, style=wx.CB_DROPDOWN|wx.CB_READONLY)
126 self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
127 self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
130 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
131 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
134 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
135 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
137 self.defaultBGColour = self.ctrl.GetBackgroundColour()
139 panel.main.settingControlList.append(self)
141 def OnMouseEnter(self, e):
142 self.panel.main.OnPopupDisplay(self)
144 def OnMouseExit(self, e):
145 self.panel.main.OnPopupHide(self)
148 def OnSettingChange(self, e):
149 if self.type == 'profile':
150 profile.putProfileSetting(self.configName, self.GetValue())
152 profile.putPreference(self.configName, self.GetValue())
153 result = validators.SUCCESS
155 for validator in self.validators:
156 res, err = validator.validate()
157 if res == validators.ERROR:
159 elif res == validators.WARNING and result != validators.ERROR:
161 if res != validators.SUCCESS:
163 if result == validators.ERROR:
164 self.ctrl.SetBackgroundColour('Red')
165 elif result == validators.WARNING:
166 self.ctrl.SetBackgroundColour('Yellow')
168 self.ctrl.SetBackgroundColour(self.defaultBGColour)
171 self.validationMsg = '\n'.join(msgs)
172 self.panel.main.UpdatePopup(self)
175 if isinstance(self.ctrl, wx.ColourPickerCtrl):
176 return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
178 return str(self.ctrl.GetValue())
180 def SetValue(self, value):
181 if isinstance(self.ctrl, wx.CheckBox):
182 self.ctrl.SetValue(str(value) == "True")
183 elif isinstance(self.ctrl, wx.ColourPickerCtrl):
184 self.ctrl.SetColour(value)
186 self.ctrl.SetValue(value)
188 #Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
189 # A bit hacky, bit it works.
190 class settingNotify():
191 def __init__(self, setting, func):
192 self.setting = setting
193 self.setting.validators.append(self)
198 return validators.SUCCESS, ''