-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtree.py
277 lines (230 loc) · 8.44 KB
/
mtree.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# mtree.py ---
#
# Filename: mtree.py
# Description:
# Author: Subhasis Ray
# Maintainer:
# Created: Tue May 14 11:51:35 2013 (+0530)
# Version:
# Last-Updated: Fri Jun 14 16:13:08 2013 (+0530)
# By: subha
# Update #: 154
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
# Implementation of moose tree widget. This can be used by multiple
# components in the moose gui.
#
#
# Change log:
#
#
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import Qt
import moose
class MooseTreeModel(QtCore.QAbstractItemModel):
"""Tree model for the MOOSE element tree.
This is not going to work as the MOOSE tree nodes are
inhomogeneous. The parent of a node is an melement, but the
children of an melement are vec objects.
Qt can handle only homogeneous tere nodes.
"""
def __init__(self, *args):
super(MooseTreeModel, self).__init__(*args)
self.rootItem = moose.element('/')
def index(self, row, column, parent):
if not self.hasIndex(row, column, parent):
return QtCore.QModelIndex()
if not parent.isValid():
parentItem = self.rootItem
else:
parentItem = parent.internalPointer()
childItem = parentItem.children[row]
if childItem.path == '/':
return QtCore.QModelIndex()
return self.createIndex(row, column, childItem)
def parent(self, index):
if not index.isValid():
return QtCore.QModelIndex()
childItem = index.internalPointer()
parentItem = childItem.parent()
if parentItem == self.rootItem:
return QtCore.QModelIndex()
return self.createIndex(parentItem.parent.children.index(parentItem), parentItem.getDataIndex(), parentItem)
def rowCount(self, parent):
if not parent.isValid():
parentItem = self.rootItem
else:
parentItem = parent.internalPointer()
ret = len(parentItem.children)
return ret
def columnCount(self, parent):
if parent.isValid():
return len(parent.internalPointer())
ret = len(self.rootItem)
return ret
def data(self, index, role):
if not index.isValid():
return None
item = index.internalPointer()
return QtCore.QVariant(item[index.column()].name)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant('Model Tree')
return None
def flags(self, index):
if not index.isValid():
return QtCore.Qt.NoItemFlags
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
class MooseTreeItem(QtGui.QTreeWidgetItem):
def __init__(self, *args):
QtGui.QTreeWidgetItem.__init__(self, *args)
self.mobj = None
def setObject(self, element):
self.mobj = moose.element(element)
self.setText(0, QtCore.QString(self.mobj.path.rpartition('/')[-1]))
self.setText(1, QtCore.QString(self.mobj.className))
#self.setToolTip(0, QtCore.QString('class:' + self.mooseObj_.className))
def updateSlot(self):
self.setText(0, QtCore.QString(self.mobj.name))
class MooseTreeWidget(QtGui.QTreeWidget):
"""Widget for displaying MOOSE model tree.
"""
# Author: subhasis ray
#
# Created: Tue Jun 23 18:54:14 2009 (+0530)
#
# Updated for moose 2 and multiscale GUI: 2012-12-06
# Further updated for pymoose2: Tue May 14 14:35:35 IST 2013
# Ignored are elements to be ignored when building the tree
ignored = ['/Msgs', '/classes']
elementInserted = QtCore.pyqtSignal('PyQt_PyObject')
def __init__(self, *args):
"""A tree widget to display model tree in MOOSE.
Members:
rootElement: melement
root element for the tree.
SIGNAL:
elementInserted(melement) emitted when a new element is inserted.
"""
QtGui.QTreeWidget.__init__(self, *args)
self.header().hide()
self.rootElement = moose.element('/')
self.odict = {}
self.recreateTree()
def setupTree(self, obj, parent, odict):
"""Recursively setup the tree items.
Parameters
----------
obj: melement
object to be associated with the tree item created.
parent: MooseTreeItem
parent item of the current tree item
odict: dict
dictionary to store melement to tree widget item mapping.
"""
for ii in MooseTreeWidget.ignored:
if obj.path.startswith(ii):
return None
item = MooseTreeItem(parent)
item.setObject(obj)
odict[obj] = item
# TODO: check and verify that this still works with synapses -
# there have been change in API. - Subha, Fri Sep 19 19:04:35 IST 2014
for child in obj.children:
ch = child
if child.name in obj.getFieldNames('fieldElementFinfo'):
ch = obj.getField(child.name)
for elm in ch:
self.setupTree(moose.element(elm), item, odict)
return item
def recreateTree(self, root=None):
"""Clears the current tree and recreates the tree. If root is not
specified it uses the current `root` otherwise replaces the
rootElement with specified root element.
Parameter
---------
root: str or melement or vec
New root element of the tree. Use current rootElement if `None`
"""
self.clear()
self.odict.clear()
if root is not None:
self.rootElement = moose.element(root)
self.setupTree(self.rootElement, self, self.odict)
self.setCurrentItem(self.rootElement)
self.expandToDepth(0)
def insertElementSlot(self, class_name):
"""Creates an instance of the class class_name and inserts it
under currently selected element in the model tree."""
# print 'Inserting element ...', class_name
current = self.currentItem()
print 'CLASS NAME:', class_name
self.insertChildElement(current, class_name)
def insertChildElement(self, item, class_name, name=''):
if len(name) == 0:
name = class_name
path = '%s/%s' % (item.mobj.path, str(name))
mclassname = 'moose.%s' % (str(class_name))
mclass = eval(mclassname)
obj = mclass(path)
newitem = MooseTreeItem(item)
newitem.setObject(obj)
item.addChild(newitem)
self.odict[obj] = newitem
self.elementInserted.emit(obj)
def setCurrentItem(self, item):
"""Overloaded version of QTreeWidget.setCurrentItem
- adds ability to set item by corresponding moose object.
"""
if isinstance(item, QtGui.QTreeWidgetItem):
QtGui.QTreeWidget.setCurrentItem(self, item)
return
mobj = moose.element(item)
QtGui.QTreeWidget.setCurrentItem(self, self.odict[mobj])
def updateItemSlot(self, element):
self.odict[element].updateSlot()
def main():
"""Test main: load a model and display the tree for it"""
model = moose.Neutral('/model')
moose.loadModel('../Demos/Genesis_files/Kholodenko.g', '/model/Kholodenko')
# tab = moose.element('/model/Kholodenko/graphs/conc1/MAPK_PP.Co')
# print tab
# for t in tab.children:
# print t
app = QtGui.QApplication(sys.argv)
mainwin = QtGui.QMainWindow()
mainwin.setWindowTitle('Model tree test')
tree = MooseTreeWidget()
tree.recreateTree(root='/model/')
mainwin.setCentralWidget(tree)
mainwin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
#
# mtree.py ends here