| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qstyleditemdelegate.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qstyleditemdelegate.h" | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #ifndef QT_NO_ITEMVIEWS | - | ||||||||||||
| 37 | #include <qabstractitemmodel.h> | - | ||||||||||||
| 38 | #include <qapplication.h> | - | ||||||||||||
| 39 | #include <qbrush.h> | - | ||||||||||||
| 40 | #include <qlineedit.h> | - | ||||||||||||
| 41 | #include <qtextedit.h> | - | ||||||||||||
| 42 | #include <qplaintextedit.h> | - | ||||||||||||
| 43 | #include <qpainter.h> | - | ||||||||||||
| 44 | #include <qpalette.h> | - | ||||||||||||
| 45 | #include <qpoint.h> | - | ||||||||||||
| 46 | #include <qrect.h> | - | ||||||||||||
| 47 | #include <qsize.h> | - | ||||||||||||
| 48 | #include <qstyle.h> | - | ||||||||||||
| 49 | #include <qdatetime.h> | - | ||||||||||||
| 50 | #include <qstyleoption.h> | - | ||||||||||||
| 51 | #include <qevent.h> | - | ||||||||||||
| 52 | #include <qpixmap.h> | - | ||||||||||||
| 53 | #include <qbitmap.h> | - | ||||||||||||
| 54 | #include <qpixmapcache.h> | - | ||||||||||||
| 55 | #include <qitemeditorfactory.h> | - | ||||||||||||
| 56 | #include <private/qitemeditorfactory_p.h> | - | ||||||||||||
| 57 | #include <qmetaobject.h> | - | ||||||||||||
| 58 | #include <qtextlayout.h> | - | ||||||||||||
| 59 | #include <private/qabstractitemdelegate_p.h> | - | ||||||||||||
| 60 | #include <private/qtextengine_p.h> | - | ||||||||||||
| 61 | #include <private/qlayoutengine_p.h> | - | ||||||||||||
| 62 | #include <qdebug.h> | - | ||||||||||||
| 63 | #include <qlocale.h> | - | ||||||||||||
| 64 | #include <qdialog.h> | - | ||||||||||||
| 65 | #include <qtableview.h> | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | #include <limits.h> | - | ||||||||||||
| 68 | - | |||||||||||||
| 69 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 70 | - | |||||||||||||
| 71 | class QStyledItemDelegatePrivate : public QAbstractItemDelegatePrivate | - | ||||||||||||
| 72 | { | - | ||||||||||||
| 73 | Q_DECLARE_PUBLIC(QStyledItemDelegate) | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | public: | - | ||||||||||||
| 76 | QStyledItemDelegatePrivate() : factory(0) { } never executed: end of block | 0 | ||||||||||||
| 77 | - | |||||||||||||
| 78 | static const QWidget *widget(const QStyleOptionViewItem &option) | - | ||||||||||||
| 79 | { | - | ||||||||||||
| 80 | return option.widget; never executed: return option.widget; | 0 | ||||||||||||
| 81 | } | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | const QItemEditorFactory *editorFactory() const | - | ||||||||||||
| 84 | { | - | ||||||||||||
| 85 | return factory ? factory : QItemEditorFactory::defaultFactory(); never executed: return factory ? factory : QItemEditorFactory::defaultFactory();
| 0 | ||||||||||||
| 86 | } | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | QItemEditorFactory *factory; | - | ||||||||||||
| 89 | }; | - | ||||||||||||
| 90 | - | |||||||||||||
| 91 | /*! | - | ||||||||||||
| 92 | \class QStyledItemDelegate | - | ||||||||||||
| 93 | - | |||||||||||||
| 94 | \brief The QStyledItemDelegate class provides display and editing facilities for | - | ||||||||||||
| 95 | data items from a model. | - | ||||||||||||
| 96 | - | |||||||||||||
| 97 | \ingroup model-view | - | ||||||||||||
| 98 | \inmodule QtWidgets | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | \since 4.4 | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | When displaying data from models in Qt item views, e.g., a | - | ||||||||||||
| 103 | QTableView, the individual items are drawn by a delegate. Also, | - | ||||||||||||
| 104 | when an item is edited, it provides an editor widget, which is | - | ||||||||||||
| 105 | placed on top of the item view while editing takes place. | - | ||||||||||||
| 106 | QStyledItemDelegate is the default delegate for all Qt item | - | ||||||||||||
| 107 | views, and is installed upon them when they are created. | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | The QStyledItemDelegate class is one of the \l{Model/View Classes} | - | ||||||||||||
| 110 | and is part of Qt's \l{Model/View Programming}{model/view | - | ||||||||||||
| 111 | framework}. The delegate allows the display and editing of items | - | ||||||||||||
| 112 | to be developed independently from the model and view. | - | ||||||||||||
| 113 | - | |||||||||||||
| 114 | The data of items in models are assigned an | - | ||||||||||||
| 115 | \l{Qt::}{ItemDataRole}; each item can store a QVariant for each | - | ||||||||||||
| 116 | role. QStyledItemDelegate implements display and editing for the | - | ||||||||||||
| 117 | most common datatypes expected by users, including booleans, | - | ||||||||||||
| 118 | integers, and strings. | - | ||||||||||||
| 119 | - | |||||||||||||
| 120 | The data will be drawn differently depending on which role they | - | ||||||||||||
| 121 | have in the model. The following table describes the roles and the | - | ||||||||||||
| 122 | data types the delegate can handle for each of them. It is often | - | ||||||||||||
| 123 | sufficient to ensure that the model returns appropriate data for | - | ||||||||||||
| 124 | each of the roles to determine the appearance of items in views. | - | ||||||||||||
| 125 | - | |||||||||||||
| 126 | \table | - | ||||||||||||
| 127 | \header \li Role \li Accepted Types | - | ||||||||||||
| 128 | \omit | - | ||||||||||||
| 129 | \row \li \l Qt::AccessibleDescriptionRole \li QString | - | ||||||||||||
| 130 | \row \li \l Qt::AccessibleTextRole \li QString | - | ||||||||||||
| 131 | \endomit | - | ||||||||||||
| 132 | \row \li \l Qt::BackgroundRole \li QBrush | - | ||||||||||||
| 133 | \row \li \l Qt::BackgroundColorRole \li QColor (obsolete; use Qt::BackgroundRole instead) | - | ||||||||||||
| 134 | \row \li \l Qt::CheckStateRole \li Qt::CheckState | - | ||||||||||||
| 135 | \row \li \l Qt::DecorationRole \li QIcon, QPixmap, QImage and QColor | - | ||||||||||||
| 136 | \row \li \l Qt::DisplayRole \li QString and types with a string representation | - | ||||||||||||
| 137 | \row \li \l Qt::EditRole \li See QItemEditorFactory for details | - | ||||||||||||
| 138 | \row \li \l Qt::FontRole \li QFont | - | ||||||||||||
| 139 | \row \li \l Qt::SizeHintRole \li QSize | - | ||||||||||||
| 140 | \omit | - | ||||||||||||
| 141 | \row \li \l Qt::StatusTipRole \li | - | ||||||||||||
| 142 | \endomit | - | ||||||||||||
| 143 | \row \li \l Qt::TextAlignmentRole \li Qt::Alignment | - | ||||||||||||
| 144 | \row \li \l Qt::ForegroundRole \li QBrush | - | ||||||||||||
| 145 | \row \li \l Qt::TextColorRole \li QColor (obsolete; use Qt::ForegroundRole instead) | - | ||||||||||||
| 146 | \omit | - | ||||||||||||
| 147 | \row \li \l Qt::ToolTipRole | - | ||||||||||||
| 148 | \row \li \l Qt::WhatsThisRole | - | ||||||||||||
| 149 | \endomit | - | ||||||||||||
| 150 | \endtable | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | Editors are created with a QItemEditorFactory; a default static | - | ||||||||||||
| 153 | instance provided by QItemEditorFactory is installed on all item | - | ||||||||||||
| 154 | delegates. You can set a custom factory using | - | ||||||||||||
| 155 | setItemEditorFactory() or set a new default factory with | - | ||||||||||||
| 156 | QItemEditorFactory::setDefaultFactory(). It is the data stored in | - | ||||||||||||
| 157 | the item model with the \l{Qt::}{EditRole} that is edited. See the | - | ||||||||||||
| 158 | QItemEditorFactory class for a more high-level introduction to | - | ||||||||||||
| 159 | item editor factories. The \l{Color Editor Factory Example}{Color | - | ||||||||||||
| 160 | Editor Factory} example shows how to create custom editors with a | - | ||||||||||||
| 161 | factory. | - | ||||||||||||
| 162 | - | |||||||||||||
| 163 | \section1 Subclassing QStyledItemDelegate | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | If the delegate does not support painting of the data types you | - | ||||||||||||
| 166 | need or you want to customize the drawing of items, you need to | - | ||||||||||||
| 167 | subclass QStyledItemDelegate, and reimplement paint() and possibly | - | ||||||||||||
| 168 | sizeHint(). The paint() function is called individually for each | - | ||||||||||||
| 169 | item, and with sizeHint(), you can specify the hint for each | - | ||||||||||||
| 170 | of them. | - | ||||||||||||
| 171 | - | |||||||||||||
| 172 | When reimplementing paint(), one would typically handle the | - | ||||||||||||
| 173 | datatypes one would like to draw and use the superclass | - | ||||||||||||
| 174 | implementation for other types. | - | ||||||||||||
| 175 | - | |||||||||||||
| 176 | The painting of check box indicators are performed by the current | - | ||||||||||||
| 177 | style. The style also specifies the size and the bounding | - | ||||||||||||
| 178 | rectangles in which to draw the data for the different data roles. | - | ||||||||||||
| 179 | The bounding rectangle of the item itself is also calculated by | - | ||||||||||||
| 180 | the style. When drawing already supported datatypes, it is | - | ||||||||||||
| 181 | therefore a good idea to ask the style for these bounding | - | ||||||||||||
| 182 | rectangles. The QStyle class description describes this in | - | ||||||||||||
| 183 | more detail. | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | If you wish to change any of the bounding rectangles calculated by | - | ||||||||||||
| 186 | the style or the painting of check box indicators, you can | - | ||||||||||||
| 187 | subclass QStyle. Note, however, that the size of the items can | - | ||||||||||||
| 188 | also be affected by reimplementing sizeHint(). | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | It is possible for a custom delegate to provide editors | - | ||||||||||||
| 191 | without the use of an editor item factory. In this case, the | - | ||||||||||||
| 192 | following virtual functions must be reimplemented: | - | ||||||||||||
| 193 | - | |||||||||||||
| 194 | \list | - | ||||||||||||
| 195 | \li createEditor() returns the widget used to change data from the model | - | ||||||||||||
| 196 | and can be reimplemented to customize editing behavior. | - | ||||||||||||
| 197 | \li setEditorData() provides the widget with data to manipulate. | - | ||||||||||||
| 198 | \li updateEditorGeometry() ensures that the editor is displayed correctly | - | ||||||||||||
| 199 | with respect to the item view. | - | ||||||||||||
| 200 | \li setModelData() returns updated data to the model. | - | ||||||||||||
| 201 | \endlist | - | ||||||||||||
| 202 | - | |||||||||||||
| 203 | The \l{Star Delegate Example}{Star Delegate} example creates | - | ||||||||||||
| 204 | editors by reimplementing these methods. | - | ||||||||||||
| 205 | - | |||||||||||||
| 206 | \section1 QStyledItemDelegate vs. QItemDelegate | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | Since Qt 4.4, there are two delegate classes: QItemDelegate and | - | ||||||||||||
| 209 | QStyledItemDelegate. However, the default delegate is QStyledItemDelegate. | - | ||||||||||||
| 210 | These two classes are independent alternatives to painting and providing | - | ||||||||||||
| 211 | editors for items in views. The difference between them is that | - | ||||||||||||
| 212 | QStyledItemDelegate uses the current style to paint its items. We therefore | - | ||||||||||||
| 213 | recommend using QStyledItemDelegate as the base class when implementing | - | ||||||||||||
| 214 | custom delegates or when working with Qt style sheets. The code required | - | ||||||||||||
| 215 | for either class should be equal unless the custom delegate needs to use | - | ||||||||||||
| 216 | the style for drawing. | - | ||||||||||||
| 217 | - | |||||||||||||
| 218 | If you wish to customize the painting of item views, you should | - | ||||||||||||
| 219 | implement a custom style. Please see the QStyle class | - | ||||||||||||
| 220 | documentation for details. | - | ||||||||||||
| 221 | - | |||||||||||||
| 222 | \sa {Delegate Classes}, QItemDelegate, QAbstractItemDelegate, QStyle, | - | ||||||||||||
| 223 | {Spin Box Delegate Example}, {Star Delegate Example}, {Color | - | ||||||||||||
| 224 | Editor Factory Example} | - | ||||||||||||
| 225 | */ | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | - | |||||||||||||
| 228 | /*! | - | ||||||||||||
| 229 | Constructs an item delegate with the given \a parent. | - | ||||||||||||
| 230 | */ | - | ||||||||||||
| 231 | QStyledItemDelegate::QStyledItemDelegate(QObject *parent) | - | ||||||||||||
| 232 | : QAbstractItemDelegate(*new QStyledItemDelegatePrivate(), parent) | - | ||||||||||||
| 233 | { | - | ||||||||||||
| 234 | } never executed: end of block | 0 | ||||||||||||
| 235 | - | |||||||||||||
| 236 | /*! | - | ||||||||||||
| 237 | Destroys the item delegate. | - | ||||||||||||
| 238 | */ | - | ||||||||||||
| 239 | QStyledItemDelegate::~QStyledItemDelegate() | - | ||||||||||||
| 240 | { | - | ||||||||||||
| 241 | } | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | /*! | - | ||||||||||||
| 244 | This function returns the string that the delegate will use to display the | - | ||||||||||||
| 245 | Qt::DisplayRole of the model in \a locale. \a value is the value of the Qt::DisplayRole | - | ||||||||||||
| 246 | provided by the model. | - | ||||||||||||
| 247 | - | |||||||||||||
| 248 | The default implementation uses the QLocale::toString to convert \a value into | - | ||||||||||||
| 249 | a QString. | - | ||||||||||||
| 250 | - | |||||||||||||
| 251 | This function is not called for empty model indices, i.e., indices for which | - | ||||||||||||
| 252 | the model returns an invalid QVariant. | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | \sa QAbstractItemModel::data() | - | ||||||||||||
| 255 | */ | - | ||||||||||||
| 256 | QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale& locale) const | - | ||||||||||||
| 257 | { | - | ||||||||||||
| 258 | return d_func()->textForRole(Qt::DisplayRole, value, locale); never executed: return d_func()->textForRole(Qt::DisplayRole, value, locale); | 0 | ||||||||||||
| 259 | } | - | ||||||||||||
| 260 | - | |||||||||||||
| 261 | /*! | - | ||||||||||||
| 262 | Initialize \a option with the values using the index \a index. This method | - | ||||||||||||
| 263 | is useful for subclasses when they need a QStyleOptionViewItem, but don't want | - | ||||||||||||
| 264 | to fill in all the information themselves. | - | ||||||||||||
| 265 | - | |||||||||||||
| 266 | \sa QStyleOption::initFrom() | - | ||||||||||||
| 267 | */ | - | ||||||||||||
| 268 | void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option, | - | ||||||||||||
| 269 | const QModelIndex &index) const | - | ||||||||||||
| 270 | { | - | ||||||||||||
| 271 | QVariant value = index.data(Qt::FontRole); | - | ||||||||||||
| 272 | if (value.isValid() && !value.isNull()) {
| 0 | ||||||||||||
| 273 | option->font = qvariant_cast<QFont>(value).resolve(option->font); | - | ||||||||||||
| 274 | option->fontMetrics = QFontMetrics(option->font); | - | ||||||||||||
| 275 | } never executed: end of block | 0 | ||||||||||||
| 276 | - | |||||||||||||
| 277 | value = index.data(Qt::TextAlignmentRole); | - | ||||||||||||
| 278 | if (value.isValid() && !value.isNull())
| 0 | ||||||||||||
| 279 | option->displayAlignment = Qt::Alignment(value.toInt()); never executed: option->displayAlignment = Qt::Alignment(value.toInt()); | 0 | ||||||||||||
| 280 | - | |||||||||||||
| 281 | value = index.data(Qt::ForegroundRole); | - | ||||||||||||
| 282 | if (value.canConvert<QBrush>())
| 0 | ||||||||||||
| 283 | option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); never executed: option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); | 0 | ||||||||||||
| 284 | - | |||||||||||||
| 285 | option->index = index; | - | ||||||||||||
| 286 | value = index.data(Qt::CheckStateRole); | - | ||||||||||||
| 287 | if (value.isValid() && !value.isNull()) {
| 0 | ||||||||||||
| 288 | option->features |= QStyleOptionViewItem::HasCheckIndicator; | - | ||||||||||||
| 289 | option->checkState = static_cast<Qt::CheckState>(value.toInt()); | - | ||||||||||||
| 290 | } never executed: end of block | 0 | ||||||||||||
| 291 | - | |||||||||||||
| 292 | value = index.data(Qt::DecorationRole); | - | ||||||||||||
| 293 | if (value.isValid() && !value.isNull()) {
| 0 | ||||||||||||
| 294 | option->features |= QStyleOptionViewItem::HasDecoration; | - | ||||||||||||
| 295 | switch (value.type()) { | - | ||||||||||||
| 296 | case QVariant::Icon: { never executed: case QVariant::Icon: | 0 | ||||||||||||
| 297 | option->icon = qvariant_cast<QIcon>(value); | - | ||||||||||||
| 298 | QIcon::Mode mode; | - | ||||||||||||
| 299 | if (!(option->state & QStyle::State_Enabled))
| 0 | ||||||||||||
| 300 | mode = QIcon::Disabled; never executed: mode = QIcon::Disabled; | 0 | ||||||||||||
| 301 | else if (option->state & QStyle::State_Selected)
| 0 | ||||||||||||
| 302 | mode = QIcon::Selected; never executed: mode = QIcon::Selected; | 0 | ||||||||||||
| 303 | else | - | ||||||||||||
| 304 | mode = QIcon::Normal; never executed: mode = QIcon::Normal; | 0 | ||||||||||||
| 305 | QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
| 0 | ||||||||||||
| 306 | QSize actualSize = option->icon.actualSize(option->decorationSize, mode, state); | - | ||||||||||||
| 307 | // For highdpi icons actualSize might be larger than decorationSize, which we don't want. Clamp it to decorationSize. | - | ||||||||||||
| 308 | option->decorationSize = QSize(qMin(option->decorationSize.width(), actualSize.width()), | - | ||||||||||||
| 309 | qMin(option->decorationSize.height(), actualSize.height())); | - | ||||||||||||
| 310 | break; never executed: break; | 0 | ||||||||||||
| 311 | } | - | ||||||||||||
| 312 | case QVariant::Color: { never executed: case QVariant::Color: | 0 | ||||||||||||
| 313 | QPixmap pixmap(option->decorationSize); | - | ||||||||||||
| 314 | pixmap.fill(qvariant_cast<QColor>(value)); | - | ||||||||||||
| 315 | option->icon = QIcon(pixmap); | - | ||||||||||||
| 316 | break; never executed: break; | 0 | ||||||||||||
| 317 | } | - | ||||||||||||
| 318 | case QVariant::Image: { never executed: case QVariant::Image: | 0 | ||||||||||||
| 319 | QImage image = qvariant_cast<QImage>(value); | - | ||||||||||||
| 320 | option->icon = QIcon(QPixmap::fromImage(image)); | - | ||||||||||||
| 321 | option->decorationSize = image.size() / image.devicePixelRatio(); | - | ||||||||||||
| 322 | break; never executed: break; | 0 | ||||||||||||
| 323 | } | - | ||||||||||||
| 324 | case QVariant::Pixmap: { never executed: case QVariant::Pixmap: | 0 | ||||||||||||
| 325 | QPixmap pixmap = qvariant_cast<QPixmap>(value); | - | ||||||||||||
| 326 | option->icon = QIcon(pixmap); | - | ||||||||||||
| 327 | option->decorationSize = pixmap.size() / pixmap.devicePixelRatio(); | - | ||||||||||||
| 328 | break; never executed: break; | 0 | ||||||||||||
| 329 | } | - | ||||||||||||
| 330 | default: never executed: default: | 0 | ||||||||||||
| 331 | break; never executed: break; | 0 | ||||||||||||
| 332 | } | - | ||||||||||||
| 333 | } | - | ||||||||||||
| 334 | - | |||||||||||||
| 335 | value = index.data(Qt::DisplayRole); | - | ||||||||||||
| 336 | if (value.isValid() && !value.isNull()) {
| 0 | ||||||||||||
| 337 | option->features |= QStyleOptionViewItem::HasDisplay; | - | ||||||||||||
| 338 | option->text = displayText(value, option->locale); | - | ||||||||||||
| 339 | } never executed: end of block | 0 | ||||||||||||
| 340 | - | |||||||||||||
| 341 | option->backgroundBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole)); | - | ||||||||||||
| 342 | - | |||||||||||||
| 343 | // disable style animations for checkboxes etc. within itemviews (QTBUG-30146) | - | ||||||||||||
| 344 | option->styleObject = 0; | - | ||||||||||||
| 345 | } never executed: end of block | 0 | ||||||||||||
| 346 | - | |||||||||||||
| 347 | /*! | - | ||||||||||||
| 348 | Renders the delegate using the given \a painter and style \a option for | - | ||||||||||||
| 349 | the item specified by \a index. | - | ||||||||||||
| 350 | - | |||||||||||||
| 351 | This function paints the item using the view's QStyle. | - | ||||||||||||
| 352 | - | |||||||||||||
| 353 | When reimplementing paint in a subclass. Use the initStyleOption() | - | ||||||||||||
| 354 | to set up the \a option in the same way as the | - | ||||||||||||
| 355 | QStyledItemDelegate. | - | ||||||||||||
| 356 | - | |||||||||||||
| 357 | Whenever possible, use the \a option while painting. | - | ||||||||||||
| 358 | Especially its \l{QStyleOption::}{rect} variable to decide | - | ||||||||||||
| 359 | where to draw and its \l{QStyleOption::}{state} to determine | - | ||||||||||||
| 360 | if it is enabled or selected. | - | ||||||||||||
| 361 | - | |||||||||||||
| 362 | After painting, you should ensure that the painter is returned to | - | ||||||||||||
| 363 | the state it was supplied in when this function was called. | - | ||||||||||||
| 364 | For example, it may be useful to call QPainter::save() before | - | ||||||||||||
| 365 | painting and QPainter::restore() afterwards. | - | ||||||||||||
| 366 | - | |||||||||||||
| 367 | \sa QItemDelegate::paint(), QStyle::drawControl(), QStyle::CE_ItemViewItem | - | ||||||||||||
| 368 | */ | - | ||||||||||||
| 369 | void QStyledItemDelegate::paint(QPainter *painter, | - | ||||||||||||
| 370 | const QStyleOptionViewItem &option, const QModelIndex &index) const | - | ||||||||||||
| 371 | { | - | ||||||||||||
| 372 | Q_ASSERT(index.isValid()); | - | ||||||||||||
| 373 | - | |||||||||||||
| 374 | QStyleOptionViewItem opt = option; | - | ||||||||||||
| 375 | initStyleOption(&opt, index); | - | ||||||||||||
| 376 | - | |||||||||||||
| 377 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 378 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||
| 379 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); | - | ||||||||||||
| 380 | } never executed: end of block | 0 | ||||||||||||
| 381 | - | |||||||||||||
| 382 | /*! | - | ||||||||||||
| 383 | Returns the size needed by the delegate to display the item | - | ||||||||||||
| 384 | specified by \a index, taking into account the style information | - | ||||||||||||
| 385 | provided by \a option. | - | ||||||||||||
| 386 | - | |||||||||||||
| 387 | This function uses the view's QStyle to determine the size of the | - | ||||||||||||
| 388 | item. | - | ||||||||||||
| 389 | - | |||||||||||||
| 390 | \sa QStyle::sizeFromContents(), QStyle::CT_ItemViewItem | - | ||||||||||||
| 391 | */ | - | ||||||||||||
| 392 | QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, | - | ||||||||||||
| 393 | const QModelIndex &index) const | - | ||||||||||||
| 394 | { | - | ||||||||||||
| 395 | QVariant value = index.data(Qt::SizeHintRole); | - | ||||||||||||
| 396 | if (value.isValid())
| 0 | ||||||||||||
| 397 | return qvariant_cast<QSize>(value); never executed: return qvariant_cast<QSize>(value); | 0 | ||||||||||||
| 398 | - | |||||||||||||
| 399 | QStyleOptionViewItem opt = option; | - | ||||||||||||
| 400 | initStyleOption(&opt, index); | - | ||||||||||||
| 401 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 402 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||
| 403 | return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget); never executed: return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget); | 0 | ||||||||||||
| 404 | } | - | ||||||||||||
| 405 | - | |||||||||||||
| 406 | /*! | - | ||||||||||||
| 407 | Returns the widget used to edit the item specified by \a index | - | ||||||||||||
| 408 | for editing. The \a parent widget and style \a option are used to | - | ||||||||||||
| 409 | control how the editor widget appears. | - | ||||||||||||
| 410 | - | |||||||||||||
| 411 | \sa QAbstractItemDelegate::createEditor() | - | ||||||||||||
| 412 | */ | - | ||||||||||||
| 413 | QWidget *QStyledItemDelegate::createEditor(QWidget *parent, | - | ||||||||||||
| 414 | const QStyleOptionViewItem &, | - | ||||||||||||
| 415 | const QModelIndex &index) const | - | ||||||||||||
| 416 | { | - | ||||||||||||
| 417 | Q_D(const QStyledItemDelegate); | - | ||||||||||||
| 418 | if (!index.isValid())
| 0 | ||||||||||||
| 419 | return 0; never executed: return 0; | 0 | ||||||||||||
| 420 | return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent); never executed: return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent); | 0 | ||||||||||||
| 421 | } | - | ||||||||||||
| 422 | - | |||||||||||||
| 423 | /*! | - | ||||||||||||
| 424 | Sets the data to be displayed and edited by the \a editor from the | - | ||||||||||||
| 425 | data model item specified by the model \a index. | - | ||||||||||||
| 426 | - | |||||||||||||
| 427 | The default implementation stores the data in the \a editor | - | ||||||||||||
| 428 | widget's \l {Qt's Property System} {user property}. | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | \sa QMetaProperty::isUser() | - | ||||||||||||
| 431 | */ | - | ||||||||||||
| 432 | void QStyledItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const | - | ||||||||||||
| 433 | { | - | ||||||||||||
| 434 | #ifdef QT_NO_PROPERTIES | - | ||||||||||||
| 435 | Q_UNUSED(editor); | - | ||||||||||||
| 436 | Q_UNUSED(index); | - | ||||||||||||
| 437 | #else | - | ||||||||||||
| 438 | QVariant v = index.data(Qt::EditRole); | - | ||||||||||||
| 439 | QByteArray n = editor->metaObject()->userProperty().name(); | - | ||||||||||||
| 440 | - | |||||||||||||
| 441 | if (!n.isEmpty()) {
| 0 | ||||||||||||
| 442 | if (!v.isValid())
| 0 | ||||||||||||
| 443 | v = QVariant(editor->property(n).userType(), (const void *)0); never executed: v = QVariant(editor->property(n).userType(), (const void *)0); | 0 | ||||||||||||
| 444 | editor->setProperty(n, v); | - | ||||||||||||
| 445 | } never executed: end of block | 0 | ||||||||||||
| 446 | #endif | - | ||||||||||||
| 447 | } never executed: end of block | 0 | ||||||||||||
| 448 | - | |||||||||||||
| 449 | /*! | - | ||||||||||||
| 450 | Gets data from the \a editor widget and stores it in the specified | - | ||||||||||||
| 451 | \a model at the item \a index. | - | ||||||||||||
| 452 | - | |||||||||||||
| 453 | The default implementation gets the value to be stored in the data | - | ||||||||||||
| 454 | model from the \a editor widget's \l {Qt's Property System} {user | - | ||||||||||||
| 455 | property}. | - | ||||||||||||
| 456 | - | |||||||||||||
| 457 | \sa QMetaProperty::isUser() | - | ||||||||||||
| 458 | */ | - | ||||||||||||
| 459 | void QStyledItemDelegate::setModelData(QWidget *editor, | - | ||||||||||||
| 460 | QAbstractItemModel *model, | - | ||||||||||||
| 461 | const QModelIndex &index) const | - | ||||||||||||
| 462 | { | - | ||||||||||||
| 463 | #ifdef QT_NO_PROPERTIES | - | ||||||||||||
| 464 | Q_UNUSED(model); | - | ||||||||||||
| 465 | Q_UNUSED(editor); | - | ||||||||||||
| 466 | Q_UNUSED(index); | - | ||||||||||||
| 467 | #else | - | ||||||||||||
| 468 | Q_D(const QStyledItemDelegate); | - | ||||||||||||
| 469 | Q_ASSERT(model); | - | ||||||||||||
| 470 | Q_ASSERT(editor); | - | ||||||||||||
| 471 | QByteArray n = editor->metaObject()->userProperty().name(); | - | ||||||||||||
| 472 | if (n.isEmpty())
| 0 | ||||||||||||
| 473 | n = d->editorFactory()->valuePropertyName( never executed: n = d->editorFactory()->valuePropertyName( model->data(index, Qt::EditRole).userType()); | 0 | ||||||||||||
| 474 | model->data(index, Qt::EditRole).userType()); never executed: n = d->editorFactory()->valuePropertyName( model->data(index, Qt::EditRole).userType()); | 0 | ||||||||||||
| 475 | if (!n.isEmpty())
| 0 | ||||||||||||
| 476 | model->setData(index, editor->property(n), Qt::EditRole); never executed: model->setData(index, editor->property(n), Qt::EditRole); | 0 | ||||||||||||
| 477 | #endif | - | ||||||||||||
| 478 | } never executed: end of block | 0 | ||||||||||||
| 479 | - | |||||||||||||
| 480 | /*! | - | ||||||||||||
| 481 | Updates the \a editor for the item specified by \a index | - | ||||||||||||
| 482 | according to the style \a option given. | - | ||||||||||||
| 483 | */ | - | ||||||||||||
| 484 | void QStyledItemDelegate::updateEditorGeometry(QWidget *editor, | - | ||||||||||||
| 485 | const QStyleOptionViewItem &option, | - | ||||||||||||
| 486 | const QModelIndex &index) const | - | ||||||||||||
| 487 | { | - | ||||||||||||
| 488 | if (!editor)
| 0 | ||||||||||||
| 489 | return; never executed: return; | 0 | ||||||||||||
| 490 | Q_ASSERT(index.isValid()); | - | ||||||||||||
| 491 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 492 | - | |||||||||||||
| 493 | QStyleOptionViewItem opt = option; | - | ||||||||||||
| 494 | initStyleOption(&opt, index); | - | ||||||||||||
| 495 | // let the editor take up all available space | - | ||||||||||||
| 496 | //if the editor is not a QLineEdit | - | ||||||||||||
| 497 | //or it is in a QTableView | - | ||||||||||||
| 498 | #if !defined(QT_NO_TABLEVIEW) && !defined(QT_NO_LINEEDIT) | - | ||||||||||||
| 499 | if (qobject_cast<QExpandingLineEdit*>(editor) && !qobject_cast<const QTableView*>(widget))
| 0 | ||||||||||||
| 500 | opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, editor); never executed: opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, editor); | 0 | ||||||||||||
| 501 | else | - | ||||||||||||
| 502 | #endif | - | ||||||||||||
| 503 | opt.showDecorationSelected = true; never executed: opt.showDecorationSelected = true; | 0 | ||||||||||||
| 504 | - | |||||||||||||
| 505 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||
| 506 | QRect geom = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, widget); | - | ||||||||||||
| 507 | if ( editor->layoutDirection() == Qt::RightToLeft) {
| 0 | ||||||||||||
| 508 | const int delta = qSmartMinSize(editor).width() - geom.width(); | - | ||||||||||||
| 509 | if (delta > 0) {
| 0 | ||||||||||||
| 510 | //we need to widen the geometry | - | ||||||||||||
| 511 | geom.adjust(-delta, 0, 0, 0); | - | ||||||||||||
| 512 | } never executed: end of block | 0 | ||||||||||||
| 513 | } never executed: end of block | 0 | ||||||||||||
| 514 | - | |||||||||||||
| 515 | editor->setGeometry(geom); | - | ||||||||||||
| 516 | } never executed: end of block | 0 | ||||||||||||
| 517 | - | |||||||||||||
| 518 | /*! | - | ||||||||||||
| 519 | Returns the editor factory used by the item delegate. | - | ||||||||||||
| 520 | If no editor factory is set, the function will return null. | - | ||||||||||||
| 521 | - | |||||||||||||
| 522 | \sa setItemEditorFactory() | - | ||||||||||||
| 523 | */ | - | ||||||||||||
| 524 | QItemEditorFactory *QStyledItemDelegate::itemEditorFactory() const | - | ||||||||||||
| 525 | { | - | ||||||||||||
| 526 | Q_D(const QStyledItemDelegate); | - | ||||||||||||
| 527 | return d->factory; never executed: return d->factory; | 0 | ||||||||||||
| 528 | } | - | ||||||||||||
| 529 | - | |||||||||||||
| 530 | /*! | - | ||||||||||||
| 531 | Sets the editor factory to be used by the item delegate to be the \a factory | - | ||||||||||||
| 532 | specified. If no editor factory is set, the item delegate will use the | - | ||||||||||||
| 533 | default editor factory. | - | ||||||||||||
| 534 | - | |||||||||||||
| 535 | \sa itemEditorFactory() | - | ||||||||||||
| 536 | */ | - | ||||||||||||
| 537 | void QStyledItemDelegate::setItemEditorFactory(QItemEditorFactory *factory) | - | ||||||||||||
| 538 | { | - | ||||||||||||
| 539 | Q_D(QStyledItemDelegate); | - | ||||||||||||
| 540 | d->factory = factory; | - | ||||||||||||
| 541 | } never executed: end of block | 0 | ||||||||||||
| 542 | - | |||||||||||||
| 543 | - | |||||||||||||
| 544 | /*! | - | ||||||||||||
| 545 | \fn bool QStyledItemDelegate::eventFilter(QObject *editor, QEvent *event) | - | ||||||||||||
| 546 | - | |||||||||||||
| 547 | Returns \c true if the given \a editor is a valid QWidget and the | - | ||||||||||||
| 548 | given \a event is handled; otherwise returns \c false. The following | - | ||||||||||||
| 549 | key press events are handled by default: | - | ||||||||||||
| 550 | - | |||||||||||||
| 551 | \list | - | ||||||||||||
| 552 | \li \uicontrol Tab | - | ||||||||||||
| 553 | \li \uicontrol Backtab | - | ||||||||||||
| 554 | \li \uicontrol Enter | - | ||||||||||||
| 555 | \li \uicontrol Return | - | ||||||||||||
| 556 | \li \uicontrol Esc | - | ||||||||||||
| 557 | \endlist | - | ||||||||||||
| 558 | - | |||||||||||||
| 559 | If the \a editor's type is QTextEdit or QPlainTextEdit then \uicontrol Enter and | - | ||||||||||||
| 560 | \uicontrol Return keys are \e not handled. | - | ||||||||||||
| 561 | - | |||||||||||||
| 562 | In the case of \uicontrol Tab, \uicontrol Backtab, \uicontrol Enter and \uicontrol Return | - | ||||||||||||
| 563 | key press events, the \a editor's data is comitted to the model | - | ||||||||||||
| 564 | and the editor is closed. If the \a event is a \uicontrol Tab key press | - | ||||||||||||
| 565 | the view will open an editor on the next item in the | - | ||||||||||||
| 566 | view. Likewise, if the \a event is a \uicontrol Backtab key press the | - | ||||||||||||
| 567 | view will open an editor on the \e previous item in the view. | - | ||||||||||||
| 568 | - | |||||||||||||
| 569 | If the event is a \uicontrol Esc key press event, the \a editor is | - | ||||||||||||
| 570 | closed \e without committing its data. | - | ||||||||||||
| 571 | - | |||||||||||||
| 572 | \sa commitData(), closeEditor() | - | ||||||||||||
| 573 | */ | - | ||||||||||||
| 574 | bool QStyledItemDelegate::eventFilter(QObject *object, QEvent *event) | - | ||||||||||||
| 575 | { | - | ||||||||||||
| 576 | Q_D(QStyledItemDelegate); | - | ||||||||||||
| 577 | return d->editorEventFilter(object, event); never executed: return d->editorEventFilter(object, event); | 0 | ||||||||||||
| 578 | } | - | ||||||||||||
| 579 | - | |||||||||||||
| 580 | /*! | - | ||||||||||||
| 581 | \reimp | - | ||||||||||||
| 582 | */ | - | ||||||||||||
| 583 | bool QStyledItemDelegate::editorEvent(QEvent *event, | - | ||||||||||||
| 584 | QAbstractItemModel *model, | - | ||||||||||||
| 585 | const QStyleOptionViewItem &option, | - | ||||||||||||
| 586 | const QModelIndex &index) | - | ||||||||||||
| 587 | { | - | ||||||||||||
| 588 | Q_ASSERT(event); | - | ||||||||||||
| 589 | Q_ASSERT(model); | - | ||||||||||||
| 590 | - | |||||||||||||
| 591 | // make sure that the item is checkable | - | ||||||||||||
| 592 | Qt::ItemFlags flags = model->flags(index); | - | ||||||||||||
| 593 | if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
| 0 | ||||||||||||
| 594 | || !(flags & Qt::ItemIsEnabled))
| 0 | ||||||||||||
| 595 | return false; never executed: return false; | 0 | ||||||||||||
| 596 | - | |||||||||||||
| 597 | // make sure that we have a check state | - | ||||||||||||
| 598 | QVariant value = index.data(Qt::CheckStateRole); | - | ||||||||||||
| 599 | if (!value.isValid())
| 0 | ||||||||||||
| 600 | return false; never executed: return false; | 0 | ||||||||||||
| 601 | - | |||||||||||||
| 602 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 603 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||
| 604 | - | |||||||||||||
| 605 | // make sure that we have the right event type | - | ||||||||||||
| 606 | if ((event->type() == QEvent::MouseButtonRelease)
| 0 | ||||||||||||
| 607 | || (event->type() == QEvent::MouseButtonDblClick)
| 0 | ||||||||||||
| 608 | || (event->type() == QEvent::MouseButtonPress)) {
| 0 | ||||||||||||
| 609 | QStyleOptionViewItem viewOpt(option); | - | ||||||||||||
| 610 | initStyleOption(&viewOpt, index); | - | ||||||||||||
| 611 | QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, widget); | - | ||||||||||||
| 612 | QMouseEvent *me = static_cast<QMouseEvent*>(event); | - | ||||||||||||
| 613 | if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
| 0 | ||||||||||||
| 614 | return false; never executed: return false; | 0 | ||||||||||||
| 615 | - | |||||||||||||
| 616 | if ((event->type() == QEvent::MouseButtonPress)
| 0 | ||||||||||||
| 617 | || (event->type() == QEvent::MouseButtonDblClick))
| 0 | ||||||||||||
| 618 | return true; never executed: return true; | 0 | ||||||||||||
| 619 | - | |||||||||||||
| 620 | } else if (event->type() == QEvent::KeyPress) { never executed: end of block
| 0 | ||||||||||||
| 621 | if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
| 0 | ||||||||||||
| 622 | && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
| 0 | ||||||||||||
| 623 | return false; never executed: return false; | 0 | ||||||||||||
| 624 | } else { never executed: end of block | 0 | ||||||||||||
| 625 | return false; never executed: return false; | 0 | ||||||||||||
| 626 | } | - | ||||||||||||
| 627 | - | |||||||||||||
| 628 | Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); | - | ||||||||||||
| 629 | if (flags & Qt::ItemIsUserTristate)
| 0 | ||||||||||||
| 630 | state = ((Qt::CheckState)((state + 1) % 3)); never executed: state = ((Qt::CheckState)((state + 1) % 3)); | 0 | ||||||||||||
| 631 | else | - | ||||||||||||
| 632 | state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked; never executed: state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
| 0 | ||||||||||||
| 633 | return model->setData(index, state, Qt::CheckStateRole); never executed: return model->setData(index, state, Qt::CheckStateRole); | 0 | ||||||||||||
| 634 | } | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | QT_END_NAMESPACE | - | ||||||||||||
| 637 | - | |||||||||||||
| 638 | #include "moc_qstyleditemdelegate.cpp" | - | ||||||||||||
| 639 | - | |||||||||||||
| 640 | #endif // QT_NO_ITEMVIEWS | - | ||||||||||||
| Source code | Switch to Preprocessed file |