source: sasview/guiframe/local_perspectives/plotting/slicerpanel.py @ 37f487e

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 37f487e was 37f487e, checked in by Gervaise Alina <gervyh@…>, 15 years ago

hightlight textcrtl for slicerpanel

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