source: sasview/guiframe/data_panel.py @ b461b8fe

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

add switch button to data_panel

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