source: sasview/guiframe/data_panel.py @ 55a3257

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

change size of combobox

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