chiark / gitweb /
Fix bug in centerX/Y update.
[cura.git] / Cura / gui / configBase.py
1 from __future__ import absolute_import
2 import __init__
3
4 import wx, wx.lib.stattext, os, sys, platform, types
5
6 from gui import validators
7 from util import profile
8
9 def main():
10         app = wx.App(False)
11         mainWindow()
12         app.MainLoop()
13
14 class configWindowBase(wx.Frame):
15         "A base class for configuration dialogs. Handles creation of settings, and popups"
16         def __init__(self, title):
17                 super(configWindowBase, self).__init__(None, title=title)
18                 
19                 self.settingControlList = []
20                 
21                 #Create the popup window
22                 self.popup = wx.PopupWindow(self, flags=wx.BORDER_SIMPLE)
23                 self.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
24                 self.popup.setting = None
25                 self.popup.text = wx.StaticText(self.popup, -1, '');
26                 self.popup.sizer = wx.BoxSizer()
27                 self.popup.sizer.Add(self.popup.text, flag=wx.EXPAND|wx.ALL, border=1)
28                 self.popup.SetSizer(self.popup.sizer)
29         
30         def CreateConfigTab(self, nb, name):
31                 leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
32                 nb.AddPage(configPanel, name)
33                 return leftConfigPanel, rightConfigPanel
34         
35         def CreateConfigPanel(self, parent):
36                 configPanel = wx.Panel(parent);
37                 leftConfigPanel = wx.Panel(configPanel)
38                 rightConfigPanel = wx.Panel(configPanel)
39                 sizer = wx.GridBagSizer(2, 2)
40                 leftConfigPanel.SetSizer(sizer)
41                 sizer = wx.GridBagSizer(2, 2)
42                 rightConfigPanel.SetSizer(sizer)
43                 sizer = wx.BoxSizer(wx.HORIZONTAL)
44                 configPanel.SetSizer(sizer)
45                 sizer.Add(leftConfigPanel)
46                 sizer.Add(rightConfigPanel)
47                 leftConfigPanel.main = self
48                 rightConfigPanel.main = self
49                 return leftConfigPanel, rightConfigPanel, configPanel
50
51         def OnPopupDisplay(self, setting):
52                 self.popup.setting = setting
53                 self.UpdatePopup(setting)
54                 self.popup.Show(True)
55                 
56         def OnPopupHide(self, e):
57                 self.popup.Show(False)
58         
59         def UpdatePopup(self, setting):
60                 if self.popup.setting == setting:
61                         if setting.validationMsg != '':
62                                 self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.helpText)
63                         else:
64                                 self.popup.text.SetLabel(setting.helpText)
65                         self.popup.text.Wrap(350)
66                         self.popup.Fit()
67                         if os.name == 'darwin':
68                                 x, y = self.ClientToScreenXY(0, 0)
69                                 sx, sy = self.GetClientSizeTuple()
70                         else:
71                                 x, y = setting.ctrl.ClientToScreenXY(0, 0)
72                                 sx, sy = setting.ctrl.GetSizeTuple()
73                         #if platform.system() == "Windows":
74                         #       for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
75                         #       wx, wy = self.ClientToScreenXY(0, 0)
76                         #       x -= wx
77                         #       y -= wy
78                         self.popup.SetPosition((x, y+sy))
79         
80         def updateProfileToControls(self):
81                 "Update the configuration wx controls to show the new configuration settings"
82                 for setting in self.settingControlList:
83                         if setting.type == 'profile':
84                                 setting.SetValue(profile.getProfileSetting(setting.configName))
85                         else:
86                                 setting.SetValue(profile.getPreference(setting.configName))
87
88 class TitleRow():
89         def __init__(self, panel, name):
90                 "Add a title row to the configuration panel"
91                 sizer = panel.GetSizer()
92                 x = sizer.GetRows()
93                 self.title = wx.StaticText(panel, -1, name)
94                 self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
95                 sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND)
96                 sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND)
97                 sizer.SetRows(x + 2)
98
99 class SettingRow():
100         def __init__(self, panel, label, configName, defaultValue = '', helpText = 'Help: TODO', type = 'profile'):
101                 "Add a setting to the configuration panel"
102                 sizer = panel.GetSizer()
103                 x = sizer.GetRows()
104                 y = 0
105                 
106                 self.validators = []
107                 self.validationMsg = ''
108                 self.helpText = helpText
109                 self.configName = configName
110                 self.panel = panel
111                 self.type = type
112
113                 self.label = wx.lib.stattext.GenStaticText(panel, -1, label)
114                 self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
115                 self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
116
117                 getSettingFunc = profile.getPreference
118                 if self.type == 'profile':
119                         getSettingFunc = profile.getProfileSetting
120                 if isinstance(defaultValue, types.StringTypes):
121                         self.ctrl = wx.TextCtrl(panel, -1, getSettingFunc(configName))
122                         self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
123                 elif isinstance(defaultValue, types.BooleanType):
124                         self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
125                         self.SetValue(getSettingFunc(configName))
126                         self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
127                 else:
128                         self.ctrl = wx.ComboBox(panel, -1, getSettingFunc(configName), choices=defaultValue, style=wx.CB_DROPDOWN|wx.CB_READONLY)
129                         self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
130
131                 sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL)
132                 sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|wx.EXPAND)
133                 sizer.SetRows(x+1)
134
135                 self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
136                 self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
137                 
138                 self.defaultBGColour = self.ctrl.GetBackgroundColour()
139                 
140                 panel.main.settingControlList.append(self)
141
142         def OnMouseEnter(self, e):
143                 self.panel.main.OnPopupDisplay(self)
144
145         def OnMouseExit(self, e):
146                 self.panel.main.OnPopupHide(self)
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                 return str(self.ctrl.GetValue())
176
177         def SetValue(self, value):
178                 if isinstance(self.ctrl, wx.CheckBox):
179                         self.ctrl.SetValue(str(value) == "True")
180                 else:
181                         self.ctrl.SetValue(value)
182
183 #Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
184 # A bit hacky, bit it works.
185 class settingNotify():
186         def __init__(self, setting, func):
187                 self.setting = setting
188                 self.setting.validators.append(self)
189                 self.func = func
190         
191         def validate(self):
192                 self.func()
193                 return validators.SUCCESS, ''
194