UserDefinitionWidget.py 6.15 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.Externals.pubsub import pub
41
from MDANSE.Framework.Plugins.ComponentPlugin import ComponentPlugin
42
from MDANSE.Framework.UserDefinitionStore import UD_STORE
43
44
45
from MDANSE.Framework.Widgets.IWidget import IWidget
from MDANSE.GUI import DATA_CONTROLLER

46
class UserDefinitionDialog(wx.Dialog):
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    
    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)
                
        pub.sendMessage("msg_set_data", plugin=self._plugin)
        
        self.Bind(wx.EVT_CLOSE, self.on_quit)

    def on_quit(self, event):
        
        d = wx.MessageDialog(None,'Do you really want to quit ?','Question',wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
        if d.ShowModal() == wx.ID_YES:
            self.Destroy()           

    @property
    def plugin(self):
        
        return self._plugin

77
78
79
80
81
class UserDefinitionWidget(IWidget):
    
    __metaclass__ = abc.ABCMeta
    
    type = None    
82
                
83
84
85
86
87
    def add_widgets(self):

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        
        self._availableUDs = wx.Choice(self._widgetPanel, wx.ID_ANY,style=wx.CB_SORT)
88
89
        viewUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="View selected definition")
        newUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="New definition")
90
        sizer.Add(self._availableUDs, 1, wx.ALL|wx.EXPAND, 5)
91
92
        sizer.Add(viewUD, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(newUD, 0, wx.ALL|wx.EXPAND, 5)
93

94
        pub.subscribe(self.msg_set_ud, "msg_set_ud")
95

96
97
        self.Bind(wx.EVT_BUTTON, self.on_view_definition, viewUD)
        self.Bind(wx.EVT_BUTTON, self.on_new_definition, newUD)
98
99
100

        return sizer
    
101
    def on_view_definition(self,event):
102
        
103
104
        viewUD = event.GetEventObject()
        ud = viewUD.Parent.GetChildren()[0].GetStringSelection()       
105
106
107
108
        if not ud:
            LOGGER("Please select a user definition","error",["dialog"])
            return
        
109
        from MDANSE.GUI.UserDefinitionViewer import UserDefinitionViewer
110
        
111
        f = UserDefinitionViewer(self,ud=[self._basename,self.type,ud])
112
113
114
        
        f.Show()
    
115
    def on_new_definition(self,event):
116
        
117
        dlg = UserDefinitionDialog(None,self._trajectory,self.type)
118
119
120
        
        dlg.ShowModal()
        
121
122
123
124
125
126
127
128
129
130
131
132
    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)
        
133
        self.msg_set_ud()
134

135
    def msg_set_ud(self):
136
         
137
        uds = UD_STORE.filter(self._basename, self.type)
138
        
139
        self._availableUDs.SetItems(uds)
140

141
class UserDefinitionPlugin(ComponentPlugin):
142
143
    
    category = ('User definition',)
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    
    def __init__(self,parent,*args,**kwargs):
        
        ComponentPlugin.__init__(self,parent,size=(800,500))
        
        self.add_ud_panel()
                
    def add_ud_panel(self):

        udPanel = wx.Panel(self._mainPanel,wx.ID_ANY)
                
        sb = wx.StaticBox(udPanel, wx.ID_ANY)
        actionsSizer = wx.StaticBoxSizer(sb, wx.HORIZONTAL)
                        
        self._udName = wx.TextCtrl(udPanel, wx.ID_ANY, style = wx.TE_PROCESS_ENTER)
159
        saveButton  = wx.Button(udPanel, wx.ID_ANY, label="Set")
160
161
162
163
164
165
166
167
        
        actionsSizer.Add(self._udName, 1, wx.ALL|wx.EXPAND, 5)
        actionsSizer.Add(saveButton, 0, wx.ALL, 5)
        
        udPanel.SetSizer(actionsSizer)
        
        self._mainPanel.GetSizer().Add(udPanel,0,wx.EXPAND|wx.ALL,5)

168
        self.Bind(wx.EVT_BUTTON, self.on_set_ud, saveButton)
169

170
    def on_set_ud(self, event):
171
172
173
174

        name = str(self._udName.GetValue().strip())
        
        if not name:
175
            LOGGER('Empty user definition name.','error',['console'])
176
177
178
179
            return

        value = self.validate()        
        if value is None:
180
181
182
            return
        
        udType = getattr(self,'udType','type')
183
184
                
        if UD_STORE.has_definition(self._target,self.type,name):
185
            LOGGER('There is already a user-definition that matches %s,%s,%s' % (self._target,udType,name),'error',['console'])
186
187
188
189
            return
                  
        UD_STORE.set_definition(self._target,self.type,name,value)
                 
190
        pub.sendMessage("msg_set_ud")
191

192
        LOGGER('User definition %r successfully set.' % name,'info',['console'])