UserDefinitionViewer.py 8.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
#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 Apr 14, 2015

:author: Gael Goret and Eric C. Pellegrini
'''

import wx

35
36
from MDANSE import LOGGER
from MDANSE.Externals.pubsub import pub
37
38
from MDANSE.Framework.UserDefinitionsStore import UD_STORE

39
class UserDefinitionViewer(wx.Frame):
40
    
41
    def __init__(self, parent, title="User Definition Viewer", ud=None,editable=True):
42
43
        
        wx.Frame.__init__(self, parent, wx.ID_ANY, size = (800,400), title = title, style=wx.DEFAULT_DIALOG_STYLE|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX|wx.RESIZE_BORDER)
44
45
46

        self._udTree = {}
        
47
        mainPanel = wx.Panel(self, wx.ID_ANY, size=self.GetSize())
48
        
49
50
        self._tree = wx.TreeCtrl(mainPanel, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT|wx.TR_EDIT_LABELS)
        
51
52
53
54
55
        self._root = self._tree.AddRoot("root")

        self.set_plugins_tree(self._root, UD_STORE)     

        self._info = wx.TextCtrl(mainPanel, wx.ID_ANY, style=wx.TE_MULTILINE|wx.TE_READONLY)
56
        
57
58
59
60
61
62
63
64
65
66
67
68
        self._save  = wx.Button(mainPanel, wx.ID_ANY, label="Save user definitions")

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        infoSizer = wx.BoxSizer(wx.HORIZONTAL)
        infoSizer.Add(self._tree, 1, wx.ALL|wx.EXPAND, 5)
        infoSizer.Add(self._info, 2, wx.ALL|wx.EXPAND, 5)
                        
        mainSizer.Add(infoSizer,1,wx.ALL|wx.EXPAND,5)                      
        mainSizer.Add(self._save,0,wx.ALL|wx.EXPAND,5)                      
        mainPanel.SetSizer(mainSizer)
                
69
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_show_info )
70
        self.Bind(wx.EVT_TREE_KEY_DOWN, self.on_delete, self._tree)
71
72
        self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.on_rename, self._tree)
        self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.on_try_rename, self._tree)
73
74
75
76
77
78
79

        self.Bind(wx.EVT_BUTTON, self.on_save_ud, self._save)

        self.Bind(wx.EVT_CLOSE, self.on_quit)
        
        if ud is not None:
            self.expand_ud(ud)
80
81
82
83
84
85
86
87
88
        
        self.set_editable(editable)
            
    def set_editable(self,editable=True):
        
        self._editable = editable
        
        self._save.Show(self._editable)
                                        
89
    def get_item_level(self,item):
90
        
91
92
93
94
95
96
97
        parent = self._tree.GetItemParent(item)
        
        if parent == self._tree.GetRootItem():
            return 1
        else: 
            return 1 + self.get_item_level(parent)

98
99
100
    def set_plugins_tree(self, node, data):
        
        for k, v in data.items():
101
            
102
103
104
105
106
107
108
            dataItem = wx.TreeItemData(v)
            subnode = self._tree.AppendItem(node, str(k), data=dataItem)

            self._tree.SetItemTextColour(subnode, 'blue')  
            
            if isinstance(v, dict):
                self.set_plugins_tree(subnode, v)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
                
    def find_ud(self,baseitem,itemNames):

        if not itemNames:
            return baseitem
        
        name = itemNames.pop(0)

        item, cookie = self._tree.GetFirstChild(baseitem)
        
        while item.IsOk():
            if self._tree.GetItemText(item) == name:
                baseitem = item
                return self.find_ud(baseitem,itemNames)
            item, cookie = self._tree.GetNextChild(baseitem,cookie)
124
            
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        return None

    def expand_ud(self,ud):

        item = self.find_ud(self._root, ud)
        
        if item is None:
            return

        self._tree.SelectItem(item,True)

        while item != self._root:
            item = self._tree.GetItemParent(item)
            self._tree.Expand(item)            
139
        
140
141
142
    def on_show_info(self, event=None):
        
        ItemData = self._tree.GetItemData(self._tree.GetSelection())
143
144
145
146
147
148
149
        
        if ItemData is None:
            return
        
        containerStr = str(ItemData.GetData())
       
        self._info.SetValue(containerStr)       
150

151
152
153
154
    def on_delete(self, event):

        if not self._editable:
            return
155
156
157
158
159
160
161
162
163
164
165
166
167

        keycode = event.GetKeyCode()
        
        if keycode == wx.WXK_DELETE:
            
            currentItem = self._tree.GetSelection()

            level = self.get_item_level(currentItem)
            
            if level > 3:
                return
                                                                        
            d = wx.MessageDialog(None, 'Do you really want to delete this definition ?', 'Question', wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
            if d.ShowModal() == wx.ID_NO:
                return

            currentItemName = str(self._tree.GetItemText(currentItem))

            if level == 1:
                UD_STORE.remove_target(currentItemName)                
            elif level == 2:
                targetItem = self._tree.GetParent(currentItem)
                targetItemName = str(self._tree.GetItemText(targetItem))
                UD_STORE.remove_section(targetItemName,currentItemName)
            elif level == 3:
                sectionItem = self._tree.GetParent(currentItem)
                sectionItemName = str(self._tree.GetItemText(sectionItem))
                targetItem = self._tree.GetParent(sectionItem)
                targetItemName = str(self._tree.GetItemText(targetItem))
                UD_STORE.remove_definition(targetItemName,sectionItemName,currentItemName)
            else:
                return
            
            self._tree.DeleteAllItems()
            self._udTree.clear()
            self.build_plugins_tree()
            self._info.Clear()
            
            pub.sendMessage("msg_set_ud")

    def on_save_ud(self,event):

        UD_STORE.save()

        LOGGER('User definitions successfully saved.','info',['console'])
        
201
    def on_try_rename(self, event):
202
203
204
205
        
        if not self._editable:
            event.Veto()
            return
206
207
208
209
210
211
212

        currentItem = self._tree.GetSelection()
        level = self.get_item_level(currentItem)

        if level != 3:
            event.Veto()
            return
213

214
215
    def on_rename(self, event):
        
216
217
218
        if not self._editable:
            return
        
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        currentItem = self._tree.GetSelection()
        
        currentItemName = str(self._tree.GetItemText(currentItem))   
        newItemName = self._tree.GetEditControl().GetValue()
        
        if currentItemName == newItemName:
            return
        
        sectionItem = self._tree.GetItemParent(currentItem)
        sectionItemName = str(self._tree.GetItemText(sectionItem))
        targetItem = self._tree.GetItemParent(sectionItem)
        targetItemName = str(self._tree.GetItemText(targetItem))

        currentItemData = self._tree.GetItemData(currentItem)
                
        UD_STORE.set_definition(targetItemName,sectionItemName,newItemName,currentItemData.GetData())
        UD_STORE.remove_definition(targetItemName,sectionItemName,currentItemName)

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

if __name__ == "__main__":
    app = wx.App(False)
245
    f = UserDefinitionViewer(None,ud=['protein_in_periodic_universe.nc','atom_selection',"sfdfdfsd"],editable=False)
246
247
    f.Show()
    app.MainLoop()