| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qabstractitemdelegate.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtWidgets 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 The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | - | ||||||||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
| 24 | ** | - | ||||||||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
| 35 | ** | - | ||||||||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 37 | ** | - | ||||||||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | #include "qabstractitemdelegate.h" | - | ||||||||||||||||||
| 41 | - | |||||||||||||||||||
| 42 | #ifndef QT_NO_ITEMVIEWS | - | ||||||||||||||||||
| 43 | #include <qabstractitemmodel.h> | - | ||||||||||||||||||
| 44 | #include <qabstractitemview.h> | - | ||||||||||||||||||
| 45 | #include <qfontmetrics.h> | - | ||||||||||||||||||
| 46 | #include <qwhatsthis.h> | - | ||||||||||||||||||
| 47 | #include <qtooltip.h> | - | ||||||||||||||||||
| 48 | #include <qevent.h> | - | ||||||||||||||||||
| 49 | #include <qstring.h> | - | ||||||||||||||||||
| 50 | #include <qdebug.h> | - | ||||||||||||||||||
| 51 | #include <qlineedit.h> | - | ||||||||||||||||||
| 52 | #include <qtextedit.h> | - | ||||||||||||||||||
| 53 | #include <qplaintextedit.h> | - | ||||||||||||||||||
| 54 | #include <qapplication.h> | - | ||||||||||||||||||
| 55 | #include <private/qtextengine_p.h> | - | ||||||||||||||||||
| 56 | #include <private/qabstractitemdelegate_p.h> | - | ||||||||||||||||||
| 57 | - | |||||||||||||||||||
| 58 | #include <qpa/qplatformintegration.h> | - | ||||||||||||||||||
| 59 | #include <qpa/qplatformdrag.h> | - | ||||||||||||||||||
| 60 | #include <private/qguiapplication_p.h> | - | ||||||||||||||||||
| 61 | #include <private/qdnd_p.h> | - | ||||||||||||||||||
| 62 | - | |||||||||||||||||||
| 63 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 64 | - | |||||||||||||||||||
| 65 | /*! | - | ||||||||||||||||||
| 66 | \class QAbstractItemDelegate | - | ||||||||||||||||||
| 67 | - | |||||||||||||||||||
| 68 | \brief The QAbstractItemDelegate class is used to display and edit | - | ||||||||||||||||||
| 69 | data items from a model. | - | ||||||||||||||||||
| 70 | - | |||||||||||||||||||
| 71 | \ingroup model-view | - | ||||||||||||||||||
| 72 | \inmodule QtWidgets | - | ||||||||||||||||||
| 73 | - | |||||||||||||||||||
| 74 | A QAbstractItemDelegate provides the interface and common functionality | - | ||||||||||||||||||
| 75 | for delegates in the model/view architecture. Delegates display | - | ||||||||||||||||||
| 76 | individual items in views, and handle the editing of model data. | - | ||||||||||||||||||
| 77 | - | |||||||||||||||||||
| 78 | The QAbstractItemDelegate class is one of the \l{Model/View Classes} | - | ||||||||||||||||||
| 79 | and is part of Qt's \l{Model/View Programming}{model/view framework}. | - | ||||||||||||||||||
| 80 | - | |||||||||||||||||||
| 81 | To render an item in a custom way, you must implement paint() and | - | ||||||||||||||||||
| 82 | sizeHint(). The QItemDelegate class provides default implementations for | - | ||||||||||||||||||
| 83 | these functions; if you do not need custom rendering, subclass that | - | ||||||||||||||||||
| 84 | class instead. | - | ||||||||||||||||||
| 85 | - | |||||||||||||||||||
| 86 | We give an example of drawing a progress bar in items; in our case | - | ||||||||||||||||||
| 87 | for a package management program. | - | ||||||||||||||||||
| 88 | - | |||||||||||||||||||
| 89 | \image widgetdelegate.png | - | ||||||||||||||||||
| 90 | - | |||||||||||||||||||
| 91 | We create the \c WidgetDelegate class, which inherits from | - | ||||||||||||||||||
| 92 | QStyledItemDelegate. We do the drawing in the paint() function: | - | ||||||||||||||||||
| 93 | - | |||||||||||||||||||
| 94 | \snippet widgetdelegate.cpp 0 | - | ||||||||||||||||||
| 95 | - | |||||||||||||||||||
| 96 | Notice that we use a QStyleOptionProgressBar and initialize its | - | ||||||||||||||||||
| 97 | members. We can then use the current QStyle to draw it. | - | ||||||||||||||||||
| 98 | - | |||||||||||||||||||
| 99 | To provide custom editing, there are two approaches that can be | - | ||||||||||||||||||
| 100 | used. The first approach is to create an editor widget and display | - | ||||||||||||||||||
| 101 | it directly on top of the item. To do this you must reimplement | - | ||||||||||||||||||
| 102 | createEditor() to provide an editor widget, setEditorData() to populate | - | ||||||||||||||||||
| 103 | the editor with the data from the model, and setModelData() so that the | - | ||||||||||||||||||
| 104 | delegate can update the model with data from the editor. | - | ||||||||||||||||||
| 105 | - | |||||||||||||||||||
| 106 | The second approach is to handle user events directly by reimplementing | - | ||||||||||||||||||
| 107 | editorEvent(). | - | ||||||||||||||||||
| 108 | - | |||||||||||||||||||
| 109 | \sa {model-view-programming}{Model/View Programming}, QItemDelegate, | - | ||||||||||||||||||
| 110 | {Pixelator Example}, QStyledItemDelegate, QStyle | - | ||||||||||||||||||
| 111 | */ | - | ||||||||||||||||||
| 112 | - | |||||||||||||||||||
| 113 | /*! | - | ||||||||||||||||||
| 114 | \enum QAbstractItemDelegate::EndEditHint | - | ||||||||||||||||||
| 115 | - | |||||||||||||||||||
| 116 | This enum describes the different hints that the delegate can give to the | - | ||||||||||||||||||
| 117 | model and view components to make editing data in a model a comfortable | - | ||||||||||||||||||
| 118 | experience for the user. | - | ||||||||||||||||||
| 119 | - | |||||||||||||||||||
| 120 | \value NoHint There is no recommended action to be performed. | - | ||||||||||||||||||
| 121 | - | |||||||||||||||||||
| 122 | These hints let the delegate influence the behavior of the view: | - | ||||||||||||||||||
| 123 | - | |||||||||||||||||||
| 124 | \value EditNextItem The view should use the delegate to open an | - | ||||||||||||||||||
| 125 | editor on the next item in the view. | - | ||||||||||||||||||
| 126 | \value EditPreviousItem The view should use the delegate to open an | - | ||||||||||||||||||
| 127 | editor on the previous item in the view. | - | ||||||||||||||||||
| 128 | - | |||||||||||||||||||
| 129 | Note that custom views may interpret the concepts of next and previous | - | ||||||||||||||||||
| 130 | differently. | - | ||||||||||||||||||
| 131 | - | |||||||||||||||||||
| 132 | The following hints are most useful when models are used that cache | - | ||||||||||||||||||
| 133 | data, such as those that manipulate data locally in order to increase | - | ||||||||||||||||||
| 134 | performance or conserve network bandwidth. | - | ||||||||||||||||||
| 135 | - | |||||||||||||||||||
| 136 | \value SubmitModelCache If the model caches data, it should write out | - | ||||||||||||||||||
| 137 | cached data to the underlying data store. | - | ||||||||||||||||||
| 138 | \value RevertModelCache If the model caches data, it should discard | - | ||||||||||||||||||
| 139 | cached data and replace it with data from the | - | ||||||||||||||||||
| 140 | underlying data store. | - | ||||||||||||||||||
| 141 | - | |||||||||||||||||||
| 142 | Although models and views should respond to these hints in appropriate | - | ||||||||||||||||||
| 143 | ways, custom components may ignore any or all of them if they are not | - | ||||||||||||||||||
| 144 | relevant. | - | ||||||||||||||||||
| 145 | */ | - | ||||||||||||||||||
| 146 | - | |||||||||||||||||||
| 147 | /*! | - | ||||||||||||||||||
| 148 | \fn void QAbstractItemDelegate::commitData(QWidget *editor) | - | ||||||||||||||||||
| 149 | - | |||||||||||||||||||
| 150 | This signal must be emitted when the \a editor widget has completed | - | ||||||||||||||||||
| 151 | editing the data, and wants to write it back into the model. | - | ||||||||||||||||||
| 152 | */ | - | ||||||||||||||||||
| 153 | - | |||||||||||||||||||
| 154 | /*! | - | ||||||||||||||||||
| 155 | \fn void QAbstractItemDelegate::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) | - | ||||||||||||||||||
| 156 | - | |||||||||||||||||||
| 157 | This signal is emitted when the user has finished editing an item using | - | ||||||||||||||||||
| 158 | the specified \a editor. | - | ||||||||||||||||||
| 159 | - | |||||||||||||||||||
| 160 | The \a hint provides a way for the delegate to influence how the model and | - | ||||||||||||||||||
| 161 | view behave after editing is completed. It indicates to these components | - | ||||||||||||||||||
| 162 | what action should be performed next to provide a comfortable editing | - | ||||||||||||||||||
| 163 | experience for the user. For example, if \c EditNextItem is specified, | - | ||||||||||||||||||
| 164 | the view should use a delegate to open an editor on the next item in the | - | ||||||||||||||||||
| 165 | model. | - | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | \sa EndEditHint | - | ||||||||||||||||||
| 168 | */ | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | /*! | - | ||||||||||||||||||
| 171 | \fn void QAbstractItemDelegate::sizeHintChanged(const QModelIndex &index) | - | ||||||||||||||||||
| 172 | \since 4.4 | - | ||||||||||||||||||
| 173 | - | |||||||||||||||||||
| 174 | This signal must be emitted when the sizeHint() of \a index changed. | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | Views automatically connect to this signal and relayout items as necessary. | - | ||||||||||||||||||
| 177 | */ | - | ||||||||||||||||||
| 178 | - | |||||||||||||||||||
| 179 | - | |||||||||||||||||||
| 180 | /*! | - | ||||||||||||||||||
| 181 | Creates a new abstract item delegate with the given \a parent. | - | ||||||||||||||||||
| 182 | */ | - | ||||||||||||||||||
| 183 | QAbstractItemDelegate::QAbstractItemDelegate(QObject *parent) | - | ||||||||||||||||||
| 184 | : QObject(*new QAbstractItemDelegatePrivate, parent) | - | ||||||||||||||||||
| 185 | { | - | ||||||||||||||||||
| 186 | - | |||||||||||||||||||
| 187 | } never executed: end of block | 0 | ||||||||||||||||||
| 188 | - | |||||||||||||||||||
| 189 | /*! | - | ||||||||||||||||||
| 190 | \internal | - | ||||||||||||||||||
| 191 | - | |||||||||||||||||||
| 192 | Creates a new abstract item delegate with the given \a parent. | - | ||||||||||||||||||
| 193 | */ | - | ||||||||||||||||||
| 194 | QAbstractItemDelegate::QAbstractItemDelegate(QObjectPrivate &dd, QObject *parent) | - | ||||||||||||||||||
| 195 | : QObject(dd, parent) | - | ||||||||||||||||||
| 196 | { | - | ||||||||||||||||||
| 197 | - | |||||||||||||||||||
| 198 | } never executed: end of block | 0 | ||||||||||||||||||
| 199 | - | |||||||||||||||||||
| 200 | /*! | - | ||||||||||||||||||
| 201 | Destroys the abstract item delegate. | - | ||||||||||||||||||
| 202 | */ | - | ||||||||||||||||||
| 203 | QAbstractItemDelegate::~QAbstractItemDelegate() | - | ||||||||||||||||||
| 204 | { | - | ||||||||||||||||||
| 205 | - | |||||||||||||||||||
| 206 | } | - | ||||||||||||||||||
| 207 | - | |||||||||||||||||||
| 208 | /*! | - | ||||||||||||||||||
| 209 | \fn void QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const = 0; | - | ||||||||||||||||||
| 210 | - | |||||||||||||||||||
| 211 | This pure abstract function must be reimplemented if you want to | - | ||||||||||||||||||
| 212 | provide custom rendering. Use the \a painter and style \a option to | - | ||||||||||||||||||
| 213 | render the item specified by the item \a index. | - | ||||||||||||||||||
| 214 | - | |||||||||||||||||||
| 215 | If you reimplement this you must also reimplement sizeHint(). | - | ||||||||||||||||||
| 216 | */ | - | ||||||||||||||||||
| 217 | - | |||||||||||||||||||
| 218 | /*! | - | ||||||||||||||||||
| 219 | \fn QSize QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const = 0 | - | ||||||||||||||||||
| 220 | - | |||||||||||||||||||
| 221 | This pure abstract function must be reimplemented if you want to | - | ||||||||||||||||||
| 222 | provide custom rendering. The options are specified by \a option | - | ||||||||||||||||||
| 223 | and the model item by \a index. | - | ||||||||||||||||||
| 224 | - | |||||||||||||||||||
| 225 | If you reimplement this you must also reimplement paint(). | - | ||||||||||||||||||
| 226 | */ | - | ||||||||||||||||||
| 227 | - | |||||||||||||||||||
| 228 | /*! | - | ||||||||||||||||||
| 229 | Returns the editor to be used for editing the data item with the | - | ||||||||||||||||||
| 230 | given \a index. Note that the index contains information about the | - | ||||||||||||||||||
| 231 | model being used. The editor's parent widget is specified by \a parent, | - | ||||||||||||||||||
| 232 | and the item options by \a option. | - | ||||||||||||||||||
| 233 | - | |||||||||||||||||||
| 234 | The base implementation returns 0. If you want custom editing you | - | ||||||||||||||||||
| 235 | will need to reimplement this function. | - | ||||||||||||||||||
| 236 | - | |||||||||||||||||||
| 237 | The returned editor widget should have Qt::StrongFocus; | - | ||||||||||||||||||
| 238 | otherwise, \l{QMouseEvent}s received by the widget will propagate | - | ||||||||||||||||||
| 239 | to the view. The view's background will shine through unless the | - | ||||||||||||||||||
| 240 | editor paints its own background (e.g., with | - | ||||||||||||||||||
| 241 | \l{QWidget::}{setAutoFillBackground()}). | - | ||||||||||||||||||
| 242 | - | |||||||||||||||||||
| 243 | \sa destroyEditor(), setModelData(), setEditorData() | - | ||||||||||||||||||
| 244 | */ | - | ||||||||||||||||||
| 245 | QWidget *QAbstractItemDelegate::createEditor(QWidget *, | - | ||||||||||||||||||
| 246 | const QStyleOptionViewItem &, | - | ||||||||||||||||||
| 247 | const QModelIndex &) const | - | ||||||||||||||||||
| 248 | { | - | ||||||||||||||||||
| 249 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
| 250 | } | - | ||||||||||||||||||
| 251 | - | |||||||||||||||||||
| 252 | - | |||||||||||||||||||
| 253 | /*! | - | ||||||||||||||||||
| 254 | Called when the \a editor is no longer needed for editing the data item | - | ||||||||||||||||||
| 255 | with the given \a index and should be destroyed. The default behavior is a | - | ||||||||||||||||||
| 256 | call to deleteLater on the editor. It is possible e.g. to avoid this delete by | - | ||||||||||||||||||
| 257 | reimplementing this function. | - | ||||||||||||||||||
| 258 | - | |||||||||||||||||||
| 259 | \since 5.0 | - | ||||||||||||||||||
| 260 | \sa createEditor() | - | ||||||||||||||||||
| 261 | */ | - | ||||||||||||||||||
| 262 | void QAbstractItemDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const | - | ||||||||||||||||||
| 263 | { | - | ||||||||||||||||||
| 264 | Q_UNUSED(index); | - | ||||||||||||||||||
| 265 | editor->deleteLater(); | - | ||||||||||||||||||
| 266 | } never executed: end of block | 0 | ||||||||||||||||||
| 267 | - | |||||||||||||||||||
| 268 | /*! | - | ||||||||||||||||||
| 269 | Sets the contents of the given \a editor to the data for the item | - | ||||||||||||||||||
| 270 | at the given \a index. Note that the index contains information | - | ||||||||||||||||||
| 271 | about the model being used. | - | ||||||||||||||||||
| 272 | - | |||||||||||||||||||
| 273 | The base implementation does nothing. If you want custom editing | - | ||||||||||||||||||
| 274 | you will need to reimplement this function. | - | ||||||||||||||||||
| 275 | - | |||||||||||||||||||
| 276 | \sa setModelData() | - | ||||||||||||||||||
| 277 | */ | - | ||||||||||||||||||
| 278 | void QAbstractItemDelegate::setEditorData(QWidget *, | - | ||||||||||||||||||
| 279 | const QModelIndex &) const | - | ||||||||||||||||||
| 280 | { | - | ||||||||||||||||||
| 281 | // do nothing | - | ||||||||||||||||||
| 282 | } | - | ||||||||||||||||||
| 283 | - | |||||||||||||||||||
| 284 | /*! | - | ||||||||||||||||||
| 285 | Sets the data for the item at the given \a index in the \a model | - | ||||||||||||||||||
| 286 | to the contents of the given \a editor. | - | ||||||||||||||||||
| 287 | - | |||||||||||||||||||
| 288 | The base implementation does nothing. If you want custom editing | - | ||||||||||||||||||
| 289 | you will need to reimplement this function. | - | ||||||||||||||||||
| 290 | - | |||||||||||||||||||
| 291 | \sa setEditorData() | - | ||||||||||||||||||
| 292 | */ | - | ||||||||||||||||||
| 293 | void QAbstractItemDelegate::setModelData(QWidget *, | - | ||||||||||||||||||
| 294 | QAbstractItemModel *, | - | ||||||||||||||||||
| 295 | const QModelIndex &) const | - | ||||||||||||||||||
| 296 | { | - | ||||||||||||||||||
| 297 | // do nothing | - | ||||||||||||||||||
| 298 | } | - | ||||||||||||||||||
| 299 | - | |||||||||||||||||||
| 300 | /*! | - | ||||||||||||||||||
| 301 | Updates the geometry of the \a editor for the item with the given | - | ||||||||||||||||||
| 302 | \a index, according to the rectangle specified in the \a option. | - | ||||||||||||||||||
| 303 | If the item has an internal layout, the editor will be laid out | - | ||||||||||||||||||
| 304 | accordingly. Note that the index contains information about the | - | ||||||||||||||||||
| 305 | model being used. | - | ||||||||||||||||||
| 306 | - | |||||||||||||||||||
| 307 | The base implementation does nothing. If you want custom editing | - | ||||||||||||||||||
| 308 | you must reimplement this function. | - | ||||||||||||||||||
| 309 | */ | - | ||||||||||||||||||
| 310 | void QAbstractItemDelegate::updateEditorGeometry(QWidget *, | - | ||||||||||||||||||
| 311 | const QStyleOptionViewItem &, | - | ||||||||||||||||||
| 312 | const QModelIndex &) const | - | ||||||||||||||||||
| 313 | { | - | ||||||||||||||||||
| 314 | // do nothing | - | ||||||||||||||||||
| 315 | } | - | ||||||||||||||||||
| 316 | - | |||||||||||||||||||
| 317 | /*! | - | ||||||||||||||||||
| 318 | When editing of an item starts, this function is called with the | - | ||||||||||||||||||
| 319 | \a event that triggered the editing, the \a model, the \a index of | - | ||||||||||||||||||
| 320 | the item, and the \a option used for rendering the item. | - | ||||||||||||||||||
| 321 | - | |||||||||||||||||||
| 322 | Mouse events are sent to editorEvent() even if they don't start | - | ||||||||||||||||||
| 323 | editing of the item. This can, for instance, be useful if you wish | - | ||||||||||||||||||
| 324 | to open a context menu when the right mouse button is pressed on | - | ||||||||||||||||||
| 325 | an item. | - | ||||||||||||||||||
| 326 | - | |||||||||||||||||||
| 327 | The base implementation returns \c false (indicating that it has not | - | ||||||||||||||||||
| 328 | handled the event). | - | ||||||||||||||||||
| 329 | */ | - | ||||||||||||||||||
| 330 | bool QAbstractItemDelegate::editorEvent(QEvent *, | - | ||||||||||||||||||
| 331 | QAbstractItemModel *, | - | ||||||||||||||||||
| 332 | const QStyleOptionViewItem &, | - | ||||||||||||||||||
| 333 | const QModelIndex &) | - | ||||||||||||||||||
| 334 | { | - | ||||||||||||||||||
| 335 | // do nothing | - | ||||||||||||||||||
| 336 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 337 | } | - | ||||||||||||||||||
| 338 | - | |||||||||||||||||||
| 339 | /*! | - | ||||||||||||||||||
| 340 | \obsolete | - | ||||||||||||||||||
| 341 | - | |||||||||||||||||||
| 342 | Use QFontMetrics::elidedText() instead. | - | ||||||||||||||||||
| 343 | - | |||||||||||||||||||
| 344 | \oldcode | - | ||||||||||||||||||
| 345 | QFontMetrics fm = ... | - | ||||||||||||||||||
| 346 | QString str = QAbstractItemDelegate::elidedText(fm, width, mode, text); | - | ||||||||||||||||||
| 347 | \newcode | - | ||||||||||||||||||
| 348 | QFontMetrics fm = ... | - | ||||||||||||||||||
| 349 | QString str = fm.elidedText(text, mode, width); | - | ||||||||||||||||||
| 350 | \endcode | - | ||||||||||||||||||
| 351 | */ | - | ||||||||||||||||||
| 352 | - | |||||||||||||||||||
| 353 | QString QAbstractItemDelegate::elidedText(const QFontMetrics &fontMetrics, int width, | - | ||||||||||||||||||
| 354 | Qt::TextElideMode mode, const QString &text) | - | ||||||||||||||||||
| 355 | { | - | ||||||||||||||||||
| 356 | return fontMetrics.elidedText(text, mode, width); never executed: return fontMetrics.elidedText(text, mode, width); | 0 | ||||||||||||||||||
| 357 | } | - | ||||||||||||||||||
| 358 | - | |||||||||||||||||||
| 359 | /*! | - | ||||||||||||||||||
| 360 | \since 4.3 | - | ||||||||||||||||||
| 361 | Whenever a help event occurs, this function is called with the \a event | - | ||||||||||||||||||
| 362 | \a view \a option and the \a index that corresponds to the item where the | - | ||||||||||||||||||
| 363 | event occurs. | - | ||||||||||||||||||
| 364 | - | |||||||||||||||||||
| 365 | Returns \c true if the delegate can handle the event; otherwise returns \c false. | - | ||||||||||||||||||
| 366 | A return value of true indicates that the data obtained using the index had | - | ||||||||||||||||||
| 367 | the required role. | - | ||||||||||||||||||
| 368 | - | |||||||||||||||||||
| 369 | For QEvent::ToolTip and QEvent::WhatsThis events that were handled successfully, | - | ||||||||||||||||||
| 370 | the relevant popup may be shown depending on the user's system configuration. | - | ||||||||||||||||||
| 371 | - | |||||||||||||||||||
| 372 | \sa QHelpEvent | - | ||||||||||||||||||
| 373 | */ | - | ||||||||||||||||||
| 374 | bool QAbstractItemDelegate::helpEvent(QHelpEvent *event, | - | ||||||||||||||||||
| 375 | QAbstractItemView *view, | - | ||||||||||||||||||
| 376 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
| 377 | const QModelIndex &index) | - | ||||||||||||||||||
| 378 | { | - | ||||||||||||||||||
| 379 | Q_D(QAbstractItemDelegate); | - | ||||||||||||||||||
| 380 | Q_UNUSED(option); | - | ||||||||||||||||||
| 381 | - | |||||||||||||||||||
| 382 | if (!event || !view)
| 0 | ||||||||||||||||||
| 383 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 384 | switch (event->type()) { | - | ||||||||||||||||||
| 385 | #ifndef QT_NO_TOOLTIP | - | ||||||||||||||||||
| 386 | case QEvent::ToolTip: { never executed: case QEvent::ToolTip: | 0 | ||||||||||||||||||
| 387 | QHelpEvent *he = static_cast<QHelpEvent*>(event); | - | ||||||||||||||||||
| 388 | const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp
| 0 | ||||||||||||||||||
| 389 | const QString tooltip = d->textForRole(Qt::ToolTipRole, index.data(Qt::ToolTipRole), option.locale, precision); | - | ||||||||||||||||||
| 390 | if (!tooltip.isEmpty()) {
| 0 | ||||||||||||||||||
| 391 | QToolTip::showText(he->globalPos(), tooltip, view); | - | ||||||||||||||||||
| 392 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 393 | } | - | ||||||||||||||||||
| 394 | break;} never executed: break; | 0 | ||||||||||||||||||
| 395 | #endif | - | ||||||||||||||||||
| 396 | #ifndef QT_NO_WHATSTHIS | - | ||||||||||||||||||
| 397 | case QEvent::QueryWhatsThis: { never executed: case QEvent::QueryWhatsThis: | 0 | ||||||||||||||||||
| 398 | if (index.data(Qt::WhatsThisRole).isValid())
| 0 | ||||||||||||||||||
| 399 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 400 | break; } never executed: break; | 0 | ||||||||||||||||||
| 401 | case QEvent::WhatsThis: { never executed: case QEvent::WhatsThis: | 0 | ||||||||||||||||||
| 402 | QHelpEvent *he = static_cast<QHelpEvent*>(event); | - | ||||||||||||||||||
| 403 | const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp
| 0 | ||||||||||||||||||
| 404 | const QString whatsthis = d->textForRole(Qt::WhatsThisRole, index.data(Qt::WhatsThisRole), option.locale, precision); | - | ||||||||||||||||||
| 405 | if (!whatsthis.isEmpty()) {
| 0 | ||||||||||||||||||
| 406 | QWhatsThis::showText(he->globalPos(), whatsthis, view); | - | ||||||||||||||||||
| 407 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 408 | } | - | ||||||||||||||||||
| 409 | break ; } never executed: break ; | 0 | ||||||||||||||||||
| 410 | #endif | - | ||||||||||||||||||
| 411 | default: never executed: default: | 0 | ||||||||||||||||||
| 412 | break; never executed: break; | 0 | ||||||||||||||||||
| 413 | } | - | ||||||||||||||||||
| 414 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 415 | } | - | ||||||||||||||||||
| 416 | - | |||||||||||||||||||
| 417 | /*! | - | ||||||||||||||||||
| 418 | \internal | - | ||||||||||||||||||
| 419 | - | |||||||||||||||||||
| 420 | This virtual method is reserved and will be used in Qt 5.1. | - | ||||||||||||||||||
| 421 | */ | - | ||||||||||||||||||
| 422 | QVector<int> QAbstractItemDelegate::paintingRoles() const | - | ||||||||||||||||||
| 423 | { | - | ||||||||||||||||||
| 424 | return QVector<int>(); never executed: return QVector<int>(); | 0 | ||||||||||||||||||
| 425 | } | - | ||||||||||||||||||
| 426 | - | |||||||||||||||||||
| 427 | QAbstractItemDelegatePrivate::QAbstractItemDelegatePrivate() | - | ||||||||||||||||||
| 428 | : QObjectPrivate() | - | ||||||||||||||||||
| 429 | { | - | ||||||||||||||||||
| 430 | } never executed: end of block | 0 | ||||||||||||||||||
| 431 | - | |||||||||||||||||||
| 432 | static bool editorHandlesKeyEvent(QWidget *editor, const QKeyEvent *event) | - | ||||||||||||||||||
| 433 | { | - | ||||||||||||||||||
| 434 | #ifndef QT_NO_TEXTEDIT | - | ||||||||||||||||||
| 435 | // do not filter enter / return / tab / backtab for QTextEdit or QPlainTextEdit | - | ||||||||||||||||||
| 436 | if (qobject_cast<QTextEdit *>(editor) || qobject_cast<QPlainTextEdit *>(editor)) {
| 0 | ||||||||||||||||||
| 437 | switch (event->key()) { | - | ||||||||||||||||||
| 438 | case Qt::Key_Tab: never executed: case Qt::Key_Tab: | 0 | ||||||||||||||||||
| 439 | case Qt::Key_Backtab: never executed: case Qt::Key_Backtab: | 0 | ||||||||||||||||||
| 440 | case Qt::Key_Enter: never executed: case Qt::Key_Enter: | 0 | ||||||||||||||||||
| 441 | case Qt::Key_Return: never executed: case Qt::Key_Return: | 0 | ||||||||||||||||||
| 442 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 443 | - | |||||||||||||||||||
| 444 | default: never executed: default: | 0 | ||||||||||||||||||
| 445 | break; never executed: break; | 0 | ||||||||||||||||||
| 446 | } | - | ||||||||||||||||||
| 447 | } | - | ||||||||||||||||||
| 448 | #endif // QT_NO_TEXTEDIT | - | ||||||||||||||||||
| 449 | - | |||||||||||||||||||
| 450 | Q_UNUSED(editor); | - | ||||||||||||||||||
| 451 | Q_UNUSED(event); | - | ||||||||||||||||||
| 452 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 453 | } | - | ||||||||||||||||||
| 454 | - | |||||||||||||||||||
| 455 | bool QAbstractItemDelegatePrivate::editorEventFilter(QObject *object, QEvent *event) | - | ||||||||||||||||||
| 456 | { | - | ||||||||||||||||||
| 457 | Q_Q(QAbstractItemDelegate); | - | ||||||||||||||||||
| 458 | - | |||||||||||||||||||
| 459 | QWidget *editor = qobject_cast<QWidget*>(object); | - | ||||||||||||||||||
| 460 | if (!editor)
| 0 | ||||||||||||||||||
| 461 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 462 | if (event->type() == QEvent::KeyPress) {
| 0 | ||||||||||||||||||
| 463 | QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); | - | ||||||||||||||||||
| 464 | if (editorHandlesKeyEvent(editor, keyEvent))
| 0 | ||||||||||||||||||
| 465 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 466 | - | |||||||||||||||||||
| 467 | if (keyEvent->matches(QKeySequence::Cancel)) {
| 0 | ||||||||||||||||||
| 468 | // don't commit data | - | ||||||||||||||||||
| 469 | emit q->closeEditor(editor, QAbstractItemDelegate::RevertModelCache); | - | ||||||||||||||||||
| 470 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 471 | } | - | ||||||||||||||||||
| 472 | - | |||||||||||||||||||
| 473 | switch (keyEvent->key()) { | - | ||||||||||||||||||
| 474 | case Qt::Key_Tab: never executed: case Qt::Key_Tab: | 0 | ||||||||||||||||||
| 475 | if (tryFixup(editor)) {
| 0 | ||||||||||||||||||
| 476 | emit q->commitData(editor); | - | ||||||||||||||||||
| 477 | emit q->closeEditor(editor, QAbstractItemDelegate::EditNextItem); | - | ||||||||||||||||||
| 478 | } never executed: end of block | 0 | ||||||||||||||||||
| 479 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 480 | case Qt::Key_Backtab: never executed: case Qt::Key_Backtab: | 0 | ||||||||||||||||||
| 481 | if (tryFixup(editor)) {
| 0 | ||||||||||||||||||
| 482 | emit q->commitData(editor); | - | ||||||||||||||||||
| 483 | emit q->closeEditor(editor, QAbstractItemDelegate::EditPreviousItem); | - | ||||||||||||||||||
| 484 | } never executed: end of block | 0 | ||||||||||||||||||
| 485 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 486 | case Qt::Key_Enter: never executed: case Qt::Key_Enter: | 0 | ||||||||||||||||||
| 487 | case Qt::Key_Return: never executed: case Qt::Key_Return: | 0 | ||||||||||||||||||
| 488 | // We want the editor to be able to process the key press | - | ||||||||||||||||||
| 489 | // before committing the data (e.g. so it can do | - | ||||||||||||||||||
| 490 | // validation/fixup of the input). | - | ||||||||||||||||||
| 491 | if (!tryFixup(editor))
| 0 | ||||||||||||||||||
| 492 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 493 | - | |||||||||||||||||||
| 494 | QMetaObject::invokeMethod(q, "_q_commitDataAndCloseEditor", | - | ||||||||||||||||||
| 495 | Qt::QueuedConnection, Q_ARG(QWidget*, editor)); | - | ||||||||||||||||||
| 496 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 497 | default: never executed: default: | 0 | ||||||||||||||||||
| 498 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 499 | } | - | ||||||||||||||||||
| 500 | } else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
| 0 | ||||||||||||||||||
| 501 | //the Hide event will take care of he editors that are in fact complete dialogs | - | ||||||||||||||||||
| 502 | if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {
| 0 | ||||||||||||||||||
| 503 | QWidget *w = QApplication::focusWidget(); | - | ||||||||||||||||||
| 504 | while (w) { // don't worry about focus changes internally in the editor
| 0 | ||||||||||||||||||
| 505 | if (w == editor)
| 0 | ||||||||||||||||||
| 506 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 507 | w = w->parentWidget(); | - | ||||||||||||||||||
| 508 | } never executed: end of block | 0 | ||||||||||||||||||
| 509 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||||||||
| 510 | // The window may lose focus during an drag operation. | - | ||||||||||||||||||
| 511 | // i.e when dragging involves the taskbar on Windows. | - | ||||||||||||||||||
| 512 | QPlatformDrag *platformDrag = QGuiApplicationPrivate::instance()->platformIntegration()->drag(); | - | ||||||||||||||||||
| 513 | if (platformDrag && platformDrag->currentDrag()) {
| 0 | ||||||||||||||||||
| 514 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 515 | } | - | ||||||||||||||||||
| 516 | #endif | - | ||||||||||||||||||
| 517 | if (tryFixup(editor))
| 0 | ||||||||||||||||||
| 518 | emit q->commitData(editor); never executed: q->commitData(editor); | 0 | ||||||||||||||||||
| 519 | - | |||||||||||||||||||
| 520 | emit q->closeEditor(editor, QAbstractItemDelegate::NoHint); | - | ||||||||||||||||||
| 521 | } never executed: end of block | 0 | ||||||||||||||||||
| 522 | } else if (event->type() == QEvent::ShortcutOverride) { never executed: end of block
| 0 | ||||||||||||||||||
| 523 | if (static_cast<QKeyEvent*>(event)->matches(QKeySequence::Cancel)) {
| 0 | ||||||||||||||||||
| 524 | event->accept(); | - | ||||||||||||||||||
| 525 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 526 | } | - | ||||||||||||||||||
| 527 | } never executed: end of block | 0 | ||||||||||||||||||
| 528 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 529 | } | - | ||||||||||||||||||
| 530 | - | |||||||||||||||||||
| 531 | bool QAbstractItemDelegatePrivate::tryFixup(QWidget *editor) | - | ||||||||||||||||||
| 532 | { | - | ||||||||||||||||||
| 533 | #ifndef QT_NO_LINEEDIT | - | ||||||||||||||||||
| 534 | if (QLineEdit *e = qobject_cast<QLineEdit*>(editor)) {
| 0 | ||||||||||||||||||
| 535 | if (!e->hasAcceptableInput()) {
| 0 | ||||||||||||||||||
| 536 | if (const QValidator *validator = e->validator()) {
| 0 | ||||||||||||||||||
| 537 | QString text = e->text(); | - | ||||||||||||||||||
| 538 | validator->fixup(text); | - | ||||||||||||||||||
| 539 | e->setText(text); | - | ||||||||||||||||||
| 540 | } never executed: end of block | 0 | ||||||||||||||||||
| 541 | return e->hasAcceptableInput(); never executed: return e->hasAcceptableInput(); | 0 | ||||||||||||||||||
| 542 | } | - | ||||||||||||||||||
| 543 | } never executed: end of block | 0 | ||||||||||||||||||
| 544 | #endif // QT_NO_LINEEDIT | - | ||||||||||||||||||
| 545 | - | |||||||||||||||||||
| 546 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 547 | } | - | ||||||||||||||||||
| 548 | - | |||||||||||||||||||
| 549 | QString QAbstractItemDelegatePrivate::textForRole(Qt::ItemDataRole role, const QVariant &value, const QLocale &locale, int precision) const | - | ||||||||||||||||||
| 550 | { | - | ||||||||||||||||||
| 551 | const QLocale::FormatType formatType = (role == Qt::DisplayRole) ? QLocale::ShortFormat : QLocale::LongFormat;
| 0 | ||||||||||||||||||
| 552 | QString text; | - | ||||||||||||||||||
| 553 | switch (value.userType()) { | - | ||||||||||||||||||
| 554 | case QMetaType::Float: never executed: case QMetaType::Float: | 0 | ||||||||||||||||||
| 555 | text = locale.toString(value.toFloat()); | - | ||||||||||||||||||
| 556 | break; never executed: break; | 0 | ||||||||||||||||||
| 557 | case QVariant::Double: never executed: case QVariant::Double: | 0 | ||||||||||||||||||
| 558 | text = locale.toString(value.toDouble(), 'g', precision); | - | ||||||||||||||||||
| 559 | break; never executed: break; | 0 | ||||||||||||||||||
| 560 | case QVariant::Int: never executed: case QVariant::Int: | 0 | ||||||||||||||||||
| 561 | case QVariant::LongLong: never executed: case QVariant::LongLong: | 0 | ||||||||||||||||||
| 562 | text = locale.toString(value.toLongLong()); | - | ||||||||||||||||||
| 563 | break; never executed: break; | 0 | ||||||||||||||||||
| 564 | case QVariant::UInt: never executed: case QVariant::UInt: | 0 | ||||||||||||||||||
| 565 | case QVariant::ULongLong: never executed: case QVariant::ULongLong: | 0 | ||||||||||||||||||
| 566 | text = locale.toString(value.toULongLong()); | - | ||||||||||||||||||
| 567 | break; never executed: break; | 0 | ||||||||||||||||||
| 568 | case QVariant::Date: never executed: case QVariant::Date: | 0 | ||||||||||||||||||
| 569 | text = locale.toString(value.toDate(), formatType); | - | ||||||||||||||||||
| 570 | break; never executed: break; | 0 | ||||||||||||||||||
| 571 | case QVariant::Time: never executed: case QVariant::Time: | 0 | ||||||||||||||||||
| 572 | text = locale.toString(value.toTime(), formatType); | - | ||||||||||||||||||
| 573 | break; never executed: break; | 0 | ||||||||||||||||||
| 574 | case QVariant::DateTime: { never executed: case QVariant::DateTime: | 0 | ||||||||||||||||||
| 575 | const QDateTime dateTime = value.toDateTime(); | - | ||||||||||||||||||
| 576 | text = locale.toString(dateTime.date(), formatType) | - | ||||||||||||||||||
| 577 | + QLatin1Char(' ') | - | ||||||||||||||||||
| 578 | + locale.toString(dateTime.time(), formatType); | - | ||||||||||||||||||
| 579 | break; } never executed: break; | 0 | ||||||||||||||||||
| 580 | default: never executed: default: | 0 | ||||||||||||||||||
| 581 | text = value.toString(); | - | ||||||||||||||||||
| 582 | if (role == Qt::DisplayRole)
| 0 | ||||||||||||||||||
| 583 | text.replace(QLatin1Char('\n'), QChar::LineSeparator); never executed: text.replace(QLatin1Char('\n'), QChar::LineSeparator); | 0 | ||||||||||||||||||
| 584 | break; never executed: break; | 0 | ||||||||||||||||||
| 585 | } | - | ||||||||||||||||||
| 586 | return text; never executed: return text; | 0 | ||||||||||||||||||
| 587 | } | - | ||||||||||||||||||
| 588 | - | |||||||||||||||||||
| 589 | void QAbstractItemDelegatePrivate::_q_commitDataAndCloseEditor(QWidget *editor) | - | ||||||||||||||||||
| 590 | { | - | ||||||||||||||||||
| 591 | Q_Q(QAbstractItemDelegate); | - | ||||||||||||||||||
| 592 | emit q->commitData(editor); | - | ||||||||||||||||||
| 593 | emit q->closeEditor(editor, QAbstractItemDelegate::SubmitModelCache); | - | ||||||||||||||||||
| 594 | } never executed: end of block | 0 | ||||||||||||||||||
| 595 | - | |||||||||||||||||||
| 596 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 597 | - | |||||||||||||||||||
| 598 | #include "moc_qabstractitemdelegate.cpp" | - | ||||||||||||||||||
| 599 | - | |||||||||||||||||||
| 600 | #endif // QT_NO_ITEMVIEWS | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |