source: sasview/src/sas/guiframe/local_perspectives/plotting/slicerpanel.py @ 79492222

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 79492222 was 79492222, checked in by krzywon, 9 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1
2
3import wx
4import wx.lib.newevent
5#from copy import deepcopy
6from sas.guiframe.utils import format_number
7from sas.guiframe.events import SlicerParameterEvent
8from sas.guiframe.events import EVT_SLICER_PARS
9from sas.guiframe.events import EVT_SLICER
10
11from sas.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 = base.parent
35        self.frame = None
36        self.type = type
37        self.listeners = []
38        self.parameters = []
39        self.bck = wx.GridBagSizer(5, 5)
40        self.SetSizer(self.bck)
41        if type == None and params == None: 
42            label = "Right-click on 2D plot for slicer options" 
43            title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
44            self.bck.Add(title, (0, 0), (1, 2), 
45                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
46        else:
47            self.set_slicer(type, params)
48        ## Bindings
49        self.parent.Bind(EVT_SLICER, self.onEVT_SLICER)
50        self.parent.Bind(EVT_SLICER_PARS, self.onParamChange)
51
52    def onEVT_SLICER(self, event):
53        """
54        Process EVT_SLICER events
55        When the slicer changes, update the panel
56       
57        :param event: EVT_SLICER event
58       
59        """
60        event.Skip()
61        if event.obj_class == None:
62            self.set_slicer(None, None) 
63        else:
64            self.set_slicer(event.type, event.params)
65       
66    def set_slicer(self, type, params):
67        """
68        Rebuild the panel
69        """
70        self.bck.Clear(True) 
71        self.type = type 
72        if type == None:
73            label = "Right-click on 2D plot for slicer options"
74            title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
75            self.bck.Add(title, (0, 0), (1, 2), 
76                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
77        else:
78            title = wx.StaticText(self, -1, "Slicer Parameters", 
79                                  style=wx.ALIGN_LEFT)
80            self.bck.Add(title, (0, 0), (1, 2), 
81                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
82            n = 1
83            self.parameters = []
84            keys = params.keys()
85            keys.sort()
86            for item in keys:
87                if not item.lower() in ["num_points", "avg", "avg_error", "sum",
88                                         "sum_error"]:
89                    n += 1
90                    text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT)
91                    self.bck.Add(text, (n-1, 0), 
92                            flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
93                    ctl = wx.TextCtrl(self, -1, size=(80, 20), 
94                                      style=wx.TE_PROCESS_ENTER)
95                    hint_msg = "Modify the value of %s to change " % item
96                    hint_msg += "the 2D slicer"
97                    ctl.SetToolTipString(hint_msg)
98                    ctl.SetValue(str(format_number(params[item])))
99                    self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter)
100                    ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
101                    ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter)
102                    self.parameters.append([item, ctl])
103                    self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0)
104            for item in keys:
105                if  item.lower() in ["num_points", "avg", "avg_error", "sum",
106                                      "sum_error"]:
107                    n += 1
108                    text = wx.StaticText(self, -1, item + ": ", 
109                                         style=wx.ALIGN_LEFT)
110                    self.bck.Add(text, (n-1, 0), 
111                                 flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 
112                                 border=15)
113                    ctl = wx.StaticText(self, -1, 
114                                        str(format_number(params[item])),
115                                        style=wx.ALIGN_LEFT)
116                    ctl.SetToolTipString("Result %s" % item)
117                    self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0)       
118        self.bck.Layout()
119        #self.bck.Fit(self)
120        self.Layout()
121        psizer = self.parent.GetSizer()
122        if psizer != None:
123            psizer.Layout()
124       
125    def onSetFocus(self, evt):
126        """
127        Hightlight the textcrtl
128        """
129        # Get a handle to the TextCtrl
130        widget = evt.GetEventObject()
131        # Select the whole control, after this event resolves
132        wx.CallAfter(widget.SetSelection, -1, -1)
133        return
134   
135    def onParamChange(self, evt):
136        """
137        Receive and event and reset the text field contained in self.parameters
138           
139        """
140        evt.Skip()
141        for item in self.parameters:             
142            if item[0] in evt.params:
143                item[1].SetValue(format_number(evt.params[item[0]]))
144                item[1].Refresh()
145   
146    def onTextEnter(self, evt): 
147        """
148        Parameters have changed
149        """ 
150        params = {}
151        has_error = False
152        for item in self.parameters:
153            try:
154                params[item[0]] = float(item[1].GetValue())
155                item[1].SetBackgroundColour(
156                        wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
157                item[1].Refresh()
158            except:
159                has_error = True
160                item[1].SetBackgroundColour("pink")
161                item[1].Refresh()
162           
163        if has_error == False:
164            # Post parameter event
165            ## base is guiframe is this case
166            event = SlicerParameterEvent(type=self.type, params=params)
167            wx.PostEvent(self.base, event)
168
169    def on_close(self, event):
170        """
171        On Close Event
172        """
173        ID = self.uid
174        self.parent.delete_panel(ID)           
175        self.frame.Destroy()
Note: See TracBrowser for help on using the repository browser.