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

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 e075203 was e075203, checked in by krzywon, 7 years ago

PEP 8

  • Property mode set to 100644
File size: 8.0 KB
Line 
1
2
3import wx
4import wx.lib.newevent
5#from copy import deepcopy
6from sas.sasgui.guiframe.events import EVT_SLICER_PARS
7from sas.sasgui.guiframe.utils import format_number
8from sas.sasgui.guiframe.events import EVT_SLICER
9from sas.sasgui.guiframe.events import SlicerParameterEvent, SlicerEvent
10
11
12class SlicerParameterPanel(wx.Dialog):
13    """
14    Panel class to show the slicer parameters
15    """
16    #TODO: show units
17    #TODO: order parameters properly
18
19    def __init__(self, parent, *args, **kwargs):
20        """
21        Dialog window that allow to edit parameters slicer
22        by entering new values
23        """
24        wx.Dialog.__init__(self, parent, *args, **kwargs)
25        self.params = {}
26        self.parent = parent
27        self.type = None
28        self.listeners = []
29        self.parameters = []
30        self.bck = wx.GridBagSizer(5, 5)
31        self.SetSizer(self.bck)
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),
35                     flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15)
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        """
42        Process EVT_SLICER events
43        When the slicer changes, update the panel
44
45        :param event: EVT_SLICER event
46        """
47        event.Skip()
48        if event.obj_class is None:
49            self.set_slicer(None, None)
50        else:
51            self.set_slicer(event.type, event.params)
52
53    def set_slicer(self, type, params):
54        """
55        Rebuild the panel
56        """
57        self.bck.Clear(True)
58        self.bck.Add((5, 5), (0, 0), (1, 1),
59                     wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
60        self.type = type
61        if type is None:
62            label = "Right-click on 2D plot for slicer options"
63            title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
64            self.bck.Add(title, (1, 0), (1, 2),
65                         flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15)
66        else:
67            title = wx.StaticText(self, -1,
68                                  "Slicer Parameters", style=wx.ALIGN_LEFT)
69            self.bck.Add(title, (1, 0), (1, 2),
70                         flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15)
71            ix = 0
72            iy = 1
73            self.parameters = []
74            keys = params.keys()
75            keys.sort()
76            for item in keys:
77                iy += 1
78                ix = 0
79                if not item in ["count", "errors"]:
80                    text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT)
81                    self.bck.Add(text, (iy, ix), (1, 1),
82                                 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
83                    ctl = wx.TextCtrl(self, -1, size=(80, 20),
84                                      style=wx.TE_PROCESS_ENTER)
85                    hint_msg = "Modify the value of %s to change" % item
86                    hint_msg += " the 2D slicer"
87                    ctl.SetToolTipString(hint_msg)
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])
92                    self.bck.Add(ctl, (iy, ix), (1, 1),
93                                 wx.EXPAND | wx.ADJUST_MINSIZE, 0)
94                    ix = 3
95                    self.bck.Add((20, 20), (iy, ix), (1, 1),
96                                 wx.EXPAND | wx.ADJUST_MINSIZE, 0)
97                else:
98                    text = wx.StaticText(self, -1, item + " : ",
99                                         style=wx.ALIGN_LEFT)
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)
105                    ix = 1
106                    self.bck.Add(ctl, (iy, ix), (1, 1),
107                                 wx.EXPAND | wx.ADJUST_MINSIZE, 0)
108            ix = 0
109            iy += 1
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),
115                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
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
126            iy += 1
127            button_label = "Batch Slicing"
128            self.batch_slicer_button = wx.Button(parent=self, label=button_label)
129            self.Bind(wx.EVT_BUTTON, self.onToggleBatchSlicing)
130            self.bck.Add(self.batch_slicer_button, (iy, ix), (1, 1),
131                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
132            self.batch_slice_params = wx.GridBagSizer(5, 5)
133            self.bck.Hide(item=self.batch_slice_params, recursive=True)
134            iy += 1
135            self.bck.Add(self.batch_slice_params, (iy, ix), (1, 1),
136                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
137            iy += 1
138            ix = 1
139            self.bck.Add((5, 5), (iy, ix), (1, 1),
140                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
141        self.bck.Layout()
142        self.bck.Fit(self)
143        self.parent.GetSizer().Layout()
144
145    def onParamChange(self, evt):
146        """
147        receive an event end reset value text fields
148        inside self.parameters
149        """
150        evt.Skip()
151        if evt.type == "UPDATE":
152            for item in self.parameters:
153                if item[0] in evt.params:
154                    item[1].SetValue("%-5.3g" % evt.params[item[0]])
155                    item[1].Refresh()
156
157    def onTextEnter(self, evt):
158        """
159        Parameters have changed
160        """
161        params = {}
162        has_error = False
163        for item in self.parameters:
164            try:
165                params[item[0]] = float(item[1].GetValue())
166                item[1].SetBackgroundColour(
167                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
168                item[1].Refresh()
169            except:
170                has_error = True
171                item[1].SetBackgroundColour("pink")
172                item[1].Refresh()
173
174        if not has_error:
175            # Post parameter event
176            # parent here is plotter2D
177            event = SlicerParameterEvent(type=self.type, params=params)
178            wx.PostEvent(self.parent, event)
179
180    def onToggleBatchSlicing(self, evt=None):
181        """
182        Batch slicing parameters button is pushed
183        :param evt: Event triggering hide/show of the batch slicer parameters
184        """
185        if self.bck.IsShown(item=self.batch_slice_params):
186            self.bck.Hide(item=self.batch_slice_params, recursive=True)
187        else:
188            self.bck.Show(item=self.batch_slice_params, recursive=True)
189
190    def onChangeSlicer(self, evt):
191        """
192        Change the slicer type when changed in the dropdown
193        :param evt: Event triggering this change
194        """
195        type = self.type_select.GetStringSelection()
196        if self.type != type:
197            if type == "SectorInteractor":
198                self.parent.onSectorQ(None)
199            elif type == "AnnulusInteractor":
200                self.parent.onSectorPhi(None)
201            elif type == "BoxInteractorX":
202                self.parent.onBoxavgX(None)
203            elif type == "BoxInteractorY":
204                self.parent.onBoxavgY(None)
Note: See TracBrowser for help on using the repository browser.