source: sasview/sansguiframe/src/sans/guiframe/data_panel.py @ 6a7cf2c

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

removed res plot too on delete data

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