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