source: sasview/guiframe/data_panel.py @ 248b918

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 248b918 was 248b918, checked in by Gervaise Alina <gervyh@…>, 13 years ago

working on remove data

  • Property mode set to 100644
File size: 41.1 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 os
14import wx
15import sys
16import warnings
17from wx.lib.scrolledpanel import ScrolledPanel
18import  wx.lib.agw.customtreectrl as CT
19from sans.guiframe.dataFitting import Data1D
20from sans.guiframe.dataFitting import Data2D
21from sans.guiframe.panel_base import PanelBase
22from sans.guiframe.events import StatusEvent
23from DataLoader.loader import Loader
24import logging
25
26try:
27    # Try to find a local config
28    import imp
29    path = os.getcwd()
30    if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \
31        (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))):
32        fObj, path, descr = imp.find_module('local_config', [path])
33        config = imp.load_module('local_config', fObj, path, descr) 
34    else:
35        # Try simply importing local_config
36        import local_config as config
37except:
38    # Didn't find local config, load the default
39    import config
40 
41extension_list = []
42if config.APPLICATION_STATE_EXTENSION is not None:
43    extension_list.append(config.APPLICATION_STATE_EXTENSION)
44EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS + extension_list   
45PLUGINS_WLIST = config.PLUGINS_WLIST
46APPLICATION_WLIST = config.APPLICATION_WLIST
47
48PANEL_WIDTH = 235
49PANEL_HEIGHT = 700
50CBOX_WIDTH = 140
51BUTTON_WIDTH = 80
52STYLE_FLAG =wx.RAISED_BORDER|CT.TR_HAS_BUTTONS| CT.TR_HIDE_ROOT|\
53                    wx.WANTS_CHARS|CT.TR_HAS_VARIABLE_ROW_HEIGHT
54                   
55                   
56class DataTreeCtrl(CT.CustomTreeCtrl):
57    """
58    Check list control to be used for Data Panel
59    """
60    def __init__(self, parent,*args, **kwds):
61        #agwstyle is introduced in wx.2.8.11 but is not working for mac
62        if sys.platform.count("darwin") != 0:
63            try:
64                kwds['style'] = STYLE_FLAG
65                CT.CustomTreeCtrl.__init__(self, parent, *args, **kwds)
66            except:
67                del kwds['style']
68                CT.CustomTreeCtrl.__init__(self, parent, *args, **kwds)
69        else:
70            #agwstyle is introduced in wx.2.8.11 .argument working only for windows
71            try:
72                kwds['agwStyle'] = STYLE_FLAG
73                CT.CustomTreeCtrl.__init__(self, parent, *args, **kwds)
74            except:
75                try:
76                    del kwds['agwStyle']
77                    kwds['style'] = STYLE_FLAG
78                    CT.CustomTreeCtrl.__init__(self, parent, *args, **kwds)
79                except:
80                    del kwds['style']
81                    CT.CustomTreeCtrl.__init__(self, parent, *args, **kwds)
82        self.root = self.AddRoot("Available Data")
83       
84class DataPanel(ScrolledPanel, PanelBase):
85    """
86    This panel displays data available in the application and widgets to
87    interact with data.
88    """
89    ## Internal name for the AUI manager
90    window_name = "Data Panel"
91    ## Title to appear on top of the window
92    window_caption = "Data Explorer"
93    #type of window
94    window_type = "Data Panel"
95    ## Flag to tell the GUI manager that this panel is not
96    #  tied to any perspective
97    #ALWAYS_ON = True
98    def __init__(self, parent, 
99                 list=None,
100                 size=(PANEL_WIDTH, PANEL_HEIGHT),
101                 list_of_perspective=None, manager=None, *args, **kwds):
102       
103        kwds['size']= size
104        kwds['style'] = STYLE_FLAG
105        ScrolledPanel.__init__(self, parent=parent, *args, **kwds)
106        PanelBase.__init__(self)
107        self.SetupScrolling()
108        self.loader = Loader() 
109        #Default location
110        self._default_save_location = None 
111        self.all_data1d = True
112        self.parent = parent
113        self.manager = manager
114        if list is None:
115            list = []
116        self.list_of_data = list
117        if list_of_perspective is None:
118            list_of_perspective = []
119        self.list_of_perspective = list_of_perspective
120        self.list_rb_perspectives= []
121        self.list_cb_data = {}
122        self.list_cb_theory = {}
123        self.tree_ctrl = None
124        self.tree_ctrl_theory = None
125        self.perspective_cbox = None
126       
127        self.owner = None
128        self.do_layout()
129        self.fill_cbox_analysis(self.list_of_perspective)
130        self.Bind(wx.EVT_SHOW, self.on_close_page)
131       
132       
133    def do_layout(self):
134        """
135        """
136        self.define_panel_structure()
137        self.layout_selection()
138        self.layout_data_list()
139        self.layout_button()
140        #self.layout_batch()
141   
142    def define_panel_structure(self):
143        """
144        Define the skeleton of the panel
145        """
146        w, h = self.parent.GetSize()
147        self.vbox  = wx.BoxSizer(wx.VERTICAL)
148        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
149        self.sizer1.SetMinSize((w/13, h*2/5))
150     
151        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
152        self.sizer3 = wx.FlexGridSizer(6, 2, 0, 0)
153        self.sizer4 = wx.BoxSizer(wx.HORIZONTAL)
154        self.sizer5 = wx.BoxSizer(wx.VERTICAL)
155       
156        self.vbox.Add(self.sizer5, 0, wx.EXPAND|wx.ALL,1)
157        self.vbox.Add(self.sizer1, 0, wx.EXPAND|wx.ALL,0)
158        self.vbox.Add(self.sizer2, 0, wx.EXPAND|wx.ALL,1)
159        self.vbox.Add(self.sizer3, 0, wx.EXPAND|wx.ALL,1)
160        self.vbox.Add(self.sizer4, 0, wx.EXPAND|wx.ALL,5)
161       
162        self.SetSizer(self.vbox)
163       
164    def layout_selection(self):
165        """
166        """
167        select_txt = wx.StaticText(self, -1, 'Selection Options')
168        select_txt.SetForegroundColour('blue')
169        self.selection_cbox = wx.ComboBox(self, -1, style=wx.CB_READONLY)
170        list_of_options = ['Select all Data',
171                            'Unselect all Data',
172                           'Select all Data 1D',
173                           'Unselect all Data 1D',
174                           'Select all Data 2D',
175                           'Unselect all Data 2D' ]
176        for option in list_of_options:
177            self.selection_cbox.Append(str(option))
178        self.selection_cbox.SetValue('Select all Data')
179        wx.EVT_COMBOBOX(self.selection_cbox,-1, self._on_selection_type)
180        self.sizer5.AddMany([(select_txt,0, wx.ALL,5),
181                            (self.selection_cbox,0, wx.ALL,5)])
182        self.enable_selection()
183       
184   
185    def _on_selection_type(self, event):
186        """
187        Select data according to patterns
188        """
189       
190        list_of_options = ['Select all Data',
191                            'Unselect all Data',
192                           'Select all Data 1D',
193                           'Unselect all Data 1D',
194                           'Select all Data 2D',
195                           'Unselect all Data 2D' ]
196        option = self.selection_cbox.GetValue()
197       
198        pos = self.selection_cbox.GetSelection()
199        if pos == wx.NOT_FOUND:
200            return 
201        option = self.selection_cbox.GetString(pos)
202        for item in self.list_cb_data.values():
203            data_ctrl, _, _, _,_, _ = item
204            data_id, data_class, _ = self.tree_ctrl.GetItemPyData(data_ctrl) 
205            if option == 'Select all Data':
206                self.tree_ctrl.CheckItem(data_ctrl, True) 
207            elif option == 'Unselect all Data':
208                self.tree_ctrl.CheckItem(data_ctrl, False)
209            elif option == 'Select all Data 1D':
210                if data_class == 'Data1D':
211                    self.tree_ctrl.CheckItem(data_ctrl, True) 
212            elif option == 'Unselect all Data 1D':
213                if data_class == 'Data1D':
214                    self.tree_ctrl.CheckItem(data_ctrl, False) 
215            elif option == 'Select all Data 1D':
216                if data_class == 'Data1D':
217                    self.tree_ctrl.CheckItem(data_ctrl, True) 
218            elif option == 'Select all Data 2D':
219                if data_class == 'Data2D':
220                    self.tree_ctrl.CheckItem(data_ctrl, True) 
221            elif option == 'Unselect all Data 2D':
222                if data_class == 'Data2D':
223                    self.tree_ctrl.CheckItem(data_ctrl, False) 
224        self.enable_append()
225        self.enable_freeze()
226        self.enable_plot()
227        self.enable_import()
228        self.enable_remove()
229               
230    def layout_button(self):
231        """
232        Layout widgets related to buttons
233        """
234        w, _ = self.GetSize()
235        self.bt_add = wx.Button(self, wx.NewId(), "Load Data", 
236                                size=(BUTTON_WIDTH, -1))
237        self.bt_add.SetToolTipString("Load data files")
238        wx.EVT_BUTTON(self, self.bt_add.GetId(), self._load_data)
239        self.bt_remove = wx.Button(self, wx.NewId(), "Remove Data",
240         size=(BUTTON_WIDTH, -1))
241        self.bt_remove.SetToolTipString("Remove data from the application")
242        wx.EVT_BUTTON(self, self.bt_remove.GetId(), self.on_remove)
243        self.bt_import = wx.Button(self, wx.NewId(), "Send To",
244                                    size=(BUTTON_WIDTH, -1))
245        self.bt_import.SetToolTipString("Send set of Data to active perspective")
246        wx.EVT_BUTTON(self, self.bt_import.GetId(), self.on_import)
247        self.perspective_cbox = wx.ComboBox(self, -1, size=(CBOX_WIDTH, -1),
248                                style=wx.CB_READONLY)
249        wx.EVT_COMBOBOX(self.perspective_cbox,-1, 
250                        self._on_perspective_selection)
251   
252        self.bt_append_plot = wx.Button(self, wx.NewId(), "Append Plot To",
253                                        size=(BUTTON_WIDTH, -1))
254        self.bt_append_plot.SetToolTipString("Plot the selected data in the active panel")
255        wx.EVT_BUTTON(self, self.bt_append_plot.GetId(), self.on_append_plot)
256       
257        self.bt_plot = wx.Button(self, wx.NewId(), "New Plot", 
258                                 size=(BUTTON_WIDTH, -1))
259        self.bt_plot.SetToolTipString("To trigger plotting")
260        wx.EVT_BUTTON(self, self.bt_plot.GetId(), self.on_plot)
261       
262        self.bt_freeze = wx.Button(self, wx.NewId(), "Freeze Theory", 
263                                   size=(BUTTON_WIDTH, -1))
264        self.bt_freeze.SetToolTipString("To trigger freeze a theory")
265        wx.EVT_BUTTON(self, self.bt_freeze.GetId(), self.on_freeze)
266       
267        self.cb_plotpanel = wx.ComboBox(self, -1, size=(CBOX_WIDTH, -1),
268                                style=wx.CB_READONLY|wx.CB_SORT)
269        wx.EVT_COMBOBOX(self.cb_plotpanel,-1, self._on_plot_selection)
270        self.cb_plotpanel.Disable()
271
272        self.sizer3.AddMany([(self.bt_add),
273                             ((10, 10)),
274                             (self.bt_remove),
275                             ((10, 10)),
276                             (self.bt_import, 0, wx.EXPAND|wx.RIGHT, 5),
277                              (self.perspective_cbox, wx.EXPAND),
278                              (self.bt_append_plot),
279                              (self.cb_plotpanel, wx.EXPAND),
280                              (self.bt_plot),
281                              ((10, 10)),
282                              (self.bt_freeze),
283                              ((10, 10))])
284
285        self.sizer3.AddGrowableCol(1, 1)
286
287        self.enable_remove()
288        self.enable_import()
289        self.enable_plot()
290        self.enable_append()
291        self.enable_freeze()
292       
293    def layout_batch(self):
294        """
295        """
296        self.rb_single_mode = wx.RadioButton(self, -1, 'Single Mode',
297                                             style=wx.RB_GROUP)
298        self.rb_batch_mode = wx.RadioButton(self, -1, 'Batch Mode')
299       
300        self.rb_single_mode.SetValue(True)
301        self.rb_batch_mode.SetValue(False)
302        self.sizer4.AddMany([(self.rb_single_mode,0, wx.ALL,5),
303                            (self.rb_batch_mode,0, wx.ALL,5)])
304     
305    def layout_data_list(self):
306        """
307        Add a listcrtl in the panel
308        """
309        tree_ctrl_label = wx.StaticText(self, -1, "Data")
310        tree_ctrl_label.SetForegroundColour('blue')
311        self.tree_ctrl = DataTreeCtrl(parent=self)
312        self.tree_ctrl.Bind(CT.EVT_TREE_ITEM_CHECKING, self.on_check_item)
313        tree_ctrl_theory_label = wx.StaticText(self, -1, "Theory")
314        tree_ctrl_theory_label.SetForegroundColour('blue')
315        self.tree_ctrl_theory = DataTreeCtrl(parent=self)
316        self.tree_ctrl_theory.Bind(CT.EVT_TREE_ITEM_CHECKING, self.on_check_item)
317        self.sizer1.Add(tree_ctrl_label, 0, wx.LEFT, 10)
318        self.sizer1.Add(self.tree_ctrl, 1, wx.EXPAND|wx.ALL, 10)
319        self.sizer1.Add(tree_ctrl_theory_label, 0,  wx.LEFT, 10)
320        self.sizer1.Add(self.tree_ctrl_theory, 1, wx.EXPAND|wx.ALL, 10)
321           
322    def onContextMenu(self, event): 
323        """
324        Retrieve the state selected state
325        """
326        # Skipping the save state functionality for release 0.9.0
327        #return
328        pos = event.GetPosition()
329        pos = self.ScreenToClient(pos)
330        self.PopupMenu(self.popUpMenu, pos) 
331     
332 
333    def on_check_item(self, event):
334        """
335        """
336        item = event.GetItem()
337        item.Check(not item.IsChecked()) 
338        self.enable_append()
339        self.enable_freeze()
340        self.enable_plot()
341        self.enable_import()
342        self.enable_remove()
343        event.Skip()
344       
345    def fill_cbox_analysis(self, plugin):
346        """
347        fill the combobox with analysis name
348        """
349        self.list_of_perspective = plugin
350        if self.parent is None or \
351            not hasattr(self.parent, "get_current_perspective") or \
352            len(self.list_of_perspective) == 0:
353            return
354        if self.parent is not None and self.perspective_cbox  is not None:
355            for plug in self.list_of_perspective:
356                if plug.get_perspective():
357                    self.perspective_cbox.Append(plug.sub_menu, plug)
358           
359            curr_pers = self.parent.get_current_perspective()
360            self.perspective_cbox.SetStringSelection(curr_pers.sub_menu)
361        self.enable_import()
362                       
363    def load_data_list(self, list):
364        """
365        add need data with its theory under the tree
366        """
367        if list:
368            for state_id, dstate in list.iteritems():
369                data = dstate.get_data()
370                theory_list = dstate.get_theory()
371                if data is not None:
372                    data_name = data.name
373                    data_class = data.__class__.__name__
374                    path = dstate.get_path() 
375                    process_list = data.process
376                    data_id = data.id
377                   
378                    if state_id not in self.list_cb_data:
379                        #new state
380                        data_c = self.tree_ctrl.InsertItem(self.tree_ctrl.root,0,
381                                                           data_name, ct_type=1, 
382                                             data=(data_id, data_class, state_id))
383                        data_c.Check(True)
384                        d_i_c = self.tree_ctrl.AppendItem(data_c, 'Info')
385                        i_c_c = self.tree_ctrl.AppendItem(d_i_c, 
386                                                      'Type: %s' % data_class)
387                        p_c_c = self.tree_ctrl.AppendItem(d_i_c,
388                                                      'Path: %s' % str(path))
389                        d_p_c = self.tree_ctrl.AppendItem(d_i_c, 'Process')
390                       
391                        for process in process_list:
392                            i_t_c = self.tree_ctrl.AppendItem(d_p_c,
393                                                              process.__str__())
394                        theory_child = self.tree_ctrl.AppendItem(data_c, "THEORIES")
395                       
396                        self.list_cb_data[state_id] = [data_c, 
397                                                       d_i_c,
398                                                       i_c_c,
399                                                        p_c_c,
400                                                         d_p_c,
401                                                         theory_child]
402                    else:
403                        data_ctrl_list =  self.list_cb_data[state_id]
404                        #This state is already display replace it contains
405                        data_c, d_i_c, i_c_c, p_c_c, d_p_c, t_c = data_ctrl_list
406                        self.tree_ctrl.SetItemText(data_c, data_name) 
407                        temp = (data_id, data_class, state_id)
408                        self.tree_ctrl.SetItemPyData(data_c, temp) 
409                        self.tree_ctrl.SetItemText(i_c_c, 'Type: %s' % data_class)
410                        self.tree_ctrl.SetItemText(p_c_c, 'Path: %s' % str(path)) 
411                        self.tree_ctrl.DeleteChildren(d_p_c) 
412                        for process in process_list:
413                            i_t_c = self.tree_ctrl.AppendItem(d_p_c,
414                                                              process.__str__())
415                self.append_theory(state_id, theory_list)
416        self.enable_remove()
417        self.enable_import()
418        self.enable_plot()
419        self.enable_freeze()
420        self.enable_selection()
421       
422    def _uncheck_all(self):
423        """
424        Uncheck all check boxes
425        """
426        for item in self.list_cb_data.values():
427            data_ctrl, _, _, _,_, _ = item
428            self.tree_ctrl.CheckItem(data_ctrl, False) 
429        self.enable_append()
430        self.enable_freeze()
431        self.enable_plot()
432        self.enable_import()
433        self.enable_remove()
434   
435    def append_theory(self, state_id, theory_list):
436        """
437        append theory object under data from a state of id = state_id
438        replace that theory if  already displayed
439        """
440        if not theory_list:
441            return 
442        if state_id not in self.list_cb_data.keys():
443            root = self.tree_ctrl_theory.root
444            tree = self.tree_ctrl_theory
445        else:
446            item = self.list_cb_data[state_id]
447            data_c, _, _, _, _, _ = item
448            root = data_c
449            tree = self.tree_ctrl
450        if root is not None:
451             self.append_theory_helper(tree=tree, root=root, 
452                                       state_id=state_id, 
453                                       theory_list=theory_list)
454     
455     
456    def append_theory_helper(self, tree, root, state_id, theory_list):
457        """
458        """
459        if state_id in self.list_cb_theory.keys():
460            #update current list of theory for this data
461            theory_list_ctrl = self.list_cb_theory[state_id]
462
463            for theory_id, item in theory_list.iteritems():
464                theory_data, theory_state = item
465                if theory_data is None:
466                    name = "Unknown"
467                    theory_class = "Unknown"
468                    theory_id = "Unknown"
469                    temp = (None, None, None)
470                else:
471                    name = theory_data.name
472                    theory_class = theory_data.__class__.__name__
473                    theory_id = theory_data.id
474                    #if theory_state is not None:
475                    #    name = theory_state.model.name
476                    temp = (theory_id, theory_class, state_id)
477                if theory_id not in theory_list_ctrl:
478                    #add new theory
479                    t_child = tree.AppendItem(root,
480                                                    name, ct_type=1, data=temp)
481                    t_i_c = tree.AppendItem(t_child, 'Info')
482                    i_c_c = tree.AppendItem(t_i_c, 
483                                                  'Type: %s' % theory_class)
484                    t_p_c = tree.AppendItem(t_i_c, 'Process')
485                   
486                    for process in theory_data.process:
487                        i_t_c = tree.AppendItem(t_p_c,
488                                                          process.__str__())
489                    theory_list_ctrl[theory_id] = [t_child, 
490                                                   i_c_c, 
491                                                   t_p_c]
492                else:
493                    #replace theory
494                    t_child, i_c_c, t_p_c = theory_list_ctrl[theory_id]
495                    tree.SetItemText(t_child, name) 
496                    tree.SetItemPyData(t_child, temp) 
497                    tree.SetItemText(i_c_c, 'Type: %s' % theory_class) 
498                    tree.DeleteChildren(t_p_c) 
499                    for process in theory_data.process:
500                        i_t_c = tree.AppendItem(t_p_c,
501                                                          process.__str__())
502             
503        else:
504            #data didn't have a theory associated it before
505            theory_list_ctrl = {}
506            for theory_id, item in theory_list.iteritems():
507                theory_data, theory_state = item
508                if theory_data is not None:
509                    name = theory_data.name
510                    theory_class = theory_data.__class__.__name__
511                    theory_id = theory_data.id
512                    #if theory_state is not None:
513                    #    name = theory_state.model.name
514                    temp = (theory_id, theory_class, state_id)
515                    t_child = tree.AppendItem(root,
516                            name, ct_type=1, 
517                            data=(theory_data.id, theory_class, state_id))
518                    t_i_c = tree.AppendItem(t_child, 'Info')
519                    i_c_c = tree.AppendItem(t_i_c, 
520                                                  'Type: %s' % theory_class)
521                    t_p_c = tree.AppendItem(t_i_c, 'Process')
522                   
523                    for process in theory_data.process:
524                        i_t_c = tree.AppendItem(t_p_c,
525                                                          process.__str__())
526           
527                    theory_list_ctrl[theory_id] = [t_child, i_c_c, t_p_c]
528                #self.list_cb_theory[data_id] = theory_list_ctrl
529                self.list_cb_theory[state_id] = theory_list_ctrl
530       
531           
532   
533    def set_data_helper(self):
534        """
535        """
536        data_to_plot = []
537        state_to_plot = []
538        theory_to_plot = []
539        for value in self.list_cb_data.values():
540            item, _, _, _, _, _ = value
541            if item.IsChecked():
542                data_id, _, state_id = self.tree_ctrl.GetItemPyData(item)
543                data_to_plot.append(data_id)
544                if state_id not in state_to_plot:
545                    state_to_plot.append(state_id)
546           
547        for theory_dict in self.list_cb_theory.values():
548            for key, value in theory_dict.iteritems():
549                item, _, _ = value
550                if item.IsChecked():
551                    theory_id, _, state_id = self.tree_ctrl.GetItemPyData(item)
552                    theory_to_plot.append(theory_id)
553                    if state_id not in state_to_plot:
554                        state_to_plot.append(state_id)
555        return data_to_plot, theory_to_plot, state_to_plot
556   
557    def remove_by_id(self, id):
558        """
559        """
560        for item in self.list_cb_data.values():
561            data_c, _, _, _, _, theory_child = item
562            data_id, _, state_id = self.tree_ctrl.GetItemPyData(data_c) 
563            if id == data_id:
564                self.tree_ctrl.Delete(data_c)
565                del self.list_cb_data[state_id]
566                del self.list_cb_theory[data_id]
567             
568    def load_error(self, error=None):
569        """
570        Pop up an error message.
571       
572        :param error: details error message to be displayed
573        """
574        if error is not None or str(error).strip() != "":
575            dial = wx.MessageDialog(self.parent, str(error), 'Error Loading File',
576                                wx.OK | wx.ICON_EXCLAMATION)
577            dial.ShowModal() 
578       
579    def _load_data(self, event):
580        """
581        Load data
582        """
583        path = None
584        if self._default_save_location == None:
585            self._default_save_location = os.getcwd()
586       
587        cards = self.loader.get_wildcards()
588        temp = [APPLICATION_WLIST] + PLUGINS_WLIST
589        for item in temp:
590            if item in cards:
591                cards.remove(item)
592        wlist =  '|'.join(cards)
593        style = wx.OPEN|wx.FD_MULTIPLE
594        dlg = wx.FileDialog(self.parent, 
595                            "Choose a file", 
596                            self._default_save_location, "",
597                             wlist,
598                             style=style)
599        if dlg.ShowModal() == wx.ID_OK:
600            file_list = dlg.GetPaths()
601            if len(file_list) >= 0 and not(file_list[0]is None):
602                self._default_save_location = os.path.dirname(file_list[0])
603                path = self._default_save_location
604        dlg.Destroy()
605       
606        if path is None or not file_list or file_list[0] is None:
607            return
608        self._get_data(file_list)
609       
610    def _get_data(self, path, format=None):
611        """
612        """
613        message = ""
614        log_msg = ''
615        output = {}
616        error_message = ""
617        for p_file in path:
618            basename  = os.path.basename(p_file)
619            root, extension = os.path.splitext(basename)
620            if extension.lower() in EXTENSIONS:
621                log_msg = "Data Loader cannot "
622                log_msg += "load: %s\n" % str(p_file)
623                log_msg += """Please try to open that file from "open project" """
624                log_msg += """or "open analysis" menu\n"""
625                error_message = log_msg + "\n"
626                logging.info(log_msg)
627                continue
628       
629            try:
630                temp =  self.loader.load(p_file, format)
631                if temp.__class__.__name__ == "list":
632                    for item in temp:
633                        data = self.parent.create_gui_data(item, p_file)
634                        output[data.id] = data
635                else:
636                    data = self.parent.create_gui_data(temp, p_file)
637                    output[data.id] = data
638                message = "Loading Data..." + str(p_file) + "\n"
639                self.load_update(output=output, message=message)
640            except:
641                error = "Error while loading Data: %s\n" % str(p_file)
642                error += str(sys.exc_value) + "\n"
643                error_message = "The data file you selected could not be loaded.\n"
644                error_message += "Make sure the content of your file"
645                error_message += " is properly formatted.\n\n"
646                error_message += "When contacting the DANSE team, mention the"
647                error_message += " following:\n%s" % str(error)
648                self.load_update(output=output, message=error_message)
649               
650        message = "Loading Data Complete! "
651        message += log_msg
652        self.load_complete(output=output, error_message=error_message,
653                       message=message, path=path)
654           
655    def load_update(self, output=None, message=""):
656        """
657        print update on the status bar
658        """
659        if message != "" and self.parent is not None:
660            wx.PostEvent(self.parent, StatusEvent(status=message,
661                                                  type="progress",
662                                                   info="warning"))
663           
664    def load_complete(self, output, message="", error_message="", path=None):
665        """
666         post message to  status bar and return list of data
667        """
668        if self.parent is not None:
669            wx.PostEvent(self.parent, StatusEvent(status=message,
670                                              info="warning",
671                                              type="stop"))
672        if error_message != "":
673            self.load_error(error_message)
674        if self.parent is not None:
675            self.parent.add_data(data_list=output)
676           
677    def on_remove(self, event):
678        """
679        Get a list of item checked and remove them from the treectrl
680        Ask the parent to remove reference to this item
681        """
682        data_to_remove, theory_to_remove, _ = self.set_data_helper()
683        data_key = []
684        theory_key = []
685        #remove  data from treectrl
686        for d_key, item in self.list_cb_data.iteritems():
687            data_c, d_i_c, i_c_c, p_c_c, d_p_c, t_c = item
688            if data_c.IsChecked():
689                self.tree_ctrl.Delete(data_c)
690                data_key.append(d_key)
691                if d_key in self.list_cb_theory.keys():
692                    theory_list_ctrl = self.list_cb_theory[d_key]
693                    theory_to_remove += theory_list_ctrl.keys()
694        # Remove theory from treectrl       
695        for t_key, theory_dict in self.list_cb_theory.iteritems():
696            for  key, value in theory_dict.iteritems():
697                item, _, _ = value
698                if item.IsChecked():
699                    self.tree_ctrl.Delete(item)
700                    theory_key.append(key)
701        #Remove data and related theory references
702        for key in data_key:
703            del self.list_cb_data[key]
704            if key in theory_key:
705                del self.list_cb_theory[key]
706        #remove theory  references independently of data
707        for key in theory_key:
708            for t_key, theory_dict in self.list_cb_theory.iteritems():
709                if key in theory_dict:
710                    del theory_dict[key]
711           
712        self.parent.remove_data(data_id=data_to_remove,
713                                  theory_id=theory_to_remove)
714        self.enable_remove()
715        self.enable_freeze()
716       
717    def on_import(self, event=None):
718        """
719        Get all select data and set them to the current active perspetive
720        """
721        data_id, theory_id, state_id = self.set_data_helper()
722        temp = data_id + state_id
723        self.parent.set_data(data_id=temp, theory_id=theory_id)
724       
725    def on_append_plot(self, event=None):
726        """
727        append plot to plot panel on focus
728        """
729        self._on_plot_selection()
730        data_id, theory_id, state_id = self.set_data_helper()
731        self.parent.plot_data(data_id=data_id, 
732                              state_id=state_id,
733                              theory_id=theory_id,
734                              append=True)
735   
736    def on_plot(self, event=None):
737        """
738        Send a list of data names to plot
739        """
740        data_id, theory_id, state_id = self.set_data_helper()
741        self.parent.plot_data(data_id=data_id, 
742                              state_id=state_id,
743                              theory_id=theory_id,
744                              append=False)
745         
746    def on_close_page(self, event=None):
747        """
748        On close
749        """
750        if event != None:
751            event.Skip()
752        # send parent to update menu with no show nor hide action
753        self.parent.show_data_panel(action=False)
754   
755    def on_freeze(self, event):
756        """
757        """
758        _, theory_id, state_id = self.set_data_helper()
759        self.parent.freeze(data_id=state_id, theory_id=theory_id)
760       
761    def set_active_perspective(self, name):
762        """
763        set the active perspective
764        """
765        self.perspective_cbox.SetStringSelection(name)
766        self.enable_import()
767       
768    def set_panel_on_focus(self, name=None):
769        """
770        set the plot panel on focus
771        """
772        for key, value in self.parent.plot_panels.iteritems():
773            name_plot_panel = str(value.window_caption)
774            if name_plot_panel not in self.cb_plotpanel.GetItems():
775                self.cb_plotpanel.Append(name_plot_panel, value)
776            if name != None and name == name_plot_panel:
777                self.cb_plotpanel.SetStringSelection(name_plot_panel)
778                break
779        self.enable_append()
780       
781    def _on_perspective_selection(self, event=None):
782        """
783        select the current perspective for guiframe
784        """
785        selection = self.perspective_cbox.GetSelection()
786
787        if self.perspective_cbox.GetValue() != 'None':
788            perspective = self.perspective_cbox.GetClientData(selection)
789            perspective.on_perspective(event=None)
790       
791    def _on_plot_selection(self, event = None):
792        """
793        On source combobox selection
794        """
795        if event != None:
796            combo = event.GetEventObject()
797            event.Skip()
798        else:
799            combo = self.cb_plotpanel
800        selection = combo.GetSelection()
801
802        if combo.GetValue() != 'None':
803            panel = combo.GetClientData(selection)
804            self.parent.on_set_plot_focus(panel)   
805           
806    def enable_remove(self):
807        """
808        enable or disable remove button
809        """
810        n_t = self.tree_ctrl.GetCount()
811        n_t_t = self.tree_ctrl_theory.GetCount()
812        if n_t + n_t_t <= 0:
813            self.bt_remove.Disable()
814        else:
815            self.bt_remove.Enable()
816           
817    def enable_import(self):
818        """
819        enable or disable send button
820        """
821        n_t = 0
822        if self.tree_ctrl != None:
823            n_t = self.tree_ctrl.GetCount()
824        if n_t > 0 and len(self.list_of_perspective) > 0:
825            self.bt_import.Enable()
826        else:
827            self.bt_import.Disable()
828        if len(self.list_of_perspective) <= 0 or \
829            self.perspective_cbox.GetValue()  in ["None",
830                                                "No Active Application"]:
831            self.perspective_cbox.Disable()
832        else:
833            self.perspective_cbox.Enable()
834           
835    def enable_plot(self):
836        """
837        enable or disable plot button
838        """
839        n_t = 0 
840        n_t_t = 0
841        if self.tree_ctrl != None:
842            n_t = self.tree_ctrl.GetCount()
843        if self.tree_ctrl_theory != None:
844            n_t_t = self.tree_ctrl_theory.GetCount()
845        if n_t + n_t_t <= 0:
846            self.bt_plot.Disable()
847        else:
848            self.bt_plot.Enable()
849        self.enable_append()
850       
851    def enable_append(self):
852        """
853        enable or disable append button
854        """
855        n_t = 0 
856        n_t_t = 0
857        if self.tree_ctrl != None:
858            n_t = self.tree_ctrl.GetCount()
859        if self.tree_ctrl_theory != None:
860            n_t_t = self.tree_ctrl_theory.GetCount()
861        if n_t + n_t_t <= 0: 
862            self.bt_append_plot.Disable()
863            self.cb_plotpanel.Disable()
864        elif self.cb_plotpanel.GetCount() <= 0:
865                self.cb_plotpanel.Disable()
866                self.bt_append_plot.Disable()
867        else:
868            self.bt_append_plot.Enable()
869            self.cb_plotpanel.Enable()
870           
871    def check_theory_to_freeze(self):
872        """
873        """
874    def enable_freeze(self):
875        """
876        enable or disable the freeze button
877        """
878        n_t_t = 0
879        n_l = 0
880        if self.tree_ctrl_theory != None:
881            n_t_t = self.tree_ctrl_theory.GetCount()
882        n_l = len(self.list_cb_theory)
883        if (n_t_t + n_l > 0):
884            self.bt_freeze.Enable()
885        else:
886            self.bt_freeze.Disable()
887       
888    def enable_selection(self):
889        """
890        enable or disable combobo box selection
891        """
892        n_t = 0
893        n_t_t = 0
894        if self.tree_ctrl != None:
895            n_t = self.tree_ctrl.GetCount()
896        if self.tree_ctrl_theory != None:
897            n_t_t = self.tree_ctrl_theory.GetCount()
898        if n_t + n_t_t > 0 and self.selection_cbox != None:
899            self.selection_cbox.Enable()
900        else:
901            self.selection_cbox.Disable()
902           
903           
904       
905class DataFrame(wx.Frame):
906    ## Internal name for the AUI manager
907    window_name = "Data Panel"
908    ## Title to appear on top of the window
909    window_caption = "Data Panel"
910    ## Flag to tell the GUI manager that this panel is not
911    #  tied to any perspective
912    ALWAYS_ON = True
913   
914    def __init__(self, parent=None, owner=None, manager=None,size=(300, 800),
915                         list_of_perspective=[],list=[], *args, **kwds):
916        kwds['size'] = size
917        kwds['id'] = -1
918        kwds['title']= "Loaded Data"
919        wx.Frame.__init__(self, parent=parent, *args, **kwds)
920        self.parent = parent
921        self.owner = owner
922        self.manager = manager
923        self.panel = DataPanel(parent=self, 
924                               #size=size,
925                               list_of_perspective=list_of_perspective)
926     
927    def load_data_list(self, list=[]):
928        """
929        Fill the list inside its panel
930        """
931        self.panel.load_data_list(list=list)
932       
933   
934   
935from dataFitting import Data1D
936from dataFitting import Data2D, Theory1D
937from data_state import DataState
938import sys
939class State():
940    def __init__(self):
941        self.msg = ""
942    def __str__(self):
943        self.msg = "model mane : model1\n"
944        self.msg += "params : \n"
945        self.msg += "name  value\n"
946        return msg
947def set_data_state(data=None, path=None, theory=None, state=None):
948    dstate = DataState(data=data)
949    dstate.set_path(path=path)
950    dstate.set_theory(theory, state)
951 
952    return dstate
953"""'
954data_list = [1:('Data1', 'Data1D', '07/01/2010', "theory1d", "state1"),
955            ('Data2', 'Data2D', '07/03/2011', "theory2d", "state1"),
956            ('Data3', 'Theory1D', '06/01/2010', "theory1d", "state1"),
957            ('Data4', 'Theory2D', '07/01/2010', "theory2d", "state1"),
958            ('Data5', 'Theory2D', '07/02/2010', "theory2d", "state1")]
959"""     
960if __name__ == "__main__":
961   
962    app = wx.App()
963    try:
964        list_of_perspective = [('perspective2', False), ('perspective1', True)]
965        data_list = {}
966        # state 1
967        data = Data2D()
968        data.name = "data2"
969        data.id = 1
970        data.append_empty_process()
971        process = data.process[len(data.process)-1]
972        process.data = "07/01/2010"
973        theory = Data2D()
974        theory.id = 34
975        theory.name = "theory1"
976        path = "path1"
977        state = State()
978        data_list['1']=set_data_state(data, path,theory, state)
979        #state 2
980        data = Data2D()
981        data.name = "data2"
982        data.id = 76
983        theory = Data2D()
984        theory.id = 78
985        theory.name = "CoreShell 07/24/25"
986        path = "path2"
987        #state3
988        state = State()
989        data_list['2']=set_data_state(data, path,theory, state)
990        data = Data1D()
991        data.id = 3
992        data.name = "data2"
993        theory = Theory1D()
994        theory.name = "CoreShell"
995        theory.id = 4
996        theory.append_empty_process()
997        process = theory.process[len(theory.process)-1]
998        process.description = "this is my description"
999        path = "path3"
1000        data.append_empty_process()
1001        process = data.process[len(data.process)-1]
1002        process.data = "07/22/2010"
1003        data_list['4']=set_data_state(data, path,theory, state)
1004        #state 4
1005        temp_data_list = {}
1006        data.name = "data5 erasing data2"
1007        temp_data_list['4'] = set_data_state(data, path,theory, state)
1008        #state 5
1009        data = Data2D()
1010        data.name = "data3"
1011        data.id = 5
1012        data.append_empty_process()
1013        process = data.process[len(data.process)-1]
1014        process.data = "07/01/2010"
1015        theory = Theory1D()
1016        theory.name = "Cylinder"
1017        path = "path2"
1018        state = State()
1019        dstate= set_data_state(data, path,theory, state)
1020        theory = Theory1D()
1021        theory.id = 6
1022        theory.name = "CoreShell"
1023        dstate.set_theory(theory)
1024        theory = Theory1D()
1025        theory.id = 6
1026        theory.name = "CoreShell replacing coreshell in data3"
1027        dstate.set_theory(theory)
1028        data_list['3'] = dstate
1029        #state 6
1030        data_list['6']=set_data_state(None, path,theory, state)
1031        data_list['6']=set_data_state(theory=theory, state=None)
1032        theory = Theory1D()
1033        theory.id = 7
1034        data_list['6']=set_data_state(theory=theory, state=None)
1035        data_list['7']=set_data_state(theory=theory, state=None)
1036        window = DataFrame(list=data_list)
1037        window.load_data_list(list=data_list)
1038        window.Show(True)
1039        window.load_data_list(list=temp_data_list)
1040    except:
1041        #raise
1042        print "error",sys.exc_value
1043       
1044    app.MainLoop() 
1045   
1046   
Note: See TracBrowser for help on using the repository browser.