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