ConfigurationPanel.py 1.77 KB
Newer Older
1
2
3
4
5
6
7
import wx

from MDANSE import REGISTRY
from MDANSE.Framework.Configurators.IConfigurator import ConfiguratorError

class ConfigurationPanel(wx.Panel):
    
8
    def __init__(self, parent, configurable):
9
10
11
        
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        
12
        self._configurable = configurable
13
14
15
16
17
18
19
20
21
        
        self._widgets = {}
        
        self.build_panel()
        
    def build_panel(self):
        
        self.panelSizer = wx.BoxSizer(wx.VERTICAL)

22
        for cfgName, cfg in self._configurable.settings.items():
23

24
            widgetClass = REGISTRY["widget"][cfg[0]]
25
                        
26
            self._widgets[cfgName] = widgetClass(self, cfgName, self._configurable)
27
            
28
            self.panelSizer.Add(self._widgets[cfgName], 0, wx.ALL|wx.EXPAND, 5)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
                  
        self.SetSizer(self.panelSizer)
                                   
        self.Layout()

        self.Fit()
                    
    @property
    def widgets(self):
        return self._widgets
                 
    def get_value(self):
        
        return dict([(k,v.get_value()) for k,v in self._widgets.items()])
    
    def validate(self):

        for w in self._widgets.values():
            w.SetBackgroundColour(wx.NullColour)
            w.Refresh()
             
        try:
            self._configuration.parameters = self.get_value()
        except ConfiguratorError as e:
            d = wx.MessageDialog(self, str(e), style=wx.ICON_ERROR|wx.STAY_ON_TOP|wx.CENTRE)
            d.ShowModal()
            w = self._widgets[e.configurator.name]
            w.SetBackgroundColour("Pink")
            w.Refresh()
            w.SetFocus()
            return False
        else:
            return True