1 | |
---|
2 | |
---|
3 | import wx |
---|
4 | import wx.lib.newevent |
---|
5 | #from copy import deepcopy |
---|
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 | |
---|
12 | |
---|
13 | class SlicerParameterPanel(wx.Dialog): |
---|
14 | """ |
---|
15 | Panel class to show the slicer parameters |
---|
16 | """ |
---|
17 | #TODO: show units |
---|
18 | #TODO: order parameters properly |
---|
19 | |
---|
20 | def __init__(self, parent, *args, **kwargs): |
---|
21 | """ |
---|
22 | Dialog window that allow to edit parameters slicer |
---|
23 | by entering new values |
---|
24 | """ |
---|
25 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
26 | self.params = {} |
---|
27 | self.parent = parent |
---|
28 | self.type = None |
---|
29 | self.listeners = [] |
---|
30 | self.parameters = [] |
---|
31 | self.bck = wx.GridBagSizer(5, 5) |
---|
32 | self.SetSizer(self.bck) |
---|
33 | label = "Right-click on 2D plot for slicer options" |
---|
34 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
35 | self.bck.Add(title, (0, 0), (1, 2), |
---|
36 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
37 | # Bindings |
---|
38 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
39 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
40 | |
---|
41 | def onEVT_SLICER(self, event): |
---|
42 | """ |
---|
43 | Process EVT_SLICER events |
---|
44 | When the slicer changes, update the panel |
---|
45 | |
---|
46 | :param event: EVT_SLICER event |
---|
47 | """ |
---|
48 | event.Skip() |
---|
49 | if event.obj_class is None: |
---|
50 | self.set_slicer(None, None) |
---|
51 | else: |
---|
52 | self.set_slicer(event.type, event.params) |
---|
53 | |
---|
54 | def set_slicer(self, type, params): |
---|
55 | """ |
---|
56 | Rebuild the panel |
---|
57 | """ |
---|
58 | self.bck.Clear(True) |
---|
59 | self.bck.Add((5, 5), (0, 0), (1, 1), |
---|
60 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
61 | self.type = type |
---|
62 | if type is None: |
---|
63 | label = "Right-click on 2D plot for slicer options" |
---|
64 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
65 | self.bck.Add(title, (1, 0), (1, 2), |
---|
66 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
67 | else: |
---|
68 | title = wx.StaticText(self, -1, |
---|
69 | "Slicer Parameters:", style=wx.ALIGN_LEFT) |
---|
70 | self.bck.Add(title, (1, 0), (1, 2), |
---|
71 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
72 | ix = 0 |
---|
73 | iy = 1 |
---|
74 | self.parameters = [] |
---|
75 | keys = params.keys() |
---|
76 | keys.sort() |
---|
77 | for item in keys: |
---|
78 | iy += 1 |
---|
79 | ix = 0 |
---|
80 | if not item in ["count", "errors"]: |
---|
81 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
82 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
83 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
84 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
85 | style=wx.TE_PROCESS_ENTER) |
---|
86 | hint_msg = "Modify the value of %s to change" % item |
---|
87 | hint_msg += " the 2D slicer" |
---|
88 | ctl.SetToolTipString(hint_msg) |
---|
89 | ix = 1 |
---|
90 | ctl.SetValue(format_number(str(params[item]))) |
---|
91 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
92 | self.parameters.append([item, ctl]) |
---|
93 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
94 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
95 | ix = 3 |
---|
96 | self.bck.Add((20, 20), (iy, ix), (1, 1), |
---|
97 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
98 | else: |
---|
99 | text = wx.StaticText(self, -1, item + " : ", |
---|
100 | style=wx.ALIGN_LEFT) |
---|
101 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
102 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
103 | ctl = wx.StaticText(self, -1, |
---|
104 | format_number(str(params[item])), |
---|
105 | style=wx.ALIGN_LEFT) |
---|
106 | ix = 1 |
---|
107 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
108 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
109 | ix = 0 |
---|
110 | iy += 1 |
---|
111 | |
---|
112 | # Change slicer within the window |
---|
113 | txt = "Slicer" |
---|
114 | text = wx.StaticText(self, -1, txt, style=wx.ALIGN_LEFT) |
---|
115 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
116 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
117 | type_list = ["SectorInteractor", "AnnulusInteractor", |
---|
118 | "BoxInteractorX", "BoxInteractorY"] |
---|
119 | self.type_select = wx.ComboBox(parent=self, choices=type_list) |
---|
120 | self.Bind(wx.EVT_COMBOBOX, self.onChangeSlicer) |
---|
121 | index = self.type_select.FindString(self.type) |
---|
122 | self.type_select.SetSelection(index) |
---|
123 | self.bck.Add(self.type_select, (iy, 1), (1, 1), |
---|
124 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
125 | |
---|
126 | # batch slicing parameters |
---|
127 | title_text = "Batch Slicing Options:" |
---|
128 | title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) |
---|
129 | iy += 1 |
---|
130 | ln = wx.StaticLine(self, -1, style=wx.LI_VERTICAL) |
---|
131 | ln.SetSize((60,60)) |
---|
132 | self.bck.Add(ln, (iy, ix), (1, 2), |
---|
133 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
134 | iy += 1 |
---|
135 | self.bck.Add(title, (iy, ix), (1, 1), |
---|
136 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
137 | iy += 1 |
---|
138 | id = wx.NewId() |
---|
139 | main_window = self.parent.parent |
---|
140 | self.loaded_data = main_window._data_manager.data_name_dict |
---|
141 | # TODO: remove 1D data from choices |
---|
142 | # TODO: auto check current data set |
---|
143 | choices = self.loaded_data.keys() |
---|
144 | self.data_list = wx.CheckListBox(parent=self, id=id, |
---|
145 | choices=choices, |
---|
146 | name="Apply Slicer to Data Sets:") |
---|
147 | self.bck.Add(self.data_list, (iy, ix), (1, 1), |
---|
148 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
149 | iy += 1 |
---|
150 | button_label = "Apply Slicer to Selected Files" |
---|
151 | self.batch_slicer_button = wx.Button(parent=self, |
---|
152 | label=button_label) |
---|
153 | self.Bind(wx.EVT_BUTTON, self.onBatchSlice) |
---|
154 | self.bck.Add(self.batch_slicer_button, (iy, ix), (1, 1), |
---|
155 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
156 | # TODO: Check box for saving file |
---|
157 | # TODO: append to file information and file type |
---|
158 | # TODO: Send to fitting options |
---|
159 | |
---|
160 | iy += 1 |
---|
161 | self.bck.Add((5, 5), (iy, ix), (1, 1), |
---|
162 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
163 | self.bck.Layout() |
---|
164 | self.bck.Fit(self) |
---|
165 | self.parent.GetSizer().Layout() |
---|
166 | |
---|
167 | def onParamChange(self, evt): |
---|
168 | """ |
---|
169 | receive an event end reset value text fields |
---|
170 | inside self.parameters |
---|
171 | """ |
---|
172 | evt.Skip() |
---|
173 | if evt.type == "UPDATE": |
---|
174 | for item in self.parameters: |
---|
175 | if item[0] in evt.params: |
---|
176 | item[1].SetValue("%-5.3g" % evt.params[item[0]]) |
---|
177 | item[1].Refresh() |
---|
178 | |
---|
179 | def onTextEnter(self, evt): |
---|
180 | """ |
---|
181 | Parameters have changed |
---|
182 | """ |
---|
183 | params = {} |
---|
184 | has_error = False |
---|
185 | for item in self.parameters: |
---|
186 | try: |
---|
187 | params[item[0]] = float(item[1].GetValue()) |
---|
188 | item[1].SetBackgroundColour( |
---|
189 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
190 | item[1].Refresh() |
---|
191 | except: |
---|
192 | has_error = True |
---|
193 | item[1].SetBackgroundColour("pink") |
---|
194 | item[1].Refresh() |
---|
195 | |
---|
196 | if not has_error: |
---|
197 | # Post parameter event |
---|
198 | # parent here is plotter2D |
---|
199 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
200 | wx.PostEvent(self.parent, event) |
---|
201 | |
---|
202 | def onBatchSlice(self, evt=None): |
---|
203 | """ |
---|
204 | Batch slicing button is pushed |
---|
205 | :param evt: Event triggering hide/show of the batch slicer parameters |
---|
206 | """ |
---|
207 | # Process each data file individually |
---|
208 | for item in self.data_list.CheckedStrings: |
---|
209 | # Get data_id |
---|
210 | num = len(item) |
---|
211 | spp = self.parent.parent |
---|
212 | data_panel = spp._data_panel |
---|
213 | data_list = data_panel.list_cb_data |
---|
214 | for key in data_list: |
---|
215 | loaded_key = (key[:num]) if len(key) > num else key |
---|
216 | if loaded_key == item: |
---|
217 | selection = key |
---|
218 | break |
---|
219 | # Check the data checkbox |
---|
220 | data_ctrl = data_list[selection][0] |
---|
221 | self.check_item_and_children(data_ctrl=data_ctrl, check_value=True) |
---|
222 | # Plot all checked data |
---|
223 | data_panel.on_plot() |
---|
224 | panel = plot_mgr = None |
---|
225 | plot_mgrs = spp.plot_panels |
---|
226 | for key, mgr in plot_mgrs.iteritems(): |
---|
227 | if isinstance(mgr, ModelPanel2D): |
---|
228 | plot_mgr = mgr |
---|
229 | break |
---|
230 | if plot_mgr is not None: |
---|
231 | # TODO: find proper plot window |
---|
232 | panels = plot_mgr._manager.plot_panels |
---|
233 | for _, pane in panels.iteritems(): |
---|
234 | if pane.window_name == item: |
---|
235 | panel = pane |
---|
236 | # TODO: apply slicer |
---|
237 | print(panels) |
---|
238 | if panel is not None: |
---|
239 | self._apply_slicer_to_plot(panel) |
---|
240 | # TODO: save file (if desired) |
---|
241 | # TODO: send to fitting (if desired) |
---|
242 | |
---|
243 | def onChangeSlicer(self, evt): |
---|
244 | """ |
---|
245 | Event driven slicer change when self.type_select changes |
---|
246 | :param evt: Event triggering this change |
---|
247 | """ |
---|
248 | self._apply_slicer_to_plot(self.parent) |
---|
249 | |
---|
250 | def _apply_slicer_to_plot(self, plot): |
---|
251 | """ |
---|
252 | Apply a slicer to *any* plot window, not just parent window |
---|
253 | :param plot: |
---|
254 | :return: |
---|
255 | """ |
---|
256 | type = self.type_select.GetStringSelection() |
---|
257 | if self.type != type: |
---|
258 | if type == "SectorInteractor": |
---|
259 | plot.onSectorQ(None) |
---|
260 | elif type == "AnnulusInteractor": |
---|
261 | plot.onSectorPhi(None) |
---|
262 | elif type == "BoxInteractorX": |
---|
263 | plot.onBoxavgX(None) |
---|
264 | elif type == "BoxInteractorY": |
---|
265 | plot.onBoxavgY(None) |
---|
266 | |
---|
267 | def check_item_and_children(self, data_ctrl, check_value=True): |
---|
268 | self.parent.parent._data_panel.tree_ctrl.CheckItem(data_ctrl, check_value) |
---|
269 | if data_ctrl.HasChildren(): |
---|
270 | if check_value and not data_ctrl.IsExpanded(): |
---|
271 | # Only select children if control is expanded |
---|
272 | # Always deselect children, regardless (see ticket #259) |
---|
273 | return |
---|
274 | for child_ctrl in data_ctrl.GetChildren(): |
---|
275 | self.tree_ctrl.CheckItem(child_ctrl, check_value) |
---|