AtomSelectionWidget.py 5.14 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 os

import wx

37
38
from MDANSE.Framework.UserDefinitionStore import UD_STORE
from MDANSE.Framework.Widgets.UserDefinitionWidget import UserDefinitionDialog, UserDefinitionWidget
39
40

from MDANSE.GUI import PUBLISHER
41
from MDANSE.GUI.Icons import ICONS
42

43
class AtomSelectionWidget(UserDefinitionWidget):
44
         
45
    type = "atom_selection"
46

47
48
49
    def add_widgets(self):
 
        self._sizer = wx.BoxSizer(wx.VERTICAL)
50

51
52
        panel = wx.Panel(self._widgetPanel,wx.ID_ANY)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
53
        
54
55
56
57
        new = wx.Button(panel, wx.ID_ANY, label="Set new selection")
        new.SetToolTip(wx.ToolTip("Pop up selection dialog"))
        add = wx.BitmapButton(panel, wx.ID_ANY, ICONS["plus",16,16])
        add.SetToolTip(wx.ToolTip("Add a new line"))
58

59
60
        sizer.Add(new, 0, wx.ALL|wx.ALIGN_RIGHT, 5)
        sizer.Add(add, 0, wx.ALL|wx.ALIGN_RIGHT, 5)
61
        
62
        panel.SetSizer(sizer)
63
        
64
        self._sizer.Add(panel, 1, wx.ALL|wx.ALIGN_RIGHT, 5)
65
        
66
        self._choices = []
67

68
        PUBLISHER.subscribe(self.msg_set_ud, "msg_set_ud")
69

70
71
        self.Bind(wx.EVT_BUTTON, self.on_new_definition, new)
        self.Bind(wx.EVT_BUTTON, self.on_add_definition, add)
72
        
73
74
75
        return self._sizer
          
    def on_add_definition(self,event):
76
        
77
78
        panel = wx.Panel(self._widgetPanel,wx.ID_ANY)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
79

80
81
82
        availableUDs = wx.Choice(panel, wx.ID_ANY,style=wx.CB_SORT)
        uds = UD_STORE.filter(self._basename, "atom_selection")
        availableUDs.SetItems(uds)
83
        
84
85
        view = wx.Button(panel, wx.ID_ANY, label="View selected definition")
        remove = wx.BitmapButton(panel, wx.ID_ANY, ICONS["minus",16,16])
86

87
88
89
        sizer.Add(availableUDs, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(view, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(remove, 0, wx.ALL|wx.EXPAND, 5)
90
        
91
        panel.SetSizer(sizer)
92
        
93
        self._sizer.Add(panel,1,wx.ALL|wx.EXPAND,5)
94
        
95
        self._widgetPanel.GrandParent.Layout()
96

97
98
        self.Bind(wx.EVT_BUTTON, self.on_view_definition, view)
        self.Bind(wx.EVT_BUTTON, self.on_remove_definition, remove)
99

100
101
102
103
104
105
    def on_new_definition(self,event):
        
        dlg = UserDefinitionDialog(None,self._trajectory,'atom_selection')
        
        dlg.ShowModal()

106
    def on_remove_definition(self,event):
107
        
108
        self._sizer.Detach(event.GetEventObject().Parent)
109
        
110
        event.GetEventObject().Parent.Destroy()
111

112
        self._widgetPanel.GrandParent.Layout()
113

114
    def get_widget_value(self):
115

116
117
        sizerItemList = list(self._sizer.GetChildren())
        del sizerItemList[0]
118

119
120
121
122
123
        uds = []
        for sizerItem in sizerItemList:
            
            panel = sizerItem.GetWindow()
            children = panel.GetChildren()
124
125
            udName = children[0]            
            uds.append(udName.GetStringSelection())
126
127
                  
        if not uds:
128
129
            return None
        else:
130
131
132
            return uds

    def msg_set_ud(self):
133
         
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        uds = UD_STORE.filter(self._basename, self.type)
        
        sizerItemList = list(self._sizer.GetChildren())
        del sizerItemList[0]

        for sizerItem in sizerItemList:
            
            panel = sizerItem.GetWindow()
            children = panel.GetChildren()
            udName = children[0]
            oldSelection = udName.GetStringSelection()            
            udName.SetItems(uds)
            udName.SetStringSelection(oldSelection)
            
148
149
150
151
if __name__ == "__main__":
    
    from MMTK.Trajectory import Trajectory
    
152
153
154
    from MDANSE import PLATFORM
    
    t = Trajectory(None,os.path.join(PLATFORM.example_data_directory(),"Trajectories","MMTK","protein_in_periodic_universe.nc"),"r")
155
156
157
    
    app = wx.App(False)
    
158
    p = UserDefinitionDialog(None,t,'q_vectors')
159
160
161
162
163
164
165
        
    p.SetSize((800,800))
            
    p.ShowModal()
    
    app.MainLoop()