1 | """ |
---|
2 | Panel class to show the slicer parameters |
---|
3 | """ |
---|
4 | |
---|
5 | import wx |
---|
6 | import wx.lib.newevent |
---|
7 | from copy import deepcopy |
---|
8 | |
---|
9 | from sans.guiframe.utils import format_number |
---|
10 | from sans.guicomm.events import SlicerParameterEvent,EVT_SLICER_PARS,EVT_SLICER |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | class 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.Layout() |
---|
104 | self.parent.GetSizer().Layout() |
---|
105 | |
---|
106 | def onSetFocus(self, evt): |
---|
107 | """ |
---|
108 | Hightlight the textcrtl |
---|
109 | """ |
---|
110 | # Get a handle to the TextCtrl |
---|
111 | widget = evt.GetEventObject() |
---|
112 | # Select the whole control, after this event resolves |
---|
113 | wx.CallAfter(widget.SetSelection, -1,-1) |
---|
114 | return |
---|
115 | |
---|
116 | |
---|
117 | def onParamChange(self, evt): |
---|
118 | """ |
---|
119 | Receive and event and reset the text field contained in self.parameters |
---|
120 | |
---|
121 | """ |
---|
122 | evt.Skip() |
---|
123 | for item in self.parameters: |
---|
124 | if item[0] in evt.params: |
---|
125 | item[1].SetValue(format_number(evt.params[item[0]])) |
---|
126 | item[1].Refresh() |
---|
127 | |
---|
128 | |
---|
129 | def onTextEnter(self, evt): |
---|
130 | """ |
---|
131 | Parameters have changed |
---|
132 | """ |
---|
133 | params = {} |
---|
134 | has_error = False |
---|
135 | for item in self.parameters: |
---|
136 | try: |
---|
137 | params[item[0]] = float(item[1].GetValue()) |
---|
138 | item[1].SetBackgroundColour( |
---|
139 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
140 | item[1].Refresh() |
---|
141 | except: |
---|
142 | has_error = True |
---|
143 | item[1].SetBackgroundColour("pink") |
---|
144 | item[1].Refresh() |
---|
145 | |
---|
146 | if has_error==False: |
---|
147 | # Post parameter event |
---|
148 | ## base is guiframe is this case |
---|
149 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
150 | wx.PostEvent(self.base, event) |
---|
151 | |
---|
152 | |
---|