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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#!/usr/bin/env python
#############################################################################
##
## This file is part of Taurus
##
## http://taurus-scada.org
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## Taurus 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 3 of the License, or
## (at your option) any later version.
##
## Taurus 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 Taurus. If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################
"""
"""
__all__ = ["QObjectRepresentation", "get_qobject_tree", "get_qobject_tree_str",
"TreeQObjectModel", "TreeQObjectWidget"]
__docformat__ = 'restructuredtext'
import weakref
from taurus.external.qt import Qt
from taurus.core.util.enumeration import Enumeration
QObjectRepresentation = Enumeration('QObjectRepresentation',
('ClassName', 'ObjectName', 'FullName'))
def _build_qobjects_as_dict(qobject, container):
container[qobject] = childs = {}
for child in qobject.children():
if isinstance(child, Qt.QWidget):
_build_qobjects_as_dict(child, childs)
def get_qobject_tree_as_dict(qobject=None):
if qobject is None:
app = Qt.QApplication.instance()
qobjects = app.topLevelWidgets()
else:
qobjects = [qobject]
tree = {}
for qobject in qobjects:
_build_qobjects_as_dict(qobject, tree)
return tree
def _build_qobjects_as_list(qobject, container):
children = qobject.children()
node = qobject, []
container.append(node)
for child in children:
if isinstance(child, Qt.QWidget):
_build_qobjects_as_list(child, node[1])
def get_qobject_tree_as_list(qobject=None):
if qobject is None:
app = Qt.QApplication.instance()
qobjects = app.topLevelWidgets()
else:
qobjects = [qobject]
tree = []
for qobject in qobjects:
_build_qobjects_as_list(qobject, tree)
return tree
get_qobject_tree = get_qobject_tree_as_list
def _get_qobject_str(qobject, representation):
if representation == QObjectRepresentation.ClassName:
return qobject.__class__.__name__
elif representation == QObjectRepresentation.ObjectName:
return str(qobject.objectName())
elif representation == QObjectRepresentation.FullName:
return '{0}("{1}")'.format(qobject.__class__.__name__, str(qobject.objectName()))
return str(qobject)
def _build_qobject_str(node, str_tree, representation=QObjectRepresentation.ClassName):
qobject, children = node
str_node = _get_qobject_str(qobject, representation)
str_children = []
str_tree.append((str_node, str_children))
for child in children:
_build_qobject_str(child, str_children, representation=representation)
def get_qobject_tree_str(qobject=None, representation=QObjectRepresentation.ClassName):
tree, str_tree = get_qobject_tree(qobject=qobject), []
for e in tree:
_build_qobject_str(e, str_tree, representation=representation)
return str_tree
from taurus.qt.qtgui.tree.qtree import QBaseTreeWidget
from taurus.qt.qtcore.model import TaurusBaseModel, TaurusBaseTreeItem
QR = QObjectRepresentation
class TreeQObjecttItem(TaurusBaseTreeItem):
def __init__(self, model, data, parent = None):
TaurusBaseTreeItem.__init__(self, model, data, parent=parent)
if data is not None:
self.qobject = weakref.ref(data)
dat = _get_qobject_str(data, QR.ClassName), \
_get_qobject_str(data, QR.ObjectName)
self.setData(0, dat)
class TreeQObjectModel(TaurusBaseModel):
ColumnNames = "Class", "Object name"
ColumnRoles = (QR.ClassName,), QR.ObjectName
def __init__(self, parent=None, data=None):
TaurusBaseModel.__init__(self, parent=parent, data=data)
# def createNewRootItem(self):
# return TreeQObjecttItem(self, self.ColumnNames)
def role(self, column, depth=0):
if column == 0:
return self.ColumnRoles[column][0]
return self.ColumnRoles[column]
def roleIcon(self, taurus_role):
return Qt.QIcon()
def roleSize(self, taurus_role):
return Qt.QSize(300, 70)
def roleToolTip(self, role):
return "widget information"
@staticmethod
def _build_qobject_item(model, parent, node):
qobject, children = node
item = TreeQObjecttItem(model, qobject, parent)
parent.appendChild(item)
for child in children:
TreeQObjectModel._build_qobject_item(model, item, child)
def setupModelData(self, data):
if data is None:
return
rootItem = self._rootItem
for node in data:
TreeQObjectModel._build_qobject_item(self, rootItem, node)
class TreeQObjectWidget(QBaseTreeWidget):
KnownPerspectives = {
"Default" : {
"label" : "Default perspecive",
"tooltip" : "QObject tree view",
"icon" : "",
"model" : [TreeQObjectModel],
},
}
DftPerspective = "Default"
def __init__(self, parent=None, designMode=False, with_navigation_bar=True,
with_filter_widget=True, perspective=None, proxy=None,
qobject_root=None):
QBaseTreeWidget.__init__(self, parent, designMode=designMode,
with_navigation_bar=with_navigation_bar,
with_filter_widget=with_filter_widget,
perspective=perspective, proxy=proxy)
qmodel = self.getQModel()
qmodel.setDataSource(get_qobject_tree(qobject=qobject_root))
def build_gui():
mw = Qt.QMainWindow()
mw.setObjectName("main window")
w = Qt.QWidget()
w.setObjectName("central widget")
mw.setCentralWidget(w)
l = Qt.QVBoxLayout()
w.setLayout(l)
l1 = Qt.QLabel("H1")
l1.setObjectName("label 1")
l.addWidget(l1)
l2 = Qt.QLabel("H2")
l2.setObjectName("label 2")
l.addWidget(l2)
mw.show()
return mw
def main():
from taurus.qt.qtgui.application import TaurusApplication
app = TaurusApplication()
w = build_gui()
tree = TreeQObjectWidget(qobject_root=w)
tree.show()
#import pprint
#pprint.pprint(get_qobject_tree_str())
w.dumpObjectTree()
app.exec_()
if __name__ == "__main__":
main()
|