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

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

data_panel: fixed clean build bug, importing local_config

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