]> SALOME platform Git repositories - modules/yacs.git/blob - src/pyqt/gui/Tree.py
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/yacs.git] / src / pyqt / gui / Tree.py
1 # Copyright (C) 2006-2012  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 # -*- coding: iso-8859-15 -*-
21 #
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
25 """
26
27 import sys
28 from qt import *
29 import Icons
30 import CONNECTOR
31
32 class Tree(QListView):
33   """Tree(parent=None)
34      Classe pour faire une vue d'un arbre
35   """
36   def __init__(self,parent=None,onSelect=None,onDblSelect=None):
37     QListView.__init__(self,parent)
38     self.setCaption("Tree")
39     self.setRootIsDecorated(1)
40     self.setSorting(-1)
41     self.addColumn("Name")
42     self.children=[]
43     self.last=None
44     self.onSelect=onSelect
45     self.onDblSelect=onDblSelect
46     self.connect(self,SIGNAL('selectionChanged(QListViewItem *)'),
47                      self.handleSelected)
48     self.connect(self,SIGNAL('doubleClicked(QListViewItem *, const QPoint &, int)'),
49                      self.handleDblSelected)
50
51   def handleSelected(self,node):
52     node.item.selected()
53     if self.onSelect:
54       self.onSelect(node.item)
55
56   def handleDblSelected(self,node,point,col):
57     node.item.dblselected()
58     if self.onDblSelect:
59       self.onDblSelect(node.item)
60
61   def additem(self,item):
62     node=Node(self,item.label,item,self.last)
63     self.last=node
64     self.children.append(node)
65     CONNECTOR.Connect(item,"selected",self.selectItem,())
66     CONNECTOR.Connect(item,"add",node.addNode,())
67     return node
68
69   def selectNodeItem(self,item,node):
70     #print "selectNodeItem",node,item
71     self.setSelected(node,True)
72
73   def selectItem(self,item):
74     #print "selectItem",item
75     node=self.selectedItem()
76     if node.item is item:
77       #print "item has been selected at item level"
78       return
79
80     #print "item has not been selected at item level"
81     #try another way
82     #find its father ???
83     it = QListViewItemIterator(self)
84     node = it.current()
85     while node:
86       if node.item is item:
87         break
88       it += 1
89       node = it.current()
90
91     if node:
92       self.setSelected(node,True)
93
94 class Node(QListViewItem):
95   """Node(parent,text,item,after)
96        Classe pour faire une vue d'un noeud d'un arbre
97   """
98   def __init__(self,parent,text,item,after=None):
99     if after is None:
100       QListViewItem.__init__(self,parent,text)
101     else:
102       QListViewItem.__init__(self,parent,after,text)
103     self.item=item
104     self.setPixmap(0,Icons.get_image(item.getIconName()))
105     self.setExpandable(self.item.isExpandable())
106     self.children = []
107
108   def selectNode(self,item):
109     self.listView().setSelected(self,True)
110
111   def addNode(self,item):
112     #print "Tree.addNode",item
113     self.additem(item)
114
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,))
120     self.takeItem(node)
121     self.children.remove(node)
122
123   def additem(self,item):
124     if self.children:
125       node=Node(self,item.label,item,self.children[-1])
126     else:
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,))
133     return node
134
135   def setOpen(self,o):
136     if o and not self.childCount():
137       for child in self.item.getChildren():
138         self.additem(child)
139     QListViewItem.setOpen(self,o)
140
141   def setOpen_old(self,o):
142     if o:
143       #open
144       for child in self.item.getChildren():
145         self.additem(child)
146     else:
147       #close
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,())
152         self.takeItem(node)
153         del node
154       self.children=[]
155     QListViewItem.setOpen(self,o)
156        
157
158 if __name__ == "__main__":
159   from Item import Item
160   app = QApplication(sys.argv)
161   t=Tree()
162   t.additem(Item("item1"))
163   #n=t.additem(Item("item2"))
164   #n.additem(Item("item3"))
165   app.setMainWidget(t)
166   t.show()
167   app.exec_loop()
168