chiark / gitweb /
Update code for translations, fix a few translation issues and add a few missing...
[cura.git] / Cura / gui / configBase.py
index 43c5a784621d7db6f49540358b98a558595a8f1b..3cf6a1d0eaf87abe149a241738330025173f4e21 100644 (file)
@@ -1,7 +1,7 @@
 from __future__ import absolute_import
 from __future__ import division
+__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
 
-import platform
 import wx, wx.lib.stattext, types
 from wx.lib.agw import floatspin
 
@@ -10,7 +10,7 @@ from Cura.util import profile
 
 class configPanelBase(wx.Panel):
        "A base class for configuration dialogs. Handles creation of settings, and popups"
-       def __init__(self, parent):
+       def __init__(self, parent, changeCallback = None):
                super(configPanelBase, self).__init__(parent)
                
                self.settingControlList = []
@@ -23,6 +23,8 @@ class configPanelBase(wx.Panel):
                self.popup.sizer = wx.BoxSizer()
                self.popup.sizer.Add(self.popup.text, flag=wx.EXPAND|wx.ALL, border=1)
                self.popup.SetSizer(self.popup.sizer)
+
+               self._callback = changeCallback
        
        def CreateConfigTab(self, nb, name):
                leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
@@ -55,11 +57,11 @@ class configPanelBase(wx.Panel):
 
                sizer = wx.GridBagSizer(2, 2)
                leftConfigPanel.SetSizer(sizer)
-               sizer.AddGrowableCol(1)
+               #sizer.AddGrowableCol(1)
 
                sizer = wx.GridBagSizer(2, 2)
                rightConfigPanel.SetSizer(sizer)
-               sizer.AddGrowableCol(1)
+               #sizer.AddGrowableCol(1)
 
                sizer = wx.BoxSizer(wx.HORIZONTAL)
                sizer.Add(leftConfigPanel, proportion=1, border=35, flag=wx.EXPAND)
@@ -90,9 +92,9 @@ class configPanelBase(wx.Panel):
        def UpdatePopup(self, setting):
                if self.popup.setting == setting:
                        if setting.validationMsg != '':
-                               self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.helpText)
+                               self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.setting.getTooltip())
                        else:
-                               self.popup.text.SetLabel(setting.helpText)
+                               self.popup.text.SetLabel(setting.setting.getTooltip())
                        self.popup.text.Wrap(350)
                        self.popup.Fit()
                        x, y = setting.ctrl.ClientToScreenXY(0, 0)
@@ -107,12 +109,15 @@ class configPanelBase(wx.Panel):
        def updateProfileToControls(self):
                "Update the configuration wx controls to show the new configuration settings"
                for setting in self.settingControlList:
-                       if setting.type == 'profile':
-                               setting.SetValue(profile.getProfileSetting(setting.configName))
-                       else:
-                               setting.SetValue(profile.getPreference(setting.configName))
+                       setting.SetValue(setting.setting.getValue())
                self.Update()
 
+       def _validate(self):
+               for setting in self.settingControlList:
+                       setting._validate()
+               if self._callback is not None:
+                       self._callback()
+
        def getLabelColumnWidth(self, panel):
                maxWidth = 0
                for child in panel.GetChildren():
@@ -127,74 +132,69 @@ class configPanelBase(wx.Panel):
                                size[0] = width
                                child.SetBestSize(size)
        
-class TitleRow():
+class TitleRow(object):
        def __init__(self, panel, name):
                "Add a title row to the configuration panel"
                sizer = panel.GetSizer()
                x = sizer.GetRows()
-               self.title = wx.StaticText(panel, -1, name)
+               self.title = wx.StaticText(panel, -1, name.replace('&', '&&'))
                self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
                sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
                sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
                sizer.SetRows(x + 2)
 
-class SettingRow():
-       def __init__(self, panel, label, configName, defaultValue = '', helpText = 'Help: TODO', type = 'profile'):
+class SettingRow(object):
+       def __init__(self, panel, configName, valueOverride = None, index = None):
                "Add a setting to the configuration panel"
                sizer = panel.GetSizer()
                x = sizer.GetRows()
                y = 0
                flag = 0
-               
-               self.validators = []
+
+               self.setting = profile.settingsDictionary[configName]
+               self.settingIndex = index
                self.validationMsg = ''
-               self.helpText = helpText
-               self.configName = configName
                self.panel = panel
-               self.type = type
 
-               self.label = wx.lib.stattext.GenStaticText(panel, -1, label)
+               self.label = wx.lib.stattext.GenStaticText(panel, -1, self.setting.getLabel())
                self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
                self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
 
