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