source: sasview/guiframe/data_panel.py @ 228a8bb

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 228a8bb was 228a8bb, checked in by Jae Cho <jhjcho@…>, 13 years ago

more accurate version checking

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