ConfigurationPanel.py 1.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
import wx

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

class ConfigurationPanel(wx.Panel):
    
    def __init__(self, parent, configuration):
        
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        
        self._configuration = configuration
        
        self._widgets = {}
        
        self.build_panel()
        
    def build_panel(self):
        
        self.panelSizer = wx.BoxSizer(wx.VERTICAL)

        for cfg in self._configuration.configurators.values():

            widgetClass = REGISTRY["configurator_widget"][cfg.widget]
                        
            self._widgets[cfg.name] = widgetClass(self, cfg.name, self._configuration)
            
            self.panelSizer.Add(self._widgets[cfg.name], 0, wx.ALL|wx.EXPAND, 5)
                  
        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