source: sasview/calculatorview/perspectives/calculator/collimation_editor.py @ 91f151a

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.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 91f151a was 91f151a, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on data editor

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