1 # Copyright (C) 2006-2012 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # -*- coding: iso-8859-15 -*-
22 """Ce module permet de créer des vues sous forme d'arbre
23 Ces vues sont construites à partir des informations
24 fournies par un modèle
32 class Tree(QListView):
34 Classe pour faire une vue d'un arbre
36 def __init__(self,parent=None,onSelect=None,onDblSelect=None):
37 QListView.__init__(self,parent)
38 self.setCaption("Tree")
39 self.setRootIsDecorated(1)
41 self.addColumn("Name")
44 self.onSelect=onSelect
45 self.onDblSelect=onDblSelect
46 self.connect(self,SIGNAL('selectionChanged(QListViewItem *)'),
48 self.connect(self,SIGNAL('doubleClicked(QListViewItem *, const QPoint &, int)'),
49 self.handleDblSelected)
51 def handleSelected(self,node):
54 self.onSelect(node.item)
56 def handleDblSelected(self,node,point,col):
57 node.item.dblselected()
59 self.onDblSelect(node.item)
61 def additem(self,item):
62 node=Node(self,item.label,item,self.last)
64 self.children.append(node)
65 CONNECTOR.Connect(item,"selected",self.selectItem,())
66 CONNECTOR.Connect(item,"add",node.addNode,())
69 def selectNodeItem(self,item,node):
70 #print "selectNodeItem",node,item
71 self.setSelected(node,True)
73 def selectItem(self,item):
74 #print "selectItem",item
75 node=self.selectedItem()
77 #print "item has been selected at item level"
80 #print "item has not been selected at item level"
83 it = QListViewItemIterator(self)
92 self.setSelected(node,True)
94 class Node(QListViewItem):
95 """Node(parent,text,item,after)
96 Classe pour faire une vue d'un noeud d'un arbre
98 def __init__(self,parent,text,item,after=None):
100 QListViewItem.__init__(self,parent,text)
102 QListViewItem.__init__(self,parent,after,text)
104 self.setPixmap(0,Icons.get_image(item.getIconName()))
105 self.setExpandable(self.item.isExpandable())
108 def selectNode(self,item):
109 self.listView().setSelected(self,True)
111 def addNode(self,item):
112 #print "Tree.addNode",item
115 def delNode(self,node):
116 #print "Tree.delNode",node,node.item
117 CONNECTOR.Disconnect(node.item,"selected",node.selectNode,())
118 CONNECTOR.Disconnect(node.item,"add",node.addNode,())
119 CONNECTOR.Disconnect(node.item,"remove",self.delNode,(node,))
121 self.children.remove(node)
123 def additem(self,item):
125 node=Node(self,item.label,item,self.children[-1])
127 node=Node(self,item.label,item)
128 self.children.append(node)
129 #CONNECTOR.Connect(item,"selected",self.listView().selectNodeItem,(node,))
130 CONNECTOR.Connect(item,"selected",node.selectNode,())
131 CONNECTOR.Connect(item,"add",node.addNode,())
132 CONNECTOR.Connect(item,"remove",self.delNode,(node,))
136 if o and not self.childCount():
137 for child in self.item.getChildren():
139 QListViewItem.setOpen(self,o)
141 def setOpen_old(self,o):
144 for child in self.item.getChildren():
148 for node in self.children:
149 #CONNECTOR.Disconnect(node.item,"selected",self.listView().selectNodeItem,(node,))
150 CONNECTOR.Disconnect(node.item,"selected",node.selectNode,())
151 CONNECTOR.Disconnect(node.item,"add",node.addNode,())
155 QListViewItem.setOpen(self,o)
158 if __name__ == "__main__":
159 from Item import Item
160 app = QApplication(sys.argv)
162 t.additem(Item("item1"))
163 #n=t.additem(Item("item2"))
164 #n.additem(Item("item3"))