DataTreePanel.py 5.16 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#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 10, 2015

:author: Eric C. Pellegrini
'''

import wx

from MDANSE.Externals.pubsub import pub

from MDANSE.GUI.DataController import DATA_CONTROLLER

class DataObject(wx.TextDataObject):

    def __init__(self, pluginStr):
        wx.TextDataObject.__init__(self)
                
        self.SetText(pluginStr)   
        
class DataTreePanel(wx.Panel):
    
    def __init__(self, parent):
        
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        
        self.build_panel()
        
        self.build_layout()

        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.on_double_click_data)
        self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.on_drag_data)
        self.Bind(wx.EVT_TREE_KEY_DOWN, self.on_delete_data, self._tree)

        pub.subscribe(self.msg_load_input_data, ('msg_load_input_data'))       
        pub.subscribe(self.msg_delete_input_data, ('msg_delete_input_data'))       
        
    @property
    def tree(self):
        return self._tree
    
    def msg_load_input_data(self, message):
        
        if message is None:
            return
        
        self.add_data(message)

    def msg_delete_input_data(self, message):

        
        item = self.get_tree_item(self._root, message)
        
        if item is None:
            return
        
        parentItem = self._tree.GetItemParent(item)
         
        self._tree.Delete(item)
         
        if not self._tree.ItemHasChildren(parentItem):
            self._tree.Delete(parentItem)
    
    def get_tree_item(self, node, name):
                
        for item in self.get_children(node):
            if self._tree.GetItemData(item).GetData() == name:
                return item
            else:
                return self.get_tree_item(item, name)
        
        return None
    
    def get_children(self, item):
        
        item, _ = self._tree.GetFirstChild(item)
        
        while item.IsOk():
            yield item
            item = self._tree.GetNextSibling(item)
                
    def add_data(self, data):
                
        dataItem = wx.TreeItemData(data.filename)

        item = None
        for cItem in self.get_children(self._root):
            if data.type == self._tree.GetItemText(cItem):
                item = cItem
                break
        if item is None:    
            item = self._tree.AppendItem(self._root, data.type, data=None)

        self._tree.AppendItem(item, data.basename, data=dataItem)

        self._tree.ExpandAll()
          
    def build_panel(self):
        
        self._tree = wx.TreeCtrl(self, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE^wx.TR_HIDE_ROOT)
        
        self._root = self._tree.AddRoot("root")
                     
    def build_layout(self):
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        sizer.Add(self._tree, 1, wx.EXPAND, 0)
        
        self.SetSizer(sizer)
         
    def on_delete_data(self, event):
        
        keycode = event.GetKeyCode()
        
        if keycode == wx.WXK_DELETE:
            
            item = self._tree.GetSelection()
            itemData = self._tree.GetItemData(item)
            
            if itemData.GetData() is None:
                return

            del DATA_CONTROLLER[itemData.GetData()]
                                    
    def on_double_click_data(self, event):
        
        itemData = self._tree.GetItemData(event.GetItem())
        
        if itemData.GetData() is None:
            return
        
        containerStr = itemData.GetData()
       
        self.TopLevelParent.panels["working"].drop(containerStr)
                                           
    def on_drag_data(self, event):

        containerStr = self._tree.GetItemData(event.GetItem()).GetData()
        
        if containerStr is None:
            return            
        
        dropSource = wx.DropSource(self)

        data = DataObject(containerStr)
        
        dropSource.SetData(data)
        
175
        dropSource.DoDragDrop(wx.Drag_CopyOnly)