|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Watch expression model. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 |
|
12 class WatchPointModel(QAbstractItemModel): |
|
13 """ |
|
14 Class implementing a custom model for watch expressions. |
|
15 """ |
|
16 def __init__(self, parent = None): |
|
17 """ |
|
18 Constructor |
|
19 |
|
20 @param reference to the parent widget (QObject) |
|
21 """ |
|
22 QObject.__init__(self, parent) |
|
23 |
|
24 self.watchpoints = [] |
|
25 self.header = [ |
|
26 QVariant(self.trUtf8("Condition")), |
|
27 QVariant(self.trUtf8("Special")), |
|
28 QVariant(self.trUtf8('Temporary')), |
|
29 QVariant(self.trUtf8('Enabled')), |
|
30 QVariant(self.trUtf8('Ignore Count')), |
|
31 ] |
|
32 self.alignments = [QVariant(Qt.Alignment(Qt.AlignLeft)), |
|
33 QVariant(Qt.Alignment(Qt.AlignLeft)), |
|
34 QVariant(Qt.Alignment(Qt.AlignHCenter)), |
|
35 QVariant(Qt.Alignment(Qt.AlignHCenter)), |
|
36 QVariant(Qt.Alignment(Qt.AlignRight)), |
|
37 ] |
|
38 |
|
39 def columnCount(self, parent = QModelIndex()): |
|
40 """ |
|
41 Public method to get the current column count. |
|
42 |
|
43 @return column count (integer) |
|
44 """ |
|
45 return len(self.header) + 1 |
|
46 |
|
47 def rowCount(self, parent = QModelIndex()): |
|
48 """ |
|
49 Public method to get the current row count. |
|
50 |
|
51 @return row count (integer) |
|
52 """ |
|
53 # we do not have a tree, parent should always be invalid |
|
54 if not parent.isValid(): |
|
55 return len(self.watchpoints) |
|
56 else: |
|
57 return 0 |
|
58 |
|
59 def data(self, index, role): |
|
60 """ |
|
61 Public method to get the requested data. |
|
62 |
|
63 @param index index of the requested data (QModelIndex) |
|
64 @param role role of the requested data (Qt.ItemDataRole) |
|
65 @return the requested data (QVariant) |
|
66 """ |
|
67 if not index.isValid(): |
|
68 return QVariant() |
|
69 |
|
70 if role == Qt.DisplayRole or role == Qt.ToolTipRole: |
|
71 if index.column() < len(self.header): |
|
72 return QVariant(self.watchpoints[index.row()][index.column()]) |
|
73 |
|
74 if role == Qt.TextAlignmentRole: |
|
75 if index.column() < len(self.alignments): |
|
76 return self.alignments[index.column()] |
|
77 |
|
78 return QVariant() |
|
79 |
|
80 def flags(self, index): |
|
81 """ |
|
82 Public method to get item flags. |
|
83 |
|
84 @param index index of the requested flags (QModelIndex) |
|
85 @return item flags for the given index (Qt.ItemFlags) |
|
86 """ |
|
87 if not index.isValid(): |
|
88 return Qt.ItemIsEnabled |
|
89 |
|
90 return Qt.ItemIsEnabled | Qt.ItemIsSelectable |
|
91 |
|
92 def headerData(self, section, orientation, role = Qt.DisplayRole): |
|
93 """ |
|
94 Public method to get header data. |
|
95 |
|
96 @param section section number of the requested header data (integer) |
|
97 @param orientation orientation of the header (Qt.Orientation) |
|
98 @param role role of the requested data (Qt.ItemDataRole) |
|
99 @return header data (QVariant) |
|
100 """ |
|
101 if orientation == Qt.Horizontal and role == Qt.DisplayRole: |
|
102 if section >= len(self.header): |
|
103 return QVariant("") |
|
104 else: |
|
105 return self.header[section] |
|
106 |
|
107 return QVariant() |
|
108 |
|
109 def index(self, row, column, parent = QModelIndex()): |
|
110 """ |
|
111 Public method to create an index. |
|
112 |
|
113 @param row row number for the index (integer) |
|
114 @param column column number for the index (integer) |
|
115 @param parent index of the parent item (QModelIndex) |
|
116 @return requested index (QModelIndex) |
|
117 """ |
|
118 if parent.isValid() or \ |
|
119 row < 0 or row >= len(self.watchpoints) or \ |
|
120 column < 0 or column >= len(self.header): |
|
121 return QModelIndex() |
|
122 |
|
123 return self.createIndex(row, column, self.watchpoints[row]) |
|
124 |
|
125 def parent(self, index): |
|
126 """ |
|
127 Public method to get the parent index. |
|
128 |
|
129 @param index index of item to get parent (QModelIndex) |
|
130 @return index of parent (QModelIndex) |
|
131 """ |
|
132 return QModelIndex() |
|
133 |
|
134 def hasChildren(self, parent = QModelIndex()): |
|
135 """ |
|
136 Public method to check for the presence of child items. |
|
137 |
|
138 @param parent index of parent item (QModelIndex) |
|
139 @return flag indicating the presence of child items (boolean) |
|
140 """ |
|
141 if not parent.isValid(): |
|
142 return len(self.watchpoints) > 0 |
|
143 else: |
|
144 return False |
|
145 |
|
146 ############################################################################ |
|
147 |
|
148 def addWatchPoint(self, cond, special, properties): |
|
149 """ |
|
150 Public method to add a new watch expression to the list. |
|
151 |
|
152 @param cond expression of the watch expression (string) |
|
153 @param special special condition of the watch expression (string) |
|
154 @param properties properties of the watch expression |
|
155 (tuple of temporary flag (bool), enabled flag (bool), ignore count (integer)) |
|
156 """ |
|
157 wp = [cond, special] + list(properties) |
|
158 cnt = len(self.watchpoints) |
|
159 self.beginInsertRows(QModelIndex(), cnt, cnt) |
|
160 self.watchpoints.append(wp) |
|
161 self.endInsertRows() |
|
162 |
|
163 def setWatchPointByIndex(self, index, cond, special, properties): |
|
164 """ |
|
165 Public method to set the values of a watch expression given by index. |
|
166 |
|
167 @param index index of the watch expression (QModelIndex) |
|
168 @param cond expression of the watch expression (string) |
|
169 @param special special condition of the watch expression (string) |
|
170 @param properties properties of the watch expression |
|
171 (tuple of temporary flag (bool), enabled flag (bool), ignore count (integer)) |
|
172 """ |
|
173 if index.isValid(): |
|
174 row = index.row() |
|
175 index1 = self.createIndex(row, 0, self.watchpoints[row]) |
|
176 index2 = self.createIndex(row, len(self.watchpoints[row]), |
|
177 self.watchpoints[row]) |
|
178 self.emit(\ |
|
179 SIGNAL("dataAboutToBeChanged(const QModelIndex &, const QModelIndex &)"), |
|
180 index1, index2) |
|
181 i = 0 |
|
182 for value in [cond, special] + list(properties): |
|
183 self.watchpoints[row][i] = value |
|
184 i += 1 |
|
185 self.emit(SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), |
|
186 index1, index2) |
|
187 |
|
188 def setWatchPointEnabledByIndex(self, index, enabled): |
|
189 """ |
|
190 Public method to set the enabled state of a watch expression given by index. |
|
191 |
|
192 @param index index of the watch expression (QModelIndex) |
|
193 @param enabled flag giving the enabled state (boolean) |
|
194 """ |
|
195 if index.isValid(): |
|
196 row = index.row() |
|
197 col = 3 |
|
198 index1 = self.createIndex(row, col, self.watchpoints[row]) |
|
199 self.emit(\ |
|
200 SIGNAL("dataAboutToBeChanged(const QModelIndex &, const QModelIndex &)"), |
|
201 index1, index1) |
|
202 self.watchpoints[row][col] = enabled |
|
203 self.emit(SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), |
|
204 index1, index1) |
|
205 |
|
206 def deleteWatchPointByIndex(self, index): |
|
207 """ |
|
208 Public method to set the values of a watch expression given by index. |
|
209 |
|
210 @param index index of the watch expression (QModelIndex) |
|
211 """ |
|
212 if index.isValid(): |
|
213 row = index.row() |
|
214 self.beginRemoveRows(QModelIndex(), row, row) |
|
215 del self.watchpoints[row] |
|
216 self.endRemoveRows() |
|
217 |
|
218 def deleteWatchPoints(self, idxList): |
|
219 """ |
|
220 Public method to delete a list of watch expressions given by their indexes. |
|
221 |
|
222 @param idxList list of watch expression indexes (list of QModelIndex) |
|
223 """ |
|
224 rows = [] |
|
225 for index in idxList: |
|
226 if index.isValid(): |
|
227 rows.append(index.row()) |
|
228 rows.sort(reverse = True) |
|
229 for row in rows: |
|
230 self.beginRemoveRows(QModelIndex(), row, row) |
|
231 del self.watchpoints[row] |
|
232 self.endRemoveRows() |
|
233 |
|
234 def deleteAll(self): |
|
235 """ |
|
236 Public method to delete all watch expressions. |
|
237 """ |
|
238 if self.watchpoints: |
|
239 self.beginRemoveRows(QModelIndex(), 0, len(self.watchpoints) - 1) |
|
240 self.watchpoints = [] |
|
241 self.endRemoveRows() |
|
242 |
|
243 def getWatchPointByIndex(self, index): |
|
244 """ |
|
245 Public method to get the values of a watch expression given by index. |
|
246 |
|
247 @param index index of the watch expression (QModelIndex) |
|
248 @return watch expression (list of six values (expression, special condition, |
|
249 temporary flag, enabled flag, ignore count, index)) |
|
250 """ |
|
251 if index.isValid(): |
|
252 return self.watchpoints[index.row()][:] # return a copy |
|
253 else: |
|
254 return [] |
|
255 |
|
256 def getWatchPointIndex(self, cond, special = ""): |
|
257 """ |
|
258 Public method to get the index of a watch expression given by expression. |
|
259 |
|
260 @param cond expression of the watch expression (string) |
|
261 @param special special condition of the watch expression (string) |
|
262 @return index (QModelIndex) |
|
263 """ |
|
264 for row in range(len(self.watchpoints)): |
|
265 wp = self.watchpoints[row] |
|
266 if wp[0] == cond: |
|
267 if special and wp[1] != special: |
|
268 continue |
|
269 return self.createIndex(row, 0, self.watchpoints[row]) |
|
270 |
|
271 return QModelIndex() |