source: sasview/guiframe/data_panel.py @ 70114ff9

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 70114ff9 was 603fb0f, checked in by Jae Cho <jhjcho@…>, 13 years ago

more constrained path string

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