Bug fix: closing the file
[codimension.git] / src / debugger / watchpointmodel.py
blob7fc8b02ea232e812e62de83f597e7730d026cd9d
2 # -*- coding: utf-8 -*-
4 # codimension - graphics python two-way code editor and analyzer
5 # Copyright (C) 2010 Sergey Satskiy <sergey.satskiy@gmail.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # $Id$
24 # The file was taken from eric 4 and adopted for codimension.
25 # Original copyright:
26 # Copyright (c) 2007 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
29 """
30 Module implementing the Watch expression model
31 """
33 from PyQt4.QtCore import ( QAbstractItemModel, QVariant, Qt, QModelIndex,
34 SIGNAL )
38 class WatchPointModel( QAbstractItemModel ):
39 " Class implementing a custom model for watch expressions "
40 def __init__( self, parent = None ):
41 QAbstractItemModel.__init__( self, parent )
43 self.watchpoints = []
44 self.header = [
45 QVariant( 'Condition' ),
46 QVariant( 'Special' ),
47 QVariant( 'Temporary' ),
48 QVariant( 'Enabled' ),
49 QVariant( 'Ignore Count' ),
51 self.alignments = [
52 QVariant( Qt.Alignment( Qt.AlignLeft ) ),
53 QVariant( Qt.Alignment( Qt.AlignLeft ) ),
54 QVariant( Qt.Alignment( Qt.AlignHCenter ) ),
55 QVariant( Qt.Alignment( Qt.AlignHCenter ) ),
56 QVariant( Qt.Alignment( Qt.AlignRight ) ),
58 return
60 def columnCount( self, parent = QModelIndex() ):
61 " Provides the current column count "
62 return len( self.header ) + 1
64 def rowCount( self, parent = QModelIndex() ):
65 " Provides the current row count "
66 # we do not have a tree, parent should always be invalid
67 if not parent.isValid():
68 return len( self.watchpoints )
69 return 0
71 def data( self, index, role ):
72 """ Provides the requested data.
74 @param index index of the requested data (QModelIndex)
75 @param role role of the requested data (Qt.ItemDataRole)
76 @return the requested data (QVariant)
77 """
78 if not index.isValid():
79 return QVariant()
81 if role == Qt.DisplayRole or role == Qt.ToolTipRole:
82 if index.column() < len( self.header ):
83 return QVariant(
84 self.watchpoints[ index.row() ][ index.column() ] )
86 if role == Qt.TextAlignmentRole:
87 if index.column() < len( self.alignments ):
88 return self.alignments[ index.column() ]
90 return QVariant()
92 def flags( self, index ):
93 """ Provides the item flags.
95 @param index index of the requested flags (QModelIndex)
96 @return item flags for the given index (Qt.ItemFlags)
97 """
98 if not index.isValid():
99 return Qt.ItemIsEnabled
101 return Qt.ItemIsEnabled | Qt.ItemIsSelectable
103 def headerData( self, section, orientation, role = Qt.DisplayRole ):
105 Public method to get header data.
107 @param section section number of the requested header data (integer)
108 @param orientation orientation of the header (Qt.Orientation)
109 @param role role of the requested data (Qt.ItemDataRole)
110 @return header data (QVariant)
112 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
113 if section >= len( self.header ):
114 return QVariant( "" )
115 return self.header[ section ]
117 return QVariant()
119 def index( self, row, column, parent = QModelIndex() ):
121 Public method to create an index.
123 @param row row number for the index (integer)
124 @param column column number for the index (integer)
125 @param parent index of the parent item (QModelIndex)
126 @return requested index (QModelIndex)
128 if parent.isValid() or \
129 row < 0 or row >= len( self.watchpoints ) or \
130 column < 0 or column >= len( self.header ):
131 return QModelIndex()
133 return self.createIndex( row, column, self.watchpoints[ row ] )
135 def parent( self, index ):
137 Public method to get the parent index.
139 @param index index of item to get parent (QModelIndex)
140 @return index of parent (QModelIndex)
142 return QModelIndex()
144 def hasChildren( self, parent = QModelIndex() ):
146 Public method to check for the presence of child items.
148 @param parent index of parent item (QModelIndex)
149 @return flag indicating the presence of child items (boolean)
151 if not parent.isValid():
152 return len( self.watchpoints ) > 0
153 return False
156 def addWatchPoint( self, cond, special, properties ):
158 Public method to add a new watch expression to the list.
160 @param cond expression of the watch expression (string)
161 @param special special condition of the watch expression (string)
162 @param properties properties of the watch expression
163 (tuple of temporary flag (bool), enabled flag (bool), ignore count (integer))
165 wpoint = [ unicode( cond ), unicode( special ) ] + list( properties )
166 cnt = len( self.watchpoints )
167 self.beginInsertRows( QModelIndex(), cnt, cnt )
168 self.watchpoints.append( wpoint )
169 self.endInsertRows()
170 return
172 def setWatchPointByIndex( self, index, cond, special, properties ):
174 Public method to set the values of a watch expression given by index.
176 @param index index of the watch expression (QModelIndex)
177 @param cond expression of the watch expression (string)
178 @param special special condition of the
179 watch expression (string)
180 @param properties properties of the watch expression
181 (tuple of temporary flag (bool), enabled flag (bool),
182 ignore count (integer))
184 if index.isValid():
185 row = index.row()
186 index1 = self.createIndex( row, 0, self.watchpoints[row] )
187 index2 = self.createIndex( row, len( self.watchpoints[ row ] ),
188 self.watchpoints[ row ] )
189 self.emit(
190 SIGNAL( "dataAboutToBeChanged(const QModelIndex&, const QModelIndex&)"),
191 index1, index2 )
192 i = 0
193 for value in [ unicode( cond ), unicode( special ) ] + \
194 list( properties ):
195 self.watchpoints[ row ][ i ] = value
196 i += 1
197 self.emit(
198 SIGNAL( "dataChanged(const QModelIndex&, const QModelIndex&)" ),
199 index1, index2 )
200 return
202 def setWatchPointEnabledByIndex( self, index, enabled ):
204 Public method to set the enabled state of a
205 watch expression given by index.
207 @param index index of the watch expression (QModelIndex)
208 @param enabled flag giving the enabled state (boolean)
210 if index.isValid():
211 row = index.row()
212 col = 3
213 index1 = self.createIndex( row, col, self.watchpoints[ row ] )
214 self.emit(
215 SIGNAL( "dataAboutToBeChanged(const QModelIndex&, const QModelIndex&)" ),
216 index1, index1 )
217 self.watchpoints[ row ][ col ] = enabled
218 self.emit(
219 SIGNAL( "dataChanged(const QModelIndex&, const QModelIndex&)" ),
220 index1, index1 )
221 return
223 def deleteWatchPointByIndex( self, index ):
225 Public method to set the values of a watch expression given by index.
227 @param index index of the watch expression (QModelIndex)
229 if index.isValid():
230 row = index.row()
231 self.beginRemoveRows( QModelIndex(), row, row )
232 del self.watchpoints[ row ]
233 self.endRemoveRows()
234 return
236 def deleteWatchPoints( self, idxList ):
238 Public method to delete a list of watch expressions
239 given by their indexes.
241 @param idxList list of watch expression indexes (list of QModelIndex)
243 rows = []
244 for index in idxList:
245 if index.isValid():
246 rows.append(index.row())
247 rows.sort(reverse = True)
248 for row in rows:
249 self.beginRemoveRows( QModelIndex(), row, row )
250 del self.watchpoints[ row ]
251 self.endRemoveRows()
252 return
254 def deleteAll( self ):
255 " Deletes all watch expressions "
256 if self.watchpoints:
257 self.beginRemoveRows( QModelIndex(), 0,
258 len( self.watchpoints ) - 1 )
259 self.watchpoints = []
260 self.endRemoveRows()
261 return
263 def getWatchPointByIndex( self, index ):
265 Public method to get the values of a watch expression given by index.
267 @param index index of the watch expression (QModelIndex)
268 @return watch expression (list of six values (expression,
269 special condition, temporary flag, enabled flag,
270 ignore count, index))
272 if index.isValid():
273 return self.watchpoints[ index.row() ][ : ] # return a copy
274 return []
276 def getWatchPointIndex( self, cond, special = "" ):
278 Public method to get the index of a watch expression
279 given by expression.
281 @param cond expression of the watch expression (string)
282 @param special special condition of the
283 watch expression (string)
284 @return index (QModelIndex)
286 cond = unicode( cond )
287 special = unicode( special )
288 for row in xrange( len( self.watchpoints ) ):
289 wpoint = self.watchpoints[ row ]
290 if unicode( wpoint[ 0 ] ) == cond:
291 if special and unicode( wpoint[ 1 ] ) != special:
292 continue
293 return self.createIndex( row, 0, self.watchpoints[ row ] )
295 return QModelIndex()