source: sasview/src/sas/sasgui/perspectives/calculator/collimation_editor.py @ fa81e94

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since fa81e94 was fa81e94, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Initial commit of the P(r) inversion perspective.
Code merged from Jeff Krzywon's ESS_GUI_Pr branch.
Also, minor 2to3 mods to sascalc/sasgui to enble error free setup.

  • Property mode set to 100755
File size: 22.2 KB
Line 
1"""
2"""
3import wx
4import sys
5from copy import deepcopy
6from sas.sascalc.dataloader.loader import Loader
7from sas.sascalc.dataloader.data_info import Aperture, Collimation
8from .aperture_editor import ApertureDialog
9
10from sas.sasgui.guiframe.utils import check_float
11_BOX_WIDTH = 60
12
13if sys.platform.count("win32") > 0:
14    _STATICBOX_WIDTH = 500
15    PANEL_WIDTH = 530
16    PANEL_HEIGHT = 430
17    FONT_VARIANT = 0
18else:
19    _STATICBOX_WIDTH = 550
20    PANEL_WIDTH = 600
21    PANEL_HEIGHT = 480
22    FONT_VARIANT = 1
23
24class CollimationDialog(wx.Dialog):
25    """
26    """
27    def __init__(self, parent=None, manager=None,
28                 collimation=[], *args, **kwds):
29        """
30        """
31        kwds['size'] = (PANEL_WIDTH, PANEL_HEIGHT)
32        kwds['title'] = "Collimation Editor"
33        wx.Dialog.__init__(self, parent=parent, *args, **kwds)
34        self.parent = parent
35        self.manager = manager
36        self._collimation = collimation
37        self._reset_collimation = deepcopy(collimation)
38        self._notes = ""
39        self._description = "Edit collimation"
40        #layout attributes
41        self.main_sizer = None
42        self.box_collimation = None
43        self.boxsizer_collimation = None
44
45
46        self._do_layout()
47        self.set_values()
48
49    def _define_structure(self):
50        """
51        define initial sizer
52        """
53        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
54        self.box_collimation = wx.StaticBox(self, -1,
55                                            str("Edit Selected Collimation"))
56        self.boxsizer_collimation = wx.StaticBoxSizer(self.box_collimation,
57                                                       wx.VERTICAL)
58
59        collimation_box = wx.StaticBox(self, -1, "Edit Number of Collimations")
60        self.collimation_sizer = wx.StaticBoxSizer(collimation_box, wx.VERTICAL)
61        self.collimation_sizer.SetMinSize((_STATICBOX_WIDTH, -1))
62        self.collimation_button_sizer = wx.BoxSizer(wx.HORIZONTAL)
63        self.collimation_hint_sizer = wx.BoxSizer(wx.HORIZONTAL)
64
65        self.name_sizer = wx.BoxSizer(wx.HORIZONTAL)
66        self.length_sizer = wx.BoxSizer(wx.HORIZONTAL)
67        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
68
69        aperture_box = wx.StaticBox(self, -1, "Edit Aperture")
70        self.aperture_sizer = wx.StaticBoxSizer(aperture_box, wx.VERTICAL)
71        self.aperture_button_sizer = wx.BoxSizer(wx.HORIZONTAL)
72        self.aperture_hint_sizer = wx.BoxSizer(wx.HORIZONTAL)
73
74    def _layout_collimation(self):
75        """
76        Do the layout for collimation related widgets
77        """
78        collimation_name_txt = wx.StaticText(self, -1, "Collimation:")
79        hint_collimation_txt = 'Current available collimation.'
80        collimation_name_txt.SetToolTipString(hint_collimation_txt)
81        self.collimation_cbox = wx.ComboBox(self, -1, style=wx.CB_READONLY)
82        wx.EVT_COMBOBOX(self.collimation_cbox, -1, self.on_select_collimation)
83        hint_collimation_name_txt = 'Name of collimations.'
84        self.collimation_cbox.SetToolTipString(hint_collimation_name_txt)
85
86        self.bt_add_collimation = wx.Button(self, -1, "Add")
87        self.bt_add_collimation.SetToolTipString("Edit data's collimation.")
88        self.bt_add_collimation.Bind(wx.EVT_BUTTON, self.add_collimation)
89
90        self.bt_remove_collimation = wx.Button(self, -1, "Remove")
91        hint = "Remove data's collimation."
92        self.bt_remove_collimation.SetToolTipString(hint)
93        self.bt_remove_collimation.Bind(wx.EVT_BUTTON, self.remove_collimation)
94
95        self.collimation_button_sizer.AddMany([(collimation_name_txt, 0,
96                                                 wx.LEFT, 15),
97                                     (self.collimation_cbox, 0, wx.LEFT, 5),
98                                     (self.bt_add_collimation, 0, wx.LEFT, 10),
99                                     (self.bt_remove_collimation,
100                                       0, wx.LEFT, 5)])
101        collimation_hint_txt = 'No collimation is available for this data.'
102        self.collimation_txt = wx.StaticText(self, -1, collimation_hint_txt)
103        self.collimation_hint_sizer.Add(self.collimation_txt, 0, wx.LEFT, 10)
104        self.collimation_sizer.AddMany([(self.collimation_button_sizer,
105                                          0, wx.ALL, 10),
106                                     (self.collimation_hint_sizer,
107                                       0, wx.ALL, 10)])
108
109        self.fill_collimation_combox()
110        self.enable_collimation()
111
112
113    def _layout_name(self):
114        """
115        Do the layout for collimation name related widgets
116        """
117        #Collimation name [string]
118        name_txt = wx.StaticText(self, -1, 'Name : ')
119        self.name_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH * 5, 20), style=0)
120        self.name_sizer.AddMany([(name_txt, 0, wx.LEFT | wx.RIGHT, 10),
121                                       (self.name_tcl, 0, wx.EXPAND)])
122
123    def _layout_length(self):
124        """
125        Do the  layout for length related widgets
126        """
127        #Collimation length
128        length_txt = wx.StaticText(self, -1, 'Length:')
129        self.length_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0)
130        length_unit_txt = wx.StaticText(self, -1, 'Unit: ')
131        self.length_unit_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20),
132                                           style=0)
133        self.length_sizer.AddMany([(length_txt, 0, wx.LEFT | wx.RIGHT, 10),
134                                     (self.length_tcl, 0, wx.LEFT, 12),
135                                     (length_unit_txt, 0, wx.LEFT | wx.RIGHT, 10),
136                                     (self.length_unit_tcl, 0, wx.EXPAND)])
137
138    def _layout_button(self):
139        """
140        Do the layout for the button widgets
141        """
142        self.bt_apply = wx.Button(self, -1, 'Apply')
143        self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply)
144        self.bt_apply.SetToolTipString("Apply current changes to collimation.")
145        self.bt_cancel = wx.Button(self, -1, 'Cancel')
146        self.bt_cancel.SetToolTipString("Cancel current changes.")
147        self.bt_cancel.Bind(wx.EVT_BUTTON, self.on_click_cancel)
148        self.bt_close = wx.Button(self, wx.ID_CANCEL, 'Close')
149        self.bt_close.SetToolTipString("Close window.")
150        self.button_sizer.AddMany([(self.bt_apply, 0, wx.LEFT, 200),
151                                   (self.bt_cancel, 0, wx.LEFT, 10),
152                                   (self.bt_close, 0, wx.LEFT, 10)])
153    def _layout_aperture(self):
154        """
155        Do the layout for aperture related widgets
156        """
157        aperture_name_txt = wx.StaticText(self, -1, "Aperture:")
158        hint_aperture_txt = 'Current available aperture.'
159        aperture_name_txt.SetToolTipString(hint_aperture_txt)
160        self.aperture_cbox = wx.ComboBox(self, -1, style=wx.CB_READONLY)
161        hint_aperture_name_txt = 'Name of apertures.'
162        self.aperture_cbox.SetToolTipString(hint_aperture_name_txt)
163
164        self.bt_add_aperture = wx.Button(self, -1, "Add")
165        self.bt_add_aperture.SetToolTipString("Edit data's aperture.")
166        self.bt_add_aperture.Bind(wx.EVT_BUTTON, self.add_aperture)
167        self.bt_edit_aperture = wx.Button(self, -1, "Edit")
168        self.bt_edit_aperture.SetToolTipString("Edit data's aperture.")
169        self.bt_edit_aperture.Bind(wx.EVT_BUTTON, self.edit_aperture)
170        self.bt_remove_aperture = wx.Button(self, -1, "Remove")
171        self.bt_remove_aperture.SetToolTipString("Remove data's aperture.")
172        self.bt_remove_aperture.Bind(wx.EVT_BUTTON, self.remove_aperture)
173
174        self.aperture_button_sizer.AddMany([(aperture_name_txt, 0, wx.LEFT, 15),
175                                     (self.aperture_cbox, 0, wx.LEFT, 5),
176                                     (self.bt_add_aperture, 0, wx.LEFT, 10),
177                                     (self.bt_edit_aperture, 0, wx.LEFT, 5),
178                                     (self.bt_remove_aperture, 0, wx.LEFT, 5)])
179        aperture_hint_txt = 'No aperture is available for this data.'
180        self.aperture_txt = wx.StaticText(self, -1, aperture_hint_txt)
181        self.aperture_hint_sizer.Add(self.aperture_txt, 0, wx.LEFT, 10)
182        self.aperture_sizer.AddMany([(self.aperture_button_sizer,
183                                       0, wx.ALL, 10),
184                                     (self.aperture_hint_sizer, 0, wx.ALL, 10)])
185        self.fill_aperture_combox()
186        self.enable_aperture()
187
188    def _do_layout(self):
189        """
190        Draw the current panel
191        """
192        self._define_structure()
193        self._layout_collimation()
194        self._layout_name()
195        self._layout_length()
196        self._layout_aperture()
197        self._layout_button()
198
199        self.boxsizer_collimation.AddMany([(self.name_sizer, 0,
200                                          wx.EXPAND | wx.TOP | wx.BOTTOM, 5),
201                                          (self.length_sizer, 0,
202                                     wx.EXPAND | wx.TOP | wx.BOTTOM, 5),
203                                     (self.aperture_sizer, 0,
204                                     wx.EXPAND | wx.ALL, 10)])
205        self.main_sizer.AddMany([(self.collimation_sizer, 0, wx.ALL, 10),
206                                  (self.boxsizer_collimation, 0, wx.ALL, 10),
207                                  (self.button_sizer, 0,
208                                    wx.EXPAND | wx.TOP | wx.BOTTOM, 5)])
209        self.SetSizer(self.main_sizer)
210        self.SetAutoLayout(True)
211
212    def get_current_collimation(self):
213        """
214        """
215        if not self.collimation_cbox.IsEnabled():
216            return None, None, None
217        position = self.collimation_cbox.GetSelection()
218        if position == wx.NOT_FOUND:
219            return None, None, None
220        collimation_name = self.collimation_cbox.GetStringSelection()
221        collimation = self.collimation_cbox.GetClientData(position)
222        return collimation, collimation_name, position
223
224    def fill_collimation_combox(self):
225        """
226        fill the current combobox with the available collimation
227        """
228        if self._collimation is None or self._collimation == []:
229            return
230        for collimation in self._collimation:
231            pos = self.collimation_cbox.Append(str(collimation.name))
232            self.collimation_cbox.SetClientData(pos, collimation)
233            self.collimation_cbox.SetSelection(pos)
234            self.collimation_cbox.SetStringSelection(str(collimation.name))
235
236    def add_collimation(self, event):
237        """
238        Append empty collimation to data's list of collimation
239        """
240
241        if not self.collimation_cbox.IsEnabled():
242            self.collimation_cbox.Enable()
243        collimation = Collimation()
244        self._collimation.append(collimation)
245        position = self.collimation_cbox.Append(str(collimation.name))
246        self.collimation_cbox.SetClientData(position, collimation)
247        self.collimation_cbox.SetSelection(position)
248        self.enable_collimation()
249        self.bt_add_aperture.Enable()
250        self.fill_aperture_combox()
251        self.enable_aperture()
252        self.set_values()
253
254    def remove_collimation(self, event):
255        """
256        Remove collimation to data's list of collimation
257        """
258        if self.collimation_cbox.IsEnabled():
259            if self.collimation_cbox.GetCount() > 0:
260                position = self.collimation_cbox.GetCurrentSelection()
261                collimation = self.collimation_cbox.GetClientData(position)
262                if collimation in self._collimation:
263                    self._collimation.remove(collimation)
264                    self.collimation_cbox.Delete(position)
265                    #set the combo box box the next available item
266                    position = self.collimation_cbox.GetCount()
267                    if position > 0:
268                        position -= 1
269                    self.collimation_cbox.SetSelection(position)
270        if not self._collimation and self.collimation_cbox.GetCount() == 0:
271            self.bt_add_aperture.Disable()
272            self.name_tcl.Clear()
273            self.length_tcl.Clear()
274            self.length_unit_tcl.Clear()
275            self.aperture_cbox.Clear()
276            self.aperture_cbox.Disable()
277        #disable or enable the combo box when necessary
278        self.enable_collimation()
279        self.enable_aperture()
280
281    def on_select_collimation(self, event):
282        """
283        fill the control on the panel according to
284        the current  selected collimation
285        """
286        self.set_values()
287        self.fill_aperture_combox()
288        self.enable_aperture()
289
290    def enable_collimation(self):
291        """
292        Enable /disable widgets related to collimation
293        """
294        if self._collimation is not None and \
295            self.collimation_cbox.GetCount() > 0:
296            self.collimation_cbox.Enable()
297            self.bt_remove_collimation.Enable()
298            n_collimation = self.collimation_cbox.GetCount()
299            collimation_hint_txt = "collimations"
300            collimation_hint_txt += " available: %s " % str(n_collimation)
301            if len(self._collimation) > 0:
302                self.bt_remove_collimation.Enable()
303            else:
304                self.bt_remove_collimation.Disable()
305        else:
306            self.collimation_cbox.Disable()
307            self.bt_remove_collimation.Disable()
308            collimation_hint_txt = 'No collimation is available for this data.'
309        self.collimation_txt.SetLabel(collimation_hint_txt)
310
311
312    def reset_collimation_combobox(self, edited_collimation):
313        """
314        take all edited editor and reset clientdata of collimation combo box
315        """
316        for position in range(self.collimation_cbox.GetCount()):
317            collimation = self.collimation_cbox.GetClientData(position)
318            if collimation == edited_collimation:
319                collimation = edited_collimation
320                self.collimation_cbox.SetString(position, str(collimation.name))
321                self.collimation_cbox.SetClientData(position, collimation)
322                self.collimation_cbox.SetStringSelection(str(collimation.name))
323
324    def add_aperture(self, event):
325        """
326        Append empty aperture to data's list of aperture
327        """
328        collimation, _, _ = self.get_current_collimation()
329        if not self.aperture_cbox.IsEnabled():
330            self.aperture_cbox.Enable()
331        aperture = Aperture()
332        collimation.aperture.append(aperture)
333        position = self.aperture_cbox.Append(str(aperture.name))
334        self.aperture_cbox.SetClientData(position, aperture)
335        self.aperture_cbox.SetSelection(position)
336        self.enable_aperture()
337
338    def edit_aperture(self, event):
339        """
340        Edit the selected aperture
341        """
342        if self._collimation is None or not self.aperture_cbox.IsEnabled():
343            return
344        position = self.aperture_cbox.GetSelection()
345        if position != wx.NOT_FOUND:
346            name = self.aperture_cbox.GetStringSelection()
347            aperture = self.aperture_cbox.GetClientData(position)
348            dlg = ApertureDialog(parent=self, aperture=aperture)
349            dlg.set_manager(self)
350            dlg.ShowModal()
351
352    def remove_aperture(self, event):
353        """
354        Remove aperture to data's list of aperture
355        """
356        if self._collimation is None or not self._collimation:
357            return
358        collimation, _, _ = self.get_current_collimation()
359        if self.aperture_cbox.IsEnabled():
360            if self.aperture_cbox.GetCount() > 1:
361                position = self.aperture_cbox.GetCurrentSelection()
362                aperture = self.aperture_cbox.GetClientData(position)
363                if aperture in collimation.aperture:
364                    collimation.aperture.remove(aperture)
365                    self.aperture_cbox.Delete(position)
366                    #set the combo box box the next available item
367                    position = self.aperture_cbox.GetCount()
368                    if position > 0:
369                        position -= 1
370                    self.aperture_cbox.SetSelection(position)
371
372        #disable or enable the combo box when necessary
373        self.enable_aperture()
374
375    def set_aperture(self, aperture):
376        """
377        set aperture for data
378        """
379        if self._collimation is None or not self._collimation:
380            return
381        collimation, _, _ = self.get_current_collimation()
382        if collimation.aperture:
383            for item in collimation.aperture:
384                if item == aperture:
385                    item = aperture
386                    self.reset_aperture_combobox(edited_aperture=aperture)
387                    return
388
389    def enable_aperture(self):
390        """
391        Enable /disable widgets crelated to aperture
392        """
393        collimation, _, _ = self.get_current_collimation()
394        if  self.aperture_cbox.GetCount() > 0:
395            self.aperture_cbox.Enable()
396            self.bt_edit_aperture.Enable()
397            self.bt_remove_aperture.Enable()
398            n_aperture = self.aperture_cbox.GetCount()
399            aperture_hint_txt = 'apertures available: %s ' % str(n_aperture)
400            if len(collimation.aperture) > 0:
401                self.bt_remove_aperture.Enable()
402            else:
403                self.bt_remove_aperture.Disable()
404        else:
405            self.aperture_cbox.Disable()
406            self.bt_edit_aperture.Disable()
407            self.bt_remove_aperture.Disable()
408            aperture_hint_txt = 'No aperture is available for this data.'
409        self.aperture_txt.SetLabel(aperture_hint_txt)
410
411    def reset_aperture_combobox(self, edited_aperture):
412        """
413        take all edited editor and reset clientdata of aperture combo box
414        """
415        for position in range(self.aperture_cbox.GetCount()):
416            aperture = self.aperture_cbox.GetClientData(position)
417            if aperture == edited_aperture:
418                aperture = edited_aperture
419                self.aperture_cbox.SetString(position, str(aperture.name))
420                self.aperture_cbox.SetClientData(position, aperture)
421                self.aperture_cbox.SetStringSelection(str(aperture.name))
422
423    def fill_aperture_combox(self):
424        """
425        fill the current combobox with the available aperture
426        """
427        self.aperture_cbox.Clear()
428        collimation, _, _ = self.get_current_collimation()
429        if collimation is None or not collimation.aperture:
430            return
431        for aperture in collimation.aperture:
432            pos = self.aperture_cbox.Append(str(aperture.name))
433            self.aperture_cbox.SetClientData(pos, aperture)
434            self.aperture_cbox.SetSelection(pos)
435            self.aperture_cbox.SetStringSelection(str(aperture.name))
436
437    def set_manager(self, manager):
438        """
439        Set manager of this window
440        """
441        self.manager = manager
442
443    def set_values(self):
444        """
445        take the collimation values of the current data and display them
446        through the panel
447        """
448        collimation, _, _ = self.get_current_collimation()
449        if collimation is None:
450            self.bt_add_aperture.Disable()
451            self.length_tcl.SetValue("")
452            self.name_tcl.SetValue("")
453            self.length_unit_tcl.SetValue("")
454            return
455        #Name
456        self.name_tcl.SetValue(str(collimation.name))
457        #length
458        self.length_tcl.SetValue(str(collimation.length))
459        #Length unit
460        self.length_unit_tcl.SetValue(str(collimation.length_unit))
461
462    def get_collimation(self):
463        """
464        return the current collimation
465        """
466        return self._collimation
467
468    def get_notes(self):
469        """
470        return notes
471        """
472        return self._notes
473
474    def on_change_name(self):
475        """
476        Change name
477        """
478        collimation, collimation_name, position = self.get_current_collimation()
479        if collimation is None:
480            return
481        #Change the name of the collimation
482        name = self.name_tcl.GetValue().lstrip().rstrip()
483        if name == "" or name == str(None):
484            name = None
485        if collimation.name != name:
486            self._notes += "Change collimation 's "
487            self._notes += "name from %s to %s \n" % (collimation.name, name)
488            collimation.name = name
489            self.collimation_cbox.SetString(position, str(collimation.name))
490            self.collimation_cbox.SetClientData(position, collimation)
491            self.collimation_cbox.SetStringSelection(str(collimation.name))
492
493    def on_change_length(self):
494        """
495        Change the length
496        """
497        collimation, collimation_name, position = self.get_current_collimation()
498        if collimation is None:
499            return
500        #Change length 
501        length = self.length_tcl.GetValue().lstrip().rstrip()
502        if length == "" or length == str(None):
503            length = None
504            collimation.length = length
505        else:
506            if check_float(self.length_tcl):
507                if collimation.length != float(length):
508                    self._notes += "Change Collimation length from "
509                    self._notes += "%s to %s \n" % (collimation.length, length)
510                    collimation.length = float(length)
511            else:
512                self._notes += "Error: Expected a float for collimation length"
513                self._notes += " won't changes length from "
514                self._notes += "%s to %s" % (collimation.length, length)
515        #change length  unit
516        unit = self.length_unit_tcl.GetValue().lstrip().rstrip()
517        if collimation.length_unit != unit:
518            self._notes += " Change length's unit from "
519            self._notes += "%s to %s" % (collimation.length_unit, unit)
520            collimation.length_unit = unit
521
522    def on_click_apply(self, event):
523        """
524        Apply user values to the collimation
525        """
526        self.on_change_name()
527        self.on_change_length()
528        self.set_values()
529        if self.manager is not None:
530            self.manager.set_collimation(self._collimation, self._notes)
531
532    def on_click_cancel(self, event):
533        """
534        leave the collimation as it is and close
535        """
536        self._collimation = deepcopy(self._reset_collimation)
537        self.set_values()
538        if self.manager is not None:
539            self.manager.set_collimation(self._collimation)
540
541
542if __name__ == "__main__":
543
544    app = wx.App()
545    dlg = CollimationDialog(collimation=[Collimation()])
546    dlg.ShowModal()
547    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.