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

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

Fixed the issue outlined in trac ticket #349. The box sum panel did not
seem to allow focus when changing the values for the sum from the panel.
A simple evt.Skip() was all that was necessary.

  • 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_text = str(type) + "Parameters"
79            title = wx.StaticText(self, -1, title_text, 
80                                  style=wx.ALIGN_LEFT)
81            self.bck.Add(title, (0, 0), (1, 2), 
82                         flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
83            n = 1
84            self.parameters = []
85            keys = params.keys()
86            keys.sort()
87            for item in keys:
88                if not item.lower() in ["num_points", "avg", "avg_error", "sum",
89                                         "sum_error"]:
90                    n += 1
91                    text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT)
92                    self.bck.Add(text, (n-1, 0), 
93                            flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
94                    ctl = wx.TextCtrl(self, -1, size=(80, 20), 
95                                      style=wx.TE_PROCESS_ENTER)
96                    hint_msg = "Modify the value of %s to change " % item
97                    hint_msg += "the 2D slicer"
98                    ctl.SetToolTipString(hint_msg)
99                    ctl.SetValue(str(format_number(params[item])))
100                    self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter)
101                    ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
102                    ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter)
103                    self.parameters.append([item, ctl])
104                    self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0)
105            for item in keys:
106                if  item.lower() in ["num_points", "avg", "avg_error", "sum",
107                                      "sum_error"]:
108                    n += 1
109                    text = wx.StaticText(self, -1, item + ": ", 
110                                         style=wx.ALIGN_LEFT)
111                    self.bck.Add(text, (n-1, 0), 
112                                 flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 
113                                 border=15)
114                    ctl = wx.StaticText(self, -1, 
115                                        str(format_number(params[item])),
116                                        style=wx.ALIGN_LEFT)
117                    ctl.SetToolTipString("Result %s" % item)
118                    self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0)       
119        self.bck.Layout()
120        #self.bck.Fit(self)
121        self.Layout()
122        psizer = self.parent.GetSizer()
123        if psizer != None:
124            psizer.Layout()
125       
126    def onSetFocus(self, evt):
127        """
128        Highlight the txtcrtl
129        """
130        evt.Skip()
131        # Get a handle to the TextCtrl
132        widget = evt.GetEventObject()
133        # Select the whole control, after this event resolves
134        wx.CallAfter(widget.SetSelection, -1, -1)
135        return
136   
137    def onParamChange(self, evt):
138        """
139        Receive and event and reset the text field contained in self.parameters
140           
141        """
142        evt.Skip()
143        for item in self.parameters:             
144            if item[0] in evt.params:
145                item[1].SetValue(format_number(evt.params[item[0]]))
146                item[1].Refresh()
147   
148    def onTextEnter(self, evt): 
149        """
150        Parameters have changed
151        """
152        evt.Skip()
153        params = {}
154        has_error = False
155        for item in self.parameters:
156            try:
157                params[item[0]] = float(item[1].GetValue())
158                item[1].SetBackgroundColour(
159                        wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
160                item[1].Refresh()
161            except:
162                has_error = True
163                item[1].SetBackgroundColour("pink")
164                item[1].Refresh()
165           
166        if has_error == False:
167            # Post parameter event
168            ## base is guiframe is this case
169            event = SlicerParameterEvent(type=self.type, params=params)
170            wx.PostEvent(self.base, event)
171
172    def on_close(self, event):
173        """
174        On Close Event
175        """
176        ID = self.uid
177        self.parent.delete_panel(ID)           
178        self.frame.Destroy()
Note: See TracBrowser for help on using the repository browser.