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/>.
24 # The file was taken from eric 4 and adopted for codimension.
26 # Copyright (c) 2007 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
30 Module implementing the Watch expression model
33 from PyQt4
.QtCore
import ( QAbstractItemModel
, QVariant
, Qt
, QModelIndex
,
38 class WatchPointModel( QAbstractItemModel
):
39 " Class implementing a custom model for watch expressions "
40 def __init__( self
, parent
= None ):
41 QAbstractItemModel
.__init
__( self
, parent
)
45 QVariant( 'Condition' ),
46 QVariant( 'Special' ),
47 QVariant( 'Temporary' ),
48 QVariant( 'Enabled' ),
49 QVariant( 'Ignore Count' ),
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
) ),
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
)
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)
78 if not index
.isValid():
81 if role
== Qt
.DisplayRole
or role
== Qt
.ToolTipRole
:
82 if index
.column() < len( self
.header
):
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() ]
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)
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
]
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
):
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)
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
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
)
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))
186 index1
= self
.createIndex( row
, 0, self
.watchpoints
[row
] )
187 index2
= self
.createIndex( row
, len( self
.watchpoints
[ row
] ),
188 self
.watchpoints
[ row
] )
190 SIGNAL( "dataAboutToBeChanged(const QModelIndex&, const QModelIndex&)"),
193 for value
in [ unicode( cond
), unicode( special
) ] + \
195 self
.watchpoints
[ row
][ i
] = value
198 SIGNAL( "dataChanged(const QModelIndex&, const QModelIndex&)" ),
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)
213 index1
= self
.createIndex( row
, col
, self
.watchpoints
[ row
] )
215 SIGNAL( "dataAboutToBeChanged(const QModelIndex&, const QModelIndex&)" ),
217 self
.watchpoints
[ row
][ col
] = enabled
219 SIGNAL( "dataChanged(const QModelIndex&, const QModelIndex&)" ),
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)
231 self
.beginRemoveRows( QModelIndex(), row
, row
)
232 del self
.watchpoints
[ row
]
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)
244 for index
in idxList
:
246 rows
.append(index
.row())
247 rows
.sort(reverse
= True)
249 self
.beginRemoveRows( QModelIndex(), row
, row
)
250 del self
.watchpoints
[ row
]
254 def deleteAll( self
):
255 " Deletes all watch expressions "
257 self
.beginRemoveRows( QModelIndex(), 0,
258 len( self
.watchpoints
) - 1 )
259 self
.watchpoints
= []
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))
273 return self
.watchpoints
[ index
.row() ][ : ] # return a copy
276 def getWatchPointIndex( self
, cond
, special
= "" ):
278 Public method to get the index of a watch 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
:
293 return self
.createIndex( row
, 0, self
.watchpoints
[ row
] )