DataTreePanel.py 5.69 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 10, 2015

:author: Eric C. Pellegrini
'''

import wx

35 36
from MDANSE.GUI import PUBLISHER
from MDANSE.GUI.DataController import DATA_CONTROLLER
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

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)
58 59
        
        self.Bind(wx.EVT_WINDOW_DESTROY,self.OnDestroy)
60

61 62
        PUBLISHER.subscribe(self.msg_load_input_data, 'msg_load_input_data')       
        PUBLISHER.subscribe(self.msg_delete_input_data, 'msg_delete_input_data')       
63 64 65 66 67
        
    @property
    def tree(self):
        return self._tree
    
68 69
    def OnDestroy(self,event):
        
70 71
        PUBLISHER.unsubscribe(self.msg_load_input_data, 'msg_load_input_data')       
        PUBLISHER.unsubscribe(self.msg_delete_input_data, 'msg_delete_input_data')       
72 73
        event.Skip()
    
74
    def msg_load_input_data(self,message):
75
        
76
        if message is None:
77
            return
78
                        
79
        self.add_data(message)
80

81
    def msg_delete_input_data(self,message):
82

83 84 85 86
        if message is None:
            return

        item = self.get_tree_item(self._root,message)
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        
        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):
eric pellegrini's avatar
eric pellegrini committed
101 102 103 104
            data = self._tree.GetItemData(item)
            if data is not None:
                if data.GetData() == name:
                    return item
105 106 107
            match = self.get_tree_item(item, name)
            if match is not None:
                return match  
108 109 110 111 112 113 114 115 116 117 118
        
        return None
    
    def get_children(self, item):
        
        item, _ = self._tree.GetFirstChild(item)
        
        while item.IsOk():
            yield item
            item = self._tree.GetNextSibling(item)
                
119
    def add_data(self,data):
120
                
121
        dataItem = wx.TreeItemData(data.name)
122 123 124

        item = None
        for cItem in self.get_children(self._root):
125
            if data._type == self._tree.GetItemText(cItem):
126 127 128
                item = cItem
                break
        if item is None:    
129
            item = self._tree.AppendItem(self._root, data._type, data=None)
130

131
        self._tree.AppendItem(item, data.shortname, data=dataItem)
132 133 134 135 136

        self._tree.ExpandAll()
          
    def build_panel(self):
        
137
        self._tree = wx.TreeCtrl(self, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        
        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)
157 158 159

            if itemData is None:
                return
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
            
            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)
        
190
        dropSource.DoDragDrop(wx.Drag_CopyOnly)