[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 |
---|
[32c0841] | 10 | |
---|
[0d9dae8] | 11 | |
---|
[ef0c170] | 12 | class SlicerParameterPanel(wx.Dialog): |
---|
[d955bf19] | 13 | """ |
---|
[b40ad40] | 14 | Panel class to show the slicer parameters |
---|
[d955bf19] | 15 | """ |
---|
[b06ef8c] | 16 | #TODO: show units |
---|
| 17 | #TODO: order parameters properly |
---|
[b40ad40] | 18 | |
---|
[cd84dca] | 19 | def __init__(self, parent, *args, **kwargs): |
---|
[12aa9b5] | 20 | """ |
---|
[b40ad40] | 21 | Dialog window that allow to edit parameters slicer |
---|
[d955bf19] | 22 | by entering new values |
---|
[12aa9b5] | 23 | """ |
---|
[b40ad40] | 24 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
[b06ef8c] | 25 | self.params = {} |
---|
| 26 | self.parent = parent |
---|
| 27 | self.type = None |
---|
| 28 | self.listeners = [] |
---|
| 29 | self.parameters = [] |
---|
[32c0841] | 30 | self.bck = wx.GridBagSizer(5, 5) |
---|
[b06ef8c] | 31 | self.SetSizer(self.bck) |
---|
[32c0841] | 32 | label = "Right-click on 2D plot for slicer options" |
---|
| 33 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
| 34 | self.bck.Add(title, (0, 0), (1, 2), |
---|
[b40ad40] | 35 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[b06ef8c] | 36 | # Bindings |
---|
| 37 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 38 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 39 | |
---|
| 40 | def onEVT_SLICER(self, event): |
---|
| 41 | """ |
---|
[d955bf19] | 42 | Process EVT_SLICER events |
---|
| 43 | When the slicer changes, update the panel |
---|
[b40ad40] | 44 | |
---|
[d955bf19] | 45 | :param event: EVT_SLICER event |
---|
[b06ef8c] | 46 | """ |
---|
| 47 | event.Skip() |
---|
[e075203] | 48 | if event.obj_class is None: |
---|
[b06ef8c] | 49 | self.set_slicer(None, None) |
---|
| 50 | else: |
---|
| 51 | self.set_slicer(event.type, event.params) |
---|
[b40ad40] | 52 | |
---|
[b06ef8c] | 53 | def set_slicer(self, type, params): |
---|
| 54 | """ |
---|
[d955bf19] | 55 | Rebuild the panel |
---|
[b06ef8c] | 56 | """ |
---|
[b40ad40] | 57 | self.bck.Clear(True) |
---|
[39f0bf4] | 58 | self.bck.Add((5, 5), (0, 0), (1, 1), |
---|
| 59 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
[b40ad40] | 60 | self.type = type |
---|
[e075203] | 61 | if type is None: |
---|
[32c0841] | 62 | label = "Right-click on 2D plot for slicer options" |
---|
| 63 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
[39f0bf4] | 64 | self.bck.Add(title, (1, 0), (1, 2), |
---|
[b40ad40] | 65 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[b06ef8c] | 66 | else: |
---|
[b40ad40] | 67 | title = wx.StaticText(self, -1, |
---|
[f6bb24d] | 68 | "Slicer Parameters:", style=wx.ALIGN_LEFT) |
---|
[39f0bf4] | 69 | self.bck.Add(title, (1, 0), (1, 2), |
---|
[b40ad40] | 70 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[ef0c170] | 71 | ix = 0 |
---|
[39f0bf4] | 72 | iy = 1 |
---|
[b06ef8c] | 73 | self.parameters = [] |
---|
| 74 | keys = params.keys() |
---|
| 75 | keys.sort() |
---|
| 76 | for item in keys: |
---|
[ef0c170] | 77 | iy += 1 |
---|
| 78 | ix = 0 |
---|
[32c0841] | 79 | if not item in ["count", "errors"]: |
---|
[0f6d05f8] | 80 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 81 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
| 82 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[32c0841] | 83 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
| 84 | style=wx.TE_PROCESS_ENTER) |
---|
[88989768] | 85 | hint_msg = "Modify the value of %s to change" % item |
---|
| 86 | hint_msg += " the 2D slicer" |
---|
[32c0841] | 87 | ctl.SetToolTipString(hint_msg) |
---|
[0f6d05f8] | 88 | ix = 1 |
---|
| 89 | ctl.SetValue(format_number(str(params[item]))) |
---|
| 90 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
| 91 | self.parameters.append([item, ctl]) |
---|
[b40ad40] | 92 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
| 93 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[32c0841] | 94 | ix = 3 |
---|
[b40ad40] | 95 | self.bck.Add((20, 20), (iy, ix), (1, 1), |
---|
| 96 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[0f6d05f8] | 97 | else: |
---|
[b40ad40] | 98 | text = wx.StaticText(self, -1, item + " : ", |
---|
[32c0841] | 99 | style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 100 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
| 101 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 102 | ctl = wx.StaticText(self, -1, |
---|
| 103 | format_number(str(params[item])), |
---|
| 104 | style=wx.ALIGN_LEFT) |
---|
[32c0841] | 105 | ix = 1 |
---|
[b40ad40] | 106 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
| 107 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[39f0bf4] | 108 | ix = 0 |
---|
[32c0841] | 109 | iy += 1 |
---|
[39f0bf4] | 110 | |
---|
| 111 | # Change slicer within the window |
---|
| 112 | txt = "Slicer" |
---|
| 113 | text = wx.StaticText(self, -1, txt, style=wx.ALIGN_LEFT) |
---|
| 114 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
[b40ad40] | 115 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[39f0bf4] | 116 | type_list = ["SectorInteractor", "AnnulusInteractor", |
---|
| 117 | "BoxInteractorX", "BoxInteractorY"] |
---|
| 118 | self.type_select = wx.ComboBox(parent=self, choices=type_list) |
---|
| 119 | self.Bind(wx.EVT_COMBOBOX, self.onChangeSlicer) |
---|
| 120 | index = self.type_select.FindString(self.type) |
---|
| 121 | self.type_select.SetSelection(index) |
---|
| 122 | self.bck.Add(self.type_select, (iy, 1), (1, 1), |
---|
| 123 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 124 | |
---|
| 125 | # batch slicing parameters |
---|
[f6bb24d] | 126 | title_text = "Batch Slicing Options:" |
---|
| 127 | title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) |
---|
[39f0bf4] | 128 | iy += 1 |
---|
[f6bb24d] | 129 | ln = wx.StaticLine(self, -1, style=wx.LI_VERTICAL) |
---|
| 130 | ln.SetSize((60,60)) |
---|
| 131 | self.bck.Add(ln, (iy, ix), (1, 2), |
---|
| 132 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 133 | iy += 1 |
---|
| 134 | self.bck.Add(title, (iy, ix), (1, 1), |
---|
| 135 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[39f0bf4] | 136 | iy += 1 |
---|
[f6bb24d] | 137 | id = wx.NewId() |
---|
[24fc06a] | 138 | main_window = self.parent.parent |
---|
| 139 | self.loaded_data = main_window._data_manager.data_name_dict |
---|
| 140 | choices = self.loaded_data.keys() |
---|
[f6bb24d] | 141 | self.data_list = wx.CheckListBox(parent=self, id=id, |
---|
| 142 | choices=choices, |
---|
| 143 | name="Apply Slicer to Data Sets:") |
---|
| 144 | self.bck.Add(self.data_list, (iy, ix), (1, 1), |
---|
[39f0bf4] | 145 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 146 | iy += 1 |
---|
[f6bb24d] | 147 | button_label = "Apply Slicer to Selected Files" |
---|
| 148 | self.batch_slicer_button = wx.Button(parent=self, |
---|
| 149 | label=button_label) |
---|
| 150 | self.Bind(wx.EVT_BUTTON, self.onBatchSlice) |
---|
| 151 | self.bck.Add(self.batch_slicer_button, (iy, ix), (1, 1), |
---|
| 152 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 153 | # TODO: Check box for saving file |
---|
| 154 | # TODO: append to file information and file type |
---|
| 155 | # TODO: Send to fitting options |
---|
| 156 | |
---|
| 157 | iy += 1 |
---|
[39f0bf4] | 158 | self.bck.Add((5, 5), (iy, ix), (1, 1), |
---|
| 159 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
[b06ef8c] | 160 | self.bck.Layout() |
---|
| 161 | self.bck.Fit(self) |
---|
| 162 | self.parent.GetSizer().Layout() |
---|
| 163 | |
---|
| 164 | def onParamChange(self, evt): |
---|
[12aa9b5] | 165 | """ |
---|
[d955bf19] | 166 | receive an event end reset value text fields |
---|
| 167 | inside self.parameters |
---|
[12aa9b5] | 168 | """ |
---|
[b06ef8c] | 169 | evt.Skip() |
---|
| 170 | if evt.type == "UPDATE": |
---|
[b40ad40] | 171 | for item in self.parameters: |
---|
[b06ef8c] | 172 | if item[0] in evt.params: |
---|
[32c0841] | 173 | item[1].SetValue("%-5.3g" % evt.params[item[0]]) |
---|
[b06ef8c] | 174 | item[1].Refresh() |
---|
[b40ad40] | 175 | |
---|
| 176 | def onTextEnter(self, evt): |
---|
[b06ef8c] | 177 | """ |
---|
[d955bf19] | 178 | Parameters have changed |
---|
[b40ad40] | 179 | """ |
---|
[b06ef8c] | 180 | params = {} |
---|
| 181 | has_error = False |
---|
| 182 | for item in self.parameters: |
---|
| 183 | try: |
---|
| 184 | params[item[0]] = float(item[1].GetValue()) |
---|
[e075203] | 185 | item[1].SetBackgroundColour( |
---|
| 186 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
[b06ef8c] | 187 | item[1].Refresh() |
---|
| 188 | except: |
---|
| 189 | has_error = True |
---|
| 190 | item[1].SetBackgroundColour("pink") |
---|
| 191 | item[1].Refresh() |
---|
| 192 | |
---|
[e075203] | 193 | if not has_error: |
---|
[b06ef8c] | 194 | # Post parameter event |
---|
[e075203] | 195 | # parent here is plotter2D |
---|
[b06ef8c] | 196 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
| 197 | wx.PostEvent(self.parent, event) |
---|
[39f0bf4] | 198 | |
---|
[f6bb24d] | 199 | def onBatchSlice(self, evt=None): |
---|
[39f0bf4] | 200 | """ |
---|
[f6bb24d] | 201 | Batch slicing button is pushed |
---|
[39f0bf4] | 202 | :param evt: Event triggering hide/show of the batch slicer parameters |
---|
| 203 | """ |
---|
[24fc06a] | 204 | # Process each data file individually |
---|
| 205 | for item in self.data_list.CheckedStrings: |
---|
| 206 | # Get data_id |
---|
| 207 | data_dict = self.parent.parent._data_manager.get_by_name([item]) |
---|
| 208 | data_ids = data_dict.keys() |
---|
| 209 | # Plot data |
---|
[ba90c32] | 210 | self.parent.parent.plot_data(state_id=[], data_id=data_ids, theory_id=[]) |
---|
[f6bb24d] | 211 | # TODO: apply slicer |
---|
| 212 | # TODO: save file (if desired) |
---|
| 213 | # TODO: send to fitting (if desired) |
---|
| 214 | plot = None |
---|
| 215 | slicer = None |
---|
| 216 | f_name = None |
---|
[39f0bf4] | 217 | |
---|
| 218 | def onChangeSlicer(self, evt): |
---|
| 219 | """ |
---|
| 220 | Change the slicer type when changed in the dropdown |
---|
| 221 | :param evt: Event triggering this change |
---|
| 222 | """ |
---|
| 223 | type = self.type_select.GetStringSelection() |
---|
| 224 | if self.type != type: |
---|
| 225 | if type == "SectorInteractor": |
---|
| 226 | self.parent.onSectorQ(None) |
---|
| 227 | elif type == "AnnulusInteractor": |
---|
| 228 | self.parent.onSectorPhi(None) |
---|
| 229 | elif type == "BoxInteractorX": |
---|
| 230 | self.parent.onBoxavgX(None) |
---|
| 231 | elif type == "BoxInteractorY": |
---|
| 232 | self.parent.onBoxavgY(None) |
---|