source: sasview/test/test_rename_guiframe/data_panel.py @ 6b33ffc

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

rename

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