-               getSettingFunc = profile.getPreference
-               if self.type == 'profile':
-                       getSettingFunc = profile.getProfileSetting
-               if isinstance(defaultValue, types.StringTypes):
-                       self.ctrl = wx.TextCtrl(panel, -1, getSettingFunc(configName))
-                       self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
-                       flag = wx.EXPAND
-               elif isinstance(defaultValue, types.FloatType):
-                       digits = 0
-                       while 1 / pow(10, digits) > defaultValue:
-                               digits += 1
-                       self.ctrl = floatspin.FloatSpin(panel, -1, value=float(getSettingFunc(configName)), increment=defaultValue, digits=digits, min_val=0.0)
-                       self.ctrl.Bind(floatspin.EVT_FLOATSPIN, self.OnSettingChange)
-                       flag = wx.EXPAND
-               elif isinstance(defaultValue, types.BooleanType):
+               #if self.setting.getType() is types.FloatType and False:
+               #       digits = 0
+               #       while 1 / pow(10, digits) > defaultValue:
+               #               digits += 1
+               #       self.ctrl = floatspin.FloatSpin(panel, -1, value=float(getSettingFunc(configName)), increment=defaultValue, digits=digits, min_val=0.0)
+               #       self.ctrl.Bind(floatspin.EVT_FLOATSPIN, self.OnSettingChange)
+               #       flag = wx.EXPAND
+               if self.setting.getType() is types.BooleanType:
                        self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
-                       self.SetValue(getSettingFunc(configName))
+                       self.SetValue(self.setting.getValue(self.settingIndex))
                        self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
-               elif isinstance(defaultValue, wx.Colour):
+               elif valueOverride is not None and valueOverride is wx.Colour:
                        self.ctrl = wx.ColourPickerCtrl(panel, -1)
-                       self.SetValue(getSettingFunc(configName))
+                       self.SetValue(self.setting.getValue(self.settingIndex))
                        self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
-               else:
-                       self.ctrl = wx.ComboBox(panel, -1, getSettingFunc(configName), choices=defaultValue, style=wx.CB_DROPDOWN|wx.CB_READONLY)
+               elif type(self.setting.getType()) is list or valueOverride is not None:
+                       value = self.setting.getValue(self.settingIndex)
+                       choices = self.setting.getType()
+                       if valueOverride is not None:
+                               choices = valueOverride
+                       self._englishChoices = choices[:]
+                       if value not in choices and len(choices) > 0:
+                               value = choices[0]
+                       for n in xrange(0, len(choices)):
+                               choices[n] = _(choices[n])
+                       value = _(value)
+                       self.ctrl = wx.ComboBox(panel, -1, value, choices=choices, style=wx.CB_DROPDOWN|wx.CB_READONLY)
                        self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
                        self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
                        flag = wx.EXPAND
+               else:
+                       self.ctrl = wx.TextCtrl(panel, -1, self.setting.getValue(self.settingIndex))
+                       self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
+                       flag = wx.EXPAND
 
-               # Set the minimum size of control to something other than the humungous default
-               minSize = self.ctrl.GetMinSize()
-               
-               ##if platform.system() == "Darwin":
-               ##      # Under MacOS, it appears that the minSize is used for the actual size, so give the field a bit more room...
-               ##      minSize[0] = 150
-               ##else:
-               ##      minSize[0] = 50
-               ##self.ctrl.SetMinSize(minSize)
-               
                sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
                sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
                sizer.SetRows(x+1)
@@ -218,20 +218,12 @@ class SettingRow():
                e.Skip()
 
        def OnSettingChange(self, e):
-               if self.type == 'profile':
-                       profile.putProfileSetting(self.configName, self.GetValue())
-               else:
-                       profile.putPreference(self.configName, self.GetValue())
-               result = validators.SUCCESS
-               msgs = []
-               for validator in self.validators:
-                       res, err = validator.validate()
-                       if res == validators.ERROR:
-                               result = res
-                       elif res == validators.WARNING and result != validators.ERROR:
-                               result = res
-                       if res != validators.SUCCESS:
-                               msgs.append(err)
+               self.setting.setValue(self.GetValue(), self.settingIndex)
+               self.panel.main._validate()
+
+       def _validate(self):
+               result, msg = self.setting.validate()
+
                ctrl = self.ctrl
                if isinstance(ctrl, floatspin.FloatSpin):
                        ctrl = ctrl.GetTextCtrl()
@@ -243,12 +235,18 @@ class SettingRow():
                        ctrl.SetBackgroundColour(self.defaultBGColour)
                ctrl.Refresh()
 
-               self.validationMsg = '\n'.join(msgs)
+               self.validationMsg = msg
                self.panel.main.UpdatePopup(self)
 
        def GetValue(self):
                if isinstance(self.ctrl, wx.ColourPickerCtrl):
                        return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
+               elif isinstance(self.ctrl, wx.ComboBox):
+                       value = str(self.ctrl.GetValue())
+                       for ret in self._englishChoices:
+                               if _(ret) == value:
+                                       return ret
+                       return value
                else:
                        return str(self.ctrl.GetValue())
 
@@ -262,18 +260,7 @@ class SettingRow():
                                self.ctrl.SetValue(float(value))
                        except ValueError:
                                pass
+               elif isinstance(self.ctrl, wx.ComboBox):
+                       self.ctrl.SetValue(_(value))
                else:
                        self.ctrl.SetValue(value)
-
-#Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
-# A bit hacky, bit it works.
-class settingNotify():
-       def __init__(self, setting, func):
-               self.setting = setting
-               self.setting.validators.append(self)
-               self.func = func
-       
-       def validate(self):
-               self.func()
-               return validators.SUCCESS, ''
-