AtomSelectionWidget.py 5.13 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
37
#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

from MDANSE.Externals.pubsub import pub
38
39
from MDANSE.Framework.UserDefinitionStore import UD_STORE
from MDANSE.Framework.Widgets.UserDefinitionWidget import UserDefinitionDialog, UserDefinitionWidget
40
from MDANSE.GUI.Icons import ICONS
41

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

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

50
51
        panel = wx.Panel(self._widgetPanel,wx.ID_ANY)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
52
        
53
54
55
56
        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"))
57

58
59
        sizer.Add(new, 0, wx.ALL|wx.ALIGN_RIGHT, 5)
        sizer.Add(add, 0, wx.ALL|wx.ALIGN_RIGHT, 5)
60
        
61
        panel.SetSizer(sizer)
62
        
63
        self._sizer.Add(panel, 1, wx.ALL|wx.ALIGN_RIGHT, 5)
64

65
        self._choices = []
66

67
        pub.subscribe(self.msg_set_ud, "msg_set_ud")
68

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

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

86
87
88
        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)
89
        
90
        panel.SetSizer(sizer)
91
        
92
        self._sizer.Add(panel,1,wx.ALL|wx.EXPAND,5)
93
        
94
        self._widgetPanel.GrandParent.Layout()
95

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

99
    def on_remove_definition(self,event):
100
        
101
        self._sizer.Detach(event.GetEventObject().Parent)
102
        
103
        event.GetEventObject().Parent.Destroy()
104

105
        self._widgetPanel.GrandParent.Layout()
106

107
    def get_widget_value(self):
108

109
110
        sizerItemList = list(self._sizer.GetChildren())
        del sizerItemList[0]
111

112
113
114
115
116
117
118
119
120
121
122
123
124
        uds = []
        for sizerItem in sizerItemList:
            
            panel = sizerItem.GetWindow()
            children = panel.GetChildren()
            udName = children[0]
            oldSelection = udName.GetStringSelection()            
            udName.SetItems(uds)
            udName.SetStringSelection(oldSelection)
            
            uds.append(udName)
                  
        if not uds:
125
126
            return None
        else:
127
128
129
            return uds

    def msg_set_ud(self):
130
         
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        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)
            
145
146
147
148
if __name__ == "__main__":
    
    from MMTK.Trajectory import Trajectory
    
149
150
151
    from MDANSE import PLATFORM
    
    t = Trajectory(None,os.path.join(PLATFORM.example_data_directory(),"Trajectories","MMTK","protein_in_periodic_universe.nc"),"r")
152
153
154
    
    app = wx.App(False)
    
155
    p = UserDefinitionDialog(None,t,'atoms_list')
156
157
158
159
160
161
162
163
164
        
    p.SetSize((800,800))
            
    p.ShowModal()
    
    p.Destroy()
    
    app.MainLoop()