source: sasview/guiframe/local_perspectives/plotting/slicerpanel.py @ d7a39e5

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 d7a39e5 was d955bf19, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

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