source: sasview/test/guiframe/local_perspectives/plotting/slicerpanel.py @ 01dc067

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 01dc067 was 01dc067, checked in by Gervaise Alina <gervyh@…>, 13 years ago

testing copy

  • Property mode set to 100644
File size: 6.1 KB
Line 
1
2
3import wx
4import wx.lib.newevent
5#from copy import deepcopy
6from sans.guiframe.utils import format_number
7from sans.guiframe.events import SlicerParameterEvent
8from sans.guiframe.events import EVT_SLICER_PARS
9from sans.guiframe.events import EVT_SLICER
10
11from sans.guiframe.panel_base import PanelBase
12
13class SlicerPanel(wx.Panel, PanelBase):
14    """
15    Panel class to show the slicer parameters
16    """
17    #TODO: show units
18    #TODO: order parameters properly
19     ## Internal name for the AUI manager
20    window_name = "Slicer panel"
21    ## Title to appear on top of the window
22    window_caption = "Slicer Panel"
23    CENTER_PANE = False
24   
25    def __init__(self, parent, id=-1, type=None, base=None,
26                params=None, *args, **kwargs):
27        wx.Panel.__init__(self, parent, id, *args, **kwargs)
28        PanelBase.__init__(self)
29        ##  Initialization of the class     
30        self.base = base
31        if params is None:
32            params = {}
33        self.params = params
34        self.parent = parent
35        self.type = type
36        self.listeners = []
37        self.parameters = []
38        self.bck = wx.GridBagSizer(5, 5)
39        self.SetSizer(self.bck)
40        if type == None and params == None: 
41            label = "Right-click on 2D plot for slicer options" 
42            title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
43            self.bck.Add(title, (0, 0), (1, 2), 
44                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
45        else:
46            self.set_slicer(type, params)
47        ## Bindings
48        self.parent.Bind(EVT_SLICER, self.onEVT_SLICER)
49        self.parent.Bind(EVT_SLICER_PARS, self.onParamChange)
50
51    def onEVT_SLICER(self, event):
52        """
53        Process EVT_SLICER events
54        When the slicer changes, update the panel
55       
56        :param event: EVT_SLICER event
57       
58        """
59        event.Skip()
60        if event.obj_class == None:
61            self.set_slicer(None, None) 
62        else:
63            self.set_slicer(event.type, event.params)
64       
65    def set_slicer(self, type, params):
66        """
67        Rebuild the panel
68        """
69        self.bck.Clear(True) 
70        self.type = type 
71        if type == None:
72            label = "Right-click on 2D plot for slicer options"
73            title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
74            self.bck.Add(title, (0, 0), (1, 2), 
75                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
76        else:
77            title = wx.StaticText(self, -1, "Slicer Parameters", 
78                                  style=wx.ALIGN_LEFT)
79            self.bck.Add(title, (0, 0), (1, 2), 
80                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
81            n = 1
82            self.parameters = []
83            keys = params.keys()
84            keys.sort()
85            for item in keys:
86                if not item.lower() in ["errors", "count"]:
87                    n += 1
88                    text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT)
89                    self.bck.Add(text, (n-1, 0), 
90                            flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
91                    ctl = wx.TextCtrl(self, -1, size=(80, 20), 
92                                      style=wx.TE_PROCESS_ENTER)
93                    hint_msg = "Modify the value of %s to change " % item
94                    hint_msg += "the 2D slicer"
95                    ctl.SetToolTipString(hint_msg)
96                    ctl.SetValue(str(format_number(params[item])))
97                    self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter)
98                    ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
99                    ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter)
100                    self.parameters.append([item, ctl])
101                    self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0)
102            for item in keys:
103                if  item.lower() in ["errors", "count"]:
104                    n += 1
105                    text = wx.StaticText(self, -1, item + ": ", 
106                                         style=wx.ALIGN_LEFT)
107                    self.bck.Add(text, (n-1, 0), 
108                                 flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 
109                                 border=15)
110                    ctl = wx.StaticText(self, -1, 
111                                        str(format_number(params[item])),
112                                        style=wx.ALIGN_LEFT)
113                    ctl.SetToolTipString("Result %s" % item)
114                    self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0)       
115        self.bck.Layout()
116        #self.bck.Fit(self)
117        self.Layout()
118        self.parent.GetSizer().Layout()
119       
120    def onSetFocus(self, evt):
121        """
122        Hightlight the textcrtl
123        """
124        # Get a handle to the TextCtrl
125        widget = evt.GetEventObject()
126        # Select the whole control, after this event resolves
127        wx.CallAfter(widget.SetSelection, -1, -1)
128        return
129   
130    def onParamChange(self, evt):
131        """
132        Receive and event and reset the text field contained in self.parameters
133           
134        """
135        evt.Skip()
136        for item in self.parameters:             
137            if item[0] in evt.params:
138                item[1].SetValue(format_number(evt.params[item[0]]))
139                item[1].Refresh()
140   
141    def onTextEnter(self, evt): 
142        """
143        Parameters have changed
144        """ 
145        params = {}
146        has_error = False
147        for item in self.parameters:
148            try:
149                params[item[0]] = float(item[1].GetValue())
150                item[1].SetBackgroundColour(
151                        wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
152                item[1].Refresh()
153            except:
154                has_error = True
155                item[1].SetBackgroundColour("pink")
156                item[1].Refresh()
157           
158        if has_error == False:
159            # Post parameter event
160            ## base is guiframe is this case
161            event = SlicerParameterEvent(type=self.type, params=params)
162            wx.PostEvent(self.base, event)
163           
164       
Note: See TracBrowser for help on using the repository browser.