source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/parameters_panel_slicer.py @ 1661cd5

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 1661cd5 was f15da54, checked in by krzywon, 7 years ago

Apply slicer to plots via events. Look for loaded plots instead of loaded data.

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