source: sasview/guiframe/local_perspectives/plotting/BoxParameters.py @ 54cc36a

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 54cc36a was 92c2345, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on boxslicer

  • Property mode set to 100644
File size: 4.7 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        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       
68        if type==None:
69            title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT)
70            self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
71
72        else:
73            title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT)
74            self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15)
75            ix = 0
76            iy = 0
77            self.parameters = []
78            #params = slicer.get_params()
79            keys = params.keys()
80            keys.sort()
81           
82            for item in keys:
83                iy += 1
84                ix = 0
85                text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT)
86                #self.bck.Add(text, (iy,ix), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15)
87                self.bck.Add(text, (iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
88                ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER)
89               
90                ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item)
91               
92                ix = 1
93                ctl.SetValue(format_number(str(params[item])))
94                self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter)
95                ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter)
96                self.parameters.append([item, ctl])
97                #self.bck.Add(ctl, (iy,ix), flag=wx.TOP|wx.BOTTOM, border = 0)
98                self.bck.Add(ctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
99               
100                ix =3
101                self.bck.Add((20,20), (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
102            iy +=1
103            ix = 1
104            self.bck.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
105        self.bck.Layout()
106        self.bck.Fit(self)
107        self.parent.GetSizer().Layout()
108
109    def onParamChange(self, evt):
110        evt.Skip()
111        if evt.type == "UPDATE":
112            for item in self.parameters:             
113                if item[0] in evt.params:
114                    item[1].SetValue("%-5.3g" %evt.params[item[0]])
115                    item[1].Refresh()
116       
117    def onTextEnter(self, evt): 
118        """
119            Parameters have changed
120        """ 
121        params = {}
122        has_error = False
123        for item in self.parameters:
124            try:
125                params[item[0]] = float(item[1].GetValue())
126                item[1].SetBackgroundColour(
127                        wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
128                item[1].Refresh()
129            except:
130                has_error = True
131                item[1].SetBackgroundColour("pink")
132                item[1].Refresh()
133
134        if has_error==False:
135            # Post parameter event
136            event = SlicerParameterEvent(type=self.type, params=params)
137            wx.PostEvent(self.parent, event)
138       
Note: See TracBrowser for help on using the repository browser.