UserDefinitionViewer.py 8.37 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
from MDANSE.Framework.UserDefinitionStore import UD_STORE
38

39
class UserDefinitionViewer(wx.Dialog):
40
    
41
    def __init__(self, parent, title="User Definition Viewer", ud=None,editable=True):
42
        
43
        wx.Dialog.__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
48
        dialogSizer = wx.BoxSizer(wx.VERTICAL)
        
49
        mainPanel = wx.Panel(self, wx.ID_ANY, size=self.GetSize())
50
        
51
52
        self._tree = wx.TreeCtrl(mainPanel, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT|wx.TR_EDIT_LABELS)
        
53
54
        self._root = self._tree.AddRoot("root")

55
        self.build_tree(self._root, UD_STORE.definitions)     
56
57

        self._info = wx.TextCtrl(mainPanel, wx.ID_ANY, style=wx.TE_MULTILINE|wx.TE_READONLY)
58
        
59
60
61
62
63
64
65
66
67
68
69
70
        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)
                
71
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_show_info )
72
        self.Bind(wx.EVT_TREE_KEY_DOWN, self.on_delete, self._tree)
73
74
        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)
75
76
77
78
79

        self.Bind(wx.EVT_BUTTON, self.on_save_ud, self._save)
        
        if ud is not None:
            self.expand_ud(ud)
80
81
        
        self.set_editable(editable)
82
83
84
85
        
        dialogSizer.Add(mainPanel,1,wx.EXPAND)
        
        self.SetSizer(dialogSizer)
86
87
88
89
90
91
92
            
    def set_editable(self,editable=True):
        
        self._editable = editable
        
        self._save.Show(self._editable)
                                        
93
    def get_item_level(self,item):
94
        
95
96
97
98
99
100
101
        parent = self._tree.GetItemParent(item)
        
        if parent == self._tree.GetRootItem():
            return 1
        else: 
            return 1 + self.get_item_level(parent)

102
103
    def build_tree(self, node, data):
                
104
        for k, v in sorted(data.items()):
105
            
106
107
108
109
110
111
            dataItem = wx.TreeItemData(v)
            subnode = self._tree.AppendItem(node, str(k), data=dataItem)

            self._tree.SetItemTextColour(subnode, 'blue')  
            
            if isinstance(v, dict):
112
                self.build_tree(subnode, v)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                
    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)
128
            
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        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)            
143
        
144
    def on_show_info(self, event=None):
145
146
147
148

        currentItem = self._tree.GetSelection()

        itemData = self._tree.GetItemData(currentItem)
149
        
150
        if itemData is None:
151
152
            return
        
153
154
155
156
157
158
159
160
161
        data = itemData.GetData()
        
        level = self.get_item_level(currentItem)

        if level <= 3:
            self._info.SetValue("Contains the followings definitions: %s" % data.keys())
            return
                       
        self._info.SetValue(str(itemData.GetData()))       
162

163
164
165
166
    def on_delete(self, event):

        if not self._editable:
            return
167
168
169
170
171
172

        keycode = event.GetKeyCode()
        
        if keycode == wx.WXK_DELETE:
            
            currentItem = self._tree.GetSelection()
173
            
174
175
176
177
178
179
            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)
180
181
182
183
            if d.ShowModal() == wx.ID_NO:
                return

            currentItemName = str(self._tree.GetItemText(currentItem))
184
            
185
            if level == 1:
186
                UD_STORE.remove_definition(currentItemName)                
187
            elif level == 2:
188
                targetItem = self._tree.GetItemParent(currentItem)
189
                targetItemName = str(self._tree.GetItemText(targetItem))
190
                UD_STORE.remove_definition(targetItemName,currentItemName)
191
            elif level == 3:
192
                sectionItem = self._tree.GetItemParent(currentItem)
193
                sectionItemName = str(self._tree.GetItemText(sectionItem))
194
                targetItem = self._tree.GetItemParent(sectionItem)
195
196
197
198
199
                targetItemName = str(self._tree.GetItemText(targetItem))
                UD_STORE.remove_definition(targetItemName,sectionItemName,currentItemName)
            else:
                return
            
200
201
            self._tree.DeleteChildren(currentItem)
            self._tree.Delete(currentItem)
202
203
204
205
206
207
208
209
210
211
212
            self._udTree.clear()
            self._info.Clear()
            
            pub.sendMessage("msg_set_ud")

    def on_save_ud(self,event):

        UD_STORE.save()

        LOGGER('User definitions successfully saved.','info',['console'])
        
213
    def on_try_rename(self, event):
214
215
216
217
        
        if not self._editable:
            event.Veto()
            return
218
219
220
221
222
223
224

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

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

226
227
    def on_rename(self, event):
        
228
229
230
        if not self._editable:
            return
        
231
        currentItem = event.GetItem()
232
233
234
        
        currentItemName = str(self._tree.GetItemText(currentItem))   
        newItemName = self._tree.GetEditControl().GetValue()
235

236
237
238
239
240
241
242
243
244
        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)
245
        
246
247
        UD_STORE.set_definition(targetItemName,sectionItemName,newItemName,currentItemData.GetData())
        UD_STORE.remove_definition(targetItemName,sectionItemName,currentItemName)
248
            
249
250
if __name__ == "__main__":
    app = wx.App(False)
251
    f = UserDefinitionViewer(None,ud=['protein_in_periodic_universe.nc','atom_selection',"sfdfdfsd"],editable=True)
252
253
    f.ShowModal()
    f.Destroy()
254
    app.MainLoop()