1 """The DetailModel module defines a model to display the details of
2 StructBase, Array, and BasicBase instances."""
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 from PyQt4 import QtCore
42
43 from pyffi.utils.graph import EdgeFilter, GlobalNode
44 from pyffi.qskope.detail_tree import DetailTreeItem, DetailTreeItemData
45
46
47
48
50 """General purpose model for QModelIndexed access to pyffi data structures
51 such as StructBase, Array, and BasicBase instances."""
52
53 NUM_COLUMNS = 3
54 COL_NAME = 0
55 COL_TYPE = 1
56 COL_VALUE = 2
57
58
59
60
61
62
63
64
65
66
67
68 - def __init__(self, parent=None, globalnode=None, globalmodel=None,
69 edge_filter=EdgeFilter()):
70 """Initialize the model to display the given global node in the
71 detail tree. We also need a reference to the global model to
72 resolve node references.
73 """
74 QtCore.QAbstractItemModel.__init__(self, parent)
75 self.root_item = DetailTreeItem(
76 data=DetailTreeItemData(node=globalnode),
77 edge_filter=EdgeFilter())
78 self.globalmodel = globalmodel
79
81 """Return flags for the given index: all indices are enabled and
82 selectable."""
83 if not index.isValid():
84 return QtCore.Qt.ItemFlags()
85
86 flags = QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
87
88 if index.column() == self.COL_VALUE:
89 try:
90 index.internalPointer().data.node.get_value()
91
92 except AttributeError:
93 pass
94 except NotImplementedError:
95 pass
96 else:
97 flags |= QtCore.Qt.ItemIsEditable
98 return QtCore.Qt.ItemFlags(flags)
99
100 - def data(self, index, role):
101 """Return the data of model index in a particular role. Only
102 QtCore.Qt.DisplayRole is implemented.
103 """
104
105
106 if not index.isValid() or role != QtCore.Qt.DisplayRole:
107 return QtCore.QVariant()
108
109 item = index.internalPointer()
110
111
112 if index.column() == self.COL_NAME:
113 return QtCore.QVariant(item.data.name)
114
115
116 elif index.column() == self.COL_TYPE:
117 return QtCore.QVariant(item.data.typename)
118
119
120 elif index.column() == self.COL_VALUE:
121
122 display = item.data.display
123 if isinstance(display, GlobalNode):
124
125 blocknum = self.globalmodel.index_dict[display]
126 if (not hasattr(display, "name") or not display.name):
127 return QtCore.QVariant(
128 "%i [%s]" % (blocknum, display.__class__.__name__))
129 else:
130 return QtCore.QVariant(
131 "%i (%s)" % (blocknum, display.name))
132 elif isinstance(display, basestring):
133
134 if len(display) > 32:
135 display = display[:32] + "..."
136 return QtCore.QVariant(
137 display.replace("\n", " ").replace("\r", " "))
138 else:
139 raise TypeError("%s: do not know how to display %s"
140 % (item.data.name, display.__class__.__name__))
141
142
143 else:
144 return QtCore.QVariant()
145
147 """Return header data."""
148 if (orientation == QtCore.Qt.Horizontal
149 and role == QtCore.Qt.DisplayRole):
150 if section == self.COL_TYPE:
151 return QtCore.QVariant("Type")
152 elif section == self.COL_NAME:
153 return QtCore.QVariant("Name")
154 elif section == self.COL_VALUE:
155 return QtCore.QVariant("Value")
156 return QtCore.QVariant()
157
158 - def rowCount(self, parent=QtCore.QModelIndex()):
159 """Calculate a row count for the given parent index."""
160 if not parent.isValid():
161
162 return len(self.root_item.children)
163 else:
164
165 return len(parent.internalPointer().children)
166
168 """Return column count."""
169
170 return self.NUM_COLUMNS
171
172 - def index(self, row, column, parent):
173 """Create an index to item (row, column) of object parent."""
174
175 if not parent.isValid():
176
177
178 item = self.root_item.children[row]
179 else:
180
181
182 item = parent.internalPointer().children[row]
183 return self.createIndex(row, column, item)
184
186 """Calculate parent of a given index."""
187
188 parent_item = index.internalPointer().parent
189
190
191 if parent_item.parent is None:
192 return QtCore.QModelIndex()
193
194
195 else:
196 return self.createIndex(parent_item.row, 0, parent_item)
197
198 - def setData(self, index, value, role):
199 """Set data of a given index from given QVariant value. Only
200 QtCore.Qt.EditRole is implemented.
201 """
202 if role == QtCore.Qt.EditRole:
203
204 node = index.internalPointer().data.node
205 currentvalue = node.get_value()
206
207 if isinstance(currentvalue, (int, long)):
208
209 pyvalue, ok = value.toLongLong()
210 elif isinstance(currentvalue, float):
211 pyvalue, ok = value.toDouble()
212 elif isinstance(currentvalue, basestring):
213 pyvalue = str(value.toString())
214 ok = True
215 elif isinstance(currentvalue, bool):
216 pyvalue, ok = value.toBool()
217 else:
218
219 return False
220
221 if not ok:
222 return False
223
224 node.set_editor_value(pyvalue)
225
226 self.emit(QtCore.SIGNAL('dataChanged(QModelIndex, QModelIndex)'),
227 index, index)
228 return True
229
230 return False
231