UserDefinitionWidget.py 4.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Mar 30, 2015

:author: Eric C. Pellegrini
'''

import abc
import os

import wx
37
import wx.aui as wxaui
38

39
from MDANSE import LOGGER, REGISTRY
40
from MDANSE.Framework.UserDefinitionStore import UD_STORE
41

42
43
from MDANSE.GUI import PUBLISHER
from MDANSE.GUI.DataController import DATA_CONTROLLER
44
from MDANSE.GUI.Widgets.IWidget import IWidget
45

46
class UserDefinitionDialog(wx.Dialog):
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    
    def __init__(self,parent,trajectory,udType,*args,**kwargs):

        wx.Dialog.__init__(self, parent, style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX)
        
        self._mgr = wxaui.AuiManager(self)

        self.datakey = trajectory.filename
         
        self._plugin = REGISTRY['plugin'][udType](self,*args,**kwargs)
        
        self.SetTitle(self._plugin.label)
        
        self._plugin.set_trajectory(trajectory)
                
62
        PUBLISHER.sendMessage("msg_set_data", message=self._plugin)
63
64
65
66
67
68
        
    @property
    def plugin(self):
        
        return self._plugin

69
70
71
class UserDefinitionWidget(IWidget):
    
    __metaclass__ = abc.ABCMeta
72
                    
73
74
75
76
77
    def add_widgets(self):

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        
        self._availableUDs = wx.Choice(self._widgetPanel, wx.ID_ANY,style=wx.CB_SORT)
78
79
        viewUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="View selected definition")
        newUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="New definition")
80
        sizer.Add(self._availableUDs, 1, wx.ALL|wx.EXPAND, 5)
81
82
        sizer.Add(viewUD, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(newUD, 0, wx.ALL|wx.EXPAND, 5)
83

84
        PUBLISHER.subscribe(self.msg_set_ud, "msg_set_ud")
85

86
87
        self.Bind(wx.EVT_BUTTON, self.on_view_definition, viewUD)
        self.Bind(wx.EVT_BUTTON, self.on_new_definition, newUD)
88
89

        return sizer
90
91
92

    def OnDestroy(self,event):
        
93
        PUBLISHER.unsubscribe(self.msg_set_ud, 'msg_set_ud')
94
95
        IWidget.OnDestroy(self,event)
        
96
    def on_view_definition(self,event):
97
        
98
99
        viewUD = event.GetEventObject()
        ud = viewUD.Parent.GetChildren()[0].GetStringSelection()       
100
101
102
103
        if not ud:
            LOGGER("Please select a user definition","error",["dialog"])
            return
        
104
        from MDANSE.GUI.UserDefinitionViewer import UserDefinitionViewer
105
        
106
        f = UserDefinitionViewer(self,ud=[self._basename,self._type,ud])
107
108
109
        
        f.Show()
    
110
    def on_new_definition(self,event):
111
        
112
        dlg = UserDefinitionDialog(None,self._trajectory,self._type)
113
114
115
        
        dlg.ShowModal()
        
116
117
118
119
120
121
122
123
124
125
126
127
    def get_widget_value(self):
        
        return str(self._availableUDs.GetStringSelection())    

    def set_data(self, datakey):

        self._filename = datakey

        self._trajectory = DATA_CONTROLLER[datakey]

        self._basename = os.path.basename(self._filename)
        
128
        self.msg_set_ud(None)
129

130
    def msg_set_ud(self,message):
131
         
132
        uds = UD_STORE.filter(self._basename, self._type)
133
        
134
        self._availableUDs.SetItems(uds)