source: sasview/guiframe/data_panel.py @ 8994b6b

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 8994b6b was 6db811e, checked in by Gervaise Alina <gervyh@…>, 14 years ago

add data loading panel

  • Property mode set to 100644
File size: 21.4 KB
Line 
1################################################################################
2#This software was developed by the University of Tennessee as part of the
3#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4#project funded by the US National Science Foundation.
5#
6#See the license text in license.txt
7#
8#copyright 2010, University of Tennessee
9################################################################################
10"""
11This module provides Graphic interface for the data_manager module.
12"""
13import wx
14import sys
15import warnings
16from wx.lib.scrolledpanel import ScrolledPanel
17import  wx.lib.agw.customtreectrl as CT
18from sans.guiframe.dataFitting import Data1D
19from sans.guiframe.dataFitting import Data2D
20
21PANEL_WIDTH = 200
22
23class DataTreeCtrl(CT.CustomTreeCtrl):
24    """
25    Check list control to be used for Data Panel
26    """
27    def __init__(self, parent,*args, **kwds):
28        kwds['style']= wx.SUNKEN_BORDER|CT.TR_HAS_BUTTONS| CT.TR_HIDE_ROOT|   \
29                    CT.TR_HAS_VARIABLE_ROW_HEIGHT|wx.WANTS_CHARS
30        CT.CustomTreeCtrl.__init__(self, parent, *args, **kwds)
31        self.root = self.AddRoot("Available Data")
32       
33class DataPanel(ScrolledPanel):
34    """
35    This panel displays data available in the application and widgets to
36    interact with data.
37    """
38    ## Internal name for the AUI manager
39    window_name = "Data Panel"
40    ## Title to appear on top of the window
41    window_caption = "Data Panel"
42    #type of window
43    window_type = "Data Panel"
44    ## Flag to tell the GUI manager that this panel is not
45    #  tied to any perspective
46    #ALWAYS_ON = True
47    def __init__(self, parent, list=[],list_of_perspective=[],
48                 size=(PANEL_WIDTH,560), manager=None, *args, **kwds):
49        kwds['size']= size
50        ScrolledPanel.__init__(self, parent=parent, *args, **kwds)
51        self.SetupScrolling()
52     
53        self.parent = parent
54        self.manager = manager
55        self.list_of_data = list
56        self.list_of_perspective = list_of_perspective
57        self.list_rb_perspectives= []
58        self.list_cb_data =[]
59        self.list_cb_theory =[]
60        self.owner = None
61        self.do_layout()
62       
63    def do_layout(self):
64        """
65        """
66        self.define_panel_structure()
67        self.layout_selection()
68        self.layout_list()
69        self.layout_button()
70        self.layout_batch()
71   
72    def define_panel_structure(self):
73        """
74        Define the skeleton of the panel
75        """
76        w, h = self.parent.GetSize()
77        self.vbox  = wx.BoxSizer(wx.VERTICAL)
78        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
79        self.sizer1.SetMinSize((w/12, h/2))
80        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
81        self.sizer3 = wx.GridBagSizer(5,5)
82        self.sizer4 = wx.BoxSizer(wx.HORIZONTAL)
83        self.sizer5 = wx.BoxSizer(wx.HORIZONTAL)
84        #self.sizer3.SetMinSize((w-60, -1))
85       
86        self.vbox.Add(self.sizer5, 0,wx.EXPAND|wx.ALL,10)
87        self.vbox.Add(self.sizer1, 0,wx.EXPAND|wx.ALL,0)
88        self.vbox.Add(self.sizer2, 0,wx.EXPAND|wx.ALL,10)
89        self.vbox.Add(self.sizer3, 0,wx.EXPAND|wx.ALL,10)
90        self.vbox.Add(self.sizer4, 0,wx.EXPAND|wx.ALL,10)
91       
92        self.SetSizer(self.vbox)
93       
94    def layout_selection(self):
95        """
96        """
97        select_txt = wx.StaticText(self, -1, 'Selection Options')
98        self.selection_cbox = wx.ComboBox(self, -1, style=wx.CB_READONLY)
99        list_of_options = ['Select all Data',
100                            'Unselect all Data',
101                           'Select all Data 1D',
102                           'Unselect all Data 1D',
103                           'Select all Data 2D',
104                           'Unselect all Data 2D' ]
105        for option in list_of_options:
106            self.selection_cbox.Append(str(option))
107        self.selection_cbox.SetValue('None')
108        wx.EVT_COMBOBOX(self.selection_cbox,-1, self._on_selection_type)
109        self.sizer5.AddMany([(select_txt,0, wx.ALL,5),
110                            (self.selection_cbox,0, wx.ALL,5)])
111    def layout_perspective(self, list_of_perspective=[]):
112        """
113        Layout widgets related to the list of plug-ins of the gui_manager
114        """
115        if len(list_of_perspective)==0:
116            return
117        w, h = self.parent.GetSize()
118        box_description_2= wx.StaticBox(self, -1, "Set Active Perspective")
119        self.boxsizer_2 = wx.StaticBoxSizer(box_description_2, wx.HORIZONTAL)
120        self.sizer_perspective = wx.GridBagSizer(5,5)
121        self.boxsizer_2.Add(self.sizer_perspective)
122        self.sizer2.Add(self.boxsizer_2,1, wx.ALL, 10)
123        self.list_of_perspective = list_of_perspective
124        self.sizer_perspective.Clear(True)
125        self.list_rb_perspectives = []
126       
127        nb_active_perspective = 0
128        if list_of_perspective:
129            ix = 0 
130            iy = 0
131            for perspective_name, is_active in list_of_perspective:
132               
133                if is_active:
134                    nb_active_perspective += 1
135                if nb_active_perspective == 1:
136                    rb = wx.RadioButton(self, -1, perspective_name, style=wx.RB_GROUP)
137                    rb.SetToolTipString("Data will be applied to this perspective")
138                    rb.SetValue(is_active)
139                else:
140                    rb = wx.RadioButton(self, -1, perspective_name)
141                    rb.SetToolTipString("Data will be applied to this perspective")
142                    #only one perpesctive can be active
143                    rb.SetValue(False)
144               
145                self.Bind(wx.EVT_RADIOBUTTON, self.on_set_active_perspective,
146                                                     id=rb.GetId())
147                self.list_rb_perspectives.append(rb)
148                self.sizer_perspective.Add(rb,(iy, ix),(1,1),
149                               wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
150                iy += 1
151        else:
152            rb = wx.RadioButton(self, -1, 'No Perspective',
153                                                      style=wx.RB_GROUP)
154            rb.SetValue(True)
155            rb.Disable()
156            ix = 0 
157            iy = 0
158            self.sizer_perspective.Add(rb,(iy, ix),(1,1),
159                           wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
160           
161    def _on_selection_type(self, event):
162        """
163        Select data according to patterns
164        """
165        option = self.selection_cbox.GetValue()
166        if option == 'Select all Data':
167            print "on select"
168           
169    def on_set_active_perspective(self, event):
170        """
171        Select the active perspective
172        """
173        ctrl = event.GetEventObject()
174       
175    def layout_button(self):
176        """
177        Layout widgets related to buttons
178        """
179        self.bt_import = wx.Button(self, wx.NewId(), "Send To")
180        self.bt_import.SetToolTipString("Send set of Data to active perspective")
181        wx.EVT_BUTTON(self, self.bt_import.GetId(), self.on_import)
182       
183        self.bt_append_plot = wx.Button(self, wx.NewId(), "Append Plot To")
184        self.bt_append_plot.SetToolTipString("Plot the selected data in the active panel")
185        wx.EVT_BUTTON(self, self.bt_append_plot.GetId(), self.on_append_plot)
186       
187        self.bt_plot = wx.Button(self, wx.NewId(), "New Plot")
188        self.bt_plot.SetToolTipString("To trigger plotting")
189        wx.EVT_BUTTON(self, self.bt_plot.GetId(), self.on_plot)
190       
191        self.bt_remove = wx.Button(self, wx.NewId(), "Delete Data")
192        self.bt_remove.SetToolTipString("Delete data from the application")
193        wx.EVT_BUTTON(self, self.bt_remove.GetId(), self.on_remove)
194       
195        self.tctrl_perspective = wx.StaticText(self, -1, 'No Active Application')
196        self.tctrl_perspective.SetToolTipString("Active Application")
197        self.tctrl_plotpanel = wx.StaticText(self, -1, 'No Plot panel on focus')
198        self.tctrl_plotpanel.SetToolTipString("Active Plot Panel")
199        #self.sizer3.AddMany([(self.bt_import,0, wx.ALL,5),
200        #                     (self.bt_append_plot,0, wx.ALL,5),
201        #                     (self.bt_plot, 0, wx.ALL,5),
202        # (self.bt_remove,0, wx.ALL,5)])
203        ix = 0
204        iy = 0
205        self.sizer3.Add(self.bt_import,( iy, ix),(1,1), 
206                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
207        ix += 1
208        self.sizer3.Add(self.tctrl_perspective,(iy, ix),(1,1),
209                          wx.EXPAND|wx.ADJUST_MINSIZE, 0)     
210        ix = 0         
211        iy += 1 
212        self.sizer3.Add(self.bt_append_plot,( iy, ix),(1,1), 
213                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
214        ix += 1
215        self.sizer3.Add(self.tctrl_plotpanel,(iy, ix),(1,1),
216                          wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
217        ix = 0         
218        iy += 1 
219        self.sizer3.Add(self.bt_plot,( iy, ix),(1,1), 
220                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
221        ix = 0         
222        iy += 1 
223        self.sizer3.Add(self.bt_remove,( iy, ix),(1,1), 
224                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
225       
226     
227    def layout_batch(self):
228        """
229        """
230       
231        self.rb_single_mode = wx.RadioButton(self, -1, 'Single Mode',
232                                             style=wx.RB_GROUP)
233        self.rb_batch_mode = wx.RadioButton(self, -1, 'Batch Mode')
234       
235        self.rb_single_mode.SetValue(True)
236        self.rb_batch_mode.SetValue(False)
237        self.sizer4.AddMany([(self.rb_single_mode,0, wx.ALL,5),
238                            (self.rb_batch_mode,0, wx.ALL,5)])
239     
240    def layout_list(self):
241        """
242        Add a listcrtl in the panel
243        """
244        self.tree_ctrl = DataTreeCtrl(parent=self)
245        self.tree_ctrl.Bind(CT.EVT_TREE_ITEM_CHECKED, self.on_check_item)
246        self.tree_ctrl.Bind(CT.EVT_TREE_ITEM_RIGHT_CLICK, self.on_right_click)
247        self.sizer1.Add(self.tree_ctrl,1, wx.EXPAND|wx.ALL, 20)
248
249    def on_right_click(self, event):
250        """
251        """
252        ## Create context menu for data
253        self.popUpMenu = wx.Menu()
254        msg = "Edit %s"%str(self.tree_ctrl.GetItemText(event.GetItem()))
255        id = wx.NewId()
256        self.edit_data_mitem = wx.MenuItem(self.popUpMenu,id,msg,
257                                 "Edit meta data")
258        wx.EVT_MENU(self, id, self.on_edit_data)
259        self.popUpMenu.AppendItem(self.edit_data_mitem)
260        self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu)
261       
262    def on_edit_data(self, event):
263        """
264        """
265        print "editing data"
266       
267    def onContextMenu(self, event): 
268        """
269        Retrieve the state selected state
270        """
271        # Skipping the save state functionality for release 0.9.0
272        #return
273        pos = event.GetPosition()
274        pos = self.ScreenToClient(pos)
275        self.PopupMenu(self.popUpMenu, pos) 
276     
277    def on_check_item(self, event):
278        """
279        """
280        item = event.GetItem()
281        name = self.tree_ctrl.GetItemText(item)
282 
283    def load_data_list(self, list):
284        """
285       
286        """
287        if not list:
288            return
289       
290        for dstate in list.values():
291            data = dstate.get_data()
292            if data is None:
293                data_name = str(data)
294                data_class = "unkonwn"
295            else:
296                data_name = data.name
297            data_class = data.__class__.__name__
298            path = dstate.get_path() 
299            theory_list = dstate.get_theory()
300            theory = None
301            if  theory_list:
302                theory = theory_list[len(theory_list)-1]
303            data_child = None
304            for item in self.list_cb_data:
305                if self.tree_ctrl.GetItemText(item) == data_name:
306                    data_child = item
307                    for process in data.process:
308                        theory_child = self.tree_ctrl.FindItem(data_child,
309                                                        "Available Theories")#,
310                        if theory is not None:
311                            av_theory_child =self.tree_ctrl.AppendItem(theory_child,
312                                                theory.name,ct_type=1, data=theory.id)
313                            self.list_cb_theory.append(av_theory_child)
314                            av_theory_child_info =self.tree_ctrl.AppendItem(av_theory_child,
315                                                     'info')
316                            for process in theory.process:
317                                info_time_child =self.tree_ctrl.AppendItem(av_theory_child_info,
318                                         process.__str__())
319                   
320                    break
321            if data_child is None:
322                data_child =self.tree_ctrl.InsertItem(self.tree_ctrl.root,0,
323                                                   data_name,ct_type=1, data=data.id)
324                cb_data = self.tree_ctrl.GetFirstChild(self.tree_ctrl.root) 
325                item, id = cb_data
326                item.Check(True)
327                self.list_cb_data.append(item)                         
328                data_info_child =self.tree_ctrl.AppendItem(data_child, 'info')#,
329                                                            #wnd=data_info_txt)
330                info_class_child =self.tree_ctrl.AppendItem(data_info_child, 
331                                                            'Type: %s'%data_class)
332                path_class_child =self.tree_ctrl.AppendItem(data_info_child,
333                                                             'Path: %s'%str(path))
334                for process in data.process:
335                    info_time_child =self.tree_ctrl.AppendItem(data_info_child,process.__str__())
336                theory_child =self.tree_ctrl.AppendItem(data_child, "Available Theories")
337               
338                if  theory_list:
339                    theory = theory_list[len(theory_list)-1]
340                    if theory is not None:
341                        av_theory_child =self.tree_ctrl.AppendItem(theory_child,
342                                                    theory.name,ct_type=1)
343                        self.list_cb_theory.append(av_theory_child)
344                        av_theory_child_info =self.tree_ctrl.AppendItem(av_theory_child,
345                                                         'info')
346                        for process in theory.process:
347                            info_time_child =self.tree_ctrl.AppendItem(av_theory_child_info,
348                                             process.__str__())
349                   
350    def set_data_helper(self):
351        """
352        """
353        data_to_plot = []
354        for item in self.list_cb_data:
355            if item.IsChecked():
356               
357               data_to_plot.append(self.tree_ctrl.GetItemPyData(item))
358        theory_to_plot = []
359        for item in self.list_cb_theory:
360            if item.IsChecked():
361                theory_to_plot.append(self.tree_ctrl.GetItemPyData(item))
362        return data_to_plot, theory_to_plot
363   
364    def on_remove(self, event):
365        data_to_remove, theory_to_remove = self.set_data_helper()
366        for item in self.list_cb_data:
367            if item.IsChecked()and \
368                self.tree_ctrl.GetItemText(item) in data_to_remove:
369                self.tree_ctrl.Delete(item)
370        for item in self.list_cb_theory:
371            if item.IsChecked()and \
372                self.tree_ctrl.GetItemText(item) in theory_to_remove:
373                self.tree_ctrl.Delete(item)
374        delete_all = False
375        if data_to_remove:
376            delete_all = True
377        self.parent.delete_data(data_id=data_to_remove,
378                                  theory_id=theory_to_remove,
379                                  delete_all=delete_all)
380       
381    def on_import(self, event=None):
382        """
383        Get all select data and set them to the current active perspetive
384        """
385        self.post_helper(plot=False)
386       
387    def on_append_plot(self, event=None):
388        """
389        append plot to plot panel on focus
390        """
391        self.post_helper(plot=True, append=True)
392   
393    def on_plot(self, event=None):
394        """
395        Send a list of data names to plot
396        """
397        self.post_helper(plot=True)
398       
399    def set_active_perspective(self, name):
400        """
401        set the active perspective
402        """
403        self.tctrl_perspective.SetLabel(str(name))
404     
405    def set_panel_on_focus(self, name):
406        """
407        set the plot panel on focus
408        """
409        self.tctrl_plotpanel.SetLabel(str(name))
410       
411    def post_helper(self, plot=False, append=False):
412        """
413        """
414        data_to_plot, theory_to_plot = self.set_data_helper()
415     
416        if self.parent is not None:
417            self.parent.get_data_from_panel(data_id=data_to_plot, plot=plot,
418                                            append=append)
419
420
421class DataFrame(wx.Frame):
422    ## Internal name for the AUI manager
423    window_name = "Data Panel"
424    ## Title to appear on top of the window
425    window_caption = "Data Panel"
426    ## Flag to tell the GUI manager that this panel is not
427    #  tied to any perspective
428    ALWAYS_ON = True
429   
430    def __init__(self, parent=None, owner=None, manager=None,size=(600, 600),
431                         list_of_perspective=[],list=[], *args, **kwds):
432        #kwds['size'] = size
433        kwds['id'] = -1
434        kwds['title']= "Loaded Data"
435        wx.Frame.__init__(self, parent=parent, *args, **kwds)
436        self.parent = parent
437        self.owner = owner
438        self.manager = manager
439        self.panel = DataPanel(parent=self, 
440                               size=size,
441                               list_of_perspective=list_of_perspective)
442     
443    def load_data_list(self, list=[]):
444        """
445        Fill the list inside its panel
446        """
447        self.panel.load_data_list(list=list)
448       
449    def layout_perspective(self, list_of_perspective=[]):
450        """
451        """
452        self.panel.layout_perspective(list_of_perspective=list_of_perspective)
453       
454   
455from dataFitting import Data1D
456from dataFitting import Data2D, Theory1D
457from data_state import DataState
458import sys
459class State():
460    def __init__(self):
461        self.msg = ""
462    def __str__(self):
463        self.msg = "model mane : model1\n"
464        self.msg += "params : \n"
465        self.msg += "name  value\n"
466        return msg
467def set_data_state(data, path, theory, state):
468    dstate = DataState(data=data)
469    dstate.set_path(path=path)
470    dstate.set_theory(theory)
471    dstate.set_state(state)
472    return dstate
473"""'
474data_list = [1:('Data1', 'Data1D', '07/01/2010', "theory1d", "state1"),
475            ('Data2', 'Data2D', '07/03/2011', "theory2d", "state1"),
476            ('Data3', 'Theory1D', '06/01/2010', "theory1d", "state1"),
477            ('Data4', 'Theory2D', '07/01/2010', "theory2d", "state1"),
478            ('Data5', 'Theory2D', '07/02/2010', "theory2d", "state1")]
479"""     
480if __name__ == "__main__":
481   
482    app = wx.App()
483    try:
484        list_of_perspective = [('perspective2', False), ('perspective1', True)]
485        data_list = {}
486        data = Data1D()
487        data.name = "data1"
488        data.id = 1
489        #data.append_process()
490        #process = data.process[len(data.process)-1]
491        #process.data = "07/01/2010"
492        theory = Theory1D()
493        theory.id = 34
494        theory.pseudo_name = "theory1"
495        path = "path1"
496        state = State()
497        data_list['1']=set_data_state(data, path,theory, state)
498       
499        data = Data1D()
500        data.name = "data2"
501        data.id = 76
502        theory = Theory1D()
503        theory.id = 78
504        theory.name = "CoreShell 07/24/25"
505        theory.pseudo_name = "CoreShell"
506        path = "path2"
507        state = State()
508        data_list['2']=set_data_state(data, path,theory, state)
509        data = Data1D()
510        data.id = 3
511        data.name = "data2"
512        theory = Theory1D()
513        theory.name = "CoreShell"
514        theory.pseudo_name = "CoreShell"
515        theory.id = 4
516        #theory.append_process()
517        #process = theory.process[len(theory.process)-1]
518        #process.description = "this is my description"
519        path = "path3"
520        #data.append_process()
521        #process = data.process[len(data.process)-1]
522        #process.data = "07/22/2010"
523        data_list['4']=set_data_state(data, path,theory, state)
524       
525        data = Data2D()
526        data.name = "data3"
527        data.id = 5
528        #data.append_process()
529        #process = data.process[len(data.process)-1]
530        #process.data = "07/01/2010"
531        theory = Theory1D()
532        theory.pseudo_name = "Cylinder"
533        path = "path2"
534        state = State()
535        dstate= set_data_state(data, path,theory, state)
536        theory = Theory1D()
537        theory.id = 6
538        theory.pseudo_name = "Sphere"
539        dstate.set_theory(theory)
540        data_list['3']=dstate
541       
542        window = DataFrame(list=data_list)
543        window.load_data_list(list=data_list)
544        window.layout_perspective(list_of_perspective=list_of_perspective)
545        window.Show(True)
546    except:
547        #raise
548        print "error",sys.exc_value
549    app.MainLoop() 
550   
551   
Note: See TracBrowser for help on using the repository browser.