UserDefinitionWidget.py 4.42 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
    
    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)
        
60
61
        self.SetSize(self._plugin.GetSize())
        
62
63
        self._plugin.set_trajectory(trajectory)
                
64
        PUBLISHER.sendMessage("msg_set_data", message=self._plugin)
65
66
67
68
69
70
        
    @property
    def plugin(self):
        
        return self._plugin

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

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

86
        PUBLISHER.subscribe(self.msg_set_ud, "msg_set_ud")
87

88
89
        self.Bind(wx.EVT_BUTTON, self.on_view_definition, viewUD)
        self.Bind(wx.EVT_BUTTON, self.on_new_definition, newUD)
90
91

        return sizer
92
93
94

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

132
    def msg_set_ud(self,message):
133
         
134
        uds = UD_STORE.filter(self._basename, self._type)
135
136

        currentSelection = self._availableUDs.GetStringSelection()
137
        
138
        self._availableUDs.SetItems(uds)
139
140
141
        
        if currentSelection:
            self._availableUDs.SetStringSelection(currentSelection)