| Line | Source Code | Coverage |
|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
| 4 | ** Contact: http://www.qt-project.org/legal | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | - |
| 7 | ** | - |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 9 | ** Commercial License Usage | - |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 11 | ** accordance with the commercial license agreement provided with the | - |
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 13 | ** a written agreement between you and Digia. For licensing terms and | - |
| 14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
| 15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
| 16 | ** | - |
| 17 | ** GNU Lesser General Public License Usage | - |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 19 | ** General Public License version 2.1 as published by the Free Software | - |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
| 21 | ** packaging of this file. Please review the following information to | - |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
| 24 | ** | - |
| 25 | ** In addition, as a special exception, Digia gives you certain additional | - |
| 26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
| 28 | ** | - |
| 29 | ** GNU General Public License Usage | - |
| 30 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 31 | ** General Public License version 3.0 as published by the Free Software | - |
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
| 33 | ** packaging of this file. Please review the following information to | - |
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
| 36 | ** | - |
| 37 | ** | - |
| 38 | ** $QT_END_LICENSE$ | - |
| 39 | ** | - |
| 40 | ****************************************************************************/ | - |
| 41 | | - |
| 42 | #include "qabstractitemdelegate.h" | - |
| 43 | | - |
| 44 | #ifndef QT_NO_ITEMVIEWS | - |
| 45 | #include <qabstractitemmodel.h> | - |
| 46 | #include <qabstractitemview.h> | - |
| 47 | #include <qfontmetrics.h> | - |
| 48 | #include <qwhatsthis.h> | - |
| 49 | #include <qtooltip.h> | - |
| 50 | #include <qevent.h> | - |
| 51 | #include <qstring.h> | - |
| 52 | #include <qdebug.h> | - |
| 53 | #include <private/qtextengine_p.h> | - |
| 54 | | - |
| 55 | QT_BEGIN_NAMESPACE | - |
| 56 | | - |
| 57 | /*! | - |
| 58 | \class QAbstractItemDelegate | - |
| 59 | | - |
| 60 | \brief The QAbstractItemDelegate class is used to display and edit | - |
| 61 | data items from a model. | - |
| 62 | | - |
| 63 | \ingroup model-view | - |
| 64 | \inmodule QtWidgets | - |
| 65 | | - |
| 66 | A QAbstractItemDelegate provides the interface and common functionality | - |
| 67 | for delegates in the model/view architecture. Delegates display | - |
| 68 | individual items in views, and handle the editing of model data. | - |
| 69 | | - |
| 70 | The QAbstractItemDelegate class is one of the \l{Model/View Classes} | - |
| 71 | and is part of Qt's \l{Model/View Programming}{model/view framework}. | - |
| 72 | | - |
| 73 | To render an item in a custom way, you must implement paint() and | - |
| 74 | sizeHint(). The QItemDelegate class provides default implementations for | - |
| 75 | these functions; if you do not need custom rendering, subclass that | - |
| 76 | class instead. | - |
| 77 | | - |
| 78 | We give an example of drawing a progress bar in items; in our case | - |
| 79 | for a package management program. | - |
| 80 | | - |
| 81 | \image widgetdelegate.png | - |
| 82 | | - |
| 83 | We create the \c WidgetDelegate class, which inherits from | - |
| 84 | QStyledItemDelegate. We do the drawing in the paint() function: | - |
| 85 | | - |
| 86 | \snippet widgetdelegate.cpp 0 | - |
| 87 | | - |
| 88 | Notice that we use a QStyleOptionProgressBar and initialize its | - |
| 89 | members. We can then use the current QStyle to draw it. | - |
| 90 | | - |
| 91 | To provide custom editing, there are two approaches that can be | - |
| 92 | used. The first approach is to create an editor widget and display | - |
| 93 | it directly on top of the item. To do this you must reimplement | - |
| 94 | createEditor() to provide an editor widget, setEditorData() to populate | - |
| 95 | the editor with the data from the model, and setModelData() so that the | - |
| 96 | delegate can update the model with data from the editor. | - |
| 97 | | - |
| 98 | The second approach is to handle user events directly by reimplementing | - |
| 99 | editorEvent(). | - |
| 100 | | - |
| 101 | \sa {model-view-programming}{Model/View Programming}, QItemDelegate, | - |
| 102 | {Pixelator Example}, QStyledItemDelegate, QStyle | - |
| 103 | */ | - |
| 104 | | - |
| 105 | /*! | - |
| 106 | \enum QAbstractItemDelegate::EndEditHint | - |
| 107 | | - |
| 108 | This enum describes the different hints that the delegate can give to the | - |
| 109 | model and view components to make editing data in a model a comfortable | - |
| 110 | experience for the user. | - |
| 111 | | - |
| 112 | \value NoHint There is no recommended action to be performed. | - |
| 113 | | - |
| 114 | These hints let the delegate influence the behavior of the view: | - |
| 115 | | - |
| 116 | \value EditNextItem The view should use the delegate to open an | - |
| 117 | editor on the next item in the view. | - |
| 118 | \value EditPreviousItem The view should use the delegate to open an | - |
| 119 | editor on the previous item in the view. | - |
| 120 | | - |
| 121 | Note that custom views may interpret the concepts of next and previous | - |
| 122 | differently. | - |
| 123 | | - |
| 124 | The following hints are most useful when models are used that cache | - |
| 125 | data, such as those that manipulate data locally in order to increase | - |
| 126 | performance or conserve network bandwidth. | - |
| 127 | | - |
| 128 | \value SubmitModelCache If the model caches data, it should write out | - |
| 129 | cached data to the underlying data store. | - |
| 130 | \value RevertModelCache If the model caches data, it should discard | - |
| 131 | cached data and replace it with data from the | - |
| 132 | underlying data store. | - |
| 133 | | - |
| 134 | Although models and views should respond to these hints in appropriate | - |
| 135 | ways, custom components may ignore any or all of them if they are not | - |
| 136 | relevant. | - |
| 137 | */ | - |
| 138 | | - |
| 139 | /*! | - |
| 140 | \fn void QAbstractItemDelegate::commitData(QWidget *editor) | - |
| 141 | | - |
| 142 | This signal must be emitted when the \a editor widget has completed | - |
| 143 | editing the data, and wants to write it back into the model. | - |
| 144 | */ | - |
| 145 | | - |
| 146 | /*! | - |
| 147 | \fn void QAbstractItemDelegate::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) | - |
| 148 | | - |
| 149 | This signal is emitted when the user has finished editing an item using | - |
| 150 | the specified \a editor. | - |
| 151 | | - |
| 152 | The \a hint provides a way for the delegate to influence how the model and | - |
| 153 | view behave after editing is completed. It indicates to these components | - |
| 154 | what action should be performed next to provide a comfortable editing | - |
| 155 | experience for the user. For example, if \c EditNextItem is specified, | - |
| 156 | the view should use a delegate to open an editor on the next item in the | - |
| 157 | model. | - |
| 158 | | - |
| 159 | \sa EndEditHint | - |
| 160 | */ | - |
| 161 | | - |
| 162 | /*! | - |
| 163 | \fn void QAbstractItemDelegate::sizeHintChanged(const QModelIndex &index) | - |
| 164 | \since 4.4 | - |
| 165 | | - |
| 166 | This signal must be emitted when the sizeHint() of \a index changed. | - |
| 167 | | - |
| 168 | Views automatically connect to this signal and relayout items as necessary. | - |
| 169 | */ | - |
| 170 | | - |
| 171 | | - |
| 172 | /*! | - |
| 173 | Creates a new abstract item delegate with the given \a parent. | - |
| 174 | */ | - |
| 175 | QAbstractItemDelegate::QAbstractItemDelegate(QObject *parent) | - |
| 176 | : QObject(parent) | - |
| 177 | { | - |
| 178 | | - |
| 179 | } executed: }Execution Count:144 | 144 |
| 180 | | - |
| 181 | /*! | - |
| 182 | \internal | - |
| 183 | | - |
| 184 | Creates a new abstract item delegate with the given \a parent. | - |
| 185 | */ | - |
| 186 | QAbstractItemDelegate::QAbstractItemDelegate(QObjectPrivate &dd, QObject *parent) | - |
| 187 | : QObject(dd, parent) | - |
| 188 | { | - |
| 189 | | - |
| 190 | } executed: }Execution Count:4684 | 4684 |
| 191 | | - |
| 192 | /*! | - |
| 193 | Destroys the abstract item delegate. | - |
| 194 | */ | - |
| 195 | QAbstractItemDelegate::~QAbstractItemDelegate() | - |
| 196 | { | - |
| 197 | | - |
| 198 | } | - |
| 199 | | - |
| 200 | /*! | - |
| 201 | \fn void QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const = 0; | - |
| 202 | | - |
| 203 | This pure abstract function must be reimplemented if you want to | - |
| 204 | provide custom rendering. Use the \a painter and style \a option to | - |
| 205 | render the item specified by the item \a index. | - |
| 206 | | - |
| 207 | If you reimplement this you must also reimplement sizeHint(). | - |
| 208 | */ | - |
| 209 | | - |
| 210 | /*! | - |
| 211 | \fn QSize QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const = 0 | - |
| 212 | | - |
| 213 | This pure abstract function must be reimplemented if you want to | - |
| 214 | provide custom rendering. The options are specified by \a option | - |
| 215 | and the model item by \a index. | - |
| 216 | | - |
| 217 | If you reimplement this you must also reimplement paint(). | - |
| 218 | */ | - |
| 219 | | - |
| 220 | /*! | - |
| 221 | Returns the editor to be used for editing the data item with the | - |
| 222 | given \a index. Note that the index contains information about the | - |
| 223 | model being used. The editor's parent widget is specified by \a parent, | - |
| 224 | and the item options by \a option. | - |
| 225 | | - |
| 226 | The base implementation returns 0. If you want custom editing you | - |
| 227 | will need to reimplement this function. | - |
| 228 | | - |
| 229 | The returned editor widget should have Qt::StrongFocus; | - |
| 230 | otherwise, \l{QMouseEvent}s received by the widget will propagate | - |
| 231 | to the view. The view's background will shine through unless the | - |
| 232 | editor paints its own background (e.g., with | - |
| 233 | \l{QWidget::}{setAutoFillBackground()}). | - |
| 234 | | - |
| 235 | \sa destroyEditor(), setModelData(), setEditorData() | - |
| 236 | */ | - |
| 237 | QWidget *QAbstractItemDelegate::createEditor(QWidget *, | - |
| 238 | const QStyleOptionViewItem &, | - |
| 239 | const QModelIndex &) const | - |
| 240 | { | - |
| 241 | return 0; never executed: return 0; | 0 |
| 242 | } | - |
| 243 | | - |
| 244 | | - |
| 245 | /*! | - |
| 246 | Called when the \a editor is no longer needed for editing the data item | - |
| 247 | with the given \a index and should be destroyed. The default behavior is a | - |
| 248 | call to deleteLater on the editor. It is possible e.g. to avoid this delete by | - |
| 249 | reimplementing this function. | - |
| 250 | | - |
| 251 | \since 5.0 | - |
| 252 | \sa createEditor() | - |
| 253 | */ | - |
| 254 | void QAbstractItemDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const | - |
| 255 | { | - |
| 256 | Q_UNUSED(index); executed (the execution status of this line is deduced): (void)index;; | - |
| 257 | editor->deleteLater(); executed (the execution status of this line is deduced): editor->deleteLater(); | - |
| 258 | } executed: }Execution Count:80 | 80 |
| 259 | | - |
| 260 | /*! | - |
| 261 | Sets the contents of the given \a editor to the data for the item | - |
| 262 | at the given \a index. Note that the index contains information | - |
| 263 | about the model being used. | - |
| 264 | | - |
| 265 | The base implementation does nothing. If you want custom editing | - |
| 266 | you will need to reimplement this function. | - |
| 267 | | - |
| 268 | \sa setModelData() | - |
| 269 | */ | - |
| 270 | void QAbstractItemDelegate::setEditorData(QWidget *, | - |
| 271 | const QModelIndex &) const | - |
| 272 | { | - |
| 273 | // do nothing | - |
| 274 | } | - |
| 275 | | - |
| 276 | /*! | - |
| 277 | Sets the data for the item at the given \a index in the \a model | - |
| 278 | to the contents of the given \a editor. | - |
| 279 | | - |
| 280 | The base implementation does nothing. If you want custom editing | - |
| 281 | you will need to reimplement this function. | - |
| 282 | | - |
| 283 | \sa setEditorData() | - |
| 284 | */ | - |
| 285 | void QAbstractItemDelegate::setModelData(QWidget *, | - |
| 286 | QAbstractItemModel *, | - |
| 287 | const QModelIndex &) const | - |
| 288 | { | - |
| 289 | // do nothing | - |
| 290 | } | - |
| 291 | | - |
| 292 | /*! | - |
| 293 | Updates the geometry of the \a editor for the item with the given | - |
| 294 | \a index, according to the rectangle specified in the \a option. | - |
| 295 | If the item has an internal layout, the editor will be laid out | - |
| 296 | accordingly. Note that the index contains information about the | - |
| 297 | model being used. | - |
| 298 | | - |
| 299 | The base implementation does nothing. If you want custom editing | - |
| 300 | you must reimplement this function. | - |
| 301 | */ | - |
| 302 | void QAbstractItemDelegate::updateEditorGeometry(QWidget *, | - |
| 303 | const QStyleOptionViewItem &, | - |
| 304 | const QModelIndex &) const | - |
| 305 | { | - |
| 306 | // do nothing | - |
| 307 | } | - |
| 308 | | - |
| 309 | /*! | - |
| 310 | When editing of an item starts, this function is called with the | - |
| 311 | \a event that triggered the editing, the \a model, the \a index of | - |
| 312 | the item, and the \a option used for rendering the item. | - |
| 313 | | - |
| 314 | Mouse events are sent to editorEvent() even if they don't start | - |
| 315 | editing of the item. This can, for instance, be useful if you wish | - |
| 316 | to open a context menu when the right mouse button is pressed on | - |
| 317 | an item. | - |
| 318 | | - |
| 319 | The base implementation returns false (indicating that it has not | - |
| 320 | handled the event). | - |
| 321 | */ | - |
| 322 | bool QAbstractItemDelegate::editorEvent(QEvent *, | - |
| 323 | QAbstractItemModel *, | - |
| 324 | const QStyleOptionViewItem &, | - |
| 325 | const QModelIndex &) | - |
| 326 | { | - |
| 327 | // do nothing | - |
| 328 | return false; never executed: return false; | 0 |
| 329 | } | - |
| 330 | | - |
| 331 | /*! | - |
| 332 | \obsolete | - |
| 333 | | - |
| 334 | Use QFontMetrics::elidedText() instead. | - |
| 335 | | - |
| 336 | \oldcode | - |
| 337 | QFontMetrics fm = ... | - |
| 338 | QString str = QAbstractItemDelegate::elidedText(fm, width, mode, text); | - |
| 339 | \newcode | - |
| 340 | QFontMetrics fm = ... | - |
| 341 | QString str = fm.elidedText(text, mode, width); | - |
| 342 | \endcode | - |
| 343 | */ | - |
| 344 | | - |
| 345 | QString QAbstractItemDelegate::elidedText(const QFontMetrics &fontMetrics, int width, | - |
| 346 | Qt::TextElideMode mode, const QString &text) | - |
| 347 | { | - |
| 348 | return fontMetrics.elidedText(text, mode, width); never executed: return fontMetrics.elidedText(text, mode, width); | 0 |
| 349 | } | - |
| 350 | | - |
| 351 | /*! | - |
| 352 | \since 4.3 | - |
| 353 | Whenever a help event occurs, this function is called with the \a event | - |
| 354 | \a view \a option and the \a index that corresponds to the item where the | - |
| 355 | event occurs. | - |
| 356 | | - |
| 357 | Returns true if the delegate can handle the event; otherwise returns false. | - |
| 358 | A return value of true indicates that the data obtained using the index had | - |
| 359 | the required role. | - |
| 360 | | - |
| 361 | For QEvent::ToolTip and QEvent::WhatsThis events that were handled successfully, | - |
| 362 | the relevant popup may be shown depending on the user's system configuration. | - |
| 363 | | - |
| 364 | \sa QHelpEvent | - |
| 365 | */ | - |
| 366 | bool QAbstractItemDelegate::helpEvent(QHelpEvent *event, | - |
| 367 | QAbstractItemView *view, | - |
| 368 | const QStyleOptionViewItem &option, | - |
| 369 | const QModelIndex &index) | - |
| 370 | { | - |
| 371 | Q_UNUSED(option); never executed (the execution status of this line is deduced): (void)option;; | - |
| 372 | | - |
| 373 | if (!event || !view) never evaluated: !event never evaluated: !view | 0 |
| 374 | return false; never executed: return false; | 0 |
| 375 | switch (event->type()) { | - |
| 376 | #ifndef QT_NO_TOOLTIP | - |
| 377 | case QEvent::ToolTip: { | - |
| 378 | QHelpEvent *he = static_cast<QHelpEvent*>(event); never executed (the execution status of this line is deduced): QHelpEvent *he = static_cast<QHelpEvent*>(event); | - |
| 379 | QVariant tooltip = index.data(Qt::ToolTipRole); never executed (the execution status of this line is deduced): QVariant tooltip = index.data(Qt::ToolTipRole); | - |
| 380 | if (tooltip.canConvert<QString>()) { never evaluated: tooltip.canConvert<QString>() | 0 |
| 381 | QToolTip::showText(he->globalPos(), tooltip.toString(), view); never executed (the execution status of this line is deduced): QToolTip::showText(he->globalPos(), tooltip.toString(), view); | - |
| 382 | return true; never executed: return true; | 0 |
| 383 | } | - |
| 384 | break;} | 0 |
| 385 | #endif | - |
| 386 | #ifndef QT_NO_WHATSTHIS | - |
| 387 | case QEvent::QueryWhatsThis: { | - |
| 388 | if (index.data(Qt::WhatsThisRole).isValid()) never evaluated: index.data(Qt::WhatsThisRole).isValid() | 0 |
| 389 | return true; never executed: return true; | 0 |
| 390 | break; } | 0 |
| 391 | case QEvent::WhatsThis: { | - |
| 392 | QHelpEvent *he = static_cast<QHelpEvent*>(event); never executed (the execution status of this line is deduced): QHelpEvent *he = static_cast<QHelpEvent*>(event); | - |
| 393 | QVariant whatsthis = index.data(Qt::WhatsThisRole); never executed (the execution status of this line is deduced): QVariant whatsthis = index.data(Qt::WhatsThisRole); | - |
| 394 | if (whatsthis.canConvert<QString>()) { never evaluated: whatsthis.canConvert<QString>() | 0 |
| 395 | QWhatsThis::showText(he->globalPos(), whatsthis.toString(), view); never executed (the execution status of this line is deduced): QWhatsThis::showText(he->globalPos(), whatsthis.toString(), view); | - |
| 396 | return true; never executed: return true; | 0 |
| 397 | } | - |
| 398 | break ; } | 0 |
| 399 | #endif | - |
| 400 | default: | - |
| 401 | break; | 0 |
| 402 | } | - |
| 403 | return false; never executed: return false; | 0 |
| 404 | } | - |
| 405 | | - |
| 406 | /*! | - |
| 407 | \internal | - |
| 408 | | - |
| 409 | This virtual method is reserved and will be used in Qt 5.1. | - |
| 410 | */ | - |
| 411 | QVector<int> QAbstractItemDelegate::paintingRoles() const | - |
| 412 | { | - |
| 413 | return QVector<int>(); never executed: return QVector<int>(); | 0 |
| 414 | } | - |
| 415 | | - |
| 416 | QT_END_NAMESPACE | - |
| 417 | | - |
| 418 | #endif // QT_NO_ITEMVIEWS | - |
| 419 | | - |
| | |