source: sasview/guiframe/local_perspectives/plotting/SlicerParameters.py @ 7608cb5

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 7608cb5 was 54cc36a, checked in by Gervaise Alina <gervyh@…>, 16 years ago

working on boxsum

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