chiark / gitweb /
Move resources in to their own subdirectory and update the code that uses the resources.
[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 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)
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 OnPopupDisplay(self, setting):
46                 self.popup.setting = setting
47                 self.UpdatePopup(setting)
48                 self.popup.Show(True)
49                 
50         def OnPopupHide(self, e):
51                 self.popup.Show(False)
52         
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)
57                         else:
58                                 self.popup.text.SetLabel(setting.helpText)
59                         self.popup.text.Wrap(350)
60                         self.popup.Fit()
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)
66                         #       x -= wx
67                         #       y -= wy
68                         self.popup.SetPosition((x, y+sy))
69         
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))
75                         else:
76                                 setting.SetValue(profile.getPreference(setting.configName))
77                 self.Update()
78
79 class TitleRow():
80         def __init__(self, panel, name):
81                 "Add a title row to the configuration panel"
82                 sizer = panel.GetSizer()
83                 x = sizer.GetRows()
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)
88                 sizer.SetRows(x + 2)
89
90 class SettingRow():
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()
94                 x = sizer.GetRows()
95                 y = 0
96                 flag = 0
97                 
98                 self.validators = []
99                 self.validationMsg = ''
100                 self.helpText = helpText
101                 self.configName = configName
102                 self.panel = panel
103                 self.type = type
104
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)
108
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)
115                         flag = wx.EXPAND
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)
124                 else:
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)
128                         flag = wx.EXPAND
129
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)
132                 sizer.SetRows(x+1)
133
134                 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
135                 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
136                 
137                 self.defaultBGColour = self.ctrl.GetBackgroundColour()
138                 
139                 panel.main.settingControlList.append(self)
140
141         def OnMouseEnter(self, e):
142                 self.panel.main.OnPopupDisplay(self)
143
144         def OnMouseExit(self, e):
145                 self.panel.main.OnPopupHide(self)
146                 e.Skip()
147
148         def OnSettingChange(self, e):
149                 if self.type == 'profile':
150                         profile.putProfileSetting(self.configName, self.GetValue())
151                 else:
152                         profile.putPreference(self.configName, self.GetValue())
153                 result = validators.SUCCESS
154                 msgs = []
155                 for validator in self.validators:
156                         res, err = validator.validate()
157                         if res == validators.ERROR:
158                                 result = res
159                         elif res == validators.WARNING and result != validators.ERROR:
160                                 result = res
161                         if res != validators.SUCCESS:
162                                 msgs.append(err)
163                 if result == validators.ERROR:
164                         self.ctrl.SetBackgroundColour('Red')
165                 elif result == validators.WARNING:
166                         self.ctrl.SetBackgroundColour('Yellow')
167                 else:
168                         self.ctrl.SetBackgroundColour(self.defaultBGColour)
169                 self.ctrl.Refresh()
170
171                 self.validationMsg = '\n'.join(msgs)
172                 self.panel.main.UpdatePopup(self)
173
174         def GetValue(self):
175                 if isinstance(self.ctrl, wx.ColourPickerCtrl):
176                         return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
177                 else:
178                         return str(self.ctrl.GetValue())
179
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)
185                 else:
186                         self.ctrl.SetValue(value)
187
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)
194                 self.func = func
195         
196         def validate(self):
197                 self.func()
198                 return validators.SUCCESS, ''
199