source: sasview/sansguiframe/src/sans/guiframe/data_panel.py @ 03a8b16

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 03a8b16 was 66f7016, checked in by Jae Cho <jhjcho@…>, 13 years ago

better size for mac

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