1 | |
---|
2 | |
---|
3 | import wx |
---|
4 | import wx.lib.newevent |
---|
5 | import time |
---|
6 | from sas.sasgui.guiframe.events import EVT_SLICER_PARS |
---|
7 | from sas.sasgui.guiframe.utils import format_number |
---|
8 | from sas.sasgui.guiframe.events import EVT_SLICER |
---|
9 | from sas.sasgui.guiframe.events import SlicerParameterEvent, SlicerEvent |
---|
10 | from Plotter2D import ModelPanel2D |
---|
11 | from sas.sascalc.dataloader.data_info import Data1D, Data2D |
---|
12 | ApplyParams, EVT_APPLY_PARAMS = wx.lib.newevent.NewEvent() |
---|
13 | |
---|
14 | |
---|
15 | class SlicerParameterPanel(wx.Dialog): |
---|
16 | """ |
---|
17 | Panel class to show the slicer parameters |
---|
18 | """ |
---|
19 | # TODO: show units |
---|
20 | # TODO: order parameters properly |
---|
21 | |
---|
22 | def __init__(self, parent, *args, **kwargs): |
---|
23 | """ |
---|
24 | Dialog window that allow to edit parameters slicer |
---|
25 | by entering new values |
---|
26 | """ |
---|
27 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
28 | self.params = {} |
---|
29 | self.parent = parent |
---|
30 | self.type = None |
---|
31 | self.listeners = [] |
---|
32 | self.parameters = [] |
---|
33 | self.bck = wx.GridBagSizer(5, 5) |
---|
34 | self.SetSizer(self.bck) |
---|
35 | label = "Right-click on 2D plot for slicer options" |
---|
36 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
37 | self.bck.Add(title, (0, 0), (1, 2), |
---|
38 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
39 | # Bindings |
---|
40 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
41 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
42 | self.Bind(EVT_APPLY_PARAMS, self.apply_params_list) |
---|
43 | |
---|
44 | def onEVT_SLICER(self, event): |
---|
45 | """ |
---|
46 | Process EVT_SLICER events |
---|
47 | When the slicer changes, update the panel |
---|
48 | |
---|
49 | :param event: EVT_SLICER event |
---|
50 | """ |
---|
51 | event.Skip() |
---|
52 | if event.obj_class is None: |
---|
53 | self.set_slicer(None, None) |
---|
54 | else: |
---|
55 | self.set_slicer(event.type, event.params) |
---|
56 | |
---|
57 | def set_slicer(self, type, params): |
---|
58 | """ |
---|
59 | Rebuild the panel |
---|
60 | """ |
---|
61 | self.bck.Clear(True) |
---|
62 | self.bck.Add((5, 5), (0, 0), (1, 1), |
---|
63 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
64 | self.type = type |
---|
65 | if type is None: |
---|
66 | label = "Right-click on 2D plot for slicer options" |
---|
67 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
68 | self.bck.Add(title, (1, 0), (1, 2), |
---|
69 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
70 | else: |
---|
71 | title = wx.StaticText(self, -1, |
---|
72 | "Slicer Parameters:", style=wx.ALIGN_LEFT) |
---|
73 | self.bck.Add(title, (1, 0), (1, 2), |
---|
74 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
75 | iy = 1 |
---|
76 | self.parameters = [] |
---|
77 | keys = params.keys() |
---|
78 | keys.sort() |
---|
79 | for item in keys: |
---|
80 | iy += 1 |
---|
81 | ix = 0 |
---|
82 | if not item in ["count", "errors"]: |
---|
83 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
84 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
85 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
86 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
87 | style=wx.TE_PROCESS_ENTER) |
---|
88 | hint_msg = "Modify the value of %s to change" % item |
---|
89 | hint_msg += " the 2D slicer" |
---|
90 | ctl.SetToolTipString(hint_msg) |
---|
91 | ix = 1 |
---|
92 | ctl.SetValue(format_number(str(params[item]))) |
---|
93 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
94 | self.parameters.append([item, ctl]) |
---|
95 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
96 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
97 | ix = 3 |
---|
98 | self.bck.Add((20, 20), (iy, ix), (1, 1), |
---|
99 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
100 | else: |
---|
101 | text = wx.StaticText(self, -1, item + " : ", |
---|
102 | style=wx.ALIGN_LEFT) |
---|
103 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
104 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
105 | ctl = wx.StaticText(self, -1, |
---|
106 | format_number(str(params[item])), |
---|
107 | style=wx.ALIGN_LEFT) |
---|
108 | ix = 1 |
---|
109 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
110 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
111 | ix = 0 |
---|
112 | iy += 1 |
---|
113 | |
---|
114 | # Change slicer within the window |
---|
115 | txt = "Slicer" |
---|
116 | text = wx.StaticText(self, -1, txt, style=wx.ALIGN_LEFT) |
---|
117 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
118 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
119 | type_list = ["SectorInteractor", "AnnulusInteractor", |
---|
120 | "BoxInteractorX", "BoxInteractorY"] |
---|
121 | self.type_select = wx.ComboBox(parent=self, choices=type_list) |
---|
122 | self.Bind(wx.EVT_COMBOBOX, self.onChangeSlicer) |
---|
123 | index = self.type_select.FindString(self.type) |
---|
124 | self.type_select.SetSelection(index) |
---|
125 | self.bck.Add(self.type_select, (iy, 1), (1, 1), |
---|
126 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
127 | |
---|
128 | # batch slicing parameters |
---|
129 | title_text = "Batch Slicing Options:" |
---|
130 | title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) |
---|
131 | iy += 1 |
---|
132 | ln = wx.StaticLine(self, -1, style=wx.LI_VERTICAL) |
---|
133 | ln.SetSize((60,60)) |
---|
134 | self.bck.Add(ln, (iy, ix), (1, 2), |
---|
135 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
136 | iy += 1 |
---|
137 | self.bck.Add(title, (iy, ix), (1, 1), |
---|
138 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
139 | iy += 1 |
---|
140 | # Create a list box with all of the 2D plots |
---|
141 | self.process_list() |
---|
142 | self.bck.Add(self.data_list, (iy, ix), (1, 1), |
---|
143 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
144 | iy += 1 |
---|
145 | # Button to start batch slicing |
---|
146 | button_label = "Apply Slicer to Selected Plots" |
---|
147 | self.batch_slicer_button = wx.Button(parent=self, |
---|
148 | label=button_label) |
---|
149 | self.Bind(wx.EVT_BUTTON, self.onBatchSlice) |
---|
150 | self.bck.Add(self.batch_slicer_button, (iy, ix), (1, 1), |
---|
151 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
152 | # TODO: Check box for saving file |
---|
153 | # TODO: append to file information and file type |
---|
154 | # TODO: Send to fitting options |
---|
155 | |
---|
156 | iy += 1 |
---|
157 | self.bck.Add((5, 5), (iy, ix), (1, 1), |
---|
158 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
159 | self.bck.Layout() |
---|
160 | self.bck.Fit(self) |
---|
161 | self.parent.GetSizer().Layout() |
---|
162 | |
---|
163 | def onParamChange(self, evt): |
---|
164 | """ |
---|
165 | receive an event end reset value text fields |
---|
166 | inside self.parameters |
---|
167 | """ |
---|
168 | evt.Skip() |
---|
169 | if evt.type == "UPDATE": |
---|
170 | for item in self.parameters: |
---|
171 | if item[0] in evt.params: |
---|
172 | item[1].SetValue("%-5.3g" % evt.params[item[0]]) |
---|
173 | item[1].Refresh() |
---|
174 | |
---|
175 | def onTextEnter(self, evt): |
---|
176 | """ |
---|
177 | Parameters have changed |
---|
178 | """ |
---|
179 | params = {} |
---|
180 | has_error = False |
---|
181 | for item in self.parameters: |
---|
182 | try: |
---|
183 | params[item[0]] = float(item[1].GetValue()) |
---|
184 | item[1].SetBackgroundColour( |
---|
185 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
186 | item[1].Refresh() |
---|
187 | except: |
---|
188 | has_error = True |
---|
189 | item[1].SetBackgroundColour("pink") |
---|
190 | item[1].Refresh() |
---|
191 | |
---|
192 | if not has_error: |
---|
193 | # Post parameter event |
---|
194 | # parent here is plotter2D |
---|
195 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
196 | wx.PostEvent(self.parent, event) |
---|
197 | |
---|
198 | def onBatchSlice(self, evt=None): |
---|
199 | """ |
---|
200 | Method invoked with batch slicing button is pressed |
---|
201 | :param evt: Event triggering hide/show of the batch slicer parameters |
---|
202 | """ |
---|
203 | apply_to_list = [] |
---|
204 | spp = self.parent.parent |
---|
205 | params = self.parent.slicer.get_params() |
---|
206 | type = self.type_select.GetStringSelection() |
---|
207 | |
---|
208 | # Find desired 2D data panels |
---|
209 | for key, mgr in spp.plot_panels.iteritems(): |
---|
210 | if mgr.graph.prop['title'] in self.data_list.CheckedStrings: |
---|
211 | apply_to_list.append(mgr) |
---|
212 | |
---|
213 | # Apply slicer type to selected panels |
---|
214 | for item in apply_to_list: |
---|
215 | self._apply_slicer_to_plot(item, type) |
---|
216 | |
---|
217 | # Post an event to apply appropriate slicer params to each slicer |
---|
218 | # Event needed due to how apply_slicer_to_plot works |
---|
219 | event = ApplyParams(params=params, plot_list=apply_to_list) |
---|
220 | wx.PostEvent(self, event) |
---|
221 | # TODO: save file (if desired) |
---|
222 | # TODO: send to fitting (if desired) |
---|
223 | |
---|
224 | def onChangeSlicer(self, evt): |
---|
225 | """ |
---|
226 | Event driven slicer change when self.type_select changes |
---|
227 | :param evt: Event triggering this change |
---|
228 | """ |
---|
229 | self._apply_slicer_to_plot(self.parent) |
---|
230 | |
---|
231 | def _apply_slicer_to_plot(self, plot, type=None): |
---|
232 | """ |
---|
233 | Apply a slicer to *any* plot window, not just parent window |
---|
234 | :param plot: 2D plot panel to apply a slicer to |
---|
235 | :param type: The type of slicer to apply to the panel |
---|
236 | """ |
---|
237 | if type is None: |
---|
238 | type = self.type_select.GetStringSelection() |
---|
239 | if type == "SectorInteractor": |
---|
240 | plot.onSectorQ(None) |
---|
241 | elif type == "AnnulusInteractor": |
---|
242 | plot.onSectorPhi(None) |
---|
243 | elif type == "BoxInteractorX": |
---|
244 | plot.onBoxavgX(None) |
---|
245 | elif type == "BoxInteractorY": |
---|
246 | plot.onBoxavgY(None) |
---|
247 | |
---|
248 | def process_list(self): |
---|
249 | """ |
---|
250 | Populate the check list from the currently plotted 2D data |
---|
251 | """ |
---|
252 | self.checkme = None |
---|
253 | main_window = self.parent.parent |
---|
254 | self.loaded_data = [] |
---|
255 | id = wx.NewId() |
---|
256 | # Iterate over the loaded plots and find all 2D panels |
---|
257 | for key, value in main_window.plot_panels.iteritems(): |
---|
258 | if isinstance(value, ModelPanel2D): |
---|
259 | self.loaded_data.append(value.data2D.name) |
---|
260 | if value.data2D.id == self.parent.data2D.id: |
---|
261 | # Set current plot panel as uncheckable |
---|
262 | self.checkme = self.loaded_data.index(value.data2D.name) |
---|
263 | self.data_list = wx.CheckListBox(parent=self, id=id, |
---|
264 | choices=self.loaded_data, |
---|
265 | name="Apply Slicer to 2D Plots:") |
---|
266 | # Check all items bty default |
---|
267 | for item in range(len(self.data_list.Items)): |
---|
268 | self.data_list.Check(item) |
---|
269 | self.data_list.Bind(wx.EVT_CHECKLISTBOX, self.onCheckBoxList) |
---|
270 | |
---|
271 | def onCheckBoxList(self, e): |
---|
272 | """ |
---|
273 | Prevent a checkbox item from being unchecked |
---|
274 | :param e: Event triggered when a checkbox list item is checked |
---|
275 | """ |
---|
276 | index = e.GetSelection() |
---|
277 | if index == self.checkme: |
---|
278 | self.data_list.Check(index) |
---|
279 | |
---|
280 | def apply_params_list(self, evt=None): |
---|
281 | """ |
---|
282 | Event based parameter setting. |
---|
283 | :param evt: Event triggered to apply parameters to a list of plots |
---|
284 | evt should have attrs plot_list and params |
---|
285 | """ |
---|
286 | # Apply parameter list to each plot as desired |
---|
287 | for item in evt.plot_list: |
---|
288 | item.slicer.set_params(evt.params) |
---|
289 | item.slicer.base.update() |
---|
290 | # Close the slicer window |
---|
291 | self.Destroy() |
---|