diff options
Diffstat (limited to 'src/widgets/itemviews/qitemdelegate.cpp')
-rw-r--r-- | src/widgets/itemviews/qitemdelegate.cpp | 100 |
1 files changed, 32 insertions, 68 deletions
diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp index 9c56c82578..d1c7bb3d58 100644 --- a/src/widgets/itemviews/qitemdelegate.cpp +++ b/src/widgets/itemviews/qitemdelegate.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qitemdelegate.h" @@ -58,6 +22,7 @@ #include <qmetaobject.h> #include <qtextlayout.h> #include <private/qabstractitemdelegate_p.h> +#include <private/qabstractitemmodel_p.h> #include <private/qtextengine_p.h> #include <qdebug.h> #include <qlocale.h> @@ -94,7 +59,7 @@ public: inline static QString replaceNewLine(QString text) { - text.replace(QLatin1Char('\n'), QChar::LineSeparator); + text.replace(u'\n', QChar::LineSeparator); return text; } @@ -292,17 +257,27 @@ QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const When subclassing QItemDelegate to create a delegate that displays items using a custom renderer, it is important to ensure that the delegate can - render items suitably for all the required states; e.g. selected, + render items suitably for all the required states; such as selected, disabled, checked. The documentation for the paint() function contains some hints to show how this can be achieved. - You can provide custom editors by using a QItemEditorFactory. The - \l{Color Editor Factory Example} shows how a custom editor can be - made available to delegates with the default item editor - factory. This way, there is no need to subclass QItemDelegate. An - alternative is to reimplement createEditor(), setEditorData(), - setModelData(), and updateEditorGeometry(). This process is - described in the \l{Spin Box Delegate Example}. + You can provide custom editors by using a QItemEditorFactory. The following + code shows how a custom editor can be made available to delegates with the + default item editor factory. + + \snippet code/src_gui_itemviews_qitemeditorfactory.cpp setDefaultFactory + + After the default factory has been set, all standard item delegates + will use it (also the delegates that were created before setting the + default factory). + + This way, you can avoid subclassing QItemDelegate, and all values of the + specified type (for example QMetaType::QDateTime) will be edited using the + provided editor (like \c{MyFancyDateTimeEdit} in the above example). + + An alternative is to reimplement createEditor(), setEditorData(), + setModelData(), and updateEditorGeometry(). This process is described + in the \l{A simple delegate}{Model/View Programming overview documentation}. \section1 QStyledItemDelegate vs. QItemDelegate @@ -316,9 +291,7 @@ QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const for either class should be equal unless the custom delegate needs to use the style for drawing. - \sa {Delegate Classes}, QStyledItemDelegate, QAbstractItemDelegate, - {Spin Box Delegate Example}, {Settings Editor Example}, - {Icons Example} + \sa {Delegate Classes}, QStyledItemDelegate, QAbstractItemDelegate */ /*! @@ -378,8 +351,10 @@ QString QItemDelegatePrivate::valueToText(const QVariant &value, const QStyleOpt For example, a selected item may need to be displayed differently to unselected items, as shown in the following code: - \snippet itemviews/pixelator/pixeldelegate.cpp 2 - \dots + \code + if (option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); + \endcode After painting, you should ensure that the painter is returned to its the state it was supplied in when this function was called. For example, @@ -432,7 +407,7 @@ void QItemDelegate::paint(QPainter *painter, Qt::CheckState checkState = Qt::Unchecked; value = index.data(Qt::CheckStateRole); if (value.isValid()) { - checkState = static_cast<Qt::CheckState>(value.toInt()); + checkState = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(value); checkRect = doCheck(opt, opt.rect, value); } @@ -522,10 +497,6 @@ QWidget *QItemDelegate::createEditor(QWidget *parent, void QItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { -#ifdef QT_NO_PROPERTIES - Q_UNUSED(editor); - Q_UNUSED(index); -#else QVariant v = index.data(Qt::EditRole); QByteArray n = editor->metaObject()->userProperty().name(); @@ -534,7 +505,6 @@ void QItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) con v = QVariant(editor->property(n).metaType()); editor->setProperty(n, v); } -#endif } /*! @@ -552,11 +522,6 @@ void QItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { -#ifdef QT_NO_PROPERTIES - Q_UNUSED(model); - Q_UNUSED(editor); - Q_UNUSED(index); -#else Q_D(const QItemDelegate); Q_ASSERT(model); Q_ASSERT(editor); @@ -566,7 +531,6 @@ void QItemDelegate::setModelData(QWidget *editor, model->data(index, Qt::EditRole).userType()); if (!n.isEmpty()) model->setData(index, editor->property(n), Qt::EditRole); -#endif } /*! @@ -1051,10 +1015,10 @@ QRect QItemDelegate::rect(const QStyleOptionViewItem &option, break; case QMetaType::QPixmap: { const QPixmap &pixmap = qvariant_cast<QPixmap>(value); - return QRect(QPoint(0, 0), pixmap.size() / pixmap.devicePixelRatio() ); } + return QRect(QPoint(0, 0), pixmap.deviceIndependentSize().toSize()); } case QMetaType::QImage: { const QImage &image = qvariant_cast<QImage>(value); - return QRect(QPoint(0, 0), image.size() / image.devicePixelRatio() ); } + return QRect(QPoint(0, 0), image.deviceIndependentSize().toSize()); } case QMetaType::QIcon: { QIcon::Mode mode = d->iconMode(option.state); QIcon::State state = d->iconState(option.state); @@ -1193,7 +1157,7 @@ bool QItemDelegate::editorEvent(QEvent *event, return false; } - Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); + Qt::CheckState state = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(value); if (flags & Qt::ItemIsUserTristate) state = ((Qt::CheckState)((state + 1) % 3)); else @@ -1220,7 +1184,7 @@ QStyleOptionViewItem QItemDelegate::setOptions(const QModelIndex &index, // set text alignment value = index.data(Qt::TextAlignmentRole); if (value.isValid()) - opt.displayAlignment = Qt::Alignment(value.toInt()); + opt.displayAlignment = QtPrivate::legacyFlagValueFromModelData<Qt::Alignment>(value); // set foreground brush value = index.data(Qt::ForegroundRole); |