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