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