diff options
Diffstat (limited to 'src/widgets/itemviews/qitemeditorfactory.cpp')
-rw-r--r-- | src/widgets/itemviews/qitemeditorfactory.cpp | 125 |
1 files changed, 43 insertions, 82 deletions
diff --git a/src/widgets/itemviews/qitemeditorfactory.cpp b/src/widgets/itemviews/qitemeditorfactory.cpp index 1465ee71a7..70d11e1b38 100644 --- a/src/widgets/itemviews/qitemeditorfactory.cpp +++ b/src/widgets/itemviews/qitemeditorfactory.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 <qplatformdefs.h> #include "qitemeditorfactory.h" @@ -91,7 +55,7 @@ class QUIntSpinBox : public QSpinBox Q_OBJECT Q_PROPERTY(uint value READ uintValue WRITE setUIntValue NOTIFY uintValueChanged USER true) public: - explicit QUIntSpinBox(QWidget *parent = 0) + explicit QUIntSpinBox(QWidget *parent = nullptr) : QSpinBox(parent) { connect(this, SIGNAL(valueChanged(int)), SIGNAL(uintValueChanged())); @@ -122,7 +86,7 @@ Q_SIGNALS: \inmodule QtWidgets When editing data in an item view, editors are created and - displayed by a delegate. QItemDelegate, which is the delegate by + displayed by a delegate. QStyledItemDelegate, which is the delegate by default installed on Qt's item views, uses a QItemEditorFactory to create editors for it. A default unique instance provided by QItemEditorFactory is used by all item delegates. If you set a @@ -156,7 +120,7 @@ Q_SIGNALS: Additional editors can be registered with the registerEditor() function. - \sa QItemDelegate, {Model/View Programming}, {Color Editor Factory Example} + \sa QStyledItemDelegate, {Model/View Programming} */ /*! @@ -176,7 +140,7 @@ QWidget *QItemEditorFactory::createEditor(int userType, QWidget *parent) const QItemEditorCreatorBase *creator = creatorMap.value(userType, 0); if (!creator) { const QItemEditorFactory *dfactory = defaultFactory(); - return dfactory == this ? 0 : dfactory->createEditor(userType, parent); + return dfactory == this ? nullptr : dfactory->createEditor(userType, parent); } return creator->createWidget(parent); } @@ -241,21 +205,21 @@ QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent) { switch (userType) { #if QT_CONFIG(combobox) - case QVariant::Bool: { + case QMetaType::Bool: { QBooleanComboBox *cb = new QBooleanComboBox(parent); cb->setFrame(false); cb->setSizePolicy(QSizePolicy::Ignored, cb->sizePolicy().verticalPolicy()); return cb; } #endif #if QT_CONFIG(spinbox) - case QVariant::UInt: { + case QMetaType::UInt: { QSpinBox *sb = new QUIntSpinBox(parent); sb->setFrame(false); sb->setMinimum(0); sb->setMaximum(INT_MAX); sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy()); return sb; } - case QVariant::Int: { + case QMetaType::Int: { QSpinBox *sb = new QSpinBox(parent); sb->setFrame(false); sb->setMinimum(INT_MIN); @@ -264,25 +228,25 @@ QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent) return sb; } #endif #if QT_CONFIG(datetimeedit) - case QVariant::Date: { + case QMetaType::QDate: { QDateTimeEdit *ed = new QDateEdit(parent); ed->setFrame(false); return ed; } - case QVariant::Time: { + case QMetaType::QTime: { QDateTimeEdit *ed = new QTimeEdit(parent); ed->setFrame(false); return ed; } - case QVariant::DateTime: { + case QMetaType::QDateTime: { QDateTimeEdit *ed = new QDateTimeEdit(parent); ed->setFrame(false); return ed; } #endif #if QT_CONFIG(label) - case QVariant::Pixmap: + case QMetaType::QPixmap: return new QLabel(parent); #endif #if QT_CONFIG(spinbox) - case QVariant::Double: { + case QMetaType::Double: { QDoubleSpinBox *sb = new QDoubleSpinBox(parent); sb->setFrame(false); sb->setMinimum(-DBL_MAX); @@ -291,12 +255,12 @@ QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent) return sb; } #endif #if QT_CONFIG(lineedit) - case QVariant::String: + case QMetaType::QString: default: { // the default editor is a lineedit QExpandingLineEdit *le = new QExpandingLineEdit(parent); - le->setFrame(le->style()->styleHint(QStyle::SH_ItemView_DrawDelegateFrame, 0, le)); - if (!le->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, le)) + le->setFrame(le->style()->styleHint(QStyle::SH_ItemView_DrawDelegateFrame, nullptr, le)); + if (!le->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, nullptr, le)) le->setWidgetOwnsGeometry(true); return le; } #else @@ -304,42 +268,42 @@ QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent) break; #endif } - return 0; + return nullptr; } QByteArray QDefaultItemEditorFactory::valuePropertyName(int userType) const { switch (userType) { #if QT_CONFIG(combobox) - case QVariant::Bool: + case QMetaType::Bool: return "currentIndex"; #endif #if QT_CONFIG(spinbox) - case QVariant::UInt: - case QVariant::Int: - case QVariant::Double: + case QMetaType::UInt: + case QMetaType::Int: + case QMetaType::Double: return "value"; #endif #if QT_CONFIG(datetimeedit) - case QVariant::Date: + case QMetaType::QDate: return "date"; - case QVariant::Time: + case QMetaType::QTime: return "time"; - case QVariant::DateTime: + case QMetaType::QDateTime: return "dateTime"; #endif - case QVariant::String: + case QMetaType::QString: default: // the default editor is a lineedit return "text"; } } -static QItemEditorFactory *q_default_factory = 0; +static QItemEditorFactory *q_default_factory = nullptr; struct QDefaultFactoryCleaner { inline QDefaultFactoryCleaner() {} - ~QDefaultFactoryCleaner() { delete q_default_factory; q_default_factory = 0; } + ~QDefaultFactoryCleaner() { delete q_default_factory; q_default_factory = nullptr; } }; /*! @@ -379,7 +343,7 @@ void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory) QItemEditorCreatorBase objects are specialized widget factories that provide editor widgets for one particular QVariant data type. They are used by QItemEditorFactory to create editors for - \l{QItemDelegate}s. Creator bases must be registered with + \l{QStyledItemDelegate}s. Creator bases must be registered with QItemEditorFactory::registerEditor(). An editor should provide a user property for the data it edits. @@ -399,7 +363,7 @@ void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory) to register widgets without the need to subclass QItemEditorCreatorBase. \sa QStandardItemEditorCreator, QItemEditorFactory, - {Model/View Programming}, {Color Editor Factory Example} + {Model/View Programming} */ /*! @@ -432,7 +396,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() the creator is registered for. For example, a creator which constructs QCheckBox widgets to edit boolean values would return the \l{QCheckBox::checkable}{checkable} property name from this function, - and must be registered in the item editor factory for the QVariant::Bool + and must be registered in the item editor factory for the QMetaType::Bool type. Note: Since Qt 4.2 the item delegates query the user property of widgets, @@ -461,14 +425,14 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 1 The constructor takes the name of the property that contains the - editing data. QItemDelegate can then access the property by name + editing data. QStyledItemDelegate can then access the property by name when it sets and retrieves editing data. Only use this class if your editor does not define a user property (using the USER keyword in the Q_PROPERTY macro). If the widget has a user property, you should use QStandardItemEditorCreator instead. \sa QItemEditorCreatorBase, QStandardItemEditorCreator, - QItemEditorFactory, {Color Editor Factory Example} + QItemEditorFactory */ /*! @@ -476,7 +440,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() Constructs an editor creator object using \a valuePropertyName as the name of the property to be used for editing. The - property name is used by QItemDelegate when setting and + property name is used by QStyledItemDelegate when setting and getting editor data. Note that the \a valuePropertyName is only used if the editor @@ -512,11 +476,11 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 2 Setting the \c editorFactory created above in an item delegate via - QItemDelegate::setItemEditorFactory() makes sure that all values of type - QVariant::DateTime will be edited in \c{MyFancyDateTimeEdit}. + QStyledItemDelegate::setItemEditorFactory() makes sure that all values of type + QMetaType::QDateTime will be edited in \c{MyFancyDateTimeEdit}. The editor must provide a user property that will contain the - editing data. The property is used by \l{QItemDelegate}s to set + editing data. The property is used by \l{QStyledItemDelegate}s to set and retrieve the data (using Qt's \l{Meta-Object System}{meta-object system}). You set the user property with the USER keyword: @@ -524,7 +488,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 3 \sa QItemEditorCreatorBase, QItemEditorCreator, - QItemEditorFactory, QItemDelegate, {Color Editor Factory Example} + QItemEditorFactory, QStyledItemDelegate */ /*! @@ -570,17 +534,14 @@ void QExpandingLineEdit::changeEvent(QEvent *e) void QExpandingLineEdit::updateMinimumWidth() { - int left, right; - getTextMargins(&left, 0, &right, 0); - int width = left + right + 4 /*horizontalMargin in qlineedit.cpp*/; - getContentsMargins(&left, 0, &right, 0); - width += left + right; + const QMargins tm = textMargins(); + const QMargins cm = contentsMargins(); + const int width = tm.left() + tm.right() + cm.left() + cm.right() + 4 /*horizontalMargin in qlineedit.cpp*/; QStyleOptionFrame opt; initStyleOption(&opt); - int minWidth = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(width, 0). - expandedTo(QApplication::globalStrut()), this).width(); + int minWidth = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(width, 0), this).width(); setMinimumWidth(minWidth); } @@ -594,7 +555,7 @@ void QExpandingLineEdit::resizeToContents() int hintWidth = minimumWidth() + fontMetrics().horizontalAdvance(displayText()); int parentWidth = parent->width(); int maxWidth = isRightToLeft() ? position.x() + oldWidth : parentWidth - position.x(); - int newWidth = qBound(originalWidth, hintWidth, maxWidth); + int newWidth = qBound(qMin(originalWidth, maxWidth), hintWidth, maxWidth); if (widgetOwnsGeometry) setMaximumWidth(newWidth); if (isRightToLeft()) |