1 | import wx |
---|
2 | import wx.lib.newevent |
---|
3 | #from copy import deepcopy |
---|
4 | from sas.sasgui.guiframe.utils import format_number |
---|
5 | from sas.sasgui.guiframe.events import SlicerParameterEvent |
---|
6 | from sas.sasgui.guiframe.events import EVT_SLICER_PARS |
---|
7 | from sas.sasgui.guiframe.events import EVT_SLICER |
---|
8 | |
---|
9 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
10 | |
---|
11 | class SlicerPanel(wx.Panel, PanelBase): |
---|
12 | """ |
---|
13 | Panel class to show the slicer parameters |
---|
14 | """ |
---|
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 | CENTER_PANE = False |
---|
22 | |
---|
23 | def __init__(self, parent, id=-1, type=None, base=None, |
---|
24 | params=None, *args, **kwargs): |
---|
25 | wx.Panel.__init__(self, parent, id, *args, **kwargs) |
---|
26 | PanelBase.__init__(self) |
---|
27 | ## Initialization of the class |
---|
28 | self.base = base |
---|
29 | if params is None: |
---|
30 | params = {} |
---|
31 | self.params = params |
---|
32 | self.parent = base.parent |
---|
33 | self.frame = None |
---|
34 | self.type = type |
---|
35 | self.listeners = [] |
---|
36 | self.parameters = [] |
---|
37 | self.bck = wx.GridBagSizer(5, 5) |
---|
38 | self.SetSizer(self.bck) |
---|
39 | if type is None and params is None: |
---|
40 | label = "Right-click on 2D plot for slicer options" |
---|
41 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
42 | self.bck.Add(title, (0, 0), (1, 2), |
---|
43 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
44 | else: |
---|
45 | self.set_slicer(type, params) |
---|
46 | ## Bindings |
---|
47 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
48 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
49 | |
---|
50 | def onEVT_SLICER(self, event): |
---|
51 | """ |
---|
52 | Process EVT_SLICER events |
---|
53 | When the slicer changes, update the panel |
---|
54 | |
---|
55 | :param event: EVT_SLICER event |
---|
56 | |
---|
57 | """ |
---|
58 | event.Skip() |
---|
59 | if event.obj_class is None: |
---|
60 | self.set_slicer(None, None) |
---|
61 | else: |
---|
62 | self.set_slicer(event.type, event.params) |
---|
63 | |
---|
64 | def set_slicer(self, type, params): |
---|
65 | """ |
---|
66 | Rebuild the panel |
---|
67 | """ |
---|
68 | self.bck.Clear(True) |
---|
69 | self.type = type |
---|
70 | if type is None: |
---|
71 | label = "Right-click on 2D plot for slicer options" |
---|
72 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
73 | self.bck.Add(title, (0, 0), (1, 2), |
---|
74 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
75 | else: |
---|
76 | title_text = str(type) + "Parameters" |
---|
77 | title = wx.StaticText(self, -1, title_text, |
---|
78 | style=wx.ALIGN_LEFT) |
---|
79 | self.bck.Add(title, (0, 0), (1, 2), |
---|
80 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
81 | n = 1 |
---|
82 | self.parameters = [] |
---|
83 | keys = params.keys() |
---|
84 | keys.sort() |
---|
85 | for item in keys: |
---|
86 | if not item.lower() in ["num_points", "avg", "avg_error", "sum", "sum_error"]: |
---|
87 | n += 1 |
---|
88 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
89 | self.bck.Add(text, (n - 1, 0), |
---|
90 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
91 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
92 | style=wx.TE_PROCESS_ENTER) |
---|
93 | hint_msg = "Modify the value of %s to change " % item |
---|
94 | hint_msg += "the 2D slicer" |
---|
95 | ctl.SetToolTipString(hint_msg) |
---|
96 | ctl.SetValue(str(format_number(params[item]))) |
---|
97 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
98 | ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
99 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
100 | self.parameters.append([item, ctl]) |
---|
101 | self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, border=0) |
---|
102 | for item in keys: |
---|
103 | if item.lower() in ["num_points", "avg", "avg_error", "sum", "sum_error"]: |
---|
104 | n += 1 |
---|
105 | text = wx.StaticText(self, -1, item + ": ", style=wx.ALIGN_LEFT) |
---|
106 | self.bck.Add(text, (n - 1, 0), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, |
---|
107 | border=15) |
---|
108 | ctl = wx.StaticText(self, -1, |
---|
109 | str(format_number(params[item])), |
---|
110 | style=wx.ALIGN_LEFT) |
---|
111 | ctl.SetToolTipString("Result %s" % item) |
---|
112 | self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, border=0) |
---|
113 | self.bck.Layout() |
---|
114 | self.Layout() |
---|
115 | psizer = self.parent.GetSizer() |
---|
116 | if psizer is not None: |
---|
117 | psizer.Layout() |
---|
118 | |
---|
119 | def onSetFocus(self, evt): |
---|
120 | """ |
---|
121 | Highlight the txtcrtl |
---|
122 | """ |
---|
123 | evt.Skip() |
---|
124 | # Get a handle to the TextCtrl |
---|
125 | widget = evt.GetEventObject() |
---|
126 | # Select the whole control, after this event resolves |
---|
127 | wx.CallAfter(widget.SetSelection, -1, -1) |
---|
128 | return |
---|
129 | |
---|
130 | def onParamChange(self, evt): |
---|
131 | """ |
---|
132 | Receive and event and reset the text field contained in self.parameters |
---|
133 | |
---|
134 | """ |
---|
135 | evt.Skip() |
---|
136 | for item in self.parameters: |
---|
137 | if item[0] in evt.params: |
---|
138 | item[1].SetValue(format_number(evt.params[item[0]])) |
---|
139 | item[1].Refresh() |
---|
140 | |
---|
141 | def onTextEnter(self, evt): |
---|
142 | """ |
---|
143 | Parameters have changed |
---|
144 | """ |
---|
145 | evt.Skip() |
---|
146 | params = {} |
---|
147 | has_error = False |
---|
148 | for item in self.parameters: |
---|
149 | try: |
---|
150 | params[item[0]] = float(item[1].GetValue()) |
---|
151 | item[1].SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
152 | item[1].Refresh() |
---|
153 | except: |
---|
154 | has_error = True |
---|
155 | item[1].SetBackgroundColour("pink") |
---|
156 | item[1].Refresh() |
---|
157 | |
---|
158 | if has_error == False: |
---|
159 | # Post parameter event |
---|
160 | ## base is guiframe is this case |
---|
161 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
162 | wx.PostEvent(self.base, event) |
---|
163 | |
---|
164 | def on_close(self, event): |
---|
165 | """ |
---|
166 | On Close Event |
---|
167 | """ |
---|
168 | ID = self.uid |
---|
169 | self.parent.delete_panel(ID) |
---|
170 | self.frame.Destroy() |
---|