[d955bf19] | 1 | |
---|
[b06ef8c] | 2 | |
---|
| 3 | import wx |
---|
| 4 | import wx.lib.newevent |
---|
[32c0841] | 5 | #from copy import deepcopy |
---|
[d85c194] | 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 |
---|
[39f0bf4] | 9 | from sas.sasgui.guiframe.events import SlicerParameterEvent, SlicerEvent |
---|
[df27aa8] | 10 | from Plotter2D import ModelPanel2D |
---|
[32c0841] | 11 | |
---|
[0d9dae8] | 12 | |
---|
[ef0c170] | 13 | class SlicerParameterPanel(wx.Dialog): |
---|
[d955bf19] | 14 | """ |
---|
[b40ad40] | 15 | Panel class to show the slicer parameters |
---|
[d955bf19] | 16 | """ |
---|
[b06ef8c] | 17 | #TODO: show units |
---|
| 18 | #TODO: order parameters properly |
---|
[b40ad40] | 19 | |
---|
[cd84dca] | 20 | def __init__(self, parent, *args, **kwargs): |
---|
[12aa9b5] | 21 | """ |
---|
[b40ad40] | 22 | Dialog window that allow to edit parameters slicer |
---|
[d955bf19] | 23 | by entering new values |
---|
[12aa9b5] | 24 | """ |
---|
[b40ad40] | 25 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
[b06ef8c] | 26 | self.params = {} |
---|
| 27 | self.parent = parent |
---|
| 28 | self.type = None |
---|
| 29 | self.listeners = [] |
---|
| 30 | self.parameters = [] |
---|
[32c0841] | 31 | self.bck = wx.GridBagSizer(5, 5) |
---|
[b06ef8c] | 32 | self.SetSizer(self.bck) |
---|
[32c0841] | 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), |
---|
[b40ad40] | 36 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[b06ef8c] | 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 | """ |
---|
[d955bf19] | 43 | Process EVT_SLICER events |
---|
| 44 | When the slicer changes, update the panel |
---|
[b40ad40] | 45 | |
---|
[d955bf19] | 46 | :param event: EVT_SLICER event |
---|
[b06ef8c] | 47 | """ |
---|
| 48 | event.Skip() |
---|
[e075203] | 49 | if event.obj_class is None: |
---|
[b06ef8c] | 50 | self.set_slicer(None, None) |
---|
| 51 | else: |
---|
| 52 | self.set_slicer(event.type, event.params) |
---|
[b40ad40] | 53 | |
---|
[b06ef8c] | 54 | def set_slicer(self, type, params): |
---|
| 55 | """ |
---|
[d955bf19] | 56 | Rebuild the panel |
---|
[b06ef8c] | 57 | """ |
---|
[b40ad40] | 58 | self.bck.Clear(True) |
---|
[39f0bf4] | 59 | self.bck.Add((5, 5), (0, 0), (1, 1), |
---|
| 60 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
[b40ad40] | 61 | self.type = type |
---|
[e075203] | 62 | if type is None: |
---|
[32c0841] | 63 | label = "Right-click on 2D plot for slicer options" |
---|
| 64 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
[39f0bf4] | 65 | self.bck.Add(title, (1, 0), (1, 2), |
---|
[b40ad40] | 66 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[b06ef8c] | 67 | else: |
---|
[b40ad40] | 68 | title = wx.StaticText(self, -1, |
---|
[f6bb24d] | 69 | "Slicer Parameters:", style=wx.ALIGN_LEFT) |
---|
[39f0bf4] | 70 | self.bck.Add(title, (1, 0), (1, 2), |
---|
[b40ad40] | 71 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[ef0c170] | 72 | ix = 0 |
---|
[39f0bf4] | 73 | iy = 1 |
---|
[b06ef8c] | 74 | self.parameters = [] |
---|
| 75 | keys = params.keys() |
---|
| 76 | keys.sort() |
---|
| 77 | for item in keys: |
---|
[ef0c170] | 78 | iy += 1 |
---|
| 79 | ix = 0 |
---|
[32c0841] | 80 | if not item in ["count", "errors"]: |
---|
[0f6d05f8] | 81 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 82 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
| 83 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[32c0841] | 84 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
| 85 | style=wx.TE_PROCESS_ENTER) |
---|
[88989768] | 86 | hint_msg = "Modify the value of %s to change" % item |
---|
| 87 | hint_msg += " the 2D slicer" |
---|
[32c0841] | 88 | ctl.SetToolTipString(hint_msg) |
---|
[0f6d05f8] | 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]) |
---|
[b40ad40] | 93 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
| 94 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[32c0841] | 95 | ix = 3 |
---|
[b40ad40] | 96 | self.bck.Add((20, 20), (iy, ix), (1, 1), |
---|
| 97 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[0f6d05f8] | 98 | else: |
---|
[b40ad40] | 99 | text = wx.StaticText(self, -1, item + " : ", |
---|
[32c0841] | 100 | style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 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) |
---|
[32c0841] | 106 | ix = 1 |
---|
[b40ad40] | 107 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
| 108 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[39f0bf4] | 109 | ix = 0 |
---|
[32c0841] | 110 | iy += 1 |
---|
[39f0bf4] | 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), |
---|
[b40ad40] | 116 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[39f0bf4] | 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 |
---|
[f6bb24d] | 127 | title_text = "Batch Slicing Options:" |
---|
| 128 | title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) |
---|
[39f0bf4] | 129 | iy += 1 |
---|
[f6bb24d] | 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) |
---|
[39f0bf4] | 137 | iy += 1 |
---|
[f6bb24d] | 138 | id = wx.NewId() |
---|
[24fc06a] | 139 | main_window = self.parent.parent |
---|
| 140 | self.loaded_data = main_window._data_manager.data_name_dict |
---|
[df27aa8] | 141 | # TODO: remove 1D data from choices |
---|
| 142 | # TODO: auto check current data set |
---|
[24fc06a] | 143 | choices = self.loaded_data.keys() |
---|
[f6bb24d] | 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), |
---|
[39f0bf4] | 148 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 149 | iy += 1 |
---|
[f6bb24d] | 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 |
---|
[39f0bf4] | 161 | self.bck.Add((5, 5), (iy, ix), (1, 1), |
---|
| 162 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
[b06ef8c] | 163 | self.bck.Layout() |
---|
| 164 | self.bck.Fit(self) |
---|
| 165 | self.parent.GetSizer().Layout() |
---|
| 166 | |
---|
| 167 | def onParamChange(self, evt): |
---|
[12aa9b5] | 168 | """ |
---|
[d955bf19] | 169 | receive an event end reset value text fields |
---|
| 170 | inside self.parameters |
---|
[12aa9b5] | 171 | """ |
---|
[b06ef8c] | 172 | evt.Skip() |
---|
| 173 | if evt.type == "UPDATE": |
---|
[b40ad40] | 174 | for item in self.parameters: |
---|
[b06ef8c] | 175 | if item[0] in evt.params: |
---|
[32c0841] | 176 | item[1].SetValue("%-5.3g" % evt.params[item[0]]) |
---|
[b06ef8c] | 177 | item[1].Refresh() |
---|
[b40ad40] | 178 | |
---|
| 179 | def onTextEnter(self, evt): |
---|
[b06ef8c] | 180 | """ |
---|
[d955bf19] | 181 | Parameters have changed |
---|
[b40ad40] | 182 | """ |
---|
[b06ef8c] | 183 | params = {} |
---|
| 184 | has_error = False |
---|
| 185 | for item in self.parameters: |
---|
| 186 | try: |
---|
| 187 | params[item[0]] = float(item[1].GetValue()) |
---|
[e075203] | 188 | item[1].SetBackgroundColour( |
---|
| 189 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
[b06ef8c] | 190 | item[1].Refresh() |
---|
| 191 | except: |
---|
| 192 | has_error = True |
---|
| 193 | item[1].SetBackgroundColour("pink") |
---|
| 194 | item[1].Refresh() |
---|
| 195 | |
---|
[e075203] | 196 | if not has_error: |
---|
[b06ef8c] | 197 | # Post parameter event |
---|
[e075203] | 198 | # parent here is plotter2D |
---|
[b06ef8c] | 199 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
| 200 | wx.PostEvent(self.parent, event) |
---|
[39f0bf4] | 201 | |
---|
[f6bb24d] | 202 | def onBatchSlice(self, evt=None): |
---|
[39f0bf4] | 203 | """ |
---|
[f6bb24d] | 204 | Batch slicing button is pushed |
---|
[39f0bf4] | 205 | :param evt: Event triggering hide/show of the batch slicer parameters |
---|
| 206 | """ |
---|
[24fc06a] | 207 | # Process each data file individually |
---|
| 208 | for item in self.data_list.CheckedStrings: |
---|
| 209 | # Get data_id |
---|
[df27aa8] | 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) |
---|
[f6bb24d] | 240 | # TODO: save file (if desired) |
---|
| 241 | # TODO: send to fitting (if desired) |
---|
[39f0bf4] | 242 | |
---|
| 243 | def onChangeSlicer(self, evt): |
---|
| 244 | """ |
---|
[df27aa8] | 245 | Event driven slicer change when self.type_select changes |
---|
[39f0bf4] | 246 | :param evt: Event triggering this change |
---|
| 247 | """ |
---|
[df27aa8] | 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 | """ |
---|
[39f0bf4] | 256 | type = self.type_select.GetStringSelection() |
---|
| 257 | if self.type != type: |
---|
| 258 | if type == "SectorInteractor": |
---|
[df27aa8] | 259 | plot.onSectorQ(None) |
---|
[39f0bf4] | 260 | elif type == "AnnulusInteractor": |
---|
[df27aa8] | 261 | plot.onSectorPhi(None) |
---|
[39f0bf4] | 262 | elif type == "BoxInteractorX": |
---|
[df27aa8] | 263 | plot.onBoxavgX(None) |
---|
[39f0bf4] | 264 | elif type == "BoxInteractorY": |
---|
[df27aa8] | 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) |
---|