summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/itemviews')
-rw-r--r--src/widgets/itemviews/itemviews.pri105
-rw-r--r--src/widgets/itemviews/qabstractitemdelegate.cpp124
-rw-r--r--src/widgets/itemviews/qabstractitemdelegate.h48
-rw-r--r--src/widgets/itemviews/qabstractitemdelegate_p.h43
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp1253
-rw-r--r--src/widgets/itemviews/qabstractitemview.h81
-rw-r--r--src/widgets/itemviews/qabstractitemview_p.h146
-rw-r--r--src/widgets/itemviews/qbsptree.cpp52
-rw-r--r--src/widgets/itemviews/qbsptree_p.h56
-rw-r--r--src/widgets/itemviews/qcolumnview.cpp170
-rw-r--r--src/widgets/itemviews/qcolumnview.h43
-rw-r--r--src/widgets/itemviews/qcolumnview_p.h66
-rw-r--r--src/widgets/itemviews/qcolumnviewgrip.cpp44
-rw-r--r--src/widgets/itemviews/qcolumnviewgrip_p.h40
-rw-r--r--src/widgets/itemviews/qdatawidgetmapper.cpp128
-rw-r--r--src/widgets/itemviews/qdatawidgetmapper.h44
-rw-r--r--src/widgets/itemviews/qdirmodel.cpp1378
-rw-r--r--src/widgets/itemviews/qdirmodel.h152
-rw-r--r--src/widgets/itemviews/qfileiconprovider.cpp162
-rw-r--r--src/widgets/itemviews/qfileiconprovider.h62
-rw-r--r--src/widgets/itemviews/qfileiconprovider_p.h45
-rw-r--r--src/widgets/itemviews/qheaderview.cpp659
-rw-r--r--src/widgets/itemviews/qheaderview.h87
-rw-r--r--src/widgets/itemviews/qheaderview_p.h102
-rw-r--r--src/widgets/itemviews/qitemdelegate.cpp168
-rw-r--r--src/widgets/itemviews/qitemdelegate.h44
-rw-r--r--src/widgets/itemviews/qitemeditorfactory.cpp105
-rw-r--r--src/widgets/itemviews/qitemeditorfactory.h40
-rw-r--r--src/widgets/itemviews/qitemeditorfactory_p.h40
-rw-r--r--src/widgets/itemviews/qlistview.cpp564
-rw-r--r--src/widgets/itemviews/qlistview.h46
-rw-r--r--src/widgets/itemviews/qlistview_p.h112
-rw-r--r--src/widgets/itemviews/qlistwidget.cpp473
-rw-r--r--src/widgets/itemviews/qlistwidget.h132
-rw-r--r--src/widgets/itemviews/qlistwidget_p.h71
-rw-r--r--src/widgets/itemviews/qstyleditemdelegate.cpp190
-rw-r--r--src/widgets/itemviews/qstyleditemdelegate.h40
-rw-r--r--src/widgets/itemviews/qtableview.cpp551
-rw-r--r--src/widgets/itemviews/qtableview.h53
-rw-r--r--src/widgets/itemviews/qtableview_p.h84
-rw-r--r--src/widgets/itemviews/qtablewidget.cpp477
-rw-r--r--src/widgets/itemviews/qtablewidget.h157
-rw-r--r--src/widgets/itemviews/qtablewidget_p.h92
-rw-r--r--src/widgets/itemviews/qtreeview.cpp636
-rw-r--r--src/widgets/itemviews/qtreeview.h54
-rw-r--r--src/widgets/itemviews/qtreeview_p.h105
-rw-r--r--src/widgets/itemviews/qtreewidget.cpp677
-rw-r--r--src/widgets/itemviews/qtreewidget.h153
-rw-r--r--src/widgets/itemviews/qtreewidget_p.h82
-rw-r--r--src/widgets/itemviews/qtreewidgetitemiterator.cpp60
-rw-r--r--src/widgets/itemviews/qtreewidgetitemiterator.h40
-rw-r--r--src/widgets/itemviews/qtreewidgetitemiterator_p.h41
-rw-r--r--src/widgets/itemviews/qwidgetitemdata_p.h43
53 files changed, 3656 insertions, 6764 deletions
diff --git a/src/widgets/itemviews/itemviews.pri b/src/widgets/itemviews/itemviews.pri
deleted file mode 100644
index e95b640d18..0000000000
--- a/src/widgets/itemviews/itemviews.pri
+++ /dev/null
@@ -1,105 +0,0 @@
-# Qt gui library, itemviews
-
-qtConfig(itemviews) {
-HEADERS += \
- itemviews/qabstractitemview.h \
- itemviews/qabstractitemview_p.h \
- itemviews/qheaderview.h \
- itemviews/qheaderview_p.h \
- itemviews/qbsptree_p.h \
- itemviews/qabstractitemdelegate.h \
- itemviews/qabstractitemdelegate_p.h \
- itemviews/qitemdelegate.h \
- itemviews/qwidgetitemdata_p.h \
- itemviews/qitemeditorfactory.h \
- itemviews/qitemeditorfactory_p.h \
- itemviews/qstyleditemdelegate.h
-
-SOURCES += \
- itemviews/qabstractitemview.cpp \
- itemviews/qheaderview.cpp \
- itemviews/qbsptree.cpp \
- itemviews/qabstractitemdelegate.cpp \
- itemviews/qitemdelegate.cpp \
- itemviews/qitemeditorfactory.cpp \
- itemviews/qstyleditemdelegate.cpp
-}
-
-qtConfig(columnview) {
- HEADERS += \
- itemviews/qcolumnviewgrip_p.h \
- itemviews/qcolumnview.h \
- itemviews/qcolumnview_p.h
-
- SOURCES += \
- itemviews/qcolumnview.cpp \
- itemviews/qcolumnviewgrip.cpp
-}
-
-qtConfig(datawidgetmapper) {
- HEADERS += itemviews/qdatawidgetmapper.h
- SOURCES += itemviews/qdatawidgetmapper.cpp
-}
-
-qtConfig(dirmodel) {
- HEADERS += itemviews/qdirmodel.h
- SOURCES += itemviews/qdirmodel.cpp
-}
-
-qtConfig(listview) {
- HEADERS += \
- itemviews/qlistview.h \
- itemviews/qlistview_p.h
-
- SOURCES += itemviews/qlistview.cpp
-}
-
-qtConfig(listwidget) {
- HEADERS += \
- itemviews/qlistwidget.h \
- itemviews/qlistwidget_p.h
-
- SOURCES += itemviews/qlistwidget.cpp
-}
-
-qtConfig(tableview) {
- HEADERS += \
- itemviews/qtableview.h \
- itemviews/qtableview_p.h
-
- SOURCES += itemviews/qtableview.cpp
-}
-
-qtConfig(tablewidget) {
- HEADERS += \
- itemviews/qtablewidget.h \
- itemviews/qtablewidget_p.h
-
- SOURCES += itemviews/qtablewidget.cpp
-}
-
-qtConfig(treeview) {
- HEADERS += \
- itemviews/qtreeview.h \
- itemviews/qtreeview_p.h
-
- SOURCES += itemviews/qtreeview.cpp
-}
-
-qtConfig(treewidget) {
- HEADERS += \
- itemviews/qtreewidget.h \
- itemviews/qtreewidget_p.h \
- itemviews/qtreewidgetitemiterator.h
-
- SOURCES += \
- itemviews/qtreewidget.cpp \
- itemviews/qtreewidgetitemiterator.cpp
-}
-
-HEADERS += \
- itemviews/qfileiconprovider.h \
- itemviews/qfileiconprovider_p.h \
-
-SOURCES += \
- itemviews/qfileiconprovider.cpp
diff --git a/src/widgets/itemviews/qabstractitemdelegate.cpp b/src/widgets/itemviews/qabstractitemdelegate.cpp
index eecc18e5c7..23e8ef0901 100644
--- a/src/widgets/itemviews/qabstractitemdelegate.cpp
+++ b/src/widgets/itemviews/qabstractitemdelegate.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 "qabstractitemdelegate.h"
@@ -45,7 +9,9 @@
#if QT_CONFIG(whatsthis)
#include <qwhatsthis.h>
#endif
+#if QT_CONFIG(tooltip)
#include <qtooltip.h>
+#endif
#include <qevent.h>
#include <qstring.h>
#include <qdebug.h>
@@ -115,8 +81,8 @@ QT_BEGIN_NAMESPACE
The second approach is to handle user events directly by reimplementing
editorEvent().
- \sa {model-view-programming}{Model/View Programming}, QStyledItemDelegate,
- {Pixelator Example}, QStyledItemDelegate, QStyle
+ \sa {model-view-programming}{Model/View Programming},
+ QStyledItemDelegate, QStyle
*/
/*!
@@ -255,7 +221,7 @@ QWidget *QAbstractItemDelegate::createEditor(QWidget *,
const QStyleOptionViewItem &,
const QModelIndex &) const
{
- return 0;
+ return nullptr;
}
@@ -345,28 +311,6 @@ bool QAbstractItemDelegate::editorEvent(QEvent *,
return false;
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \obsolete
-
- Use QFontMetrics::elidedText() instead.
-
- \oldcode
- QFontMetrics fm = ...
- QString str = QAbstractItemDelegate::elidedText(fm, width, mode, text);
- \newcode
- QFontMetrics fm = ...
- QString str = fm.elidedText(text, mode, width);
- \endcode
-*/
-
-QString QAbstractItemDelegate::elidedText(const QFontMetrics &fontMetrics, int width,
- Qt::TextElideMode mode, const QString &text)
-{
- return fontMetrics.elidedText(text, mode, width);
-}
-#endif
-
/*!
\since 4.3
Whenever a help event occurs, this function is called with the \a event
@@ -389,21 +333,18 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
{
if (!event || !view)
return false;
- Q_D(QAbstractItemDelegate);
+ Q_UNUSED(index);
+ Q_UNUSED(option);
switch (event->type()) {
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
case QEvent::ToolTip: {
+ Q_D(QAbstractItemDelegate);
QHelpEvent *he = static_cast<QHelpEvent*>(event);
const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp
const QString tooltip = index.isValid() ?
d->textForRole(Qt::ToolTipRole, index.data(Qt::ToolTipRole), option.locale, precision) :
QString();
- QRect rect;
- if (index.isValid()) {
- const QRect r = view->visualRect(index);
- rect = QRect(view->mapToGlobal(r.topLeft()), r.size());
- }
- QToolTip::showText(he->globalPos(), tooltip, view, rect);
+ QToolTip::showText(he->globalPos(), tooltip, view->viewport(), option.rect);
event->setAccepted(!tooltip.isEmpty());
break;
}
@@ -413,6 +354,7 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
event->setAccepted(index.data(Qt::WhatsThisRole).isValid());
break;
case QEvent::WhatsThis: {
+ Q_D(QAbstractItemDelegate);
QHelpEvent *he = static_cast<QHelpEvent*>(event);
const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp
const QString whatsthis = index.isValid() ?
@@ -423,6 +365,7 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
break;
}
#endif
+ case QEvent::None:
default:
break;
}
@@ -434,9 +377,9 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
This virtual method is reserved and will be used in Qt 5.1.
*/
-QVector<int> QAbstractItemDelegate::paintingRoles() const
+QList<int> QAbstractItemDelegate::paintingRoles() const
{
- return QVector<int>();
+ return QList<int>();
}
QAbstractItemDelegatePrivate::QAbstractItemDelegatePrivate()
@@ -537,12 +480,13 @@ bool QAbstractItemDelegatePrivate::editorEventFilter(QObject *object, QEvent *ev
// If the application loses focus while editing, then the focus needs to go back
// to the itemview when the editor closes. This ensures that when the application
// is active again it will have the focus on the itemview as expected.
+ QWidget *editorParent = editor->parentWidget();
const bool manuallyFixFocus = (event->type() == QEvent::FocusOut) && !editor->hasFocus() &&
- editor->parentWidget() &&
+ editorParent &&
(static_cast<QFocusEvent *>(event)->reason() == Qt::ActiveWindowFocusReason);
emit q->closeEditor(editor, QAbstractItemDelegate::NoHint);
if (manuallyFixFocus)
- editor->parentWidget()->setFocus();
+ editorParent->setFocus();
}
#ifndef QT_NO_SHORTCUT
} else if (event->type() == QEvent::ShortcutOverride) {
@@ -570,8 +514,16 @@ bool QAbstractItemDelegatePrivate::tryFixup(QWidget *editor)
return e->hasAcceptableInput();
}
}
+#endif
+#if QT_CONFIG(spinbox)
+ // Give a chance to the spinbox to interpret the text and emit
+ // the appropriate signals before committing data.
+ if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox *>(editor)) {
+ if (!sb->keyboardTracking())
+ sb->interpretText();
+ }
#else
- Q_UNUSED(editor)
+ Q_UNUSED(editor);
#endif // QT_CONFIG(lineedit)
return true;
@@ -585,27 +537,27 @@ QString QAbstractItemDelegatePrivate::textForRole(Qt::ItemDataRole role, const Q
case QMetaType::Float:
text = locale.toString(value.toFloat());
break;
- case QVariant::Double:
+ case QMetaType::Double:
text = locale.toString(value.toDouble(), 'g', precision);
break;
- case QVariant::Int:
- case QVariant::LongLong:
+ case QMetaType::Int:
+ case QMetaType::LongLong:
text = locale.toString(value.toLongLong());
break;
- case QVariant::UInt:
- case QVariant::ULongLong:
+ case QMetaType::UInt:
+ case QMetaType::ULongLong:
text = locale.toString(value.toULongLong());
break;
- case QVariant::Date:
+ case QMetaType::QDate:
text = locale.toString(value.toDate(), formatType);
break;
- case QVariant::Time:
+ case QMetaType::QTime:
text = locale.toString(value.toTime(), formatType);
break;
- case QVariant::DateTime:
+ case QMetaType::QDateTime:
text = locale.toString(value.toDateTime(), formatType);
break;
- case QVariant::Type(QMetaType::QJsonValue): {
+ case QMetaType::QJsonValue: {
const QJsonValue val = value.toJsonValue();
if (val.isBool()) {
text = QVariant(val.toBool()).toString();
@@ -621,7 +573,7 @@ QString QAbstractItemDelegatePrivate::textForRole(Qt::ItemDataRole role, const Q
default: {
text = value.toString();
if (role == Qt::DisplayRole)
- text.replace(QLatin1Char('\n'), QChar::LineSeparator);
+ text.replace(u'\n', QChar::LineSeparator);
break;
}
}
diff --git a/src/widgets/itemviews/qabstractitemdelegate.h b/src/widgets/itemviews/qabstractitemdelegate.h
index 5696e55691..b28a1c4f80 100644
--- a/src/widgets/itemviews/qabstractitemdelegate.h
+++ b/src/widgets/itemviews/qabstractitemdelegate.h
@@ -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
#ifndef QABSTRACTITEMDELEGATE_H
#define QABSTRACTITEMDELEGATE_H
@@ -103,18 +67,12 @@ public:
const QStyleOptionViewItem &option,
const QModelIndex &index);
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X("Use QFontMetrics::elidedText() instead")
- static QString elidedText(const QFontMetrics &fontMetrics, int width,
- Qt::TextElideMode mode, const QString &text);
-#endif
-
virtual bool helpEvent(QHelpEvent *event,
QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index);
- virtual QVector<int> paintingRoles() const;
+ virtual QList<int> paintingRoles() const;
Q_SIGNALS:
void commitData(QWidget *editor);
diff --git a/src/widgets/itemviews/qabstractitemdelegate_p.h b/src/widgets/itemviews/qabstractitemdelegate_p.h
index da76d31e8b..e720f4d105 100644
--- a/src/widgets/itemviews/qabstractitemdelegate_p.h
+++ b/src/widgets/itemviews/qabstractitemdelegate_p.h
@@ -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
#ifndef QABSTRACTITEMDELEGATE_P_H
#define QABSTRACTITEMDELEGATE_P_H
@@ -55,6 +19,9 @@
#include "qabstractitemdelegate.h"
#include <private/qobject_p.h>
+#include <qvariant.h>
+#include <qmetatype.h>
+
QT_REQUIRE_CONFIG(itemviews);
QT_BEGIN_NAMESPACE
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index b1557e9af4..85e478a71e 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.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 "qabstractitemview.h"
@@ -49,7 +13,9 @@
#endif
#include <qevent.h>
#include <qscrollbar.h>
+#if QT_CONFIG(tooltip)
#include <qtooltip.h>
+#endif
#include <qdatetime.h>
#if QT_CONFIG(lineedit)
#include <qlineedit.h>
@@ -64,7 +30,7 @@
#include <private/qapplication_p.h>
#include <private/qguiapplication_p.h>
#include <private/qscrollbar_p.h>
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
#include <qaccessible.h>
#endif
#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
@@ -77,16 +43,19 @@ QT_BEGIN_NAMESPACE
QAbstractItemViewPrivate::QAbstractItemViewPrivate()
: model(QAbstractItemModelPrivate::staticEmptyModel()),
- itemDelegate(0),
- selectionModel(0),
+ itemDelegate(nullptr),
+ selectionModel(nullptr),
ctrlDragSelectionFlag(QItemSelectionModel::NoUpdate),
noSelectionOnMousePress(false),
selectionMode(QAbstractItemView::ExtendedSelection),
selectionBehavior(QAbstractItemView::SelectItems),
- currentlyCommittingEditor(0),
+ currentlyCommittingEditor(nullptr),
+ pressClosedEditor(false),
+ waitForIMCommit(false),
pressedModifiers(Qt::NoModifier),
pressedPosition(QPoint(-1, -1)),
pressedAlreadySelected(false),
+ releaseFromDoubleClick(false),
viewportEnteredNeeded(false),
state(QAbstractItemView::NoState),
stateBeforeAnimation(QAbstractItemView::NoState),
@@ -98,6 +67,7 @@ QAbstractItemViewPrivate::QAbstractItemViewPrivate()
dragEnabled(false),
dragDropMode(QAbstractItemView::NoDragDrop),
overwrite(false),
+ dropEventMoved(false),
dropIndicatorPosition(QAbstractItemView::OnItem),
defaultDropAction(Qt::IgnoreAction),
#endif
@@ -132,21 +102,22 @@ void QAbstractItemViewPrivate::init()
vbar->setRange(0, 0);
hbar->setRange(0, 0);
- QObject::connect(vbar, SIGNAL(actionTriggered(int)),
- q, SLOT(verticalScrollbarAction(int)));
- QObject::connect(hbar, SIGNAL(actionTriggered(int)),
- q, SLOT(horizontalScrollbarAction(int)));
- QObject::connect(vbar, SIGNAL(valueChanged(int)),
- q, SLOT(verticalScrollbarValueChanged(int)));
- QObject::connect(hbar, SIGNAL(valueChanged(int)),
- q, SLOT(horizontalScrollbarValueChanged(int)));
-
+ scrollbarConnections = {
+ QObject::connect(vbar, &QScrollBar::actionTriggered,
+ q, &QAbstractItemView::verticalScrollbarAction),
+ QObject::connect(hbar, &QScrollBar::actionTriggered,
+ q, &QAbstractItemView::horizontalScrollbarAction),
+ QObject::connect(vbar, &QScrollBar::valueChanged,
+ q, &QAbstractItemView::verticalScrollbarValueChanged),
+ QObject::connect(hbar, &QScrollBar::valueChanged,
+ q, &QAbstractItemView::horizontalScrollbarValueChanged)
+ };
viewport->setBackgroundRole(QPalette::Base);
q->setAttribute(Qt::WA_InputMethodEnabled);
- verticalScrollMode = static_cast<QAbstractItemView::ScrollMode>(q->style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, q, 0));
- horizontalScrollMode = static_cast<QAbstractItemView::ScrollMode>(q->style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, q, 0));
+ verticalScrollMode = static_cast<QAbstractItemView::ScrollMode>(q->style()->styleHint(QStyle::SH_ItemView_ScrollMode, nullptr, q, nullptr));
+ horizontalScrollMode = static_cast<QAbstractItemView::ScrollMode>(q->style()->styleHint(QStyle::SH_ItemView_ScrollMode, nullptr, q, nullptr));
}
void QAbstractItemViewPrivate::setHoverIndex(const QPersistentModelIndex &index)
@@ -159,8 +130,8 @@ void QAbstractItemViewPrivate::setHoverIndex(const QPersistentModelIndex &index)
q->update(hover); //update the old one
q->update(index); //update the new one
} else {
- QRect oldHoverRect = q->visualRect(hover);
- QRect newHoverRect = q->visualRect(index);
+ const QRect oldHoverRect = visualRect(hover);
+ const QRect newHoverRect = visualRect(index);
viewport->update(QRect(0, newHoverRect.y(), viewport->width(), newHoverRect.height()));
viewport->update(QRect(0, oldHoverRect.y(), viewport->width(), oldHoverRect.height()));
}
@@ -202,7 +173,7 @@ void QAbstractItemViewPrivate::checkMouseMove(const QPersistentModelIndex &index
#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
// stores and restores the selection and current item when flicking
-void QAbstractItemViewPrivate::_q_scrollerStateChanged()
+void QAbstractItemViewPrivate::scrollerStateChanged()
{
Q_Q(QAbstractItemView);
@@ -220,7 +191,11 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged()
// restore the old selection if we really start scrolling
if (q->selectionModel()) {
q->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
+ // block autoScroll logic while we are already handling scrolling
+ const bool wasAutoScroll = autoScroll;
+ autoScroll = false;
q->selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
+ autoScroll = wasAutoScroll;
}
Q_FALLTHROUGH();
@@ -234,6 +209,73 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged()
#endif // QT_NO_GESTURES
+void QAbstractItemViewPrivate::delegateSizeHintChanged(const QModelIndex &index)
+{
+ Q_Q(QAbstractItemView);
+ if (model) {
+ if (!model->checkIndex(index))
+ qWarning("Delegate size hint changed for a model index that does not belong to this view");
+ }
+ QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
+}
+
+void QAbstractItemViewPrivate::connectDelegate(QAbstractItemDelegate *delegate)
+{
+ if (!delegate)
+ return;
+ Q_Q(QAbstractItemView);
+ QObject::connect(delegate, &QAbstractItemDelegate::closeEditor,
+ q, &QAbstractItemView::closeEditor);
+ QObject::connect(delegate, &QAbstractItemDelegate::commitData,
+ q, &QAbstractItemView::commitData);
+ QObjectPrivate::connect(delegate, &QAbstractItemDelegate::sizeHintChanged,
+ this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
+}
+
+void QAbstractItemViewPrivate::disconnectDelegate(QAbstractItemDelegate *delegate)
+{
+ if (!delegate)
+ return;
+ Q_Q(QAbstractItemView);
+ QObject::disconnect(delegate, &QAbstractItemDelegate::closeEditor,
+ q, &QAbstractItemView::closeEditor);
+ QObject::disconnect(delegate, &QAbstractItemDelegate::commitData,
+ q, &QAbstractItemView::commitData);
+ QObjectPrivate::disconnect(delegate, &QAbstractItemDelegate::sizeHintChanged,
+ this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
+}
+
+void QAbstractItemViewPrivate::disconnectAll()
+{
+ Q_Q(QAbstractItemView);
+ for (const QMetaObject::Connection &connection : modelConnections)
+ QObject::disconnect(connection);
+ for (const QMetaObject::Connection &connection : scrollbarConnections)
+ QObject::disconnect(connection);
+ disconnectDelegate(itemDelegate);
+ for (QAbstractItemDelegate *delegate : std::as_const(rowDelegates))
+ disconnectDelegate(delegate);
+ for (QAbstractItemDelegate *delegate : std::as_const(columnDelegates))
+ disconnectDelegate(delegate);
+ if (model && selectionModel) {
+ QObject::disconnect(model, &QAbstractItemModel::destroyed,
+ selectionModel, &QItemSelectionModel::deleteLater);
+ }
+ if (selectionModel) {
+ QObject::disconnect(selectionModel, &QItemSelectionModel::selectionChanged,
+ q, &QAbstractItemView::selectionChanged);
+ QObject::disconnect(selectionModel, &QItemSelectionModel::currentChanged,
+ q, &QAbstractItemView::currentChanged);
+ }
+ for (const auto &info : std::as_const(indexEditorHash)) {
+ if (!info.isStatic && info.widget)
+ QObject::disconnect(info.widget, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
+ }
+#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
+ QObject::disconnect(scollerConnection);
+#endif
+}
+
/*!
\class QAbstractItemView
@@ -268,7 +310,7 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged()
\li Changes the current item and selects it. The previously
selected item(s) is not deselected.
\row
- \li Ctr+Space
+ \li Ctrl+Space
\li Toggles selection of the current item.
\row
\li Tab/Backtab
@@ -334,7 +376,7 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged()
\l{QWidget::update()}{update()} as all painting operations take place on the
viewport.
- \sa {View Classes}, {Model/View Programming}, QAbstractItemModel, {Chart Example}
+ \sa {View Classes}, {Model/View Programming}, QAbstractItemModel
*/
/*!
@@ -647,6 +689,7 @@ QAbstractItemView::~QAbstractItemView()
d->autoScrollTimer.stop();
d->delayedLayout.stop();
d->fetchMoreTimer.stop();
+ d->disconnectAll();
}
/*!
@@ -675,77 +718,47 @@ void QAbstractItemView::setModel(QAbstractItemModel *model)
if (model == d->model)
return;
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
- disconnect(d->model, SIGNAL(destroyed()),
- this, SLOT(_q_modelDestroyed()));
- disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
- this, SLOT(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
- disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
- this, SLOT(_q_headerDataChanged()));
- disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(rowsInserted(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_rowsMoved(QModelIndex,int,int,QModelIndex,int)));
- disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(_q_rowsInserted(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_columnsRemoved(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
- this, SLOT(_q_columnsInserted(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_columnsMoved(QModelIndex,int,int,QModelIndex,int)));
-
- disconnect(d->model, SIGNAL(modelReset()), this, SLOT(reset()));
- disconnect(d->model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged()));
+ for (const QMetaObject::Connection &connection : d->modelConnections)
+ disconnect(connection);
}
d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());
- // These asserts do basic sanity checking of the model
- Q_ASSERT_X(d->model->index(0,0) == d->model->index(0,0),
- "QAbstractItemView::setModel",
- "A model should return the exact same index "
- "(including its internal id/pointer) when asked for it twice in a row.");
- Q_ASSERT_X(!d->model->index(0,0).parent().isValid(),
- "QAbstractItemView::setModel",
- "The parent of a top level index should be invalid");
-
if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
- connect(d->model, SIGNAL(destroyed()),
- this, SLOT(_q_modelDestroyed()));
- connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
- this, SLOT(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
- connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
- this, SLOT(_q_headerDataChanged()));
- connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(rowsInserted(QModelIndex,int,int)));
- connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(_q_rowsInserted(QModelIndex,int,int)));
- connect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)));
- connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
- connect(d->model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_rowsMoved(QModelIndex,int,int,QModelIndex,int)));
- connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
- connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_columnsRemoved(QModelIndex,int,int)));
- connect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
- this, SLOT(_q_columnsInserted(QModelIndex,int,int)));
- connect(d->model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_columnsMoved(QModelIndex,int,int,QModelIndex,int)));
-
- connect(d->model, SIGNAL(modelReset()), this, SLOT(reset()));
- connect(d->model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged()));
+ d->modelConnections = {
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::destroyed,
+ d, &QAbstractItemViewPrivate::modelDestroyed),
+ QObject::connect(d->model, &QAbstractItemModel::dataChanged,
+ this, &QAbstractItemView::dataChanged),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged,
+ d, &QAbstractItemViewPrivate::headerDataChanged),
+ QObject::connect(d->model, &QAbstractItemModel::rowsInserted,
+ this, &QAbstractItemView::rowsInserted),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted,
+ d, &QAbstractItemViewPrivate::rowsInserted),
+ QObject::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved,
+ this, &QAbstractItemView::rowsAboutToBeRemoved),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved,
+ d, &QAbstractItemViewPrivate::rowsRemoved),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved,
+ d, &QAbstractItemViewPrivate::rowsMoved),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved,
+ d, &QAbstractItemViewPrivate::columnsAboutToBeRemoved),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved,
+ d, &QAbstractItemViewPrivate::columnsRemoved),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted,
+ d, &QAbstractItemViewPrivate::columnsInserted),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved,
+ d, &QAbstractItemViewPrivate::columnsMoved),
+ QObject::connect(d->model, &QAbstractItemModel::modelReset,
+ this, &QAbstractItemView::reset),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged,
+ d, &QAbstractItemViewPrivate::layoutChanged),
+ };
}
QItemSelectionModel *selection_model = new QItemSelectionModel(d->model, this);
- connect(d->model, SIGNAL(destroyed()), selection_model, SLOT(deleteLater()));
+ connect(d->model, &QAbstractItemModel::destroyed,
+ selection_model, &QItemSelectionModel::deleteLater);
setSelectionModel(selection_model);
reset(); // kill editors, set new root and do layout
@@ -757,7 +770,7 @@ void QAbstractItemView::setModel(QAbstractItemModel *model)
QAbstractItemModel *QAbstractItemView::model() const
{
Q_D(const QAbstractItemView);
- return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ? 0 : d->model);
+ return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ? nullptr : d->model);
}
/*!
@@ -795,20 +808,19 @@ void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
oldSelection = d->selectionModel->selection();
oldCurrentIndex = d->selectionModel->currentIndex();
}
-
- disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
- this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
- disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- this, SLOT(currentChanged(QModelIndex,QModelIndex)));
+ disconnect(d->selectionModel, &QItemSelectionModel::selectionChanged,
+ this, &QAbstractItemView::selectionChanged);
+ disconnect(d->selectionModel, &QItemSelectionModel::currentChanged,
+ this, &QAbstractItemView::currentChanged);
}
d->selectionModel = selectionModel;
if (d->selectionModel) {
- connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
- this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
- connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- this, SLOT(currentChanged(QModelIndex,QModelIndex)));
+ connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
+ this, &QAbstractItemView::selectionChanged);
+ connect(d->selectionModel, &QItemSelectionModel::currentChanged,
+ this, &QAbstractItemView::currentChanged);
selectionChanged(d->selectionModel->selection(), oldSelection);
currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
@@ -848,21 +860,13 @@ void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
return;
if (d->itemDelegate) {
- if (d->delegateRefCount(d->itemDelegate) == 1) {
- disconnect(d->itemDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- disconnect(d->itemDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
- disconnect(d->itemDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
- }
+ if (d->delegateRefCount(d->itemDelegate) == 1)
+ d->disconnectDelegate(delegate);
}
if (delegate) {
- if (d->delegateRefCount(delegate) == 0) {
- connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
- connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
- }
+ if (d->delegateRefCount(delegate) == 0)
+ d->connectDelegate(delegate);
}
d->itemDelegate = delegate;
viewport()->update();
@@ -885,10 +889,26 @@ QAbstractItemDelegate *QAbstractItemView::itemDelegate() const
*/
QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query) const
{
+ Q_D(const QAbstractItemView);
const QModelIndex current = currentIndex();
- if (!current.isValid() || query != Qt::ImCursorRectangle)
- return QAbstractScrollArea::inputMethodQuery(query);
- return visualRect(current);
+ QVariant result;
+ if (current.isValid()) {
+ if (QWidget *currentEditor;
+ d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
+ // An editor is open but the initial preedit is still ongoing. Delegate
+ // queries to the editor and map coordinates from editor to this view.
+ result = currentEditor->inputMethodQuery(query);
+ if (result.typeId() == QMetaType::QRect) {
+ const QRect editorRect = result.value<QRect>();
+ result = QRect(currentEditor->mapTo(this, editorRect.topLeft()), editorRect.size());
+ }
+ } else if (query == Qt::ImCursorRectangle) {
+ result = visualRect(current);
+ }
+ }
+ if (!result.isValid())
+ result = QAbstractScrollArea::inputMethodQuery(query);
+ return result;
}
/*!
@@ -915,22 +935,14 @@ QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query) const
void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *delegate)
{
Q_D(QAbstractItemView);
- if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row, 0)) {
- if (d->delegateRefCount(rowDelegate) == 1) {
- disconnect(rowDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- disconnect(rowDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
- disconnect(rowDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
- }
+ if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row, nullptr)) {
+ if (d->delegateRefCount(rowDelegate) == 1)
+ d->disconnectDelegate(rowDelegate);
d->rowDelegates.remove(row);
}
if (delegate) {
- if (d->delegateRefCount(delegate) == 0) {
- connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
- connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
- }
+ if (d->delegateRefCount(delegate) == 0)
+ d->connectDelegate(delegate);
d->rowDelegates.insert(row, delegate);
}
viewport()->update();
@@ -949,7 +961,7 @@ void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *de
QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(int row) const
{
Q_D(const QAbstractItemView);
- return d->rowDelegates.value(row, 0);
+ return d->rowDelegates.value(row, nullptr);
}
/*!
@@ -975,22 +987,14 @@ QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(int row) const
void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate)
{
Q_D(QAbstractItemView);
- if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column, 0)) {
- if (d->delegateRefCount(columnDelegate) == 1) {
- disconnect(columnDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- disconnect(columnDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
- disconnect(columnDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
- }
+ if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column, nullptr)) {
+ if (d->delegateRefCount(columnDelegate) == 1)
+ d->disconnectDelegate(columnDelegate);
d->columnDelegates.remove(column);
}
if (delegate) {
- if (d->delegateRefCount(delegate) == 0) {
- connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
- connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
- }
+ if (d->delegateRefCount(delegate) == 0)
+ d->connectDelegate(delegate);
d->columnDelegates.insert(column, delegate);
}
viewport()->update();
@@ -1009,14 +1013,25 @@ void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelega
QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(int column) const
{
Q_D(const QAbstractItemView);
- return d->columnDelegates.value(column, 0);
+ return d->columnDelegates.value(column, nullptr);
}
/*!
+ \fn QAbstractItemDelegate *QAbstractItemView::itemDelegate(const QModelIndex &index) const
+ \deprecated Use itemDelegateForIndex() instead.
+ Returns the item delegate used by this view and model for
+ the given \a index.
+*/
+
+/*!
+ \since 6.0
+
Returns the item delegate used by this view and model for
the given \a index.
+
+ \sa setItemDelegate(), setItemDelegateForRow(), setItemDelegateForColumn()
*/
-QAbstractItemDelegate *QAbstractItemView::itemDelegate(const QModelIndex &index) const
+QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(const QModelIndex &index) const
{
Q_D(const QAbstractItemView);
return d->delegateForIndex(index);
@@ -1084,7 +1099,7 @@ void QAbstractItemView::setCurrentIndex(const QModelIndex &index)
{
Q_D(QAbstractItemView);
if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
- QItemSelectionModel::SelectionFlags command = selectionCommand(index, 0);
+ QItemSelectionModel::SelectionFlags command = selectionCommand(index, nullptr);
d->selectionModel->setCurrentIndex(index, command);
d->currentIndexSet = true;
if ((command & QItemSelectionModel::Current) == 0)
@@ -1117,7 +1132,11 @@ void QAbstractItemView::reset()
{
Q_D(QAbstractItemView);
d->delayedReset.stop(); //make sure we stop the timer
- foreach (const QEditorInfo &info, d->indexEditorHash) {
+ // Taking a copy because releaseEditor() eventurally calls deleteLater() on the
+ // editor, which calls QCoreApplication::postEvent(), the latter may invoke unknown
+ // code that may modify d->indexEditorHash.
+ const auto copy = d->indexEditorHash;
+ for (const auto &[index, info] : copy.asKeyValueRange()) {
if (info.widget)
d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
}
@@ -1129,7 +1148,7 @@ void QAbstractItemView::reset()
setRootIndex(QModelIndex());
if (d->selectionModel)
d->selectionModel->reset();
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(this, QAccessibleTableModelChangeEvent::ModelReset);
QAccessible::updateAccessibility(&accessibleEvent);
@@ -1151,6 +1170,12 @@ void QAbstractItemView::setRootIndex(const QModelIndex &index)
return;
}
d->root = index;
+#if QT_CONFIG(accessibility)
+ if (QAccessible::isActive()) {
+ QAccessibleTableModelChangeEvent accessibleEvent(this, QAccessibleTableModelChangeEvent::ModelReset);
+ QAccessible::updateAccessibility(&accessibleEvent);
+ }
+#endif
d->doDelayedItemsLayout();
d->updateGeometry();
}
@@ -1176,12 +1201,21 @@ QModelIndex QAbstractItemView::rootIndex() const
void QAbstractItemView::selectAll()
{
Q_D(QAbstractItemView);
- SelectionMode mode = d->selectionMode;
- if (mode == MultiSelection || mode == ExtendedSelection)
+ const SelectionMode mode = d->selectionMode;
+ switch (mode) {
+ case MultiSelection:
+ case ExtendedSelection:
d->selectAll(QItemSelectionModel::ClearAndSelect
- |d->selectionBehaviorFlags());
- else if (mode != SingleSelection)
- d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
+ | d->selectionBehaviorFlags());
+ break;
+ case NoSelection:
+ case ContiguousSelection:
+ if (d->model->hasChildren(d->root))
+ d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
+ break;
+ case SingleSelection:
+ break;
+ }
}
/*!
@@ -1201,7 +1235,7 @@ void QAbstractItemView::edit(const QModelIndex &index)
Q_D(QAbstractItemView);
if (Q_UNLIKELY(!d->isIndexValid(index)))
qWarning("edit: index was invalid");
- if (Q_UNLIKELY(!edit(index, AllEditTriggers, 0)))
+ if (Q_UNLIKELY(!edit(index, AllEditTriggers, nullptr)))
qWarning("edit: editing failed");
}
@@ -1286,7 +1320,7 @@ QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode() const
void QAbstractItemView::resetVerticalScrollMode()
{
- auto sm = static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, this, 0));
+ auto sm = static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode, nullptr, this, nullptr));
setVerticalScrollMode(sm);
d_func()->verticalScrollModeSet = false;
}
@@ -1323,7 +1357,7 @@ QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode() const
void QAbstractItemView::resetHorizontalScrollMode()
{
- auto sm = static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, this, 0));
+ auto sm = static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode, nullptr, this, nullptr));
setHorizontalScrollMode(sm);
d_func()->horizontalScrollModeSet = false;
}
@@ -1534,7 +1568,7 @@ QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode() const
/*!
\property QAbstractItemView::defaultDropAction
- \brief the drop action that will be used by default in QAbstractItemView::drag()
+ \brief the drop action that will be used by default in QAbstractItemView::drag().
If the property is not set, the drop action is CopyAction when the supported
actions support CopyAction.
@@ -1627,7 +1661,7 @@ Qt::TextElideMode QAbstractItemView::textElideMode() const
bool QAbstractItemView::focusNextPrevChild(bool next)
{
Q_D(QAbstractItemView);
- if (d->tabKeyNavigation && isEnabled() && d->viewport->isEnabled()) {
+ if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
keyPressEvent(&event);
if (event.isAccepted())
@@ -1698,9 +1732,15 @@ bool QAbstractItemView::viewportEvent(QEvent *event)
{
Q_D(QAbstractItemView);
switch (event->type()) {
+ case QEvent::Paint:
+ // Similar to pre-painting in QAbstractItemView::event to update scrollbar
+ // visibility, make sure that all pending layout requests have been executed
+ // so that the view's data structures are up-to-date before rendering.
+ d->executePostedLayout();
+ break;
case QEvent::HoverMove:
case QEvent::HoverEnter:
- d->setHoverIndex(indexAt(static_cast<QHoverEvent*>(event)->pos()));
+ d->setHoverIndex(indexAt(static_cast<QHoverEvent*>(event)->position().toPoint()));
break;
case QEvent::HoverLeave:
d->setHoverIndex(QModelIndex());
@@ -1725,11 +1765,12 @@ bool QAbstractItemView::viewportEvent(QEvent *event)
case QEvent::WhatsThis: {
QHelpEvent *he = static_cast<QHelpEvent*>(event);
const QModelIndex index = indexAt(he->pos());
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
option.rect = visualRect(index);
option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
- QAbstractItemDelegate *delegate = d->delegateForIndex(index);
+ QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
if (!delegate)
return false;
return delegate->helpEvent(he, this, option, index);
@@ -1744,7 +1785,10 @@ bool QAbstractItemView::viewportEvent(QEvent *event)
case QEvent::ScrollPrepare:
executeDelayedItemsLayout();
#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
- connect(QScroller::scroller(d->viewport), SIGNAL(stateChanged(QScroller::State)), this, SLOT(_q_scrollerStateChanged()), Qt::UniqueConnection);
+ d->scollerConnection = QObjectPrivate::connect(
+ QScroller::scroller(d->viewport), &QScroller::stateChanged,
+ d, &QAbstractItemViewPrivate::scrollerStateChanged,
+ Qt::UniqueConnection);
#endif
break;
@@ -1762,12 +1806,15 @@ bool QAbstractItemView::viewportEvent(QEvent *event)
void QAbstractItemView::mousePressEvent(QMouseEvent *event)
{
Q_D(QAbstractItemView);
+ d->releaseFromDoubleClick = false;
d->delayedAutoScroll.stop(); //any interaction with the view cancel the auto scrolling
- QPoint pos = event->pos();
+ QPoint pos = event->position().toPoint();
QPersistentModelIndex index = indexAt(pos);
- if (!d->selectionModel
- || (d->state == EditingState && d->hasEditor(index)))
+ // this is the mouse press event that closed the last editor (via focus event)
+ d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
+
+ if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
return;
d->pressedAlreadySelected = d->selectionModel->isSelected(index);
@@ -1776,8 +1823,16 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event)
QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
QPoint offset = d->offset();
- d->pressedPosition = pos + offset;
- if ((command & QItemSelectionModel::Current) == 0) {
+ d->draggedPosition = pos + offset;
+
+#if QT_CONFIG(draganddrop)
+ // update the pressed position when drag was enable
+ if (d->dragEnabled)
+ d->pressedPosition = d->draggedPosition;
+#endif
+
+ if (!(command & QItemSelectionModel::Current)) {
+ d->pressedPosition = pos + offset;
d->currentSelectionStartIndex = index;
}
else if (!d->currentSelectionStartIndex.isValid())
@@ -1799,7 +1854,7 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event)
command |= d->ctrlDragSelectionFlag;
}
- if ((command & QItemSelectionModel::Current) == 0) {
+ if (!(command & QItemSelectionModel::Current)) {
setSelection(QRect(pos, QSize(1, 1)), command);
} else {
QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
@@ -1828,21 +1883,16 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event)
void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QAbstractItemView);
- QPoint topLeft;
- QPoint bottomRight = event->pos();
+ QPoint bottomRight = event->position().toPoint();
+
+ d->draggedPosition = bottomRight + d->offset();
if (state() == ExpandingState || state() == CollapsingState)
return;
#if QT_CONFIG(draganddrop)
if (state() == DraggingState) {
- topLeft = d->pressedPosition - d->offset();
- if ((topLeft - bottomRight).manhattanLength() > QApplication::startDragDistance()) {
- d->pressedIndex = QModelIndex();
- startDrag(d->model->supportedDragActions());
- setState(NoState); // the startDrag will return when the dnd operation is done
- stopAutoScroll();
- }
+ d->maybeStartDrag(bottomRight);
return;
}
#endif // QT_CONFIG(draganddrop)
@@ -1853,10 +1903,8 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
|| edit(index, NoEditTriggers, event))
return;
- if (d->selectionMode != SingleSelection)
- topLeft = d->pressedPosition - d->offset();
- else
- topLeft = bottomRight;
+ const QPoint topLeft =
+ d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
d->checkMouseMove(index);
@@ -1867,6 +1915,7 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
&& (event->buttons() != Qt::NoButton)
&& !d->selectedDraggableIndexes().isEmpty()) {
setState(DraggingState);
+ d->maybeStartDrag(bottomRight);
return;
}
#endif
@@ -1884,10 +1933,10 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
setSelection(selectionRect, command);
// set at the end because it might scroll the view
- if (index.isValid()
- && (index != d->selectionModel->currentIndex())
- && d->isIndexEnabled(index))
+ if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
+ else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
+ startAutoScroll();
}
}
@@ -1901,8 +1950,10 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
{
Q_D(QAbstractItemView);
+ const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
+ d->releaseFromDoubleClick = false;
- QPoint pos = event->pos();
+ QPoint pos = event->position().toPoint();
QPersistentModelIndex index = indexAt(pos);
if (state() == EditingState) {
@@ -1913,18 +1964,22 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
return;
}
- bool click = (index == d->pressedIndex && index.isValid());
- bool selectedClicked = click && (event->button() == Qt::LeftButton) && d->pressedAlreadySelected;
+ bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
+ bool selectedClicked = click && d->pressedAlreadySelected
+ && (event->button() == Qt::LeftButton)
+ && (event->modifiers() == Qt::NoModifier);
EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
- const bool edited = click ? edit(index, trigger, event) : false;
+ const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) : false;
d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
if (d->selectionModel && d->noSelectionOnMousePress) {
d->noSelectionOnMousePress = false;
- d->selectionModel->select(index, selectionCommand(index, event));
+ if (!d->pressClosedEditor)
+ d->selectionModel->select(index, selectionCommand(index, event));
}
+ d->pressClosedEditor = false;
setState(NoState);
if (click) {
@@ -1932,7 +1987,8 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
emit clicked(index);
if (edited)
return;
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
if (d->pressedAlreadySelected)
option.state |= QStyle::State_Selected;
if ((d->model->flags(index) & Qt::ItemIsEnabled)
@@ -1950,13 +2006,14 @@ void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_D(QAbstractItemView);
- QModelIndex index = indexAt(event->pos());
+ QModelIndex index = indexAt(event->position().toPoint());
if (!index.isValid()
|| !d->isIndexEnabled(index)
|| (d->pressedIndex != index)) {
QMouseEvent me(QEvent::MouseButtonPress,
- event->localPos(), event->windowPos(), event->screenPos(),
- event->button(), event->buttons(), event->modifiers(), event->source());
+ event->position(), event->scenePosition(), event->globalPosition(),
+ event->button(), event->buttons(), event->modifiers(),
+ event->source(), event->pointingDevice());
mousePressEvent(&me);
return;
}
@@ -1964,9 +2021,9 @@ void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
QPersistentModelIndex persistent = index;
emit doubleClicked(persistent);
if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
- && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, this))
+ && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, nullptr, this))
emit activated(persistent);
- d->pressedIndex = QModelIndex();
+ d->releaseFromDoubleClick = true;
}
#if QT_CONFIG(draganddrop)
@@ -2003,6 +2060,7 @@ void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
{
Q_D(QAbstractItemView);
+ d->draggedPosition = event->position().toPoint() + d->offset();
if (dragDropMode() == InternalMove
&& (event->source() != this || !(event->possibleActions() & Qt::MoveAction)))
return;
@@ -2010,14 +2068,14 @@ void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
// ignore by default
event->ignore();
- QModelIndex index = indexAt(event->pos());
+ QModelIndex index = indexAt(event->position().toPoint());
d->hover = index;
if (!d->droppingOnItself(event, index)
&& d->canDrop(event)) {
if (index.isValid() && d->showDropIndicator) {
QRect rect = visualRect(index);
- d->dropIndicatorPosition = d->position(event->pos(), rect, index);
+ d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
switch (d->dropIndicatorPosition) {
case AboveItem:
if (d->isIndexDropEnabled(index.parent())) {
@@ -2060,7 +2118,7 @@ void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
d->viewport->update();
} // can drop
- if (d->shouldAutoScroll(event->pos()))
+ if (d->shouldAutoScroll(event->position().toPoint()))
startAutoScroll();
}
@@ -2157,9 +2215,9 @@ bool QAbstractItemViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *drop
QModelIndex index;
// rootIndex() (i.e. the viewport) might be a valid index
- if (viewport->rect().contains(event->pos())) {
- index = q->indexAt(event->pos());
- if (!index.isValid() || !q->visualRect(index).contains(event->pos()))
+ if (viewport->rect().contains(event->position().toPoint())) {
+ index = q->indexAt(event->position().toPoint());
+ if (!index.isValid() || !q->visualRect(index).contains(event->position().toPoint()))
index = root;
}
@@ -2168,7 +2226,7 @@ bool QAbstractItemViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *drop
int row = -1;
int col = -1;
if (index != root) {
- dropIndicatorPosition = position(event->pos(), q->visualRect(index), index);
+ dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
switch (dropIndicatorPosition) {
case QAbstractItemView::AboveItem:
row = index.row();
@@ -2336,11 +2394,12 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event)
#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
if (event == QKeySequence::Copy) {
- QVariant variant;
- if (d->model)
- variant = d->model->data(currentIndex(), Qt::DisplayRole);
- if (variant.type() == QVariant::String)
- QGuiApplication::clipboard()->setText(variant.toString());
+ const QModelIndex index = currentIndex();
+ if (index.isValid() && d->model) {
+ const QVariant variant = d->model->data(index, Qt::DisplayRole);
+ if (variant.canConvert<QString>())
+ QGuiApplication::clipboard()->setText(variant.toString());
+ }
event->accept();
}
#endif
@@ -2388,7 +2447,7 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event)
setFocus();
QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
if (command != QItemSelectionModel::NoUpdate
- || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection, 0, this)) {
+ || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection, nullptr, this)) {
// note that we don't check if the new current index is enabled because moveCursor() makes sure it is
if (command & QItemSelectionModel::Current) {
d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
@@ -2464,7 +2523,7 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event)
}
#endif
break;
-#ifdef Q_OS_OSX
+#ifdef Q_OS_MACOS
case Qt::Key_Enter:
case Qt::Key_Return:
// Propagate the enter if you couldn't edit the item and there are no
@@ -2479,7 +2538,7 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event)
break;
case Qt::Key_Enter:
case Qt::Key_Return:
- // ### we can't open the editor on enter, becuse
+ // ### we can't open the editor on enter, because
// some widgets will forward the enter event back
// to the viewport, starting an endless loop
if (state() != EditingState || hasFocus()) {
@@ -2496,7 +2555,7 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event)
break;
}
#endif
-#ifdef Q_OS_OSX
+#ifdef Q_OS_MACOS
if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
emit activated(currentIndex());
break;
@@ -2562,6 +2621,8 @@ void QAbstractItemView::timerEvent(QTimerEvent *event)
//we only get here if there was no double click
if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
scrollTo(d->pressedIndex);
+ } else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
+ d->pressClosedEditorWatcher.stop();
}
}
@@ -2570,14 +2631,53 @@ void QAbstractItemView::timerEvent(QTimerEvent *event)
*/
void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
{
- if (event->commitString().isEmpty() && event->preeditString().isEmpty()) {
+ Q_D(QAbstractItemView);
+ // When QAbstractItemView::AnyKeyPressed is used, a new IM composition might
+ // start before the editor widget acquires focus. Changing focus would interrupt
+ // the composition, so we keep focus on the view until that first composition
+ // is complete, and pass QInputMethoEvents on to the editor widget so that the
+ // user gets the expected feedback. See also inputMethodQuery, which redirects
+ // calls to the editor widget during that period.
+ bool forwardEventToEditor = false;
+ const bool commit = !event->commitString().isEmpty();
+ const bool preediting = !event->preeditString().isEmpty();
+ if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
+ if (d->waitForIMCommit) {
+ if (commit || !preediting) {
+ // commit or cancel
+ d->waitForIMCommit = false;
+ QApplication::sendEvent(currentEditor, event);
+ if (!commit) {
+ QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
+ if (delegate)
+ delegate->setEditorData(currentEditor, currentIndex());
+ d->selectAllInEditor(currentEditor);
+ }
+ if (currentEditor->focusPolicy() != Qt::NoFocus)
+ currentEditor->setFocus();
+ } else {
+ // more pre-editing
+ QApplication::sendEvent(currentEditor, event);
+ }
+ return;
+ }
+ } else if (preediting) {
+ // don't set focus when the editor opens
+ d->waitForIMCommit = true;
+ // but pass preedit on to editor
+ forwardEventToEditor = true;
+ } else if (!commit) {
event->ignore();
return;
}
if (!edit(currentIndex(), AnyKeyPressed, event)) {
- if (!event->commitString().isEmpty())
+ d->waitForIMCommit = false;
+ if (commit)
keyboardSearch(event->commitString());
event->ignore();
+ } else if (QWidget *currentEditor; forwardEventToEditor
+ && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
+ QApplication::sendEvent(currentEditor, event);
}
}
@@ -2628,8 +2728,7 @@ QModelIndexList QAbstractItemView::selectedIndexes() const
auto isHidden = [this](const QModelIndex &idx) {
return isIndexHidden(idx);
};
- const auto end = indexes.end();
- indexes.erase(std::remove_if(indexes.begin(), end, isHidden), end);
+ indexes.removeIf(isHidden);
}
return indexes;
}
@@ -2654,10 +2753,13 @@ bool QAbstractItemView::edit(const QModelIndex &index, EditTrigger trigger, QEve
if (!d->isIndexValid(index))
return false;
- if (QWidget *w = (d->persistent.isEmpty() ? static_cast<QWidget*>(0) : d->editorForIndex(index).widget.data())) {
+ if (QWidget *w = (d->persistent.isEmpty() ? static_cast<QWidget*>(nullptr) : d->editorForIndex(index).widget.data())) {
if (w->focusPolicy() == Qt::NoFocus)
return false;
- w->setFocus();
+ if (!d->waitForIMCommit)
+ w->setFocus();
+ else
+ updateMicroFocus();
return true;
}
@@ -2668,16 +2770,23 @@ bool QAbstractItemView::edit(const QModelIndex &index, EditTrigger trigger, QEve
d->delayedEditing.stop();
}
+ // in case e.g. setData() triggers a reset()
+ QPersistentModelIndex safeIndex(index);
+
if (d->sendDelegateEvent(index, event)) {
- update(index);
+ update(safeIndex);
return true;
}
+ if (!safeIndex.isValid()) {
+ return false;
+ }
+
// save the previous trigger before updating
EditTriggers lastTrigger = d->lastTrigger;
d->lastTrigger = trigger;
- if (!d->shouldEdit(trigger, d->model->buddy(index)))
+ if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
return false;
if (d->delayedEditing.isActive())
@@ -2692,7 +2801,7 @@ bool QAbstractItemView::edit(const QModelIndex &index, EditTrigger trigger, QEve
if (trigger == SelectedClicked)
d->delayedEditing.start(QApplication::doubleClickInterval(), this);
else
- d->openEditor(index, d->shouldForwardEvent(trigger, event) ? event : 0);
+ d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event : nullptr);
return true;
}
@@ -2714,14 +2823,15 @@ void QAbstractItemView::updateEditorData()
void QAbstractItemView::updateEditorGeometries()
{
Q_D(QAbstractItemView);
- if(d->editorIndexHash.isEmpty())
+ if (d->editorIndexHash.isEmpty())
return;
if (d->delayedPendingLayout) {
// doItemsLayout() will end up calling this function again
d->executePostedLayout();
return;
}
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
QEditorIndexHash::iterator it = d->editorIndexHash.begin();
QWidgetList editorsToRelease;
QWidgetList editorsToHide;
@@ -2732,7 +2842,7 @@ void QAbstractItemView::updateEditorGeometries()
option.rect = visualRect(index);
if (option.rect.isValid()) {
editor->show();
- QAbstractItemDelegate *delegate = d->delegateForIndex(index);
+ QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
if (delegate)
delegate->updateEditorGeometry(editor, option, index);
} else {
@@ -2748,10 +2858,10 @@ void QAbstractItemView::updateEditorGeometries()
//we hide and release the editor outside of the loop because it might change the focus and try
//to change the editors hashes.
- for (int i = 0; i < editorsToHide.count(); ++i) {
+ for (int i = 0; i < editorsToHide.size(); ++i) {
editorsToHide.at(i)->hide();
}
- for (int i = 0; i < editorsToRelease.count(); ++i) {
+ for (int i = 0; i < editorsToRelease.size(); ++i) {
d->releaseEditor(editorsToRelease.at(i));
}
}
@@ -2826,33 +2936,50 @@ void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndE
// Close the editor
if (editor) {
- bool isPersistent = d->persistent.contains(editor);
- bool hadFocus = editor->hasFocus();
- QModelIndex index = d->indexForEditor(editor);
- if (!index.isValid())
- return; // the editor was not registered
-
- if (!isPersistent) {
- setState(NoState);
- QModelIndex index = d->indexForEditor(editor);
- editor->removeEventFilter(d->delegateForIndex(index));
- d->removeEditor(editor);
- }
- if (hadFocus) {
- if (focusPolicy() != Qt::NoFocus)
- setFocus(); // this will send a focusLost event to the editor
- else
- editor->clearFocus();
+ const bool isPersistent = d->persistent.contains(editor);
+ const QModelIndex index = d->indexForEditor(editor);
+ if (!index.isValid()) {
+ if (!editor->isVisible()) {
+ // The commit might have removed the index (e.g. it might get filtered), in
+ // which case the editor is already hidden and scheduled for deletion. We
+ // don't have to do anything, except reset the state, and continue with
+ // EndEditHint processing.
+ if (!isPersistent)
+ setState(NoState);
+ } else {
+ qWarning("QAbstractItemView::closeEditor called with an editor that does not belong to this view");
+ return;
+ }
} else {
- d->checkPersistentEditorFocus();
- }
+ const bool hadFocus = editor->hasFocus();
+ // start a timer that expires immediately when we return to the event loop
+ // to identify whether this close was triggered by a mousepress-initiated
+ // focus event
+ d->pressClosedEditorWatcher.start(0, this);
+ d->lastEditedIndex = index;
+
+ if (!isPersistent) {
+ setState(NoState);
+ QModelIndex index = d->indexForEditor(editor);
+ editor->removeEventFilter(itemDelegateForIndex(index));
+ d->removeEditor(editor);
+ }
+ if (hadFocus) {
+ if (focusPolicy() != Qt::NoFocus)
+ setFocus(); // this will send a focusLost event to the editor
+ else
+ editor->clearFocus();
+ } else {
+ d->checkPersistentEditorFocus();
+ }
- QPointer<QWidget> ed = editor;
- QCoreApplication::sendPostedEvents(editor, 0);
- editor = ed;
+ QPointer<QWidget> ed = editor;
+ QCoreApplication::sendPostedEvents(editor, 0);
+ editor = ed;
- if (!isPersistent && editor)
- d->releaseEditor(editor, index);
+ if (!isPersistent && editor)
+ d->releaseEditor(editor, index);
+ }
}
// The EndEditHint part
@@ -2902,14 +3029,16 @@ void QAbstractItemView::commitData(QWidget *editor)
if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
return;
QModelIndex index = d->indexForEditor(editor);
- if (!index.isValid())
+ if (!index.isValid()) {
+ qWarning("QAbstractItemView::commitData called with an editor that does not belong to this view");
return;
+ }
d->currentlyCommittingEditor = editor;
- QAbstractItemDelegate *delegate = d->delegateForIndex(index);
+ QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
editor->removeEventFilter(delegate);
delegate->setModelData(editor, d->model, index);
editor->installEventFilter(delegate);
- d->currentlyCommittingEditor = 0;
+ d->currentlyCommittingEditor = nullptr;
}
/*!
@@ -2927,65 +3056,7 @@ void QAbstractItemView::editorDestroyed(QObject *editor)
setState(NoState);
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \obsolete
- Sets the horizontal scroll bar's steps per item to \a steps.
-
- This is the number of steps used by the horizontal scroll bar to
- represent the width of an item.
-
- Note that if the view has a horizontal header, the item steps
- will be ignored and the header section size will be used instead.
-
- \sa horizontalStepsPerItem(), setVerticalStepsPerItem()
-*/
-void QAbstractItemView::setHorizontalStepsPerItem(int steps)
-{
- Q_UNUSED(steps)
- // do nothing
-}
-
-/*!
- \obsolete
- Returns the horizontal scroll bar's steps per item.
- \sa setHorizontalStepsPerItem(), verticalStepsPerItem()
-*/
-int QAbstractItemView::horizontalStepsPerItem() const
-{
- return 1;
-}
-
-/*!
- \obsolete
- Sets the vertical scroll bar's steps per item to \a steps.
-
- This is the number of steps used by the vertical scroll bar to
- represent the height of an item.
-
- Note that if the view has a vertical header, the item steps
- will be ignored and the header section size will be used instead.
-
- \sa verticalStepsPerItem(), setHorizontalStepsPerItem()
-*/
-void QAbstractItemView::setVerticalStepsPerItem(int steps)
-{
- Q_UNUSED(steps)
- // do nothing
-}
-
-/*!
- \obsolete
- Returns the vertical scroll bar's steps per item.
-
- \sa setVerticalStepsPerItem(), horizontalStepsPerItem()
-*/
-int QAbstractItemView::verticalStepsPerItem() const
-{
- return 1;
-}
-#endif
/*!
Moves to and selects the item best matching the string \a search.
@@ -3020,9 +3091,9 @@ void QAbstractItemView::keyboardSearch(const QString &search)
// special case for searches with same key like 'aaaaa'
bool sameKey = false;
- if (d->keyboardInput.length() > 1) {
- int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.length() - 1));
- sameKey = (c == d->keyboardInput.length());
+ if (d->keyboardInput.size() > 1) {
+ int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
+ sameKey = (c == d->keyboardInput.size());
if (sameKey)
skipRow = true;
}
@@ -3074,9 +3145,12 @@ void QAbstractItemView::keyboardSearch(const QString &search)
QSize QAbstractItemView::sizeHintForIndex(const QModelIndex &index) const
{
Q_D(const QAbstractItemView);
- if (!d->isIndexValid(index) || !d->itemDelegate)
+ if (!d->isIndexValid(index))
return QSize();
- return d->delegateForIndex(index)->sizeHint(d->viewOptionsV1(), index);
+ const auto delegate = itemDelegateForIndex(index);
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+ return delegate ? delegate->sizeHint(option, index) : QSize();
}
/*!
@@ -3104,14 +3178,15 @@ int QAbstractItemView::sizeHintForRow(int row) const
ensurePolished();
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
int height = 0;
int colCount = d->model->columnCount(d->root);
for (int c = 0; c < colCount; ++c) {
const QModelIndex index = d->model->index(row, c, d->root);
if (QWidget *editor = d->editorForIndex(index).widget.data())
height = qMax(height, editor->height());
- if (const QAbstractItemDelegate *delegate = d->delegateForIndex(index))
+ if (const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
height = qMax(height, delegate->sizeHint(option, index).height());
}
return height;
@@ -3134,14 +3209,15 @@ int QAbstractItemView::sizeHintForColumn(int column) const
ensurePolished();
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
int width = 0;
int rows = d->model->rowCount(d->root);
for (int r = 0; r < rows; ++r) {
const QModelIndex index = d->model->index(r, column, d->root);
if (QWidget *editor = d->editorForIndex(index).widget.data())
width = qMax(width, editor->sizeHint().width());
- if (const QAbstractItemDelegate *delegate = d->delegateForIndex(index))
+ if (const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
width = qMax(width, delegate->sizeHint(option, index).width());
}
return width;
@@ -3156,7 +3232,8 @@ int QAbstractItemView::sizeHintForColumn(int column) const
void QAbstractItemView::openPersistentEditor(const QModelIndex &index)
{
Q_D(QAbstractItemView);
- QStyleOptionViewItem options = d->viewOptionsV1();
+ QStyleOptionViewItem options;
+ initViewItemOption(&options);
options.rect = visualRect(index);
options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
@@ -3243,8 +3320,10 @@ void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget
widget->installEventFilter(this);
widget->show();
dataChanged(index, index); // update the geometry
- if (!d->delayedPendingLayout)
+ if (!d->delayedPendingLayout) {
widget->setGeometry(visualRect(index));
+ d->doDelayedItemsLayout(); // relayout due to updated geometry
+ }
}
}
@@ -3260,7 +3339,7 @@ QWidget* QAbstractItemView::indexWidget(const QModelIndex &index) const
if (QWidget *editor = d->editorForIndex(index).widget.data())
return editor;
- return 0;
+ return nullptr;
}
/*!
@@ -3302,8 +3381,8 @@ void QAbstractItemView::update(const QModelIndex &index)
{
Q_D(QAbstractItemView);
if (index.isValid()) {
- const QRect rect = visualRect(index);
- //this test is important for peformance reason
+ const QRect rect = d->visualRect(index);
+ //this test is important for performance reason
//For example in dataChanged we simply update all the cells without checking
//it can be a major bottleneck to update rects that aren't even part of the viewport
if (d->viewport->rect().intersects(rect))
@@ -3318,8 +3397,11 @@ void QAbstractItemView::update(const QModelIndex &index)
The \a roles which have been changed can either be an empty container (meaning everything
has changed), or a non-empty container with the subset of roles which have changed.
+
+ \note: Qt::ToolTipRole is not honored by dataChanged() in the views provided by Qt.
*/
-void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
+void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles)
{
Q_UNUSED(roles);
// Single item changed
@@ -3328,7 +3410,7 @@ void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelInde
const QEditorInfo &editorInfo = d->editorForIndex(topLeft);
//we don't update the edit data if it is static
if (!editorInfo.isStatic && editorInfo.widget) {
- QAbstractItemDelegate *delegate = d->delegateForIndex(topLeft);
+ QAbstractItemDelegate *delegate = itemDelegateForIndex(topLeft);
if (delegate) {
delegate->setEditorData(editorInfo.widget.data(), topLeft);
}
@@ -3339,11 +3421,22 @@ void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelInde
}
} else {
d->updateEditorData(topLeft, bottomRight);
- if (isVisible() && !d->delayedPendingLayout)
- d->viewport->update();
+ if (isVisible() && !d->delayedPendingLayout) {
+ if (!topLeft.isValid() ||
+ topLeft.parent() != bottomRight.parent() ||
+ topLeft.row() > bottomRight.row() ||
+ topLeft.column() > bottomRight.column()) {
+ // invalid parameter - call update() to redraw all
+ d->viewport->update();
+ } else {
+ const QRect updateRect = d->intersectedRect(d->viewport->rect(), topLeft, bottomRight);
+ if (!updateRect.isEmpty())
+ d->viewport->update(updateRect);
+ }
+ }
}
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(this, QAccessibleTableModelChangeEvent::DataChanged);
accessibleEvent.setFirstRow(topLeft.row());
@@ -3401,24 +3494,59 @@ void QAbstractItemView::rowsAboutToBeRemoved(const QModelIndex &parent, int star
} else {
int row = end + 1;
QModelIndex next;
- do { // find the next visible and enabled item
+ const int rowCount = d->model->rowCount(parent);
+ bool found = false;
+ // find the next visible and enabled item
+ while (row < rowCount && !found) {
next = d->model->index(row++, current.column(), current.parent());
- } while (next.isValid() && (isIndexHidden(next) || !d->isIndexEnabled(next)));
- if (row > d->model->rowCount(parent)) {
+#ifdef QT_DEBUG
+ if (!next.isValid()) {
+ qWarning("Model unexpectedly returned an invalid index");
+ break;
+ }
+#endif
+ if (!isIndexHidden(next) && d->isIndexEnabled(next)) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
row = start - 1;
- do { // find the previous visible and enabled item
+ // find the previous visible and enabled item
+ while (row >= 0) {
next = d->model->index(row--, current.column(), current.parent());
- } while (next.isValid() && (isIndexHidden(next) || !d->isIndexEnabled(next)));
+#ifdef QT_DEBUG
+ if (!next.isValid()) {
+ qWarning("Model unexpectedly returned an invalid index");
+ break;
+ }
+#endif
+ if (!isIndexHidden(next) && d->isIndexEnabled(next))
+ break;
+ }
}
+
setCurrentIndex(next);
}
}
// Remove all affected editors; this is more efficient than waiting for updateGeometries() to clean out editors for invalid indexes
+ const auto findDirectChildOf = [](const QModelIndex &parent, QModelIndex child)
+ {
+ while (child.isValid()) {
+ const auto parentIndex = child.parent();
+ if (parentIndex == parent)
+ return child;
+ child = parentIndex;
+ }
+ return QModelIndex();
+ };
QEditorIndexHash::iterator i = d->editorIndexHash.begin();
while (i != d->editorIndexHash.end()) {
const QModelIndex index = i.value();
- if (index.row() >= start && index.row() <= end && d->model->parent(index) == parent) {
+ const QModelIndex directChild = findDirectChildOf(parent, index);
+ if (directChild.isValid() && directChild.row() >= start && directChild.row() <= end) {
QWidget *editor = i.key();
QEditorInfo info = d->indexEditorHash.take(index);
i = d->editorIndexHash.erase(i);
@@ -3437,17 +3565,17 @@ void QAbstractItemView::rowsAboutToBeRemoved(const QModelIndex &parent, int star
rows are those under the given \a parent from \a start to \a end
inclusive.
*/
-void QAbstractItemViewPrivate::_q_rowsRemoved(const QModelIndex &index, int start, int end)
+void QAbstractItemViewPrivate::rowsRemoved(const QModelIndex &index, int start, int end)
{
- Q_UNUSED(index)
- Q_UNUSED(start)
- Q_UNUSED(end)
+ Q_UNUSED(index);
+ Q_UNUSED(start);
+ Q_UNUSED(end);
Q_Q(QAbstractItemView);
if (q->isVisible())
q->updateEditorGeometries();
q->setState(QAbstractItemView::NoState);
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(q, QAccessibleTableModelChangeEvent::RowsRemoved);
accessibleEvent.setFirstRow(start);
@@ -3465,7 +3593,7 @@ void QAbstractItemViewPrivate::_q_rowsRemoved(const QModelIndex &index, int star
columns are those under the given \a parent from \a start to \a end
inclusive.
*/
-void QAbstractItemViewPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
+void QAbstractItemViewPrivate::columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
Q_Q(QAbstractItemView);
@@ -3487,9 +3615,19 @@ void QAbstractItemViewPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &par
} else {
int column = end;
QModelIndex next;
- do { // find the next visible and enabled item
+ const int columnCount = model->columnCount(current.parent());
+ // find the next visible and enabled item
+ while (column < columnCount) {
next = model->index(current.row(), column++, current.parent());
- } while (next.isValid() && (q->isIndexHidden(next) || !isIndexEnabled(next)));
+#ifdef QT_DEBUG
+ if (!next.isValid()) {
+ qWarning("Model unexpectedly returned an invalid index");
+ break;
+ }
+#endif
+ if (!q->isIndexHidden(next) && isIndexEnabled(next))
+ break;
+ }
q->setCurrentIndex(next);
}
}
@@ -3518,17 +3656,17 @@ void QAbstractItemViewPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &par
rows are those under the given \a parent from \a start to \a end
inclusive.
*/
-void QAbstractItemViewPrivate::_q_columnsRemoved(const QModelIndex &index, int start, int end)
+void QAbstractItemViewPrivate::columnsRemoved(const QModelIndex &index, int start, int end)
{
- Q_UNUSED(index)
- Q_UNUSED(start)
- Q_UNUSED(end)
+ Q_UNUSED(index);
+ Q_UNUSED(start);
+ Q_UNUSED(end);
Q_Q(QAbstractItemView);
if (q->isVisible())
q->updateEditorGeometries();
q->setState(QAbstractItemView::NoState);
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(q, QAccessibleTableModelChangeEvent::ColumnsRemoved);
accessibleEvent.setFirstColumn(start);
@@ -3545,13 +3683,13 @@ void QAbstractItemViewPrivate::_q_columnsRemoved(const QModelIndex &index, int s
This slot is called when rows have been inserted.
*/
-void QAbstractItemViewPrivate::_q_rowsInserted(const QModelIndex &index, int start, int end)
+void QAbstractItemViewPrivate::rowsInserted(const QModelIndex &index, int start, int end)
{
- Q_UNUSED(index)
- Q_UNUSED(start)
- Q_UNUSED(end)
+ Q_UNUSED(index);
+ Q_UNUSED(start);
+ Q_UNUSED(end);
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
Q_Q(QAbstractItemView);
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(q, QAccessibleTableModelChangeEvent::RowsInserted);
@@ -3568,16 +3706,16 @@ void QAbstractItemViewPrivate::_q_rowsInserted(const QModelIndex &index, int sta
This slot is called when columns have been inserted.
*/
-void QAbstractItemViewPrivate::_q_columnsInserted(const QModelIndex &index, int start, int end)
+void QAbstractItemViewPrivate::columnsInserted(const QModelIndex &index, int start, int end)
{
- Q_UNUSED(index)
- Q_UNUSED(start)
- Q_UNUSED(end)
+ Q_UNUSED(index);
+ Q_UNUSED(start);
+ Q_UNUSED(end);
Q_Q(QAbstractItemView);
if (q->isVisible())
q->updateEditorGeometries();
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(q, QAccessibleTableModelChangeEvent::ColumnsInserted);
accessibleEvent.setFirstColumn(start);
@@ -3591,7 +3729,7 @@ void QAbstractItemViewPrivate::_q_columnsInserted(const QModelIndex &index, int
/*!
\internal
*/
-void QAbstractItemViewPrivate::_q_modelDestroyed()
+void QAbstractItemViewPrivate::modelDestroyed()
{
model = QAbstractItemModelPrivate::staticEmptyModel();
doDelayedReset();
@@ -3602,10 +3740,10 @@ void QAbstractItemViewPrivate::_q_modelDestroyed()
This slot is called when the layout is changed.
*/
-void QAbstractItemViewPrivate::_q_layoutChanged()
+void QAbstractItemViewPrivate::layoutChanged()
{
doDelayedItemsLayout();
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
Q_Q(QAbstractItemView);
if (QAccessible::isActive()) {
QAccessibleTableModelChangeEvent accessibleEvent(q, QAccessibleTableModelChangeEvent::ModelReset);
@@ -3614,14 +3752,27 @@ void QAbstractItemViewPrivate::_q_layoutChanged()
#endif
}
-void QAbstractItemViewPrivate::_q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
+void QAbstractItemViewPrivate::rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
+{
+ layoutChanged();
+}
+
+void QAbstractItemViewPrivate::columnsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
{
- _q_layoutChanged();
+ layoutChanged();
}
-void QAbstractItemViewPrivate::_q_columnsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
+QRect QAbstractItemViewPrivate::intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const
{
- _q_layoutChanged();
+ Q_Q(const QAbstractItemView);
+
+ const auto parentIdx = topLeft.parent();
+ QRect updateRect;
+ for (int r = topLeft.row(); r <= bottomRight.row(); ++r) {
+ for (int c = topLeft.column(); c <= bottomRight.column(); ++c)
+ updateRect |= q->visualRect(model->index(r, c, parentIdx));
+ }
+ return rect.intersected(updateRect);
}
/*!
@@ -3673,7 +3824,7 @@ void QAbstractItemView::currentChanged(const QModelIndex &current, const QModelI
if (d->autoScroll)
scrollTo(current);
update(current);
- edit(current, CurrentChanged, 0);
+ edit(current, CurrentChanged, nullptr);
if (current.row() == (d->model->rowCount(d->root) - 1))
d->fetchMore();
} else {
@@ -3691,7 +3842,7 @@ void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
{
Q_D(QAbstractItemView);
QModelIndexList indexes = d->selectedDraggableIndexes();
- if (indexes.count() > 0) {
+ if (indexes.size() > 0) {
QMimeData *data = d->model->mimeData(indexes);
if (!data)
return;
@@ -3703,12 +3854,18 @@ void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
drag->setMimeData(data);
drag->setHotSpot(d->pressedPosition - rect.topLeft());
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
+ if (dragDropMode() == InternalMove)
+ supportedActions &= ~Qt::CopyAction;
if (d->defaultDropAction != Qt::IgnoreAction && (supportedActions & d->defaultDropAction))
defaultDropAction = d->defaultDropAction;
else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove)
defaultDropAction = Qt::CopyAction;
- if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
- d->clearOrRemove();
+ d->dropEventMoved = false;
+ if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction && !d->dropEventMoved) {
+ if (dragDropMode() != InternalMove || drag->target() == viewport())
+ d->clearOrRemove();
+ }
+ d->dropEventMoved = false;
// Reset the drop indicator
d->dropIndicatorRect = QRect();
d->dropIndicatorPosition = OnItem;
@@ -3717,47 +3874,45 @@ void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
#endif // QT_CONFIG(draganddrop)
/*!
- Returns a QStyleOptionViewItem structure populated with the view's
- palette, font, state, alignments etc.
+ \since 6.0
+
+ Initialize the \a option structure with the view's palette, font, state,
+ alignments etc.
+
+ \note Implementations of this methods should check the \l{QStyleOption::}{version}
+ of the structure received, populate all members the implementation is familiar with,
+ and set the version member to the one supported by the implementation before returning.
*/
-QStyleOptionViewItem QAbstractItemView::viewOptions() const
+void QAbstractItemView::initViewItemOption(QStyleOptionViewItem *option) const
{
Q_D(const QAbstractItemView);
- QStyleOptionViewItem option;
- option.init(this);
- option.state &= ~QStyle::State_MouseOver;
- option.font = font();
+ option->initFrom(this);
+ option->state &= ~QStyle::State_MouseOver;
+ option->font = font();
// On mac the focus appearance follows window activation
// not widget activation
if (!hasFocus())
- option.state &= ~QStyle::State_Active;
+ option->state &= ~QStyle::State_Active;
- option.state &= ~QStyle::State_HasFocus;
+ option->state &= ~QStyle::State_HasFocus;
if (d->iconSize.isValid()) {
- option.decorationSize = d->iconSize;
+ option->decorationSize = d->iconSize;
} else {
- int pm = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this);
- option.decorationSize = QSize(pm, pm);
+ int pm = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this);
+ option->decorationSize = QSize(pm, pm);
}
- option.decorationPosition = QStyleOptionViewItem::Left;
- option.decorationAlignment = Qt::AlignCenter;
- option.displayAlignment = Qt::AlignLeft|Qt::AlignVCenter;
- option.textElideMode = d->textElideMode;
- option.rect = QRect();
- option.showDecorationSelected = style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, this);
+ option->decorationPosition = QStyleOptionViewItem::Left;
+ option->decorationAlignment = Qt::AlignCenter;
+ option->displayAlignment = Qt::AlignLeft|Qt::AlignVCenter;
+ option->textElideMode = d->textElideMode;
+ option->rect = QRect();
+ option->showDecorationSelected = style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, nullptr, this);
if (d->wrapItemText)
- option.features = QStyleOptionViewItem::WrapText;
- option.locale = locale();
- option.locale.setNumberOptions(QLocale::OmitGroupSeparator);
- option.widget = this;
- return option;
-}
-
-QStyleOptionViewItem QAbstractItemViewPrivate::viewOptionsV1() const
-{
- Q_Q(const QAbstractItemView);
- return q->viewOptions();
+ option->features = QStyleOptionViewItem::WrapText;
+ option->locale = locale();
+ option->locale.setNumberOptions(QLocale::OmitGroupSeparator);
+ option->widget = this;
}
/*!
@@ -3898,17 +4053,17 @@ void QAbstractItemView::doAutoScroll()
}
}
- int verticalStep = verticalScroll->pageStep();
- int horizontalStep = horizontalScroll->pageStep();
+ const int verticalStep = verticalScroll->pageStep();
+ const int horizontalStep = horizontalScroll->pageStep();
if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
++d->autoScrollCount;
- int margin = d->autoScrollMargin;
- int verticalValue = verticalScroll->value();
- int horizontalValue = horizontalScroll->value();
+ const int margin = d->autoScrollMargin;
+ const int verticalValue = verticalScroll->value();
+ const int horizontalValue = horizontalScroll->value();
- QPoint pos = d->viewport->mapFromGlobal(QCursor::pos());
- QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
+ const QPoint pos = d->draggedPosition - d->offset();
+ const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
// do the scrolling if we are in the scroll margins
if (pos.y() - area.top() < margin)
@@ -3920,8 +4075,8 @@ void QAbstractItemView::doAutoScroll()
else if (area.right() - pos.x() < margin)
horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
// if nothing changed, stop scrolling
- bool verticalUnchanged = (verticalValue == verticalScroll->value());
- bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
+ const bool verticalUnchanged = (verticalValue == verticalScroll->value());
+ const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
if (verticalUnchanged && horizontalUnchanged) {
stopAutoScroll();
} else {
@@ -3929,14 +4084,39 @@ void QAbstractItemView::doAutoScroll()
d->dropIndicatorRect = QRect();
d->dropIndicatorPosition = QAbstractItemView::OnViewport;
#endif
+ switch (state()) {
+ case QAbstractItemView::DragSelectingState: {
+ // mouseMoveEvent updates the drag-selection rectangle, so fake an event. This also
+ // updates draggedPosition taking the now scrolled viewport into account.
+ const QPoint globalPos = d->viewport->mapToGlobal(pos);
+ const QPoint windowPos = window()->mapFromGlobal(globalPos);
+ QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
+ Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
+ Qt::MouseEventSynthesizedByQt);
+ QApplication::sendEvent(viewport(), &mm);
+ break;
+ }
+ case QAbstractItemView::DraggingState: {
+ // we can't simulate mouse (it would throw off the drag'n'drop state logic) or drag
+ // (we don't have the mime data or the actions) move events during drag'n'drop, so
+ // update our dragged position manually after the scroll. "pos" is the old
+ // draggedPosition - d->offset(), and d->offset() is now updated after scrolling, so
+ // pos + d->offset() gives us the new position.
+ d->draggedPosition = pos + d->offset();
+ break;
+ }
+ default:
+ break;
+ }
d->viewport->update();
}
}
/*!
- Returns the SelectionFlags to be used when updating a selection with
- to include the \a index specified. The \a event is a user input event,
- such as a mouse or keyboard event.
+ Returns the SelectionFlags to be used when updating a selection model
+ for the specified \a index. The result depends on the current
+ selectionMode(), and on the user input event \a event, which can be
+ \nullptr.
Reimplement this function to define your own selection behavior.
@@ -3946,31 +4126,35 @@ QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QM
const QEvent *event) const
{
Q_D(const QAbstractItemView);
- Qt::KeyboardModifiers keyModifiers = Qt::NoModifier;
- if (event) {
- switch (event->type()) {
- case QEvent::MouseButtonDblClick:
- case QEvent::MouseButtonPress:
- case QEvent::MouseButtonRelease:
- case QEvent::MouseMove:
- case QEvent::KeyPress:
- case QEvent::KeyRelease:
- keyModifiers = (static_cast<const QInputEvent*>(event))->modifiers();
- break;
- default:
- keyModifiers = QGuiApplication::keyboardModifiers();
- }
- }
+ Qt::KeyboardModifiers keyModifiers = event && event->isInputEvent()
+ ? static_cast<const QInputEvent*>(event)->modifiers()
+ : Qt::NoModifier;
switch (d->selectionMode) {
case NoSelection: // Never update selection model
return QItemSelectionModel::NoUpdate;
case SingleSelection: // ClearAndSelect on valid index otherwise NoUpdate
- if (event && event->type() == QEvent::MouseButtonRelease)
- return QItemSelectionModel::NoUpdate;
- if ((keyModifiers & Qt::ControlModifier) && d->selectionModel->isSelected(index) && event->type() != QEvent::MouseMove)
- return QItemSelectionModel::Deselect | d->selectionBehaviorFlags();
- else
- return QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
+ if (event) {
+ switch (event->type()) {
+ case QEvent::MouseButtonPress:
+ // press with any modifiers on a selected item does nothing
+ if (d->pressedAlreadySelected)
+ return QItemSelectionModel::NoUpdate;
+ break;
+ case QEvent::MouseButtonRelease:
+ // clicking into area with no items does nothing
+ if (!index.isValid())
+ return QItemSelectionModel::NoUpdate;
+ Q_FALLTHROUGH();
+ case QEvent::KeyPress:
+ // ctrl-release on selected item deselects
+ if ((keyModifiers & Qt::ControlModifier) && d->selectionModel->isSelected(index))
+ return QItemSelectionModel::Deselect | d->selectionBehaviorFlags();
+ break;
+ default:
+ break;
+ }
+ }
+ return QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
case MultiSelection:
return d->multiSelectionCommand(index, event);
case ExtendedSelection:
@@ -3984,7 +4168,7 @@ QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QM
QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::multiSelectionCommand(
const QModelIndex &index, const QEvent *event) const
{
- Q_UNUSED(index)
+ Q_UNUSED(index);
if (event) {
switch (event->type()) {
@@ -3994,16 +4178,31 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::multiSelectionComm
return QItemSelectionModel::Toggle|selectionBehaviorFlags();
break;
case QEvent::MouseButtonPress:
- if (static_cast<const QMouseEvent*>(event)->button() == Qt::LeftButton)
- return QItemSelectionModel::Toggle|selectionBehaviorFlags(); // toggle
+ if (static_cast<const QMouseEvent*>(event)->button() == Qt::LeftButton) {
+ // since the press might start a drag, deselect only on release
+ if (!pressedAlreadySelected
+#if QT_CONFIG(draganddrop)
+ || !dragEnabled || !isIndexDragEnabled(index)
+#endif
+ )
+ return QItemSelectionModel::Toggle|selectionBehaviorFlags(); // toggle
+ }
break;
case QEvent::MouseButtonRelease:
- if (static_cast<const QMouseEvent*>(event)->button() == Qt::LeftButton)
+ if (static_cast<const QMouseEvent*>(event)->button() == Qt::LeftButton) {
+ if (pressedAlreadySelected
+#if QT_CONFIG(draganddrop)
+ && dragEnabled && isIndexDragEnabled(index)
+#endif
+ && index == pressedIndex)
+ return QItemSelectionModel::Toggle|selectionBehaviorFlags();
return QItemSelectionModel::NoUpdate|selectionBehaviorFlags(); // finalize
+ }
break;
case QEvent::MouseMove:
if (static_cast<const QMouseEvent*>(event)->buttons() & Qt::LeftButton)
return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags(); // toggle drag select
+ break;
default:
break;
}
@@ -4016,18 +4215,18 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::multiSelectionComm
QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
const QModelIndex &index, const QEvent *event) const
{
- Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
+ Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
+ ? static_cast<const QInputEvent*>(event)->modifiers()
+ : QGuiApplication::keyboardModifiers();
if (event) {
switch (event->type()) {
case QEvent::MouseMove: {
// Toggle on MouseMove
- modifiers = static_cast<const QMouseEvent*>(event)->modifiers();
if (modifiers & Qt::ControlModifier)
return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
break;
}
case QEvent::MouseButtonPress: {
- modifiers = static_cast<const QMouseEvent*>(event)->modifiers();
const Qt::MouseButton button = static_cast<const QMouseEvent*>(event)->button();
const bool rightButtonPressed = button & Qt::RightButton;
const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
@@ -4041,11 +4240,18 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionC
return QItemSelectionModel::Clear;
if (!index.isValid())
return QItemSelectionModel::NoUpdate;
+ // since the press might start a drag, deselect only on release
+ if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
+#if QT_CONFIG(draganddrop)
+ && dragEnabled && isIndexDragEnabled(index)
+#endif
+ ) {
+ return QItemSelectionModel::NoUpdate;
+ }
break;
}
case QEvent::MouseButtonRelease: {
// ClearAndSelect on MouseButtonRelease if MouseButtonPress on selected item or empty area
- modifiers = static_cast<const QMouseEvent*>(event)->modifiers();
const Qt::MouseButton button = static_cast<const QMouseEvent*>(event)->button();
const bool rightButtonPressed = button & Qt::RightButton;
const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
@@ -4054,11 +4260,17 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionC
|| !index.isValid()) && state != QAbstractItemView::DragSelectingState
&& !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
+ if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
+#if QT_CONFIG(draganddrop)
+ && dragEnabled && isIndexDragEnabled(index)
+#endif
+ ) {
+ break;
+ }
return QItemSelectionModel::NoUpdate;
}
case QEvent::KeyPress: {
// NoUpdate on Key movement and Ctrl
- modifiers = static_cast<const QKeyEvent*>(event)->modifiers();
switch (static_cast<const QKeyEvent*>(event)->key()) {
case Qt::Key_Backtab:
modifiers = modifiers & ~Qt::ShiftModifier; // special case for backtab
@@ -4089,6 +4301,7 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionC
default:
break;
}
+ break;
}
default:
break;
@@ -4223,39 +4436,48 @@ void QAbstractItemViewPrivate::updateGeometry()
q->updateGeometry();
}
+/*
+ Handles selection of content for some editors containing QLineEdit.
+
+ ### Qt 7 This should be done by a virtual method in QAbstractItemDelegate.
+*/
+void QAbstractItemViewPrivate::selectAllInEditor(QWidget *editor)
+{
+ while (QWidget *fp = editor->focusProxy())
+ editor = fp;
+
+#if QT_CONFIG(lineedit)
+ if (QLineEdit *le = qobject_cast<QLineEdit*>(editor))
+ le->selectAll();
+#endif
+#if QT_CONFIG(spinbox)
+ if (QSpinBox *sb = qobject_cast<QSpinBox*>(editor))
+ sb->selectAll();
+ else if (QDoubleSpinBox *dsb = qobject_cast<QDoubleSpinBox*>(editor))
+ dsb->selectAll();
+#endif
+}
+
QWidget *QAbstractItemViewPrivate::editor(const QModelIndex &index,
const QStyleOptionViewItem &options)
{
Q_Q(QAbstractItemView);
QWidget *w = editorForIndex(index).widget.data();
if (!w) {
- QAbstractItemDelegate *delegate = delegateForIndex(index);
+ QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
if (!delegate)
- return 0;
+ return nullptr;
w = delegate->createEditor(viewport, options, index);
if (w) {
w->installEventFilter(delegate);
- QObject::connect(w, SIGNAL(destroyed(QObject*)), q, SLOT(editorDestroyed(QObject*)));
+ QObject::connect(w, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
delegate->updateEditorGeometry(w, options, index);
delegate->setEditorData(w, index);
addEditor(index, w, false);
if (w->parent() == viewport)
QWidget::setTabOrder(q, w);
- // Special cases for some editors containing QLineEdit
- QWidget *focusWidget = w;
- while (QWidget *fp = focusWidget->focusProxy())
- focusWidget = fp;
-#if QT_CONFIG(lineedit)
- if (QLineEdit *le = qobject_cast<QLineEdit*>(focusWidget))
- le->selectAll();
-#endif
-#if QT_CONFIG(spinbox)
- if (QSpinBox *sb = qobject_cast<QSpinBox*>(focusWidget))
- sb->selectAll();
- else if (QDoubleSpinBox *dsb = qobject_cast<QDoubleSpinBox*>(focusWidget))
- dsb->selectAll();
-#endif
+ selectAllInEditor(w);
}
}
@@ -4264,6 +4486,7 @@ QWidget *QAbstractItemViewPrivate::editor(const QModelIndex &index,
void QAbstractItemViewPrivate::updateEditorData(const QModelIndex &tl, const QModelIndex &br)
{
+ Q_Q(QAbstractItemView);
// we are counting on having relatively few editors
const bool checkIndexes = tl.isValid() && br.isValid();
const QModelIndex parent = tl.parent();
@@ -4282,7 +4505,7 @@ void QAbstractItemViewPrivate::updateEditorData(const QModelIndex &tl, const QMo
|| index.parent() != parent)))
continue;
- QAbstractItemDelegate *delegate = delegateForIndex(index);
+ QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
if (delegate) {
delegate->setEditorData(editor, index);
}
@@ -4294,7 +4517,7 @@ void QAbstractItemViewPrivate::updateEditorData(const QModelIndex &tl, const QMo
In DND if something has been moved then this is called.
Typically this means you should "remove" the selected item or row,
- but the behavior is view dependant (table just clears the selected indexes for example).
+ but the behavior is view-dependent (table just clears the selected indexes for example).
Either remove the selected rows or clear them
*/
@@ -4384,6 +4607,9 @@ QModelIndex QAbstractItemViewPrivate::indexForEditor(QWidget *editor) const
void QAbstractItemViewPrivate::removeEditor(QWidget *editor)
{
+ Q_Q(QAbstractItemView);
+ if (editor)
+ QObject::disconnect(editor, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
const auto it = editorIndexHash.constFind(editor);
if (it != editorIndexHash.cend()) {
indexEditorHash.remove(it.value());
@@ -4401,10 +4627,11 @@ bool QAbstractItemViewPrivate::sendDelegateEvent(const QModelIndex &index, QEven
{
Q_Q(const QAbstractItemView);
QModelIndex buddy = model->buddy(index);
- QStyleOptionViewItem options = viewOptionsV1();
+ QStyleOptionViewItem options;
+ q->initViewItemOption(&options);
options.rect = q->visualRect(buddy);
options.state |= (buddy == q->currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
- QAbstractItemDelegate *delegate = delegateForIndex(index);
+ QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
return (event && delegate && delegate->editorEvent(event, model, options, buddy));
}
@@ -4413,7 +4640,8 @@ bool QAbstractItemViewPrivate::openEditor(const QModelIndex &index, QEvent *even
Q_Q(QAbstractItemView);
QModelIndex buddy = model->buddy(index);
- QStyleOptionViewItem options = viewOptionsV1();
+ QStyleOptionViewItem options;
+ q->initViewItemOption(&options);
options.rect = q->visualRect(buddy);
options.state |= (buddy == q->currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
@@ -4423,7 +4651,10 @@ bool QAbstractItemViewPrivate::openEditor(const QModelIndex &index, QEvent *even
q->setState(QAbstractItemView::EditingState);
w->show();
- w->setFocus();
+ if (!waitForIMCommit)
+ w->setFocus();
+ else
+ q->updateMicroFocus();
if (event)
QCoreApplication::sendEvent(w->focusProxy() ? w->focusProxy() : w, event);
@@ -4451,12 +4682,15 @@ QItemViewPaintPairs QAbstractItemViewPrivate::draggablePaintPairs(const QModelIn
rect |= current;
}
}
- rect &= viewportRect;
+ QRect clipped = rect & viewportRect;
+ rect.setLeft(clipped.left());
+ rect.setRight(clipped.right());
return ret;
}
QPixmap QAbstractItemViewPrivate::renderToPixmap(const QModelIndexList &indexes, QRect *r) const
{
+ Q_Q(const QAbstractItemView);
Q_ASSERT(r);
QItemViewPaintPairs paintPairs = draggablePaintPairs(indexes, r);
if (paintPairs.isEmpty())
@@ -4470,13 +4704,14 @@ QPixmap QAbstractItemViewPrivate::renderToPixmap(const QModelIndexList &indexes,
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
- QStyleOptionViewItem option = viewOptionsV1();
+ QStyleOptionViewItem option;
+ q->initViewItemOption(&option);
option.state |= QStyle::State_Selected;
- for (int j = 0; j < paintPairs.count(); ++j) {
+ for (int j = 0; j < paintPairs.size(); ++j) {
option.rect = paintPairs.at(j).rect.translated(-r->topLeft());
const QModelIndex &current = paintPairs.at(j).index;
adjustViewOptionsForIndex(&option, current);
- delegateForIndex(current)->paint(&painter, option, current);
+ q->itemDelegateForIndex(current)->paint(&painter, option, current);
}
return pixmap;
}
@@ -4485,6 +4720,8 @@ void QAbstractItemViewPrivate::selectAll(QItemSelectionModel::SelectionFlags com
{
if (!selectionModel)
return;
+ if (!model->hasChildren(root))
+ return;
QItemSelection selection;
QModelIndex tl = model->index(0, 0, root);
@@ -4495,6 +4732,7 @@ void QAbstractItemViewPrivate::selectAll(QItemSelectionModel::SelectionFlags com
selectionModel->select(selection, command);
}
+#if QT_CONFIG(draganddrop)
QModelIndexList QAbstractItemViewPrivate::selectedDraggableIndexes() const
{
Q_Q(const QAbstractItemView);
@@ -4502,12 +4740,25 @@ QModelIndexList QAbstractItemViewPrivate::selectedDraggableIndexes() const
auto isNotDragEnabled = [this](const QModelIndex &index) {
return !isIndexDragEnabled(index);
};
- indexes.erase(std::remove_if(indexes.begin(), indexes.end(),
- isNotDragEnabled),
- indexes.end());
+ indexes.removeIf(isNotDragEnabled);
return indexes;
}
+void QAbstractItemViewPrivate::maybeStartDrag(QPoint eventPosition)
+{
+ Q_Q(QAbstractItemView);
+
+ const QPoint topLeft = pressedPosition - offset();
+ if ((topLeft - eventPosition).manhattanLength() > QApplication::startDragDistance()) {
+ pressedIndex = QModelIndex();
+ q->startDrag(model->supportedDragActions());
+ q->setState(QAbstractItemView::NoState); // the startDrag will return when the dnd operation
+ // is done
+ q->stopAutoScroll();
+ }
+}
+#endif
+
/*!
\reimp
*/
diff --git a/src/widgets/itemviews/qabstractitemview.h b/src/widgets/itemviews/qabstractitemview.h
index be8fa07c94..837419100a 100644
--- a/src/widgets/itemviews/qabstractitemview.h
+++ b/src/widgets/itemviews/qabstractitemview.h
@@ -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
#ifndef QABSTRACTITEMVIEW_H
#define QABSTRACTITEMVIEW_H
@@ -74,11 +38,14 @@ class Q_WIDGETS_EXPORT QAbstractItemView : public QAbstractScrollArea
#endif
Q_PROPERTY(bool alternatingRowColors READ alternatingRowColors WRITE setAlternatingRowColors)
Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
- Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior)
+ Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior
+ WRITE setSelectionBehavior)
Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode)
- Q_PROPERTY(ScrollMode verticalScrollMode READ verticalScrollMode WRITE setVerticalScrollMode RESET resetVerticalScrollMode)
- Q_PROPERTY(ScrollMode horizontalScrollMode READ horizontalScrollMode WRITE setHorizontalScrollMode RESET resetHorizontalScrollMode)
+ Q_PROPERTY(ScrollMode verticalScrollMode READ verticalScrollMode WRITE setVerticalScrollMode
+ RESET resetVerticalScrollMode)
+ Q_PROPERTY(ScrollMode horizontalScrollMode READ horizontalScrollMode
+ WRITE setHorizontalScrollMode RESET resetHorizontalScrollMode)
public:
enum SelectionMode {
@@ -223,7 +190,12 @@ public:
void setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate);
QAbstractItemDelegate *itemDelegateForColumn(int column) const;
- QAbstractItemDelegate *itemDelegate(const QModelIndex &index) const;
+#if QT_DEPRECATED_SINCE(6, 0)
+ QT_DEPRECATED_VERSION_X_6_0("Use itemDelegateForIndex instead")
+ QAbstractItemDelegate *itemDelegate(const QModelIndex &index) const
+ { return itemDelegateForIndex(index); }
+#endif
+ virtual QAbstractItemDelegate *itemDelegateForIndex(const QModelIndex &index) const;
virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
@@ -242,7 +214,8 @@ public Q_SLOTS:
void update(const QModelIndex &index);
protected Q_SLOTS:
- virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
+ virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles = QList<int>());
virtual void rowsInserted(const QModelIndex &parent, int start, int end);
virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
@@ -272,13 +245,6 @@ Q_SIGNALS:
protected:
QAbstractItemView(QAbstractItemViewPrivate &, QWidget *parent = nullptr);
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED void setHorizontalStepsPerItem(int steps);
- QT_DEPRECATED int horizontalStepsPerItem() const;
- QT_DEPRECATED void setVerticalStepsPerItem(int steps);
- QT_DEPRECATED int verticalStepsPerItem() const;
-#endif
-
enum CursorAction { MoveUp, MoveDown, MoveLeft, MoveRight,
MoveHome, MoveEnd, MovePageUp, MovePageDown,
MoveNext, MovePrevious };
@@ -303,7 +269,7 @@ protected:
virtual void startDrag(Qt::DropActions supportedActions);
#endif
- virtual QStyleOptionViewItem viewOptions() const;
+ virtual void initViewItemOption(QStyleOptionViewItem *option) const;
enum State {
NoState,
@@ -360,19 +326,6 @@ protected:
private:
Q_DECLARE_PRIVATE(QAbstractItemView)
Q_DISABLE_COPY(QAbstractItemView)
- Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
- Q_PRIVATE_SLOT(d_func(), void _q_columnsRemoved(const QModelIndex&, int, int))
- Q_PRIVATE_SLOT(d_func(), void _q_columnsInserted(const QModelIndex&, int, int))
- Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex&, int, int))
- Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex&, int, int))
- Q_PRIVATE_SLOT(d_func(), void _q_columnsMoved(const QModelIndex&, int, int, const QModelIndex&, int))
- Q_PRIVATE_SLOT(d_func(), void _q_rowsMoved(const QModelIndex&, int, int, const QModelIndex&, int))
- Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
- Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged())
- Q_PRIVATE_SLOT(d_func(), void _q_headerDataChanged())
-#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
- Q_PRIVATE_SLOT(d_func(), void _q_scrollerStateChanged())
-#endif
friend class ::tst_QAbstractItemView;
friend class ::tst_QTreeView;
diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h
index fe1c00248f..433429f48b 100644
--- a/src/widgets/itemviews/qabstractitemview_p.h
+++ b/src/widgets/itemviews/qabstractitemview_p.h
@@ -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
#ifndef QABSTRACTITEMVIEW_P_H
#define QABSTRACTITEMVIEW_P_H
@@ -52,17 +16,22 @@
//
#include <QtWidgets/private/qtwidgetsglobal_p.h>
+#include "qabstractitemview.h"
#include "private/qabstractscrollarea_p.h"
#include "private/qabstractitemmodel_p.h"
#include "QtWidgets/qapplication.h"
#include "QtGui/qevent.h"
#include "QtCore/qmimedata.h"
#include "QtGui/qpainter.h"
-#include "QtCore/qpair.h"
#include "QtGui/qregion.h"
+
#include "QtCore/qdebug.h"
#include "QtCore/qbasictimer.h"
#include "QtCore/qelapsedtimer.h"
+#include <QtCore/qpointer.h>
+
+
+#include <array>
QT_REQUIRE_CONFIG(itemviews);
@@ -87,7 +56,7 @@ struct QItemViewPaintPair {
template <>
class QTypeInfo<QItemViewPaintPair> : public QTypeInfoMerger<QItemViewPaintPair, QRect, QModelIndex> {};
-typedef QVector<QItemViewPaintPair> QItemViewPaintPairs;
+typedef QList<QItemViewPaintPair> QItemViewPaintPairs;
class Q_AUTOTEST_EXPORT QAbstractItemViewPrivate : public QAbstractScrollAreaPrivate
{
@@ -99,18 +68,20 @@ public:
void init();
- virtual void _q_rowsRemoved(const QModelIndex &parent, int start, int end);
- virtual void _q_rowsInserted(const QModelIndex &parent, int start, int end);
- virtual void _q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
- virtual void _q_columnsRemoved(const QModelIndex &parent, int start, int end);
- virtual void _q_columnsInserted(const QModelIndex &parent, int start, int end);
- virtual void _q_modelDestroyed();
- virtual void _q_layoutChanged();
- virtual void _q_rowsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
- virtual void _q_columnsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
-
- void _q_headerDataChanged() { doDelayedItemsLayout(); }
- void _q_scrollerStateChanged();
+ virtual void rowsRemoved(const QModelIndex &parent, int start, int end);
+ virtual void rowsInserted(const QModelIndex &parent, int start, int end);
+ virtual void columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
+ virtual void columnsRemoved(const QModelIndex &parent, int start, int end);
+ virtual void columnsInserted(const QModelIndex &parent, int start, int end);
+ virtual void modelDestroyed();
+ virtual void layoutChanged();
+ virtual void rowsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
+ virtual void columnsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
+ virtual QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const;
+
+ void headerDataChanged() { doDelayedItemsLayout(); }
+ void scrollerStateChanged();
+ void delegateSizeHintChanged(const QModelIndex &index);
void fetchMore();
@@ -139,6 +110,7 @@ public:
bool sendDelegateEvent(const QModelIndex &index, QEvent *event) const;
bool openEditor(const QModelIndex &index, QEvent *event);
void updateEditorData(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+ void selectAllInEditor(QWidget *w);
QItemSelectionModel::SelectionFlags multiSelectionCommand(const QModelIndex &index,
const QEvent *event) const;
@@ -197,7 +169,7 @@ public:
#endif
) {
QStyleOption opt;
- opt.init(q_func());
+ opt.initFrom(q_func());
opt.rect = dropIndicatorRect;
q_func()->style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemDrop, &opt, painter, q_func());
}
@@ -210,11 +182,12 @@ public:
inline void releaseEditor(QWidget *editor, const QModelIndex &index = QModelIndex()) const {
if (editor) {
- QObject::disconnect(editor, SIGNAL(destroyed(QObject*)),
- q_func(), SLOT(editorDestroyed(QObject*)));
+ Q_Q(const QAbstractItemView);
+ QObject::disconnect(editor, &QWidget::destroyed,
+ q, &QAbstractItemView::editorDestroyed);
editor->removeEventFilter(itemDelegate);
editor->hide();
- QAbstractItemDelegate *delegate = delegateForIndex(index);
+ QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
if (delegate)
delegate->destroyEditor(editor, index);
@@ -275,20 +248,6 @@ public:
return state == QAbstractItemView::AnimatingState;
}
- inline QAbstractItemDelegate *delegateForIndex(const QModelIndex &index) const {
- QMap<int, QPointer<QAbstractItemDelegate> >::ConstIterator it;
-
- it = rowDelegates.find(index.row());
- if (it != rowDelegates.end())
- return it.value();
-
- it = columnDelegates.find(index.column());
- if (it != columnDelegates.end())
- return it.value();
-
- return itemDelegate;
- }
-
inline bool isIndexValid(const QModelIndex &index) const {
return (index.row() >= 0) && (index.column() >= 0) && (index.model() == model);
}
@@ -298,12 +257,14 @@ public:
inline bool isIndexEnabled(const QModelIndex &index) const {
return (model->flags(index) & Qt::ItemIsEnabled);
}
+#if QT_CONFIG(draganddrop)
inline bool isIndexDropEnabled(const QModelIndex &index) const {
return (model->flags(index) & Qt::ItemIsDropEnabled);
}
inline bool isIndexDragEnabled(const QModelIndex &index) const {
return (model->flags(index) & Qt::ItemIsDragEnabled);
}
+#endif
virtual bool selectionAllowed(const QModelIndex &index) const {
// in some views we want to go ahead with selections, even if the index is invalid
@@ -350,15 +311,16 @@ public:
return static_cast<QAbstractItemModelPrivate *>(model->d_ptr.data())->persistent.indexes.contains(index);
}
+#if QT_CONFIG(draganddrop)
QModelIndexList selectedDraggableIndexes() const;
-
- QStyleOptionViewItem viewOptionsV1() const;
+ void maybeStartDrag(QPoint eventPoint);
+#endif
void doDelayedReset()
{
//we delay the reset of the timer because some views (QTableView)
//with headers can't handle the fact that the model has been destroyed
- //all _q_modelDestroyed slots must have been called
+ //all modelDestroyed() slots must have been called
if (!delayedReset.isActive())
delayedReset.start(0, q_func());
}
@@ -378,13 +340,19 @@ public:
QIndexEditorHash indexEditorHash;
QSet<QWidget*> persistent;
QWidget *currentlyCommittingEditor;
+ QBasicTimer pressClosedEditorWatcher;
+ QPersistentModelIndex lastEditedIndex;
+ bool pressClosedEditor;
+ bool waitForIMCommit;
QPersistentModelIndex enteredIndex;
QPersistentModelIndex pressedIndex;
QPersistentModelIndex currentSelectionStartIndex;
Qt::KeyboardModifiers pressedModifiers;
QPoint pressedPosition;
+ QPoint draggedPosition;
bool pressedAlreadySelected;
+ bool releaseFromDoubleClick;
//forces the next mouseMoveEvent to send the viewportEntered signal
//if the mouse is over the viewport and not over an item
@@ -406,6 +374,7 @@ public:
bool dragEnabled;
QAbstractItemView::DragDropMode dragDropMode;
bool overwrite;
+ bool dropEventMoved;
QAbstractItemView::DropIndicatorPosition dropIndicatorPosition;
Qt::DropAction defaultDropAction;
#endif
@@ -452,17 +421,42 @@ public:
bool verticalScrollModeSet;
bool horizontalScrollModeSet;
+ virtual QRect visualRect(const QModelIndex &index) const { return q_func()->visualRect(index); }
+
+ std::array<QMetaObject::Connection, 14> modelConnections;
+ std::array<QMetaObject::Connection, 4> scrollbarConnections;
+#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
+ QMetaObject::Connection scollerConnection;
+#endif
+
private:
+ void connectDelegate(QAbstractItemDelegate *delegate);
+ void disconnectDelegate(QAbstractItemDelegate *delegate);
+ void disconnectAll();
+ inline QAbstractItemDelegate *delegateForIndex(const QModelIndex &index) const {
+ QMap<int, QPointer<QAbstractItemDelegate> >::ConstIterator it;
+
+ it = rowDelegates.find(index.row());
+ if (it != rowDelegates.end())
+ return it.value();
+
+ it = columnDelegates.find(index.column());
+ if (it != columnDelegates.end())
+ return it.value();
+
+ return itemDelegate;
+ }
+
mutable QBasicTimer delayedLayout;
mutable QBasicTimer fetchMoreTimer;
};
QT_BEGIN_INCLUDE_NAMESPACE
-#include <qvector.h>
+#include <qlist.h>
QT_END_INCLUDE_NAMESPACE
-template <typename T>
-inline int qBinarySearch(const QVector<T> &vec, const T &item, int start, int end)
+template<typename T>
+inline int qBinarySearch(const QList<T> &vec, const T &item, int start, int end)
{
int i = (start + end + 1) >> 1;
while (end - start > 0) {
diff --git a/src/widgets/itemviews/qbsptree.cpp b/src/widgets/itemviews/qbsptree.cpp
index 0e896317d0..d75144b1a8 100644
--- a/src/widgets/itemviews/qbsptree.cpp
+++ b/src/widgets/itemviews/qbsptree.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 "qbsptree_p.h"
@@ -56,8 +20,8 @@ void QBspTree::create(int n, int d)
}
depth = qMax(depth, uint(1));
- nodes.resize((1 << depth) - 1); // resize to number of nodes
- leaves.resize(1 << depth); // resize to number of leaves
+ nodes.resize((1ll << depth) - 1); // resize to number of nodes
+ leaves.resize(1ll << depth); // resize to number of leaves
}
void QBspTree::destroy()
@@ -76,9 +40,9 @@ void QBspTree::climbTree(const QRect &rect, callback *function, QBspTreeData dat
void QBspTree::climbTree(const QRect &area, callback *function, QBspTreeData data, int index)
{
- if (index >= nodes.count()) { // the index points to a leaf
+ if (index >= nodes.size()) { // the index points to a leaf
Q_ASSERT(!nodes.isEmpty());
- function(leaf(index - nodes.count()), area, visited, data);
+ function(leaf(index - nodes.size()), area, visited, data);
return;
}
@@ -128,12 +92,12 @@ void QBspTree::init(const QRect &area, int depth, NodeType type, int index)
}
}
-void QBspTree::insert(QVector<int> &leaf, const QRect &, uint, QBspTreeData data)
+void QBspTree::insert(QList<int> &leaf, const QRect &, uint, QBspTreeData data)
{
leaf.append(data.i);
}
-void QBspTree::remove(QVector<int> &leaf, const QRect &, uint, QBspTreeData data)
+void QBspTree::remove(QList<int> &leaf, const QRect &, uint, QBspTreeData data)
{
int i = leaf.indexOf(data.i);
if (i != -1)
diff --git a/src/widgets/itemviews/qbsptree_p.h b/src/widgets/itemviews/qbsptree_p.h
index 5fd7a5c45f..1f00616e87 100644
--- a/src/widgets/itemviews/qbsptree_p.h
+++ b/src/widgets/itemviews/qbsptree_p.h
@@ -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
#ifndef QBSPTREE_P_H
#define QBSPTREE_P_H
@@ -52,7 +16,7 @@
//
#include <QtWidgets/private/qtwidgetsglobal_p.h>
-#include <qvector.h>
+#include <qlist.h>
#include <qrect.h>
QT_BEGIN_NAMESPACE
@@ -80,7 +44,7 @@ public:
};
};
typedef QBspTree::Data QBspTreeData;
- typedef void callback(QVector<int> &leaf, const QRect &area, uint visited, QBspTreeData data);
+ typedef void callback(QList<int> &leaf, const QRect &area, uint visited, QBspTreeData data);
QBspTree();
@@ -91,8 +55,8 @@ public:
void climbTree(const QRect &rect, callback *function, QBspTreeData data);
- inline int leafCount() const { return leaves.count(); }
- inline QVector<int> &leaf(int i) { return leaves[i]; }
+ inline int leafCount() const { return leaves.size(); }
+ inline QList<int> &leaf(int i) { return leaves[i]; }
inline void insertLeaf(const QRect &r, int i) { climbTree(r, &insert, i, 0); }
inline void removeLeaf(const QRect &r, int i) { climbTree(r, &remove, i, 0); }
@@ -103,14 +67,14 @@ protected:
inline int parentIndex(int i) const { return (i & 1) ? ((i - 1) / 2) : ((i - 2) / 2); }
inline int firstChildIndex(int i) const { return ((i * 2) + 1); }
- static void insert(QVector<int> &leaf, const QRect &area, uint visited, QBspTreeData data);
- static void remove(QVector<int> &leaf, const QRect &area, uint visited, QBspTreeData data);
+ static void insert(QList<int> &leaf, const QRect &area, uint visited, QBspTreeData data);
+ static void remove(QList<int> &leaf, const QRect &area, uint visited, QBspTreeData data);
private:
uint depth;
mutable uint visited;
- QVector<Node> nodes;
- mutable QVector< QVector<int> > leaves; // the leaves are just indices into the items
+ QList<Node> nodes;
+ mutable QList<QList<int>> leaves; // the leaves are just indices into the items
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qcolumnview.cpp b/src/widgets/itemviews/qcolumnview.cpp
index a4eed2d885..04bc8f5f88 100644
--- a/src/widgets/itemviews/qcolumnview.cpp
+++ b/src/widgets/itemviews/qcolumnview.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) 2020 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 <qglobal.h>
#include "qcolumnview.h"
@@ -104,7 +68,9 @@ void QColumnViewPrivate::initialize()
Q_Q(QColumnView);
q->setTextElideMode(Qt::ElideMiddle);
#if QT_CONFIG(animation)
- QObject::connect(&currentAnimation, SIGNAL(finished()), q, SLOT(_q_changeCurrentColumn()));
+ animationConnection =
+ QObjectPrivate::connect(&currentAnimation, &QPropertyAnimation::finished,
+ this, &QColumnViewPrivate::changeCurrentColumn);
currentAnimation.setTargetObject(hbar);
currentAnimation.setPropertyName("value");
currentAnimation.setEasingCurve(QEasingCurve::InOutQuad);
@@ -113,11 +79,26 @@ void QColumnViewPrivate::initialize()
q->setItemDelegate(new QColumnViewDelegate(q));
}
+void QColumnViewPrivate::clearConnections()
+{
+#if QT_CONFIG(animation)
+ QObject::disconnect(animationConnection);
+#endif
+ for (const QMetaObject::Connection &connection : gripConnections)
+ QObject::disconnect(connection);
+ const auto copy = viewConnections; // disconnectView modifies this container
+ for (auto it = copy.keyBegin(); it != copy.keyEnd(); ++it)
+ disconnectView(*it);
+}
+
+
/*!
Destroys the column view.
*/
QColumnView::~QColumnView()
{
+ Q_D(QColumnView);
+ d->clearConnections();
}
/*!
@@ -134,15 +115,18 @@ void QColumnView::setResizeGripsVisible(bool visible)
if (d->showResizeGrips == visible)
return;
d->showResizeGrips = visible;
- for (int i = 0; i < d->columns.count(); ++i) {
- QAbstractItemView *view = d->columns[i];
+ d->gripConnections.clear();
+ for (QAbstractItemView *view : std::as_const(d->columns)) {
if (visible) {
QColumnViewGrip *grip = new QColumnViewGrip(view);
view->setCornerWidget(grip);
- connect(grip, SIGNAL(gripMoved(int)), this, SLOT(_q_gripMoved(int)));
+ d->gripConnections.push_back(
+ QObjectPrivate::connect(grip, &QColumnViewGrip::gripMoved,
+ d, &QColumnViewPrivate::gripMoved)
+ );
} else {
QWidget *widget = view->cornerWidget();
- view->setCornerWidget(0);
+ view->setCornerWidget(nullptr);
widget->deleteLater();
}
}
@@ -176,7 +160,7 @@ void QColumnView::setRootIndex(const QModelIndex &index)
return;
d->closeColumns();
- Q_ASSERT(d->columns.count() == 0);
+ Q_ASSERT(d->columns.size() == 0);
QAbstractItemView *view = d->createColumn(index, true);
if (view->selectionModel())
@@ -242,7 +226,7 @@ void QColumnView::scrollContentsBy(int dx, int dy)
return;
dx = isRightToLeft() ? -dx : dx;
- for (int i = 0; i < d->columns.count(); ++i)
+ for (int i = 0; i < d->columns.size(); ++i)
d->columns.at(i)->move(d->columns.at(i)->x() + dx, 0);
d->offset += dx;
QAbstractItemView::scrollContentsBy(dx, dy);
@@ -303,7 +287,7 @@ void QColumnView::scrollTo(const QModelIndex &index, ScrollHint hint)
if (leftEdge > -horizontalOffset()
&& rightEdge <= ( -horizontalOffset() + viewport()->size().width())) {
d->columns.at(indexColumn)->scrollTo(index);
- d->_q_changeCurrentColumn();
+ d->changeCurrentColumn();
return;
}
@@ -327,7 +311,7 @@ void QColumnView::scrollTo(const QModelIndex &index, ScrollHint hint)
}
#if QT_CONFIG(animation)
- if (const int animationDuration = style()->styleHint(QStyle::SH_Widget_Animation_Duration, 0, this)) {
+ if (const int animationDuration = style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this)) {
d->currentAnimation.setDuration(animationDuration);
d->currentAnimation.setEndValue(newScrollbarValue);
d->currentAnimation.start();
@@ -456,7 +440,7 @@ int QColumnView::verticalOffset() const
*/
QRegion QColumnView::visualRegionForSelection(const QItemSelection &selection) const
{
- int ranges = selection.count();
+ int ranges = selection.size();
if (ranges == 0)
return QRect();
@@ -522,7 +506,7 @@ QSize QColumnView::sizeHint() const
\internal
Move all widgets from the corner grip and to the right
*/
-void QColumnViewPrivate::_q_gripMoved(int offset)
+void QColumnViewPrivate::gripMoved(int offset)
{
Q_Q(QColumnView);
@@ -566,7 +550,7 @@ void QColumnViewPrivate::closeColumns(const QModelIndex &parent, bool build)
bool clearAll = !parent.isValid();
bool passThroughRoot = false;
- QVector<QModelIndex> dirsToAppend;
+ QList<QModelIndex> dirsToAppend;
// Find the last column that matches the parent's tree
int currentColumn = -1;
@@ -614,8 +598,10 @@ void QColumnViewPrivate::closeColumns(const QModelIndex &parent, bool build)
QAbstractItemView* notShownAnymore = columns.at(i);
columns.removeAt(i);
notShownAnymore->setVisible(false);
- if (notShownAnymore != previewColumn)
+ if (notShownAnymore != previewColumn) {
notShownAnymore->deleteLater();
+ disconnectView(notShownAnymore);
+ }
}
if (columns.isEmpty()) {
@@ -634,12 +620,22 @@ void QColumnViewPrivate::closeColumns(const QModelIndex &parent, bool build)
createColumn(parent, false);
}
-void QColumnViewPrivate::_q_clicked(const QModelIndex &index)
+void QColumnViewPrivate::disconnectView(QAbstractItemView *view)
+{
+ const auto it = viewConnections.find(view);
+ if (it == viewConnections.end())
+ return;
+ for (const QMetaObject::Connection &connection : it.value())
+ QObject::disconnect(connection);
+ viewConnections.erase(it);
+}
+
+void QColumnViewPrivate::clicked(const QModelIndex &index)
{
Q_Q(QColumnView);
QModelIndex parent = index.parent();
- QAbstractItemView *columnClicked = 0;
- for (int column = 0; column < columns.count(); ++column) {
+ QAbstractItemView *columnClicked = nullptr;
+ for (int column = 0; column < columns.size(); ++column) {
if (columns.at(column)->rootIndex() == parent) {
columnClicked = columns[column];
break;
@@ -666,11 +662,12 @@ void QColumnViewPrivate::_q_clicked(const QModelIndex &index)
QAbstractItemView *QColumnViewPrivate::createColumn(const QModelIndex &index, bool show)
{
Q_Q(QColumnView);
- QAbstractItemView *view = 0;
+ QAbstractItemView *view = nullptr;
+ QMetaObject::Connection clickedConnection;
if (model->hasChildren(index)) {
view = q->createColumn(index);
- q->connect(view, SIGNAL(clicked(QModelIndex)),
- q, SLOT(_q_clicked(QModelIndex)));
+ clickedConnection = QObjectPrivate::connect(view, &QAbstractItemView::clicked,
+ this, &QColumnViewPrivate::clicked);
} else {
if (!previewColumn)
setPreviewWidget(new QWidget(q));
@@ -678,16 +675,14 @@ QAbstractItemView *QColumnViewPrivate::createColumn(const QModelIndex &index, bo
view->setMinimumWidth(qMax(view->minimumWidth(), previewWidget->minimumWidth()));
}
- q->connect(view, SIGNAL(activated(QModelIndex)),
- q, SIGNAL(activated(QModelIndex)));
- q->connect(view, SIGNAL(clicked(QModelIndex)),
- q, SIGNAL(clicked(QModelIndex)));
- q->connect(view, SIGNAL(doubleClicked(QModelIndex)),
- q, SIGNAL(doubleClicked(QModelIndex)));
- q->connect(view, SIGNAL(entered(QModelIndex)),
- q, SIGNAL(entered(QModelIndex)));
- q->connect(view, SIGNAL(pressed(QModelIndex)),
- q, SIGNAL(pressed(QModelIndex)));
+ viewConnections[view] = {
+ QObject::connect(view, &QAbstractItemView::activated, q, &QColumnView::activated),
+ QObject::connect(view, &QAbstractItemView::clicked, q, &QColumnView::clicked),
+ QObject::connect(view, &QAbstractItemView::doubleClicked, q, &QColumnView::doubleClicked),
+ QObject::connect(view, &QAbstractItemView::entered, q, &QColumnView::entered),
+ QObject::connect(view, &QAbstractItemView::pressed, q, &QColumnView::pressed),
+ clickedConnection
+ };
view->setFocusPolicy(Qt::NoFocus);
view->setParent(viewport);
@@ -697,19 +692,22 @@ QAbstractItemView *QColumnViewPrivate::createColumn(const QModelIndex &index, bo
if (showResizeGrips) {
QColumnViewGrip *grip = new QColumnViewGrip(view);
view->setCornerWidget(grip);
- q->connect(grip, SIGNAL(gripMoved(int)), q, SLOT(_q_gripMoved(int)));
+ gripConnections.push_back(
+ QObjectPrivate::connect(grip, &QColumnViewGrip::gripMoved,
+ this, &QColumnViewPrivate::gripMoved)
+ );
}
- if (columnSizes.count() > columns.count()) {
- view->setGeometry(0, 0, columnSizes.at(columns.count()), viewport->height());
+ if (columnSizes.size() > columns.size()) {
+ view->setGeometry(0, 0, columnSizes.at(columns.size()), viewport->height());
} else {
int initialWidth = view->sizeHint().width();
if (q->isRightToLeft())
view->setGeometry(viewport->width() - initialWidth, 0, initialWidth, viewport->height());
else
view->setGeometry(0, 0, initialWidth, viewport->height());
- columnSizes.resize(qMax(columnSizes.count(), columns.count() + 1));
- columnSizes[columns.count()] = initialWidth;
+ columnSizes.resize(qMax(columnSizes.size(), columns.size() + 1));
+ columnSizes[columns.size()] = initialWidth;
}
if (!columns.isEmpty() && columns.constLast()->isHidden())
columns.constLast()->setVisible(true);
@@ -862,8 +860,8 @@ void QColumnView::setColumnWidths(const QList<int> &list)
{
Q_D(QColumnView);
int i = 0;
- const int listCount = list.count();
- const int count = qMin(listCount, d->columns.count());
+ const int listCount = list.size();
+ const int count = qMin(listCount, d->columns.size());
for (; i < count; ++i) {
d->columns.at(i)->resize(list.at(i), d->columns.at(i)->height());
d->columnSizes[i] = list.at(i);
@@ -883,7 +881,7 @@ QList<int> QColumnView::columnWidths() const
{
Q_D(const QColumnView);
QList<int> list;
- const int columnCount = d->columns.count();
+ const int columnCount = d->columns.size();
list.reserve(columnCount);
for (int i = 0; i < columnCount; ++i)
list.append(d->columnSizes.at(i));
@@ -951,7 +949,7 @@ void QColumnView::currentChanged(const QModelIndex &current, const QModelIndex &
We have change the current column and need to update focus and selection models
on the new current column.
*/
-void QColumnViewPrivate::_q_changeCurrentColumn()
+void QColumnViewPrivate::changeCurrentColumn()
{
Q_Q(QColumnView);
if (columns.isEmpty())
@@ -1020,9 +1018,9 @@ void QColumnView::selectAll()
QModelIndexList indexList = selectionModel()->selectedIndexes();
QModelIndex parent = rootIndex();
QItemSelection selection;
- if (indexList.count() >= 1)
+ if (indexList.size() >= 1)
parent = indexList.at(0).parent();
- if (indexList.count() == 1) {
+ if (indexList.size() == 1) {
parent = indexList.at(0);
if (!model()->hasChildren(parent))
parent = parent.parent();
@@ -1045,8 +1043,8 @@ QColumnViewPrivate::QColumnViewPrivate()
: QAbstractItemViewPrivate()
,showResizeGrips(true)
,offset(0)
-,previewWidget(0)
-,previewColumn(0)
+,previewWidget(nullptr)
+,previewColumn(nullptr)
{
}
@@ -1058,9 +1056,9 @@ QColumnViewPrivate::~QColumnViewPrivate()
\internal
*/
-void QColumnViewPrivate::_q_columnsInserted(const QModelIndex &parent, int start, int end)
+void QColumnViewPrivate::columnsInserted(const QModelIndex &parent, int start, int end)
{
- QAbstractItemViewPrivate::_q_columnsInserted(parent, start, end);
+ QAbstractItemViewPrivate::columnsInserted(parent, start, end);
checkColumnCreation(parent);
}
@@ -1075,7 +1073,7 @@ void QColumnViewPrivate::checkColumnCreation(const QModelIndex &parent)
if (parent == q_func()->currentIndex() && model->hasChildren(parent)) {
//the parent has children and is the current
//let's try to find out if there is already a mapping that is good
- for (int i = 0; i < columns.count(); ++i) {
+ for (int i = 0; i < columns.size(); ++i) {
QAbstractItemView *view = columns.at(i);
if (view->rootIndex() == parent) {
if (view == previewColumn) {
@@ -1126,14 +1124,12 @@ void QColumnViewPrivate::doLayout()
Draws a delegate with a > if an object has children.
- \sa {Model/View Programming}, QItemDelegate
+ \sa {Model/View Programming}, QStyledItemDelegate
*/
void QColumnViewDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
- drawBackground(painter, option, index );
-
bool reverse = (option.direction == Qt::RightToLeft);
int width = ((option.rect.height() * 2) / 3);
// Modify the options to give us room to add an arrow
@@ -1148,7 +1144,7 @@ void QColumnViewDelegate::paint(QPainter *painter,
opt.state |= QStyle::State_Selected;
}
- QItemDelegate::paint(painter, opt, index);
+ QStyledItemDelegate::paint(painter, opt, index);
if (reverse)
opt.rect = QRect(option.rect.x(), option.rect.y(), width, option.rect.height());
diff --git a/src/widgets/itemviews/qcolumnview.h b/src/widgets/itemviews/qcolumnview.h
index 5c62f9c9af..c0c1398692 100644
--- a/src/widgets/itemviews/qcolumnview.h
+++ b/src/widgets/itemviews/qcolumnview.h
@@ -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
#ifndef QCOLUMNVIEW_H
#define QCOLUMNVIEW_H
@@ -103,9 +67,6 @@ protected:
private:
Q_DECLARE_PRIVATE(QColumnView)
Q_DISABLE_COPY(QColumnView)
- Q_PRIVATE_SLOT(d_func(), void _q_gripMoved(int))
- Q_PRIVATE_SLOT(d_func(), void _q_changeCurrentColumn())
- Q_PRIVATE_SLOT(d_func(), void _q_clicked(const QModelIndex &))
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qcolumnview_p.h b/src/widgets/itemviews/qcolumnview_p.h
index c60579255e..f9b2f3baa4 100644
--- a/src/widgets/itemviews/qcolumnview_p.h
+++ b/src/widgets/itemviews/qcolumnview_p.h
@@ -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) 2020 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
#ifndef QCOLUMNVIEW_P_H
#define QCOLUMNVIEW_P_H
@@ -62,11 +26,13 @@
#endif
#include <QtWidgets/qabstractitemdelegate.h>
#include <QtWidgets/qabstractitemview.h>
-#include <QtWidgets/qitemdelegate.h>
+#include <QtWidgets/qstyleditemdelegate.h>
#include <qlistview.h>
#include <qevent.h>
#include <qscrollbar.h>
+#include <vector>
+
QT_REQUIRE_CONFIG(columnview);
QT_BEGIN_NAMESPACE
@@ -152,28 +118,35 @@ public:
QColumnViewPrivate();
~QColumnViewPrivate();
void initialize();
+ void clearConnections();
QAbstractItemView *createColumn(const QModelIndex &index, bool show);
void updateScrollbars();
void closeColumns(const QModelIndex &parent = QModelIndex(), bool build = false);
+ void disconnectView(QAbstractItemView *view);
void doLayout();
void setPreviewWidget(QWidget *widget);
void checkColumnCreation(const QModelIndex &parent);
- void _q_gripMoved(int offset);
- void _q_changeCurrentColumn();
- void _q_clicked(const QModelIndex &index);
- void _q_columnsInserted(const QModelIndex &parent, int start, int end) override;
+ void gripMoved(int offset);
+ void changeCurrentColumn();
+ void clicked(const QModelIndex &index);
+ void columnsInserted(const QModelIndex &parent, int start, int end) override;
QList<QAbstractItemView*> columns;
- QVector<int> columnSizes; // used during init and corner moving
+ QList<int> columnSizes; // used during init and corner moving
bool showResizeGrips;
int offset;
#if QT_CONFIG(animation)
QPropertyAnimation currentAnimation;
+ QMetaObject::Connection animationConnection;
#endif
+ std::vector<QMetaObject::Connection> gripConnections;
+ using ViewConnections = std::vector<QMetaObject::Connection>;
+ QHash<QAbstractItemView *, ViewConnections> viewConnections;
+
QWidget *previewWidget;
QAbstractItemView *previewColumn;
};
@@ -181,12 +154,11 @@ public:
/*!
* This is a delegate that will paint the triangle
*/
-// ### Qt6: QStyledItemDelegate
-class QColumnViewDelegate : public QItemDelegate
+class QColumnViewDelegate : public QStyledItemDelegate
{
public:
- explicit QColumnViewDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
+ explicit QColumnViewDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
~QColumnViewDelegate() {}
void paint(QPainter *painter,
diff --git a/src/widgets/itemviews/qcolumnviewgrip.cpp b/src/widgets/itemviews/qcolumnviewgrip.cpp
index 00cd7df20a..22becae872 100644
--- a/src/widgets/itemviews/qcolumnviewgrip.cpp
+++ b/src/widgets/itemviews/qcolumnviewgrip.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 "qcolumnviewgrip_p.h"
#include <qstyleoption.h>
@@ -149,7 +113,7 @@ void QColumnViewGrip::mouseDoubleClickEvent(QMouseEvent *event)
void QColumnViewGrip::mousePressEvent(QMouseEvent *event)
{
Q_D(QColumnViewGrip);
- d->originalXLocation = event->globalX();
+ d->originalXLocation = event->globalPosition().toPoint().x();
event->accept();
}
@@ -160,7 +124,7 @@ void QColumnViewGrip::mousePressEvent(QMouseEvent *event)
void QColumnViewGrip::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QColumnViewGrip);
- int offset = event->globalX() - d->originalXLocation;
+ int offset = event->globalPosition().toPoint().x() - d->originalXLocation;
d->originalXLocation = moveGrip(offset) + d->originalXLocation;
event->accept();
}
diff --git a/src/widgets/itemviews/qcolumnviewgrip_p.h b/src/widgets/itemviews/qcolumnviewgrip_p.h
index 13e4c764e8..30e8ce0f79 100644
--- a/src/widgets/itemviews/qcolumnviewgrip_p.h
+++ b/src/widgets/itemviews/qcolumnviewgrip_p.h
@@ -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
#ifndef QCOLUMNVIEWGRIP_P_H
#define QCOLUMNVIEWGRIP_P_H
diff --git a/src/widgets/itemviews/qdatawidgetmapper.cpp b/src/widgets/itemviews/qdatawidgetmapper.cpp
index 125ee73194..3b7e97eed9 100644
--- a/src/widgets/itemviews/qdatawidgetmapper.cpp
+++ b/src/widgets/itemviews/qdatawidgetmapper.cpp
@@ -1,51 +1,18 @@
-/****************************************************************************
-**
-** 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 "qdatawidgetmapper.h"
#include "qabstractitemmodel.h"
-#include "qitemdelegate.h"
#include "qmetaobject.h"
#include "qwidget.h"
+#include "qstyleditemdelegate.h"
+
#include "private/qobject_p.h"
#include "private/qabstractitemmodel_p.h"
+#include <QtCore/qpointer.h>
+#include <array>
#include <iterator>
QT_BEGIN_NAMESPACE
@@ -56,7 +23,7 @@ public:
Q_DECLARE_PUBLIC(QDataWidgetMapper)
QDataWidgetMapperPrivate()
- : model(QAbstractItemModelPrivate::staticEmptyModel()), delegate(0),
+ : model(QAbstractItemModelPrivate::staticEmptyModel()), delegate(nullptr),
orientation(Qt::Horizontal), submitPolicy(QDataWidgetMapper::AutoSubmit)
{
}
@@ -102,10 +69,22 @@ public:
void populate();
// private slots
- void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &);
- void _q_commitData(QWidget *);
- void _q_closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint);
- void _q_modelDestroyed();
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &);
+ void commitData(QWidget *);
+ void closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint);
+ void modelDestroyed();
+
+ void disconnectModel()
+ {
+ for (const QMetaObject::Connection &connection : modelConnections)
+ QObject::disconnect(connection);
+ }
+ void disconnectDelegate()
+ {
+ for (const QMetaObject::Connection &connection : delegateConnections)
+ QObject::disconnect(connection);
+ }
struct WidgetMapper
{
@@ -121,8 +100,10 @@ public:
bool commit(const WidgetMapper &m);
std::vector<WidgetMapper> widgetMap;
+ std::array<QMetaObject::Connection, 2> modelConnections;
+ std::array<QMetaObject::Connection, 2> delegateConnections;
};
-Q_DECLARE_TYPEINFO(QDataWidgetMapperPrivate::WidgetMapper, Q_MOVABLE_TYPE);
+Q_DECLARE_TYPEINFO(QDataWidgetMapperPrivate::WidgetMapper, Q_RELOCATABLE_TYPE);
int QDataWidgetMapperPrivate::findWidget(QWidget *w) const
{
@@ -176,7 +157,8 @@ static bool qContainsIndex(const QModelIndex &idx, const QModelIndex &topLeft,
&& idx.column() >= topLeft.column() && idx.column() <= bottomRight.column();
}
-void QDataWidgetMapperPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &)
+void QDataWidgetMapperPrivate::dataChanged(const QModelIndex &topLeft,
+ const QModelIndex &bottomRight, const QList<int> &)
{
if (topLeft.parent() != rootIndex)
return; // not in our hierarchy
@@ -187,7 +169,7 @@ void QDataWidgetMapperPrivate::_q_dataChanged(const QModelIndex &topLeft, const
}
}
-void QDataWidgetMapperPrivate::_q_commitData(QWidget *w)
+void QDataWidgetMapperPrivate::commitData(QWidget *w)
{
if (submitPolicy == QDataWidgetMapper::ManualSubmit)
return;
@@ -199,7 +181,7 @@ void QDataWidgetMapperPrivate::_q_commitData(QWidget *w)
commit(widgetMap[idx]);
}
-void QDataWidgetMapperPrivate::_q_closeEditor(QWidget *w, QAbstractItemDelegate::EndEditHint hint)
+void QDataWidgetMapperPrivate::closeEditor(QWidget *w, QAbstractItemDelegate::EndEditHint hint)
{
int idx = findWidget(w);
if (idx == -1)
@@ -222,11 +204,11 @@ void QDataWidgetMapperPrivate::_q_closeEditor(QWidget *w, QAbstractItemDelegate:
}
}
-void QDataWidgetMapperPrivate::_q_modelDestroyed()
+void QDataWidgetMapperPrivate::modelDestroyed()
{
Q_Q(QDataWidgetMapper);
- model = 0;
+ model = nullptr;
q->setModel(QAbstractItemModelPrivate::staticEmptyModel());
}
@@ -253,7 +235,7 @@ void QDataWidgetMapperPrivate::_q_modelDestroyed()
instead of the default user property.
It is possible to set an item delegate to support custom widgets. By default,
- a QItemDelegate is used to synchronize the model with the widgets.
+ a QStyledItemDelegate is used to synchronize the model with the widgets.
Let us assume that we have an item model named \c{model} with the following contents:
@@ -324,8 +306,7 @@ void QDataWidgetMapperPrivate::_q_modelDestroyed()
QDataWidgetMapper::QDataWidgetMapper(QObject *parent)
: QObject(*new QDataWidgetMapperPrivate, parent)
{
- // ### Qt6: QStyledItemDelegate
- setItemDelegate(new QItemDelegate(this));
+ setItemDelegate(new QStyledItemDelegate(this));
}
/*!
@@ -333,6 +314,9 @@ QDataWidgetMapper::QDataWidgetMapper(QObject *parent)
*/
QDataWidgetMapper::~QDataWidgetMapper()
{
+ Q_D(QDataWidgetMapper);
+ d->disconnectModel();
+ d->disconnectDelegate();
}
/*!
@@ -348,21 +332,19 @@ void QDataWidgetMapper::setModel(QAbstractItemModel *model)
if (d->model == model)
return;
- if (d->model) {
- disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this,
- SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
- disconnect(d->model, SIGNAL(destroyed()), this,
- SLOT(_q_modelDestroyed()));
- }
+ d->disconnectModel();
clearMapping();
d->rootIndex = QModelIndex();
d->currentTopLeft = QModelIndex();
d->model = model;
- connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
- SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
- connect(model, SIGNAL(destroyed()), SLOT(_q_modelDestroyed()));
+ d->modelConnections = {
+ QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged,
+ d, &QDataWidgetMapperPrivate::dataChanged),
+ QObjectPrivate::connect(model, &QAbstractItemModel::destroyed,
+ d, &QDataWidgetMapperPrivate::modelDestroyed)
+ };
}
/*!
@@ -374,7 +356,7 @@ QAbstractItemModel *QDataWidgetMapper::model() const
{
Q_D(const QDataWidgetMapper);
return d->model == QAbstractItemModelPrivate::staticEmptyModel()
- ? static_cast<QAbstractItemModel *>(0)
+ ? static_cast<QAbstractItemModel *>(nullptr)
: d->model;
}
@@ -383,6 +365,9 @@ QAbstractItemModel *QDataWidgetMapper::model() const
data from the model into the widget and from the widget to the model,
using QAbstractItemDelegate::setEditorData() and QAbstractItemDelegate::setModelData().
+ Any existing delegate will be removed, but not deleted. QDataWidgetMapper
+ does not take ownership of \a delegate.
+
The delegate also decides when to apply data and when to change the editor,
using QAbstractItemDelegate::commitData() and QAbstractItemDelegate::closeEditor().
@@ -395,18 +380,17 @@ void QDataWidgetMapper::setItemDelegate(QAbstractItemDelegate *delegate)
{
Q_D(QDataWidgetMapper);
QAbstractItemDelegate *oldDelegate = d->delegate;
- if (oldDelegate) {
- disconnect(oldDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(_q_commitData(QWidget*)));
- disconnect(oldDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- this, SLOT(_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
- }
+ d->disconnectDelegate();
d->delegate = delegate;
if (delegate) {
- connect(delegate, SIGNAL(commitData(QWidget*)), SLOT(_q_commitData(QWidget*)));
- connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
- SLOT(_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
+ d->delegateConnections = {
+ QObjectPrivate::connect(delegate, &QAbstractItemDelegate::commitData,
+ d, &QDataWidgetMapperPrivate::commitData),
+ QObjectPrivate::connect(delegate, &QAbstractItemDelegate::closeEditor,
+ d, &QDataWidgetMapperPrivate::closeEditor)
+ };
}
d->flipEventFilters(oldDelegate, delegate);
@@ -567,7 +551,7 @@ QWidget *QDataWidgetMapper::mappedWidgetAt(int section) const
return e.widget;
}
- return 0;
+ return nullptr;
}
/*!
diff --git a/src/widgets/itemviews/qdatawidgetmapper.h b/src/widgets/itemviews/qdatawidgetmapper.h
index 2d75b63a5e..6c7beff7fc 100644
--- a/src/widgets/itemviews/qdatawidgetmapper.h
+++ b/src/widgets/itemviews/qdatawidgetmapper.h
@@ -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
#ifndef QDATAWIDGETMAPPER_H
#define QDATAWIDGETMAPPER_H
@@ -108,10 +72,6 @@ Q_SIGNALS:
private:
Q_DECLARE_PRIVATE(QDataWidgetMapper)
Q_DISABLE_COPY(QDataWidgetMapper)
- Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &, const QVector<int> &))
- Q_PRIVATE_SLOT(d_func(), void _q_commitData(QWidget *))
- Q_PRIVATE_SLOT(d_func(), void _q_closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint))
- Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qdirmodel.cpp b/src/widgets/itemviews/qdirmodel.cpp
deleted file mode 100644
index c9e7c7b7a6..0000000000
--- a/src/widgets/itemviews/qdirmodel.cpp
+++ /dev/null
@@ -1,1378 +0,0 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
-
-#include "qdirmodel.h"
-
-#if QT_DEPRECATED_SINCE(5, 15)
-
-#include <qfile.h>
-#include <qfilesystemmodel.h>
-#include <qurl.h>
-#include <qmimedata.h>
-#include <qpair.h>
-#include <qvector.h>
-#include <qobject.h>
-#include <qdatetime.h>
-#include <qlocale.h>
-#include <qstyle.h>
-#include <qapplication.h>
-#include <private/qabstractitemmodel_p.h>
-#include <private/qfilesystementry_p.h>
-#include <qdebug.h>
-
-#include <stack>
-#include <vector>
-
-/*!
- \enum QDirModel::Roles
- \value FileIconRole
- \value FilePathRole
- \value FileNameRole
-*/
-
-QT_BEGIN_NAMESPACE
-
-class QDirModelPrivate : public QAbstractItemModelPrivate
-{
- Q_DECLARE_PUBLIC(QDirModel)
-
-public:
- struct QDirNode
- {
- QDirNode() : parent(0), populated(false), stat(false) {}
- QDirNode *parent;
- QFileInfo info;
- QIcon icon; // cache the icon
- mutable QVector<QDirNode> children;
- mutable bool populated; // have we read the children
- mutable bool stat;
- };
-
- QDirModelPrivate()
- : resolveSymlinks(true),
- readOnly(true),
- lazyChildCount(false),
- allowAppendChild(true),
- iconProvider(&defaultProvider),
- shouldStat(true) // ### This is set to false by QFileDialog
- { }
-
- void init();
- QDirNode *node(int row, QDirNode *parent) const;
- QVector<QDirNode> children(QDirNode *parent, bool stat) const;
-
- void _q_refresh();
-
- void savePersistentIndexes();
- void restorePersistentIndexes();
-
- QFileInfoList entryInfoList(const QString &path) const;
- QStringList entryList(const QString &path) const;
-
- QString name(const QModelIndex &index) const;
- QString size(const QModelIndex &index) const;
- QString type(const QModelIndex &index) const;
- QString time(const QModelIndex &index) const;
-
- void appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const;
- static QFileInfo resolvedInfo(QFileInfo info);
-
- inline QDirNode *node(const QModelIndex &index) const;
- inline void populate(QDirNode *parent) const;
- inline void clear(QDirNode *parent) const;
-
- void invalidate();
-
- mutable QDirNode root;
- bool resolveSymlinks;
- bool readOnly;
- bool lazyChildCount;
- bool allowAppendChild;
-
- QDir::Filters filters;
- QDir::SortFlags sort;
- QStringList nameFilters;
-
- QFileIconProvider *iconProvider;
- QFileIconProvider defaultProvider;
-
- struct SavedPersistent {
- QString path;
- int column;
- QPersistentModelIndexData *data;
- QPersistentModelIndex index;
- };
- QVector<SavedPersistent> savedPersistent;
- QPersistentModelIndex toBeRefreshed;
-
- bool shouldStat; // use the "carefull not to stat directories" mode
-};
-Q_DECLARE_TYPEINFO(QDirModelPrivate::SavedPersistent, Q_MOVABLE_TYPE);
-
-void qt_setDirModelShouldNotStat(QDirModelPrivate *modelPrivate)
-{
- modelPrivate->shouldStat = false;
-}
-
-QDirModelPrivate::QDirNode *QDirModelPrivate::node(const QModelIndex &index) const
-{
- QDirModelPrivate::QDirNode *n =
- static_cast<QDirModelPrivate::QDirNode*>(index.internalPointer());
- Q_ASSERT(n);
- return n;
-}
-
-void QDirModelPrivate::populate(QDirNode *parent) const
-{
- Q_ASSERT(parent);
- parent->children = children(parent, parent->stat);
- parent->populated = true;
-}
-
-void QDirModelPrivate::clear(QDirNode *parent) const
-{
- Q_ASSERT(parent);
- parent->children.clear();
- parent->populated = false;
-}
-
-void QDirModelPrivate::invalidate()
-{
- std::stack<const QDirNode*, std::vector<const QDirNode*> > nodes;
- nodes.push(&root);
- while (!nodes.empty()) {
- const QDirNode *current = nodes.top();
- nodes.pop();
- current->stat = false;
- const QVector<QDirNode> &children = current->children;
- for (const auto &child : children)
- nodes.push(&child);
- }
-}
-
-/*!
- \class QDirModel
- \obsolete
- \brief The QDirModel class provides a data model for the local filesystem.
-
- \ingroup model-view
- \inmodule QtWidgets
-
- The usage of QDirModel is not recommended anymore. The
- QFileSystemModel class is a more performant alternative.
-
- This class provides access to the local filesystem, providing functions
- for renaming and removing files and directories, and for creating new
- directories. In the simplest case, it can be used with a suitable display
- widget as part of a browser or filer.
-
- QDirModel keeps a cache with file information. The cache needs to be
- updated with refresh().
-
- QDirModel can be accessed using the standard interface provided by
- QAbstractItemModel, but it also provides some convenience functions
- that are specific to a directory model. The fileInfo() and isDir()
- functions provide information about the underlying files and directories
- related to items in the model.
-
- Directories can be created and removed using mkdir(), rmdir(), and the
- model will be automatically updated to take the changes into account.
-
- \note QDirModel requires an instance of \l QApplication.
-
- \sa nameFilters(), setFilter(), filter(), QListView, QTreeView, QFileSystemModel,
- {Dir View Example}, {Model Classes}
-*/
-
-/*!
- Constructs a new directory model with the given \a parent.
- Only those files matching the \a nameFilters and the
- \a filters are included in the model. The sort order is given by the
- \a sort flags.
-*/
-
-QDirModel::QDirModel(const QStringList &nameFilters,
- QDir::Filters filters,
- QDir::SortFlags sort,
- QObject *parent)
- : QAbstractItemModel(*new QDirModelPrivate, parent)
-{
- Q_D(QDirModel);
- // we always start with QDir::drives()
- d->nameFilters = nameFilters.isEmpty() ? QStringList(QLatin1String("*")) : nameFilters;
- d->filters = filters;
- d->sort = sort;
- d->root.parent = 0;
- d->root.info = QFileInfo();
- d->clear(&d->root);
-}
-
-/*!
- Constructs a directory model with the given \a parent.
-*/
-
-QDirModel::QDirModel(QObject *parent)
- : QAbstractItemModel(*new QDirModelPrivate, parent)
-{
- Q_D(QDirModel);
- d->init();
-}
-
-/*!
- \internal
-*/
-QDirModel::QDirModel(QDirModelPrivate &dd, QObject *parent)
- : QAbstractItemModel(dd, parent)
-{
- Q_D(QDirModel);
- d->init();
-}
-
-/*!
- Destroys this directory model.
-*/
-
-QDirModel::~QDirModel()
-{
-
-}
-
-/*!
- Returns the model item index for the item in the \a parent with the
- given \a row and \a column.
-
-*/
-
-QModelIndex QDirModel::index(int row, int column, const QModelIndex &parent) const
-{
- Q_D(const QDirModel);
- // note that rowCount does lazy population
- if (column < 0 || column >= columnCount(parent) || row < 0 || parent.column() > 0)
- return QModelIndex();
- // make sure the list of children is up to date
- QDirModelPrivate::QDirNode *p = (d->indexValid(parent) ? d->node(parent) : &d->root);
- Q_ASSERT(p);
- if (!p->populated)
- d->populate(p); // populate without stat'ing
- if (row >= p->children.count())
- return QModelIndex();
- // now get the internal pointer for the index
- QDirModelPrivate::QDirNode *n = d->node(row, d->indexValid(parent) ? p : 0);
- Q_ASSERT(n);
-
- return createIndex(row, column, n);
-}
-
-/*!
- Return the parent of the given \a child model item.
-*/
-
-QModelIndex QDirModel::parent(const QModelIndex &child) const
-{
- Q_D(const QDirModel);
-
- if (!d->indexValid(child))
- return QModelIndex();
- QDirModelPrivate::QDirNode *node = d->node(child);
- QDirModelPrivate::QDirNode *par = (node ? node->parent : 0);
- if (par == 0) // parent is the root node
- return QModelIndex();
-
- // get the parent's row
- const QVector<QDirModelPrivate::QDirNode> children =
- par->parent ? par->parent->children : d->root.children;
- Q_ASSERT(children.count() > 0);
- int row = (par - &(children.at(0)));
- Q_ASSERT(row >= 0);
-
- return createIndex(row, 0, par);
-}
-
-/*!
- Returns the number of rows in the \a parent model item.
-
-*/
-
-int QDirModel::rowCount(const QModelIndex &parent) const
-{
- Q_D(const QDirModel);
- if (parent.column() > 0)
- return 0;
-
- if (!parent.isValid()) {
- if (!d->root.populated) // lazy population
- d->populate(&d->root);
- return d->root.children.count();
- }
- if (parent.model() != this)
- return 0;
- QDirModelPrivate::QDirNode *p = d->node(parent);
- if (p->info.isDir() && !p->populated) // lazy population
- d->populate(p);
- return p->children.count();
-}
-
-/*!
- Returns the number of columns in the \a parent model item.
-
-*/
-
-int QDirModel::columnCount(const QModelIndex &parent) const
-{
- if (parent.column() > 0)
- return 0;
- return 4;
-}
-
-/*!
- Returns the data for the model item \a index with the given \a role.
-*/
-QVariant QDirModel::data(const QModelIndex &index, int role) const
-{
- Q_D(const QDirModel);
- if (!d->indexValid(index))
- return QVariant();
-
- if (role == Qt::DisplayRole || role == Qt::EditRole) {
- switch (index.column()) {
- case 0: return d->name(index);
- case 1: return d->size(index);
- case 2: return d->type(index);
- case 3: return d->time(index);
- default:
- qWarning("data: invalid display value column %d", index.column());
- return QVariant();
- }
- }
-
- if (index.column() == 0) {
- if (role == FileIconRole)
- return fileIcon(index);
- if (role == FilePathRole)
- return filePath(index);
- if (role == FileNameRole)
- return fileName(index);
- }
-
- if (index.column() == 1 && Qt::TextAlignmentRole == role) {
- return Qt::AlignRight;
- }
- return QVariant();
-}
-
-/*!
- Sets the data for the model item \a index with the given \a role to
- the data referenced by the \a value. Returns \c true if successful;
- otherwise returns \c false.
-
- \sa Qt::ItemDataRole
-*/
-
-bool QDirModel::setData(const QModelIndex &index, const QVariant &value, int role)
-{
- Q_D(QDirModel);
- if (!d->indexValid(index) || index.column() != 0
- || (flags(index) & Qt::ItemIsEditable) == 0 || role != Qt::EditRole)
- return false;
-
- QDirModelPrivate::QDirNode *node = d->node(index);
- QDir dir = node->info.dir();
- QString name = value.toString();
- if (dir.rename(node->info.fileName(), name)) {
- node->info = QFileInfo(dir, name);
- QModelIndex sibling = index.sibling(index.row(), 3);
- emit dataChanged(index, sibling);
-
- d->toBeRefreshed = index.parent();
- QMetaObject::invokeMethod(this, "_q_refresh", Qt::QueuedConnection);
-
- return true;
- }
-
- return false;
-}
-
-/*!
- Returns the data stored under the given \a role for the specified \a section
- of the header with the given \a orientation.
-*/
-
-QVariant QDirModel::headerData(int section, Qt::Orientation orientation, int role) const
-{
- if (orientation == Qt::Horizontal) {
- if (role != Qt::DisplayRole)
- return QVariant();
- switch (section) {
- case 0: return tr("Name");
- case 1: return tr("Size");
- case 2: return
-#ifdef Q_OS_MAC
- tr("Kind", "Match OS X Finder");
-#else
- tr("Type", "All other platforms");
-#endif
- // Windows - Type
- // OS X - Kind
- // Konqueror - File Type
- // Nautilus - Type
- case 3: return tr("Date Modified");
- default: return QVariant();
- }
- }
- return QAbstractItemModel::headerData(section, orientation, role);
-}
-
-/*!
- Returns \c true if the \a parent model item has children; otherwise
- returns \c false.
-*/
-
-bool QDirModel::hasChildren(const QModelIndex &parent) const
-{
- Q_D(const QDirModel);
- if (parent.column() > 0)
- return false;
-
- if (!parent.isValid()) // the invalid index is the "My Computer" item
- return true; // the drives
- QDirModelPrivate::QDirNode *p = d->node(parent);
- Q_ASSERT(p);
-
- if (d->lazyChildCount) // optimization that only checks for children if the node has been populated
- return p->info.isDir();
- return p->info.isDir() && rowCount(parent) > 0;
-}
-
-/*!
- Returns the item flags for the given \a index in the model.
-
- \sa Qt::ItemFlags
-*/
-Qt::ItemFlags QDirModel::flags(const QModelIndex &index) const
-{
- Q_D(const QDirModel);
- Qt::ItemFlags flags = QAbstractItemModel::flags(index);
- if (!d->indexValid(index))
- return flags;
- flags |= Qt::ItemIsDragEnabled;
- if (d->readOnly)
- return flags;
- QDirModelPrivate::QDirNode *node = d->node(index);
- if ((index.column() == 0) && node->info.isWritable()) {
- flags |= Qt::ItemIsEditable;
- if (fileInfo(index).isDir()) // is directory and is editable
- flags |= Qt::ItemIsDropEnabled;
- }
- return flags;
-}
-
-/*!
- Sort the model items in the \a column using the \a order given.
- The order is a value defined in \l Qt::SortOrder.
-*/
-
-void QDirModel::sort(int column, Qt::SortOrder order)
-{
- QDir::SortFlags sort = QDir::DirsFirst | QDir::IgnoreCase;
- if (order == Qt::DescendingOrder)
- sort |= QDir::Reversed;
-
- switch (column) {
- case 0:
- sort |= QDir::Name;
- break;
- case 1:
- sort |= QDir::Size;
- break;
- case 2:
- sort |= QDir::Type;
- break;
- case 3:
- sort |= QDir::Time;
- break;
- default:
- break;
- }
-
- setSorting(sort);
-}
-
-/*!
- Returns a list of MIME types that can be used to describe a list of items
- in the model.
-*/
-
-QStringList QDirModel::mimeTypes() const
-{
- return QStringList(QLatin1String("text/uri-list"));
-}
-
-/*!
- Returns an object that contains a serialized description of the specified
- \a indexes. The format used to describe the items corresponding to the
- indexes is obtained from the mimeTypes() function.
-
- If the list of indexes is empty, 0 is returned rather than a serialized
- empty list.
-*/
-
-QMimeData *QDirModel::mimeData(const QModelIndexList &indexes) const
-{
- QList<QUrl> urls;
- QList<QModelIndex>::const_iterator it = indexes.begin();
- for (; it != indexes.end(); ++it)
- if ((*it).column() == 0)
- urls << QUrl::fromLocalFile(filePath(*it));
- QMimeData *data = new QMimeData();
- data->setUrls(urls);
- return data;
-}
-
-/*!
- Handles the \a data supplied by a drag and drop operation that ended with
- the given \a action over the row in the model specified by the \a row and
- \a column and by the \a parent index.
-
- Returns \c true if the drop was successful, and false otherwise.
-
- \sa supportedDropActions()
-*/
-
-bool QDirModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
- int /* row */, int /* column */, const QModelIndex &parent)
-{
- Q_D(QDirModel);
- if (!d->indexValid(parent) || isReadOnly())
- return false;
-
- bool success = true;
- QString to = filePath(parent) + QDir::separator();
- QModelIndex _parent = parent;
-
- QList<QUrl> urls = data->urls();
- QList<QUrl>::const_iterator it = urls.constBegin();
-
- switch (action) {
- case Qt::CopyAction:
- for (; it != urls.constEnd(); ++it) {
- QString path = (*it).toLocalFile();
- success = QFile::copy(path, to + QFileInfo(path).fileName()) && success;
- }
- break;
- case Qt::LinkAction:
- for (; it != urls.constEnd(); ++it) {
- QString path = (*it).toLocalFile();
- success = QFile::link(path, to + QFileInfo(path).fileName()) && success;
- }
- break;
- case Qt::MoveAction:
- for (; it != urls.constEnd(); ++it) {
- QString path = (*it).toLocalFile();
- if (QFile::copy(path, to + QFileInfo(path).fileName())
- && QFile::remove(path)) {
- QModelIndex idx=index(QFileInfo(path).path());
- if (idx.isValid()) {
- refresh(idx);
- //the previous call to refresh may invalidate the _parent. so recreate a new QModelIndex
- _parent = index(to);
- }
- } else {
- success = false;
- }
- }
- break;
- default:
- return false;
- }
-
- if (success)
- refresh(_parent);
-
- return success;
-}
-
-/*!
- Returns the drop actions supported by this model.
-
- \sa Qt::DropActions
-*/
-
-Qt::DropActions QDirModel::supportedDropActions() const
-{
- return Qt::CopyAction | Qt::MoveAction; // FIXME: LinkAction is not supported yet
-}
-
-/*!
- Sets the \a provider of file icons for the directory model.
-
-*/
-
-void QDirModel::setIconProvider(QFileIconProvider *provider)
-{
- Q_D(QDirModel);
- d->iconProvider = provider;
-}
-
-/*!
- Returns the file icon provider for this directory model.
-*/
-
-QFileIconProvider *QDirModel::iconProvider() const
-{
- Q_D(const QDirModel);
- return d->iconProvider;
-}
-
-/*!
- Sets the name \a filters for the directory model.
-*/
-
-void QDirModel::setNameFilters(const QStringList &filters)
-{
- Q_D(QDirModel);
- d->nameFilters = filters;
- emit layoutAboutToBeChanged();
- if (d->shouldStat)
- refresh(QModelIndex());
- else
- d->invalidate();
- emit layoutChanged();
-}
-
-/*!
- Returns a list of filters applied to the names in the model.
-*/
-
-QStringList QDirModel::nameFilters() const
-{
- Q_D(const QDirModel);
- return d->nameFilters;
-}
-
-/*!
- Sets the directory model's filter to that specified by \a filters.
-
- Note that the filter you set should always include the QDir::AllDirs enum value,
- otherwise QDirModel won't be able to read the directory structure.
-
- \sa QDir::Filters
-*/
-
-void QDirModel::setFilter(QDir::Filters filters)
-{
- Q_D(QDirModel);
- d->filters = filters;
- emit layoutAboutToBeChanged();
- if (d->shouldStat)
- refresh(QModelIndex());
- else
- d->invalidate();
- emit layoutChanged();
-}
-
-/*!
- Returns the filter specification for the directory model.
-
- \sa QDir::Filters
-*/
-
-QDir::Filters QDirModel::filter() const
-{
- Q_D(const QDirModel);
- return d->filters;
-}
-
-/*!
- Sets the directory model's sorting order to that specified by \a sort.
-
- \sa QDir::SortFlags
-*/
-
-void QDirModel::setSorting(QDir::SortFlags sort)
-{
- Q_D(QDirModel);
- d->sort = sort;
- emit layoutAboutToBeChanged();
- if (d->shouldStat)
- refresh(QModelIndex());
- else
- d->invalidate();
- emit layoutChanged();
-}
-
-/*!
- Returns the sorting method used for the directory model.
-
- \sa QDir::SortFlags
-*/
-
-QDir::SortFlags QDirModel::sorting() const
-{
- Q_D(const QDirModel);
- return d->sort;
-}
-
-/*!
- \property QDirModel::resolveSymlinks
- \brief Whether the directory model should resolve symbolic links
-
- This is only relevant on operating systems that support symbolic
- links.
-*/
-void QDirModel::setResolveSymlinks(bool enable)
-{
- Q_D(QDirModel);
- d->resolveSymlinks = enable;
-}
-
-bool QDirModel::resolveSymlinks() const
-{
- Q_D(const QDirModel);
- return d->resolveSymlinks;
-}
-
-/*!
- \property QDirModel::readOnly
- \brief Whether the directory model allows writing to the file system
-
- If this property is set to false, the directory model will allow renaming, copying
- and deleting of files and directories.
-
- This property is \c true by default
-*/
-
-void QDirModel::setReadOnly(bool enable)
-{
- Q_D(QDirModel);
- d->readOnly = enable;
-}
-
-bool QDirModel::isReadOnly() const
-{
- Q_D(const QDirModel);
- return d->readOnly;
-}
-
-/*!
- \property QDirModel::lazyChildCount
- \brief Whether the directory model optimizes the hasChildren function
- to only check if the item is a directory.
-
- If this property is set to false, the directory model will make sure that a directory
- actually containes any files before reporting that it has children.
- Otherwise the directory model will report that an item has children if the item
- is a directory.
-
- This property is \c false by default
-*/
-
-void QDirModel::setLazyChildCount(bool enable)
-{
- Q_D(QDirModel);
- d->lazyChildCount = enable;
-}
-
-bool QDirModel::lazyChildCount() const
-{
- Q_D(const QDirModel);
- return d->lazyChildCount;
-}
-
-/*!
- QDirModel caches file information. This function updates the
- cache. The \a parent parameter is the directory from which the
- model is updated; the default value will update the model from
- root directory of the file system (the entire model).
-*/
-
-void QDirModel::refresh(const QModelIndex &parent)
-{
- Q_D(QDirModel);
-
- QDirModelPrivate::QDirNode *n = d->indexValid(parent) ? d->node(parent) : &(d->root);
-
- int rows = n->children.count();
- if (rows == 0) {
- emit layoutAboutToBeChanged();
- n->stat = true; // make sure that next time we read all the info
- n->populated = false;
- emit layoutChanged();
- return;
- }
-
- emit layoutAboutToBeChanged();
- d->savePersistentIndexes();
- d->rowsAboutToBeRemoved(parent, 0, rows - 1);
- n->stat = true; // make sure that next time we read all the info
- d->clear(n);
- d->rowsRemoved(parent, 0, rows - 1);
- d->restorePersistentIndexes();
- emit layoutChanged();
-}
-
-/*!
- \overload
-
- Returns the model item index for the given \a path.
-*/
-
-QModelIndex QDirModel::index(const QString &path, int column) const
-{
- Q_D(const QDirModel);
-
- if (path.isEmpty() || path == QCoreApplication::translate("QFileDialog", "My Computer"))
- return QModelIndex();
-
- QString absolutePath = QDir(path).absolutePath();
-#if defined(Q_OS_WIN)
- absolutePath = absolutePath.toLower();
- // On Windows, "filename......." and "filename" are equivalent
- if (absolutePath.endsWith(QLatin1Char('.'))) {
- int i;
- for (i = absolutePath.count() - 1; i >= 0; --i) {
- if (absolutePath.at(i) != QLatin1Char('.'))
- break;
- }
- absolutePath = absolutePath.left(i+1);
- }
-#endif
-
- QStringList pathElements = absolutePath.split(QLatin1Char('/'), QString::SkipEmptyParts);
- if ((pathElements.isEmpty() || !QFileInfo::exists(path))
-#if !defined(Q_OS_WIN)
- && path != QLatin1String("/")
-#endif
- )
- return QModelIndex();
-
- QModelIndex idx; // start with "My Computer"
- if (!d->root.populated) // make sure the root is populated
- d->populate(&d->root);
-
-#if defined(Q_OS_WIN)
- if (absolutePath.startsWith(QLatin1String("//"))) { // UNC path
- QString host = pathElements.constFirst();
- int r = 0;
- for (; r < d->root.children.count(); ++r)
- if (d->root.children.at(r).info.fileName() == host)
- break;
- bool childAppended = false;
- if (r >= d->root.children.count() && d->allowAppendChild) {
- d->appendChild(&d->root, QLatin1String("//") + host);
- childAppended = true;
- }
- idx = index(r, 0, QModelIndex());
- pathElements.pop_front();
- if (childAppended)
- emit const_cast<QDirModel*>(this)->layoutChanged();
- } else
-#endif
-#if defined(Q_OS_WIN)
- if (pathElements.at(0).endsWith(QLatin1Char(':'))) {
- pathElements[0] += QLatin1Char('/');
- }
-#else
- // add the "/" item, since it is a valid path element on unix
- pathElements.prepend(QLatin1String("/"));
-#endif
-
- for (int i = 0; i < pathElements.count(); ++i) {
- Q_ASSERT(!pathElements.at(i).isEmpty());
- QString element = pathElements.at(i);
- QDirModelPrivate::QDirNode *parent = (idx.isValid() ? d->node(idx) : &d->root);
-
- Q_ASSERT(parent);
- if (!parent->populated)
- d->populate(parent);
-
- // search for the element in the child nodes first
- int row = -1;
- for (int j = parent->children.count() - 1; j >= 0; --j) {
- const QFileInfo& fi = parent->children.at(j).info;
- QString childFileName;
- childFileName = idx.isValid() ? fi.fileName() : fi.absoluteFilePath();
-#if defined(Q_OS_WIN)
- childFileName = childFileName.toLower();
-#endif
- if (childFileName == element) {
- if (i == pathElements.count() - 1)
- parent->children[j].stat = true;
- row = j;
- break;
- }
- }
-
- // we couldn't find the path element, we create a new node since we _know_ that the path is valid
- if (row == -1) {
- QString newPath = parent->info.absoluteFilePath() + QLatin1Char('/') + element;
- if (!d->allowAppendChild || !QFileInfo(newPath).isDir())
- return QModelIndex();
- d->appendChild(parent, newPath);
- row = parent->children.count() - 1;
- if (i == pathElements.count() - 1) // always stat children of the last element
- parent->children[row].stat = true;
- emit const_cast<QDirModel*>(this)->layoutChanged();
- }
-
- Q_ASSERT(row >= 0);
- idx = createIndex(row, 0, static_cast<void*>(&parent->children[row]));
- Q_ASSERT(idx.isValid());
- }
-
- if (column != 0)
- return idx.sibling(idx.row(), column);
- return idx;
-}
-
-/*!
- Returns \c true if the model item \a index represents a directory;
- otherwise returns \c false.
-*/
-
-bool QDirModel::isDir(const QModelIndex &index) const
-{
- Q_D(const QDirModel);
- Q_ASSERT(d->indexValid(index));
- QDirModelPrivate::QDirNode *node = d->node(index);
- return node->info.isDir();
-}
-
-/*!
- Create a directory with the \a name in the \a parent model item.
-*/
-
-QModelIndex QDirModel::mkdir(const QModelIndex &parent, const QString &name)
-{
- Q_D(QDirModel);
- if (!d->indexValid(parent) || isReadOnly())
- return QModelIndex();
-
- QDirModelPrivate::QDirNode *p = d->node(parent);
- QString path = p->info.absoluteFilePath();
- // For the indexOf() method to work, the new directory has to be a direct child of
- // the parent directory.
-
- QDir newDir(name);
- QDir dir(path);
- if (newDir.isRelative())
- newDir = QDir(path + QLatin1Char('/') + name);
- QString childName = newDir.dirName(); // Get the singular name of the directory
- newDir.cdUp();
-
- if (newDir.absolutePath() != dir.absolutePath() || !dir.mkdir(name))
- return QModelIndex(); // nothing happened
-
- refresh(parent);
-
- QStringList entryList = d->entryList(path);
- int r = entryList.indexOf(childName);
- QModelIndex i = index(r, 0, parent); // return an invalid index
-
- return i;
-}
-
-/*!
- Removes the directory corresponding to the model item \a index in the
- directory model and \b{deletes the corresponding directory from the
- file system}, returning true if successful. If the directory cannot be
- removed, false is returned.
-
- \warning This function deletes directories from the file system; it does
- \b{not} move them to a location where they can be recovered.
-
- \sa remove()
-*/
-
-bool QDirModel::rmdir(const QModelIndex &index)
-{
- Q_D(QDirModel);
- if (!d->indexValid(index) || isReadOnly())
- return false;
-
- QDirModelPrivate::QDirNode *n = d_func()->node(index);
- if (Q_UNLIKELY(!n->info.isDir())) {
- qWarning("rmdir: the node is not a directory");
- return false;
- }
-
- QModelIndex par = parent(index);
- QDirModelPrivate::QDirNode *p = d_func()->node(par);
- QDir dir = p->info.dir(); // parent dir
- QString path = n->info.absoluteFilePath();
- if (!dir.rmdir(path))
- return false;
-
- refresh(par);
-
- return true;
-}
-
-/*!
- Removes the model item \a index from the directory model and \b{deletes the
- corresponding file from the file system}, returning true if successful. If the
- item cannot be removed, false is returned.
-
- \warning This function deletes files from the file system; it does \b{not}
- move them to a location where they can be recovered.
-
- \sa rmdir()
-*/
-
-bool QDirModel::remove(const QModelIndex &index)
-{
- Q_D(QDirModel);
- if (!d->indexValid(index) || isReadOnly())
- return false;
-
- QDirModelPrivate::QDirNode *n = d_func()->node(index);
- if (n->info.isDir())
- return false;
-
- QModelIndex par = parent(index);
- QDirModelPrivate::QDirNode *p = d_func()->node(par);
- QDir dir = p->info.dir(); // parent dir
- QString path = n->info.absoluteFilePath();
- if (!dir.remove(path))
- return false;
-
- refresh(par);
-
- return true;
-}
-
-/*!
- Returns the path of the item stored in the model under the
- \a index given.
-
-*/
-
-QString QDirModel::filePath(const QModelIndex &index) const
-{
- Q_D(const QDirModel);
- if (d->indexValid(index)) {
- QFileInfo fi = fileInfo(index);
- if (d->resolveSymlinks && fi.isSymLink())
- fi = QDirModelPrivate::resolvedInfo(fi);
- return QDir::cleanPath(fi.absoluteFilePath());
- }
- return QString(); // root path
-}
-
-/*!
- Returns the name of the item stored in the model under the
- \a index given.
-
-*/
-
-QString QDirModel::fileName(const QModelIndex &index) const
-{
- Q_D(const QDirModel);
- if (!d->indexValid(index))
- return QString();
- QFileInfo info = fileInfo(index);
- const QString &path = info.absoluteFilePath();
- if (QFileSystemEntry::isRootPath(path))
- return path;
- if (d->resolveSymlinks && info.isSymLink())
- info = QDirModelPrivate::resolvedInfo(info);
- return info.fileName();
-}
-
-/*!
- Returns the icons for the item stored in the model under the given
- \a index.
-*/
-
-QIcon QDirModel::fileIcon(const QModelIndex &index) const
-{
- Q_D(const QDirModel);
- if (!d->indexValid(index))
- return d->iconProvider->icon(QFileIconProvider::Computer);
- QDirModelPrivate::QDirNode *node = d->node(index);
- if (node->icon.isNull())
- node->icon = d->iconProvider->icon(node->info);
- return node->icon;
-}
-
-/*!
- Returns the file information for the specified model \a index.
-
- \b{Note:} If the model index represents a symbolic link in the
- underlying filing system, the file information returned will contain
- information about the symbolic link itself, regardless of whether
- resolveSymlinks is enabled or not.
-
- \sa QFileInfo::symLinkTarget()
-*/
-
-QFileInfo QDirModel::fileInfo(const QModelIndex &index) const
-{
- Q_D(const QDirModel);
- Q_ASSERT(d->indexValid(index));
-
- QDirModelPrivate::QDirNode *node = d->node(index);
- return node->info;
-}
-
-/*
- The root node is never seen outside the model.
-*/
-
-void QDirModelPrivate::init()
-{
- filters = QDir::AllEntries | QDir::NoDotAndDotDot;
- sort = QDir::Name;
- nameFilters << QLatin1String("*");
- root.parent = 0;
- root.info = QFileInfo();
- clear(&root);
- roleNames.insert(QDirModel::FileIconRole, QByteArrayLiteral("fileIcon")); // == Qt::decoration
- roleNames.insert(QDirModel::FilePathRole, QByteArrayLiteral("filePath"));
- roleNames.insert(QDirModel::FileNameRole, QByteArrayLiteral("fileName"));
-}
-
-QDirModelPrivate::QDirNode *QDirModelPrivate::node(int row, QDirNode *parent) const
-{
- if (row < 0)
- return 0;
-
- bool isDir = !parent || parent->info.isDir();
- QDirNode *p = (parent ? parent : &root);
- if (isDir && !p->populated)
- populate(p); // will also resolve symlinks
-
- if (Q_UNLIKELY(row >= p->children.count())) {
- qWarning("node: the row does not exist");
- return 0;
- }
-
- return const_cast<QDirNode*>(&p->children.at(row));
-}
-
-QVector<QDirModelPrivate::QDirNode> QDirModelPrivate::children(QDirNode *parent, bool stat) const
-{
- Q_ASSERT(parent);
- QFileInfoList infoList;
- if (parent == &root) {
- parent = 0;
- infoList = QDir::drives();
- } else if (parent->info.isDir()) {
- //resolve directory links only if requested.
- if (parent->info.isSymLink() && resolveSymlinks) {
- QString link = parent->info.symLinkTarget();
- if (link.size() > 1 && link.at(link.size() - 1) == QDir::separator())
- link.chop(1);
- if (stat)
- infoList = entryInfoList(link);
- else
- infoList = QDir(link).entryInfoList(nameFilters, QDir::AllEntries | QDir::System);
- } else {
- if (stat)
- infoList = entryInfoList(parent->info.absoluteFilePath());
- else
- infoList = QDir(parent->info.absoluteFilePath()).entryInfoList(nameFilters, QDir::AllEntries | QDir::System);
- }
- }
-
- QVector<QDirNode> nodes(infoList.count());
- for (int i = 0; i < infoList.count(); ++i) {
- QDirNode &node = nodes[i];
- node.parent = parent;
- node.info = infoList.at(i);
- node.populated = false;
- node.stat = shouldStat;
- }
-
- return nodes;
-}
-
-void QDirModelPrivate::_q_refresh()
-{
- Q_Q(QDirModel);
- q->refresh(toBeRefreshed);
- toBeRefreshed = QModelIndex();
-}
-
-void QDirModelPrivate::savePersistentIndexes()
-{
- Q_Q(QDirModel);
- savedPersistent.clear();
- savedPersistent.reserve(persistent.indexes.size());
- foreach (QPersistentModelIndexData *data, persistent.indexes) {
- QModelIndex index = data->index;
- SavedPersistent saved = {
- q->filePath(index),
- index.column(),
- data,
- index,
- };
- savedPersistent.push_back(std::move(saved));
- }
-}
-
-void QDirModelPrivate::restorePersistentIndexes()
-{
- Q_Q(QDirModel);
- bool allow = allowAppendChild;
- allowAppendChild = false;
- for (const SavedPersistent &sp : qAsConst(savedPersistent)) {
- QPersistentModelIndexData *data = sp.data;
- QModelIndex idx = q->index(sp.path, sp.column);
- if (idx != data->index || data->index.model() == nullptr) {
- //data->model may be equal to 0 if the model is getting destroyed
- persistent.indexes.remove(data->index);
- data->index = idx;
- if (idx.isValid())
- persistent.indexes.insert(idx, data);
- }
- }
- savedPersistent.clear();
- allowAppendChild = allow;
-}
-
-QFileInfoList QDirModelPrivate::entryInfoList(const QString &path) const
-{
- const QDir dir(path);
- return dir.entryInfoList(nameFilters, filters, sort);
-}
-
-QStringList QDirModelPrivate::entryList(const QString &path) const
-{
- const QDir dir(path);
- return dir.entryList(nameFilters, filters, sort);
-}
-
-QString QDirModelPrivate::name(const QModelIndex &index) const
-{
- const QDirNode *n = node(index);
- const QFileInfo info = n->info;
- QString name = info.absoluteFilePath();
- if (QFileSystemEntry::isRootPath(name)) {
-#if defined(Q_OS_WIN)
- if (name.startsWith(QLatin1Char('/'))) // UNC host
- return info.fileName();
- if (name.endsWith(QLatin1Char('/')))
- name.chop(1);
-#endif
- return name;
- }
- return info.fileName();
-}
-
-QString QDirModelPrivate::size(const QModelIndex &index) const
-{
- const QDirNode *n = node(index);
- if (n->info.isDir()) {
-#ifdef Q_OS_MAC
- return QLatin1String("--");
-#else
- return QLatin1String("");
-#endif
- // Windows - ""
- // OS X - "--"
- // Konqueror - "4 KB"
- // Nautilus - "9 items" (the number of children)
- }
-
- return QLocale::system().formattedDataSize(n->info.size());
-}
-
-QString QDirModelPrivate::type(const QModelIndex &index) const
-{
- return iconProvider->type(node(index)->info);
-}
-
-QString QDirModelPrivate::time(const QModelIndex &index) const
-{
-#if QT_CONFIG(datestring)
- return node(index)->info.lastModified().toString(Qt::LocalDate);
-#else
- Q_UNUSED(index);
- return QString();
-#endif
-}
-
-void QDirModelPrivate::appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const
-{
- QDirModelPrivate::QDirNode node;
- node.populated = false;
- node.stat = shouldStat;
- node.parent = (parent == &root ? 0 : parent);
- node.info = QFileInfo(path);
- node.info.setCaching(true);
-
- // The following append(node) may reallocate the vector, thus
- // we need to update the pointers to the childnodes parent.
- QDirModelPrivate *that = const_cast<QDirModelPrivate *>(this);
- that->savePersistentIndexes();
- parent->children.append(node);
- for (int i = 0; i < parent->children.count(); ++i) {
- QDirNode *childNode = &parent->children[i];
- for (int j = 0; j < childNode->children.count(); ++j)
- childNode->children[j].parent = childNode;
- }
- that->restorePersistentIndexes();
-}
-
-QFileInfo QDirModelPrivate::resolvedInfo(QFileInfo info)
-{
-#ifdef Q_OS_WIN
- // On windows, we cannot create a shortcut to a shortcut.
- return QFileInfo(info.symLinkTarget());
-#else
- QStringList paths;
- do {
- QFileInfo link(info.symLinkTarget());
- if (link.isRelative())
- info.setFile(info.absolutePath(), link.filePath());
- else
- info = link;
- if (paths.contains(info.absoluteFilePath()))
- return QFileInfo();
- paths.append(info.absoluteFilePath());
- } while (info.isSymLink());
- return info;
-#endif
-}
-
-QT_END_NAMESPACE
-
-#include "moc_qdirmodel.cpp"
-
-#endif // QT_DEPRECATED_SINCE(5, 15)
diff --git a/src/widgets/itemviews/qdirmodel.h b/src/widgets/itemviews/qdirmodel.h
deleted file mode 100644
index 665a622dbe..0000000000
--- a/src/widgets/itemviews/qdirmodel.h
+++ /dev/null
@@ -1,152 +0,0 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
-
-#ifndef QDIRMODEL_H
-#define QDIRMODEL_H
-
-#include <QtWidgets/qtwidgetsglobal.h>
-#include <QtCore/qabstractitemmodel.h>
-#include <QtCore/qdir.h>
-#include <QtWidgets/qfileiconprovider.h>
-
-#if QT_DEPRECATED_SINCE(5, 15)
-
-QT_REQUIRE_CONFIG(dirmodel);
-
-QT_BEGIN_NAMESPACE
-
-class QDirModelPrivate;
-
-class Q_WIDGETS_EXPORT QDirModel : public QAbstractItemModel
-{
- Q_OBJECT
- Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
- Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
- Q_PROPERTY(bool lazyChildCount READ lazyChildCount WRITE setLazyChildCount)
-
-public:
- enum Roles {
- FileIconRole = Qt::DecorationRole,
- FilePathRole = Qt::UserRole + 1,
- FileNameRole
- };
-
- QT_DEPRECATED_VERSION_X_5_15("Use QFileSystemModel") QDirModel(const QStringList &nameFilters,
- QDir::Filters filters, QDir::SortFlags sort,
- QObject *parent = nullptr);
- QT_DEPRECATED_VERSION_X_5_15("Use QFileSystemModel") explicit QDirModel(QObject *parent = nullptr);
- ~QDirModel();
-
- QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
- QModelIndex parent(const QModelIndex &child) const override;
-
- int rowCount(const QModelIndex &parent = QModelIndex()) const override;
- int columnCount(const QModelIndex &parent = QModelIndex()) const override;
-
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
- bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
-
- QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
-
- bool hasChildren(const QModelIndex &index = QModelIndex()) const override;
- Qt::ItemFlags flags(const QModelIndex &index) const override;
-
- void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
-
- QStringList mimeTypes() const override;
- QMimeData *mimeData(const QModelIndexList &indexes) const override;
- bool dropMimeData(const QMimeData *data, Qt::DropAction action,
- int row, int column, const QModelIndex &parent) override;
- Qt::DropActions supportedDropActions() const override;
-
- // QDirModel specific API
-
- void setIconProvider(QFileIconProvider *provider);
- QFileIconProvider *iconProvider() const;
-
- void setNameFilters(const QStringList &filters);
- QStringList nameFilters() const;
-
- void setFilter(QDir::Filters filters);
- QDir::Filters filter() const;
-
- void setSorting(QDir::SortFlags sort);
- QDir::SortFlags sorting() const;
-
- void setResolveSymlinks(bool enable);
- bool resolveSymlinks() const;
-
- void setReadOnly(bool enable);
- bool isReadOnly() const;
-
- void setLazyChildCount(bool enable);
- bool lazyChildCount() const;
-
- QModelIndex index(const QString &path, int column = 0) const;
-
- bool isDir(const QModelIndex &index) const;
- QModelIndex mkdir(const QModelIndex &parent, const QString &name);
- bool rmdir(const QModelIndex &index);
- bool remove(const QModelIndex &index);
-
- QString filePath(const QModelIndex &index) const;
- QString fileName(const QModelIndex &index) const;
- QIcon fileIcon(const QModelIndex &index) const;
- QFileInfo fileInfo(const QModelIndex &index) const;
-
- using QObject::parent;
-
-public Q_SLOTS:
- void refresh(const QModelIndex &parent = QModelIndex());
-
-protected:
- QDirModel(QDirModelPrivate &, QObject *parent = nullptr);
- friend class QFileDialogPrivate;
-
-private:
- Q_DECLARE_PRIVATE(QDirModel)
- Q_DISABLE_COPY(QDirModel)
- Q_PRIVATE_SLOT(d_func(), void _q_refresh())
-};
-
-QT_END_NAMESPACE
-
-#endif // QT_DEPRECATED_SINCE(5, 15)
-
-#endif // QDIRMODEL_H
diff --git a/src/widgets/itemviews/qfileiconprovider.cpp b/src/widgets/itemviews/qfileiconprovider.cpp
index 65ae6b9505..003fef4224 100644
--- a/src/widgets/itemviews/qfileiconprovider.cpp
+++ b/src/widgets/itemviews/qfileiconprovider.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 "qfileiconprovider.h"
#include "qfileiconprovider_p.h"
@@ -53,10 +17,8 @@
#if defined(Q_OS_WIN)
# include <qt_windows.h>
-# ifndef Q_OS_WINRT
-# include <commctrl.h>
-# include <objbase.h>
-# endif
+# include <commctrl.h>
+# include <objbase.h>
#endif
QT_BEGIN_NAMESPACE
@@ -66,32 +28,11 @@ QT_BEGIN_NAMESPACE
\inmodule QtWidgets
- \brief The QFileIconProvider class provides file icons for the QDirModel and the QFileSystemModel classes.
+ \brief The QFileIconProvider class provides file icons for the QFileSystemModel class.
*/
-/*!
- \enum QFileIconProvider::IconType
- \value Computer
- \value Desktop
- \value Trashcan
- \value Network
- \value Drive
- \value Folder
- \value File
-*/
-
-
-/*!
- \enum QFileIconProvider::Option
- \since 5.2
-
- \value DontUseCustomDirectoryIcons Always use the default directory icon.
- Some platforms allow the user to set a different icon. Custom icon lookup
- cause a big performance impact over network or removable drives.
-*/
-
-QFileIconProviderPrivate::QFileIconProviderPrivate(QFileIconProvider *q) :
- q_ptr(q), homePath(QDir::home().absolutePath())
+QFileIconProviderPrivate::QFileIconProviderPrivate(QFileIconProvider *q)
+ : QAbstractFileIconProviderPrivate(q), homePath(QDir::home().absolutePath())
{
}
@@ -157,46 +98,18 @@ QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const
*/
QFileIconProvider::QFileIconProvider()
- : d_ptr(new QFileIconProviderPrivate(this))
+ : QAbstractFileIconProvider(*new QFileIconProviderPrivate(this))
{
}
/*!
Destroys the file icon provider.
-
-*/
-
-QFileIconProvider::~QFileIconProvider()
-{
-}
-
-/*!
- \since 5.2
- Sets \a options that affect the icon provider.
- \sa options()
*/
-void QFileIconProvider::setOptions(QFileIconProvider::Options options)
-{
- Q_D(QFileIconProvider);
- d->options = options;
-}
+QFileIconProvider::~QFileIconProvider() = default;
/*!
- \since 5.2
- Returns all the options that affect the icon provider.
- By default, all options are disabled.
- \sa setOptions()
-*/
-
-QFileIconProvider::Options QFileIconProvider::options() const
-{
- Q_D(const QFileIconProvider);
- return d->options;
-}
-
-/*!
- Returns an icon set for the given \a type.
+ \reimp
*/
QIcon QFileIconProvider::icon(IconType type) const
@@ -223,21 +136,13 @@ QIcon QFileIconProvider::icon(IconType type) const
return QIcon();
}
-static inline QPlatformTheme::IconOptions toThemeIconOptions(QFileIconProvider::Options options)
-{
- QPlatformTheme::IconOptions result;
- if (options & QFileIconProvider::DontUseCustomDirectoryIcons)
- result |= QPlatformTheme::DontUseCustomDirectoryIcons;
- return result;
-}
-
QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const
{
- return QGuiApplicationPrivate::platformTheme()->fileIcon(fi, toThemeIconOptions(options));
+ return getPlatformThemeIcon(fi);
}
/*!
- Returns an icon for the file described by \a info.
+ \reimp
*/
QIcon QFileIconProvider::icon(const QFileInfo &info) const
@@ -250,7 +155,7 @@ QIcon QFileIconProvider::icon(const QFileInfo &info) const
const QString &path = info.absoluteFilePath();
if (path.isEmpty() || QFileSystemEntry::isRootPath(path))
-#if defined (Q_OS_WIN) && !defined(Q_OS_WINRT)
+#if defined (Q_OS_WIN)
{
UINT type = GetDriveType(reinterpret_cast<const wchar_t *>(path.utf16()));
@@ -294,45 +199,4 @@ QIcon QFileIconProvider::icon(const QFileInfo &info) const
return QIcon();
}
-/*!
- Returns the type of the file described by \a info.
-*/
-
-QString QFileIconProvider::type(const QFileInfo &info) const
-{
- if (QFileSystemEntry::isRootPath(info.absoluteFilePath()))
- return QApplication::translate("QFileDialog", "Drive");
- if (info.isFile()) {
- if (!info.suffix().isEmpty()) {
- //: %1 is a file name suffix, for example txt
- return QApplication::translate("QFileDialog", "%1 File").arg(info.suffix());
- }
- return QApplication::translate("QFileDialog", "File");
- }
-
- if (info.isDir())
-#ifdef Q_OS_WIN
- return QApplication::translate("QFileDialog", "File Folder", "Match Windows Explorer");
-#else
- return QApplication::translate("QFileDialog", "Folder", "All other platforms");
-#endif
- // Windows - "File Folder"
- // OS X - "Folder"
- // Konqueror - "Folder"
- // Nautilus - "folder"
-
- if (info.isSymLink())
-#ifdef Q_OS_MAC
- return QApplication::translate("QFileDialog", "Alias", "OS X Finder");
-#else
- return QApplication::translate("QFileDialog", "Shortcut", "All other platforms");
-#endif
- // OS X - "Alias"
- // Windows - "Shortcut"
- // Konqueror - "Folder" or "TXT File" i.e. what it is pointing to
- // Nautilus - "link to folder" or "link to object file", same as Konqueror
-
- return QApplication::translate("QFileDialog", "Unknown");
-}
-
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qfileiconprovider.h b/src/widgets/itemviews/qfileiconprovider.h
index 732ca1ac8e..3cc481b756 100644
--- a/src/widgets/itemviews/qfileiconprovider.h
+++ b/src/widgets/itemviews/qfileiconprovider.h
@@ -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
#ifndef QFILEICONPROVIDER_H
#define QFILEICONPROVIDER_H
@@ -44,39 +8,27 @@
#include <QtCore/qfileinfo.h>
#include <QtCore/qscopedpointer.h>
#include <QtGui/qicon.h>
+#include <QtGui/qabstractfileiconprovider.h>
QT_BEGIN_NAMESPACE
class QFileIconProviderPrivate;
-class Q_WIDGETS_EXPORT QFileIconProvider
+class Q_WIDGETS_EXPORT QFileIconProvider : public QAbstractFileIconProvider
{
public:
QFileIconProvider();
- virtual ~QFileIconProvider();
- enum IconType { Computer, Desktop, Trashcan, Network, Drive, Folder, File };
+ ~QFileIconProvider();
- enum Option {
- DontUseCustomDirectoryIcons = 0x00000001
- };
- Q_DECLARE_FLAGS(Options, Option)
-
- virtual QIcon icon(IconType type) const;
- virtual QIcon icon(const QFileInfo &info) const;
- virtual QString type(const QFileInfo &info) const;
-
- void setOptions(Options options);
- Options options() const;
+ QIcon icon(IconType type) const override;
+ QIcon icon(const QFileInfo &info) const override;
private:
Q_DECLARE_PRIVATE(QFileIconProvider)
- QScopedPointer<QFileIconProviderPrivate> d_ptr;
Q_DISABLE_COPY(QFileIconProvider)
};
-Q_DECLARE_OPERATORS_FOR_FLAGS(QFileIconProvider::Options)
-
QT_END_NAMESPACE
#endif // QFILEICONPROVIDER_H
diff --git a/src/widgets/itemviews/qfileiconprovider_p.h b/src/widgets/itemviews/qfileiconprovider_p.h
index 4726b15816..e94980066f 100644
--- a/src/widgets/itemviews/qfileiconprovider_p.h
+++ b/src/widgets/itemviews/qfileiconprovider_p.h
@@ -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
#ifndef QFILEICONPROVIDER_P_H
#define QFILEICONPROVIDER_P_H
@@ -54,6 +18,7 @@
#include <QtWidgets/private/qtwidgetsglobal_p.h>
#include "qfileiconprovider.h"
+#include <private/qabstractfileiconprovider_p.h>
#include <QtCore/qstring.h>
#include <QtGui/qicon.h>
#include <QtWidgets/qstyle.h>
@@ -62,7 +27,7 @@ QT_BEGIN_NAMESPACE
class QFileInfo;
-class QFileIconProviderPrivate
+class QFileIconProviderPrivate : public QAbstractFileIconProviderPrivate
{
Q_DECLARE_PUBLIC(QFileIconProvider)
@@ -71,9 +36,7 @@ public:
QIcon getIcon(QStyle::StandardPixmap name) const;
QIcon getIcon(const QFileInfo &fi) const;
- QFileIconProvider *q_ptr;
const QString homePath;
- QFileIconProvider::Options options;
private:
mutable QIcon file;
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index d7bdf6aa4c..9ce09dbacc 100644
--- a/src/widgets/itemviews/qheaderview.cpp
+++ b/src/widgets/itemviews/qheaderview.cpp
@@ -1,63 +1,30 @@
-/****************************************************************************
-**
-** 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 "qheaderview.h"
+#include <qabstractitemdelegate.h>
+#include <qapplication.h>
#include <qbitarray.h>
#include <qbrush.h>
#include <qdebug.h>
#include <qevent.h>
+#include <qlist.h>
#include <qpainter.h>
#include <qscrollbar.h>
-#include <qtooltip.h>
-#if QT_CONFIG(whatsthis)
-#include <qwhatsthis.h>
-#endif
#include <qstyle.h>
#include <qstyleoption.h>
-#include <qvector.h>
-#include <qapplication.h>
+#if QT_CONFIG(tooltip)
+#include <qtooltip.h>
+#endif
#include <qvarlengtharray.h>
-#include <qabstractitemdelegate.h>
#include <qvariant.h>
+#if QT_CONFIG(whatsthis)
+#include <qwhatsthis.h>
+#endif
#include <private/qheaderview_p.h>
#include <private/qabstractitemmodel_p.h>
+#include <private/qabstractitemdelegate_p.h>
#ifndef QT_NO_DATASTREAM
#include <qdatastream.h>
@@ -328,6 +295,8 @@ QHeaderView::QHeaderView(QHeaderViewPrivate &dd,
QHeaderView::~QHeaderView()
{
+ Q_D(QHeaderView);
+ d->disconnectModel();
}
/*!
@@ -355,68 +324,35 @@ void QHeaderView::setModel(QAbstractItemModel *model)
return;
Q_D(QHeaderView);
d->layoutChangePersistentSections.clear();
- if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
- if (d->orientation == Qt::Horizontal) {
- QObject::disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
- this, SLOT(sectionsInserted(QModelIndex,int,int)));
- QObject::disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int)));
- QObject::disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_sectionsRemoved(QModelIndex,int,int)));
- QObject::disconnect(d->model, SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
- QObject::disconnect(d->model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsMoved(QModelIndex,int,int,QModelIndex,int)));
- } else {
- QObject::disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(sectionsInserted(QModelIndex,int,int)));
- QObject::disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int)));
- QObject::disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_sectionsRemoved(QModelIndex,int,int)));
- QObject::disconnect(d->model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
- QObject::disconnect(d->model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsMoved(QModelIndex,int,int,QModelIndex,int)));
- }
- QObject::disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
- this, SLOT(headerDataChanged(Qt::Orientation,int,int)));
- QObject::disconnect(d->model, SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- this, SLOT(_q_sectionsAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
- QObject::disconnect(d->model, SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- this, SLOT(_q_sectionsChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
- }
+ if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel())
+ d->disconnectModel();
if (model && model != QAbstractItemModelPrivate::staticEmptyModel()) {
- if (d->orientation == Qt::Horizontal) {
- QObject::connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
- this, SLOT(sectionsInserted(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_sectionsRemoved(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
- QObject::connect(model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsMoved(QModelIndex,int,int,QModelIndex,int)));
- } else {
- QObject::connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(sectionsInserted(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
- this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_sectionsRemoved(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
- QObject::connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
- this, SLOT(_q_sectionsMoved(QModelIndex,int,int,QModelIndex,int)));
- }
- QObject::connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
- this, SLOT(headerDataChanged(Qt::Orientation,int,int)));
- QObject::connect(model, SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- this, SLOT(_q_sectionsAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
- QObject::connect(model, SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- this, SLOT(_q_sectionsChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
+ const bool hor = d->orientation == Qt::Horizontal;
+ d->modelConnections = {
+ QObject::connect(model, hor ? &QAbstractItemModel::columnsInserted
+ : &QAbstractItemModel::rowsInserted,
+ this, &QHeaderView::sectionsInserted),
+ QObject::connect(model, hor ? &QAbstractItemModel::columnsAboutToBeRemoved
+ : &QAbstractItemModel::rowsAboutToBeRemoved,
+ this, &QHeaderView::sectionsAboutToBeRemoved),
+ QObjectPrivate::connect(model, hor ? &QAbstractItemModel::columnsRemoved
+ : &QAbstractItemModel::rowsRemoved,
+ d, &QHeaderViewPrivate::sectionsRemoved),
+ QObjectPrivate::connect(model, hor ? &QAbstractItemModel::columnsAboutToBeMoved
+ : &QAbstractItemModel::rowsAboutToBeMoved,
+ d, &QHeaderViewPrivate::sectionsAboutToBeMoved),
+ QObjectPrivate::connect(model, hor ? &QAbstractItemModel::columnsMoved
+ : &QAbstractItemModel::rowsMoved,
+ d, &QHeaderViewPrivate::sectionsMoved),
+
+ QObject::connect(model, &QAbstractItemModel::headerDataChanged,
+ this, &QHeaderView::headerDataChanged),
+ QObjectPrivate::connect(model, &QAbstractItemModel::layoutAboutToBeChanged,
+ d, &QHeaderViewPrivate::sectionsAboutToBeChanged),
+ QObjectPrivate::connect(model, &QAbstractItemModel::layoutChanged,
+ d, &QHeaderViewPrivate::sectionsChanged)
+ };
}
d->state = QHeaderViewPrivate::NoClear;
@@ -451,7 +387,7 @@ Qt::Orientation QHeaderView::orientation() const
int QHeaderView::offset() const
{
Q_D(const QHeaderView);
- return d->offset;
+ return d->headerOffset;
}
/*!
@@ -465,10 +401,10 @@ int QHeaderView::offset() const
void QHeaderView::setOffset(int newOffset)
{
Q_D(QHeaderView);
- if (d->offset == (int)newOffset)
+ if (d->headerOffset == newOffset)
return;
- int ndelta = d->offset - newOffset;
- d->offset = newOffset;
+ int ndelta = d->headerOffset - newOffset;
+ d->headerOffset = newOffset;
if (d->orientation == Qt::Horizontal)
d->viewport->scroll(isRightToLeft() ? -ndelta : ndelta, 0);
else
@@ -624,7 +560,7 @@ int QHeaderView::visualIndexAt(int position) const
if (d->reverse())
vposition = d->viewport->width() - vposition - 1;
- vposition += d->offset;
+ vposition += d->headerOffset;
if (vposition > d->length)
return -1;
@@ -714,7 +650,7 @@ int QHeaderView::sectionViewportPosition(int logicalIndex) const
int position = sectionPosition(logicalIndex);
if (position < 0)
return position; // the section was hidden
- int offsetPosition = position - d->offset;
+ int offsetPosition = position - d->headerOffset;
if (d->reverse())
return d->viewport->width() - (offsetPosition + sectionSize(logicalIndex));
return offsetPosition;
@@ -1013,7 +949,7 @@ bool QHeaderView::isSectionHidden(int logicalIndex) const
int QHeaderView::hiddenSectionCount() const
{
Q_D(const QHeaderView);
- return d->hiddenSectionSize.count();
+ return d->hiddenSectionSize.size();
}
/*!
@@ -1094,7 +1030,7 @@ int QHeaderView::visualIndex(int logicalIndex) const
if (d->visualIndices.isEmpty()) { // nothing has been moved, so we have no mapping
if (logicalIndex < d->sectionCount())
return logicalIndex;
- } else if (logicalIndex < d->visualIndices.count()) {
+ } else if (logicalIndex < d->visualIndices.size()) {
int visual = d->visualIndices.at(logicalIndex);
Q_ASSERT(visual < d->sectionCount());
return visual;
@@ -1120,19 +1056,22 @@ int QHeaderView::logicalIndex(int visualIndex) const
}
/*!
- \since 5.0
+ \property QHeaderView::sectionsMovable
- If \a movable is true, the header sections may be moved by the user;
+ If \a sectionsMovable is true, the header sections may be moved by the user;
otherwise they are fixed in place.
When used in combination with QTreeView, the first column is not
movable (since it contains the tree structure), by default.
You can make it movable with setFirstSectionMovable(true).
- \sa sectionsMovable(), sectionMoved()
+ \sa sectionMoved()
\sa setFirstSectionMovable()
*/
+/*!
+ Sets \l sectionsMovable to \a movable.
+ */
void QHeaderView::setSectionsMovable(bool movable)
{
Q_D(QHeaderView);
@@ -1140,17 +1079,8 @@ void QHeaderView::setSectionsMovable(bool movable)
}
/*!
- \since 5.0
-
- Returns \c true if the header can be moved by the user; otherwise returns
- false.
-
- By default, sections are movable in QTreeView (except for the first one),
- and not movable in QTableView.
-
- \sa setSectionsMovable()
+ Returns \l sectionsMovable.
*/
-
bool QHeaderView::sectionsMovable() const
{
Q_D(const QHeaderView);
@@ -1170,6 +1100,11 @@ bool QHeaderView::sectionsMovable() const
In such a scenario, it is recommended to call QTreeView::setRootIsDecorated(false)
as well.
+ \code
+ treeView->setRootIsDecorated(false);
+ treeView->header()->setFirstSectionMovable(true);
+ \endcode
+
Setting it to true has no effect unless setSectionsMovable(true) is called
as well.
@@ -1189,14 +1124,17 @@ bool QHeaderView::isFirstSectionMovable() const
}
/*!
- \since 5.0
+ \property QHeaderView::sectionsClickable
- If \a clickable is true, the header will respond to single clicks.
+ Holds \c true if the header is clickable; otherwise \c false. A
+ clickable header could be set up to allow the user to change the
+ representation of the data in the view related to the header.
- \sa sectionsClickable(), sectionClicked(), sectionPressed(),
- setSortIndicatorShown()
+ \sa sectionPressed(), setSortIndicatorShown()
+*/
+/*!
+ Set \l sectionsClickable to \a clickable.
*/
-
void QHeaderView::setSectionsClickable(bool clickable)
{
Q_D(QHeaderView);
@@ -1204,15 +1142,8 @@ void QHeaderView::setSectionsClickable(bool clickable)
}
/*!
- \since 5.0
-
- Returns \c true if the header is clickable; otherwise returns \c false. A
- clickable header could be set up to allow the user to change the
- representation of the data in the view related to the header.
-
- \sa setSectionsClickable()
+ Returns \l sectionsClickable.
*/
-
bool QHeaderView::sectionsClickable() const
{
Q_D(const QHeaderView);
@@ -1465,6 +1396,41 @@ Qt::SortOrder QHeaderView::sortIndicatorOrder() const
}
/*!
+ \property QHeaderView::sortIndicatorClearable
+ \brief Whether the sort indicator can be cleared by clicking on a section multiple times
+ \since 6.1
+
+ This property controls whether the user is able to remove the
+ sorting indicator on a given section by clicking on the section
+ multiple times. Normally, clicking on a section will simply change
+ the sorting order for that section. By setting this property to
+ true, the sorting indicator will be cleared after alternating to
+ ascending and descending; this will typically restore the original
+ sorting of a model.
+
+ Setting this property to true has no effect unless
+ sectionsClickable() is also true (which is the default for certain
+ views, for instance QTableView, or is automatically set when making
+ a view sortable, for instance by calling
+ QTreeView::setSortingEnabled).
+*/
+
+void QHeaderView::setSortIndicatorClearable(bool clearable)
+{
+ Q_D(QHeaderView);
+ if (d->sortIndicatorClearable == clearable)
+ return;
+ d->sortIndicatorClearable = clearable;
+ emit sortIndicatorClearableChanged(clearable);
+}
+
+bool QHeaderView::isSortIndicatorClearable() const
+{
+ Q_D(const QHeaderView);
+ return d->sortIndicatorClearable;
+}
+
+/*!
\property QHeaderView::stretchLastSection
\brief whether the last visible section in the header takes up all the
available space
@@ -1570,8 +1536,7 @@ void QHeaderView::resetDefaultSectionSize()
\brief the minimum size of the header sections.
The minimum section size is the smallest section size allowed. If the
- minimum section size is set to -1, QHeaderView will use the maximum of
- the \l{QApplication::globalStrut()}{global strut} or the
+ minimum section size is set to -1, QHeaderView will use the
\l{fontMetrics()}{font metrics} size.
This property is honored by all \l{ResizeMode}{resize modes}.
@@ -1582,11 +1547,10 @@ int QHeaderView::minimumSectionSize() const
{
Q_D(const QHeaderView);
if (d->minimumSectionSize == -1) {
- QSize strut = QApplication::globalStrut();
- int margin = 2 * style()->pixelMetric(QStyle::PM_HeaderMargin, 0, this);
+ int margin = 2 * style()->pixelMetric(QStyle::PM_HeaderMargin, nullptr, this);
if (d->orientation == Qt::Horizontal)
- return qMax(strut.width(), (fontMetrics().maxWidth() + margin));
- return qMax(strut.height(), (fontMetrics().height() + margin));
+ return fontMetrics().maxWidth() + margin;
+ return fontMetrics().height() + margin;
}
return d->minimumSectionSize;
}
@@ -1742,6 +1706,7 @@ QByteArray QHeaderView::saveState() const
Q_D(const QHeaderView);
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
+ stream.setVersion(QDataStream::Qt_5_0);
stream << QHeaderViewPrivate::VersionMarker;
stream << 0; // current version is 0
d->write(stream);
@@ -1761,21 +1726,27 @@ bool QHeaderView::restoreState(const QByteArray &state)
Q_D(QHeaderView);
if (state.isEmpty())
return false;
- QByteArray data = state;
- QDataStream stream(&data, QIODevice::ReadOnly);
- int marker;
- int ver;
- stream >> marker;
- stream >> ver;
- if (stream.status() != QDataStream::Ok
+
+ for (const auto dataStreamVersion : {QDataStream::Qt_5_0, QDataStream::Qt_6_0}) {
+
+ QByteArray data = state;
+ QDataStream stream(&data, QIODevice::ReadOnly);
+ stream.setVersion(dataStreamVersion);
+ int marker;
+ int ver;
+ stream >> marker;
+ stream >> ver;
+ if (stream.status() != QDataStream::Ok
|| marker != QHeaderViewPrivate::VersionMarker
- || ver != 0) // current version is 0
- return false;
+ || ver != 0) { // current version is 0
+ return false;
+ }
- if (d->read(stream)) {
- emit sortIndicatorChanged(d->sortIndicatorSection, d->sortIndicatorOrder );
- d->viewport->update();
- return true;
+ if (d->read(stream)) {
+ emit sortIndicatorChanged(d->sortIndicatorSection, d->sortIndicatorOrder );
+ d->viewport->update();
+ return true;
+ }
}
return false;
}
@@ -1873,8 +1844,9 @@ void QHeaderView::sectionsInserted(const QModelIndex &parent,
int logicalFirst, int logicalLast)
{
Q_D(QHeaderView);
- if (parent != d->root)
- return; // we only handle changes in the root level
+ // only handle root level changes and return on no-op
+ if (parent != d->root || d->modelSectionCount() == d->sectionCount())
+ return;
int oldCount = d->sectionCount();
d->invalidateCachedSizeHint();
@@ -1904,10 +1876,10 @@ void QHeaderView::sectionsInserted(const QModelIndex &parent,
QHeaderViewPrivate::SectionItem section(d->defaultSectionSize, d->globalResizeMode);
d->sectionStartposRecalc = true;
- if (d->sectionItems.isEmpty() || insertAt >= d->sectionItems.count()) {
+ if (d->sectionItems.isEmpty() || insertAt >= d->sectionItems.size()) {
int insertLength = d->defaultSectionSize * insertCount;
d->length += insertLength;
- d->sectionItems.insert(d->sectionItems.count(), insertCount, section); // append
+ d->sectionItems.insert(d->sectionItems.size(), insertCount, section); // append
} else {
// separate them out into their own sections
int insertLength = d->defaultSectionSize * insertCount;
@@ -1930,8 +1902,8 @@ void QHeaderView::sectionsInserted(const QModelIndex &parent,
// update mapping
if (!d->visualIndices.isEmpty() && !d->logicalIndices.isEmpty()) {
- Q_ASSERT(d->visualIndices.count() == d->logicalIndices.count());
- int mappingCount = d->visualIndices.count();
+ Q_ASSERT(d->visualIndices.size() == d->logicalIndices.size());
+ int mappingCount = d->visualIndices.size();
for (int i = 0; i < mappingCount; ++i) {
if (d->visualIndices.at(i) >= logicalFirst)
d->visualIndices[i] += insertCount;
@@ -1997,8 +1969,8 @@ void QHeaderViewPrivate::updateHiddenSections(int logicalFirst, int logicalLast)
hiddenSectionSize = newHiddenSectionSize;
}
-void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent,
- int logicalFirst, int logicalLast)
+void QHeaderViewPrivate::sectionsRemoved(const QModelIndex &parent,
+ int logicalFirst, int logicalLast)
{
Q_Q(QHeaderView);
if (parent != root)
@@ -2021,7 +1993,7 @@ void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent,
if (logicalFirst == logicalLast) { // Remove just one index.
int l = logicalFirst;
int visual = visualIndices.at(l);
- Q_ASSERT(sectionCount() == logicalIndices.count());
+ Q_ASSERT(sectionCount() == logicalIndices.size());
for (int v = 0; v < sectionCount(); ++v) {
if (v > visual) {
int logical = logicalIndices.at(v);
@@ -2036,17 +2008,17 @@ void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent,
removeSectionsFromSectionItems(visual, visual);
} else {
sectionStartposRecalc = true; // We will need to recalc positions after removing items
- for (int u = 0; u < sectionItems.count(); ++u) // Store section info
+ for (int u = 0; u < sectionItems.size(); ++u) // Store section info
sectionItems.at(u).tmpLogIdx = logicalIndices.at(u);
- for (int v = sectionItems.count() - 1; v >= 0; --v) { // Remove the sections
+ for (int v = sectionItems.size() - 1; v >= 0; --v) { // Remove the sections
if (logicalFirst <= sectionItems.at(v).tmpLogIdx && sectionItems.at(v).tmpLogIdx <= logicalLast)
removeSectionsFromSectionItems(v, v);
}
- visualIndices.resize(sectionItems.count());
- logicalIndices.resize(sectionItems.count());
+ visualIndices.resize(sectionItems.size());
+ logicalIndices.resize(sectionItems.size());
int* visual_data = visualIndices.data();
int* logical_data = logicalIndices.data();
- for (int w = 0; w < sectionItems.count(); ++w) { // Restore visual and logical indexes
+ for (int w = 0; w < sectionItems.size(); ++w) { // Restore visual and logical indexes
int logindex = sectionItems.at(w).tmpLogIdx;
if (logindex > logicalFirst)
logindex -= changeCount;
@@ -2083,28 +2055,32 @@ void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent,
viewport->update();
}
-void QHeaderViewPrivate::_q_sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination)
+void QHeaderViewPrivate::sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart,
+ int logicalEnd, const QModelIndex &destinationParent,
+ int logicalDestination)
{
if (sourceParent != root || destinationParent != root)
return; // we only handle changes in the root level
Q_UNUSED(logicalStart);
Q_UNUSED(logicalEnd);
Q_UNUSED(logicalDestination);
- _q_sectionsAboutToBeChanged();
+ sectionsAboutToBeChanged();
}
-void QHeaderViewPrivate::_q_sectionsMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination)
+void QHeaderViewPrivate::sectionsMoved(const QModelIndex &sourceParent, int logicalStart,
+ int logicalEnd, const QModelIndex &destinationParent,
+ int logicalDestination)
{
if (sourceParent != root || destinationParent != root)
return; // we only handle changes in the root level
Q_UNUSED(logicalStart);
Q_UNUSED(logicalEnd);
Q_UNUSED(logicalDestination);
- _q_sectionsChanged();
+ sectionsChanged();
}
-void QHeaderViewPrivate::_q_sectionsAboutToBeChanged(const QList<QPersistentModelIndex> &,
- QAbstractItemModel::LayoutChangeHint hint)
+void QHeaderViewPrivate::sectionsAboutToBeChanged(const QList<QPersistentModelIndex> &,
+ QAbstractItemModel::LayoutChangeHint hint)
{
if ((hint == QAbstractItemModel::VerticalSortHint && orientation == Qt::Horizontal) ||
(hint == QAbstractItemModel::HorizontalSortHint && orientation == Qt::Vertical))
@@ -2119,9 +2095,9 @@ void QHeaderViewPrivate::_q_sectionsAboutToBeChanged(const QList<QPersistentMode
return;
layoutChangePersistentSections.clear();
- layoutChangePersistentSections.reserve(std::min(10, sectionItems.count()));
+ layoutChangePersistentSections.reserve(std::min(10, int(sectionItems.size())));
// after layoutChanged another section can be last stretched section
- if (stretchLastSection && lastSectionLogicalIdx >= 0 && lastSectionLogicalIdx < sectionItems.count()) {
+ if (stretchLastSection && lastSectionLogicalIdx >= 0 && lastSectionLogicalIdx < sectionItems.size()) {
const int visual = visualIndex(lastSectionLogicalIdx);
if (visual >= 0 && visual < sectionItems.size()) {
auto &itemRef = sectionItems[visual];
@@ -2149,8 +2125,8 @@ void QHeaderViewPrivate::_q_sectionsAboutToBeChanged(const QList<QPersistentMode
}
}
-void QHeaderViewPrivate::_q_sectionsChanged(const QList<QPersistentModelIndex> &,
- QAbstractItemModel::LayoutChangeHint hint)
+void QHeaderViewPrivate::sectionsChanged(const QList<QPersistentModelIndex> &,
+ QAbstractItemModel::LayoutChangeHint hint)
{
if ((hint == QAbstractItemModel::VerticalSortHint && orientation == Qt::Horizontal) ||
(hint == QAbstractItemModel::HorizontalSortHint && orientation == Qt::Vertical))
@@ -2180,7 +2156,7 @@ void QHeaderViewPrivate::_q_sectionsChanged(const QList<QPersistentModelIndex> &
}
// Though far from perfect we here try to retain earlier/existing behavior
- // ### See QHeaderViewPrivate::_q_layoutAboutToBeChanged()
+ // ### See QHeaderViewPrivate::layoutAboutToBeChanged()
// When we don't have valid hasPersistantIndexes it can be due to
// - all sections are default sections
// - the row/column 0 which is used for persistent indexes is gone
@@ -2216,7 +2192,7 @@ void QHeaderViewPrivate::_q_sectionsChanged(const QList<QPersistentModelIndex> &
: index.row());
// the new visualIndices are already adjusted / reset by initializeSections()
const int newVisualIndex = visualIndex(newLogicalIndex);
- if (newVisualIndex < sectionItems.count()) {
+ if (newVisualIndex < sectionItems.size()) {
auto &newSection = sectionItems[newVisualIndex];
newSection = item.section;
@@ -2281,7 +2257,7 @@ void QHeaderView::initializeSections(int start, int end)
int newCount = end + 1;
d->removeSectionsFromSectionItems(newCount, d->sectionCount() - 1);
if (!d->hiddenSectionSize.isEmpty()) {
- if (oldCount - newCount > d->hiddenSectionSize.count()) {
+ if (oldCount - newCount > d->hiddenSectionSize.size()) {
for (int i = end + 1; i < d->sectionCount(); ++i)
d->hiddenSectionSize.remove(i);
} else {
@@ -2327,7 +2303,7 @@ void QHeaderView::initializeSections(int start, int end)
d->contentsSections = newSectionCount;
if (newSectionCount > oldCount)
- d->createSectionItems(start, end, (end - start + 1) * d->defaultSectionSize, d->globalResizeMode);
+ d->createSectionItems(start, end, d->defaultSectionSize, d->globalResizeMode);
//Q_ASSERT(d->headerLength() == d->length);
if (d->sectionCount() != oldCount)
@@ -2371,7 +2347,7 @@ bool QHeaderView::event(QEvent *e)
switch (e->type()) {
case QEvent::HoverEnter: {
QHoverEvent *he = static_cast<QHoverEvent*>(e);
- d->hover = logicalIndexAt(he->pos());
+ d->hover = logicalIndexAt(he->position().toPoint());
if (d->hover != -1)
updateSection(d->hover);
break; }
@@ -2384,7 +2360,7 @@ bool QHeaderView::event(QEvent *e)
case QEvent::HoverMove: {
QHoverEvent *he = static_cast<QHoverEvent*>(e);
int oldHover = d->hover;
- d->hover = logicalIndexAt(he->pos());
+ d->hover = logicalIndexAt(he->position().toPoint());
if (d->hover != oldHover) {
if (oldHover != -1)
updateSection(oldHover);
@@ -2478,7 +2454,7 @@ void QHeaderView::paintEvent(QPaintEvent *e)
}
QStyleOption opt;
- opt.init(this);
+ opt.initFrom(this);
// Paint the area beyond where there are indexes
if (d->reverse()) {
opt.state |= QStyle::State_Horizontal;
@@ -2506,9 +2482,9 @@ void QHeaderView::paintEvent(QPaintEvent *e)
for (int a = 0, i = 0; i < d->sectionItems.count(); ++i) {
QColor color((i & 4 ? 255 : 0), (i & 2 ? 255 : 0), (i & 1 ? 255 : 0));
if (d->orientation == Qt::Horizontal)
- painter.fillRect(a - d->offset, 0, d->sectionItems.at(i).size, 4, color);
+ painter.fillRect(a - d->headerOffset, 0, d->sectionItems.at(i).size, 4, color);
else
- painter.fillRect(0, a - d->offset, 4, d->sectionItems.at(i).size, color);
+ painter.fillRect(0, a - d->headerOffset, 4, d->sectionItems.at(i).size, color);
a += d->sectionItems.at(i).size;
}
@@ -2524,11 +2500,11 @@ void QHeaderView::mousePressEvent(QMouseEvent *e)
Q_D(QHeaderView);
if (d->state != QHeaderViewPrivate::NoState || e->button() != Qt::LeftButton)
return;
- int pos = d->orientation == Qt::Horizontal ? e->x() : e->y();
+ int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y();
int handle = d->sectionHandleAt(pos);
d->originalSize = -1; // clear the stored original size
if (handle == -1) {
- d->pressed = logicalIndexAt(pos);
+ d->firstPressed = d->pressed = logicalIndexAt(pos);
if (d->clickableSections)
emit sectionPressed(d->pressed);
@@ -2537,7 +2513,8 @@ void QHeaderView::mousePressEvent(QMouseEvent *e)
acceptMoveSection = false; // Do not allow moving the tree nod
if (acceptMoveSection) {
- d->section = d->target = d->pressed;
+ d->target = -1;
+ d->section = d->pressed;
if (d->section == -1)
return;
d->state = QHeaderViewPrivate::MoveSection;
@@ -2566,7 +2543,7 @@ void QHeaderView::mousePressEvent(QMouseEvent *e)
void QHeaderView::mouseMoveEvent(QMouseEvent *e)
{
Q_D(QHeaderView);
- int pos = d->orientation == Qt::Horizontal ? e->x() : e->y();
+ const int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y();
if (pos < 0 && d->state != QHeaderViewPrivate::SelectSections)
return;
if (e->buttons() == Qt::NoButton) {
@@ -2576,7 +2553,7 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
// just before the mouseReleaseEvent and resets the state. This prevents
// column dragging from working. So this code is disabled under Cocoa.
d->state = QHeaderViewPrivate::NoState;
- d->pressed = -1;
+ d->firstPressed = d->pressed = -1;
}
switch (d->state) {
case QHeaderViewPrivate::ResizeSection: {
@@ -2594,8 +2571,10 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
return;
}
case QHeaderViewPrivate::MoveSection: {
- if (d->shouldAutoScroll(e->pos()))
+ if (d->shouldAutoScroll(e->position().toPoint())) {
+ d->draggedPosition = e->pos() + d->offset();
d->startAutoScroll();
+ }
if (qAbs(pos - d->firstPos) >= QApplication::startDragDistance()
#if QT_CONFIG(label)
|| !d->sectionIndicator->isHidden()
@@ -2607,27 +2586,31 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
if (visual == 0 && logicalIndex(0) == 0 && !d->allowUserMoveOfSection0)
return;
- int posThreshold = d->headerSectionPosition(visual) - d->offset + d->headerSectionSize(visual) / 2;
+ const int posThreshold = d->headerSectionPosition(visual) - d->headerOffset + d->headerSectionSize(visual) / 2;
+ const int checkPos = d->reverse() ? d->viewport->width() - pos : pos;
int moving = visualIndex(d->section);
+ int oldTarget = d->target;
if (visual < moving) {
- if (pos < posThreshold)
+ if (checkPos < posThreshold)
d->target = d->logicalIndex(visual);
else
d->target = d->logicalIndex(visual + 1);
} else if (visual > moving) {
- if (pos > posThreshold)
+ if (checkPos > posThreshold)
d->target = d->logicalIndex(visual);
else
d->target = d->logicalIndex(visual - 1);
} else {
d->target = d->section;
}
+ if (oldTarget != d->target || oldTarget == -1)
+ d->updateSectionsBeforeAfter(d->target);
d->updateSectionIndicator(d->section, pos);
}
return;
}
case QHeaderViewPrivate::SelectSections: {
- int logical = logicalIndexAt(qMax(-d->offset, pos));
+ int logical = logicalIndexAt(qMax(-d->headerOffset, pos));
if (logical == -1 && pos > 0)
logical = logicalIndex(d->lastVisibleVisualIndex());
if (logical == d->pressed)
@@ -2678,7 +2661,7 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
void QHeaderView::mouseReleaseEvent(QMouseEvent *e)
{
Q_D(QHeaderView);
- int pos = d->orientation == Qt::Horizontal ? e->x() : e->y();
+ int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y();
switch (d->state) {
case QHeaderViewPrivate::MoveSection:
if (true
@@ -2693,6 +2676,8 @@ void QHeaderView::mouseReleaseEvent(QMouseEvent *e)
moveSection(from, to);
d->section = d->target = -1;
d->updateSectionIndicator(d->section, pos);
+ if (from == to)
+ d->updateSectionsBeforeAfter(from);
break;
} // not moving
Q_FALLTHROUGH();
@@ -2705,9 +2690,27 @@ void QHeaderView::mouseReleaseEvent(QMouseEvent *e)
case QHeaderViewPrivate::NoState:
if (d->clickableSections) {
int section = logicalIndexAt(pos);
- if (section != -1 && section == d->pressed) {
- d->flipSortIndicator(section);
- emit sectionClicked(section);
+ if (section != -1 && section == d->firstPressed) {
+ QRect firstPressedSectionRect;
+ switch (d->orientation) {
+ case Qt::Horizontal:
+ firstPressedSectionRect.setRect(sectionViewportPosition(d->firstPressed),
+ 0,
+ sectionSize(d->firstPressed),
+ d->viewport->height());
+ break;
+ case Qt::Vertical:
+ firstPressedSectionRect.setRect(0,
+ sectionViewportPosition(d->firstPressed),
+ d->viewport->width(),
+ sectionSize(d->firstPressed));
+ break;
+ };
+
+ if (firstPressedSectionRect.contains(e->position().toPoint())) {
+ d->flipSortIndicator(section);
+ emit sectionClicked(section);
+ }
}
if (d->pressed != -1)
updateSection(d->pressed);
@@ -2721,7 +2724,7 @@ void QHeaderView::mouseReleaseEvent(QMouseEvent *e)
break;
}
d->state = QHeaderViewPrivate::NoState;
- d->pressed = -1;
+ d->firstPressed = d->pressed = -1;
}
/*!
@@ -2731,7 +2734,7 @@ void QHeaderView::mouseReleaseEvent(QMouseEvent *e)
void QHeaderView::mouseDoubleClickEvent(QMouseEvent *e)
{
Q_D(QHeaderView);
- int pos = d->orientation == Qt::Horizontal ? e->x() : e->y();
+ int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y();
int handle = d->sectionHandleAt(pos);
if (handle > -1 && sectionResizeMode(handle) == Interactive) {
emit sectionHandleDoubleClicked(handle);
@@ -2746,7 +2749,7 @@ void QHeaderView::mouseDoubleClickEvent(QMouseEvent *e)
}
#endif
} else {
- emit sectionDoubleClicked(logicalIndexAt(e->pos()));
+ emit sectionDoubleClicked(logicalIndexAt(e->position().toPoint()));
}
}
@@ -2758,7 +2761,7 @@ bool QHeaderView::viewportEvent(QEvent *e)
{
Q_D(QHeaderView);
switch (e->type()) {
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
case QEvent::ToolTip: {
QHelpEvent *he = static_cast<QHelpEvent*>(e);
int logical = logicalIndexAt(he->pos());
@@ -2833,23 +2836,26 @@ bool QHeaderView::viewportEvent(QEvent *e)
}
/*!
- Paints the section specified by the given \a logicalIndex, using the given
- \a painter and \a rect.
+ \fn void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const
+ \since 6.0
- Normally, you do not have to call this function.
+ Initializes the style \a option from the specified \a logicalIndex.
+ This function is called by the default implementation of paintSection after
+ initStyleOption has been called.
+
+ \sa paintSection(), initStyleOption()
*/
-void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
+void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const
{
Q_D(const QHeaderView);
- if (!rect.isValid())
+
+ if (!option)
return;
- // get the state of the section
- QStyleOptionHeader opt;
- initStyleOption(&opt);
+ QStyleOptionHeader &opt = *option;
+ QStyleOptionHeaderV2 *optV2 = qstyleoption_cast<QStyleOptionHeaderV2*>(option);
+
QStyle::State state = QStyle::State_None;
- if (isEnabled())
- state |= QStyle::State_Enabled;
if (window()->isActiveWindow())
state |= QStyle::State_Active;
if (d->clickableSections) {
@@ -2863,7 +2869,6 @@ void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logical
if (d->isSectionSelected(logicalIndex))
state |= QStyle::State_Sunken;
}
-
}
if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex)
opt.sortIndicator = (sortIndicatorOrder() == Qt::AscendingOrder)
@@ -2872,50 +2877,39 @@ void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logical
// setup the style options structure
QVariant textAlignment = d->model->headerData(logicalIndex, d->orientation,
Qt::TextAlignmentRole);
- opt.rect = rect;
opt.section = logicalIndex;
opt.state |= state;
- opt.textAlignment = Qt::Alignment(textAlignment.isValid()
- ? Qt::Alignment(textAlignment.toInt())
- : d->defaultAlignment);
+ opt.textAlignment = textAlignment.isValid()
+ ? QtPrivate::legacyFlagValueFromModelData<Qt::Alignment>(textAlignment)
+ : d->defaultAlignment;
opt.iconAlignment = Qt::AlignVCenter;
opt.text = d->model->headerData(logicalIndex, d->orientation,
Qt::DisplayRole).toString();
- int margin = 2 * style()->pixelMetric(QStyle::PM_HeaderMargin, 0, this);
-
- const Qt::Alignment headerArrowAlignment = static_cast<Qt::Alignment>(style()->styleHint(QStyle::SH_Header_ArrowAlignment, 0, this));
- const bool isHeaderArrowOnTheSide = headerArrowAlignment & Qt::AlignVCenter;
- if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex && isHeaderArrowOnTheSide)
- margin += style()->pixelMetric(QStyle::PM_HeaderMarkSize, 0, this);
-
const QVariant variant = d->model->headerData(logicalIndex, d->orientation,
Qt::DecorationRole);
opt.icon = qvariant_cast<QIcon>(variant);
if (opt.icon.isNull())
opt.icon = qvariant_cast<QPixmap>(variant);
- if (!opt.icon.isNull()) // see CT_HeaderSection
- margin += style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this) +
- style()->pixelMetric(QStyle::PM_HeaderMargin, 0, this);
- if (d->textElideMode != Qt::ElideNone) {
- const QRect textRect = style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
- opt.text = opt.fontMetrics.elidedText(opt.text, d->textElideMode, textRect.width() - margin);
- }
+ QVariant var = d->model->headerData(logicalIndex, d->orientation,
+ Qt::FontRole);
+ if (var.isValid() && var.canConvert<QFont>())
+ opt.fontMetrics = QFontMetrics(qvariant_cast<QFont>(var));
+ if (optV2)
+ optV2->textElideMode = d->textElideMode;
QVariant foregroundBrush = d->model->headerData(logicalIndex, d->orientation,
Qt::ForegroundRole);
if (foregroundBrush.canConvert<QBrush>())
opt.palette.setBrush(QPalette::ButtonText, qvariant_cast<QBrush>(foregroundBrush));
- QPointF oldBO = painter->brushOrigin();
QVariant backgroundBrush = d->model->headerData(logicalIndex, d->orientation,
Qt::BackgroundRole);
if (backgroundBrush.canConvert<QBrush>()) {
opt.palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
opt.palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
- painter->setBrushOrigin(opt.rect.topLeft());
}
// the section position
@@ -2943,9 +2937,44 @@ void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logical
opt.selectedPosition = QStyleOptionHeader::NextIsSelected;
else
opt.selectedPosition = QStyleOptionHeader::NotAdjacent;
- // draw the section
- style()->drawControl(QStyle::CE_Header, &opt, painter, this);
+ if (optV2)
+ optV2->isSectionDragTarget = d->target == logicalIndex;
+}
+
+/*!
+ Paints the section specified by the given \a logicalIndex, using the given
+ \a painter and \a rect.
+
+ Normally, you do not have to call this function.
+*/
+
+void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
+{
+ if (!rect.isValid())
+ return;
+
+ QStyleOptionHeaderV2 opt;
+ QPointF oldBO = painter->brushOrigin();
+
+ initStyleOption(&opt);
+ QBrush oBrushButton = opt.palette.brush(QPalette::Button);
+ QBrush oBrushWindow = opt.palette.brush(QPalette::Window);
+
+ initStyleOptionForIndex(&opt, logicalIndex);
+ // We set rect here. If it needs to be changed it can be changed by overriding this function
+ opt.rect = rect;
+
+ QBrush nBrushButton = opt.palette.brush(QPalette::Button);
+ QBrush nBrushWindow = opt.palette.brush(QPalette::Window);
+
+ // If relevant brushes are not the same as from the regular widgets we set the brush origin
+ if (oBrushButton != nBrushButton || oBrushWindow != nBrushWindow) {
+ painter->setBrushOrigin(opt.rect.topLeft());
+ }
+
+ // draw the section.
+ style()->drawControl(QStyle::CE_Header, &opt, painter, this);
painter->setBrushOrigin(oldBO);
}
@@ -2969,7 +2998,7 @@ QSize QHeaderView::sectionSizeFromContents(int logicalIndex) const
return qvariant_cast<QSize>(variant);
// otherwise use the contents
- QStyleOptionHeader opt;
+ QStyleOptionHeaderV2 opt;
initStyleOption(&opt);
opt.section = logicalIndex;
QVariant var = d->model->headerData(logicalIndex, d->orientation,
@@ -3003,7 +3032,7 @@ int QHeaderView::horizontalOffset() const
{
Q_D(const QHeaderView);
if (d->orientation == Qt::Horizontal)
- return d->offset;
+ return d->headerOffset;
return 0;
}
@@ -3018,7 +3047,7 @@ int QHeaderView::verticalOffset() const
{
Q_D(const QHeaderView);
if (d->orientation == Qt::Vertical)
- return d->offset;
+ return d->headerOffset;
return 0;
}
@@ -3050,7 +3079,8 @@ void QHeaderView::scrollContentsBy(int dx, int dy)
\reimp
\internal
*/
-void QHeaderView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
+void QHeaderView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles)
{
Q_D(QHeaderView);
if (!roles.isEmpty()) {
@@ -3275,7 +3305,7 @@ int QHeaderViewPrivate::sectionHandleAt(int position)
return -1;
int log = logicalIndex(visual);
int pos = q->sectionViewportPosition(log);
- int grip = q->style()->pixelMetric(QStyle::PM_HeaderGripMargin, 0, q);
+ int grip = q->style()->pixelMetric(QStyle::PM_HeaderGripMargin, nullptr, q);
bool atLeft = position < pos + grip;
bool atRight = (position > pos + q->sectionSize(log) - grip);
@@ -3318,7 +3348,7 @@ void QHeaderViewPrivate::setupSectionIndicator(int section, int position)
sectionIndicator->resize(w, h);
#endif
- const qreal pixmapDevicePixelRatio = q->devicePixelRatioF();
+ const qreal pixmapDevicePixelRatio = q->devicePixelRatio();
QPixmap pm(QSize(w, h) * pixmapDevicePixelRatio);
pm.setDevicePixelRatio(pixmapDevicePixelRatio);
pm.fill(QColor(0, 0, 0, 45));
@@ -3369,7 +3399,7 @@ void QHeaderViewPrivate::updateSectionIndicator(int section, int position)
useful for subclasses when they need a QStyleOptionHeader, but do not want
to fill in all the information themselves.
- \sa QStyleOption::initFrom()
+ \sa QStyleOption::initFrom(), initStyleOptionForIndex()
*/
void QHeaderView::initStyleOption(QStyleOptionHeader *option) const
{
@@ -3384,10 +3414,17 @@ void QHeaderView::initStyleOption(QStyleOptionHeader *option) const
option->section = 0;
}
+void QHeaderView::initStyleOption(QStyleOptionFrame *option) const
+{
+ // The QFrame version is only here to avoid compiler warnings.
+ // If invoked we just pass it on to the base class.
+ QFrame::initStyleOption(option);
+}
+
bool QHeaderViewPrivate::isSectionSelected(int section) const
{
int i = section * 2;
- if (i < 0 || i >= sectionSelected.count())
+ if (i < 0 || i >= sectionSelected.size())
return false;
if (sectionSelected.testBit(i)) // if the value was cached
return sectionSelected.testBit(i + 1);
@@ -3590,8 +3627,7 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool
//Q_ASSERT(newSectionLength > 0);
if ((previousSectionResizeMode != newSectionResizeMode
|| previousSectionLength != newSectionLength) && i > 0) {
- int spanLength = (i - spanStartSection) * previousSectionLength;
- createSectionItems(spanStartSection, i - 1, spanLength, previousSectionResizeMode);
+ createSectionItems(spanStartSection, i - 1, previousSectionLength, previousSectionResizeMode);
//Q_ASSERT(headerLength() == length);
spanStartSection = i;
}
@@ -3604,17 +3640,15 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool
}
createSectionItems(spanStartSection, sectionCount() - 1,
- (sectionCount() - spanStartSection) * previousSectionLength,
- previousSectionResizeMode);
+ previousSectionLength, previousSectionResizeMode);
//Q_ASSERT(headerLength() == length);
resizeRecursionBlock = false;
viewport->update();
}
-void QHeaderViewPrivate::createSectionItems(int start, int end, int size, QHeaderView::ResizeMode mode)
+void QHeaderViewPrivate::createSectionItems(int start, int end, int sizePerSection, QHeaderView::ResizeMode mode)
{
- int sizePerSection = size / (end - start + 1);
- if (end >= sectionItems.count()) {
+ if (end >= sectionItems.size()) {
sectionItems.resize(end + 1);
sectionStartposRecalc = true;
}
@@ -3630,7 +3664,7 @@ void QHeaderViewPrivate::createSectionItems(int start, int end, int size, QHeade
void QHeaderViewPrivate::removeSectionsFromSectionItems(int start, int end)
{
// remove sections
- sectionStartposRecalc |= (end != sectionItems.count() - 1);
+ sectionStartposRecalc |= (end != sectionItems.size() - 1);
int removedlength = 0;
for (int u = start; u <= end; ++u)
removedlength += sectionItems.at(u).size;
@@ -3652,22 +3686,47 @@ void QHeaderViewPrivate::clear()
}
}
+static Qt::SortOrder flipOrder(Qt::SortOrder order)
+{
+ switch (order) {
+ case Qt::AscendingOrder:
+ return Qt::DescendingOrder;
+ case Qt::DescendingOrder:
+ return Qt::AscendingOrder;
+ };
+ Q_UNREACHABLE_RETURN(Qt::AscendingOrder);
+};
+
void QHeaderViewPrivate::flipSortIndicator(int section)
{
Q_Q(QHeaderView);
Qt::SortOrder sortOrder;
if (sortIndicatorSection == section) {
- sortOrder = (sortIndicatorOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder;
+ if (sortIndicatorClearable) {
+ const Qt::SortOrder defaultSortOrder = defaultSortOrderForSection(section);
+ if (sortIndicatorOrder == defaultSortOrder) {
+ sortOrder = flipOrder(sortIndicatorOrder);
+ } else {
+ section = -1;
+ sortOrder = Qt::AscendingOrder;
+ }
+ } else {
+ sortOrder = flipOrder(sortIndicatorOrder);
+ }
} else {
- const QVariant value = model->headerData(section, orientation, Qt::InitialSortOrderRole);
- if (value.canConvert(QVariant::Int))
- sortOrder = static_cast<Qt::SortOrder>(value.toInt());
- else
- sortOrder = Qt::AscendingOrder;
+ sortOrder = defaultSortOrderForSection(section);
}
q->setSortIndicator(section, sortOrder);
}
+Qt::SortOrder QHeaderViewPrivate::defaultSortOrderForSection(int section) const
+{
+ const QVariant value = model->headerData(section, orientation, Qt::InitialSortOrderRole);
+ if (value.canConvert<int>())
+ return static_cast<Qt::SortOrder>(value.toInt());
+ return Qt::AscendingOrder;
+}
+
void QHeaderViewPrivate::cascadingResize(int visual, int newSize)
{
Q_Q(QHeaderView);
@@ -3712,12 +3771,9 @@ void QHeaderViewPrivate::cascadingResize(int visual, int newSize)
if (currentSectionSize <= minimumSize)
continue;
int newSectionSize = qMax(currentSectionSize - delta, minimumSize);
- //qDebug() << "### cascading to" << i << newSectionSize - currentSectionSize << delta;
resizeSectionItem(i, currentSectionSize, newSectionSize);
saveCascadingSectionSize(i, currentSectionSize);
delta = delta - (currentSectionSize - newSectionSize);
- //qDebug() << "new delta" << delta;
- //if (newSectionSize != minimumSize)
if (delta <= 0)
break;
}
@@ -3735,7 +3791,6 @@ void QHeaderViewPrivate::cascadingResize(int visual, int newSize)
int newSectionSize = currentSectionSize - delta;
resizeSectionItem(i, currentSectionSize, newSectionSize);
if (newSectionSize >= originalSectionSize && false) {
- //qDebug() << "section" << i << "restored to" << originalSectionSize;
cascadingSectionSize.remove(i); // the section is now restored
}
sectionResized = true;
@@ -3792,7 +3847,7 @@ void QHeaderViewPrivate::setDefaultSectionSize(int size)
customDefaultSectionSize = true;
if (state == QHeaderViewPrivate::ResizeSection)
preventCursorChangeInSetOffset = true;
- for (int i = 0; i < sectionItems.count(); ++i) {
+ for (int i = 0; i < sectionItems.size(); ++i) {
QHeaderViewPrivate::SectionItem &section = sectionItems[i];
if (hiddenSectionSize.isEmpty() || !isVisualIndexHidden(i)) { // resize on not hidden.
const int newSize = size;
@@ -3814,10 +3869,10 @@ void QHeaderViewPrivate::updateDefaultSectionSizeFromStyle()
{
Q_Q(QHeaderView);
if (orientation == Qt::Horizontal) {
- defaultSectionSize = q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeHorizontal, 0, q);
+ defaultSectionSize = q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeHorizontal, nullptr, q);
} else {
defaultSectionSize = qMax(q->minimumSectionSize(),
- q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeVertical, 0, q));
+ q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeVertical, nullptr, q));
}
}
@@ -3861,7 +3916,7 @@ int QHeaderViewPrivate::headerVisualIndexAt(int position) const
if (sectionStartposRecalc)
recalcSectionStartPos();
int startidx = 0;
- int endidx = sectionItems.count() - 1;
+ int endidx = sectionItems.size() - 1;
while (startidx <= endidx) {
int middle = (endidx + startidx) / 2;
if (sectionItems.at(middle).calculated_startpos > position) {
@@ -3884,7 +3939,7 @@ void QHeaderViewPrivate::setHeaderSectionResizeMode(int visual, QHeaderView::Res
QHeaderView::ResizeMode QHeaderViewPrivate::headerSectionResizeMode(int visual) const
{
- if (visual < 0 || visual >= sectionItems.count())
+ if (visual < 0 || visual >= sectionItems.size())
return globalResizeMode;
return static_cast<QHeaderView::ResizeMode>(sectionItems.at(visual).resizeMode);
}
@@ -3892,7 +3947,7 @@ QHeaderView::ResizeMode QHeaderViewPrivate::headerSectionResizeMode(int visual)
void QHeaderViewPrivate::setGlobalHeaderResizeMode(QHeaderView::ResizeMode mode)
{
globalResizeMode = mode;
- for (int i = 0; i < sectionItems.count(); ++i)
+ for (int i = 0; i < sectionItems.size(); ++i)
sectionItems[i].resizeMode = mode;
}
@@ -3911,7 +3966,7 @@ int QHeaderViewPrivate::adjustedVisualIndex(int visualIndex) const
if (!hiddenSectionSize.isEmpty()) {
int adjustedVisualIndex = visualIndex;
int currentVisualIndex = 0;
- for (int i = 0; i < sectionItems.count(); ++i) {
+ for (int i = 0; i < sectionItems.size(); ++i) {
if (isVisualIndexHidden(i))
++adjustedVisualIndex;
else
@@ -3937,6 +3992,25 @@ void QHeaderViewPrivate::setScrollOffset(const QScrollBar *scrollBar, QAbstractI
}
}
+void QHeaderViewPrivate::updateSectionsBeforeAfter(int logical)
+{
+ Q_Q(QHeaderView);
+ const int visual = visualIndex(logical);
+ int from = logicalIndex(visual > 1 ? visual - 1 : 0);
+ int to = logicalIndex(visual + 1 >= sectionCount() ? visual : visual + 1);
+ QRect updateRect;
+ if (orientation == Qt::Horizontal) {
+ if (reverse())
+ std::swap(from, to);
+ updateRect = QRect(QPoint(q->sectionViewportPosition(from), 0),
+ QPoint(q->sectionViewportPosition(to) + headerSectionSize(to), viewport->height()));
+ } else {
+ updateRect = QRect(QPoint(0, q->sectionViewportPosition(from)),
+ QPoint(viewport->width(), q->sectionViewportPosition(to) + headerSectionSize(to)));
+ }
+ viewport->update(updateRect);
+}
+
#ifndef QT_NO_DATASTREAM
void QHeaderViewPrivate::write(QDataStream &out) const
{
@@ -3970,6 +4044,7 @@ void QHeaderViewPrivate::write(QDataStream &out) const
out << resizeContentsPrecision;
out << customDefaultSectionSize;
out << lastSectionSize;
+ out << int(sortIndicatorClearable);
}
bool QHeaderViewPrivate::read(QDataStream &in)
@@ -3979,8 +4054,8 @@ bool QHeaderViewPrivate::read(QDataStream &in)
int sortIndicatorSectionIn;
bool sortIndicatorShownIn;
int lengthIn;
- QVector<int> visualIndicesIn;
- QVector<int> logicalIndicesIn;
+ QList<int> visualIndicesIn;
+ QList<int> logicalIndicesIn;
QHash<int, int> hiddenSectionSizeIn;
bool movableSectionsIn;
bool clickableSectionsIn;
@@ -3991,7 +4066,7 @@ bool QHeaderViewPrivate::read(QDataStream &in)
int contentsSectionsIn;
int defaultSectionSizeIn;
int minimumSectionSizeIn;
- QVector<SectionItem> sectionItemsIn;
+ QList<SectionItem> sectionItemsIn;
in >> orient;
in >> order;
@@ -4027,12 +4102,21 @@ bool QHeaderViewPrivate::read(QDataStream &in)
in >> global;
+ // Check parameter consistency
+ // Global orientation out of bounds?
+ if (global < 0 || global > QHeaderView::ResizeToContents)
+ return false;
+
+ // Alignment out of bounds?
+ if (align < 0 || align > Qt::AlignVertical_Mask)
+ return false;
+
in >> sectionItemsIn;
// In Qt4 we had a vector of spans where one span could hold information on more sections.
// Now we have an itemvector where one items contains information about one section
// For backward compatibility with Qt4 we do the following
- QVector<SectionItem> newSectionItems;
- for (int u = 0; u < sectionItemsIn.count(); ++u) {
+ QList<SectionItem> newSectionItems;
+ for (int u = 0; u < sectionItemsIn.size(); ++u) {
int count = sectionItemsIn.at(u).tmpDataStreamSectionCount;
if (count > 1)
sectionItemsIn[u].size /= count;
@@ -4041,25 +4125,25 @@ bool QHeaderViewPrivate::read(QDataStream &in)
}
int sectionItemsLengthTotal = 0;
- for (const SectionItem &section : qAsConst(newSectionItems))
+ for (const SectionItem &section : std::as_const(newSectionItems))
sectionItemsLengthTotal += section.size;
if (sectionItemsLengthTotal != lengthIn)
return false;
const int currentCount = (orient == Qt::Horizontal ? model->columnCount(root) : model->rowCount(root));
- if (newSectionItems.count() < currentCount) {
+ if (newSectionItems.size() < currentCount) {
// we have sections not in the saved state, give them default settings
if (!visualIndicesIn.isEmpty() && !logicalIndicesIn.isEmpty()) {
- for (int i = newSectionItems.count(); i < currentCount; ++i) {
+ for (int i = newSectionItems.size(); i < currentCount; ++i) {
visualIndicesIn.append(i);
logicalIndicesIn.append(i);
}
}
- const int insertCount = currentCount - newSectionItems.count();
+ const int insertCount = currentCount - newSectionItems.size();
const int insertLength = defaultSectionSizeIn * insertCount;
lengthIn += insertLength;
SectionItem section(defaultSectionSizeIn, globalResizeMode);
- newSectionItems.insert(newSectionItems.count(), insertCount, section); // append
+ newSectionItems.insert(newSectionItems.size(), insertCount, section); // append
}
orientation = static_cast<Qt::Orientation>(orient);
@@ -4113,6 +4197,11 @@ bool QHeaderViewPrivate::read(QDataStream &in)
doDelayedResizeSections();
}
+ int inSortIndicatorClearable;
+ in >> inSortIndicatorClearable;
+ if (in.status() == QDataStream::Ok) // we haven't read past end
+ sortIndicatorClearable = inSortIndicatorClearable;
+
return true;
}
diff --git a/src/widgets/itemviews/qheaderview.h b/src/widgets/itemviews/qheaderview.h
index 7e950add1f..bd0050df5e 100644
--- a/src/widgets/itemviews/qheaderview.h
+++ b/src/widgets/itemviews/qheaderview.h
@@ -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
#ifndef QHEADERVIEW_H
#define QHEADERVIEW_H
@@ -55,13 +19,19 @@ class Q_WIDGETS_EXPORT QHeaderView : public QAbstractItemView
Q_OBJECT
Q_PROPERTY(bool firstSectionMovable READ isFirstSectionMovable WRITE setFirstSectionMovable)
Q_PROPERTY(bool showSortIndicator READ isSortIndicatorShown WRITE setSortIndicatorShown)
+ Q_PROPERTY(bool sectionsMovable READ sectionsMovable WRITE setSectionsMovable)
+ Q_PROPERTY(bool sectionsClickable READ sectionsClickable WRITE setSectionsClickable)
Q_PROPERTY(bool highlightSections READ highlightSections WRITE setHighlightSections)
Q_PROPERTY(bool stretchLastSection READ stretchLastSection WRITE setStretchLastSection)
- Q_PROPERTY(bool cascadingSectionResizes READ cascadingSectionResizes WRITE setCascadingSectionResizes)
- Q_PROPERTY(int defaultSectionSize READ defaultSectionSize WRITE setDefaultSectionSize RESET resetDefaultSectionSize)
+ Q_PROPERTY(bool cascadingSectionResizes READ cascadingSectionResizes
+ WRITE setCascadingSectionResizes)
+ Q_PROPERTY(int defaultSectionSize READ defaultSectionSize WRITE setDefaultSectionSize
+ RESET resetDefaultSectionSize)
Q_PROPERTY(int minimumSectionSize READ minimumSectionSize WRITE setMinimumSectionSize)
Q_PROPERTY(int maximumSectionSize READ maximumSectionSize WRITE setMaximumSectionSize)
Q_PROPERTY(Qt::Alignment defaultAlignment READ defaultAlignment WRITE setDefaultAlignment)
+ Q_PROPERTY(bool sortIndicatorClearable READ isSortIndicatorClearable
+ WRITE setSortIndicatorClearable NOTIFY sortIndicatorClearableChanged)
public:
@@ -115,19 +85,11 @@ public:
void setSectionsMovable(bool movable);
bool sectionsMovable() const;
-#if QT_DEPRECATED_SINCE(5, 0)
- inline QT_DEPRECATED void setMovable(bool movable) { setSectionsMovable(movable); }
- inline QT_DEPRECATED bool isMovable() const { return sectionsMovable(); }
-#endif
void setFirstSectionMovable(bool movable);
bool isFirstSectionMovable() const;
void setSectionsClickable(bool clickable);
bool sectionsClickable() const;
-#if QT_DEPRECATED_SINCE(5, 0)
- inline QT_DEPRECATED void setClickable(bool clickable) { setSectionsClickable(clickable); }
- inline QT_DEPRECATED bool isClickable() const { return sectionsClickable(); }
-#endif
void setHighlightSections(bool highlight);
bool highlightSections() const;
@@ -139,15 +101,6 @@ public:
void setResizeContentsPrecision(int precision);
int resizeContentsPrecision() const;
-#if QT_DEPRECATED_SINCE(5, 0)
- inline QT_DEPRECATED void setResizeMode(ResizeMode mode)
- { setSectionResizeMode(mode); }
- inline QT_DEPRECATED void setResizeMode(int logicalindex, ResizeMode mode)
- { setSectionResizeMode(logicalindex, mode); }
- inline QT_DEPRECATED ResizeMode resizeMode(int logicalindex) const
- { return sectionResizeMode(logicalindex); }
-#endif
-
int stretchSectionCount() const;
void setSortIndicatorShown(bool show);
@@ -157,6 +110,9 @@ public:
int sortIndicatorSection() const;
Qt::SortOrder sortIndicatorOrder() const;
+ void setSortIndicatorClearable(bool clearable);
+ bool isSortIndicatorClearable() const;
+
bool stretchLastSection() const;
void setStretchLastSection(bool stretch);
@@ -203,6 +159,7 @@ Q_SIGNALS:
void sectionHandleDoubleClicked(int logicalIndex);
void geometriesChanged();
void sortIndicatorChanged(int logicalIndex, Qt::SortOrder order);
+ void sortIndicatorClearableChanged(bool clearable);
protected Q_SLOTS:
void updateSection(int logicalIndex);
@@ -234,7 +191,8 @@ protected:
void updateGeometries() override;
void scrollContentsBy(int dx, int dy) override;
- void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override;
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles = QList<int>()) override;
void rowsInserted(const QModelIndex &parent, int start, int end) override;
QRect visualRect(const QModelIndex &index) const override;
@@ -246,20 +204,15 @@ protected:
QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers) override;
void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags) override;
QRegion visualRegionForSelection(const QItemSelection &selection) const override;
- void initStyleOption(QStyleOptionHeader *option) const;
+ virtual void initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const;
+ virtual void initStyleOption(QStyleOptionHeader *option) const;
friend class QTableView;
friend class QTreeView;
private:
- // ### Qt6: make them protected slots in QHeaderViewPrivate
- Q_PRIVATE_SLOT(d_func(), void _q_sectionsRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast))
- Q_PRIVATE_SLOT(d_func(), void _q_sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination))
- Q_PRIVATE_SLOT(d_func(), void _q_sectionsMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination))
- Q_PRIVATE_SLOT(d_func(), void _q_sectionsAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
- QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint))
- Q_PRIVATE_SLOT(d_func(), void _q_sectionsChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
- QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint))
+ void initStyleOption(QStyleOptionFrame *option) const override;
+
Q_DECLARE_PRIVATE(QHeaderView)
Q_DISABLE_COPY(QHeaderView)
};
diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h
index 766adef36d..8b214e1d03 100644
--- a/src/widgets/itemviews/qheaderview_p.h
+++ b/src/widgets/itemviews/qheaderview_p.h
@@ -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
#ifndef QHEADERVIEW_P_H
#define QHEADERVIEW_P_H
@@ -52,6 +16,7 @@
//
#include <QtWidgets/private/qtwidgetsglobal_p.h>
+#include "qheaderview.h"
#include "private/qabstractitemview_p.h"
#include "QtCore/qbitarray.h"
@@ -60,6 +25,8 @@
#include "QtWidgets/qlabel.h"
#endif
+#include <array>
+
QT_REQUIRE_CONFIG(itemviews);
QT_BEGIN_NAMESPACE
@@ -73,15 +40,17 @@ public:
QHeaderViewPrivate()
: state(NoState),
- offset(0),
+ headerOffset(0),
sortIndicatorOrder(Qt::DescendingOrder),
sortIndicatorSection(0),
sortIndicatorShown(false),
+ sortIndicatorClearable(false),
lastPos(-1),
firstPos(-1),
originalSize(-1),
section(-1),
target(-1),
+ firstPressed(-1),
pressed(-1),
hover(-1),
length(0),
@@ -119,13 +88,17 @@ public:
void updateSectionIndicator(int section, int position);
void updateHiddenSections(int logicalFirst, int logicalLast);
void resizeSections(QHeaderView::ResizeMode globalMode, bool useGlobalMode = false);
- void _q_sectionsRemoved(const QModelIndex &,int,int);
- void _q_sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination);
- void _q_sectionsMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination);
- void _q_sectionsAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
- QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
- void _q_sectionsChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
- QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
+ void sectionsRemoved(const QModelIndex &,int,int);
+ void sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart,
+ int logicalEnd, const QModelIndex &destinationParent,
+ int logicalDestination);
+ void sectionsMoved(const QModelIndex &sourceParent, int logicalStart,
+ int logicalEnd, const QModelIndex &destinationParent,
+ int logicalDestination);
+ void sectionsAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
+ QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
+ void sectionsChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
+ QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
bool isSectionSelected(int section) const;
bool isFirstVisibleSection(int section) const;
@@ -154,12 +127,12 @@ public:
inline void prepareSectionSelected() {
if (!selectionModel || !selectionModel->hasSelection())
sectionSelected.clear();
- else if (sectionSelected.count() != sectionCount() * 2)
+ else if (sectionSelected.size() != sectionCount() * 2)
sectionSelected.fill(false, sectionCount() * 2);
else sectionSelected.fill(false);
}
- inline int sectionCount() const {return sectionItems.count();}
+ inline int sectionCount() const {return sectionItems.size();}
inline bool reverse() const {
return orientation == Qt::Horizontal && q_func()->isRightToLeft();
@@ -200,8 +173,8 @@ public:
}
inline void initializeIndexMapping() const {
- if (visualIndices.count() != sectionCount()
- || logicalIndices.count() != sectionCount()) {
+ if (visualIndices.size() != sectionCount()
+ || logicalIndices.size() != sectionCount()) {
visualIndices.resize(sectionCount());
logicalIndices.resize(sectionCount());
for (int s = 0; s < sectionCount(); ++s) {
@@ -212,7 +185,7 @@ public:
}
inline void clearCascadingSections() {
- firstCascadingSection = sectionItems.count();
+ firstCascadingSection = sectionItems.size();
lastCascadingSection = 0;
cascadingSectionSize.clear();
}
@@ -246,20 +219,28 @@ public:
}
}
+ inline void disconnectModel()
+ {
+ for (const QMetaObject::Connection &connection : modelConnections)
+ QObject::disconnect(connection);
+ }
+
void clear();
void flipSortIndicator(int section);
+ Qt::SortOrder defaultSortOrderForSection(int section) const;
void cascadingResize(int visual, int newSize);
enum State { NoState, ResizeSection, MoveSection, SelectSections, NoClear } state;
- int offset;
+ int headerOffset;
Qt::Orientation orientation;
Qt::SortOrder sortIndicatorOrder;
int sortIndicatorSection;
bool sortIndicatorShown;
+ bool sortIndicatorClearable;
- mutable QVector<int> visualIndices; // visualIndex = visualIndices.at(logicalIndex)
- mutable QVector<int> logicalIndices; // logicalIndex = row or column in the model
+ mutable QList<int> visualIndices; // visualIndex = visualIndices.at(logicalIndex)
+ mutable QList<int> logicalIndices; // logicalIndex = row or column in the model
mutable QBitArray sectionSelected; // from logical index to bit
mutable QHash<int, int> hiddenSectionSize; // from logical index to section size
mutable QHash<int, int> cascadingSectionSize; // from visual index to section size
@@ -274,6 +255,7 @@ public:
int originalSize;
int section; // used for resizing and moving sections
int target;
+ int firstPressed;
int pressed;
int hover;
@@ -329,14 +311,15 @@ public:
#endif
};
- QVector<SectionItem> sectionItems;
+ QList<SectionItem> sectionItems;
struct LayoutChangeItem {
QPersistentModelIndex index;
SectionItem section;
};
- QVector<LayoutChangeItem> layoutChangePersistentSections;
+ QList<LayoutChangeItem> layoutChangePersistentSections;
+ std::array<QMetaObject::Connection, 8> modelConnections;
- void createSectionItems(int start, int end, int size, QHeaderView::ResizeMode mode);
+ void createSectionItems(int start, int end, int sectionSize, QHeaderView::ResizeMode mode);
void removeSectionsFromSectionItems(int start, int end);
void resizeSectionItem(int visualIndex, int oldSize, int newSize);
void setDefaultSectionSize(int size);
@@ -363,7 +346,7 @@ public:
void setHiddenSectionsFromBitVector(const QBitArray &sectionHidden) {
SectionItem *sectionData = sectionItems.data();
- for (int i = 0; i < sectionHidden.count(); ++i)
+ for (int i = 0; i < sectionHidden.size(); ++i)
sectionData[i].isHidden = sectionHidden.at(i);
}
@@ -380,6 +363,7 @@ public:
int viewSectionSizeHint(int logical) const;
int adjustedVisualIndex(int visualIndex) const;
void setScrollOffset(const QScrollBar *scrollBar, QAbstractItemView::ScrollMode scrollMode);
+ void updateSectionsBeforeAfter(int logical);
#ifndef QT_NO_DATASTREAM
void write(QDataStream &out) const;
@@ -388,7 +372,7 @@ public:
};
Q_DECLARE_TYPEINFO(QHeaderViewPrivate::SectionItem, Q_PRIMITIVE_TYPE);
-Q_DECLARE_TYPEINFO(QHeaderViewPrivate::LayoutChangeItem, Q_MOVABLE_TYPE);
+Q_DECLARE_TYPEINFO(QHeaderViewPrivate::LayoutChangeItem, Q_RELOCATABLE_TYPE);
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp
index 460764f1b8..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>
@@ -77,7 +42,7 @@ class QItemDelegatePrivate : public QAbstractItemDelegatePrivate
Q_DECLARE_PUBLIC(QItemDelegate)
public:
- QItemDelegatePrivate() : f(0), clipPainting(true) {}
+ QItemDelegatePrivate() : f(nullptr), clipPainting(true) {}
inline const QItemEditorFactory *editorFactory() const
{ return f ? f : QItemEditorFactory::defaultFactory(); }
@@ -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;
}
@@ -149,7 +114,7 @@ QRect QItemDelegatePrivate::textLayoutBounds(const QStyleOptionViewItem &option,
QStyle *style = w ? w->style() : QApplication::style();
const bool wrapText = option.features & QStyleOptionViewItem::WrapText;
// see QItemDelegate::drawDisplay
- const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, w) + 1;
+ const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, w) + 1;
switch (option.decorationPosition) {
case QStyleOptionViewItem::Left:
case QStyleOptionViewItem::Right:
@@ -267,7 +232,6 @@ QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const
\row \li \l Qt::AccessibleTextRole \li QString
\endomit
\row \li \l Qt::BackgroundRole \li QBrush (\since 4.2)
- \row \li \l Qt::BackgroundColorRole \li QColor (obsolete; use Qt::BackgroundRole instead)
\row \li \l Qt::CheckStateRole \li Qt::CheckState
\row \li \l Qt::DecorationRole \li QIcon, QPixmap and QColor
\row \li \l Qt::DisplayRole \li QString and types with a string representation
@@ -279,7 +243,6 @@ QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const
\endomit
\row \li \l Qt::TextAlignmentRole \li Qt::Alignment
\row \li \l Qt::ForegroundRole \li QBrush (\since 4.2)
- \row \li \l Qt::TextColorRole \li QColor (obsolete; use Qt::ForegroundRole instead)
\omit
\row \li \l Qt::ToolTipRole
\row \li \l Qt::WhatsThisRole
@@ -294,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
@@ -318,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
*/
/*!
@@ -380,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,
@@ -414,7 +387,7 @@ void QItemDelegate::paint(QPainter *painter,
if (value.isValid()) {
// ### we need the pixmap to call the virtual function
pixmap = decoration(opt, value);
- if (value.type() == QVariant::Icon) {
+ if (value.userType() == QMetaType::QIcon) {
d->tmp.icon = qvariant_cast<QIcon>(value);
d->tmp.mode = d->iconMode(option.state);
d->tmp.state = d->iconState(option.state);
@@ -434,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);
}
@@ -502,9 +475,9 @@ QWidget *QItemDelegate::createEditor(QWidget *parent,
{
Q_D(const QItemDelegate);
if (!index.isValid())
- return 0;
+ return nullptr;
const QItemEditorFactory *factory = d->f;
- if (factory == 0)
+ if (factory == nullptr)
factory = QItemEditorFactory::defaultFactory();
QWidget *w = factory->createEditor(index.data(Qt::EditRole).userType(), parent);
if (w)
@@ -524,19 +497,14 @@ 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();
if (!n.isEmpty()) {
if (!v.isValid())
- v = QVariant(editor->property(n).userType(), (const void *)0);
+ v = QVariant(editor->property(n).metaType());
editor->setProperty(n, v);
}
-#endif
}
/*!
@@ -554,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);
@@ -568,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
}
/*!
@@ -586,7 +548,7 @@ void QItemDelegate::updateEditorGeometry(QWidget *editor,
QPixmap pixmap = decoration(option, index.data(Qt::DecorationRole));
QString text = QItemDelegatePrivate::replaceNewLine(index.data(Qt::DisplayRole).toString());
QRect pixmapRect = QRect(QPoint(0, 0), option.decorationSize).intersected(pixmap.rect());
- QRect textRect = textRectangle(0, option.rect, option.font, text);
+ QRect textRect = textRectangle(nullptr, option.rect, option.font, text);
QRect checkRect = doCheck(option, textRect, index.data(Qt::CheckStateRole));
QStyleOptionViewItem opt = option;
opt.showDecorationSelected = true; // let the editor take up all available space
@@ -654,7 +616,7 @@ void QItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &o
const QWidget *widget = d->widget(option);
QStyle *style = widget ? widget->style() : QApplication::style();
- const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1;
+ const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
const bool wrapText = opt.features & QStyleOptionViewItem::WrapText;
d->textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
@@ -697,10 +659,12 @@ void QItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &o
|| textRect.height() < textLayoutSize.height())) {
painter->save();
painter->setClipRect(layoutRect);
- d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect);
+ d->textLayout.draw(painter, layoutRect.topLeft(), QList<QTextLayout::FormatRange>(),
+ layoutRect);
painter->restore();
} else {
- d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect);
+ d->textLayout.draw(painter, layoutRect.topLeft(), QList<QTextLayout::FormatRange>(),
+ layoutRect);
}
}
@@ -841,7 +805,7 @@ void QItemDelegate::doLayout(const QStyleOptionViewItem &option,
const bool hasText = textRect->isValid();
const bool hasMargin = (hasText | hasPixmap | hasCheck);
const int frameHMargin = hasMargin ?
- style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
+ style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1 : 0;
const int textMargin = hasText ? frameHMargin : 0;
const int pixmapMargin = hasPixmap ? frameHMargin : 0;
const int checkMargin = hasCheck ? frameHMargin : 0;
@@ -969,12 +933,12 @@ void QItemDelegate::doLayout(const QStyleOptionViewItem &option,
QPixmap QItemDelegate::decoration(const QStyleOptionViewItem &option, const QVariant &variant) const
{
Q_D(const QItemDelegate);
- switch (variant.type()) {
- case QVariant::Icon: {
+ switch (variant.userType()) {
+ case QMetaType::QIcon: {
QIcon::Mode mode = d->iconMode(option.state);
QIcon::State state = d->iconState(option.state);
return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, mode, state); }
- case QVariant::Color: {
+ case QMetaType::QColor: {
static QPixmap pixmap(option.decorationSize);
pixmap.fill(qvariant_cast<QColor>(variant));
return pixmap; }
@@ -1001,20 +965,6 @@ static QString qPixmapSerial(quint64 i, bool enabled)
return QString((const QChar *)ptr, int(&arr[sizeof(arr) / sizeof(ushort)] - ptr));
}
-#if QT_DEPRECATED_SINCE(5, 13)
-QPixmap *QItemDelegate::selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const
-{
- const QString key = qPixmapSerial(pixmap.cacheKey(), enabled);
-QT_WARNING_PUSH
-QT_WARNING_DISABLE_DEPRECATED
- QPixmap *pm = QPixmapCache::find(key);
- if (pm)
- return pm;
- selectedPixmap(pixmap, palette, enabled);
- return QPixmapCache::find(key);
-QT_WARNING_POP
-}
-#endif
/*!
\internal
@@ -1031,7 +981,7 @@ QPixmap QItemDelegate::selectedPixmap(const QPixmap &pixmap, const QPalette &pal
QColor color = palette.color(enabled ? QPalette::Normal : QPalette::Disabled,
QPalette::Highlight);
- color.setAlphaF((qreal)0.3);
+ color.setAlphaF(0.3f);
QPainter painter(&img);
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
@@ -1060,24 +1010,24 @@ QRect QItemDelegate::rect(const QStyleOptionViewItem &option,
if (role == Qt::CheckStateRole)
return doCheck(option, option.rect, value);
if (value.isValid() && !value.isNull()) {
- switch (value.type()) {
- case QVariant::Invalid:
+ switch (value.userType()) {
+ case QMetaType::UnknownType:
break;
- case QVariant::Pixmap: {
+ case QMetaType::QPixmap: {
const QPixmap &pixmap = qvariant_cast<QPixmap>(value);
- return QRect(QPoint(0, 0), pixmap.size() / pixmap.devicePixelRatio() ); }
- case QVariant::Image: {
+ 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() ); }
- case QVariant::Icon: {
+ 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);
QIcon icon = qvariant_cast<QIcon>(value);
QSize size = icon.actualSize(option.decorationSize, mode, state);
return QRect(QPoint(0, 0), size); }
- case QVariant::Color:
+ case QMetaType::QColor:
return QRect(QPoint(0, 0), option.decorationSize);
- case QVariant::String:
+ case QMetaType::QString:
default: {
const QString text = d->valueToText(value, option);
value = index.data(Qt::FontRole);
@@ -1122,7 +1072,7 @@ QRect QItemDelegate::textRectangle(QPainter * /*painter*/, const QRect &rect,
QSizeF fpSize = d->doTextLayout(rect.width());
const QSize size = QSize(qCeil(fpSize.width()), qCeil(fpSize.height()));
// ###: textRectangle should take style option as argument
- const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
+ const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr) + 1;
return QRect(0, 0, size.width() + 2 * textMargin, size.height());
}
@@ -1191,7 +1141,7 @@ bool QItemDelegate::editorEvent(QEvent *event,
QRect emptyRect;
doLayout(option, &checkRect, &emptyRect, &emptyRect, false);
QMouseEvent *me = static_cast<QMouseEvent*>(event);
- if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
+ if (me->button() != Qt::LeftButton || !checkRect.contains(me->position().toPoint()))
return false;
// eat the double click events inside the check rect
@@ -1207,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
@@ -1234,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);
@@ -1242,7 +1192,7 @@ QStyleOptionViewItem QItemDelegate::setOptions(const QModelIndex &index,
opt.palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value));
// disable style animations for checkboxes etc. within itemviews (QTBUG-30146)
- opt.styleObject = 0;
+ opt.styleObject = nullptr;
return opt;
}
diff --git a/src/widgets/itemviews/qitemdelegate.h b/src/widgets/itemviews/qitemdelegate.h
index e504615fb2..5ad32e663e 100644
--- a/src/widgets/itemviews/qitemdelegate.h
+++ b/src/widgets/itemviews/qitemdelegate.h
@@ -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
#ifndef QITEMDELEGATE_H
#define QITEMDELEGATE_H
@@ -114,10 +78,6 @@ protected:
QPixmap decoration(const QStyleOptionViewItem &option, const QVariant &variant) const;
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X("Use selectedPixmap() instead")
- QPixmap *selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const;
-#endif
static QPixmap selectedPixmap(const QPixmap &pixmap, const QPalette &palette, bool enabled);
QRect doCheck(const QStyleOptionViewItem &option, const QRect &bounding,
diff --git a/src/widgets/itemviews/qitemeditorfactory.cpp b/src/widgets/itemviews/qitemeditorfactory.cpp
index 8ed2ee5f28..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()));
@@ -156,7 +120,7 @@ Q_SIGNALS:
Additional editors can be registered with the registerEditor() function.
- \sa QStyledItemDelegate, {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; }
};
/*!
@@ -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,
@@ -468,7 +432,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase()
property, you should use QStandardItemEditorCreator instead.
\sa QItemEditorCreatorBase, QStandardItemEditorCreator,
- QItemEditorFactory, {Color Editor Factory Example}
+ QItemEditorFactory
*/
/*!
@@ -513,7 +477,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase()
Setting the \c editorFactory created above in an item delegate via
QStyledItemDelegate::setItemEditorFactory() makes sure that all values of type
- QVariant::DateTime will be edited in \c{MyFancyDateTimeEdit}.
+ 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{QStyledItemDelegate}s to set
@@ -524,7 +488,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase()
\snippet code/src_gui_itemviews_qitemeditorfactory.cpp 3
\sa QItemEditorCreatorBase, QItemEditorCreator,
- QItemEditorFactory, QStyledItemDelegate, {Color Editor Factory Example}
+ QItemEditorFactory, QStyledItemDelegate
*/
/*!
@@ -577,8 +541,7 @@ void QExpandingLineEdit::updateMinimumWidth()
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);
}
@@ -592,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())
diff --git a/src/widgets/itemviews/qitemeditorfactory.h b/src/widgets/itemviews/qitemeditorfactory.h
index 118f6e45e8..2c8ed301cd 100644
--- a/src/widgets/itemviews/qitemeditorfactory.h
+++ b/src/widgets/itemviews/qitemeditorfactory.h
@@ -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
#ifndef QITEMEDITORFACTORY_H
#define QITEMEDITORFACTORY_H
diff --git a/src/widgets/itemviews/qitemeditorfactory_p.h b/src/widgets/itemviews/qitemeditorfactory_p.h
index e78f42b729..4f4252a7c4 100644
--- a/src/widgets/itemviews/qitemeditorfactory_p.h
+++ b/src/widgets/itemviews/qitemeditorfactory_p.h
@@ -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
#ifndef QITEMEDITORFACTORY_P_H
#define QITEMEDITORFACTORY_P_H
diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp
index 04cddf2926..a7f1931947 100644
--- a/src/widgets/itemviews/qlistview.cpp
+++ b/src/widgets/itemviews/qlistview.cpp
@@ -1,66 +1,30 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2013 Samuel Gaist <samuel.gaist@deltech.ch>
-** 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.
+// Copyright (C) 2013 Samuel Gaist <samuel.gaist@deltech.ch>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qlistview.h"
#include <qabstractitemdelegate.h>
+#if QT_CONFIG(accessibility)
+#include <qaccessible.h>
+#endif
#include <qapplication.h>
-#include <qpainter.h>
+#include <qstylepainter.h>
#include <qbitmap.h>
+#include <qdebug.h>
#if QT_CONFIG(draganddrop)
#include <qdrag.h>
#endif
-#include <qvector.h>
-#include <qstyle.h>
#include <qevent.h>
-#include <qscrollbar.h>
+#include <qlist.h>
#if QT_CONFIG(rubberband)
#include <qrubberband.h>
#endif
+#include <qscrollbar.h>
+#include <qstyle.h>
#include <private/qapplication_p.h>
#include <private/qlistview_p.h>
#include <private/qscrollbar_p.h>
-#include <qdebug.h>
-#ifndef QT_NO_ACCESSIBILITY
-#include <qaccessible.h>
-#endif
#include <algorithm>
@@ -124,7 +88,7 @@ extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event);
that can be taken for views that are intended to display items with equal sizes
is to set the \l uniformItemSizes property to true.
- \sa {View Classes}, {Item Views Puzzle Example}, QTreeView, QTableView, QListWidget
+ \sa {View Classes}, QTreeView, QTableView, QListWidget
*/
/*!
@@ -214,8 +178,8 @@ QListView::~QListView()
grid, or cannot be moved at all.
This property determines how the user can move the items in the
- view. \l Static means that the items can't be moved the user. \l
- Free means that the user can drag and drop the items to any
+ view. \l Static means that the items can't be moved by the user.
+ \l Free means that the user can drag and drop the items to any
position in the view. \l Snap means that the user can drag and
drop the items, but only to the positions in a notional grid
signified by the gridSize property.
@@ -388,7 +352,7 @@ int QListView::spacing() const
/*!
\property QListView::batchSize
\brief the number of items laid out in each batch if \l layoutMode is
- set to \l Batched
+ set to \l Batched.
The default value is 100.
@@ -572,6 +536,8 @@ void QListView::scrollTo(const QModelIndex &index, ScrollHint hint)
return;
const QRect rect = visualRect(index);
+ if (!rect.isValid())
+ return;
if (hint == EnsureVisible && d->viewport->rect().contains(rect)) {
d->viewport->update(rect);
return;
@@ -655,7 +621,8 @@ QItemViewPaintPairs QListViewPrivate::draggablePaintPairs(const QModelIndexList
QRect &rect = *r;
const QRect viewportRect = viewport->rect();
QItemViewPaintPairs ret;
- QVector<QModelIndex> visibleIndexes = intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset()));
+ QList<QModelIndex> visibleIndexes =
+ intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset()));
std::sort(visibleIndexes.begin(), visibleIndexes.end());
for (const auto &index : indexes) {
if (std::binary_search(visibleIndexes.cbegin(), visibleIndexes.cend(), index)) {
@@ -664,7 +631,9 @@ QItemViewPaintPairs QListViewPrivate::draggablePaintPairs(const QModelIndexList
rect |= current;
}
}
- rect &= viewportRect;
+ QRect clipped = rect & viewportRect;
+ rect.setLeft(clipped.left());
+ rect.setRight(clipped.right());
return ret;
}
@@ -680,12 +649,12 @@ void QListView::reset()
}
/*!
- \internal
+ \reimp
*/
void QListView::setRootIndex(const QModelIndex &index)
{
Q_D(QListView);
- d->column = qBound(0, d->column, d->model->columnCount(index) - 1);
+ d->column = qMax(0, qMin(d->column, d->model->columnCount(index) - 1));
QAbstractItemView::setRootIndex(index);
// sometimes we get an update before reset() is called
d->clear();
@@ -693,7 +662,7 @@ void QListView::setRootIndex(const QModelIndex &index)
}
/*!
- \internal
+ \reimp
Scroll the view contents by \a dx and \a dy.
*/
@@ -729,7 +698,8 @@ QSize QListView::contentsSize() const
/*!
\reimp
*/
-void QListView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
+void QListView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles)
{
d_func()->commonListView->dataChanged(topLeft, bottomRight);
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
@@ -783,9 +753,12 @@ void QListView::mouseMoveEvent(QMouseEvent *e)
&& d->showElasticBand
&& d->selectionMode != SingleSelection
&& d->selectionMode != NoSelection) {
- QRect rect(d->pressedPosition, e->pos() + QPoint(horizontalOffset(), verticalOffset()));
+ QRect rect(d->pressedPosition, e->position().toPoint() + QPoint(horizontalOffset(), verticalOffset()));
rect = rect.normalized();
- d->viewport->update(d->mapToViewport(rect.united(d->elasticBand)));
+ const int margin = 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
+ const QRect viewPortRect = rect.united(d->elasticBand)
+ .adjusted(-margin, -margin, margin, margin);
+ d->viewport->update(d->mapToViewport(viewPortRect));
d->elasticBand = rect;
}
}
@@ -799,7 +772,9 @@ void QListView::mouseReleaseEvent(QMouseEvent *e)
QAbstractItemView::mouseReleaseEvent(e);
// #### move this implementation into a dynamic class
if (d->showElasticBand && d->elasticBand.isValid()) {
- d->viewport->update(d->mapToViewport(d->elasticBand));
+ const int margin = 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
+ const QRect viewPortRect = d->elasticBand.adjusted(-margin, -margin, margin, margin);
+ d->viewport->update(d->mapToViewport(viewPortRect));
d->elasticBand = QRect();
}
}
@@ -907,10 +882,68 @@ void QListView::dragLeaveEvent(QDragLeaveEvent *e)
/*!
\reimp
*/
-void QListView::dropEvent(QDropEvent *e)
+void QListView::dropEvent(QDropEvent *event)
{
- if (!d_func()->commonListView->filterDropEvent(e))
- QAbstractItemView::dropEvent(e);
+ Q_D(QListView);
+
+ if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
+ dragDropMode() == QAbstractItemView::InternalMove)) {
+ QModelIndex topIndex;
+ bool topIndexDropped = false;
+ int col = -1;
+ int row = -1;
+ // check whether a subclass has already accepted the event, ie. moved the data
+ if (!event->isAccepted() && d->dropOn(event, &row, &col, &topIndex)) {
+ const QList<QModelIndex> selIndexes = selectedIndexes();
+ QList<QPersistentModelIndex> persIndexes;
+ persIndexes.reserve(selIndexes.size());
+
+ for (const auto &index : selIndexes) {
+ persIndexes.append(index);
+ if (index == topIndex) {
+ topIndexDropped = true;
+ break;
+ }
+ }
+
+ if (!topIndexDropped && !topIndex.isValid()) {
+ std::sort(persIndexes.begin(), persIndexes.end()); // The dropped items will remain in the same visual order.
+
+ QPersistentModelIndex dropRow = model()->index(row, col, topIndex);
+
+ int r = row == -1 ? model()->rowCount() : (dropRow.row() >= 0 ? dropRow.row() : row);
+ bool dataMoved = false;
+ for (int i = 0; i < persIndexes.size(); ++i) {
+ const QPersistentModelIndex &pIndex = persIndexes.at(i);
+ // only generate a move when not same row or behind itself
+ if (r != pIndex.row() && r != pIndex.row() + 1) {
+ // try to move (preserves selection)
+ dataMoved |= model()->moveRow(QModelIndex(), pIndex.row(), QModelIndex(), r);
+ if (!dataMoved) // can't move - abort and let QAbstractItemView handle this
+ break;
+ } else {
+ // move onto itself is blocked, don't delete anything
+ dataMoved = true;
+ }
+ r = pIndex.row() + 1; // Dropped items are inserted contiguously and in the right order.
+ }
+ if (dataMoved)
+ event->accept();
+ }
+ }
+
+ // either we or a subclass accepted the move event, so assume that the data was
+ // moved and that QAbstractItemView shouldn't remove the source when QDrag::exec returns
+ if (event->isAccepted())
+ d->dropEventMoved = true;
+ }
+
+ if (!d->commonListView->filterDropEvent(event) || !d->dropEventMoved) {
+ // icon view didn't move the data, and moveRows not implemented, so fall back to default
+ if (!d->dropEventMoved)
+ event->ignore();
+ QAbstractItemView::dropEvent(event);
+ }
}
/*!
@@ -927,29 +960,27 @@ void QListView::startDrag(Qt::DropActions supportedActions)
/*!
\reimp
*/
-QStyleOptionViewItem QListView::viewOptions() const
+void QListView::initViewItemOption(QStyleOptionViewItem *option) const
{
Q_D(const QListView);
- QStyleOptionViewItem option = QAbstractItemView::viewOptions();
+ QAbstractItemView::initViewItemOption(option);
if (!d->iconSize.isValid()) { // otherwise it was already set in abstractitemview
int pm = (d->viewMode == QListView::ListMode
- ? style()->pixelMetric(QStyle::PM_ListViewIconSize, 0, this)
- : style()->pixelMetric(QStyle::PM_IconViewIconSize, 0, this));
- option.decorationSize = QSize(pm, pm);
+ ? style()->pixelMetric(QStyle::PM_ListViewIconSize, nullptr, this)
+ : style()->pixelMetric(QStyle::PM_IconViewIconSize, nullptr, this));
+ option->decorationSize = QSize(pm, pm);
}
if (d->viewMode == QListView::IconMode) {
- option.showDecorationSelected = false;
- option.decorationPosition = QStyleOptionViewItem::Top;
- option.displayAlignment = Qt::AlignCenter;
+ option->showDecorationSelected = false;
+ option->decorationPosition = QStyleOptionViewItem::Top;
+ option->displayAlignment = Qt::AlignCenter;
} else {
- option.decorationPosition = QStyleOptionViewItem::Left;
+ option->decorationPosition = QStyleOptionViewItem::Left;
}
if (d->gridSize().isValid()) {
- option.rect.setSize(d->gridSize());
+ option->rect.setSize(d->gridSize());
}
-
- return option;
}
@@ -961,10 +992,12 @@ void QListView::paintEvent(QPaintEvent *e)
Q_D(QListView);
if (!d->itemDelegate)
return;
- QStyleOptionViewItem option = d->viewOptionsV1();
- QPainter painter(d->viewport);
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+ QStylePainter painter(d->viewport);
- const QVector<QModelIndex> toBeRendered = d->intersectingSet(e->rect().translated(horizontalOffset(), verticalOffset()), false);
+ const QList<QModelIndex> toBeRendered =
+ d->intersectingSet(e->rect().translated(horizontalOffset(), verticalOffset()), false);
const QModelIndex current = currentIndex();
const QModelIndex hover = d->hover;
@@ -983,8 +1016,8 @@ void QListView::paintEvent(QPaintEvent *e)
? qMax(viewport()->size().width(), d->contentsSize().width()) - 2 * d->spacing()
: qMax(viewport()->size().height(), d->contentsSize().height()) - 2 * d->spacing();
- QVector<QModelIndex>::const_iterator end = toBeRendered.constEnd();
- for (QVector<QModelIndex>::const_iterator it = toBeRendered.constBegin(); it != end; ++it) {
+ QList<QModelIndex>::const_iterator end = toBeRendered.constEnd();
+ for (QList<QModelIndex>::const_iterator it = toBeRendered.constBegin(); it != end; ++it) {
Q_ASSERT((*it).isValid());
option.rect = visualRect(*it);
@@ -1032,14 +1065,14 @@ void QListView::paintEvent(QPaintEvent *e)
// is provided by the delegate
QStyle::State oldState = option.state;
option.state &= ~QStyle::State_Selected;
- style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &option, &painter, this);
+ painter.drawPrimitive(QStyle::PE_PanelItemViewRow, option);
option.state = oldState;
alternateBase = !alternateBase;
previousRow = row;
}
- d->delegateForIndex(*it)->paint(&painter, option, *it);
+ itemDelegateForIndex(*it)->paint(&painter, option, *it);
}
#if QT_CONFIG(draganddrop)
@@ -1056,7 +1089,7 @@ void QListView::paintEvent(QPaintEvent *e)
opt.rect = d->mapToViewport(d->elasticBand, false).intersected(
d->viewport->rect().adjusted(-16, -16, 16, 16));
painter.save();
- style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
+ painter.drawControl(QStyle::CE_RubberBand, opt);
painter.restore();
}
#endif
@@ -1069,8 +1102,8 @@ QModelIndex QListView::indexAt(const QPoint &p) const
{
Q_D(const QListView);
QRect rect(p.x() + horizontalOffset(), p.y() + verticalOffset(), 1, 1);
- const QVector<QModelIndex> intersectVector = d->intersectingSet(rect);
- QModelIndex index = intersectVector.count() > 0
+ const QList<QModelIndex> intersectVector = d->intersectingSet(rect);
+ QModelIndex index = intersectVector.size() > 0
? intersectVector.last() : QModelIndex();
if (index.isValid() && visualRect(index).contains(p))
return index;
@@ -1148,7 +1181,7 @@ QModelIndex QListView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie
if (d->gridSize().isValid()) rect.setSize(d->gridSize());
QSize contents = d->contentsSize();
- QVector<QModelIndex> intersectVector;
+ QList<QModelIndex> intersectVector;
switch (cursorAction) {
case MoveLeft:
@@ -1173,13 +1206,28 @@ QModelIndex QListView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie
d->removeCurrentAndDisabled(&intersectVector, current);
}
return d->closestIndex(initialRect, intersectVector);
- case MovePageUp:
- // move current by (visibileRowCount - 1) items.
- // rect.translate(0, -rect.height()); will happen in the switch fallthrough for MoveUp.
- rect.moveTop(rect.top() - d->viewport->height() + 2 * rect.height());
- if (rect.top() < rect.height())
- rect.moveTop(rect.height());
- Q_FALLTHROUGH();
+ case MovePageUp: {
+ if (rect.height() >= d->viewport->height())
+ return moveCursor(QAbstractItemView::MoveUp, modifiers);
+
+ rect.moveTop(rect.top() - d->viewport->height() + 1);
+ if (rect.top() < rect.height()) {
+ rect.setTop(0);
+ rect.setBottom(1);
+ }
+ QModelIndex findindex = current;
+ while (intersectVector.isEmpty()
+ || rectForIndex(findindex).top() <= (rectForIndex(current).bottom() - d->viewport->rect().height())
+ || rect.top() <= 0) {
+ rect.translate(0, 1);
+ if (rect.bottom() <= 0) {
+ return current;
+ }
+ intersectVector = d->intersectingSet(rect);
+ findindex = d->closestIndex(initialRect, intersectVector);
+ }
+ return findindex;
+ }
case MovePrevious:
case MoveUp:
while (intersectVector.isEmpty()) {
@@ -1202,13 +1250,29 @@ QModelIndex QListView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie
d->removeCurrentAndDisabled(&intersectVector, current);
}
return d->closestIndex(initialRect, intersectVector);
- case MovePageDown:
- // move current by (visibileRowCount - 1) items.
- // rect.translate(0, rect.height()); will happen in the switch fallthrough for MoveDown.
- rect.moveTop(rect.top() + d->viewport->height() - 2 * rect.height());
- if (rect.bottom() > contents.height() - rect.height())
- rect.moveBottom(contents.height() - rect.height());
- Q_FALLTHROUGH();
+ case MovePageDown: {
+ if (rect.height() >= d->viewport->height())
+ return moveCursor(QAbstractItemView::MoveDown, modifiers);
+
+ rect.moveTop(rect.top() + d->viewport->height() - 1);
+ if (rect.bottom() > contents.height() - rect.height()) {
+ rect.setTop(contents.height() - 1);
+ rect.setBottom(contents.height());
+ }
+ QModelIndex index = current;
+ // index's bottom() - current's top() always <= (d->viewport->rect().height()
+ while (intersectVector.isEmpty()
+ || rectForIndex(index).bottom() >= (d->viewport->rect().height() + rectForIndex(current).top())
+ || rect.bottom() > contents.height()) {
+ rect.translate(0, -1);
+ if (rect.top() >= contents.height()) {
+ return current;
+ }
+ intersectVector = d->intersectingSet(rect);
+ index = d->closestIndex(initialRect, intersectVector);
+ }
+ return index;
+ }
case MoveNext:
case MoveDown:
while (intersectVector.isEmpty()) {
@@ -1281,7 +1345,7 @@ void QListView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFl
if (!d->selectionModel)
return;
- // if we are wrapping, we can only selecte inside the contents rectangle
+ // if we are wrapping, we can only select inside the contents rectangle
int w = qMax(d->contentsSize().width(), d->viewport->width());
int h = qMax(d->contentsSize().height(), d->viewport->height());
if (d->wrap && !QRect(0, 0, w, h).intersects(rect))
@@ -1290,7 +1354,8 @@ void QListView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFl
QItemSelection selection;
if (rect.width() == 1 && rect.height() == 1) {
- const QVector<QModelIndex> intersectVector = d->intersectingSet(rect.translated(horizontalOffset(), verticalOffset()));
+ const QList<QModelIndex> intersectVector =
+ d->intersectingSet(rect.translated(horizontalOffset(), verticalOffset()));
QModelIndex tl;
if (!intersectVector.isEmpty())
tl = intersectVector.last(); // special case for mouse press; only select the top item
@@ -1303,7 +1368,7 @@ void QListView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFl
QModelIndex tl, br;
// get the first item
const QRect topLeft(rect.left() + horizontalOffset(), rect.top() + verticalOffset(), 1, 1);
- QVector<QModelIndex> intersectVector = d->intersectingSet(topLeft);
+ QList<QModelIndex> intersectVector = d->intersectingSet(topLeft);
if (!intersectVector.isEmpty())
tl = intersectVector.last();
// get the last item
@@ -1328,7 +1393,7 @@ void QListView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFl
top = bottom;
bottom = tmp;
}
- // if the rect are on differnet lines, expand
+ // if the rect are on different lines, expand
if (top.top() != bottom.top()) {
// top rectangle
if (isRightToLeft())
@@ -1467,8 +1532,7 @@ QModelIndexList QListView::selectedIndexes() const
auto ignorable = [this, d](const QModelIndex &index) {
return index.column() != d->column || index.parent() != d->root || isIndexHidden(index);
};
- viewSelected.erase(std::remove_if(viewSelected.begin(), viewSelected.end(), ignorable),
- viewSelected.end());
+ viewSelected.removeIf(ignorable);
return viewSelected;
}
@@ -1487,12 +1551,14 @@ void QListView::doItemsLayout()
setState(ExpandingState);
if (d->model->columnCount(d->root) > 0) { // no columns means no contents
d->resetBatchStartRow();
- if (layoutMode() == SinglePass)
+ if (layoutMode() == SinglePass) {
d->doItemsLayout(d->model->rowCount(d->root)); // layout everything
- else if (!d->batchLayoutTimer.isActive()) {
+ } else if (!d->batchLayoutTimer.isActive()) {
if (!d->doItemsLayout(d->batchSize)) // layout is done
d->batchLayoutTimer.start(0, this); // do a new batch as fast as possible
}
+ } else { // clear the QBspTree generated by the last layout
+ d->clear();
}
QAbstractItemView::doItemsLayout();
setState(oldState); // restoring the oldState
@@ -1509,7 +1575,8 @@ void QListView::updateGeometries()
verticalScrollBar()->setRange(0, 0);
} else {
QModelIndex index = d->model->index(0, d->column, d->root);
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
QSize step = d->itemSize(option, index);
d->commonListView->updateHorizontalScrollBar(step);
d->commonListView->updateVerticalScrollBar(step);
@@ -1560,6 +1627,12 @@ void QListView::setModelColumn(int column)
return;
d->column = column;
d->doDelayedItemsLayout();
+#if QT_CONFIG(accessibility)
+ if (QAccessible::isActive()) {
+ QAccessibleTableModelChangeEvent event(this, QAccessibleTableModelChangeEvent::ModelReset);
+ QAccessible::updateAccessibility(&event);
+ }
+#endif
}
int QListView::modelColumn() const
@@ -1688,7 +1761,7 @@ bool QListView::event(QEvent *e)
QListViewPrivate::QListViewPrivate()
: QAbstractItemViewPrivate(),
- commonListView(0),
+ commonListView(nullptr),
wrap(false),
space(0),
flow(QListView::TopToBottom),
@@ -1726,17 +1799,21 @@ void QListViewPrivate::prepareItemsLayout()
layoutBounds = QRect(QPoint(), q->maximumViewportSize());
int frameAroundContents = 0;
- if (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents))
- frameAroundContents = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
+ if (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) {
+ QStyleOption option;
+ option.initFrom(q);
+ frameAroundContents = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &option, q) * 2;
+ }
// maximumViewportSize() already takes scrollbar into account if policy is
// Qt::ScrollBarAlwaysOn but scrollbar extent must be deduced if policy
// is Qt::ScrollBarAsNeeded
- int verticalMargin = vbarpolicy==Qt::ScrollBarAsNeeded
- ? q->style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, vbar) + frameAroundContents
+ int verticalMargin = (vbarpolicy == Qt::ScrollBarAsNeeded) && (flow == QListView::LeftToRight || vbar->isVisible())
+ && !q->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, nullptr, vbar)
+ ? q->style()->pixelMetric(QStyle::PM_ScrollBarExtent, nullptr, vbar) + frameAroundContents
: 0;
int horizontalMargin = hbarpolicy==Qt::ScrollBarAsNeeded
- ? q->style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, hbar) + frameAroundContents
+ ? q->style()->pixelMetric(QStyle::PM_ScrollBarExtent, nullptr, hbar) + frameAroundContents
: 0;
layoutBounds.adjust(0, 0, -verticalMargin, -horizontalMargin);
@@ -1797,12 +1874,12 @@ QRect QListViewPrivate::mapToViewport(const QRect &rect, bool extend) const
}
QModelIndex QListViewPrivate::closestIndex(const QRect &target,
- const QVector<QModelIndex> &candidates) const
+ const QList<QModelIndex> &candidates) const
{
int distance = 0;
int shortest = INT_MAX;
QModelIndex closest;
- QVector<QModelIndex>::const_iterator it = candidates.begin();
+ QList<QModelIndex>::const_iterator it = candidates.begin();
for (; it != candidates.end(); ++it) {
if (!(*it).isValid())
@@ -1834,14 +1911,15 @@ QModelIndex QListViewPrivate::closestIndex(const QRect &target,
QSize QListViewPrivate::itemSize(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
+ Q_Q(const QListView);
if (!uniformItemSizes) {
- const QAbstractItemDelegate *delegate = delegateForIndex(index);
+ const QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
return delegate ? delegate->sizeHint(option, index) : QSize();
}
- if (!cachedItemSize.isValid()) { // the last item is probaly the largest, so we use its size
+ if (!cachedItemSize.isValid()) { // the last item is probably the largest, so we use its size
int row = model->rowCount(root) - 1;
QModelIndex sample = model->index(row, column, root);
- const QAbstractItemDelegate *delegate = delegateForIndex(sample);
+ const QAbstractItemDelegate *delegate = q->itemDelegateForIndex(sample);
cachedItemSize = delegate ? delegate->sizeHint(option, sample) : QSize();
}
return cachedItemSize;
@@ -1851,8 +1929,8 @@ QItemSelection QListViewPrivate::selection(const QRect &rect) const
{
QItemSelection selection;
QModelIndex tl, br;
- const QVector<QModelIndex> intersectVector = intersectingSet(rect);
- QVector<QModelIndex>::const_iterator it = intersectVector.begin();
+ const QList<QModelIndex> intersectVector = intersectingSet(rect);
+ QList<QModelIndex>::const_iterator it = intersectVector.begin();
for (; it != intersectVector.end(); ++it) {
if (!tl.isValid() && !br.isValid()) {
tl = br = *it;
@@ -1894,14 +1972,13 @@ bool QListViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QMo
}
#endif
-void QListViewPrivate::removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const
+void QListViewPrivate::removeCurrentAndDisabled(QList<QModelIndex> *indexes,
+ const QModelIndex &current) const
{
auto isCurrentOrDisabled = [this, current](const QModelIndex &index) {
return !isIndexEnabled(index) || index == current;
};
- indexes->erase(std::remove_if(indexes->begin(), indexes->end(),
- isCurrentOrDisabled),
- indexes->end());
+ indexes->removeIf(isCurrentOrDisabled);
}
/*
@@ -2090,11 +2167,11 @@ void QListModeViewBase::dragMoveEvent(QDragMoveEvent *event)
event->ignore();
// can't use indexAt, doesn't account for spacing.
- QPoint p = event->pos();
+ QPoint p = event->position().toPoint();
QRect rect(p.x() + horizontalOffset(), p.y() + verticalOffset(), 1, 1);
rect.adjust(-dd->spacing(), -dd->spacing(), dd->spacing(), dd->spacing());
- const QVector<QModelIndex> intersectVector = dd->intersectingSet(rect);
- QModelIndex index = intersectVector.count() > 0
+ const QList<QModelIndex> intersectVector = dd->intersectingSet(rect);
+ QModelIndex index = intersectVector.size() > 0
? intersectVector.last() : QModelIndex();
dd->hover = index;
if (!dd->droppingOnItself(event, index)
@@ -2102,7 +2179,7 @@ void QListModeViewBase::dragMoveEvent(QDragMoveEvent *event)
if (index.isValid() && dd->showDropIndicator) {
QRect rect = qq->visualRect(index);
- dd->dropIndicatorPosition = position(event->pos(), rect, index);
+ dd->dropIndicatorPosition = position(event->position().toPoint(), rect, index);
// if spacing, should try to draw between items, not just next to item.
switch (dd->dropIndicatorPosition) {
case QAbstractItemView::AboveItem:
@@ -2146,7 +2223,7 @@ void QListModeViewBase::dragMoveEvent(QDragMoveEvent *event)
dd->viewport->update();
} // can drop
- if (dd->shouldAutoScroll(event->pos()))
+ if (dd->shouldAutoScroll(event->position().toPoint()))
qq->startAutoScroll();
}
@@ -2167,13 +2244,13 @@ bool QListModeViewBase::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QM
return false;
QModelIndex index;
- if (dd->viewport->rect().contains(event->pos())) {
+ if (dd->viewport->rect().contains(event->position().toPoint())) {
// can't use indexAt, doesn't account for spacing.
- QPoint p = event->pos();
+ QPoint p = event->position().toPoint();
QRect rect(p.x() + horizontalOffset(), p.y() + verticalOffset(), 1, 1);
rect.adjust(-dd->spacing(), -dd->spacing(), dd->spacing(), dd->spacing());
- const QVector<QModelIndex> intersectVector = dd->intersectingSet(rect);
- index = intersectVector.count() > 0
+ const QList<QModelIndex> intersectVector = dd->intersectingSet(rect);
+ index = intersectVector.size() > 0
? intersectVector.last() : QModelIndex();
if (!index.isValid())
index = dd->root;
@@ -2184,7 +2261,7 @@ bool QListModeViewBase::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QM
int row = -1;
int col = -1;
if (index != dd->root) {
- dd->dropIndicatorPosition = position(event->pos(), qq->visualRect(index), index);
+ dd->dropIndicatorPosition = position(event->position().toPoint(), qq->visualRect(index), index);
switch (dd->dropIndicatorPosition) {
case QAbstractItemView::AboveItem:
row = index.row();
@@ -2219,7 +2296,7 @@ void QListModeViewBase::updateVerticalScrollBar(const QSize &step)
if (verticalScrollMode() == QAbstractItemView::ScrollPerItem
&& ((flow() == QListView::TopToBottom && !isWrapping())
|| (flow() == QListView::LeftToRight && isWrapping()))) {
- const int steps = (flow() == QListView::TopToBottom ? scrollValueMap : segmentPositions).count() - 1;
+ const int steps = (flow() == QListView::TopToBottom ? scrollValueMap : segmentPositions).size() - 1;
if (steps > 0) {
const int pageSteps = perItemScrollingPageSteps(viewport()->height(), contentsSize.height(), isWrapping());
verticalScrollBar()->setSingleStep(1);
@@ -2240,7 +2317,7 @@ void QListModeViewBase::updateHorizontalScrollBar(const QSize &step)
if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem
&& ((flow() == QListView::TopToBottom && isWrapping())
|| (flow() == QListView::LeftToRight && !isWrapping()))) {
- int steps = (flow() == QListView::TopToBottom ? segmentPositions : scrollValueMap).count() - 1;
+ int steps = (flow() == QListView::TopToBottom ? segmentPositions : scrollValueMap).size() - 1;
if (steps > 0) {
const int pageSteps = perItemScrollingPageSteps(viewport()->width(), contentsSize.width(), isWrapping());
horizontalScrollBar()->setSingleStep(1);
@@ -2264,10 +2341,10 @@ int QListModeViewBase::verticalScrollToValue(int index, QListView::ScrollHint hi
} else {
int scrollBarValue = verticalScrollBar()->value();
int numHidden = 0;
- for (const auto &idx : qAsConst(dd->hiddenRows))
+ for (const auto &idx : std::as_const(dd->hiddenRows))
if (idx.row() <= scrollBarValue)
++numHidden;
- value = qBound(0, scrollValueMap.at(verticalScrollBar()->value()) - numHidden, flowPositions.count() - 1);
+ value = qBound(0, scrollValueMap.at(verticalScrollBar()->value()) - numHidden, flowPositions.size() - 1);
}
if (above)
hint = QListView::PositionAtTop;
@@ -2287,7 +2364,7 @@ int QListModeViewBase::horizontalOffset() const
if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem) {
if (isWrapping()) {
if (flow() == QListView::TopToBottom && !segmentPositions.isEmpty()) {
- const int max = segmentPositions.count() - 1;
+ const int max = segmentPositions.size() - 1;
int currentValue = qBound(0, horizontalScrollBar()->value(), max);
int position = segmentPositions.at(currentValue);
int maximumValue = qBound(0, horizontalScrollBar()->maximum(), max);
@@ -2309,13 +2386,13 @@ int QListModeViewBase::verticalOffset() const
if (isWrapping()) {
if (flow() == QListView::LeftToRight && !segmentPositions.isEmpty()) {
int value = verticalScrollBar()->value();
- if (value >= segmentPositions.count())
+ if (value >= segmentPositions.size())
return 0;
return segmentPositions.at(value) - spacing();
}
} else if (flow() == QListView::TopToBottom && !flowPositions.isEmpty()) {
int value = verticalScrollBar()->value();
- if (value > scrollValueMap.count())
+ if (value > scrollValueMap.size())
return 0;
return flowPositions.at(scrollValueMap.at(value)) - spacing();
}
@@ -2333,7 +2410,7 @@ int QListModeViewBase::horizontalScrollToValue(int index, QListView::ScrollHint
if (scrollValueMap.isEmpty())
value = 0;
else
- value = qBound(0, scrollValueMap.at(horizontalScrollBar()->value()), flowPositions.count() - 1);
+ value = qBound(0, scrollValueMap.at(horizontalScrollBar()->value()), flowPositions.size() - 1);
if (leftOf)
hint = QListView::PositionAtTop;
else if (rightOf)
@@ -2355,7 +2432,7 @@ void QListModeViewBase::scrollContentsBy(int dx, int dy, bool scrollElasticBand)
if (isWrapping()) {
if (segmentPositions.isEmpty())
return;
- const int max = segmentPositions.count() - 1;
+ const int max = segmentPositions.size() - 1;
if (horizontal && flow() == QListView::TopToBottom && dx != 0) {
int currentValue = qBound(0, horizontalValue, max);
int previousValue = qBound(0, currentValue + dx, max);
@@ -2372,7 +2449,7 @@ void QListModeViewBase::scrollContentsBy(int dx, int dy, bool scrollElasticBand)
} else {
if (flowPositions.isEmpty())
return;
- const int max = scrollValueMap.count() - 1;
+ const int max = scrollValueMap.size() - 1;
if (vertical && flow() == QListView::TopToBottom && dy != 0) {
int currentValue = qBound(0, verticalValue, max);
int previousValue = qBound(0, currentValue + dy, max);
@@ -2400,14 +2477,15 @@ QListViewItem QListModeViewBase::indexToListViewItem(const QModelIndex &index) c
{
if (flowPositions.isEmpty()
|| segmentPositions.isEmpty()
- || index.row() >= flowPositions.count() - 1)
+ || index.row() >= flowPositions.size() - 1)
return QListViewItem();
const int segment = qBinarySearch<int>(segmentStartRows, index.row(),
- 0, segmentStartRows.count() - 1);
+ 0, segmentStartRows.size() - 1);
- QStyleOptionViewItem options = viewOptions();
+ QStyleOptionViewItem options;
+ initViewItemOption(&options);
options.rect.setSize(contentsSize);
QSize size = (uniformItemSizes() && cachedItemSize().isValid())
? cachedItemSize() : itemSize(options, index);
@@ -2421,7 +2499,7 @@ QListViewItem QListModeViewBase::indexToListViewItem(const QModelIndex &index) c
pos.setY(flowPositions.at(index.row()));
pos.setX(segmentPositions.at(segment));
if (isWrapping()) { // make the items as wide as the segment
- int right = (segment + 1 >= segmentPositions.count()
+ int right = (segment + 1 >= segmentPositions.size()
? contentsSize.width()
: segmentPositions.at(segment + 1));
cellSize.setWidth(right - pos.x());
@@ -2483,7 +2561,8 @@ void QListModeViewBase::doStaticLayout(const QListViewLayoutInfo &info)
{
const bool useItemSize = !info.grid.isValid();
const QPoint topLeft = initStaticLayout(info);
- QStyleOptionViewItem option = viewOptions();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
option.rect = info.bounds;
option.rect.adjust(info.spacing, info.spacing, -info.spacing, -info.spacing);
@@ -2544,7 +2623,7 @@ void QListModeViewBase::doStaticLayout(const QListViewLayoutInfo &info)
deltaSegPosition = 0;
}
// save the flow position of this item
- scrollValueMap.append(flowPositions.count());
+ scrollValueMap.append(flowPositions.size());
flowPositions.append(flowPosition);
// prepare for the next item
deltaSegPosition = qMax(deltaSegHint, deltaSegPosition);
@@ -2560,21 +2639,21 @@ void QListModeViewBase::doStaticLayout(const QListViewLayoutInfo &info)
// set the contents size
QRect rect = info.bounds;
if (info.flow == QListView::LeftToRight) {
- rect.setRight(segmentPositions.count() == 1 ? flowPosition : info.bounds.right());
+ rect.setRight(segmentPositions.size() == 1 ? flowPosition : info.bounds.right());
rect.setBottom(segPosition + deltaSegPosition);
} else { // TopToBottom
rect.setRight(segPosition + deltaSegPosition);
- rect.setBottom(segmentPositions.count() == 1 ? flowPosition : info.bounds.bottom());
+ rect.setBottom(segmentPositions.size() == 1 ? flowPosition : info.bounds.bottom());
}
contentsSize = QSize(rect.right(), rect.bottom());
// if it is the last batch, save the end of the segments
if (info.last == info.max) {
segmentExtents.append(flowPosition);
- scrollValueMap.append(flowPositions.count());
+ scrollValueMap.append(flowPositions.size());
flowPositions.append(flowPosition);
segmentPositions.append(info.wrap ? segPosition + deltaSegPosition : INT_MAX);
}
- // if the new items are visble, update the viewport
+ // if the new items are visible, update the viewport
QRect changedRect(topLeft, rect.bottomRight());
if (clipRect().intersects(changedRect))
viewport()->update();
@@ -2585,9 +2664,9 @@ void QListModeViewBase::doStaticLayout(const QListViewLayoutInfo &info)
Finds the set of items intersecting with \a area.
In this function, itemsize is counted from topleft to the start of the next item.
*/
-QVector<QModelIndex> QListModeViewBase::intersectingSet(const QRect &area) const
+QList<QModelIndex> QListModeViewBase::intersectingSet(const QRect &area) const
{
- QVector<QModelIndex> ret;
+ QList<QModelIndex> ret;
int segStartPosition;
int segEndPosition;
int flowStartPosition;
@@ -2603,10 +2682,10 @@ QVector<QModelIndex> QListModeViewBase::intersectingSet(const QRect &area) const
flowStartPosition = area.top();
flowEndPosition = area.bottom();
}
- if (segmentPositions.count() < 2 || flowPositions.isEmpty())
+ if (segmentPositions.size() < 2 || flowPositions.isEmpty())
return ret;
// the last segment position is actually the edge of the last segment
- const int segLast = segmentPositions.count() - 2;
+ const int segLast = segmentPositions.size() - 2;
int seg = qBinarySearch<int>(segmentPositions, segStartPosition, 0, segLast + 1);
for (; seg <= segLast && segmentPositions.at(seg) <= segEndPosition; ++seg) {
int first = segmentStartRows.at(seg);
@@ -2664,7 +2743,7 @@ QRect QListModeViewBase::mapToViewport(const QRect &rect) const
int QListModeViewBase::perItemScrollingPageSteps(int length, int bounds, bool wrap) const
{
- QVector<int> positions;
+ QList<int> positions;
if (wrap)
positions = segmentPositions;
else if (!flowPositions.isEmpty()) {
@@ -2673,15 +2752,15 @@ int QListModeViewBase::perItemScrollingPageSteps(int length, int bounds, bool wr
positions.append(flowPositions.at(itemShown));
}
if (positions.isEmpty() || bounds <= length)
- return positions.count();
+ return positions.size();
if (uniformItemSizes()) {
- for (int i = 1; i < positions.count(); ++i)
+ for (int i = 1; i < positions.size(); ++i)
if (positions.at(i) > 0)
return length / positions.at(i);
return 0; // all items had height 0
}
int pageSteps = 0;
- int steps = positions.count() - 1;
+ int steps = positions.size() - 1;
int max = qMax(length, bounds);
int min = qMin(length, bounds);
int pos = min - (max - positions.constLast());
@@ -2705,7 +2784,7 @@ int QListModeViewBase::perItemScrollToValue(int index, int scrollValue, int view
return scrollValue;
itemExtent += spacing();
- QVector<int> hiddenRows = dd->hiddenRowIds();
+ QList<int> hiddenRows = dd->hiddenRowIds();
std::sort(hiddenRows.begin(), hiddenRows.end());
int hiddenRowsBefore = 0;
for (int i = 0; i < hiddenRows.size() - 1; ++i)
@@ -2743,7 +2822,7 @@ int QListModeViewBase::perItemScrollToValue(int index, int scrollValue, int view
// ### wrapped scrolling in the flow direction
return flowPositions.at(index + hiddenRowsBefore); // ### always pixel based for now
} else if (!segmentStartRows.isEmpty()) { // we are scrolling in the "segment" direction
- int segment = qBinarySearch<int>(segmentStartRows, index, 0, segmentStartRows.count() - 1);
+ int segment = qBinarySearch<int>(segmentStartRows, index, 0, segmentStartRows.size() - 1);
int leftSegment = segment;
const int rightSegment = leftSegment;
const int bottomCoordinate = segmentPositions.at(segment);
@@ -2786,7 +2865,7 @@ void QListModeViewBase::clear()
void QIconModeViewBase::setPositionForIndex(const QPoint &position, const QModelIndex &index)
{
- if (index.row() >= items.count())
+ if (index.row() >= items.size())
return;
const QSize oldContents = contentsSize;
qq->update(index); // update old position
@@ -2799,7 +2878,7 @@ void QIconModeViewBase::setPositionForIndex(const QPoint &position, const QModel
void QIconModeViewBase::appendHiddenRow(int row)
{
- if (row >= 0 && row < items.count()) //remove item
+ if (row >= 0 && row < items.size()) //remove item
tree.removeLeaf(items.at(row).rect(), row);
QCommonListViewBase::appendHiddenRow(row);
}
@@ -2807,7 +2886,7 @@ void QIconModeViewBase::appendHiddenRow(int row)
void QIconModeViewBase::removeHiddenRow(int row)
{
QCommonListViewBase::removeHiddenRow(row);
- if (row >= 0 && row < items.count()) //insert item
+ if (row >= 0 && row < items.size()) //insert item
tree.insertLeaf(items.at(row).rect(), row);
}
@@ -2818,7 +2897,7 @@ bool QIconModeViewBase::filterStartDrag(Qt::DropActions supportedActions)
// plus adding viewitems to the draggedItems list.
// We need these items to draw the drag items
QModelIndexList indexes = dd->selectionModel->selectedIndexes();
- if (indexes.count() > 0 ) {
+ if (indexes.size() > 0 ) {
if (viewport()->acceptDrops()) {
QModelIndexList::ConstIterator it = indexes.constBegin();
for (; it != indexes.constEnd(); ++it)
@@ -2834,12 +2913,15 @@ bool QIconModeViewBase::filterStartDrag(Qt::DropActions supportedActions)
drag->setMimeData(dd->model->mimeData(indexes));
drag->setPixmap(pixmap);
drag->setHotSpot(dd->pressedPosition - rect.topLeft());
+ dd->dropEventMoved = false;
Qt::DropAction action = drag->exec(supportedActions, dd->defaultDropAction);
draggedItems.clear();
- // for internal moves the action was set to Qt::CopyAction in
- // filterDropEvent() to avoid the deletion here
- if (action == Qt::MoveAction)
- dd->clearOrRemove();
+ // delete item, unless it has already been moved internally (see filterDropEvent)
+ if (action == Qt::MoveAction && !dd->dropEventMoved) {
+ if (dd->dragDropMode != QAbstractItemView::InternalMove || drag->target() == qq->viewport())
+ dd->clearOrRemove();
+ }
+ dd->dropEventMoved = false;
}
return true;
}
@@ -2851,10 +2933,10 @@ bool QIconModeViewBase::filterDropEvent(QDropEvent *e)
const QSize contents = contentsSize;
QPoint offset(horizontalOffset(), verticalOffset());
- QPoint end = e->pos() + offset;
+ QPoint end = e->position().toPoint() + offset;
if (qq->acceptDrops()) {
const Qt::ItemFlags dropableFlags = Qt::ItemIsDropEnabled|Qt::ItemIsEnabled;
- const QVector<QModelIndex> &dropIndices = intersectingSet(QRect(end, QSize(1, 1)));
+ const QList<QModelIndex> &dropIndices = intersectingSet(QRect(end, QSize(1, 1)));
for (const QModelIndex &index : dropIndices)
if ((index.flags() & dropableFlags) == dropableFlags)
return false;
@@ -2875,9 +2957,9 @@ bool QIconModeViewBase::filterDropEvent(QDropEvent *e)
draggedItems.clear();
dd->emitIndexesMoved(indexes);
// do not delete item on internal move, see filterStartDrag()
- e->setDropAction(Qt::CopyAction);
+ dd->dropEventMoved = true;
e->accept(); // we have handled the event
- // if the size has not grown, we need to check if it has shrinked
+ // if the size has not grown, we need to check if it has shrunk
if (contentsSize != contents) {
if ((contentsSize.width() <= contents.width()
|| contentsSize.height() <= contents.height())) {
@@ -2912,17 +2994,17 @@ bool QIconModeViewBase::filterDragMoveEvent(QDragMoveEvent *e)
QRect itemsRect = this->itemsRect(draggedItems);
viewport()->update(itemsRect.translated(draggedItemsDelta()));
// update position
- draggedItemsPos = e->pos();
+ draggedItemsPos = e->position().toPoint();
// get new items rect
viewport()->update(itemsRect.translated(draggedItemsDelta()));
// set the item under the cursor to current
QModelIndex index;
if (movement() == QListView::Snap) {
- QRect rect(snapToGrid(e->pos() + offset()), gridSize());
- const QVector<QModelIndex> intersectVector = intersectingSet(rect);
- index = intersectVector.count() > 0 ? intersectVector.last() : QModelIndex();
+ QRect rect(snapToGrid(e->position().toPoint() + offset()), gridSize());
+ const QList<QModelIndex> intersectVector = intersectingSet(rect);
+ index = intersectVector.size() > 0 ? intersectVector.last() : QModelIndex();
} else {
- index = qq->indexAt(e->pos());
+ index = qq->indexAt(e->position().toPoint());
}
// check if we allow drops here
if (draggedItems.contains(index))
@@ -2933,7 +3015,7 @@ bool QIconModeViewBase::filterDragMoveEvent(QDragMoveEvent *e)
e->accept(); // allow dropping in empty areas
// the event was treated. do autoscrolling
- if (dd->shouldAutoScroll(e->pos()))
+ if (dd->shouldAutoScroll(e->position().toPoint()))
dd->startAutoScroll();
return true;
}
@@ -2957,8 +3039,9 @@ void QIconModeViewBase::scrollContentsBy(int dx, int dy, bool scrollElasticBand)
void QIconModeViewBase::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
if (column() >= topLeft.column() && column() <= bottomRight.column()) {
- const QStyleOptionViewItem option = viewOptions();
- const int bottom = qMin(items.count(), bottomRight.row() + 1);
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+ const int bottom = qMin(items.size(), bottomRight.row() + 1);
const bool useItemSize = !dd->grid.isValid();
for (int row = topLeft.row(); row < bottom; ++row)
{
@@ -2975,10 +3058,11 @@ void QIconModeViewBase::dataChanged(const QModelIndex &topLeft, const QModelInde
bool QIconModeViewBase::doBatchedItemLayout(const QListViewLayoutInfo &info, int max)
{
- if (info.last >= items.count()) {
+ if (info.last >= items.size()) {
//first we create the items
- QStyleOptionViewItem option = viewOptions();
- for (int row = items.count(); row <= info.last; ++row) {
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+ for (int row = items.size(); row <= info.last; ++row) {
QSize size = itemSize(option, modelIndex(row));
QListViewItem item(QRect(0, 0, size.width(), size.height()), row); // default pos
items.append(item);
@@ -2990,7 +3074,7 @@ bool QIconModeViewBase::doBatchedItemLayout(const QListViewLayoutInfo &info, int
QListViewItem QIconModeViewBase::indexToListViewItem(const QModelIndex &index) const
{
- if (index.isValid() && index.row() < items.count())
+ if (index.isValid() && index.row() < items.size())
return items.at(index.row());
return QListViewItem();
}
@@ -3068,11 +3152,12 @@ void QIconModeViewBase::doDynamicLayout(const QListViewLayoutInfo &info)
segPosition = topLeft.x();
}
- if (moved.count() != items.count())
- moved.resize(items.count());
+ if (moved.size() != items.size())
+ moved.resize(items.size());
QRect rect(QPoint(), topLeft);
- QListViewItem *item = 0;
+ QListViewItem *item = nullptr;
+ Q_ASSERT(info.first <= info.last);
for (int row = info.first; row <= info.last; ++row) {
item = &items[row];
if (isHidden(row)) {
@@ -3143,6 +3228,7 @@ void QIconModeViewBase::doDynamicLayout(const QListViewLayoutInfo &info)
flowPosition += deltaFlowPosition; // current position + item width + gap
}
}
+ Q_ASSERT(item);
batchSavedDeltaSeg = deltaSegPosition;
batchStartRow = info.last + 1;
bool done = (info.last >= rowCount() - 1);
@@ -3165,32 +3251,28 @@ void QIconModeViewBase::doDynamicLayout(const QListViewLayoutInfo &info)
// insert items in tree
for (int row = insertFrom; row <= info.last; ++row)
tree.insertLeaf(items.at(row).rect(), row);
- // if the new items are visble, update the viewport
+ // if the new items are visible, update the viewport
QRect changedRect(topLeft, rect.bottomRight());
if (clipRect().intersects(changedRect))
viewport()->update();
}
-QVector<QModelIndex> QIconModeViewBase::intersectingSet(const QRect &area) const
+QList<QModelIndex> QIconModeViewBase::intersectingSet(const QRect &area) const
{
QIconModeViewBase *that = const_cast<QIconModeViewBase*>(this);
QBspTree::Data data(static_cast<void*>(that));
- QVector<QModelIndex> res;
+ QList<QModelIndex> res;
that->interSectingVector = &res;
that->tree.climbTree(area, &QIconModeViewBase::addLeaf, data);
- that->interSectingVector = 0;
+ that->interSectingVector = nullptr;
return res;
}
-QRect QIconModeViewBase::itemsRect(const QVector<QModelIndex> &indexes) const
+QRect QIconModeViewBase::itemsRect(const QList<QModelIndex> &indexes) const
{
- QVector<QModelIndex>::const_iterator it = indexes.begin();
- QListViewItem item = indexToListViewItem(*it);
- QRect rect(item.x, item.y, item.w, item.h);
- for (; it != indexes.end(); ++it) {
- item = indexToListViewItem(*it);
- rect |= viewItemRect(item);
- }
+ QRect rect;
+ for (const auto &index : indexes)
+ rect |= viewItemRect(indexToListViewItem(index));
return rect;
}
@@ -3199,15 +3281,15 @@ int QIconModeViewBase::itemIndex(const QListViewItem &item) const
if (!item.isValid())
return -1;
int i = item.indexHint;
- if (i < items.count()) {
+ if (i < items.size()) {
if (items.at(i) == item)
return i;
} else {
- i = items.count() - 1;
+ i = items.size() - 1;
}
int j = i;
- int c = items.count();
+ int c = items.size();
bool a = true;
bool b = true;
@@ -3230,14 +3312,14 @@ int QIconModeViewBase::itemIndex(const QListViewItem &item) const
return -1;
}
-void QIconModeViewBase::addLeaf(QVector<int> &leaf, const QRect &area,
- uint visited, QBspTree::Data data)
+void QIconModeViewBase::addLeaf(QList<int> &leaf, const QRect &area, uint visited,
+ QBspTree::Data data)
{
QListViewItem *vi;
QIconModeViewBase *_this = static_cast<QIconModeViewBase *>(data.ptr);
- for (int i = 0; i < leaf.count(); ++i) {
+ for (int i = 0; i < leaf.size(); ++i) {
int idx = leaf.at(i);
- if (idx < 0 || idx >= _this->items.count())
+ if (idx < 0 || idx >= _this->items.size())
continue;
vi = &_this->items[idx];
Q_ASSERT(vi);
@@ -3265,8 +3347,8 @@ void QIconModeViewBase::moveItem(int index, const QPoint &dest)
contentsSize = (QRect(QPoint(0, 0), contentsSize)|QRect(dest, rect.size())).size();
// mark the item as moved
- if (moved.count() != items.count())
- moved.resize(items.count());
+ if (moved.size() != items.size())
+ moved.resize(items.size());
moved.setBit(index, true);
}
@@ -3318,7 +3400,7 @@ void QIconModeViewBase::clear()
void QIconModeViewBase::updateContentsSize()
{
QRect bounding;
- for (int i = 0; i < items.count(); ++i)
+ for (int i = 0; i < items.size(); ++i)
bounding |= items.at(i).rect();
contentsSize = bounding.size();
}
@@ -3328,9 +3410,10 @@ void QIconModeViewBase::updateContentsSize()
*/
void QListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
-#ifndef QT_NO_ACCESSIBILITY
+ QAbstractItemView::currentChanged(current, previous);
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
- if (current.isValid()) {
+ if (current.isValid() && hasFocus()) {
int entry = visualIndex(current);
QAccessibleEvent event(this, QAccessible::Focus);
event.setChild(entry);
@@ -3338,7 +3421,6 @@ void QListView::currentChanged(const QModelIndex &current, const QModelIndex &pr
}
}
#endif
- QAbstractItemView::currentChanged(current, previous);
}
/*!
@@ -3347,7 +3429,7 @@ void QListView::currentChanged(const QModelIndex &current, const QModelIndex &pr
void QListView::selectionChanged(const QItemSelection &selected,
const QItemSelection &deselected)
{
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
// ### does not work properly for selection ranges.
QModelIndex sel = selected.indexes().value(0);
@@ -3375,7 +3457,7 @@ int QListView::visualIndex(const QModelIndex &index) const
d->executePostedLayout();
QListViewItem itm = d->indexToListViewItem(index);
int visualIndex = d->commonListView->itemIndex(itm);
- for (const auto &idx : qAsConst(d->hiddenRows)) {
+ for (const auto &idx : std::as_const(d->hiddenRows)) {
if (idx.row() <= index.row())
--visualIndex;
}
@@ -3389,7 +3471,47 @@ int QListView::visualIndex(const QModelIndex &index) const
*/
QSize QListView::viewportSizeHint() const
{
- return QAbstractItemView::viewportSizeHint();
+ Q_D(const QListView);
+ // We don't have a nice simple size hint for invalid or wrapping list views.
+ if (!d->model)
+ return QAbstractItemView::viewportSizeHint();
+ const int rc = d->model->rowCount();
+ if (rc == 0 || isWrapping())
+ return QAbstractItemView::viewportSizeHint();
+
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+
+ if (uniformItemSizes()) {
+ QSize sz = d->cachedItemSize;
+ if (!sz.isValid()) {
+ QModelIndex idx = d->model->index(0, d->column, d->root);
+ sz = d->itemSize(option, idx);
+ }
+ sz.setHeight(rc * sz.height());
+ return sz;
+ }
+
+ // Using AdjustToContents with a high number of rows will normally not make sense, so we limit
+ // this to default 1000 (that is btw the default for QHeaderView::resizeContentsPrecision())
+ // (By setting the property _q_resizeContentPrecision the user can however override this).
+ int maximumRows = 1000;
+ const QVariant userOverrideValue = property("_q_resizeContentPrecision");
+ if (userOverrideValue.isValid() && userOverrideValue.toInt() > 0) {
+ maximumRows = userOverrideValue.toInt();
+ }
+ const int rowCount = qMin(rc, maximumRows);
+
+ int h = 0;
+ int w = 0;
+
+ for (int row = 0; row < rowCount; ++row) {
+ QModelIndex idx = d->model->index(row, d->column, d->root);
+ QSize itemSize = d->itemSize(option, idx);
+ h += itemSize.height();
+ w = qMax(w, itemSize.width());
+ }
+ return QSize(w, h);
}
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qlistview.h b/src/widgets/itemviews/qlistview.h
index 8a5d5e02ae..31b02d660e 100644
--- a/src/widgets/itemviews/qlistview.h
+++ b/src/widgets/itemviews/qlistview.h
@@ -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
#ifndef QLISTVIEW_H
#define QLISTVIEW_H
@@ -150,7 +114,8 @@ protected:
void resizeContents(int width, int height);
QSize contentsSize() const;
- void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override;
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles = QList<int>()) override;
void rowsInserted(const QModelIndex &parent, int start, int end) override;
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override;
@@ -169,7 +134,7 @@ protected:
void startDrag(Qt::DropActions supportedActions) override;
#endif // QT_CONFIG(draganddrop)
- QStyleOptionViewItem viewOptions() const override;
+ void initViewItemOption(QStyleOptionViewItem *option) const override;
void paintEvent(QPaintEvent *e) override;
int horizontalOffset() const override;
@@ -193,6 +158,7 @@ protected:
private:
int visualIndex(const QModelIndex &index) const;
+ friend class QCommonListViewBase;
Q_DECLARE_PRIVATE(QListView)
Q_DISABLE_COPY(QListView)
diff --git a/src/widgets/itemviews/qlistview_p.h b/src/widgets/itemviews/qlistview_p.h
index 86331bb862..40dabf5656 100644
--- a/src/widgets/itemviews/qlistview_p.h
+++ b/src/widgets/itemviews/qlistview_p.h
@@ -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
#ifndef QLISTVIEW_P_H
#define QLISTVIEW_P_H
@@ -52,6 +16,7 @@
//
#include <QtWidgets/private/qtwidgetsglobal_p.h>
+#include "qlistview.h"
#include "private/qabstractitemview_p.h"
#include "qbitarray.h"
#include "qbsptree_p.h"
@@ -68,28 +33,28 @@ class QListViewItem
friend class QListModeViewBase;
friend class QIconModeViewBase;
public:
- Q_DECL_CONSTEXPR QListViewItem()
+ constexpr QListViewItem()
: x(-1), y(-1), w(0), h(0), indexHint(-1), visited(0xffff) {}
- Q_DECL_CONSTEXPR QListViewItem(QRect r, int i)
+ constexpr QListViewItem(QRect r, int i)
: x(r.x()), y(r.y()), w(qMin(r.width(), SHRT_MAX)), h(qMin(r.height(), SHRT_MAX)),
indexHint(i), visited(0xffff) {}
- Q_DECL_CONSTEXPR bool operator==(const QListViewItem &other) const {
+ constexpr bool operator==(const QListViewItem &other) const {
return (x == other.x && y == other.y && w == other.w && h == other.h &&
indexHint == other.indexHint); }
- Q_DECL_CONSTEXPR bool operator!=(const QListViewItem &other) const
+ constexpr bool operator!=(const QListViewItem &other) const
{ return !(*this == other); }
- Q_DECL_CONSTEXPR bool isValid() const
+ constexpr bool isValid() const
{ return rect().isValid() && (indexHint > -1); }
- Q_DECL_RELAXED_CONSTEXPR void invalidate()
+ constexpr void invalidate()
{ x = -1; y = -1; w = 0; h = 0; }
- Q_DECL_RELAXED_CONSTEXPR void resize(QSize size)
+ constexpr void resize(QSize size)
{ w = qMin(size.width(), SHRT_MAX); h = qMin(size.height(), SHRT_MAX); }
- Q_DECL_RELAXED_CONSTEXPR void move(QPoint position)
+ constexpr void move(QPoint position)
{ x = position.x(); y = position.y(); }
- Q_DECL_CONSTEXPR int width() const { return w; }
- Q_DECL_CONSTEXPR int height() const { return h; }
+ constexpr int width() const { return w; }
+ constexpr int height() const { return h; }
private:
- Q_DECL_CONSTEXPR QRect rect() const
+ constexpr QRect rect() const
{ return QRect(x, y, w, h); }
int x, y;
short w, h;
@@ -126,7 +91,7 @@ public:
virtual bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) = 0;
virtual void clear() = 0;
virtual void setRowCount(int) = 0;
- virtual QVector<QModelIndex> intersectingSet(const QRect &area) const = 0;
+ virtual QList<QModelIndex> intersectingSet(const QRect &area) const = 0;
virtual void dataChanged(const QModelIndex &, const QModelIndex &) = 0;
virtual int horizontalScrollToValue(int index, QListView::ScrollHint hint,
@@ -172,7 +137,7 @@ public:
inline QModelIndex modelIndex(int row) const;
inline int rowCount() const;
- inline QStyleOptionViewItem viewOptions() const;
+ inline void initViewItemOption(QStyleOptionViewItem *option) const;
inline QWidget *viewport() const;
inline QRect clipRect() const;
@@ -198,11 +163,11 @@ class QListModeViewBase : public QCommonListViewBase
public:
QListModeViewBase(QListView *q, QListViewPrivate *d);
- QVector<int> flowPositions;
- QVector<int> segmentPositions;
- QVector<int> segmentStartRows;
- QVector<int> segmentExtents;
- QVector<int> scrollValueMap;
+ QList<int> flowPositions;
+ QList<int> segmentPositions;
+ QList<int> segmentStartRows;
+ QList<int> segmentExtents;
+ QList<int> scrollValueMap;
// used when laying out in batches
int batchSavedPosition;
@@ -213,7 +178,7 @@ public:
bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override;
void clear() override;
void setRowCount(int rowCount) override { flowPositions.resize(rowCount); }
- QVector<QModelIndex> intersectingSet(const QRect &area) const override;
+ QList<QModelIndex> intersectingSet(const QRect &area) const override;
void dataChanged(const QModelIndex &, const QModelIndex &) override;
int horizontalScrollToValue(int index, QListView::ScrollHint hint,
@@ -251,14 +216,14 @@ public:
QIconModeViewBase(QListView *q, QListViewPrivate *d) : QCommonListViewBase(q, d), interSectingVector(nullptr) {}
QBspTree tree;
- QVector<QListViewItem> items;
+ QList<QListViewItem> items;
QBitArray moved;
- QVector<QModelIndex> draggedItems; // indices to the tree.itemVector
+ QList<QModelIndex> draggedItems; // indices to the tree.itemVector
mutable QPoint draggedItemsPos;
// used when laying out in batches
- QVector<QModelIndex> *interSectingVector; //used from within intersectingSet
+ QList<QModelIndex> *interSectingVector; // used from within intersectingSet
//reimplementations
int itemIndex(const QListViewItem &item) const override;
@@ -266,7 +231,7 @@ public:
bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override;
void clear() override;
void setRowCount(int rowCount) override;
- QVector<QModelIndex> intersectingSet(const QRect &area) const override;
+ QList<QModelIndex> intersectingSet(const QRect &area) const override;
void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override;
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override;
@@ -285,14 +250,13 @@ private:
void initBspTree(const QSize &contents);
QPoint initDynamicLayout(const QListViewLayoutInfo &info);
void doDynamicLayout(const QListViewLayoutInfo &info);
- static void addLeaf(QVector<int> &leaf, const QRect &area,
- uint visited, QBspTree::Data data);
- QRect itemsRect(const QVector<QModelIndex> &indexes) const;
+ static void addLeaf(QList<int> &leaf, const QRect &area, uint visited, QBspTree::Data data);
+ QRect itemsRect(const QList<QModelIndex> &indexes) const;
QRect draggedItemsRect() const;
QPoint snapToGrid(const QPoint &pos) const;
void updateContentsSize();
QPoint draggedItemsDelta() const;
- void drawItems(QPainter *painter, const QVector<QModelIndex> &indexes) const;
+ void drawItems(QPainter *painter, const QList<QModelIndex> &indexes) const;
void moveItem(int index, const QPoint &dest);
};
@@ -309,7 +273,8 @@ public:
bool doItemsLayout(int num);
- inline QVector<QModelIndex> intersectingSet(const QRect &area, bool doLayout = true) const {
+ inline QList<QModelIndex> intersectingSet(const QRect &area, bool doLayout = true) const
+ {
if (doLayout) executePostedLayout();
QRect a = (q_func()->isRightToLeft() ? flipX(area.normalized()) : area.normalized());
return commonListView->intersectingSet(a);
@@ -363,7 +328,7 @@ public:
QRect mapToViewport(const QRect &rect, bool extend = true) const;
- QModelIndex closestIndex(const QRect &target, const QVector<QModelIndex> &candidates) const;
+ QModelIndex closestIndex(const QRect &target, const QList<QModelIndex> &candidates) const;
QSize itemSize(const QStyleOptionViewItem &option, const QModelIndex &index) const;
bool selectionAllowed(const QModelIndex &index) const override
@@ -395,8 +360,9 @@ public:
return isPersistent(idx) && hiddenRows.contains(idx);
}
// helper to avoid checking for isPersistent and creating persistent indexes as above in isHidden
- QVector<int> hiddenRowIds() const {
- QVector<int> rowIds;
+ QList<int> hiddenRowIds() const
+ {
+ QList<int> rowIds;
rowIds.reserve(hiddenRows.size());
for (const auto &idx : hiddenRows)
rowIds += idx.row();
@@ -404,7 +370,7 @@ public:
}
inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); }
- void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const;
+ void removeCurrentAndDisabled(QList<QModelIndex> *indexes, const QModelIndex &current) const;
void scrollElasticBandBy(int dx, int dy);
@@ -482,7 +448,7 @@ inline QModelIndex QCommonListViewBase::modelIndex(int row) const
{ return dd->model->index(row, dd->column, dd->root); }
inline int QCommonListViewBase::rowCount() const { return dd->model->rowCount(dd->root); }
-inline QStyleOptionViewItem QCommonListViewBase::viewOptions() const { return dd->viewOptionsV1(); }
+inline void QCommonListViewBase::initViewItemOption(QStyleOptionViewItem *option) const { qq->initViewItemOption(option); }
inline QWidget *QCommonListViewBase::viewport() const { return dd->viewport; }
inline QRect QCommonListViewBase::clipRect() const { return dd->clipRect(); }
@@ -492,10 +458,10 @@ inline QSize QCommonListViewBase::itemSize(const QStyleOptionViewItem &opt, cons
{ return dd->itemSize(opt, idx); }
inline QAbstractItemDelegate *QCommonListViewBase::delegate(const QModelIndex &idx) const
- { return dd->delegateForIndex(idx); }
+ { return qq->itemDelegateForIndex(idx); }
inline bool QCommonListViewBase::isHidden(int row) const { return dd->isHidden(row); }
-inline int QCommonListViewBase::hiddenCount() const { return dd->hiddenRows.count(); }
+inline int QCommonListViewBase::hiddenCount() const { return dd->hiddenRows.size(); }
inline bool QCommonListViewBase::isRightToLeft() const { return qq->isRightToLeft(); }
diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp
index fefadef1a2..a91902813a 100644
--- a/src/widgets/itemviews/qlistwidget.cpp
+++ b/src/widgets/itemviews/qlistwidget.cpp
@@ -1,45 +1,8 @@
-/****************************************************************************
-**
-** 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 "qlistwidget.h"
-#include <qitemdelegate.h>
#include <private/qlistview_p.h>
#include <private/qwidgetitemdata_p.h>
#include <private/qlistwidget_p.h>
@@ -72,10 +35,10 @@ QListModel::~QListModel()
void QListModel::clear()
{
beginResetModel();
- for (int i = 0; i < items.count(); ++i) {
+ for (int i = 0; i < items.size(); ++i) {
if (items.at(i)) {
items.at(i)->d->theid = -1;
- items.at(i)->view = 0;
+ items.at(i)->view = nullptr;
delete items.at(i);
}
}
@@ -96,7 +59,7 @@ void QListModel::remove(QListWidgetItem *item)
Q_ASSERT(row != -1);
beginRemoveRows(QModelIndex(), row, row);
items.at(row)->d->theid = -1;
- items.at(row)->view = 0;
+ items.at(row)->view = nullptr;
items.removeAt(row);
endRemoveRows();
}
@@ -116,8 +79,8 @@ void QListModel::insert(int row, QListWidgetItem *item)
} else {
if (row < 0)
row = 0;
- else if (row > items.count())
- row = items.count();
+ else if (row > items.size())
+ row = items.size();
}
beginInsertRows(QModelIndex(), row, row);
items.insert(row, item);
@@ -127,7 +90,7 @@ void QListModel::insert(int row, QListWidgetItem *item)
void QListModel::insert(int row, const QStringList &labels)
{
- const int count = labels.count();
+ const int count = labels.size();
if (count <= 0)
return;
QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
@@ -140,8 +103,8 @@ void QListModel::insert(int row, const QStringList &labels)
} else {
if (row < 0)
row = 0;
- else if (row > items.count())
- row = items.count();
+ else if (row > items.size())
+ row = items.size();
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int i = 0; i < count; ++i) {
QListWidgetItem *item = new QListWidgetItem(labels.at(i));
@@ -155,12 +118,12 @@ void QListModel::insert(int row, const QStringList &labels)
QListWidgetItem *QListModel::take(int row)
{
- if (row < 0 || row >= items.count())
- return 0;
+ if (row < 0 || row >= items.size())
+ return nullptr;
beginRemoveRows(QModelIndex(), row, row);
items.at(row)->d->theid = -1;
- items.at(row)->view = 0;
+ items.at(row)->view = nullptr;
QListWidgetItem *item = items.takeAt(row);
endRemoveRows();
return item;
@@ -169,8 +132,8 @@ QListWidgetItem *QListModel::take(int row)
void QListModel::move(int srcRow, int dstRow)
{
if (srcRow == dstRow
- || srcRow < 0 || srcRow >= items.count()
- || dstRow < 0 || dstRow > items.count())
+ || srcRow < 0 || srcRow >= items.size()
+ || dstRow < 0 || dstRow > items.size())
return;
if (!beginMoveRows(QModelIndex(), srcRow, srcRow, QModelIndex(), dstRow))
@@ -183,7 +146,7 @@ void QListModel::move(int srcRow, int dstRow)
int QListModel::rowCount(const QModelIndex &parent) const
{
- return parent.isValid() ? 0 : items.count();
+ return parent.isValid() ? 0 : items.size();
}
QModelIndex QListModel::index(const QListWidgetItem *item_) const
@@ -194,7 +157,7 @@ QModelIndex QListModel::index(const QListWidgetItem *item_) const
return QModelIndex();
int row;
const int theid = item->d->theid;
- if (theid >= 0 && theid < items.count() && items.at(theid) == item) {
+ if (theid >= 0 && theid < items.size() && items.at(theid) == item) {
row = theid;
} else { // we need to search for the item
row = items.lastIndexOf(item); // lastIndexOf is an optimization in favor of indexOf
@@ -214,20 +177,19 @@ QModelIndex QListModel::index(int row, int column, const QModelIndex &parent) co
QVariant QListModel::data(const QModelIndex &index, int role) const
{
- if (!index.isValid() || index.row() >= items.count())
+ if (!index.isValid() || index.row() >= items.size())
return QVariant();
return items.at(index.row())->data(role);
}
bool QListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
- if (!index.isValid() || index.row() >= items.count())
+ if (!index.isValid() || index.row() >= items.size())
return false;
items.at(index.row())->setData(role, value);
return true;
}
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool QListModel::clearItemData(const QModelIndex &index)
{
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
@@ -238,18 +200,17 @@ bool QListModel::clearItemData(const QModelIndex &index)
if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); }))
return true; //it's already cleared
item->d->values.clear();
- emit dataChanged(index, index, QVector<int>{});
+ emit dataChanged(index, index, QList<int> {});
return true;
}
-#endif
QMap<int, QVariant> QListModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> roles;
- if (!index.isValid() || index.row() >= items.count())
+ if (!index.isValid() || index.row() >= items.size())
return roles;
QListWidgetItem *itm = items.at(index.row());
- for (int i = 0; i < itm->d->values.count(); ++i) {
+ for (int i = 0; i < itm->d->values.size(); ++i) {
roles.insert(itm->d->values.at(i).role,
itm->d->values.at(i).value);
}
@@ -263,7 +224,7 @@ bool QListModel::insertRows(int row, int count, const QModelIndex &parent)
beginInsertRows(QModelIndex(), row, row + count - 1);
QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
- QListWidgetItem *itm = 0;
+ QListWidgetItem *itm = nullptr;
for (int r = row; r < row + count; ++r) {
itm = new QListWidgetItem;
@@ -282,10 +243,10 @@ bool QListModel::removeRows(int row, int count, const QModelIndex &parent)
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
- QListWidgetItem *itm = 0;
+ QListWidgetItem *itm = nullptr;
for (int r = row; r < row + count; ++r) {
itm = items.takeAt(row);
- itm->view = 0;
+ itm->view = nullptr;
itm->d->theid = -1;
delete itm;
}
@@ -301,16 +262,23 @@ bool QListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
{
if (sourceRow < 0
|| sourceRow + count - 1 >= rowCount(sourceParent)
- || destinationChild <= 0
+ || destinationChild < 0
|| destinationChild > rowCount(destinationParent)
+ || sourceRow == destinationChild
|| sourceRow == destinationChild - 1
- || count <= 0) {
+ || count <= 0
+ || sourceParent.isValid()
+ || destinationParent.isValid()) {
return false;
}
if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
return false;
- destinationChild--;
- const int fromRow = destinationChild < sourceRow ? (sourceRow + count - 1) : sourceRow;
+
+ int fromRow = sourceRow;
+ if (destinationChild < sourceRow)
+ fromRow += count - 1;
+ else
+ destinationChild--;
while (count--)
items.move(fromRow, destinationChild);
endMoveRows();
@@ -319,7 +287,7 @@ bool QListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
Qt::ItemFlags QListModel::flags(const QModelIndex &index) const
{
- if (!index.isValid() || index.row() >= items.count() || index.model() != this)
+ if (!index.isValid() || index.row() >= items.size() || index.model() != this)
return Qt::ItemIsDropEnabled; // we allow drops outside the items
return items.at(index.row())->flags();
}
@@ -331,18 +299,18 @@ void QListModel::sort(int column, Qt::SortOrder order)
emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint);
- QVector < QPair<QListWidgetItem*,int> > sorting(items.count());
- for (int i = 0; i < items.count(); ++i) {
+ QList<QPair<QListWidgetItem *, int>> sorting(items.size());
+ for (int i = 0; i < items.size(); ++i) {
QListWidgetItem *item = items.at(i);
sorting[i].first = item;
sorting[i].second = i;
}
const auto compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
- std::sort(sorting.begin(), sorting.end(), compare);
+ std::stable_sort(sorting.begin(), sorting.end(), compare);
QModelIndexList fromIndexes;
QModelIndexList toIndexes;
- const int sortingCount = sorting.count();
+ const int sortingCount = sorting.size();
fromIndexes.reserve(sortingCount);
toIndexes.reserve(sortingCount);
for (int r = 0; r < sortingCount; ++r) {
@@ -359,72 +327,34 @@ void QListModel::sort(int column, Qt::SortOrder order)
/**
* This function assumes that all items in the model except the items that are between
* (inclusive) start and end are sorted.
- * With these assumptions, this function can ensure that the model is sorted in a
- * much more efficient way than doing a naive 'sort everything'.
- * (provided that the range is relatively small compared to the total number of items)
*/
void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int end)
{
if (column != 0)
return;
- int count = end - start + 1;
- QVector < QPair<QListWidgetItem*,int> > sorting(count);
- for (int i = 0; i < count; ++i) {
- sorting[i].first = items.at(start + i);
- sorting[i].second = start + i;
- }
+ const auto compareLt = [](const QListWidgetItem *left, const QListWidgetItem *right) -> bool {
+ return *left < *right;
+ };
- const auto compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
- std::sort(sorting.begin(), sorting.end(), compare);
-
- QModelIndexList oldPersistentIndexes = persistentIndexList();
- QModelIndexList newPersistentIndexes = oldPersistentIndexes;
- QList<QListWidgetItem*> tmp = items;
- QList<QListWidgetItem*>::iterator lit = tmp.begin();
- bool changed = false;
- for (int i = 0; i < count; ++i) {
- int oldRow = sorting.at(i).second;
- int tmpitepos = lit - tmp.begin();
- QListWidgetItem *item = tmp.takeAt(oldRow);
- if (tmpitepos > tmp.size())
- --tmpitepos;
- lit = tmp.begin() + tmpitepos;
- lit = sortedInsertionIterator(lit, tmp.end(), order, item);
- int newRow = qMax<qsizetype>(lit - tmp.begin(), 0);
- lit = tmp.insert(lit, item);
- if (newRow != oldRow) {
- changed = true;
- for (int j = i + 1; j < count; ++j) {
- int otherRow = sorting.at(j).second;
- if (oldRow < otherRow && newRow >= otherRow)
- --sorting[j].second;
- else if (oldRow > otherRow && newRow <= otherRow)
- ++sorting[j].second;
- }
- for (int k = 0; k < newPersistentIndexes.count(); ++k) {
- QModelIndex pi = newPersistentIndexes.at(k);
- int oldPersistentRow = pi.row();
- int newPersistentRow = oldPersistentRow;
- if (oldPersistentRow == oldRow)
- newPersistentRow = newRow;
- else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)
- newPersistentRow = oldPersistentRow - 1;
- else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)
- newPersistentRow = oldPersistentRow + 1;
- if (newPersistentRow != oldPersistentRow)
- newPersistentIndexes[k] = createIndex(newPersistentRow,
- pi.column(), pi.internalPointer());
- }
- }
- }
+ const auto compareGt = [](const QListWidgetItem *left, const QListWidgetItem *right) -> bool {
+ return *right < *left;
+ };
- if (changed) {
- emit layoutAboutToBeChanged();
- items = tmp;
- changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);
- emit layoutChanged();
- }
+ /** Check if range [start,end] is already in sorted position in list.
+ * Take for this the assumption, that outside [start,end] the list
+ * is already sorted. Therefore the sorted check has to be extended
+ * to the first element that is known to be sorted before the range
+ * [start, end], which is (start-1) and the first element after the
+ * range [start, end], which is (end+2) due to end being included.
+ */
+ const auto beginChangedIterator = items.constBegin() + qMax(start - 1, 0);
+ const auto endChangedIterator = items.constBegin() + qMin(end + 2, items.size());
+ const bool needsSorting = !std::is_sorted(beginChangedIterator, endChangedIterator,
+ order == Qt::AscendingOrder ? compareLt : compareGt);
+
+ if (needsSorting)
+ sort(column, order);
}
bool QListModel::itemLessThan(const QPair<QListWidgetItem*,int> &left,
@@ -449,7 +379,7 @@ QList<QListWidgetItem*>::iterator QListModel::sortedInsertionIterator(
return std::lower_bound(begin, end, item, QListModelGreaterThan());
}
-void QListModel::itemChanged(QListWidgetItem *item, const QVector<int> &roles)
+void QListModel::itemChanged(QListWidgetItem *item, const QList<int> &roles)
{
const QModelIndex idx = index(item);
emit dataChanged(idx, idx, roles);
@@ -458,7 +388,9 @@ void QListModel::itemChanged(QListWidgetItem *item, const QVector<int> &roles)
QStringList QListModel::mimeTypes() const
{
const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
- return view->mimeTypes();
+ if (view)
+ return view->mimeTypes();
+ return {};
}
QMimeData *QListModel::internalMimeData() const
@@ -469,7 +401,7 @@ QMimeData *QListModel::internalMimeData() const
QMimeData *QListModel::mimeData(const QModelIndexList &indexes) const
{
QList<QListWidgetItem*> itemlist;
- const int indexesCount = indexes.count();
+ const int indexesCount = indexes.size();
itemlist.reserve(indexesCount);
for (int i = 0; i < indexesCount; ++i)
itemlist << at(indexes.at(i).row());
@@ -490,7 +422,7 @@ bool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
if (index.isValid())
row = index.row();
else if (row == -1)
- row = items.count();
+ row = items.size();
return view->dropMimeData(row, data, action);
}
@@ -727,7 +659,7 @@ void QListWidgetItem::setData(int role, const QVariant &value)
{
bool found = false;
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
- for (int i = 0; i < d->values.count(); ++i) {
+ for (int i = 0; i < d->values.size(); ++i) {
if (d->values.at(i).role == role) {
if (d->values.at(i).value == value)
return;
@@ -739,9 +671,9 @@ void QListWidgetItem::setData(int role, const QVariant &value)
if (!found)
d->values.append(QWidgetItemData(role, value));
if (QListModel *model = listModel()) {
- const QVector<int> roles((role == Qt::DisplayRole) ?
- QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
- QVector<int>({role}));
+ const QList<int> roles((role == Qt::DisplayRole)
+ ? QList<int>({ Qt::DisplayRole, Qt::EditRole })
+ : QList<int>({ role }));
model->itemChanged(this, roles);
}
}
@@ -755,7 +687,7 @@ void QListWidgetItem::setData(int role, const QVariant &value)
QVariant QListWidgetItem::data(int role) const
{
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
- for (int i = 0; i < d->values.count(); ++i)
+ for (int i = 0; i < d->values.size(); ++i)
if (d->values.at(i).role == role)
return d->values.at(i).value;
return QVariant();
@@ -923,21 +855,25 @@ QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)
*/
/*!
+ \if defined(qt7)
+
+ \fn Qt::Alignment QListWidgetItem::textAlignment() const
+
+ Returns the text alignment for the list item.
+
+ \else
+
\fn int QListWidgetItem::textAlignment() const
Returns the text alignment for the list item.
- \sa Qt::AlignmentFlag
-*/
+ \note This function returns an int for historical reasons. It will
+ be corrected to return Qt::Alignment in Qt 7.
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn QColor QListWidgetItem::backgroundColor() const
- \obsolete
+ \sa Qt::Alignment
- This function is deprecated. Use background() instead.
+ \endif
*/
-#endif
/*!
\fn QBrush QListWidgetItem::background() const
@@ -948,17 +884,6 @@ QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)
\sa setBackground(), foreground()
*/
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn QColor QListWidgetItem::textColor() const
- \obsolete
-
- Returns the color used to display the list item's text.
-
- This function is deprecated. Use foreground() instead.
-*/
-#endif
-
/*!
\fn QBrush QListWidgetItem::foreground() const
\since 4.2
@@ -1101,18 +1026,26 @@ void QListWidgetItem::setFlags(Qt::ItemFlags aflags)
*/
/*!
+ \obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
+
\fn void QListWidgetItem::setTextAlignment(int alignment)
Sets the list item's text alignment to \a alignment.
- \sa Qt::AlignmentFlag
+ \sa Qt::Alignment
*/
/*!
- \fn void QListWidgetItem::setBackgroundColor(const QColor &color)
- \obsolete
+ \since 6.4
- This function is deprecated. Use setBackground() instead.
+ \fn void QListWidgetItem::setTextAlignment(Qt::Alignment alignment)
+
+ Sets the list item's text alignment to \a alignment.
+*/
+
+/*!
+ \fn void QListWidgetItem::setTextAlignment(Qt::AlignmentFlag alignment)
+ \internal
*/
/*!
@@ -1126,15 +1059,6 @@ void QListWidgetItem::setFlags(Qt::ItemFlags aflags)
\sa background(), setForeground()
*/
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn void QListWidgetItem::setTextColor(const QColor &color)
- \obsolete
-
- This function is deprecated. Use setForeground() instead.
-*/
-#endif
-
/*!
\fn void QListWidgetItem::setForeground(const QBrush &brush)
\since 4.2
@@ -1159,57 +1083,71 @@ void QListWidgetPrivate::setup()
Q_Q(QListWidget);
q->QListView::setModel(new QListModel(q));
// view signals
- QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex)));
- QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex)));
- QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
- q, SLOT(_q_emitItemDoubleClicked(QModelIndex)));
- QObject::connect(q, SIGNAL(activated(QModelIndex)),
- q, SLOT(_q_emitItemActivated(QModelIndex)));
- QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex)));
- QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- q, SLOT(_q_emitItemChanged(QModelIndex)));
- QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- q, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
- QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
-}
-
-void QListWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
+ connections = {
+ QObjectPrivate::connect(q, &QListWidget::pressed,
+ this, &QListWidgetPrivate::emitItemPressed),
+ QObjectPrivate::connect(q, &QListWidget::clicked,
+ this, &QListWidgetPrivate::emitItemClicked),
+ QObjectPrivate::connect(q, &QListWidget::doubleClicked,
+ this, &QListWidgetPrivate::emitItemDoubleClicked),
+ QObjectPrivate::connect(q, &QListWidget::activated,
+ this, &QListWidgetPrivate::emitItemActivated),
+ QObjectPrivate::connect(q, &QListWidget::entered,
+ this, &QListWidgetPrivate::emitItemEntered),
+ QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged,
+ this, &QListWidgetPrivate::emitItemChanged),
+ QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged,
+ this, &QListWidgetPrivate::dataChanged),
+ QObjectPrivate::connect(model, &QAbstractItemModel::columnsRemoved,
+ this, &QListWidgetPrivate::sort)
+ };
+}
+
+void QListWidgetPrivate::clearConnections()
+{
+ for (const QMetaObject::Connection &connection : connections)
+ QObject::disconnect(connection);
+ for (const QMetaObject::Connection &connection : selectionModelConnections)
+ QObject::disconnect(connection);
+}
+
+void QListWidgetPrivate::emitItemPressed(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemPressed(listModel()->at(index.row()));
}
-void QListWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
+void QListWidgetPrivate::emitItemClicked(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemClicked(listModel()->at(index.row()));
}
-void QListWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
+void QListWidgetPrivate::emitItemDoubleClicked(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemDoubleClicked(listModel()->at(index.row()));
}
-void QListWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
+void QListWidgetPrivate::emitItemActivated(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemActivated(listModel()->at(index.row()));
}
-void QListWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
+void QListWidgetPrivate::emitItemEntered(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemEntered(listModel()->at(index.row()));
}
-void QListWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
+void QListWidgetPrivate::emitItemChanged(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemChanged(listModel()->at(index.row()));
}
-void QListWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
+void QListWidgetPrivate::emitCurrentItemChanged(const QModelIndex &current,
const QModelIndex &previous)
{
Q_Q(QListWidget);
@@ -1220,21 +1158,21 @@ void QListWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
//persistentCurrent is invalid if something changed the model in response
//to the currentItemChanged signal emission and the item was removed
if (!persistentCurrent.isValid()) {
- currentItem = 0;
+ currentItem = nullptr;
}
emit q->currentTextChanged(currentItem ? currentItem->text() : QString());
emit q->currentRowChanged(persistentCurrent.row());
}
-void QListWidgetPrivate::_q_sort()
+void QListWidgetPrivate::sort()
{
if (sortingEnabled)
model->sort(0, sortOrder);
}
-void QListWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
- const QModelIndex &bottomRight)
+void QListWidgetPrivate::dataChanged(const QModelIndex &topLeft,
+ const QModelIndex &bottomRight)
{
if (sortingEnabled && topLeft.isValid() && bottomRight.isValid())
listModel()->ensureSorted(topLeft.column(), sortOrder,
@@ -1440,6 +1378,8 @@ QListWidget::QListWidget(QWidget *parent)
QListWidget::~QListWidget()
{
+ Q_D(QListWidget);
+ d->clearConnections();
}
/*!
@@ -1450,20 +1390,18 @@ void QListWidget::setSelectionModel(QItemSelectionModel *selectionModel)
{
Q_D(QListWidget);
- if (d->selectionModel) {
- QObject::disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
- QObject::disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
- this, SIGNAL(itemSelectionChanged()));
- }
+ for (const QMetaObject::Connection &connection : d->selectionModelConnections)
+ disconnect(connection);
QListView::setSelectionModel(selectionModel);
if (d->selectionModel) {
- QObject::connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
- QObject::connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
- this, SIGNAL(itemSelectionChanged()));
+ d->selectionModelConnections = {
+ QObjectPrivate::connect(d->selectionModel, &QItemSelectionModel::currentChanged,
+ d, &QListWidgetPrivate::emitCurrentItemChanged),
+ QObject::connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
+ this, &QListWidget::itemSelectionChanged)
+ };
}
}
@@ -1478,7 +1416,7 @@ QListWidgetItem *QListWidget::item(int row) const
{
Q_D(const QListWidget);
if (row < 0 || row >= d->model->rowCount())
- return 0;
+ return nullptr;
return d->listModel()->at(row);
}
@@ -1548,7 +1486,7 @@ QListWidgetItem *QListWidget::takeItem(int row)
{
Q_D(QListWidget);
if (row < 0 || row >= d->model->rowCount())
- return 0;
+ return nullptr;
return d->listModel()->take(row);
}
@@ -1784,34 +1722,6 @@ void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
QAbstractItemView::setIndexWidget(index, widget);
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- Returns \c true if \a item is selected; otherwise returns \c false.
-
- \obsolete
-
- This function is deprecated. Use QListWidgetItem::isSelected() instead.
-*/
-bool QListWidget::isItemSelected(const QListWidgetItem *item) const
-{
- return ((item && item->listWidget() == this) ? item->isSelected() : false);
-}
-
-/*!
- Selects or deselects the given \a item depending on whether \a select is
- true of false.
-
- \obsolete
-
- This function is deprecated. Use QListWidgetItem::setSelected() instead.
-*/
-void QListWidget::setItemSelected(const QListWidgetItem *item, bool select)
-{
- if (item && item->listWidget() == this)
- const_cast<QListWidgetItem*>(item)->setSelected(select);
-}
-#endif
-
/*!
Returns a list of all selected items in the list widget.
*/
@@ -1821,7 +1731,7 @@ QList<QListWidgetItem*> QListWidget::selectedItems() const
Q_D(const QListWidget);
QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QListWidgetItem*> items;
- const int numIndexes = indexes.count();
+ const int numIndexes = indexes.size();
items.reserve(numIndexes);
for (int i = 0; i < numIndexes; ++i)
items.append(d->listModel()->at(indexes.at(i).row()));
@@ -1846,32 +1756,6 @@ QList<QListWidgetItem*> QListWidget::findItems(const QString &text, Qt::MatchFla
return items;
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- Returns \c true if the \a item is explicitly hidden; otherwise returns \c false.
-
- \obsolete
-
- This function is deprecated. Use QListWidgetItem::isHidden() instead.
-*/
-bool QListWidget::isItemHidden(const QListWidgetItem *item) const
-{
- return isRowHidden(row(item));
-}
-
-/*!
- If \a hide is true, the \a item will be hidden; otherwise it will be shown.
-
- \obsolete
-
- This function is deprecated. Use QListWidgetItem::setHidden() instead.
-*/
-void QListWidget::setItemHidden(const QListWidgetItem *item, bool hide)
-{
- setRowHidden(row(item), hide);
-}
-#endif
-
/*!
Scrolls the view if necessary to ensure that the \a item is visible.
@@ -1916,11 +1800,7 @@ QStringList QListWidget::mimeTypes() const
If the list of items is empty, \nullptr is returned instead of a
serialized empty list.
*/
-#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QMimeData *QListWidget::mimeData(const QList<QListWidgetItem *> &items) const
-#else
-QMimeData *QListWidget::mimeData(const QList<QListWidgetItem*> items) const
-#endif
{
Q_D(const QListWidget);
@@ -1928,7 +1808,7 @@ QMimeData *QListWidget::mimeData(const QList<QListWidgetItem*> items) const
// if non empty, it's called from the model's own mimeData
if (cachedIndexes.isEmpty()) {
- cachedIndexes.reserve(items.count());
+ cachedIndexes.reserve(items.size());
for (QListWidgetItem *item : items)
cachedIndexes << indexFromItem(item);
@@ -1964,45 +1844,8 @@ bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction
}
/*! \reimp */
-void QListWidget::dropEvent(QDropEvent *event) {
- Q_D(QListWidget);
- if (event->source() == this && d->movement != Static) {
- QListView::dropEvent(event);
- return;
- }
-
- if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
- dragDropMode() == QAbstractItemView::InternalMove)) {
- QModelIndex topIndex;
- int col = -1;
- int row = -1;
- if (d->dropOn(event, &row, &col, &topIndex)) {
- QList<QModelIndex> selIndexes = selectedIndexes();
- QList<QPersistentModelIndex> persIndexes;
- const int selIndexesCount = selIndexes.count();
- persIndexes.reserve(selIndexesCount);
- for (int i = 0; i < selIndexesCount; i++)
- persIndexes.append(selIndexes.at(i));
-
- if (persIndexes.contains(topIndex))
- return;
- std::sort(persIndexes.begin(), persIndexes.end()); // The dropped items will remain in the same visual order.
-
- QPersistentModelIndex dropRow = model()->index(row, col, topIndex);
-
- int r = row == -1 ? count() : (dropRow.row() >= 0 ? dropRow.row() : row);
- for (int i = 0; i < persIndexes.count(); ++i) {
- const QPersistentModelIndex &pIndex = persIndexes.at(i);
- d->listModel()->move(pIndex.row(), r);
- r = pIndex.row() + 1; // Dropped items are inserted contiguously and in the right order.
- }
-
- event->accept();
- // Don't want QAbstractItemView to delete it because it was "moved" we already did it
- event->setDropAction(Qt::CopyAction);
- }
- }
-
+void QListWidget::dropEvent(QDropEvent *event)
+{
QListView::dropEvent(event);
}
@@ -2043,18 +1886,6 @@ QModelIndex QListWidget::indexFromItem(const QListWidgetItem *item) const
return d->listModel()->index(item);
}
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
-/*!
- \internal
- \obsolete
- \overload
-*/
-QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const
-{
- return indexFromItem(const_cast<const QListWidgetItem *>(item));
-}
-#endif
-
/*!
Returns a pointer to the QListWidgetItem associated with the given \a index.
*/
@@ -2064,7 +1895,7 @@ QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const
Q_D(const QListWidget);
if (d->isIndexValid(index))
return d->listModel()->at(index.row());
- return 0;
+ return nullptr;
}
/*!
diff --git a/src/widgets/itemviews/qlistwidget.h b/src/widgets/itemviews/qlistwidget.h
index 1319d658ab..c6ba714c43 100644
--- a/src/widgets/itemviews/qlistwidget.h
+++ b/src/widgets/itemviews/qlistwidget.h
@@ -1,50 +1,14 @@
-/****************************************************************************
-**
-** 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
#ifndef QLISTWIDGET_H
#define QLISTWIDGET_H
-#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qlistview.h>
-#include <QtCore/qvariant.h>
-#include <QtCore/qvector.h>
+#include <QtWidgets/qtwidgetsglobal.h>
+#include <QtCore/qlist.h>
#include <QtCore/qitemselectionmodel.h>
+#include <QtCore/qvariant.h>
QT_REQUIRE_CONFIG(listwidget);
@@ -93,7 +57,7 @@ public:
{ return data(Qt::StatusTipRole).toString(); }
inline void setStatusTip(const QString &statusTip);
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
inline QString toolTip() const
{ return data(Qt::ToolTipRole).toString(); }
inline void setToolTip(const QString &toolTip);
@@ -109,42 +73,35 @@ public:
{ return qvariant_cast<QFont>(data(Qt::FontRole)); }
inline void setFont(const QFont &font);
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
inline int textAlignment() const
{ return data(Qt::TextAlignmentRole).toInt(); }
+#else
+ inline Qt::Alignment textAlignment() const
+ { return qvariant_cast<Qt::Alignment>(data(Qt::TextAlignmentRole)); }
+#endif
+#if QT_DEPRECATED_SINCE(6, 4)
+ QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
inline void setTextAlignment(int alignment)
{ setData(Qt::TextAlignmentRole, alignment); }
-
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QListWidgetItem::background() instead")
- inline QColor backgroundColor() const
- { return qvariant_cast<QColor>(data(Qt::BackgroundRole)); }
+ inline void setTextAlignment(Qt::AlignmentFlag alignment)
+ { setData(Qt::TextAlignmentRole, QVariant::fromValue(Qt::Alignment(alignment))); }
#endif
- // no QT_DEPRECATED_SINCE because it is a virtual function
- QT_DEPRECATED_X ("Use QListWidgetItem::setBackground() instead")
- virtual void setBackgroundColor(const QColor &color)
- { setData(Qt::BackgroundRole, color); }
+ inline void setTextAlignment(Qt::Alignment alignment)
+ { setData(Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
inline QBrush background() const
{ return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }
inline void setBackground(const QBrush &brush)
{ setData(Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QListWidgetItem::foreground() instead")
- inline QColor textColor() const
- { return qvariant_cast<QColor>(data(Qt::ForegroundRole)); }
- QT_DEPRECATED_X ("Use QListWidgetItem::setForeground() instead")
- inline void setTextColor(const QColor &color)
- { setData(Qt::ForegroundRole, color); }
-#endif
-
inline QBrush foreground() const
{ return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); }
inline void setForeground(const QBrush &brush)
{ setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
inline Qt::CheckState checkState() const
- { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }
+ { return qvariant_cast<Qt::CheckState>(data(Qt::CheckStateRole)); }
inline void setCheckState(Qt::CheckState state)
{ setData(Qt::CheckStateRole, static_cast<int>(state)); }
@@ -169,7 +126,6 @@ public:
private:
QListModel *listModel() const;
int rtti;
- QVector<void *> dummy;
QListWidget *view;
QListWidgetItemPrivate *d;
Qt::ItemFlags itemFlags;
@@ -184,7 +140,7 @@ inline void QListWidgetItem::setIcon(const QIcon &aicon)
inline void QListWidgetItem::setStatusTip(const QString &astatusTip)
{ setData(Qt::StatusTipRole, astatusTip); }
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
inline void QListWidgetItem::setToolTip(const QString &atoolTip)
{ setData(Qt::ToolTipRole, atoolTip); }
#endif
@@ -208,7 +164,8 @@ class Q_WIDGETS_EXPORT QListWidget : public QListView
{
Q_OBJECT
Q_PROPERTY(int count READ count)
- Q_PROPERTY(int currentRow READ currentRow WRITE setCurrentRow NOTIFY currentRowChanged USER true)
+ Q_PROPERTY(int currentRow READ currentRow WRITE setCurrentRow NOTIFY currentRowChanged
+ USER true)
Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
friend class QListWidgetItem;
@@ -256,24 +213,15 @@ public:
void setItemWidget(QListWidgetItem *item, QWidget *widget);
inline void removeItemWidget(QListWidgetItem *item);
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QListWidgetItem::isSelected() instead")
- bool isItemSelected(const QListWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QListWidgetItem::setSelected() instead")
- void setItemSelected(const QListWidgetItem *item, bool select);
-#endif
QList<QListWidgetItem*> selectedItems() const;
QList<QListWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags) const;
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QListWidgetItem::isHidden() instead")
- bool isItemHidden(const QListWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QListWidgetItem::setHidden() instead")
- void setItemHidden(const QListWidgetItem *item, bool hide);
-#endif
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ QList<QListWidgetItem*> items(const QMimeData *data) const;
+
+ QModelIndex indexFromItem(const QListWidgetItem *item) const;
+ QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
+
protected:
-#endif
#if QT_CONFIG(draganddrop)
void dropEvent(QDropEvent *event) override;
#endif
@@ -287,7 +235,6 @@ Q_SIGNALS:
void itemDoubleClicked(QListWidgetItem *item);
void itemActivated(QListWidgetItem *item);
void itemEntered(QListWidgetItem *item);
- // ### Qt 6: add changed roles
void itemChanged(QListWidgetItem *item);
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
@@ -299,45 +246,18 @@ Q_SIGNALS:
protected:
bool event(QEvent *e) override;
virtual QStringList mimeTypes() const;
-#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
virtual QMimeData *mimeData(const QList<QListWidgetItem *> &items) const;
-#else
- virtual QMimeData *mimeData(const QList<QListWidgetItem*> items) const;
-#endif
#if QT_CONFIG(draganddrop)
virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action);
virtual Qt::DropActions supportedDropActions() const;
#endif
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
-public:
-#else
-protected:
-#endif
- QList<QListWidgetItem*> items(const QMimeData *data) const;
-
- QModelIndex indexFromItem(const QListWidgetItem *item) const;
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
- QModelIndex indexFromItem(QListWidgetItem *item) const; // ### Qt 6: remove
-#endif
- QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
-
private:
void setModel(QAbstractItemModel *model) override;
Qt::SortOrder sortOrder() const;
Q_DECLARE_PRIVATE(QListWidget)
Q_DISABLE_COPY(QListWidget)
-
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
- Q_PRIVATE_SLOT(d_func(), void _q_sort())
- Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
};
inline void QListWidget::removeItemWidget(QListWidgetItem *aItem)
diff --git a/src/widgets/itemviews/qlistwidget_p.h b/src/widgets/itemviews/qlistwidget_p.h
index 65a7124322..1007542ddc 100644
--- a/src/widgets/itemviews/qlistwidget_p.h
+++ b/src/widgets/itemviews/qlistwidget_p.h
@@ -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
#ifndef QLISTWIDGET_P_H
#define QLISTWIDGET_P_H
@@ -54,10 +18,11 @@
#include <QtCore/qabstractitemmodel.h>
#include <QtWidgets/qabstractitemview.h>
#include <QtWidgets/qlistwidget.h>
-#include <qitemdelegate.h>
#include <private/qlistview_p.h>
#include <private/qwidgetitemdata_p.h>
+#include <array>
+
QT_REQUIRE_CONFIG(listwidget);
QT_BEGIN_NAMESPACE
@@ -100,9 +65,7 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool clearItemData(const QModelIndex &index) override;
-#endif
QMap<int, QVariant> itemData(const QModelIndex &index) const override;
@@ -123,7 +86,7 @@ public:
const QList<QListWidgetItem*>::iterator &end,
Qt::SortOrder order, QListWidgetItem *item);
- void itemChanged(QListWidgetItem *item, const QVector<int> &roles = QVector<int>());
+ void itemChanged(QListWidgetItem *item, const QList<int> &roles = QList<int>());
// dnd
QStringList mimeTypes() const override;
@@ -151,17 +114,21 @@ public:
QListWidgetPrivate() : QListViewPrivate(), sortOrder(Qt::AscendingOrder), sortingEnabled(false) {}
inline QListModel *listModel() const { return qobject_cast<QListModel*>(model); }
void setup();
- void _q_emitItemPressed(const QModelIndex &index);
- void _q_emitItemClicked(const QModelIndex &index);
- void _q_emitItemDoubleClicked(const QModelIndex &index);
- void _q_emitItemActivated(const QModelIndex &index);
- void _q_emitItemEntered(const QModelIndex &index);
- void _q_emitItemChanged(const QModelIndex &index);
- void _q_emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
- void _q_sort();
- void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+ void clearConnections();
+ void emitItemPressed(const QModelIndex &index);
+ void emitItemClicked(const QModelIndex &index);
+ void emitItemDoubleClicked(const QModelIndex &index);
+ void emitItemActivated(const QModelIndex &index);
+ void emitItemEntered(const QModelIndex &index);
+ void emitItemChanged(const QModelIndex &index);
+ void emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
+ void sort();
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
Qt::SortOrder sortOrder;
bool sortingEnabled;
+
+ std::array<QMetaObject::Connection, 8> connections;
+ std::array<QMetaObject::Connection, 2> selectionModelConnections;
};
class QListWidgetItemPrivate
@@ -169,7 +136,7 @@ class QListWidgetItemPrivate
public:
QListWidgetItemPrivate(QListWidgetItem *item) : q(item), theid(-1) {}
QListWidgetItem *q;
- QVector<QWidgetItemData> values;
+ QList<QWidgetItemData> values;
int theid;
};
diff --git a/src/widgets/itemviews/qstyleditemdelegate.cpp b/src/widgets/itemviews/qstyleditemdelegate.cpp
index 702e290da3..54c1fb4f52 100644
--- a/src/widgets/itemviews/qstyleditemdelegate.cpp
+++ b/src/widgets/itemviews/qstyleditemdelegate.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 "qstyleditemdelegate.h"
@@ -66,6 +30,7 @@
#include <qmetaobject.h>
#include <qtextlayout.h>
#include <private/qabstractitemdelegate_p.h>
+#include <private/qabstractitemmodel_p.h>
#include <private/qtextengine_p.h>
#include <private/qlayoutengine_p.h>
#include <qdebug.h>
@@ -74,6 +39,7 @@
#include <qtableview.h>
#endif
+#include <array>
#include <limits.h>
QT_BEGIN_NAMESPACE
@@ -83,7 +49,7 @@ class QStyledItemDelegatePrivate : public QAbstractItemDelegatePrivate
Q_DECLARE_PUBLIC(QStyledItemDelegate)
public:
- QStyledItemDelegatePrivate() : factory(0) { }
+ QStyledItemDelegatePrivate() : factory(nullptr) { }
static const QWidget *widget(const QStyleOptionViewItem &option)
{
@@ -96,6 +62,16 @@ public:
}
QItemEditorFactory *factory;
+
+ mutable std::array<QModelRoleData, 7> modelRoleData = {
+ QModelRoleData(Qt::FontRole),
+ QModelRoleData(Qt::TextAlignmentRole),
+ QModelRoleData(Qt::ForegroundRole),
+ QModelRoleData(Qt::CheckStateRole),
+ QModelRoleData(Qt::DecorationRole),
+ QModelRoleData(Qt::DisplayRole),
+ QModelRoleData(Qt::BackgroundRole)
+ };
};
/*!
@@ -139,8 +115,7 @@ public:
\row \li \l Qt::AccessibleDescriptionRole \li QString
\row \li \l Qt::AccessibleTextRole \li QString
\endomit
- \row \li \l Qt::BackgroundRole \li QBrush (\since 4.2)
- \row \li \l Qt::BackgroundColorRole \li QColor (obsolete; use Qt::BackgroundRole instead)
+ \row \li \l Qt::BackgroundRole \li QBrush \since 4.2
\row \li \l Qt::CheckStateRole \li Qt::CheckState
\row \li \l Qt::DecorationRole \li QIcon, QPixmap, QImage and QColor
\row \li \l Qt::DisplayRole \li QString and types with a string representation
@@ -151,8 +126,7 @@ public:
\row \li \l Qt::StatusTipRole \li
\endomit
\row \li \l Qt::TextAlignmentRole \li Qt::Alignment
- \row \li \l Qt::ForegroundRole \li QBrush (\since 4.2)
- \row \li \l Qt::TextColorRole \li QColor (obsolete; use Qt::ForegroundRole instead)
+ \row \li \l Qt::ForegroundRole \li QBrush \since 4.2
\omit
\row \li \l Qt::ToolTipRole
\row \li \l Qt::WhatsThisRole
@@ -163,12 +137,17 @@ public:
instance provided by QItemEditorFactory is installed on all item
delegates. You can set a custom factory using
setItemEditorFactory() or set a new default factory with
- QItemEditorFactory::setDefaultFactory(). It is the data stored in
- the item model with the \l{Qt::}{EditRole} that is edited. See the
- QItemEditorFactory class for a more high-level introduction to
- item editor factories. The \l{Color Editor Factory Example}{Color
- Editor Factory} example shows how to create custom editors with a
- factory.
+ QItemEditorFactory::setDefaultFactory().
+
+ \snippet code/src_gui_itemviews_qitemeditorfactory.cpp setDefaultFactory
+
+ After the new factory has been set, all standard item delegates
+ will use it (i.e, also delegates that were created before the new
+ default factory was set).
+
+ It is the data stored in the item model with the \l{Qt::}{EditRole}
+ that is edited. See the QItemEditorFactory class for a more
+ high-level introduction to item editor factories.
\section1 Subclassing QStyledItemDelegate
@@ -230,8 +209,7 @@ public:
documentation for details.
\sa {Delegate Classes}, QItemDelegate, QAbstractItemDelegate, QStyle,
- {Spin Box Delegate Example}, {Star Delegate Example}, {Color
- Editor Factory Example}
+ {Star Delegate Example}
*/
@@ -278,33 +256,43 @@ QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale& l
void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option,
const QModelIndex &index) const
{
- QVariant value = index.data(Qt::FontRole);
- if (value.isValid() && !value.isNull()) {
- option->font = qvariant_cast<QFont>(value).resolve(option->font);
+ option->index = index;
+
+ Q_D(const QStyledItemDelegate);
+ QModelRoleDataSpan modelRoleDataSpan = d->modelRoleData;
+ index.multiData(modelRoleDataSpan);
+
+ const QVariant *value;
+ value = modelRoleDataSpan.dataForRole(Qt::FontRole);
+ if (value->isValid() && !value->isNull()) {
+ option->font = qvariant_cast<QFont>(*value).resolve(option->font);
option->fontMetrics = QFontMetrics(option->font);
}
- value = index.data(Qt::TextAlignmentRole);
- if (value.isValid() && !value.isNull())
- option->displayAlignment = Qt::Alignment(value.toInt());
+ value = modelRoleDataSpan.dataForRole(Qt::TextAlignmentRole);
+ if (value->isValid() && !value->isNull())
+ option->displayAlignment = QtPrivate::legacyFlagValueFromModelData<Qt::Alignment>(*value);
- value = index.data(Qt::ForegroundRole);
- if (value.canConvert<QBrush>())
- option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value));
+ value = modelRoleDataSpan.dataForRole(Qt::ForegroundRole);
+ if (value->canConvert<QBrush>())
+ option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(*value));
- option->index = index;
- value = index.data(Qt::CheckStateRole);
- if (value.isValid() && !value.isNull()) {
+ value = modelRoleDataSpan.dataForRole(Qt::CheckStateRole);
+ if (value->isValid() && !value->isNull()) {
option->features |= QStyleOptionViewItem::HasCheckIndicator;
- option->checkState = static_cast<Qt::CheckState>(value.toInt());
+ option->checkState = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(*value);
}
- value = index.data(Qt::DecorationRole);
- if (value.isValid() && !value.isNull()) {
+ value = modelRoleDataSpan.dataForRole(Qt::DecorationRole);
+ if (value->isValid() && !value->isNull()) {
option->features |= QStyleOptionViewItem::HasDecoration;
- switch (value.type()) {
- case QVariant::Icon: {
- option->icon = qvariant_cast<QIcon>(value);
+ switch (value->userType()) {
+ case QMetaType::QIcon: {
+ option->icon = qvariant_cast<QIcon>(*value);
+ if (option->icon.isNull()) {
+ option->features &= ~QStyleOptionViewItem::HasDecoration;
+ break;
+ }
QIcon::Mode mode;
if (!(option->state & QStyle::State_Enabled))
mode = QIcon::Disabled;
@@ -319,22 +307,22 @@ void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option,
qMin(option->decorationSize.height(), actualSize.height()));
break;
}
- case QVariant::Color: {
+ case QMetaType::QColor: {
QPixmap pixmap(option->decorationSize);
- pixmap.fill(qvariant_cast<QColor>(value));
+ pixmap.fill(qvariant_cast<QColor>(*value));
option->icon = QIcon(pixmap);
break;
}
- case QVariant::Image: {
- QImage image = qvariant_cast<QImage>(value);
+ case QMetaType::QImage: {
+ QImage image = qvariant_cast<QImage>(*value);
option->icon = QIcon(QPixmap::fromImage(image));
- option->decorationSize = image.size() / image.devicePixelRatio();
+ option->decorationSize = image.deviceIndependentSize().toSize();
break;
}
- case QVariant::Pixmap: {
- QPixmap pixmap = qvariant_cast<QPixmap>(value);
+ case QMetaType::QPixmap: {
+ QPixmap pixmap = qvariant_cast<QPixmap>(*value);
option->icon = QIcon(pixmap);
- option->decorationSize = pixmap.size() / pixmap.devicePixelRatio();
+ option->decorationSize = pixmap.deviceIndependentSize().toSize();
break;
}
default:
@@ -342,16 +330,17 @@ void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option,
}
}
- value = index.data(Qt::DisplayRole);
- if (value.isValid() && !value.isNull()) {
+ value = modelRoleDataSpan.dataForRole(Qt::DisplayRole);
+ if (value->isValid() && !value->isNull()) {
option->features |= QStyleOptionViewItem::HasDisplay;
- option->text = displayText(value, option->locale);
+ option->text = displayText(*value, option->locale);
}
- option->backgroundBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
+ value = modelRoleDataSpan.dataForRole(Qt::BackgroundRole);
+ option->backgroundBrush = qvariant_cast<QBrush>(*value);
// disable style animations for checkboxes etc. within itemviews (QTBUG-30146)
- option->styleObject = 0;
+ option->styleObject = nullptr;
}
/*!
@@ -421,12 +410,13 @@ QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option,
\sa QAbstractItemDelegate::createEditor()
*/
QWidget *QStyledItemDelegate::createEditor(QWidget *parent,
- const QStyleOptionViewItem &,
+ const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
+ Q_UNUSED(option);
Q_D(const QStyledItemDelegate);
if (!index.isValid())
- return 0;
+ return nullptr;
return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent);
}
@@ -441,19 +431,14 @@ QWidget *QStyledItemDelegate::createEditor(QWidget *parent,
*/
void QStyledItemDelegate::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();
if (!n.isEmpty()) {
if (!v.isValid())
- v = QVariant(editor->property(n).userType(), (const void *)0);
+ v = QVariant(editor->property(n).metaType());
editor->setProperty(n, v);
}
-#endif
}
/*!
@@ -470,11 +455,6 @@ void QStyledItemDelegate::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 QStyledItemDelegate);
Q_ASSERT(model);
Q_ASSERT(editor);
@@ -484,7 +464,6 @@ void QStyledItemDelegate::setModelData(QWidget *editor,
model->data(index, Qt::EditRole).userType());
if (!n.isEmpty())
model->setData(index, editor->property(n), Qt::EditRole);
-#endif
}
/*!
@@ -502,15 +481,7 @@ void QStyledItemDelegate::updateEditorGeometry(QWidget *editor,
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
- // let the editor take up all available space
- //if the editor is not a QLineEdit
- //or it is in a QTableView
-#if QT_CONFIG(tableview) && QT_CONFIG(lineedit)
- if (qobject_cast<QExpandingLineEdit*>(editor) && !qobject_cast<const QTableView*>(widget))
- opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, editor);
- else
-#endif
- opt.showDecorationSelected = true;
+ opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, nullptr, editor);
QStyle *style = widget ? widget->style() : QApplication::style();
QRect geom = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, widget);
@@ -558,8 +529,9 @@ void QStyledItemDelegate::setItemEditorFactory(QItemEditorFactory *factory)
\li \uicontrol Esc
\endlist
- If the \a editor's type is QTextEdit or QPlainTextEdit then \uicontrol Enter and
- \uicontrol Return keys are \e not handled.
+ If the \a editor's type is QTextEdit or QPlainTextEdit then \uicontrol Tab,
+ \uicontrol Backtab, \uicontrol Enter and \uicontrol Return keys are \e not
+ handled.
In the case of \uicontrol Tab, \uicontrol Backtab, \uicontrol Enter and \uicontrol Return
key press events, the \a editor's data is committed to the model
@@ -612,7 +584,7 @@ bool QStyledItemDelegate::editorEvent(QEvent *event,
initStyleOption(&viewOpt, index);
QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, widget);
QMouseEvent *me = static_cast<QMouseEvent*>(event);
- if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
+ if (me->button() != Qt::LeftButton || !checkRect.contains(me->position().toPoint()))
return false;
if ((event->type() == QEvent::MouseButtonPress)
@@ -627,7 +599,7 @@ bool QStyledItemDelegate::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
diff --git a/src/widgets/itemviews/qstyleditemdelegate.h b/src/widgets/itemviews/qstyleditemdelegate.h
index 2df2450f07..23f059355d 100644
--- a/src/widgets/itemviews/qstyleditemdelegate.h
+++ b/src/widgets/itemviews/qstyleditemdelegate.h
@@ -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
#ifndef QSTYLEDITEMDELEGATE_H
#define QSTYLEDITEMDELEGATE_H
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp
index 81fceba8dc..5726348bc5 100644
--- a/src/widgets/itemviews/qtableview.cpp
+++ b/src/widgets/itemviews/qtableview.cpp
@@ -1,46 +1,10 @@
-/****************************************************************************
-**
-** 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 "qtableview.h"
#include <qheaderview.h>
-#include <qitemdelegate.h>
+#include <qabstractitemdelegate.h>
#include <qapplication.h>
#include <qpainter.h>
#include <qstyle.h>
@@ -55,7 +19,7 @@
#include <private/qtableview_p.h>
#include <private/qheaderview_p.h>
#include <private/qscrollbar_p.h>
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
#include <qaccessible.h>
#endif
@@ -79,7 +43,7 @@ void QSpanCollection::addSpan(QSpanCollection::Span *span)
const SubIndex previousList = it_y.value();
for (Span *s : previousList) {
//If a subspans intersect the row, we need to split it into subspans
- if(s->bottom() >= span->top())
+ if (s->bottom() >= span->top())
sub_index.insert(-s->left(), s);
}
}
@@ -90,7 +54,7 @@ void QSpanCollection::addSpan(QSpanCollection::Span *span)
//insert the span as supspan in all the lists that intesects the span
while(-it_y.key() <= span->bottom()) {
(*it_y).insert(-span->left(), span);
- if(it_y == index.begin())
+ if (it_y == index.begin())
break;
--it_y;
}
@@ -112,7 +76,7 @@ void QSpanCollection::updateSpan(QSpanCollection::Span *span, int old_height)
Q_ASSERT(it_y != index.end()); //it_y must exist since the span is in the list
while (-it_y.key() <= span->bottom()) {
(*it_y).insert(-span->left(), span);
- if(it_y == index.begin())
+ if (it_y == index.begin())
break;
--it_y;
}
@@ -123,12 +87,13 @@ void QSpanCollection::updateSpan(QSpanCollection::Span *span, int old_height)
while (-it_y.key() <= span->top() + old_height -1) {
if (-it_y.key() > span->bottom()) {
int removed = (*it_y).remove(-span->left());
- Q_ASSERT(removed == 1); Q_UNUSED(removed);
+ Q_ASSERT(removed == 1);
+ Q_UNUSED(removed);
if (it_y->isEmpty()) {
it_y = index.erase(it_y);
}
}
- if(it_y == index.begin())
+ if (it_y == index.begin())
break;
--it_y;
}
@@ -176,7 +141,7 @@ QSet<QSpanCollection::Span *> QSpanCollection::spansInRect(int x, int y, int w,
{
QSet<Span *> list;
Index::const_iterator it_y = index.lowerBound(-y);
- if(it_y == index.end())
+ if (it_y == index.end())
--it_y;
while(-it_y.key() <= y + h) {
SubIndex::const_iterator it_x = (*it_y).lowerBound(-x);
@@ -190,7 +155,7 @@ QSet<QSpanCollection::Span *> QSpanCollection::spansInRect(int x, int y, int w,
break;
--it_x;
}
- if(it_y == index.begin())
+ if (it_y == index.begin())
break;
--it_y;
}
@@ -205,6 +170,13 @@ QDebug operator<<(QDebug str, const QSpanCollection::Span &span)
str << '(' << span.top() << ',' << span.left() << ',' << span.bottom() << ',' << span.right() << ')';
return str;
}
+
+QDebug operator<<(QDebug debug, const QSpanCollection::SpanList &spans)
+{
+ for (const auto *span : spans)
+ debug << span << *span;
+ return debug;
+}
#endif
/** \internal
@@ -235,8 +207,7 @@ void QSpanCollection::updateInsertedRows(int start, int end)
#ifdef DEBUG_SPAN_UPDATE
qDebug("After");
- foreach (QSpanCollection::Span *span, spans)
- qDebug() << span << *span;
+ qDebug() << spans;
#endif
for (Index::iterator it_y = index.begin(); it_y != index.end(); ) {
@@ -282,8 +253,7 @@ void QSpanCollection::updateInsertedColumns(int start, int end)
#ifdef DEBUG_SPAN_UPDATE
qDebug("After");
- foreach (QSpanCollection::Span *span, spans)
- qDebug() << span << *span;
+ qDebug() << spans;
#endif
for (Index::iterator it_y = index.begin(); it_y != index.end(); ++it_y) {
@@ -387,8 +357,7 @@ void QSpanCollection::updateRemovedRows(int start, int end)
#ifdef DEBUG_SPAN_UPDATE
qDebug("After");
- foreach (QSpanCollection::Span *span, spans)
- qDebug() << span << *span;
+ qDebug() << spans;
#endif
if (spans.empty()) {
qDeleteAll(spansToBeDeleted);
@@ -455,8 +424,7 @@ void QSpanCollection::updateRemovedRows(int start, int end)
#ifdef DEBUG_SPAN_UPDATE
qDebug() << index;
qDebug("Deleted");
- foreach (QSpanCollection::Span *span, spansToBeDeleted)
- qDebug() << span << *span;
+ qDebug() << spansToBeDeleted;
#endif
qDeleteAll(spansToBeDeleted);
}
@@ -514,8 +482,7 @@ void QSpanCollection::updateRemovedColumns(int start, int end)
#ifdef DEBUG_SPAN_UPDATE
qDebug("After");
- foreach (QSpanCollection::Span *span, spans)
- qDebug() << span << *span;
+ qDebug() << spans;
#endif
if (spans.empty()) {
qDeleteAll(toBeDeleted);
@@ -534,8 +501,7 @@ void QSpanCollection::updateRemovedColumns(int start, int end)
#ifdef DEBUG_SPAN_UPDATE
qDebug() << index;
qDebug("Deleted");
- foreach (QSpanCollection::Span *span, toBeDeleted)
- qDebug() << span << *span;
+ qDebug() << toBeDeleted;
#endif
qDeleteAll(toBeDeleted);
}
@@ -589,7 +555,7 @@ public:
QTableCornerButton(QWidget *parent) : QAbstractButton(parent) {}
void paintEvent(QPaintEvent*) override {
QStyleOptionHeader opt;
- opt.init(this);
+ opt.initFrom(this);
QStyle::State state = QStyle::State_None;
if (isEnabled())
state |= QStyle::State_Enabled;
@@ -627,7 +593,25 @@ void QTableViewPrivate::init()
#if QT_CONFIG(abstractbutton)
cornerWidget = new QTableCornerButton(q);
cornerWidget->setFocusPolicy(Qt::NoFocus);
- QObject::connect(cornerWidget, SIGNAL(clicked()), q, SLOT(selectAll()));
+ cornerWidgetConnection = QObject::connect(
+ cornerWidget, &QTableCornerButton::clicked,
+ q, &QTableView::selectAll);
+#endif
+}
+
+void QTableViewPrivate::clearConnections()
+{
+ for (const QMetaObject::Connection &connection : modelConnections)
+ QObject::disconnect(connection);
+ for (const QMetaObject::Connection &connection : verHeaderConnections)
+ QObject::disconnect(connection);
+ for (const QMetaObject::Connection &connection : horHeaderConnections)
+ QObject::disconnect(connection);
+ for (const QMetaObject::Connection &connection : dynHorHeaderConnections)
+ QObject::disconnect(connection);
+ QObject::disconnect(selectionmodelConnection);
+#if QT_CONFIG(abstractbutton)
+ QObject::disconnect(cornerWidgetConnection);
#endif
}
@@ -669,6 +653,52 @@ void QTableViewPrivate::trimHiddenSelections(QItemSelectionRange *range) const
*range = QItemSelectionRange(topLeft, bottomRight);
}
+QRect QTableViewPrivate::intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const
+{
+ Q_Q(const QTableView);
+
+ using minMaxPair = std::pair<int, int>;
+ const auto calcMinMax = [q](QHeaderView *hdr, int startIdx, int endIdx, minMaxPair bounds) -> minMaxPair
+ {
+ minMaxPair ret(std::numeric_limits<int>::max(), std::numeric_limits<int>::min());
+ if (hdr->sectionsMoved()) {
+ for (int i = startIdx; i <= endIdx; ++i) {
+ const int start = hdr->sectionViewportPosition(i);
+ const int end = start + hdr->sectionSize(i);
+ ret.first = std::min(start, ret.first);
+ ret.second = std::max(end, ret.second);
+ if (ret.first <= bounds.first && ret.second >= bounds.second)
+ break;
+ }
+ } else {
+ if (q->isRightToLeft() && q->horizontalHeader() == hdr)
+ std::swap(startIdx, endIdx);
+ ret.first = hdr->sectionViewportPosition(startIdx);
+ ret.second = hdr->sectionViewportPosition(endIdx) +
+ hdr->sectionSize(endIdx);
+ }
+ return ret;
+ };
+
+ const auto yVals = calcMinMax(verticalHeader, topLeft.row(), bottomRight.row(),
+ minMaxPair(rect.top(), rect.bottom()));
+ if (yVals.first == yVals.second) // all affected rows are hidden
+ return QRect();
+
+ // short circuit: check if no row is inside rect
+ const QRect colRect(QPoint(rect.left(), yVals.first),
+ QPoint(rect.right(), yVals.second));
+ const QRect intersected = rect.intersected(colRect);
+ if (intersected.isNull())
+ return QRect();
+
+ const auto xVals = calcMinMax(horizontalHeader, topLeft.column(), bottomRight.column(),
+ minMaxPair(rect.left(), rect.right()));
+ const QRect updateRect(QPoint(xVals.first, yVals.first),
+ QPoint(xVals.second, yVals.second));
+ return rect.intersected(updateRect);
+}
+
/*!
\internal
Sets the span for the cell at (\a row, \a column).
@@ -871,13 +901,32 @@ void QTableViewPrivate::drawAndClipSpans(const QRegion &area, QPainter *painter,
visibleSpans = spans.spansInRect(logicalColumn(firstVisualColumn), logicalRow(firstVisualRow),
lastVisualColumn - firstVisualColumn + 1, lastVisualRow - firstVisualRow + 1);
} else {
- for(int x = firstVisualColumn; x <= lastVisualColumn; x++)
- for(int y = firstVisualRow; y <= lastVisualRow; y++)
- visibleSpans.insert(spans.spanAt(x,y));
- visibleSpans.remove(nullptr);
- }
-
- for (QSpanCollection::Span *span : qAsConst(visibleSpans)) {
+ // Any cell outside the viewport, on the top or left, can still end up visible inside the
+ // viewport if is has a span. Calculating if a spanned cell overlaps with the viewport is
+ // "easy" enough when the columns (or rows) in the view are aligned with the columns
+ // in the model; In that case you know that if a column is outside the viewport on the
+ // right, it cannot affect the drawing of the cells inside the viewport, even with a span.
+ // And under that assumption, the spansInRect() function can be used (which is optimized
+ // to only iterate the spans that are close to the viewport).
+ // But when the view has rearranged the columns (or rows), this is no longer true. In that
+ // case, even if a column, according to the model, is outside the viewport on the right, it
+ // can still overlap with the viewport. This can happen if it was moved to the left of the
+ // viewport and one of its cells has a span. In that case we need to take the theoretically
+ // slower route and iterate through all the spans, and check if any of them overlaps with
+ // the viewport.
+ const auto spanList = spans.spans;
+ for (QSpanCollection::Span *span : spanList) {
+ const int spanVisualLeft = visualColumn(span->left());
+ const int spanVisualTop = visualRow(span->top());
+ const int spanVisualRight = spanVisualLeft + span->width() - 1;
+ const int spanVisualBottom = spanVisualTop + span->height() - 1;
+ if ((spanVisualLeft <= lastVisualColumn && spanVisualRight >= firstVisualColumn)
+ && (spanVisualTop <= lastVisualRow && spanVisualBottom >= firstVisualRow))
+ visibleSpans.insert(span);
+ }
+ }
+
+ for (QSpanCollection::Span *span : std::as_const(visibleSpans)) {
int row = span->top();
int col = span->left();
QModelIndex index = model->index(row, col, root);
@@ -926,9 +975,9 @@ void QTableViewPrivate::drawAndClipSpans(const QRegion &area, QPainter *painter,
\internal
Updates spans after row insertion.
*/
-void QTableViewPrivate::_q_updateSpanInsertedRows(const QModelIndex &parent, int start, int end)
+void QTableViewPrivate::updateSpanInsertedRows(const QModelIndex &parent, int start, int end)
{
- Q_UNUSED(parent)
+ Q_UNUSED(parent);
spans.updateInsertedRows(start, end);
}
@@ -936,9 +985,9 @@ void QTableViewPrivate::_q_updateSpanInsertedRows(const QModelIndex &parent, int
\internal
Updates spans after column insertion.
*/
-void QTableViewPrivate::_q_updateSpanInsertedColumns(const QModelIndex &parent, int start, int end)
+void QTableViewPrivate::updateSpanInsertedColumns(const QModelIndex &parent, int start, int end)
{
- Q_UNUSED(parent)
+ Q_UNUSED(parent);
spans.updateInsertedColumns(start, end);
}
@@ -946,9 +995,9 @@ void QTableViewPrivate::_q_updateSpanInsertedColumns(const QModelIndex &parent,
\internal
Updates spans after row removal.
*/
-void QTableViewPrivate::_q_updateSpanRemovedRows(const QModelIndex &parent, int start, int end)
+void QTableViewPrivate::updateSpanRemovedRows(const QModelIndex &parent, int start, int end)
{
- Q_UNUSED(parent)
+ Q_UNUSED(parent);
spans.updateRemovedRows(start, end);
}
@@ -956,9 +1005,9 @@ void QTableViewPrivate::_q_updateSpanRemovedRows(const QModelIndex &parent, int
\internal
Updates spans after column removal.
*/
-void QTableViewPrivate::_q_updateSpanRemovedColumns(const QModelIndex &parent, int start, int end)
+void QTableViewPrivate::updateSpanRemovedColumns(const QModelIndex &parent, int start, int end)
{
- Q_UNUSED(parent)
+ Q_UNUSED(parent);
spans.updateRemovedColumns(start, end);
}
@@ -966,7 +1015,7 @@ void QTableViewPrivate::_q_updateSpanRemovedColumns(const QModelIndex &parent, i
\internal
Sort the model when the header sort indicator changed
*/
-void QTableViewPrivate::_q_sortIndicatorChanged(int column, Qt::SortOrder order)
+void QTableViewPrivate::sortIndicatorChanged(int column, Qt::SortOrder order)
{
model->sort(column, order);
}
@@ -1003,7 +1052,7 @@ void QTableViewPrivate::drawCell(QPainter *painter, const QStyleOptionViewItem &
q->style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, q);
- q->itemDelegate(index)->paint(painter, opt, index);
+ q->itemDelegateForIndex(index)->paint(painter, opt, index);
}
/*!
@@ -1013,6 +1062,7 @@ void QTableViewPrivate::drawCell(QPainter *painter, const QStyleOptionViewItem &
int QTableViewPrivate::widthHintForIndex(const QModelIndex &index, int hint, const QStyleOptionViewItem &option) const
{
Q_Q(const QTableView);
+ const int oldHint = hint;
QWidget *editor = editorForIndex(index).widget.data();
if (editor && persistent.contains(editor)) {
hint = qMax(hint, editor->sizeHint().width());
@@ -1020,7 +1070,18 @@ int QTableViewPrivate::widthHintForIndex(const QModelIndex &index, int hint, con
int max = editor->maximumSize().width();
hint = qBound(min, hint, max);
}
- hint = qMax(hint, q->itemDelegate(index)->sizeHint(option, index).width());
+ hint = qMax(hint, q->itemDelegateForIndex(index)->sizeHint(option, index).width());
+
+ if (hasSpans()) {
+ auto span = spans.spanAt(index.column(), index.row());
+ if (span && span->m_left == index.column() && span->m_top == index.row()) {
+ // spans are screwed up when sections are moved
+ const auto left = logicalColumn(span->m_left);
+ for (int i = 1; i <= span->width(); ++i)
+ hint -= q->columnWidth(visualColumn(left + i));
+ }
+ hint = std::max(hint, oldHint);
+ }
return hint;
}
@@ -1042,7 +1103,7 @@ int QTableViewPrivate::heightHintForIndex(const QModelIndex &index, int hint, QS
if (wrapItemText) {// for wrapping boundaries
option.rect.setY(q->rowViewportPosition(index.row()));
int height = q->rowHeight(index.row());
- // if the option.height == 0 then q->itemDelegate(index)->sizeHint(option, index) will be wrong.
+ // if the option.height == 0 then q->itemDelegateForIndex(index)->sizeHint(option, index) will be wrong.
// The option.height == 0 is used to conclude that the text is not wrapped, and hence it will
// (exactly like widthHintForIndex) return a QSize with a long width (that we don't use) -
// and the height of the text if it was/is on one line.
@@ -1053,11 +1114,16 @@ int QTableViewPrivate::heightHintForIndex(const QModelIndex &index, int hint, QS
option.rect.setHeight(height);
option.rect.setX(q->columnViewportPosition(index.column()));
option.rect.setWidth(q->columnWidth(index.column()));
+ if (hasSpans()) {
+ auto span = spans.spanAt(index.column(), index.row());
+ if (span && span->m_left == index.column() && span->m_top == index.row())
+ option.rect.setWidth(std::max(option.rect.width(), visualSpanRect(*span).width()));
+ }
// 1px less space when grid is shown (see drawCell)
if (showGrid)
option.rect.setWidth(option.rect.width() - 1);
}
- hint = qMax(hint, q->itemDelegate(index)->sizeHint(option, index).height());
+ hint = qMax(hint, q->itemDelegateForIndex(index)->sizeHint(option, index).height());
return hint;
}
@@ -1101,6 +1167,8 @@ int QTableViewPrivate::heightHintForIndex(const QModelIndex &index, int hint, QS
table can be found by using rowHeight(); similarly, the width of
columns can be found using columnWidth(). Since both of these are plain
widgets, you can hide either of them using their hide() functions.
+ Each header is configured with its \l{QHeaderView::}{highlightSections}
+ and \l{QHeaderView::}{sectionsClickable} properties set to \c true.
Rows and columns can be hidden and shown with hideRow(), hideColumn(),
showRow(), and showColumn(). They can be selected with selectRow()
@@ -1139,7 +1207,7 @@ int QTableViewPrivate::heightHintForIndex(const QModelIndex &index, int hint, QS
operations between x-coordinates and column indexes.
\sa QTableWidget, {View Classes}, QAbstractItemModel, QAbstractItemView,
- {Chart Example}, {Pixelator Example}, {Table Model Example}
+ {Table Model Example}
*/
/*!
@@ -1170,6 +1238,8 @@ QTableView::QTableView(QTableViewPrivate &dd, QWidget *parent)
*/
QTableView::~QTableView()
{
+ Q_D(QTableView);
+ d->clearConnections();
}
/*!
@@ -1193,28 +1263,23 @@ void QTableView::setModel(QAbstractItemModel *model)
return;
//let's disconnect from the old model
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
- disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanInsertedRows(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanInsertedColumns(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanRemovedRows(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanRemovedColumns(QModelIndex,int,int)));
+ for (const QMetaObject::Connection &connection : d->modelConnections)
+ disconnect(connection);
}
if (d->selectionModel) { // support row editing
- disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
- d->model, SLOT(submit()));
+ disconnect(d->selectionmodelConnection);
}
if (model) { //and connect to the new one
- connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanInsertedRows(QModelIndex,int,int)));
- connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanInsertedColumns(QModelIndex,int,int)));
- connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanRemovedRows(QModelIndex,int,int)));
- connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_updateSpanRemovedColumns(QModelIndex,int,int)));
+ d->modelConnections = {
+ QObjectPrivate::connect(model, &QAbstractItemModel::rowsInserted,
+ d, &QTableViewPrivate::updateSpanInsertedRows),
+ QObjectPrivate::connect(model, &QAbstractItemModel::columnsInserted,
+ d, &QTableViewPrivate::updateSpanInsertedColumns),
+ QObjectPrivate::connect(model, &QAbstractItemModel::rowsRemoved,
+ d, &QTableViewPrivate::updateSpanRemovedRows),
+ QObjectPrivate::connect(model, &QAbstractItemModel::columnsRemoved,
+ d, &QTableViewPrivate::updateSpanRemovedColumns)
+ };
}
d->verticalHeader->setModel(model);
d->horizontalHeader->setModel(model);
@@ -1256,8 +1321,7 @@ void QTableView::setSelectionModel(QItemSelectionModel *selectionModel)
Q_ASSERT(selectionModel);
if (d->selectionModel) {
// support row editing
- disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
- d->model, SLOT(submit()));
+ disconnect(d->selectionmodelConnection);
}
d->verticalHeader->setSelectionModel(selectionModel);
@@ -1266,8 +1330,9 @@ void QTableView::setSelectionModel(QItemSelectionModel *selectionModel)
if (d->selectionModel) {
// support row editing
- connect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
- d->model, SLOT(submit()));
+ d->selectionmodelConnection =
+ connect(d->selectionModel, &QItemSelectionModel::currentRowChanged,
+ d->model, &QAbstractItemModel::submit);
}
}
@@ -1304,6 +1369,8 @@ void QTableView::setHorizontalHeader(QHeaderView *header)
if (!header || header == d->horizontalHeader)
return;
+ for (const QMetaObject::Connection &connection : d->horHeaderConnections)
+ disconnect(connection);
if (d->horizontalHeader && d->horizontalHeader->parent() == this)
delete d->horizontalHeader;
d->horizontalHeader = header;
@@ -1315,18 +1382,18 @@ void QTableView::setHorizontalHeader(QHeaderView *header)
d->horizontalHeader->setSelectionModel(d->selectionModel);
}
- connect(d->horizontalHeader,SIGNAL(sectionResized(int,int,int)),
- this, SLOT(columnResized(int,int,int)));
- connect(d->horizontalHeader, SIGNAL(sectionMoved(int,int,int)),
- this, SLOT(columnMoved(int,int,int)));
- connect(d->horizontalHeader, SIGNAL(sectionCountChanged(int,int)),
- this, SLOT(columnCountChanged(int,int)));
- connect(d->horizontalHeader, SIGNAL(sectionPressed(int)), this, SLOT(selectColumn(int)));
- connect(d->horizontalHeader, SIGNAL(sectionEntered(int)), this, SLOT(_q_selectColumn(int)));
- connect(d->horizontalHeader, SIGNAL(sectionHandleDoubleClicked(int)),
- this, SLOT(resizeColumnToContents(int)));
- connect(d->horizontalHeader, SIGNAL(geometriesChanged()), this, SLOT(updateGeometries()));
-
+ d->horHeaderConnections = {
+ connect(d->horizontalHeader,&QHeaderView::sectionResized,
+ this, &QTableView::columnResized),
+ connect(d->horizontalHeader, &QHeaderView::sectionMoved,
+ this, &QTableView::columnMoved),
+ connect(d->horizontalHeader, &QHeaderView::sectionCountChanged,
+ this, &QTableView::columnCountChanged),
+ connect(d->horizontalHeader, &QHeaderView::sectionHandleDoubleClicked,
+ this, &QTableView::resizeColumnToContents),
+ connect(d->horizontalHeader, &QHeaderView::geometriesChanged,
+ this, &QTableView::updateGeometries),
+ };
//update the sorting enabled states on the new header
setSortingEnabled(d->sortingEnabled);
}
@@ -1342,6 +1409,8 @@ void QTableView::setVerticalHeader(QHeaderView *header)
if (!header || header == d->verticalHeader)
return;
+ for (const QMetaObject::Connection &connection : d->verHeaderConnections)
+ disconnect(connection);
if (d->verticalHeader && d->verticalHeader->parent() == this)
delete d->verticalHeader;
d->verticalHeader = header;
@@ -1353,21 +1422,26 @@ void QTableView::setVerticalHeader(QHeaderView *header)
d->verticalHeader->setSelectionModel(d->selectionModel);
}
- connect(d->verticalHeader, SIGNAL(sectionResized(int,int,int)),
- this, SLOT(rowResized(int,int,int)));
- connect(d->verticalHeader, SIGNAL(sectionMoved(int,int,int)),
- this, SLOT(rowMoved(int,int,int)));
- connect(d->verticalHeader, SIGNAL(sectionCountChanged(int,int)),
- this, SLOT(rowCountChanged(int,int)));
- connect(d->verticalHeader, SIGNAL(sectionPressed(int)), this, SLOT(selectRow(int)));
- connect(d->verticalHeader, SIGNAL(sectionEntered(int)), this, SLOT(_q_selectRow(int)));
- connect(d->verticalHeader, SIGNAL(sectionHandleDoubleClicked(int)),
- this, SLOT(resizeRowToContents(int)));
- connect(d->verticalHeader, SIGNAL(geometriesChanged()), this, SLOT(updateGeometries()));
+ d->verHeaderConnections = {
+ connect(d->verticalHeader, &QHeaderView::sectionResized,
+ this, &QTableView::rowResized),
+ connect(d->verticalHeader, &QHeaderView::sectionMoved,
+ this, &QTableView::rowMoved),
+ connect(d->verticalHeader, &QHeaderView::sectionCountChanged,
+ this, &QTableView::rowCountChanged),
+ connect(d->verticalHeader, &QHeaderView::sectionPressed,
+ this, &QTableView::selectRow),
+ connect(d->verticalHeader, &QHeaderView::sectionHandleDoubleClicked,
+ this, &QTableView::resizeRowToContents),
+ connect(d->verticalHeader, &QHeaderView::geometriesChanged,
+ this, &QTableView::updateGeometries),
+ connect(d->verticalHeader, &QHeaderView::sectionEntered,
+ this, [d](int row) { d->selectRow(row, false); })
+ };
}
/*!
- \internal
+ \reimp
Scroll the contents of the table view by (\a dx, \a dy).
*/
@@ -1412,11 +1486,10 @@ void QTableView::scrollContentsBy(int dx, int dy)
/*!
\reimp
*/
-QStyleOptionViewItem QTableView::viewOptions() const
+void QTableView::initViewItemOption(QStyleOptionViewItem *option) const
{
- QStyleOptionViewItem option = QAbstractItemView::viewOptions();
- option.showDecorationSelected = true;
- return option;
+ QAbstractItemView::initViewItemOption(option);
+ option->showDecorationSelected = true;
}
/*!
@@ -1426,13 +1499,14 @@ void QTableView::paintEvent(QPaintEvent *event)
{
Q_D(QTableView);
// setup temp variables for the painting
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
const QPoint offset = d->scrollDelayOffset;
const bool showGrid = d->showGrid;
const int gridSize = showGrid ? 1 : 0;
const int gridHint = style()->styleHint(QStyle::SH_Table_GridLineColor, &option, this);
const QColor gridColor = QColor::fromRgba(static_cast<QRgb>(gridHint));
- const QPen gridPen = QPen(gridColor, 0, d->gridStyle);
+ const QPen gridPen = QPen(gridColor, 1, d->gridStyle);
const QHeaderView *verticalHeader = d->verticalHeader;
const QHeaderView *horizontalHeader = d->horizontalHeader;
const bool alternate = d->alternatingColors;
@@ -1566,7 +1640,8 @@ void QTableView::paintEvent(QPaintEvent *event)
int rowY = rowViewportPosition(row);
rowY += offset.y();
int rowh = rowHeight(row) - gridSize;
- painter.drawLine(dirtyArea.left(), rowY + rowh, dirtyArea.right(), rowY + rowh);
+ QLineF line(dirtyArea.left(), rowY + rowh, dirtyArea.right(), rowY + rowh);
+ painter.drawLine(line.translated(0.5, 0.5));
}
// Paint each column
@@ -1578,7 +1653,30 @@ void QTableView::paintEvent(QPaintEvent *event)
colp += offset.x();
if (!rightToLeft)
colp += columnWidth(col) - gridSize;
- painter.drawLine(colp, dirtyArea.top(), colp, dirtyArea.bottom());
+ QLineF line(colp, dirtyArea.top(), colp, dirtyArea.bottom());
+ painter.drawLine(line.translated(0.5, 0.5));
+ }
+ const bool drawWhenHidden = style()->styleHint(QStyle::SH_Table_AlwaysDrawLeftTopGridLines,
+ &option, this);
+ if (drawWhenHidden && horizontalHeader->isHidden()) {
+ const int row = verticalHeader->logicalIndex(top);
+ if (!verticalHeader->isSectionHidden(row)) {
+ const int rowY = rowViewportPosition(row) + offset.y();
+ if (rowY == dirtyArea.top())
+ painter.drawLine(dirtyArea.left(), rowY, dirtyArea.right(), rowY);
+ }
+ }
+ if (drawWhenHidden && verticalHeader->isHidden()) {
+ const int col = horizontalHeader->logicalIndex(left);
+ if (!horizontalHeader->isSectionHidden(col)) {
+ int colX = columnViewportPosition(col) + offset.x();
+ if (!isLeftToRight())
+ colX += columnWidth(left) - 1;
+ if (isLeftToRight() && colX == dirtyArea.left())
+ painter.drawLine(colX, dirtyArea.top(), colX, dirtyArea.bottom());
+ if (!isLeftToRight() && colX == dirtyArea.right())
+ painter.drawLine(colX, dirtyArea.top(), colX, dirtyArea.bottom());
+ }
}
painter.setPen(old);
}
@@ -1918,6 +2016,9 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF
if (d->hasSpans()) {
bool expanded;
+ // when the current selection does not intersect with any spans of merged cells,
+ // the range of selected cells must be the same as if there were no merged cells
+ bool intersectsSpan = false;
int top = qMin(d->visualRow(tl.row()), d->visualRow(br.row()));
int left = qMin(d->visualColumn(tl.column()), d->visualColumn(br.column()));
int bottom = qMax(d->visualRow(tl.row()), d->visualRow(br.row()));
@@ -1932,6 +2033,7 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF
int r = d->visualColumn(d->columnSpanEndLogical(span.left(), span.width()));
if ((t > bottom) || (l > right) || (top > b) || (left > r))
continue; // no intersect
+ intersectsSpan = true;
if (t < top) {
top = t;
expanded = true;
@@ -1952,14 +2054,20 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF
break;
}
} while (expanded);
- selection.reserve((right - left + 1) * (bottom - top + 1));
- for (int horizontal = left; horizontal <= right; ++horizontal) {
- int column = d->logicalColumn(horizontal);
- for (int vertical = top; vertical <= bottom; ++vertical) {
- int row = d->logicalRow(vertical);
- QModelIndex index = d->model->index(row, column, d->root);
- selection.append(QItemSelectionRange(index));
+ if (intersectsSpan) {
+ selection.reserve((right - left + 1) * (bottom - top + 1));
+ for (int horizontal = left; horizontal <= right; ++horizontal) {
+ int column = d->logicalColumn(horizontal);
+ for (int vertical = top; vertical <= bottom; ++vertical) {
+ int row = d->logicalRow(vertical);
+ QModelIndex index = d->model->index(row, column, d->root);
+ selection.append(QItemSelectionRange(index));
+ }
}
+ } else {
+ QItemSelectionRange range(tl, br);
+ if (!range.isEmpty())
+ selection.append(range);
}
} else if (verticalMoved && horizontalMoved) {
int top = d->visualRow(tl.row());
@@ -2005,7 +2113,7 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF
}
/*!
- \internal
+ \reimp
Returns the rectangle from the viewport of the items in the given
\a selection.
@@ -2114,7 +2222,7 @@ QModelIndexList QTableView::selectedIndexes() const
QModelIndexList modelSelected;
if (d->selectionModel)
modelSelected = d->selectionModel->selectedIndexes();
- for (int i = 0; i < modelSelected.count(); ++i) {
+ for (int i = 0; i < modelSelected.size(); ++i) {
QModelIndex index = modelSelected.at(i);
if (!isIndexHidden(index) && index.parent() == d->root)
viewSelected.append(index);
@@ -2305,7 +2413,8 @@ int QTableView::sizeHintForRow(int row) const
if (right == -1) // the table don't have enough columns to fill the viewport
right = d->model->columnCount(d->root) - 1;
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
int hint = 0;
QModelIndex index;
@@ -2323,12 +2432,12 @@ int QTableView::sizeHintForRow(int row) const
break;
}
- int actualRight = d->model->columnCount(d->root) - 1;
+ const int actualRight = d->model->columnCount(d->root) - 1;
int idxLeft = left;
int idxRight = column - 1;
- if (maximumProcessCols == 0)
- columnsProcessed = 0; // skip the while loop
+ if (maximumProcessCols == 0 || actualRight < idxLeft)
+ columnsProcessed = maximumProcessCols; // skip the while loop
while (columnsProcessed != maximumProcessCols && (idxLeft > 0 || idxRight < actualRight)) {
int logicalIdx = -1;
@@ -2352,11 +2461,10 @@ int QTableView::sizeHintForRow(int row) const
break;
}
}
- if (logicalIdx < 0)
- continue;
-
- index = d->model->index(row, logicalIdx, d->root);
- hint = d->heightHintForIndex(index, hint, option);
+ if (logicalIdx >= 0) {
+ index = d->model->index(row, logicalIdx, d->root);
+ hint = d->heightHintForIndex(index, hint, option);
+ }
++columnsProcessed;
}
@@ -2393,7 +2501,8 @@ int QTableView::sizeHintForColumn(int column) const
if (!isVisible() || bottom == -1) // the table don't have enough rows to fill the viewport
bottom = d->model->rowCount(d->root) - 1;
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
int hint = 0;
int rowsProcessed = 0;
@@ -2411,12 +2520,12 @@ int QTableView::sizeHintForColumn(int column) const
break;
}
- int actualBottom = d->model->rowCount(d->root) - 1;
+ const int actualBottom = d->model->rowCount(d->root) - 1;
int idxTop = top;
int idxBottom = row - 1;
- if (maximumProcessRows == 0)
- rowsProcessed = 0; // skip the while loop
+ if (maximumProcessRows == 0 || actualBottom < idxTop)
+ rowsProcessed = maximumProcessRows; // skip the while loop
while (rowsProcessed != maximumProcessRows && (idxTop > 0 || idxBottom < actualBottom)) {
int logicalIdx = -1;
@@ -2440,11 +2549,10 @@ int QTableView::sizeHintForColumn(int column) const
break;
}
}
- if (logicalIdx < 0)
- continue;
-
- index = d->model->index(logicalIdx, column, d->root);
- hint = d->widthHintForIndex(index, hint, option);
+ if (logicalIdx >= 0) {
+ index = d->model->index(logicalIdx, column, d->root);
+ hint = d->widthHintForIndex(index, hint, option);
+ }
++rowsProcessed;
}
@@ -2619,24 +2727,25 @@ void QTableView::setSortingEnabled(bool enable)
{
Q_D(QTableView);
horizontalHeader()->setSortIndicatorShown(enable);
+ for (const QMetaObject::Connection &connection : d->dynHorHeaderConnections)
+ disconnect(connection);
+ d->dynHorHeaderConnections.clear();
if (enable) {
- disconnect(d->horizontalHeader, SIGNAL(sectionEntered(int)),
- this, SLOT(_q_selectColumn(int)));
- disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
- this, SLOT(selectColumn(int)));
//sortByColumn has to be called before we connect or set the sortingEnabled flag
// because otherwise it will not call sort on the model.
- sortByColumn(horizontalHeader()->sortIndicatorSection(),
- horizontalHeader()->sortIndicatorOrder());
- connect(horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
- this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)), Qt::UniqueConnection);
+ sortByColumn(d->horizontalHeader->sortIndicatorSection(),
+ d->horizontalHeader->sortIndicatorOrder());
+ d->dynHorHeaderConnections = {
+ QObjectPrivate::connect(d->horizontalHeader, &QHeaderView::sortIndicatorChanged,
+ d, &QTableViewPrivate::sortIndicatorChanged)
+ };
} else {
- connect(d->horizontalHeader, SIGNAL(sectionEntered(int)),
- this, SLOT(_q_selectColumn(int)), Qt::UniqueConnection);
- connect(horizontalHeader(), SIGNAL(sectionPressed(int)),
- this, SLOT(selectColumn(int)), Qt::UniqueConnection);
- disconnect(horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
- this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)));
+ d->dynHorHeaderConnections = {
+ connect(d->horizontalHeader, &QHeaderView::sectionPressed,
+ this, &QTableView::selectColumn),
+ connect(d->horizontalHeader, &QHeaderView::sectionEntered,
+ this, [d](int column) {d->selectColumn(column, false); })
+ };
}
d->sortingEnabled = enable;
}
@@ -2746,7 +2855,7 @@ bool QTableView::isCornerButtonEnabled() const
#endif
/*!
- \internal
+ \reimp
Returns the rectangle on the viewport occupied by the given \a
index.
@@ -2776,9 +2885,9 @@ QRect QTableView::visualRect(const QModelIndex &index) const
}
/*!
- \internal
+ \reimp
- Makes sure that the given \a item is visible in the table view,
+ Makes sure that the given \a index is visible in the table view,
scrolling if necessary.
*/
void QTableView::scrollTo(const QModelIndex &index, ScrollHint hint)
@@ -2939,14 +3048,19 @@ void QTableView::timerEvent(QTimerEvent *event)
Q_D(QTableView);
if (event->timerId() == d->columnResizeTimerID) {
- updateGeometries();
- killTimer(d->columnResizeTimerID);
- d->columnResizeTimerID = 0;
+ const int oldScrollMax = horizontalScrollBar()->maximum();
+ if (horizontalHeader()->d_func()->state != QHeaderViewPrivate::ResizeSection) {
+ updateGeometries();
+ killTimer(d->columnResizeTimerID);
+ d->columnResizeTimerID = 0;
+ } else {
+ updateEditorGeometries();
+ }
QRect rect;
int viewportHeight = d->viewport->height();
int viewportWidth = d->viewport->width();
- if (d->hasSpans()) {
+ if (d->hasSpans() || horizontalScrollBar()->value() == oldScrollMax) {
rect = QRect(0, 0, viewportWidth, viewportHeight);
} else {
for (int i = d->columnsToUpdate.size()-1; i >= 0; --i) {
@@ -2964,14 +3078,19 @@ void QTableView::timerEvent(QTimerEvent *event)
}
if (event->timerId() == d->rowResizeTimerID) {
- updateGeometries();
- killTimer(d->rowResizeTimerID);
- d->rowResizeTimerID = 0;
+ const int oldScrollMax = verticalScrollBar()->maximum();
+ if (verticalHeader()->d_func()->state != QHeaderViewPrivate::ResizeSection) {
+ updateGeometries();
+ killTimer(d->rowResizeTimerID);
+ d->rowResizeTimerID = 0;
+ } else {
+ updateEditorGeometries();
+ }
int viewportHeight = d->viewport->height();
int viewportWidth = d->viewport->width();
int top;
- if (d->hasSpans()) {
+ if (d->hasSpans() || verticalScrollBar()->value() == oldScrollMax) {
top = 0;
} else {
top = viewportHeight;
@@ -2995,8 +3114,9 @@ void QTableView::timerEvent(QTimerEvent *event)
\sa columnMoved()
*/
-void QTableView::rowMoved(int, int oldIndex, int newIndex)
+void QTableView::rowMoved(int row, int oldIndex, int newIndex)
{
+ Q_UNUSED(row);
Q_D(QTableView);
updateGeometries();
@@ -3023,8 +3143,9 @@ void QTableView::rowMoved(int, int oldIndex, int newIndex)
\sa rowMoved()
*/
-void QTableView::columnMoved(int, int oldIndex, int newIndex)
+void QTableView::columnMoved(int column, int oldIndex, int newIndex)
{
+ Q_UNUSED(column);
Q_D(QTableView);
updateGeometries();
@@ -3167,22 +3288,6 @@ void QTableView::resizeColumnsToContents()
d->horizontalHeader->resizeSections(QHeaderView::ResizeToContents);
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \obsolete
- \overload
-
- This function is deprecated. Use
- sortByColumn(int column, Qt::SortOrder order) instead.
- Sorts the model by the values in the given \a column.
-*/
-void QTableView::sortByColumn(int column)
-{
- Q_D(QTableView);
- sortByColumn(column, d->horizontalHeader->sortIndicatorOrder());
-}
-#endif
-
/*!
\since 4.2
@@ -3199,11 +3304,11 @@ void QTableView::sortByColumn(int column, Qt::SortOrder order)
Q_D(QTableView);
if (column < -1)
return;
- // If sorting is enabled it will emit a signal connected to
- // _q_sortIndicatorChanged, which then actually sorts
d->horizontalHeader->setSortIndicator(column, order);
- // If sorting is not enabled, force to sort now
- if (!d->sortingEnabled)
+ // If sorting is not enabled or has the same order as before, force to sort now
+ // else sorting will be trigger through sortIndicatorChanged()
+ if (!d->sortingEnabled ||
+ (d->horizontalHeader->sortIndicatorSection() == column && d->horizontalHeader->sortIndicatorOrder() == order))
d->model->sort(column, order);
}
@@ -3300,16 +3405,6 @@ void QTableView::clearSpans()
d->viewport->update();
}
-void QTableViewPrivate::_q_selectRow(int row)
-{
- selectRow(row, false);
-}
-
-void QTableViewPrivate::_q_selectColumn(int column)
-{
- selectColumn(column, false);
-}
-
void QTableViewPrivate::selectRow(int row, bool anchor)
{
Q_Q(QTableView);
@@ -3326,7 +3421,7 @@ void QTableViewPrivate::selectRow(int row, bool anchor)
selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
if ((anchor && !(command & QItemSelectionModel::Current))
|| (q->selectionMode() == QTableView::SingleSelection))
- rowSectionAnchor = row;
+ currentSelectionStartIndex = model->index(row, column, root);
if (q->selectionMode() != QTableView::SingleSelection
&& command.testFlag(QItemSelectionModel::Toggle)) {
@@ -3339,6 +3434,7 @@ void QTableViewPrivate::selectRow(int row, bool anchor)
command |= QItemSelectionModel::Current;
}
+ const auto rowSectionAnchor = currentSelectionStartIndex.row();
QModelIndex upper = model->index(qMin(rowSectionAnchor, row), column, root);
QModelIndex lower = model->index(qMax(rowSectionAnchor, row), column, root);
if ((verticalHeader->sectionsMoved() && upper.row() != lower.row())) {
@@ -3365,12 +3461,12 @@ void QTableViewPrivate::selectColumn(int column, bool anchor)
selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
if ((anchor && !(command & QItemSelectionModel::Current))
|| (q->selectionMode() == QTableView::SingleSelection))
- columnSectionAnchor = column;
+ currentSelectionStartIndex = model->index(row, column, root);
if (q->selectionMode() != QTableView::SingleSelection
&& command.testFlag(QItemSelectionModel::Toggle)) {
if (anchor)
- ctrlDragSelectionFlag = horizontalHeader->selectionModel()->selectedColumns().contains(index)
+ ctrlDragSelectionFlag = horizontalHeader->selectionModel()->selectedColumns(row).contains(index)
? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
command &= ~QItemSelectionModel::Toggle;
command |= ctrlDragSelectionFlag;
@@ -3378,6 +3474,7 @@ void QTableViewPrivate::selectColumn(int column, bool anchor)
command |= QItemSelectionModel::Current;
}
+ const auto columnSectionAnchor = currentSelectionStartIndex.column();
QModelIndex left = model->index(row, qMin(columnSectionAnchor, column), root);
QModelIndex right = model->index(row, qMax(columnSectionAnchor, column), root);
if ((horizontalHeader->sectionsMoved() && left.column() != right.column())) {
@@ -3393,9 +3490,9 @@ void QTableViewPrivate::selectColumn(int column, bool anchor)
*/
void QTableView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
- if (current.isValid()) {
+ if (current.isValid() && hasFocus()) {
Q_D(QTableView);
int entry = d->accessibleTable2Index(current);
QAccessibleEvent event(this, QAccessible::Focus);
@@ -3414,8 +3511,8 @@ void QTableView::selectionChanged(const QItemSelection &selected,
const QItemSelection &deselected)
{
Q_D(QTableView);
- Q_UNUSED(d)
-#ifndef QT_NO_ACCESSIBILITY
+ Q_UNUSED(d);
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
// ### does not work properly for selection ranges.
QModelIndex sel = selected.indexes().value(0);
diff --git a/src/widgets/itemviews/qtableview.h b/src/widgets/itemviews/qtableview.h
index 3275c09f88..eff0ea3502 100644
--- a/src/widgets/itemviews/qtableview.h
+++ b/src/widgets/itemviews/qtableview.h
@@ -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
#ifndef QTABLEVIEW_H
#define QTABLEVIEW_H
@@ -130,10 +94,6 @@ public Q_SLOTS:
void resizeRowsToContents();
void resizeColumnToContents(int column);
void resizeColumnsToContents();
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTableView::sortByColumn(int column, Qt::SortOrder order) instead")
- void sortByColumn(int column);
-#endif
void sortByColumn(int column, Qt::SortOrder order);
void setShowGrid(bool show);
@@ -149,7 +109,7 @@ protected:
QTableView(QTableViewPrivate &, QWidget *parent);
void scrollContentsBy(int dx, int dy) override;
- QStyleOptionViewItem viewOptions() const override;
+ void initViewItemOption(QStyleOptionViewItem *option) const override;
void paintEvent(QPaintEvent *e) override;
void timerEvent(QTimerEvent *event) override;
@@ -185,13 +145,6 @@ private:
Q_DECLARE_PRIVATE(QTableView)
Q_DISABLE_COPY(QTableView)
- Q_PRIVATE_SLOT(d_func(), void _q_selectRow(int))
- Q_PRIVATE_SLOT(d_func(), void _q_selectColumn(int))
- Q_PRIVATE_SLOT(d_func(), void _q_updateSpanInsertedRows(QModelIndex,int,int))
- Q_PRIVATE_SLOT(d_func(), void _q_updateSpanInsertedColumns(QModelIndex,int,int))
- Q_PRIVATE_SLOT(d_func(), void _q_updateSpanRemovedRows(QModelIndex,int,int))
- Q_PRIVATE_SLOT(d_func(), void _q_updateSpanRemovedColumns(QModelIndex,int,int))
- Q_PRIVATE_SLOT(d_func(), void _q_sortIndicatorChanged(int column, Qt::SortOrder order))
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qtableview_p.h b/src/widgets/itemviews/qtableview_p.h
index 8f174351d2..862a016d5f 100644
--- a/src/widgets/itemviews/qtableview_p.h
+++ b/src/widgets/itemviews/qtableview_p.h
@@ -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
#ifndef QTABLEVIEW_P_H
#define QTABLEVIEW_P_H
@@ -52,13 +16,18 @@
//
#include <QtWidgets/private/qtwidgetsglobal_p.h>
+#include "qtableview.h"
+#include "qheaderview.h"
+
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QSet>
#include <QtCore/QDebug>
#include "private/qabstractitemview_p.h"
+#include <array>
#include <list>
+#include <vector>
QT_REQUIRE_CONFIG(tableview);
@@ -127,16 +96,17 @@ private:
bool cleanSpanSubIndex(SubIndex &subindex, int end, bool update = false);
};
-Q_DECLARE_TYPEINFO ( QSpanCollection::Span, Q_MOVABLE_TYPE);
-
+Q_DECLARE_TYPEINFO ( QSpanCollection::Span, Q_RELOCATABLE_TYPE);
-class QTableViewPrivate : public QAbstractItemViewPrivate
+#if QT_CONFIG(abstractbutton)
+class QTableCornerButton;
+#endif
+class Q_AUTOTEST_EXPORT QTableViewPrivate : public QAbstractItemViewPrivate
{
Q_DECLARE_PUBLIC(QTableView)
public:
QTableViewPrivate()
: showGrid(true), gridStyle(Qt::SolidLine),
- rowSectionAnchor(-1), columnSectionAnchor(-1),
columnResizeTimerID(0), rowResizeTimerID(0),
horizontalHeader(nullptr), verticalHeader(nullptr),
sortingEnabled(false), geometryRecursionBlock(false),
@@ -148,7 +118,9 @@ public:
#endif
}
void init();
+ void clearConnections();
void trimHiddenSelections(QItemSelectionRange *range) const;
+ QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const override;
inline bool isHidden(int row, int col) const {
return verticalHeader->isSectionHidden(row)
@@ -185,17 +157,22 @@ public:
bool showGrid;
Qt::PenStyle gridStyle;
- int rowSectionAnchor;
- int columnSectionAnchor;
int columnResizeTimerID;
int rowResizeTimerID;
- QVector<int> columnsToUpdate;
- QVector<int> rowsToUpdate;
+ QList<int> columnsToUpdate;
+ QList<int> rowsToUpdate;
QHeaderView *horizontalHeader;
QHeaderView *verticalHeader;
#if QT_CONFIG(abstractbutton)
- QWidget *cornerWidget;
+ QTableCornerButton *cornerWidget;
+ QMetaObject::Connection cornerWidgetConnection;
#endif
+ QMetaObject::Connection selectionmodelConnection;
+ std::array<QMetaObject::Connection, 4> modelConnections;
+ std::array<QMetaObject::Connection, 7> verHeaderConnections;
+ std::array<QMetaObject::Connection, 5> horHeaderConnections;
+ std::vector<QMetaObject::Connection> dynHorHeaderConnections;
+
bool sortingEnabled;
bool geometryRecursionBlock;
QPoint visualCursor; // (Row,column) cell coordinates to track through span navigation.
@@ -248,17 +225,14 @@ public:
QRect visualSpanRect(const QSpanCollection::Span &span) const;
- void _q_selectRow(int row);
- void _q_selectColumn(int column);
-
void selectRow(int row, bool anchor);
void selectColumn(int column, bool anchor);
- void _q_updateSpanInsertedRows(const QModelIndex &parent, int start, int end);
- void _q_updateSpanInsertedColumns(const QModelIndex &parent, int start, int end);
- void _q_updateSpanRemovedRows(const QModelIndex &parent, int start, int end);
- void _q_updateSpanRemovedColumns(const QModelIndex &parent, int start, int end);
- void _q_sortIndicatorChanged(int column, Qt::SortOrder order);
+ void updateSpanInsertedRows(const QModelIndex &parent, int start, int end);
+ void updateSpanInsertedColumns(const QModelIndex &parent, int start, int end);
+ void updateSpanRemovedRows(const QModelIndex &parent, int start, int end);
+ void updateSpanRemovedColumns(const QModelIndex &parent, int start, int end);
+ void sortIndicatorChanged(int column, Qt::SortOrder order);
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp
index 91860341ee..6dd812f6fb 100644
--- a/src/widgets/itemviews/qtablewidget.cpp
+++ b/src/widgets/itemviews/qtablewidget.cpp
@@ -1,45 +1,8 @@
-/****************************************************************************
-**
-** 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 "qtablewidget.h"
-#include <qitemdelegate.h>
#include <qpainter.h>
#include <private/qtablewidget_p.h>
@@ -49,7 +12,7 @@ QT_BEGIN_NAMESPACE
QTableModel::QTableModel(int rows, int columns, QTableWidget *parent)
: QAbstractTableModel(parent),
- prototype(0),
+ prototype(nullptr),
tableItems(rows * columns, 0),
verticalHeaderItems(rows, 0),
horizontalHeaderItems(columns, 0)
@@ -63,12 +26,12 @@ QTableModel::~QTableModel()
bool QTableModel::insertRows(int row, int count, const QModelIndex &)
{
- if (count < 1 || row < 0 || row > verticalHeaderItems.count())
+ if (count < 1 || row < 0 || row > verticalHeaderItems.size())
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
- int rc = verticalHeaderItems.count();
- int cc = horizontalHeaderItems.count();
+ int rc = verticalHeaderItems.size();
+ int cc = horizontalHeaderItems.size();
verticalHeaderItems.insert(row, count, 0);
if (rc == 0)
tableItems.resize(cc * count);
@@ -80,12 +43,12 @@ bool QTableModel::insertRows(int row, int count, const QModelIndex &)
bool QTableModel::insertColumns(int column, int count, const QModelIndex &)
{
- if (count < 1 || column < 0 || column > horizontalHeaderItems.count())
+ if (count < 1 || column < 0 || column > horizontalHeaderItems.size())
return false;
beginInsertColumns(QModelIndex(), column, column + count - 1);
- int rc = verticalHeaderItems.count();
- int cc = horizontalHeaderItems.count();
+ int rc = verticalHeaderItems.size();
+ int cc = horizontalHeaderItems.size();
horizontalHeaderItems.insert(column, count, 0);
if (cc == 0)
tableItems.resize(rc * count);
@@ -98,24 +61,24 @@ bool QTableModel::insertColumns(int column, int count, const QModelIndex &)
bool QTableModel::removeRows(int row, int count, const QModelIndex &)
{
- if (count < 1 || row < 0 || row + count > verticalHeaderItems.count())
+ if (count < 1 || row < 0 || row + count > verticalHeaderItems.size())
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
int i = tableIndex(row, 0);
int n = count * columnCount();
- QTableWidgetItem *oldItem = 0;
+ QTableWidgetItem *oldItem = nullptr;
for (int j = i; j < n + i; ++j) {
oldItem = tableItems.at(j);
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete oldItem;
}
tableItems.remove(qMax(i, 0), n);
for (int v = row; v < row + count; ++v) {
oldItem = verticalHeaderItems.at(v);
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete oldItem;
}
verticalHeaderItems.remove(row, count);
@@ -125,17 +88,17 @@ bool QTableModel::removeRows(int row, int count, const QModelIndex &)
bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
{
- if (count < 1 || column < 0 || column + count > horizontalHeaderItems.count())
+ if (count < 1 || column < 0 || column + count > horizontalHeaderItems.size())
return false;
beginRemoveColumns(QModelIndex(), column, column + count - 1);
- QTableWidgetItem *oldItem = 0;
+ QTableWidgetItem *oldItem = nullptr;
for (int row = rowCount() - 1; row >= 0; --row) {
int i = tableIndex(row, column);
for (int j = i; j < i + count; ++j) {
oldItem = tableItems.at(j);
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete oldItem;
}
tableItems.remove(i, count);
@@ -143,7 +106,7 @@ bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
for (int h=column; h<column+count; ++h) {
oldItem = horizontalHeaderItems.at(h);
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete oldItem;
}
horizontalHeaderItems.remove(column, count);
@@ -154,7 +117,7 @@ bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
{
int i = tableIndex(row, column);
- if (i < 0 || i >= tableItems.count())
+ if (i < 0 || i >= tableItems.size())
return;
QTableWidgetItem *oldItem = tableItems.at(i);
if (item == oldItem)
@@ -162,7 +125,7 @@ void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
// remove old
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete tableItems.at(i);
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
@@ -176,15 +139,15 @@ void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
&& view->horizontalHeader()->sortIndicatorSection() == column) {
// sorted insertion
Qt::SortOrder order = view->horizontalHeader()->sortIndicatorOrder();
- QVector<QTableWidgetItem*> colItems = columnItems(column);
- if (row < colItems.count())
+ QList<QTableWidgetItem *> colItems = columnItems(column);
+ if (row < colItems.size())
colItems.remove(row);
int sortedRow;
- if (item == 0) {
+ if (item == nullptr) {
// move to after all non-0 (sortable) items
- sortedRow = colItems.count();
+ sortedRow = colItems.size();
} else {
- QVector<QTableWidgetItem*>::iterator it;
+ QList<QTableWidgetItem *>::iterator it;
it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item);
sortedRow = qMax((int)(it - colItems.begin()), 0);
}
@@ -192,7 +155,7 @@ void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint);
// move the items @ row to sortedRow
int cc = columnCount();
- QVector<QTableWidgetItem*> rowItems(cc);
+ QList<QTableWidgetItem *> rowItems(cc);
for (int j = 0; j < cc; ++j)
rowItems[j] = tableItems.at(tableIndex(row, j));
tableItems.remove(tableIndex(row, 0), cc);
@@ -222,7 +185,7 @@ QTableWidgetItem *QTableModel::takeItem(int row, int column)
long i = tableIndex(row, column);
QTableWidgetItem *itm = tableItems.value(i);
if (itm) {
- itm->view = 0;
+ itm->view = nullptr;
itm->d->id = -1;
tableItems[i] = 0;
const QModelIndex ind = index(row, column);
@@ -239,7 +202,7 @@ QTableWidgetItem *QTableModel::item(int row, int column) const
QTableWidgetItem *QTableModel::item(const QModelIndex &index) const
{
if (!isValid(index))
- return 0;
+ return nullptr;
return tableItems.at(tableIndex(index.row(), index.column()));
}
@@ -247,8 +210,8 @@ void QTableModel::removeItem(QTableWidgetItem *item)
{
int i = tableItems.indexOf(item);
if (i != -1) {
- tableItems[i] = 0;
QModelIndex idx = index(item);
+ tableItems[i] = nullptr;
emit dataChanged(idx, idx);
return;
}
@@ -270,21 +233,21 @@ void QTableModel::removeItem(QTableWidgetItem *item)
void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item)
{
- if (section < 0 || section >= horizontalHeaderItems.count())
+ if (section < 0 || section >= horizontalHeaderItems.size())
return;
QTableWidgetItem *oldItem = horizontalHeaderItems.at(section);
if (item == oldItem)
return;
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete oldItem;
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
if (item) {
item->view = view;
- item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem);
+ item->d->headerItem = true;
}
horizontalHeaderItems[section] = item;
emit headerDataChanged(Qt::Horizontal, section, section);
@@ -292,21 +255,21 @@ void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item)
void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item)
{
- if (section < 0 || section >= verticalHeaderItems.count())
+ if (section < 0 || section >= verticalHeaderItems.size())
return;
QTableWidgetItem *oldItem = verticalHeaderItems.at(section);
if (item == oldItem)
return;
if (oldItem)
- oldItem->view = 0;
+ oldItem->view = nullptr;
delete oldItem;
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
if (item) {
item->view = view;
- item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem);
+ item->d->headerItem = true;
}
verticalHeaderItems[section] = item;
emit headerDataChanged(Qt::Vertical, section, section);
@@ -314,12 +277,12 @@ void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item)
QTableWidgetItem *QTableModel::takeHorizontalHeaderItem(int section)
{
- if (section < 0 || section >= horizontalHeaderItems.count())
- return 0;
+ if (section < 0 || section >= horizontalHeaderItems.size())
+ return nullptr;
QTableWidgetItem *itm = horizontalHeaderItems.at(section);
if (itm) {
- itm->view = 0;
- itm->itemFlags &= ~ItemIsHeaderItem;
+ itm->view = nullptr;
+ itm->d->headerItem = false;
horizontalHeaderItems[section] = 0;
}
return itm;
@@ -327,12 +290,12 @@ QTableWidgetItem *QTableModel::takeHorizontalHeaderItem(int section)
QTableWidgetItem *QTableModel::takeVerticalHeaderItem(int section)
{
- if (section < 0 || section >= verticalHeaderItems.count())
- return 0;
+ if (section < 0 || section >= verticalHeaderItems.size())
+ return nullptr;
QTableWidgetItem *itm = verticalHeaderItems.at(section);
if (itm) {
- itm->view = 0;
- itm->itemFlags &= ~ItemIsHeaderItem;
+ itm->view = nullptr;
+ itm->d->headerItem = false;
verticalHeaderItems[section] = 0;
}
return itm;
@@ -354,7 +317,7 @@ QModelIndex QTableModel::index(const QTableWidgetItem *item) const
return QModelIndex();
int i = -1;
const int id = item->d->id;
- if (id >= 0 && id < tableItems.count() && tableItems.at(id) == item) {
+ if (id >= 0 && id < tableItems.size() && tableItems.at(id) == item) {
i = id;
} else { // we need to search for the item
i = tableItems.indexOf(const_cast<QTableWidgetItem*>(item));
@@ -368,7 +331,7 @@ QModelIndex QTableModel::index(const QTableWidgetItem *item) const
void QTableModel::setRowCount(int rows)
{
- int rc = verticalHeaderItems.count();
+ int rc = verticalHeaderItems.size();
if (rows < 0 || rc == rows)
return;
if (rc < rows)
@@ -379,7 +342,7 @@ void QTableModel::setRowCount(int rows)
void QTableModel::setColumnCount(int columns)
{
- int cc = horizontalHeaderItems.count();
+ int cc = horizontalHeaderItems.size();
if (columns < 0 || cc == columns)
return;
if (cc < columns)
@@ -390,12 +353,12 @@ void QTableModel::setColumnCount(int columns)
int QTableModel::rowCount(const QModelIndex &parent) const
{
- return parent.isValid() ? 0 : verticalHeaderItems.count();
+ return parent.isValid() ? 0 : verticalHeaderItems.size();
}
int QTableModel::columnCount(const QModelIndex &parent) const
{
- return parent.isValid() ? 0 : horizontalHeaderItems.count();
+ return parent.isValid() ? 0 : horizontalHeaderItems.size();
}
QVariant QTableModel::data(const QModelIndex &index, int role) const
@@ -436,7 +399,7 @@ QMap<int, QVariant> QTableModel::itemData(const QModelIndex &index) const
QMap<int, QVariant> roles;
QTableWidgetItem *itm = item(index);
if (itm) {
- for (int i = 0; i < itm->values.count(); ++i) {
+ for (int i = 0; i < itm->values.size(); ++i) {
roles.insert(itm->values.at(i).role,
itm->values.at(i).value);
}
@@ -454,7 +417,7 @@ bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant
QTableWidgetItem *itm = item(index);
if (itm) {
itm->view = nullptr; // prohibits item from calling itemChanged()
- QVector<int> rolesVec;
+ QList<int> rolesVec;
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
const int role = (it.key() == Qt::EditRole ? Qt::DisplayRole : it.key());
if (itm->data(role) != it.value()) {
@@ -480,7 +443,6 @@ bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant
return true;
}
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool QTableModel::clearItemData(const QModelIndex &index)
{
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
@@ -493,10 +455,9 @@ bool QTableModel::clearItemData(const QModelIndex &index)
if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); }))
return true; //it's already cleared
itm->values.clear();
- emit dataChanged(index, index, QVector<int>{});
+ emit dataChanged(index, index, QList<int> {});
return true;
}
-#endif
Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
{
@@ -514,8 +475,8 @@ Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
void QTableModel::sort(int column, Qt::SortOrder order)
{
- QVector<QPair<QTableWidgetItem*, int> > sortable;
- QVector<int> unsortable;
+ QList<QPair<QTableWidgetItem *, int>> sortable;
+ QList<int> unsortable;
sortable.reserve(rowCount());
unsortable.reserve(rowCount());
@@ -530,7 +491,7 @@ void QTableModel::sort(int column, Qt::SortOrder order)
const auto compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
std::stable_sort(sortable.begin(), sortable.end(), compare);
- QVector<QTableWidgetItem*> sorted_table(tableItems.count());
+ QList<QTableWidgetItem *> sorted_table(tableItems.size());
QModelIndexList from;
QModelIndexList to;
const int numRows = rowCount();
@@ -538,9 +499,9 @@ void QTableModel::sort(int column, Qt::SortOrder order)
from.reserve(numRows * numColumns);
to.reserve(numRows * numColumns);
for (int i = 0; i < numRows; ++i) {
- int r = (i < sortable.count()
+ int r = (i < sortable.size()
? sortable.at(i).second
- : unsortable.at(i - sortable.count()));
+ : unsortable.at(i - sortable.size()));
for (int c = 0; c < numColumns; ++c) {
sorted_table[tableIndex(i, c)] = item(r, c);
from.append(createIndex(r, c));
@@ -567,11 +528,11 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order,
int start, int end)
{
int count = end - start + 1;
- QVector < QPair<QTableWidgetItem*,int> > sorting;
+ QList<QPair<QTableWidgetItem *, int>> sorting;
sorting.reserve(count);
for (int row = start; row <= end; ++row) {
QTableWidgetItem *itm = item(row, column);
- if (itm == 0) {
+ if (itm == nullptr) {
// no more sortable items (all 0-items are
// at the end of the table when it is sorted)
break;
@@ -582,16 +543,19 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order,
const auto compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
std::stable_sort(sorting.begin(), sorting.end(), compare);
QModelIndexList oldPersistentIndexes, newPersistentIndexes;
- QVector<QTableWidgetItem*> newTable = tableItems;
- QVector<QTableWidgetItem*> newVertical = verticalHeaderItems;
- QVector<QTableWidgetItem*> colItems = columnItems(column);
- QVector<QTableWidgetItem*>::iterator vit = colItems.begin();
+ QList<QTableWidgetItem *> newTable = tableItems;
+ QList<QTableWidgetItem *> newVertical = verticalHeaderItems;
+ QList<QTableWidgetItem *> colItems = columnItems(column);
+ QList<QTableWidgetItem *>::iterator vit = colItems.begin();
+ qsizetype distanceFromBegin = 0;
bool changed = false;
- for (int i = 0; i < sorting.count(); ++i) {
+ for (int i = 0; i < sorting.size(); ++i) {
+ distanceFromBegin = std::distance(colItems.begin(), vit);
int oldRow = sorting.at(i).second;
QTableWidgetItem *item = colItems.at(oldRow);
colItems.remove(oldRow);
- vit = sortedInsertionIterator(vit, colItems.end(), order, item);
+ vit = sortedInsertionIterator(colItems.begin() + distanceFromBegin, colItems.end(), order,
+ item);
int newRow = qMax((int)(vit - colItems.begin()), 0);
if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1)) && !(*colItems.at(oldRow - 1) < *item))
newRow = oldRow;
@@ -605,7 +569,7 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order,
}
// move the items @ oldRow to newRow
int cc = columnCount();
- QVector<QTableWidgetItem*> rowItems(cc);
+ QList<QTableWidgetItem *> rowItems(cc);
for (int j = 0; j < cc; ++j)
rowItems[j] = newTable.at(tableIndex(oldRow, j));
newTable.remove(tableIndex(oldRow, 0), cc);
@@ -618,7 +582,7 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order,
// update persistent indexes
updateRowIndexes(newPersistentIndexes, oldRow, newRow);
// the index of the remaining rows may have changed
- for (int j = i + 1; j < sorting.count(); ++j) {
+ for (int j = i + 1; j < sorting.size(); ++j) {
int otherRow = sorting.at(j).second;
if (oldRow < otherRow && newRow >= otherRow)
--sorting[j].second;
@@ -642,14 +606,14 @@ void QTableModel::ensureSorted(int column, Qt::SortOrder order,
Returns the non-0 items in column \a column.
*/
-QVector<QTableWidgetItem*> QTableModel::columnItems(int column) const
+QList<QTableWidgetItem *> QTableModel::columnItems(int column) const
{
- QVector<QTableWidgetItem*> items;
+ QList<QTableWidgetItem *> items;
int rc = rowCount();
items.reserve(rc);
for (int row = 0; row < rc; ++row) {
QTableWidgetItem *itm = item(row, column);
- if (itm == 0) {
+ if (itm == nullptr) {
// no more sortable items (all 0-items are
// at the end of the table when it is sorted)
break;
@@ -691,10 +655,10 @@ void QTableModel::updateRowIndexes(QModelIndexList &indexes,
inserted in the interval (\a begin, \a end) according to
the given sort \a order.
*/
-QVector<QTableWidgetItem*>::iterator QTableModel::sortedInsertionIterator(
- const QVector<QTableWidgetItem*>::iterator &begin,
- const QVector<QTableWidgetItem*>::iterator &end,
- Qt::SortOrder order, QTableWidgetItem *item)
+QList<QTableWidgetItem *>::iterator
+QTableModel::sortedInsertionIterator(const QList<QTableWidgetItem *>::iterator &begin,
+ const QList<QTableWidgetItem *>::iterator &end,
+ Qt::SortOrder order, QTableWidgetItem *item)
{
if (order == Qt::AscendingOrder)
return std::lower_bound(begin, end, item, QTableModelLessThan());
@@ -718,10 +682,10 @@ QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int r
if (section < 0)
return QVariant();
- QTableWidgetItem *itm = 0;
- if (orientation == Qt::Horizontal && section < horizontalHeaderItems.count())
+ QTableWidgetItem *itm = nullptr;
+ if (orientation == Qt::Horizontal && section < horizontalHeaderItems.size())
itm = horizontalHeaderItems.at(section);
- else if (orientation == Qt::Vertical && section < verticalHeaderItems.count())
+ else if (orientation == Qt::Vertical && section < verticalHeaderItems.size())
itm = verticalHeaderItems.at(section);
else
return QVariant(); // section is out of bounds
@@ -741,7 +705,7 @@ bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
(orientation == Qt::Vertical && verticalHeaderItems.size() <= section))
return false;
- QTableWidgetItem *itm = 0;
+ QTableWidgetItem *itm = nullptr;
if (orientation == Qt::Horizontal)
itm = horizontalHeaderItems.at(section);
else
@@ -756,22 +720,22 @@ bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
bool QTableModel::isValid(const QModelIndex &index) const
{
return (index.isValid()
- && index.row() < verticalHeaderItems.count()
- && index.column() < horizontalHeaderItems.count());
+ && index.row() < verticalHeaderItems.size()
+ && index.column() < horizontalHeaderItems.size());
}
void QTableModel::clear()
{
- for (int j = 0; j < verticalHeaderItems.count(); ++j) {
+ for (int j = 0; j < verticalHeaderItems.size(); ++j) {
if (verticalHeaderItems.at(j)) {
- verticalHeaderItems.at(j)->view = 0;
+ verticalHeaderItems.at(j)->view = nullptr;
delete verticalHeaderItems.at(j);
verticalHeaderItems[j] = 0;
}
}
- for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
+ for (int k = 0; k < horizontalHeaderItems.size(); ++k) {
if (horizontalHeaderItems.at(k)) {
- horizontalHeaderItems.at(k)->view = 0;
+ horizontalHeaderItems.at(k)->view = nullptr;
delete horizontalHeaderItems.at(k);
horizontalHeaderItems[k] = 0;
}
@@ -782,9 +746,9 @@ void QTableModel::clear()
void QTableModel::clearContents()
{
beginResetModel();
- for (int i = 0; i < tableItems.count(); ++i) {
+ for (int i = 0; i < tableItems.size(); ++i) {
if (tableItems.at(i)) {
- tableItems.at(i)->view = 0;
+ tableItems.at(i)->view = nullptr;
delete tableItems.at(i);
tableItems[i] = 0;
}
@@ -792,11 +756,11 @@ void QTableModel::clearContents()
endResetModel();
}
-void QTableModel::itemChanged(QTableWidgetItem *item, const QVector<int> &roles)
+void QTableModel::itemChanged(QTableWidgetItem *item, const QList<int> &roles)
{
if (!item)
return;
- if (item->flags() & ItemIsHeaderItem) {
+ if (item->d->headerItem) {
int row = verticalHeaderItems.indexOf(item);
if (row >= 0) {
emit headerDataChanged(Qt::Vertical, row, row);
@@ -844,7 +808,7 @@ QMimeData *QTableModel::internalMimeData() const
QMimeData *QTableModel::mimeData(const QModelIndexList &indexes) const
{
QList<QTableWidgetItem*> items;
- const int indexesCount = indexes.count();
+ const int indexesCount = indexes.size();
items.reserve(indexesCount);
for (int i = 0; i < indexesCount; ++i)
items << item(indexes.at(i));
@@ -853,7 +817,7 @@ QMimeData *QTableModel::mimeData(const QModelIndexList &indexes) const
// cachedIndexes is a little hack to avoid copying from QModelIndexList to
// QList<QTreeWidgetItem*> and back again in the view
cachedIndexes = indexes;
- QMimeData *mimeData = (view ? view->mimeData(items) : 0);
+ QMimeData *mimeData = (view ? view->mimeData(items) : nullptr);
cachedIndexes.clear();
return mimeData;
}
@@ -900,38 +864,36 @@ Qt::DropActions QTableModel::supportedDropActions() const
*/
/*!
- Constructs an table selection range, i.e. a range
+ \fn QTableWidgetSelectionRange::QTableWidgetSelectionRange()
+
+ Constructs an empty table selection range, i.e. a range
whose rowCount() and columnCount() are 0.
+
+ \sa topRow(), leftColumn(), bottomRow(), rightColumn()
*/
-QTableWidgetSelectionRange::QTableWidgetSelectionRange()
- : top(-1), left(-1), bottom(-2), right(-2)
-{
-}
/*!
+ \fn QTableWidgetSelectionRange::QTableWidgetSelectionRange(int top, int left, int bottom, int right)
+
Constructs the table selection range from the given \a top, \a
left, \a bottom and \a right table rows and columns.
\sa topRow(), leftColumn(), bottomRow(), rightColumn()
*/
-QTableWidgetSelectionRange::QTableWidgetSelectionRange(int top, int left, int bottom, int right)
- : top(top), left(left), bottom(bottom), right(right)
-{
-}
/*!
- Constructs a the table selection range by copying the given \a
- other table selection range.
+ \fn bool QTableWidgetSelectionRange::operator==(const QTableWidgetSelectionRange &lhs, const QTableWidgetSelectionRange &rhs)
+ \since 6.3
+
+ Returns true if \a lhs and \a rhs are equal, otherwise returns false.
*/
-QTableWidgetSelectionRange::QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other) = default;
-QTableWidgetSelectionRange &QTableWidgetSelectionRange::operator=(const QTableWidgetSelectionRange &other) = default;
/*!
- Destroys the table selection range.
+ \fn bool QTableWidgetSelectionRange::operator!=(const QTableWidgetSelectionRange &lhs, const QTableWidgetSelectionRange &rhs)
+ \since 6.3
+
+ Returns true if \a lhs and \a rhs are not equal, otherwise returns false.
*/
-QTableWidgetSelectionRange::~QTableWidgetSelectionRange()
-{
-}
/*!
\fn int QTableWidgetSelectionRange::topRow() const
@@ -1249,22 +1211,6 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
\sa font(), setText(), setForeground()
*/
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn QColor QTableWidgetItem::backgroundColor() const
- \obsolete
-
- This function is deprecated. Use background() instead.
-*/
-
-/*!
- \fn void QTableWidgetItem::setBackgroundColor(const QColor &color)
- \obsolete
-
- This function is deprecated. Use setBackground() instead.
-*/
-#endif
-
/*!
\fn QBrush QTableWidgetItem::background() const
\since 4.2
@@ -1285,22 +1231,6 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
\sa setForeground()
*/
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn QColor QTableWidgetItem::textColor() const
- \obsolete
-
- This function is deprecated. Use foreground() instead.
-*/
-
-/*!
- \fn void QTableWidgetItem::setTextColor(const QColor &color)
- \obsolete
-
- This function is deprecated. Use setForeground() instead.
-*/
-#endif
-
/*!
\fn QBrush QTableWidgetItem::foreground() const
\since 4.2
@@ -1322,14 +1252,29 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
*/
/*!
+ \if defined(qt7)
+
+ \fn Qt::Alignment QTableWidgetItem::textAlignment() const
+
+ Returns the text alignment for the list item.
+
+ \else
+
\fn int QTableWidgetItem::textAlignment() const
Returns the text alignment for the item's text.
+ \note This function returns an int for historical reasons. It will
+ be corrected to return Qt::Alignment in Qt 7.
+
\sa Qt::Alignment
+
+ \endif
*/
/*!
+ \obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
+
\fn void QTableWidgetItem::setTextAlignment(int alignment)
Sets the text alignment for the item's text to the \a alignment
@@ -1339,6 +1284,20 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
*/
/*!
+ \since 6.4
+
+ \fn void QTableWidgetItem::setTextAlignment(Qt::Alignment alignment)
+
+ Sets the text alignment for the item's text to the \a alignment
+ specified.
+*/
+
+/*!
+ \fn void QTableWidgetItem::setTextAlignment(Qt::AlignmentFlag alignment)
+ \internal
+*/
+
+/*!
Constructs a table item of the specified \a type that does not belong
to any table.
@@ -1420,7 +1379,7 @@ void QTableWidgetItem::setData(int role, const QVariant &value)
{
bool found = false;
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
- for (int i = 0; i < values.count(); ++i) {
+ for (int i = 0; i < values.size(); ++i) {
if (values.at(i).role == role) {
if (values[i].value == value)
return;
@@ -1434,9 +1393,9 @@ void QTableWidgetItem::setData(int role, const QVariant &value)
values.append(QWidgetItemData(role, value));
if (QTableModel *model = tableModel())
{
- const QVector<int> roles((role == Qt::DisplayRole) ?
- QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
- QVector<int>({role}));
+ const QList<int> roles((role == Qt::DisplayRole)
+ ? QList<int>({ Qt::DisplayRole, Qt::EditRole })
+ : QList<int>({ role }));
model->itemChanged(this, roles);
}
}
@@ -1632,28 +1591,41 @@ QTableWidgetItem &QTableWidgetItem::operator=(const QTableWidgetItem &other)
void QTableWidgetPrivate::setup()
{
Q_Q(QTableWidget);
- // view signals
- QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex)));
- QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex)));
- QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
- q, SLOT(_q_emitItemDoubleClicked(QModelIndex)));
- QObject::connect(q, SIGNAL(activated(QModelIndex)), q, SLOT(_q_emitItemActivated(QModelIndex)));
- QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex)));
- // model signals
- QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- q, SLOT(_q_emitItemChanged(QModelIndex)));
- // selection signals
- QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
- QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
- q, SIGNAL(itemSelectionChanged()));
- // sorting
- QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- q, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
- QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
-}
-
-void QTableWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
+ connections = {
+ // view signals
+ QObjectPrivate::connect(q, &QTableWidget::pressed,
+ this, &QTableWidgetPrivate::emitItemPressed),
+ QObjectPrivate::connect(q, &QTableWidget::clicked,
+ this, &QTableWidgetPrivate::emitItemClicked),
+ QObjectPrivate::connect(q, &QTableWidget::doubleClicked,
+ this, &QTableWidgetPrivate::emitItemDoubleClicked),
+ QObjectPrivate::connect(q, &QTableWidget::activated,
+ this, &QTableWidgetPrivate::emitItemActivated),
+ QObjectPrivate::connect(q, &QTableWidget::entered,
+ this, &QTableWidgetPrivate::emitItemEntered),
+ // model signals
+ QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged,
+ this, &QTableWidgetPrivate::emitItemChanged),
+ // selection signals
+ QObjectPrivate::connect(q->selectionModel(), &QItemSelectionModel::currentChanged,
+ this, &QTableWidgetPrivate::emitCurrentItemChanged),
+ QObject::connect(q->selectionModel(), &QItemSelectionModel::selectionChanged,
+ q, &QTableWidget::itemSelectionChanged),
+ // sorting
+ QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged,
+ this, &QTableWidgetPrivate::dataChanged),
+ QObjectPrivate::connect(model, &QAbstractItemModel::columnsRemoved,
+ this, &QTableWidgetPrivate::sort)
+ };
+}
+
+void QTableWidgetPrivate::clearConnections()
+{
+ for (const QMetaObject::Connection &connection : connections)
+ QObject::disconnect(connection);
+}
+
+void QTableWidgetPrivate::emitItemPressed(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
@@ -1661,7 +1633,7 @@ void QTableWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
emit q->cellPressed(index.row(), index.column());
}
-void QTableWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
+void QTableWidgetPrivate::emitItemClicked(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
@@ -1669,7 +1641,7 @@ void QTableWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
emit q->cellClicked(index.row(), index.column());
}
-void QTableWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
+void QTableWidgetPrivate::emitItemDoubleClicked(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
@@ -1677,7 +1649,7 @@ void QTableWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
emit q->cellDoubleClicked(index.row(), index.column());
}
-void QTableWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
+void QTableWidgetPrivate::emitItemActivated(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
@@ -1685,7 +1657,7 @@ void QTableWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
emit q->cellActivated(index.row(), index.column());
}
-void QTableWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
+void QTableWidgetPrivate::emitItemEntered(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
@@ -1693,7 +1665,7 @@ void QTableWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
emit q->cellEntered(index.row(), index.column());
}
-void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
+void QTableWidgetPrivate::emitItemChanged(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
@@ -1701,7 +1673,7 @@ void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
emit q->cellChanged(index.row(), index.column());
}
-void QTableWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
+void QTableWidgetPrivate::emitCurrentItemChanged(const QModelIndex &current,
const QModelIndex &previous)
{
Q_Q(QTableWidget);
@@ -1712,7 +1684,7 @@ void QTableWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column());
}
-void QTableWidgetPrivate::_q_sort()
+void QTableWidgetPrivate::sort()
{
if (sortingEnabled) {
int column = horizontalHeader->sortIndicatorSection();
@@ -1721,8 +1693,8 @@ void QTableWidgetPrivate::_q_sort()
}
}
-void QTableWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
- const QModelIndex &bottomRight)
+void QTableWidgetPrivate::dataChanged(const QModelIndex &topLeft,
+ const QModelIndex &bottomRight)
{
if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) {
int column = horizontalHeader->sortIndicatorSection();
@@ -1920,6 +1892,8 @@ QTableWidget::QTableWidget(int rows, int columns, QWidget *parent)
*/
QTableWidget::~QTableWidget()
{
+ Q_D(QTableWidget);
+ d->clearConnections();
}
/*!
@@ -2040,7 +2014,7 @@ QTableWidgetItem *QTableWidget::takeItem(int row, int column)
Q_D(QTableWidget);
QTableWidgetItem *item = d->tableModel()->takeItem(row, column);
if (item)
- item->view = 0;
+ item->view = nullptr;
return item;
}
@@ -2076,7 +2050,7 @@ QTableWidgetItem *QTableWidget::takeVerticalHeaderItem(int row)
Q_D(QTableWidget);
QTableWidgetItem *itm = d->tableModel()->takeVerticalHeaderItem(row);
if (itm)
- itm->view = 0;
+ itm->view = nullptr;
return itm;
}
@@ -2115,7 +2089,7 @@ QTableWidgetItem *QTableWidget::takeHorizontalHeaderItem(int column)
Q_D(QTableWidget);
QTableWidgetItem *itm = d->tableModel()->takeHorizontalHeaderItem(column);
if (itm)
- itm->view = 0;
+ itm->view = nullptr;
return itm;
}
@@ -2126,8 +2100,8 @@ void QTableWidget::setVerticalHeaderLabels(const QStringList &labels)
{
Q_D(QTableWidget);
QTableModel *model = d->tableModel();
- QTableWidgetItem *item = 0;
- for (int i = 0; i < model->rowCount() && i < labels.count(); ++i) {
+ QTableWidgetItem *item = nullptr;
+ for (int i = 0; i < model->rowCount() && i < labels.size(); ++i) {
item = model->verticalHeaderItem(i);
if (!item) {
item = model->createItem();
@@ -2144,8 +2118,8 @@ void QTableWidget::setHorizontalHeaderLabels(const QStringList &labels)
{
Q_D(QTableWidget);
QTableModel *model = d->tableModel();
- QTableWidgetItem *item = 0;
- for (int i = 0; i < model->columnCount() && i < labels.count(); ++i) {
+ QTableWidgetItem *item = nullptr;
+ for (int i = 0; i < model->columnCount() && i < labels.size(); ++i) {
item = model->horizontalHeaderItem(i);
if (!item) {
item = model->createItem();
@@ -2358,34 +2332,6 @@ void QTableWidget::setCellWidget(int row, int column, QWidget *widget)
QAbstractItemView::setIndexWidget(index, widget);
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- Returns \c true if the \a item is selected, otherwise returns \c false.
-
- \obsolete
-
- This function is deprecated. Use \l{QTableWidgetItem::isSelected()} instead.
-*/
-
-bool QTableWidget::isItemSelected(const QTableWidgetItem *item) const
-{
- return ((item && item->tableWidget() == this) ? item->isSelected() : false);
-}
-
-/*!
- Selects or deselects \a item depending on \a select.
-
- \obsolete
-
- This function is deprecated. Use \l{QTableWidgetItem::setSelected()} instead.
-*/
-void QTableWidget::setItemSelected(const QTableWidgetItem *item, bool select)
-{
- if (item && item->tableWidget() == this)
- const_cast<QTableWidgetItem*>(item)->setSelected(select);
-}
-#endif
-
/*!
Selects or deselects the \a range depending on \a select.
*/
@@ -2412,13 +2358,13 @@ QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
{
const QList<QItemSelectionRange> ranges = selectionModel()->selection();
QList<QTableWidgetSelectionRange> result;
- const int rangesCount = ranges.count();
+ const int rangesCount = ranges.size();
result.reserve(rangesCount);
for (int i = 0; i < rangesCount; ++i)
- result.append(QTableWidgetSelectionRange(ranges.at(i).top(),
- ranges.at(i).left(),
- ranges.at(i).bottom(),
- ranges.at(i).right()));
+ result.append({ranges.at(i).top(),
+ ranges.at(i).left(),
+ ranges.at(i).bottom(),
+ ranges.at(i).right()});
return result;
}
@@ -2642,11 +2588,7 @@ QStringList QTableWidget::mimeTypes() const
If the list of items is empty, \nullptr is returned rather than a
serialized empty list.
*/
-#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem *> &items) const
-#else
-QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem*> items) const
-#endif
{
Q_D(const QTableWidget);
@@ -2654,7 +2596,7 @@ QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem*> items) const
// if non empty, it's called from the model's own mimeData
if (cachedIndexes.isEmpty()) {
- cachedIndexes.reserve(items.count());
+ cachedIndexes.reserve(items.size());
for (QTableWidgetItem *item : items)
cachedIndexes << indexFromItem(item);
@@ -2725,18 +2667,6 @@ QModelIndex QTableWidget::indexFromItem(const QTableWidgetItem *item) const
return d->tableModel()->index(item);
}
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
-/*!
- \internal
- \obsolete
- \overload
-*/
-QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const
-{
- return indexFromItem(const_cast<const QTableWidgetItem *>(item));
-}
-#endif
-
/*!
Returns a pointer to the QTableWidgetItem associated with the given \a index.
*/
@@ -2770,7 +2700,8 @@ void QTableWidget::dropEvent(QDropEvent *event) {
QModelIndex topIndex;
int col = -1;
int row = -1;
- if (d->dropOn(event, &row, &col, &topIndex)) {
+ // check whether a subclass has already accepted the event, ie. moved the data
+ if (!event->isAccepted() && d->dropOn(event, &row, &col, &topIndex)) {
const QModelIndexList indexes = selectedIndexes();
int top = INT_MAX;
int left = INT_MAX;
@@ -2780,7 +2711,7 @@ void QTableWidget::dropEvent(QDropEvent *event) {
}
QList<QTableWidgetItem *> taken;
- const int indexesCount = indexes.count();
+ const int indexesCount = indexes.size();
taken.reserve(indexesCount);
for (const auto &index : indexes)
taken.append(takeItem(index.row(), index.column()));
@@ -2792,9 +2723,11 @@ void QTableWidget::dropEvent(QDropEvent *event) {
}
event->accept();
- // Don't want QAbstractItemView to delete it because it was "moved" we already did it
- event->setDropAction(Qt::CopyAction);
}
+ // either we or a subclass accepted the move event, so assume that the data was
+ // moved and that QAbstractItemView shouldn't remove the source when QDrag::exec returns
+ if (event->isAccepted())
+ d->dropEventMoved = true;
}
QTableView::dropEvent(event);
diff --git a/src/widgets/itemviews/qtablewidget.h b/src/widgets/itemviews/qtablewidget.h
index 70e2046400..303f4d5f5b 100644
--- a/src/widgets/itemviews/qtablewidget.h
+++ b/src/widgets/itemviews/qtablewidget.h
@@ -1,74 +1,44 @@
-/****************************************************************************
-**
-** 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
#ifndef QTABLEWIDGET_H
#define QTABLEWIDGET_H
-#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qtableview.h>
+#include <QtWidgets/qtwidgetsglobal.h>
+#include <QtCore/qlist.h>
#include <QtCore/qvariant.h>
-#include <QtCore/qvector.h>
QT_REQUIRE_CONFIG(tablewidget);
QT_BEGIN_NAMESPACE
-// ### Qt6 unexport the class, remove the user-defined special 3 and make it a literal type.
-class Q_WIDGETS_EXPORT QTableWidgetSelectionRange
+class QTableWidgetSelectionRange
{
public:
- QTableWidgetSelectionRange();
- QTableWidgetSelectionRange(int top, int left, int bottom, int right);
- ~QTableWidgetSelectionRange();
-
- QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other);
- QTableWidgetSelectionRange &operator=(const QTableWidgetSelectionRange &other);
-
- inline int topRow() const { return top; }
- inline int bottomRow() const { return bottom; }
- inline int leftColumn() const { return left; }
- inline int rightColumn() const { return right; }
- inline int rowCount() const { return bottom - top + 1; }
- inline int columnCount() const { return right - left + 1; }
-
+ QTableWidgetSelectionRange() = default;
+ QTableWidgetSelectionRange(int top, int left, int bottom, int right)
+ : m_top(top), m_left(left), m_bottom(bottom), m_right(right)
+ {}
+
+ friend bool operator==(const QTableWidgetSelectionRange &lhs,
+ const QTableWidgetSelectionRange &rhs) noexcept
+ {
+ return lhs.m_top == rhs.m_top && lhs.m_left == rhs.m_left && lhs.m_bottom == rhs.m_bottom
+ && lhs.m_right == rhs.m_right;
+ }
+ friend bool operator!=(const QTableWidgetSelectionRange &lhs,
+ const QTableWidgetSelectionRange &rhs) noexcept
+ { return !(lhs == rhs); }
+
+ inline int topRow() const { return m_top; }
+ inline int bottomRow() const { return m_bottom; }
+ inline int leftColumn() const { return m_left; }
+ inline int rightColumn() const { return m_right; }
+ inline int rowCount() const { return m_bottom - m_top + 1; }
+ inline int columnCount() const { return m_right - m_left + 1; }
private:
- int top, left, bottom, right;
+ int m_top = -1, m_left = -1, m_bottom = -2, m_right = -2;
};
class QTableWidget;
@@ -113,7 +83,7 @@ public:
{ return data(Qt::StatusTipRole).toString(); }
inline void setStatusTip(const QString &statusTip);
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
inline QString toolTip() const
{ return data(Qt::ToolTipRole).toString(); }
inline void setToolTip(const QString &toolTip);
@@ -129,41 +99,35 @@ public:
{ return qvariant_cast<QFont>(data(Qt::FontRole)); }
inline void setFont(const QFont &font);
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
inline int textAlignment() const
{ return data(Qt::TextAlignmentRole).toInt(); }
+#else
+ inline Qt::Alignment textAlignment() const
+ { return qvariant_cast<Qt::Alignment>(data(Qt::TextAlignmentRole)); }
+#endif
+#if QT_DEPRECATED_SINCE(6, 4)
+ QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
inline void setTextAlignment(int alignment)
{ setData(Qt::TextAlignmentRole, alignment); }
-
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTableWidgetItem::background() instead")
- inline QColor backgroundColor() const
- { return qvariant_cast<QColor>(data(Qt::BackgroundRole)); }
- QT_DEPRECATED_X ("Use QTableWidgetItem::setBackground() instead")
- inline void setBackgroundColor(const QColor &color)
- { setData(Qt::BackgroundRole, color); }
+ inline void setTextAlignment(Qt::AlignmentFlag alignment)
+ { setData(Qt::TextAlignmentRole, QVariant::fromValue(Qt::Alignment(alignment))); }
#endif
+ inline void setTextAlignment(Qt::Alignment alignment)
+ { setData(Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
inline QBrush background() const
{ return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }
inline void setBackground(const QBrush &brush)
{ setData(Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTableWidgetItem::foreground() instead")
- inline QColor textColor() const
- { return qvariant_cast<QColor>(data(Qt::ForegroundRole)); }
- QT_DEPRECATED_X ("Use QTableWidgetItem::setForeground() instead")
- inline void setTextColor(const QColor &color)
- { setData(Qt::ForegroundRole, color); }
-#endif
-
inline QBrush foreground() const
{ return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); }
inline void setForeground(const QBrush &brush)
{ setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
inline Qt::CheckState checkState() const
- { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }
+ { return qvariant_cast<Qt::CheckState>(data(Qt::CheckStateRole)); }
inline void setCheckState(Qt::CheckState state)
{ setData(Qt::CheckStateRole, state); }
@@ -190,7 +154,7 @@ private:
private:
int rtti;
- QVector<QWidgetItemData> values;
+ QList<QWidgetItemData> values;
QTableWidget *view;
QTableWidgetItemPrivate *d;
Qt::ItemFlags itemFlags;
@@ -205,7 +169,7 @@ inline void QTableWidgetItem::setIcon(const QIcon &aicon)
inline void QTableWidgetItem::setStatusTip(const QString &astatusTip)
{ setData(Qt::StatusTipRole, astatusTip); }
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
inline void QTableWidgetItem::setToolTip(const QString &atoolTip)
{ setData(Qt::ToolTipRole, atoolTip); }
#endif
@@ -249,6 +213,9 @@ public:
QTableWidgetItem *item(int row, int column) const;
void setItem(int row, int column, QTableWidgetItem *item);
QTableWidgetItem *takeItem(int row, int column);
+ QList<QTableWidgetItem*> items(const QMimeData *data) const;
+ QModelIndex indexFromItem(const QTableWidgetItem *item) const;
+ QTableWidgetItem *itemFromIndex(const QModelIndex &index) const;
QTableWidgetItem *verticalHeaderItem(int row) const;
void setVerticalHeaderItem(int row, QTableWidgetItem *item);
@@ -282,12 +249,6 @@ public:
void setCellWidget(int row, int column, QWidget *widget);
inline void removeCellWidget(int row, int column);
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTableWidgetItem::isSelected() instead")
- bool isItemSelected(const QTableWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QTableWidgetItem::setSelected() instead")
- void setItemSelected(const QTableWidgetItem *item, bool select);
-#endif
void setRangeSelected(const QTableWidgetSelectionRange &range, bool select);
QList<QTableWidgetSelectionRange> selectedRanges() const;
@@ -320,7 +281,6 @@ Q_SIGNALS:
void itemActivated(QTableWidgetItem *item);
void itemEntered(QTableWidgetItem *item);
- // ### Qt 6: add changed roles
void itemChanged(QTableWidgetItem *item);
void currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous);
@@ -339,27 +299,10 @@ Q_SIGNALS:
protected:
bool event(QEvent *e) override;
virtual QStringList mimeTypes() const;
-#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
virtual QMimeData *mimeData(const QList<QTableWidgetItem *> &items) const;
-#else
- virtual QMimeData *mimeData(const QList<QTableWidgetItem*> items) const;
-#endif
virtual bool dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action);
virtual Qt::DropActions supportedDropActions() const;
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
-public:
-#else
-protected:
-#endif
- QList<QTableWidgetItem*> items(const QMimeData *data) const;
-
- QModelIndex indexFromItem(const QTableWidgetItem *item) const;
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
- QModelIndex indexFromItem(QTableWidgetItem *item) const; // ### Qt 6: remove
-#endif
- QTableWidgetItem *itemFromIndex(const QModelIndex &index) const;
-
protected:
#if QT_CONFIG(draganddrop)
void dropEvent(QDropEvent *event) override;
@@ -369,16 +312,6 @@ private:
Q_DECLARE_PRIVATE(QTableWidget)
Q_DISABLE_COPY(QTableWidget)
-
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
- Q_PRIVATE_SLOT(d_func(), void _q_sort())
- Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
};
inline void QTableWidget::removeCellWidget(int arow, int acolumn)
diff --git a/src/widgets/itemviews/qtablewidget_p.h b/src/widgets/itemviews/qtablewidget_p.h
index d88326f129..210910fc52 100644
--- a/src/widgets/itemviews/qtablewidget_p.h
+++ b/src/widgets/itemviews/qtablewidget_p.h
@@ -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
#ifndef QTABLEWIDGET_P_H
#define QTABLEWIDGET_P_H
@@ -58,6 +22,8 @@
#include <private/qtableview_p.h>
#include <private/qwidgetitemdata_p.h>
+#include <array>
+
QT_REQUIRE_CONFIG(tablewidget);
QT_BEGIN_NAMESPACE
@@ -89,10 +55,6 @@ class QTableModel : public QAbstractTableModel
friend class QTableWidget;
public:
- enum ItemFlagsExtension {
- ItemIsHeaderItem = 128
- }; // we need this to separate header items from other items
-
QTableModel(int rows, int columns, QTableWidget *parent);
~QTableModel();
@@ -129,9 +91,7 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool clearItemData(const QModelIndex &index) override;
-#endif
QMap<int, QVariant> itemData(const QModelIndex &index) const override;
@@ -147,20 +107,20 @@ public:
const QPair<QTableWidgetItem*,int> &right);
void ensureSorted(int column, Qt::SortOrder order, int start, int end);
- QVector<QTableWidgetItem*> columnItems(int column) const;
+ QList<QTableWidgetItem *> columnItems(int column) const;
void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow);
- static QVector<QTableWidgetItem*>::iterator sortedInsertionIterator(
- const QVector<QTableWidgetItem*>::iterator &begin,
- const QVector<QTableWidgetItem*>::iterator &end,
- Qt::SortOrder order, QTableWidgetItem *item);
+ static QList<QTableWidgetItem *>::iterator
+ sortedInsertionIterator(const QList<QTableWidgetItem *>::iterator &begin,
+ const QList<QTableWidgetItem *>::iterator &end, Qt::SortOrder order,
+ QTableWidgetItem *item);
bool isValid(const QModelIndex &index) const;
inline long tableIndex(int row, int column) const
- { return (row * horizontalHeaderItems.count()) + column; }
+ { return (row * horizontalHeaderItems.size()) + column; }
void clear();
void clearContents();
- void itemChanged(QTableWidgetItem *item, const QVector<int> &roles = QVector<int>());
+ void itemChanged(QTableWidgetItem *item, const QList<int> &roles = QList<int>());
QTableWidgetItem *createItem() const;
const QTableWidgetItem *itemPrototype() const;
@@ -177,9 +137,9 @@ public:
private:
const QTableWidgetItem *prototype;
- QVector<QTableWidgetItem*> tableItems;
- QVector<QTableWidgetItem*> verticalHeaderItems;
- QVector<QTableWidgetItem*> horizontalHeaderItems;
+ QList<QTableWidgetItem *> tableItems;
+ QList<QTableWidgetItem *> verticalHeaderItems;
+ QList<QTableWidgetItem *> horizontalHeaderItems;
// A cache must be mutable if get-functions should have const modifiers
mutable QModelIndexList cachedIndexes;
@@ -192,28 +152,32 @@ public:
QTableWidgetPrivate() : QTableViewPrivate() {}
inline QTableModel *tableModel() const { return qobject_cast<QTableModel*>(model); }
void setup();
+ void clearConnections();
// view signals
- void _q_emitItemPressed(const QModelIndex &index);
- void _q_emitItemClicked(const QModelIndex &index);
- void _q_emitItemDoubleClicked(const QModelIndex &index);
- void _q_emitItemActivated(const QModelIndex &index);
- void _q_emitItemEntered(const QModelIndex &index);
+ void emitItemPressed(const QModelIndex &index);
+ void emitItemClicked(const QModelIndex &index);
+ void emitItemDoubleClicked(const QModelIndex &index);
+ void emitItemActivated(const QModelIndex &index);
+ void emitItemEntered(const QModelIndex &index);
// model signals
- void _q_emitItemChanged(const QModelIndex &index);
+ void emitItemChanged(const QModelIndex &index);
// selection signals
- void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current);
+ void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current);
// sorting
- void _q_sort();
- void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+ void sort();
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+
+ std::array<QMetaObject::Connection, 10> connections;
};
class QTableWidgetItemPrivate
{
public:
- QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1) {}
+ QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1), headerItem(false) {}
QTableWidgetItem *q;
int id;
+ bool headerItem; // Qt 7 TODO: inline this stuff in the public class.
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp
index 442369c2ec..744f29ca17 100644
--- a/src/widgets/itemviews/qtreeview.cpp
+++ b/src/widgets/itemviews/qtreeview.cpp
@@ -1,45 +1,9 @@
-/****************************************************************************
-**
-** 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 "qtreeview.h"
#include <qheaderview.h>
-#include <qitemdelegate.h>
+#include <qabstractitemdelegate.h>
#include <qapplication.h>
#include <qscrollbar.h>
#include <qpainter.h>
@@ -51,7 +15,7 @@
#include <qdebug.h>
#include <QMetaMethod>
#include <private/qscrollbar_p.h>
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
#include <qaccessible.h>
#endif
@@ -160,8 +124,7 @@ QT_BEGIN_NAMESPACE
that can be taken for views that are intended to display items with equal heights
is to set the \l uniformRowHeights property to true.
- \sa QListView, QTreeWidget, {View Classes}, QAbstractItemModel, QAbstractItemView,
- {Dir View Example}
+ \sa QListView, QTreeWidget, {View Classes}, QAbstractItemModel, QAbstractItemView
*/
@@ -206,6 +169,8 @@ QTreeView::QTreeView(QTreeViewPrivate &dd, QWidget *parent)
*/
QTreeView::~QTreeView()
{
+ Q_D(QTreeView);
+ d->clearConnections();
}
/*!
@@ -217,18 +182,12 @@ void QTreeView::setModel(QAbstractItemModel *model)
if (model == d->model)
return;
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
- disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(rowsRemoved(QModelIndex,int,int)));
-
- disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));
+ for (const QMetaObject::Connection &connection : d->modelConnections)
+ QObject::disconnect(connection);
}
if (d->selectionModel) { // support row editing
- disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
- d->model, SLOT(submit()));
- disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(rowsRemoved(QModelIndex,int,int)));
- disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));
+ QObject::disconnect(d->selectionmodelConnection);
}
d->viewItems.clear();
d->expandedIndexes.clear();
@@ -238,20 +197,24 @@ void QTreeView::setModel(QAbstractItemModel *model)
d->geometryRecursionBlock = false;
QAbstractItemView::setModel(model);
- // QAbstractItemView connects to a private slot
- disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
- // do header layout after the tree
- disconnect(d->model, SIGNAL(layoutChanged()),
- d->header, SLOT(_q_layoutChanged()));
- // QTreeView has a public slot for this
- connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
- this, SLOT(rowsRemoved(QModelIndex,int,int)));
-
- connect(d->model, SIGNAL(modelAboutToBeReset()), SLOT(_q_modelAboutToBeReset()));
-
+ if (d->model) {
+ // QAbstractItemView connects to a private slot
+ QObjectPrivate::disconnect(d->model, &QAbstractItemModel::rowsRemoved,
+ d, &QAbstractItemViewPrivate::rowsRemoved);
+ // do header layout after the tree
+ QObjectPrivate::disconnect(d->model, &QAbstractItemModel::layoutChanged,
+ d->header->d_func(), &QAbstractItemViewPrivate::layoutChanged);
+
+ d->modelConnections = {
+ // QTreeView has a public slot for this
+ QObject::connect(d->model, &QAbstractItemModel::rowsRemoved,
+ this, &QTreeView::rowsRemoved),
+ QObjectPrivate::connect(d->model, &QAbstractItemModel::modelAboutToBeReset,
+ d, &QTreeViewPrivate::modelAboutToBeReset)
+ };
+ }
if (d->sortingEnabled)
- d->_q_sortIndicatorChanged(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
+ d->sortIndicatorChanged(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
}
/*!
@@ -273,8 +236,7 @@ void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
Q_ASSERT(selectionModel);
if (d->selectionModel) {
// support row editing
- disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
- d->model, SLOT(submit()));
+ QObject::disconnect(d->selectionmodelConnection);
}
d->header->setSelectionModel(selectionModel);
@@ -282,8 +244,9 @@ void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
if (d->selectionModel) {
// support row editing
- connect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
- d->model, SLOT(submit()));
+ d->selectionmodelConnection =
+ connect(d->selectionModel, &QItemSelectionModel::currentRowChanged,
+ d->model, &QAbstractItemModel::submit);
}
}
@@ -323,16 +286,18 @@ void QTreeView::setHeader(QHeaderView *header)
d->header->setSelectionModel(d->selectionModel);
}
- connect(d->header, SIGNAL(sectionResized(int,int,int)),
- this, SLOT(columnResized(int,int,int)));
- connect(d->header, SIGNAL(sectionMoved(int,int,int)),
- this, SLOT(columnMoved()));
- connect(d->header, SIGNAL(sectionCountChanged(int,int)),
- this, SLOT(columnCountChanged(int,int)));
- connect(d->header, SIGNAL(sectionHandleDoubleClicked(int)),
- this, SLOT(resizeColumnToContents(int)));
- connect(d->header, SIGNAL(geometriesChanged()),
- this, SLOT(updateGeometries()));
+ d->headerConnections = {
+ connect(d->header, &QHeaderView::sectionResized,
+ this, &QTreeView::columnResized),
+ connect(d->header, &QHeaderView::sectionMoved,
+ this, &QTreeView::columnMoved),
+ connect(d->header, &QHeaderView::sectionCountChanged,
+ this, &QTreeView::columnCountChanged),
+ connect(d->header, &QHeaderView::sectionHandleDoubleClicked,
+ this, &QTreeView::resizeColumnToContents),
+ connect(d->header, &QHeaderView::geometriesChanged,
+ this, &QTreeView::updateGeometries)
+ };
setSortingEnabled(d->sortingEnabled);
d->updateGeometry();
@@ -616,7 +581,7 @@ void QTreeView::setRowHidden(int row, const QModelIndex &parent, bool hide)
if (hide) {
d->hiddenIndexes.insert(index);
- } else if(d->isPersistent(index)) { //if the index is not persistent, it cannot be in the set
+ } else if (d->isPersistent(index)) { //if the index is not persistent, it cannot be in the set
d->hiddenIndexes.remove(index);
}
@@ -674,7 +639,8 @@ void QTreeView::setFirstColumnSpanned(int row, const QModelIndex &parent, bool s
/*!
\reimp
*/
-void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
+void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles)
{
Q_D(QTreeView);
@@ -875,11 +841,12 @@ void QTreeView::setSortingEnabled(bool enable)
//sortByColumn has to be called before we connect or set the sortingEnabled flag
// because otherwise it will not call sort on the model.
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
- connect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
- this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)), Qt::UniqueConnection);
+ d->sortHeaderConnection =
+ QObjectPrivate::connect(header(), &QHeaderView::sortIndicatorChanged,
+ d, &QTreeViewPrivate::sortIndicatorChanged,
+ Qt::UniqueConnection);
} else {
- disconnect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
- this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)));
+ QObject::disconnect(d->sortHeaderConnection);
}
d->sortingEnabled = enable;
}
@@ -1037,9 +1004,9 @@ void QTreeView::keyboardSearch(const QString &search)
// special case for searches with same key like 'aaaaa'
bool sameKey = false;
- if (d->keyboardInput.length() > 1) {
- int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.length() - 1));
- sameKey = (c == d->keyboardInput.length());
+ if (d->keyboardInput.size() > 1) {
+ int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
+ sameKey = (c == d->keyboardInput.size());
if (sameKey)
skipRow = true;
}
@@ -1064,7 +1031,7 @@ void QTreeView::keyboardSearch(const QString &search)
int bestAbove = -1;
int bestBelow = -1;
QString searchString = sameKey ? QString(d->keyboardInput.at(0)) : d->keyboardInput;
- for (int i = 0; i < d->viewItems.count(); ++i) {
+ for (int i = 0; i < d->viewItems.size(); ++i) {
if ((int)d->viewItems.at(i).level > previousLevel) {
QModelIndex searchFrom = d->viewItems.at(i).index;
if (start.column() > 0)
@@ -1072,7 +1039,7 @@ void QTreeView::keyboardSearch(const QString &search)
if (searchFrom.parent() == start.parent())
searchFrom = start;
QModelIndexList match = d->model->match(searchFrom, Qt::DisplayRole, searchString);
- if (match.count()) {
+ if (match.size()) {
int hitIndex = d->viewIndex(match.at(0));
if (hitIndex >= 0 && hitIndex < startIndex)
bestAbove = bestAbove == -1 ? hitIndex : qMin(hitIndex, bestAbove);
@@ -1103,33 +1070,64 @@ void QTreeView::keyboardSearch(const QString &search)
QRect QTreeView::visualRect(const QModelIndex &index) const
{
Q_D(const QTreeView);
+ return d->visualRect(index, QTreeViewPrivate::SingleSection);
+}
- if (!d->isIndexValid(index) || isIndexHidden(index))
+/*!
+ \internal
+ \return the visual rectangle at \param index, according to \param rule.
+ \list
+ \li SingleSection
+ The return value matches the section, which \a index points to.
+ \li FullRow
+ Return the rectangle of the entire row, no matter which section
+ \a index points to.
+ \li AddRowIndicatorToFirstSection
+ Like SingleSection. If \index points to the first section, add the
+ row indicator and its margins.
+ \endlist
+ */
+QRect QTreeViewPrivate::visualRect(const QModelIndex &index, RectRule rule) const
+{
+ Q_Q(const QTreeView);
+
+ if (!isIndexValid(index))
return QRect();
- d->executePostedLayout();
+ // Calculate the entire row's rectangle, even if one of the elements is hidden
+ if (q->isIndexHidden(index) && rule != FullRow)
+ return QRect();
- int vi = d->viewIndex(index);
- if (vi < 0)
+ executePostedLayout();
+
+ const int viewIndex = this->viewIndex(index);
+ if (viewIndex < 0)
return QRect();
- bool spanning = d->viewItems.at(vi).spanning;
+ const bool spanning = viewItems.at(viewIndex).spanning;
+ const int column = index.column();
// if we have a spanning item, make the selection stretch from left to right
- int x = (spanning ? 0 : columnViewportPosition(index.column()));
- int w = (spanning ? d->header->length() : columnWidth(index.column()));
- // handle indentation
- if (d->isTreePosition(index.column())) {
- int i = d->indentationForItem(vi);
- w -= i;
- if (!isRightToLeft())
- x += i;
+ int x = (spanning ? 0 : q->columnViewportPosition(column));
+ int width = (spanning ? header->length() : q->columnWidth(column));
+
+ const bool addIndentation = isTreePosition(column) && (column > 0 || rule == SingleSection);
+
+ if (rule == FullRow) {
+ x = 0;
+ width = q->viewport()->width();
+ } else if (addIndentation) {
+ // calculate indentation
+ const int indentation = indentationForItem(viewIndex);
+ width -= indentation;
+ if (!q->isRightToLeft())
+ x += indentation;
}
- int y = d->coordinateForItem(vi);
- int h = d->itemHeight(vi);
+ const int y = coordinateForItem(viewIndex);
+ const int height = itemHeight(viewIndex);
- return QRect(x, y, w, h);
+ return QRect(x, y, width, height);
}
/*!
@@ -1239,6 +1237,22 @@ void QTreeView::scrollTo(const QModelIndex &index, ScrollHint hint)
/*!
\reimp
*/
+void QTreeView::changeEvent(QEvent *event)
+{
+ Q_D(QTreeView);
+ if (event->type() == QEvent::StyleChange) {
+ if (!d->customIndent) {
+ // QAbstractItemView calls this method in case of a style change,
+ // so update the indentation here if it wasn't set manually.
+ d->updateIndentationFromStyle();
+ }
+ }
+ QAbstractItemView::changeEvent(event);
+}
+
+/*!
+ \reimp
+*/
void QTreeView::timerEvent(QTimerEvent *event)
{
Q_D(QTreeView);
@@ -1296,16 +1310,13 @@ bool QTreeView::viewportEvent(QEvent *event)
case QEvent::HoverLeave:
case QEvent::HoverMove: {
QHoverEvent *he = static_cast<QHoverEvent*>(event);
- int oldBranch = d->hoverBranch;
- d->hoverBranch = d->itemDecorationAt(he->pos());
- QModelIndex newIndex = indexAt(he->pos());
+ const int oldBranch = d->hoverBranch;
+ d->hoverBranch = d->itemDecorationAt(he->position().toPoint());
+ QModelIndex newIndex = indexAt(he->position().toPoint());
if (d->hover != newIndex || d->hoverBranch != oldBranch) {
// Update the whole hovered over row. No need to update the old hovered
// row, that is taken care in superclass hover handling.
- QRect rect = visualRect(newIndex);
- rect.setX(0);
- rect.setWidth(viewport()->width());
- viewport()->update(rect);
+ viewport()->update(d->visualRect(newIndex, QTreeViewPrivate::FullRow));
}
break; }
default:
@@ -1388,12 +1399,28 @@ bool QTreeViewPrivate::expandOrCollapseItemAtPos(const QPoint &pos)
return false;
}
-void QTreeViewPrivate::_q_modelDestroyed()
+void QTreeViewPrivate::modelDestroyed()
{
//we need to clear the viewItems because it contains QModelIndexes to
//the model currently being destroyed
viewItems.clear();
- QAbstractItemViewPrivate::_q_modelDestroyed();
+ QAbstractItemViewPrivate::modelDestroyed();
+}
+
+QRect QTreeViewPrivate::intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const
+{
+ const auto parentIdx = topLeft.parent();
+ executePostedLayout();
+ QRect updateRect;
+ for (int r = topLeft.row(); r <= bottomRight.row(); ++r) {
+ if (isRowHidden(model->index(r, 0, parentIdx)))
+ continue;
+ for (int c = topLeft.column(); c <= bottomRight.column(); ++c) {
+ const QModelIndex idx(model->index(r, c, parentIdx));
+ updateRect |= visualRect(idx, SingleSection);
+ }
+ }
+ return rect.intersected(updateRect);
}
/*!
@@ -1426,8 +1453,11 @@ void QTreeViewPrivate::adjustViewOptionsForIndex(QStyleOptionViewItem *option, c
option->showDecorationSelected = (selectionBehavior & QTreeView::SelectRows)
|| option->showDecorationSelected;
- QVector<int> logicalIndices; // index = visual index of visible columns only. data = logical index.
- QVector<QStyleOptionViewItem::ViewItemPosition> viewItemPosList; // vector of left/middle/end for each logicalIndex, visible columns only.
+ QList<int>
+ logicalIndices; // index = visual index of visible columns only. data = logical index.
+ QList<QStyleOptionViewItem::ViewItemPosition>
+ viewItemPosList; // vector of left/middle/end for each logicalIndex, visible columns
+ // only.
const bool spanning = viewItems.at(row).spanning;
const int left = (spanning ? header->visualIndex(0) : 0);
const int right = (spanning ? header->visualIndex(0) : header->count() - 1 );
@@ -1448,13 +1478,15 @@ void QTreeViewPrivate::adjustViewOptionsForIndex(QStyleOptionViewItem *option, c
void QTreeView::drawTree(QPainter *painter, const QRegion &region) const
{
Q_D(const QTreeView);
- const QVector<QTreeViewItem> viewItems = d->viewItems;
+ // d->viewItems changes when posted layouts are executed in itemDecorationAt, so don't copy
+ const QList<QTreeViewItem> &viewItems = d->viewItems;
- QStyleOptionViewItem option = d->viewOptionsV1();
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
const QStyle::State state = option.state;
d->current = 0;
- if (viewItems.count() == 0 || d->header->count() == 0 || !d->itemDelegate) {
+ if (viewItems.size() == 0 || d->header->count() == 0 || !d->itemDelegate) {
d->paintAlternatingRowColors(painter, &option, 0, region.boundingRect().bottom()+1);
return;
}
@@ -1471,7 +1503,7 @@ void QTreeView::drawTree(QPainter *painter, const QRegion &region) const
QPoint hoverPos = d->viewport->mapFromGlobal(QCursor::pos());
d->hoverBranch = d->itemDecorationAt(hoverPos);
- QVector<int> drawn;
+ QList<int> drawn;
bool multipleRects = (region.rectCount() > 1);
for (const QRect &a : region) {
const QRect area = (multipleRects
@@ -1483,7 +1515,7 @@ void QTreeView::drawTree(QPainter *painter, const QRegion &region) const
int y = firstVisibleItemOffset; // we may only see part of the first item
// start at the top of the viewport and iterate down to the update area
- for (; i < viewItems.count(); ++i) {
+ for (; i < viewItems.size(); ++i) {
const int itemHeight = d->itemHeight(i);
if (y + itemHeight > area.top())
break;
@@ -1491,9 +1523,10 @@ void QTreeView::drawTree(QPainter *painter, const QRegion &region) const
}
// paint the visible rows
- for (; i < viewItems.count() && y <= area.bottom(); ++i) {
+ for (; i < viewItems.size() && y <= area.bottom(); ++i) {
+ const QModelIndex &index = viewItems.at(i).index;
const int itemHeight = d->itemHeight(i);
- option.rect.setRect(0, y, viewportWidth, itemHeight);
+ option.rect = d->visualRect(index, QTreeViewPrivate::FullRow);
option.state = state | (viewItems.at(i).expanded ? QStyle::State_Open : QStyle::State_None)
| (viewItems.at(i).hasChildren ? QStyle::State_Children : QStyle::State_None)
| (viewItems.at(i).hasMoreSiblings ? QStyle::State_Sibling : QStyle::State_None);
@@ -1517,14 +1550,16 @@ void QTreeView::drawTree(QPainter *painter, const QRegion &region) const
/// ### move to QObject :)
static inline bool ancestorOf(QObject *widget, QObject *other)
{
- for (QObject *parent = other; parent != 0; parent = parent->parent()) {
+ for (QObject *parent = other; parent != nullptr; parent = parent->parent()) {
if (parent == widget)
return true;
}
return false;
}
-void QTreeViewPrivate::calcLogicalIndices(QVector<int> *logicalIndices, QVector<QStyleOptionViewItem::ViewItemPosition> *itemPositions, int left, int right) const
+void QTreeViewPrivate::calcLogicalIndices(
+ QList<int> *logicalIndices, QList<QStyleOptionViewItem::ViewItemPosition> *itemPositions,
+ int left, int right) const
{
const int columnCount = header->count();
/* 'left' and 'right' are the left-most and right-most visible visual indices.
@@ -1550,11 +1585,11 @@ void QTreeViewPrivate::calcLogicalIndices(QVector<int> *logicalIndices, QVector<
}
}
- itemPositions->resize(logicalIndices->count());
- for (int currentLogicalSection = 0; currentLogicalSection < logicalIndices->count(); ++currentLogicalSection) {
+ itemPositions->resize(logicalIndices->size());
+ for (int currentLogicalSection = 0; currentLogicalSection < logicalIndices->size(); ++currentLogicalSection) {
const int headerSection = logicalIndices->at(currentLogicalSection);
// determine the viewItemPosition depending on the position of column 0
- int nextLogicalSection = currentLogicalSection + 1 >= logicalIndices->count()
+ int nextLogicalSection = currentLogicalSection + 1 >= logicalIndices->size()
? logicalIndexAfterRight
: logicalIndices->at(currentLogicalSection + 1);
int prevLogicalSection = currentLogicalSection - 1 < 0
@@ -1580,6 +1615,7 @@ void QTreeViewPrivate::calcLogicalIndices(QVector<int> *logicalIndices, QVector<
*/
int QTreeViewPrivate::widthHintForIndex(const QModelIndex &index, int hint, const QStyleOptionViewItem &option, int i) const
{
+ Q_Q(const QTreeView);
QWidget *editor = editorForIndex(index).widget.data();
if (editor && persistent.contains(editor)) {
hint = qMax(hint, editor->sizeHint().width());
@@ -1587,7 +1623,7 @@ int QTreeViewPrivate::widthHintForIndex(const QModelIndex &index, int hint, cons
int max = editor->maximumSize().width();
hint = qBound(min, hint, max);
}
- int xhint = delegateForIndex(index)->sizeHint(option, index).width();
+ int xhint = q->itemDelegateForIndex(index)->sizeHint(option, index).width();
hint = qMax(hint, xhint + (isTreePosition(index.column()) ? indentationForItem(i) : 0));
return hint;
}
@@ -1652,8 +1688,11 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
}
}
- // ### special case: treeviews with multiple columns draw
- // the selections differently than with only one column
+ // ### special case: if we select entire rows, then we need to draw the
+ // selection in the first column all the way to the second column, rather
+ // than just around the item text. We abuse showDecorationSelected to
+ // indicate this to the style. Below we will reset this value temporarily
+ // to only respect the styleHint while we are rendering the decoration.
opt.showDecorationSelected = (d->selectionBehavior & SelectRows)
|| option.showDecorationSelected;
@@ -1664,11 +1703,12 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
&& index.parent() == hover.parent()
&& index.row() == hover.row();
- QVector<int> logicalIndices;
- QVector<QStyleOptionViewItem::ViewItemPosition> viewItemPosList; // vector of left/middle/end for each logicalIndex
+ QList<int> logicalIndices;
+ QList<QStyleOptionViewItem::ViewItemPosition>
+ viewItemPosList; // vector of left/middle/end for each logicalIndex
d->calcLogicalIndices(&logicalIndices, &viewItemPosList, left, right);
- for (int currentLogicalSection = 0; currentLogicalSection < logicalIndices.count(); ++currentLogicalSection) {
+ for (int currentLogicalSection = 0; currentLogicalSection < logicalIndices.size(); ++currentLogicalSection) {
int headerSection = logicalIndices.at(currentLogicalSection);
position = columnViewportPosition(headerSection) + offset.x();
width = header->sectionSize(headerSection);
@@ -1725,7 +1765,7 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
/* Prior to Qt 4.3, the background of the branch (in selected state and
alternate row color was provided by the view. For backward compatibility,
- this is now delegated to the style using PE_PanelViewItemRow which
+ this is now delegated to the style using PE_PanelItemViewRow which
does the appropriate fill */
if (d->isTreePosition(headerSection)) {
const int i = d->indentationForItem(d->current);
@@ -1737,8 +1777,16 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
}
// draw background for the branch (selection + alternate row)
opt.rect = branches;
- if (style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, &opt, this))
- style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);
+
+ // We use showDecorationSelected both to store the style hint, and to indicate
+ // that the entire row has to be selected (see overrides of the value if
+ // selectionBehavior == SelectRow).
+ // While we are only painting the background we don't care for the
+ // selectionBehavior factor, so respect only the style value, and reset later.
+ const bool oldShowDecorationSelected = opt.showDecorationSelected;
+ opt.showDecorationSelected = style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected,
+ &opt, this);
+ style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);
// draw background of the item (only alternate row). rest of the background
// is provided by the delegate
@@ -1747,6 +1795,7 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
opt.rect.setRect(reverse ? position : i + position, y, width - i, height);
style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this);
opt.state = oldState;
+ opt.showDecorationSelected = oldShowDecorationSelected;
if (d->indent != 0)
drawBranches(painter, branches, index);
@@ -1760,7 +1809,7 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
opt.state = oldState;
}
- d->delegateForIndex(modelIndex)->paint(painter, opt, modelIndex);
+ itemDelegateForIndex(modelIndex)->paint(painter, opt, modelIndex);
}
if (currentRowHasFocus) {
@@ -1809,7 +1858,8 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect,
QModelIndex current = parent;
QModelIndex ancestor = current.parent();
- QStyleOptionViewItem opt = viewOptions();
+ QStyleOptionViewItem opt;
+ initViewItemOption(&opt);
QStyle::State extraFlags = QStyle::State_None;
if (isEnabled())
extraFlags |= QStyle::State_Enabled;
@@ -1888,9 +1938,9 @@ void QTreeView::mousePressEvent(QMouseEvent *event)
{
Q_D(QTreeView);
bool handled = false;
- if (style()->styleHint(QStyle::SH_ListViewExpand_SelectMouseType, 0, this) == QEvent::MouseButtonPress)
- handled = d->expandOrCollapseItemAtPos(event->pos());
- if (!handled && d->itemDecorationAt(event->pos()) == -1)
+ if (style()->styleHint(QStyle::SH_ListViewExpand_SelectMouseType, nullptr, this) == QEvent::MouseButtonPress)
+ handled = d->expandOrCollapseItemAtPos(event->position().toPoint());
+ if (!handled && d->itemDecorationAt(event->position().toPoint()) == -1)
QAbstractItemView::mousePressEvent(event);
else
d->pressedIndex = QModelIndex();
@@ -1902,13 +1952,13 @@ void QTreeView::mousePressEvent(QMouseEvent *event)
void QTreeView::mouseReleaseEvent(QMouseEvent *event)
{
Q_D(QTreeView);
- if (d->itemDecorationAt(event->pos()) == -1) {
+ if (d->itemDecorationAt(event->position().toPoint()) == -1) {
QAbstractItemView::mouseReleaseEvent(event);
} else {
if (state() == QAbstractItemView::DragSelectingState || state() == QAbstractItemView::DraggingState)
setState(QAbstractItemView::NoState);
- if (style()->styleHint(QStyle::SH_ListViewExpand_SelectMouseType, 0, this) == QEvent::MouseButtonRelease)
- d->expandOrCollapseItemAtPos(event->pos());
+ if (style()->styleHint(QStyle::SH_ListViewExpand_SelectMouseType, nullptr, this) == QEvent::MouseButtonRelease)
+ d->expandOrCollapseItemAtPos(event->position().toPoint());
}
}
@@ -1918,17 +1968,17 @@ void QTreeView::mouseReleaseEvent(QMouseEvent *event)
void QTreeView::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_D(QTreeView);
- if (state() != NoState || !d->viewport->rect().contains(event->pos()))
+ if (state() != NoState || !d->viewport->rect().contains(event->position().toPoint()))
return;
- int i = d->itemDecorationAt(event->pos());
+ int i = d->itemDecorationAt(event->position().toPoint());
if (i == -1) {
- i = d->itemAtCoordinate(event->y());
+ i = d->itemAtCoordinate(event->position().toPoint().y());
if (i == -1)
return; // user clicked outside the items
const QPersistentModelIndex firstColumnIndex = d->viewItems.at(i).index;
- const QPersistentModelIndex persistent = indexAt(event->pos());
+ const QPersistentModelIndex persistent = indexAt(event->position().toPoint());
if (d->pressedIndex != persistent) {
mousePressEvent(event);
@@ -1944,21 +1994,21 @@ void QTreeView::mouseDoubleClickEvent(QMouseEvent *event)
if (edit(persistent, DoubleClicked, event) || state() != NoState)
return; // the double click triggered editing
- if (!style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, this))
+ if (!style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, nullptr, this))
emit activated(persistent);
- d->pressedIndex = QModelIndex();
+ d->releaseFromDoubleClick = true;
d->executePostedLayout(); // we need to make sure viewItems is updated
if (d->itemsExpandable
&& d->expandsOnDoubleClick
&& d->hasVisibleChildren(persistent)) {
- if (!((i < d->viewItems.count()) && (d->viewItems.at(i).index == firstColumnIndex))) {
+ if (!((i < d->viewItems.size()) && (d->viewItems.at(i).index == firstColumnIndex))) {
// find the new index of the item
- for (i = 0; i < d->viewItems.count(); ++i) {
+ for (i = 0; i < d->viewItems.size(); ++i) {
if (d->viewItems.at(i).index == firstColumnIndex)
break;
}
- if (i == d->viewItems.count())
+ if (i == d->viewItems.size())
return;
}
if (d->viewItems.at(i).expanded)
@@ -1977,7 +2027,7 @@ void QTreeView::mouseDoubleClickEvent(QMouseEvent *event)
void QTreeView::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QTreeView);
- if (d->itemDecorationAt(event->pos()) == -1) // ### what about expanding/collapsing state ?
+ if (d->itemDecorationAt(event->position().toPoint()) == -1) // ### what about expanding/collapsing state ?
QAbstractItemView::mouseMoveEvent(event);
}
@@ -2056,7 +2106,7 @@ QModelIndex QTreeView::indexBelow(const QModelIndex &index) const
return QModelIndex();
d->executePostedLayout();
int i = d->viewIndex(index);
- if (++i >= d->viewItems.count())
+ if (++i >= d->viewItems.size())
return QModelIndex();
const QModelIndex firstColumnIndex = d->viewItems.at(i).index;
return firstColumnIndex.sibling(firstColumnIndex.row(), index.column());
@@ -2070,12 +2120,6 @@ QModelIndex QTreeView::indexBelow(const QModelIndex &index) const
void QTreeView::doItemsLayout()
{
Q_D(QTreeView);
- if (!d->customIndent) {
- // ### Qt 6: move to event()
- // QAbstractItemView calls this method in case of a style change,
- // so update the indentation here if it wasn't set manually.
- d->updateIndentationFromStyle();
- }
if (d->hasRemovedItems) {
//clean the QSet that may contains old (and this invalid) indexes
d->hasRemovedItems = false;
@@ -2101,6 +2145,7 @@ void QTreeView::doItemsLayout()
}
QAbstractItemView::doItemsLayout();
d->header->doItemsLayout();
+ d->updateAccessibility();
}
/*!
@@ -2146,7 +2191,7 @@ int QTreeView::verticalOffset() const
// ### find a faster way to do this
d->executePostedLayout();
int offset = 0;
- const int cnt = std::min(d->viewItems.count(), verticalScrollBar()->value());
+ const int cnt = qMin(d->viewItems.size(), verticalScrollBar()->value());
for (int i = 0; i < cnt; ++i)
offset += d->itemHeight(i);
return offset;
@@ -2172,14 +2217,13 @@ QModelIndex QTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie
int c = 0;
while (c < d->header->count() && d->header->isSectionHidden(d->header->logicalIndex(c)))
++c;
- if (i < d->viewItems.count() && c < d->header->count()) {
+ if (i < d->viewItems.size() && c < d->header->count()) {
return d->modelIndex(i, d->header->logicalIndex(c));
}
return QModelIndex();
}
- int vi = -1;
- if (vi < 0)
- vi = qMax(0, d->viewIndex(current));
+
+ const int vi = qMax(0, d->viewIndex(current));
if (isRightToLeft()) {
if (cursorAction == MoveRight)
@@ -2204,11 +2248,11 @@ QModelIndex QTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie
return d->modelIndex(d->above(vi), current.column());
case MoveLeft: {
QScrollBar *sb = horizontalScrollBar();
- if (vi < d->viewItems.count() && d->viewItems.at(vi).expanded && d->itemsExpandable && sb->value() == sb->minimum()) {
+ if (vi < d->viewItems.size() && d->viewItems.at(vi).expanded && d->itemsExpandable && sb->value() == sb->minimum()) {
d->collapse(vi, true);
d->moveCursorUpdatedView = true;
} else {
- bool descend = style()->styleHint(QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren, 0, this);
+ bool descend = style()->styleHint(QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren, nullptr, this);
if (descend) {
QModelIndex par = current.parent();
if (par.isValid() && par != rootIndex())
@@ -2239,12 +2283,12 @@ QModelIndex QTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie
break;
}
case MoveRight:
- if (vi < d->viewItems.count() && !d->viewItems.at(vi).expanded && d->itemsExpandable
+ if (vi < d->viewItems.size() && !d->viewItems.at(vi).expanded && d->itemsExpandable
&& d->hasVisibleChildren(d->viewItems.at(vi).index)) {
d->expand(vi, true);
d->moveCursorUpdatedView = true;
} else {
- bool descend = style()->styleHint(QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren, 0, this);
+ bool descend = style()->styleHint(QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren, nullptr, this);
if (descend) {
QModelIndex idx = d->modelIndex(d->below(vi));
if (idx.parent() == current)
@@ -2353,7 +2397,7 @@ QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) con
}
if (!leftIndex.isValid())
continue;
- const QRect leftRect = visualRect(leftIndex);
+ const QRect leftRect = d->visualRect(leftIndex, QTreeViewPrivate::SingleSection);
int top = leftRect.top();
QModelIndex rightIndex = range.bottomRight();
while (rightIndex.isValid() && isIndexHidden(rightIndex)) {
@@ -2364,7 +2408,7 @@ QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) con
}
if (!rightIndex.isValid())
continue;
- const QRect rightRect = visualRect(rightIndex);
+ const QRect rightRect = d->visualRect(rightIndex, QTreeViewPrivate::SingleSection);
int bottom = rightRect.bottom();
if (top > bottom)
qSwap<int>(top, bottom);
@@ -2394,7 +2438,7 @@ QModelIndexList QTreeView::selectedIndexes() const
QModelIndexList modelSelected;
if (selectionModel())
modelSelected = selectionModel()->selectedIndexes();
- for (int i = 0; i < modelSelected.count(); ++i) {
+ for (int i = 0; i < modelSelected.size(); ++i) {
// check that neither the parents nor the index is hidden before we add
QModelIndex index = modelSelected.at(i);
while (index.isValid() && !isIndexHidden(index))
@@ -2431,7 +2475,7 @@ void QTreeView::scrollContentsBy(int dx, int dy)
// guestimate the number of items in the viewport
int viewCount = d->viewport->height() / itemHeight;
- int maxDeltaY = qMin(d->viewItems.count(), viewCount);
+ int maxDeltaY = qMin(d->viewItems.size(), viewCount);
// no need to do a lot of work if we are going to redraw the whole thing anyway
if (qAbs(dy) > qAbs(maxDeltaY) && d->editorIndexHash.isEmpty()) {
verticalScrollBar()->update();
@@ -2447,12 +2491,12 @@ void QTreeView::scrollContentsBy(int dx, int dy)
dy = 0;
if (previousViewIndex < currentViewIndex) { // scrolling down
for (int i = previousViewIndex; i < currentViewIndex; ++i) {
- if (i < d->viewItems.count())
+ if (i < d->viewItems.size())
dy -= d->itemHeight(i);
}
} else if (previousViewIndex > currentViewIndex) { // scrolling up
for (int i = previousViewIndex - 1; i >= currentViewIndex; --i) {
- if (i < d->viewItems.count())
+ if (i < d->viewItems.size())
dy += d->itemHeight(i);
}
}
@@ -2540,7 +2584,7 @@ void QTreeView::rowsRemoved(const QModelIndex &parent, int start, int end)
d->viewItems.clear();
d->doDelayedItemsLayout();
d->hasRemovedItems = true;
- d->_q_rowsRemoved(parent, start, end);
+ d->rowsRemoved(parent, start, end);
}
/*!
@@ -2576,22 +2620,6 @@ void QTreeView::resizeColumnToContents(int column)
d->header->resizeSection(column, qMax(contents, header));
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \obsolete
- \overload
-
- This function is deprecated. Use
- sortByColumn(int column, Qt::SortOrder order) instead.
- Sorts the model by the values in the given \a column.
-*/
-void QTreeView::sortByColumn(int column)
-{
- Q_D(QTreeView);
- sortByColumn(column, d->header->sortIndicatorOrder());
-}
-#endif
-
/*!
\since 4.2
@@ -2608,11 +2636,11 @@ void QTreeView::sortByColumn(int column, Qt::SortOrder order)
Q_D(QTreeView);
if (column < -1)
return;
- // If sorting is enabled it will emit a signal connected to
- // _q_sortIndicatorChanged, which then actually sorts
d->header->setSortIndicator(column, order);
- // If sorting is not enabled, force to sort now
- if (!d->sortingEnabled)
+ // If sorting is not enabled or has the same order as before, force to sort now
+ // else sorting will be trigger through sortIndicatorChanged()
+ if (!d->sortingEnabled ||
+ (d->header->sortIndicatorSection() == column && d->header->sortIndicatorOrder() == order))
d->model->sort(column, order);
}
@@ -2647,7 +2675,8 @@ QSize QTreeView::viewportSizeHint() const
return QAbstractItemView::viewportSizeHint();
// Get rect for last item
- const QRect deepestRect = visualRect(d->viewItems.last().index);
+ const QRect deepestRect = d->visualRect(d->viewItems.last().index,
+ QTreeViewPrivate::SingleSection);
if (!deepestRect.isValid())
return QAbstractItemView::viewportSizeHint();
@@ -2664,7 +2693,10 @@ QSize QTreeView::viewportSizeHint() const
\since 4.2
Expands all expandable items.
- \warning: if the model contains a large number of items,
+ \note This function will not try to \l{QAbstractItemModel::fetchMore}{fetch more}
+ data.
+
+ \warning If the model contains a large number of items,
this function will take some time to execute.
\sa collapseAll(), expand(), collapse(), setExpanded()
@@ -2677,6 +2709,7 @@ void QTreeView::expandAll()
d->layout(-1, true);
updateGeometries();
d->viewport->update();
+ d->updateAccessibility();
}
/*!
@@ -2686,7 +2719,10 @@ void QTreeView::expandAll()
A \a depth of -1 will expand all children, a \a depth of 0 will
only expand the given \a index.
- \warning: if the model contains a large number of items,
+ \note This function will not try to \l{QAbstractItemModel::fetchMore}{fetch more}
+ data.
+
+ \warning If the model contains a large number of items,
this function will take some time to execute.
\sa expandAll()
@@ -2751,6 +2787,9 @@ void QTreeView::collapseAll()
\since 4.3
Expands all expandable items to the given \a depth.
+ \note This function will not try to \l{QAbstractItemModel::fetchMore}{fetch more}
+ data.
+
\sa expandAll(), collapseAll(), expand(), collapse(), setExpanded()
*/
void QTreeView::expandToDepth(int depth)
@@ -2762,7 +2801,7 @@ void QTreeView::expandToDepth(int depth)
d->expandedIndexes.clear();
d->interruptDelayedItemsLayout();
d->layout(-1);
- for (int i = 0; i < d->viewItems.count(); ++i) {
+ for (int i = 0; i < d->viewItems.size(); ++i) {
if (d->viewItems.at(i).level <= (uint)depth) {
d->viewItems[i].expanded = true;
d->layout(i);
@@ -2794,6 +2833,7 @@ void QTreeView::expandToDepth(int depth)
updateGeometries();
d->viewport->update();
+ d->updateAccessibility();
}
/*!
@@ -2859,8 +2899,9 @@ int QTreeView::sizeHintForColumn(int column) const
return -1;
ensurePolished();
int w = 0;
- QStyleOptionViewItem option = d->viewOptionsV1();
- const QVector<QTreeViewItem> viewItems = d->viewItems;
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+ const QList<QTreeViewItem> viewItems = d->viewItems;
const int maximumProcessRows = d->header->resizeContentsPrecision(); // To avoid this to take forever.
@@ -2971,8 +3012,9 @@ int QTreeView::indexRowSizeHint(const QModelIndex &index) const
qSwap(end, start);
int height = -1;
- QStyleOptionViewItem option = d->viewOptionsV1();
- // ### If we want word wrapping in the items,
+ QStyleOptionViewItem option;
+ initViewItemOption(&option);
+ // ### If we want word wrapping in the items,
// ### we need to go through all the columns
// ### and set the width of the column
@@ -2992,7 +3034,7 @@ int QTreeView::indexRowSizeHint(const QModelIndex &index) const
int max = editor->maximumSize().height();
height = qBound(min, height, max);
}
- int hint = d->delegateForIndex(idx)->sizeHint(option, idx).height();
+ int hint = itemDelegateForIndex(idx)->sizeHint(option, idx).height();
height = qMax(height, hint);
}
}
@@ -3051,11 +3093,26 @@ void QTreeViewPrivate::initialize()
header->setDefaultAlignment(Qt::AlignLeft|Qt::AlignVCenter);
q->setHeader(header);
#if QT_CONFIG(animation)
- animationsEnabled = q->style()->styleHint(QStyle::SH_Widget_Animation_Duration, 0, q) > 0;
- QObject::connect(&animatedOperation, SIGNAL(finished()), q, SLOT(_q_endAnimatedOperation()));
+ animationsEnabled = q->style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, q) > 0;
+ animationConnection =
+ QObjectPrivate::connect(&animatedOperation, &QVariantAnimation::finished,
+ this, &QTreeViewPrivate::endAnimatedOperation);
#endif // animation
}
+void QTreeViewPrivate::clearConnections()
+{
+ for (const QMetaObject::Connection &connection : modelConnections)
+ QObject::disconnect(connection);
+ for (const QMetaObject::Connection &connection : headerConnections)
+ QObject::disconnect(connection);
+ QObject::disconnect(selectionmodelConnection);
+ QObject::disconnect(sortHeaderConnection);
+#if QT_CONFIG(animation)
+ QObject::disconnect(animationConnection);
+#endif
+}
+
void QTreeViewPrivate::expand(int item, bool emitSignal)
{
Q_Q(QTreeView);
@@ -3088,13 +3145,14 @@ void QTreeViewPrivate::expand(int item, bool emitSignal)
beginAnimatedOperation();
#endif // animation
}
+ updateAccessibility();
}
void QTreeViewPrivate::insertViewItems(int pos, int count, const QTreeViewItem &viewItem)
{
viewItems.insert(pos, count, viewItem);
QTreeViewItem *items = viewItems.data();
- for (int i = pos + count; i < viewItems.count(); i++)
+ for (int i = pos + count; i < viewItems.size(); i++)
if (items[i].parentItem >= pos)
items[i].parentItem += count;
}
@@ -3103,7 +3161,7 @@ void QTreeViewPrivate::removeViewItems(int pos, int count)
{
viewItems.remove(pos, count);
QTreeViewItem *items = viewItems.data();
- for (int i = pos; i < viewItems.count(); i++)
+ for (int i = pos; i < viewItems.size(); i++)
if (items[i].parentItem >= pos)
items[i].parentItem -= count;
}
@@ -3243,14 +3301,15 @@ QPixmap QTreeViewPrivate::renderTreeToPixmapForAnimation(const QRect &rect) cons
painter.end();
//and now let's render the editors the editors
- QStyleOptionViewItem option = viewOptionsV1();
+ QStyleOptionViewItem option;
+ q->initViewItemOption(&option);
for (QEditorIndexHash::const_iterator it = editorIndexHash.constBegin(); it != editorIndexHash.constEnd(); ++it) {
QWidget *editor = it.key();
const QModelIndex &index = it.value();
- option.rect = q->visualRect(index);
+ option.rect = visualRect(index, SingleSection);
if (option.rect.isValid()) {
- if (QAbstractItemDelegate *delegate = delegateForIndex(index))
+ if (QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index))
delegate->updateEditorGeometry(editor, option, index);
const QPoint pos = editor->pos();
@@ -3267,7 +3326,7 @@ QPixmap QTreeViewPrivate::renderTreeToPixmapForAnimation(const QRect &rect) cons
return pixmap;
}
-void QTreeViewPrivate::_q_endAnimatedOperation()
+void QTreeViewPrivate::endAnimatedOperation()
{
Q_Q(QTreeView);
q->setState(stateBeforeAnimation);
@@ -3276,25 +3335,40 @@ void QTreeViewPrivate::_q_endAnimatedOperation()
}
#endif // animation
-void QTreeViewPrivate::_q_modelAboutToBeReset()
+void QTreeViewPrivate::modelAboutToBeReset()
{
viewItems.clear();
}
-void QTreeViewPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
+void QTreeViewPrivate::columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
if (start <= 0 && 0 <= end)
viewItems.clear();
- QAbstractItemViewPrivate::_q_columnsAboutToBeRemoved(parent, start, end);
+ QAbstractItemViewPrivate::columnsAboutToBeRemoved(parent, start, end);
}
-void QTreeViewPrivate::_q_columnsRemoved(const QModelIndex &parent, int start, int end)
+void QTreeViewPrivate::columnsRemoved(const QModelIndex &parent, int start, int end)
{
if (start <= 0 && 0 <= end)
doDelayedItemsLayout();
- QAbstractItemViewPrivate::_q_columnsRemoved(parent, start, end);
+ QAbstractItemViewPrivate::columnsRemoved(parent, start, end);
}
+void QTreeViewPrivate::updateAccessibility()
+{
+#if QT_CONFIG(accessibility)
+ Q_Q(QTreeView);
+ if (pendingAccessibilityUpdate) {
+ pendingAccessibilityUpdate = false;
+ if (QAccessible::isActive()) {
+ QAccessibleTableModelChangeEvent event(q, QAccessibleTableModelChangeEvent::ModelReset);
+ QAccessible::updateAccessibility(&event);
+ }
+ }
+#endif
+}
+
+
/** \internal
creates and initialize the viewItem structure of the children of the element \li
@@ -3315,11 +3389,32 @@ void QTreeViewPrivate::layout(int i, bool recursiveExpanding, bool afterIsUninit
return;
}
+#if QT_CONFIG(accessibility)
+ // QAccessibleTree's rowCount implementation uses viewItems.size(), so
+ // we need to invalidate any cached accessibility data structures if
+ // that value changes during the run of this function.
+ const auto resetModelIfNeeded = qScopeGuard([oldViewItemsSize = viewItems.size(), this]{
+ pendingAccessibilityUpdate |= oldViewItemsSize != viewItems.size();
+ });
+#endif
+
int count = 0;
if (model->hasChildren(parent)) {
- if (model->canFetchMore(parent))
+ if (model->canFetchMore(parent)) {
+ // fetchMore first, otherwise we might not yet have any data for sizeHintForRow
model->fetchMore(parent);
- count = model->rowCount(parent);
+ // guestimate the number of items in the viewport, and fetch as many as might fit
+ const int itemHeight = defaultItemHeight <= 0 ? q->sizeHintForRow(0) : defaultItemHeight;
+ const int viewCount = itemHeight ? viewport->height() / itemHeight : 0;
+ int lastCount = -1;
+ while ((count = model->rowCount(parent)) < viewCount &&
+ count != lastCount && model->canFetchMore(parent)) {
+ model->fetchMore(parent);
+ lastCount = count;
+ }
+ } else {
+ count = model->rowCount(parent);
+ }
}
bool expanding = true;
@@ -3334,7 +3429,7 @@ void QTreeViewPrivate::layout(int i, bool recursiveExpanding, bool afterIsUninit
if (!afterIsUninitialized)
insertViewItems(i + 1, count, QTreeViewItem()); // expand
else if (count > 0)
- viewItems.resize(viewItems.count() + count);
+ viewItems.resize(viewItems.size() + count);
} else {
expanding = false;
}
@@ -3344,7 +3439,7 @@ void QTreeViewPrivate::layout(int i, bool recursiveExpanding, bool afterIsUninit
int hidden = 0;
int last = 0;
int children = 0;
- QTreeViewItem *item = 0;
+ QTreeViewItem *item = nullptr;
for (int j = first; j < first + count; ++j) {
current = model->index(j - first, 0, parent);
if (isRowHidden(current)) {
@@ -3404,7 +3499,7 @@ int QTreeViewPrivate::pageUp(int i) const
index = 0;
while (isItemHiddenOrDisabled(index))
index++;
- return index >= viewItems.count() ? 0 : index;
+ return index >= viewItems.size() ? 0 : index;
}
int QTreeViewPrivate::pageDown(int i) const
@@ -3412,11 +3507,11 @@ int QTreeViewPrivate::pageDown(int i) const
int index = itemAtCoordinate(coordinateForItem(i) + viewport->height());
while (isItemHiddenOrDisabled(index))
index++;
- if (index == -1 || index >= viewItems.count())
- index = viewItems.count() - 1;
+ if (index == -1 || index >= viewItems.size())
+ index = viewItems.size() - 1;
while (isItemHiddenOrDisabled(index))
index--;
- return index == -1 ? viewItems.count() - 1 : index;
+ return index == -1 ? viewItems.size() - 1 : index;
}
int QTreeViewPrivate::itemForKeyHome() const
@@ -3424,20 +3519,20 @@ int QTreeViewPrivate::itemForKeyHome() const
int index = 0;
while (isItemHiddenOrDisabled(index))
index++;
- return index >= viewItems.count() ? 0 : index;
+ return index >= viewItems.size() ? 0 : index;
}
int QTreeViewPrivate::itemForKeyEnd() const
{
- int index = viewItems.count() - 1;
+ int index = viewItems.size() - 1;
while (isItemHiddenOrDisabled(index))
index--;
- return index == -1 ? viewItems.count() - 1 : index;
+ return index == -1 ? viewItems.size() - 1 : index;
}
int QTreeViewPrivate::indentationForItem(int item) const
{
- if (item < 0 || item >= viewItems.count())
+ if (item < 0 || item >= viewItems.size())
return 0;
int level = viewItems.at(item).level;
if (rootDecoration)
@@ -3447,6 +3542,7 @@ int QTreeViewPrivate::indentationForItem(int item) const
int QTreeViewPrivate::itemHeight(int item) const
{
+ Q_ASSERT(item < viewItems.size());
if (uniformRowHeights)
return defaultItemHeight;
if (viewItems.isEmpty())
@@ -3474,7 +3570,7 @@ int QTreeViewPrivate::coordinateForItem(int item) const
return (item * defaultItemHeight) - vbar->value();
// ### optimize (maybe do like QHeaderView by letting items have startposition)
int y = 0;
- for (int i = 0; i < viewItems.count(); ++i) {
+ for (int i = 0; i < viewItems.size(); ++i) {
if (i == item)
return y - vbar->value();
y += itemHeight(i);
@@ -3488,7 +3584,7 @@ int QTreeViewPrivate::coordinateForItem(int item) const
// ### slow if the item is not visible
int viewItemCoordinate = 0;
int viewItemIndex = topViewItemIndex;
- while (viewItemIndex < viewItems.count()) {
+ while (viewItemIndex < viewItems.size()) {
if (viewItemIndex == item)
return viewItemCoordinate;
viewItemCoordinate += itemHeight(viewItemIndex);
@@ -3520,7 +3616,7 @@ int QTreeViewPrivate::coordinateForItem(int item) const
*/
int QTreeViewPrivate::itemAtCoordinate(int coordinate) const
{
- const int itemCount = viewItems.count();
+ const int itemCount = viewItems.size();
if (itemCount == 0)
return -1;
if (uniformRowHeights && defaultItemHeight <= 0)
@@ -3533,7 +3629,7 @@ int QTreeViewPrivate::itemAtCoordinate(int coordinate) const
// ### optimize
int viewItemCoordinate = 0;
const int contentsCoordinate = coordinate + vbar->value();
- for (int viewItemIndex = 0; viewItemIndex < viewItems.count(); ++viewItemIndex) {
+ for (int viewItemIndex = 0; viewItemIndex < viewItems.size(); ++viewItemIndex) {
viewItemCoordinate += itemHeight(viewItemIndex);
if (viewItemCoordinate > contentsCoordinate)
return (viewItemIndex >= itemCount ? -1 : viewItemIndex);
@@ -3549,7 +3645,7 @@ int QTreeViewPrivate::itemAtCoordinate(int coordinate) const
if (coordinate >= 0) {
// the coordinate is in or below the viewport
int viewItemCoordinate = 0;
- for (int viewItemIndex = topViewItemIndex; viewItemIndex < viewItems.count(); ++viewItemIndex) {
+ for (int viewItemIndex = topViewItemIndex; viewItemIndex < viewItems.size(); ++viewItemIndex) {
viewItemCoordinate += itemHeight(viewItemIndex);
if (viewItemCoordinate > coordinate)
return (viewItemIndex >= itemCount ? -1 : viewItemIndex);
@@ -3572,7 +3668,7 @@ int QTreeViewPrivate::viewIndex(const QModelIndex &_index) const
if (!_index.isValid() || viewItems.isEmpty())
return -1;
- const int totalCount = viewItems.count();
+ const int totalCount = viewItems.size();
const QModelIndex index = _index.sibling(_index.row(), 0);
const int row = index.row();
const quintptr internalId = index.internalId();
@@ -3613,7 +3709,7 @@ int QTreeViewPrivate::viewIndex(const QModelIndex &_index) const
QModelIndex QTreeViewPrivate::modelIndex(int i, int column) const
{
- if (i < 0 || i >= viewItems.count())
+ if (i < 0 || i >= viewItems.size())
return QModelIndex();
QModelIndex ret = viewItems.at(i).index;
@@ -3628,7 +3724,7 @@ int QTreeViewPrivate::firstVisibleItem(int *offset) const
if (verticalScrollMode == QAbstractItemView::ScrollPerItem) {
if (offset)
*offset = 0;
- return (value < 0 || value >= viewItems.count()) ? -1 : value;
+ return (value < 0 || value >= viewItems.size()) ? -1 : value;
}
// ScrollMode == ScrollPerPixel
if (uniformRowHeights) {
@@ -3640,7 +3736,7 @@ int QTreeViewPrivate::firstVisibleItem(int *offset) const
return value / defaultItemHeight;
}
int y = 0; // ### (maybe do like QHeaderView by letting items have startposition)
- for (int i = 0; i < viewItems.count(); ++i) {
+ for (int i = 0; i < viewItems.size(); ++i) {
y += itemHeight(i); // the height value is cached
if (y > value) {
if (offset)
@@ -3661,7 +3757,7 @@ int QTreeViewPrivate::lastVisibleItem(int firstVisual, int offset) const
int y = - offset;
int value = viewport->height();
- for (int i = firstVisual; i < viewItems.count(); ++i) {
+ for (int i = firstVisual; i < viewItems.size(); ++i) {
y += itemHeight(i); // the height value is cached
if (y > value)
return i;
@@ -3689,11 +3785,11 @@ void QTreeViewPrivate::updateScrollBars()
int itemsInViewport = 0;
if (uniformRowHeights) {
if (defaultItemHeight <= 0)
- itemsInViewport = viewItems.count();
+ itemsInViewport = viewItems.size();
else
itemsInViewport = viewportSize.height() / defaultItemHeight;
} else {
- const int itemsCount = viewItems.count();
+ const int itemsCount = viewItems.size();
const int viewportHeight = viewportSize.height();
for (int height = 0, item = itemsCount - 1; item >= 0; --item) {
height += itemHeight(item);
@@ -3705,15 +3801,15 @@ void QTreeViewPrivate::updateScrollBars()
if (verticalScrollMode == QAbstractItemView::ScrollPerItem) {
if (!viewItems.isEmpty())
itemsInViewport = qMax(1, itemsInViewport);
- vbar->setRange(0, viewItems.count() - itemsInViewport);
+ vbar->setRange(0, viewItems.size() - itemsInViewport);
vbar->setPageStep(itemsInViewport);
vbar->setSingleStep(1);
} else { // scroll per pixel
int contentsHeight = 0;
if (uniformRowHeights) {
- contentsHeight = defaultItemHeight * viewItems.count();
+ contentsHeight = defaultItemHeight * viewItems.size();
} else { // ### (maybe do like QHeaderView by letting items have startposition)
- for (int i = 0; i < viewItems.count(); ++i)
+ for (int i = 0; i < viewItems.size(); ++i)
contentsHeight += itemHeight(i);
}
vbar->setRange(0, contentsHeight - viewportSize.height());
@@ -3755,7 +3851,8 @@ int QTreeViewPrivate::itemDecorationAt(const QPoint &pos) const
bool spanned = false;
if (!spanningIndexes.isEmpty()) {
const QModelIndex index = q->indexAt(pos);
- spanned = q->isFirstColumnSpanned(index.row(), index.parent());
+ if (index.isValid())
+ spanned = q->isFirstColumnSpanned(index.row(), index.parent());
}
const int column = spanned ? 0 : header->logicalIndexAt(pos.x());
if (!isTreePosition(column))
@@ -3796,8 +3893,8 @@ QRect QTreeViewPrivate::itemDecorationRect(const QModelIndex &index) const
return q->style()->subElementRect(QStyle::SE_TreeViewDisclosureItem, &opt, q);
}
-QVector<QPair<int, int> > QTreeViewPrivate::columnRanges(const QModelIndex &topIndex,
- const QModelIndex &bottomIndex) const
+QList<QPair<int, int>> QTreeViewPrivate::columnRanges(const QModelIndex &topIndex,
+ const QModelIndex &bottomIndex) const
{
const int topVisual = header->visualIndex(topIndex.column()),
bottomVisual = header->visualIndex(bottomIndex.column());
@@ -3817,11 +3914,11 @@ QVector<QPair<int, int> > QTreeViewPrivate::columnRanges(const QModelIndex &topI
//let's sort the list
std::sort(logicalIndexes.begin(), logicalIndexes.end());
- QVector<QPair<int, int> > ret;
+ QList<QPair<int, int>> ret;
QPair<int, int> current;
current.first = -2; // -1 is not enough because -1+1 = 0
current.second = -2;
- for(int i = 0; i < logicalIndexes.count(); ++i) {
+ for(int i = 0; i < logicalIndexes.size(); ++i) {
const int logicalColumn = logicalIndexes.at(i);
if (current.second + 1 != logicalColumn) {
if (current.first != -2) {
@@ -3851,8 +3948,8 @@ void QTreeViewPrivate::select(const QModelIndex &topIndex, const QModelIndex &bo
const int top = viewIndex(topIndex),
bottom = viewIndex(bottomIndex);
- const QVector<QPair<int, int> > colRanges = columnRanges(topIndex, bottomIndex);
- QVector<QPair<int, int> >::const_iterator it;
+ const QList<QPair<int, int>> colRanges = columnRanges(topIndex, bottomIndex);
+ QList<QPair<int, int>>::const_iterator it;
for (it = colRanges.begin(); it != colRanges.end(); ++it) {
const int left = (*it).first,
right = (*it).second;
@@ -3897,7 +3994,7 @@ void QTreeViewPrivate::select(const QModelIndex &topIndex, const QModelIndex &bo
}
if (currentRange.isValid())
selection.append(currentRange);
- for (int i = 0; i < rangeStack.count(); ++i)
+ for (int i = 0; i < rangeStack.size(); ++i)
selection.append(rangeStack.at(i));
}
q->selectionModel()->select(selection, command);
@@ -3915,7 +4012,7 @@ QPair<int,int> QTreeViewPrivate::startAndEndColumns(const QRect &rect) const
start = (start == -1 ? 0 : start);
end = (end == -1 ? header->count() - 1 : end);
}
- return qMakePair<int,int>(qMin(start, end), qMax(start, end));
+ return qMakePair(qMin(start, end), qMax(start, end));
}
bool QTreeViewPrivate::hasVisibleChildren(const QModelIndex& parent) const
@@ -3939,7 +4036,7 @@ bool QTreeViewPrivate::hasVisibleChildren(const QModelIndex& parent) const
return false;
}
-void QTreeViewPrivate::_q_sortIndicatorChanged(int column, Qt::SortOrder order)
+void QTreeViewPrivate::sortIndicatorChanged(int column, Qt::SortOrder order)
{
model->sort(column, order);
}
@@ -3955,7 +4052,7 @@ int QTreeViewPrivate::accessibleTree2Index(const QModelIndex &index) const
void QTreeViewPrivate::updateIndentationFromStyle()
{
Q_Q(const QTreeView);
- indent = q->style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, q);
+ indent = q->style()->pixelMetric(QStyle::PM_TreeViewIndentation, nullptr, q);
}
/*!
@@ -3963,24 +4060,17 @@ void QTreeViewPrivate::updateIndentationFromStyle()
*/
void QTreeView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
+ Q_D(QTreeView);
QAbstractItemView::currentChanged(current, previous);
if (allColumnsShowFocus()) {
- if (previous.isValid()) {
- QRect previousRect = visualRect(previous);
- previousRect.setX(0);
- previousRect.setWidth(viewport()->width());
- viewport()->update(previousRect);
- }
- if (current.isValid()) {
- QRect currentRect = visualRect(current);
- currentRect.setX(0);
- currentRect.setWidth(viewport()->width());
- viewport()->update(currentRect);
- }
+ if (previous.isValid())
+ viewport()->update(d->visualRect(previous, QTreeViewPrivate::FullRow));
+ if (current.isValid())
+ viewport()->update(d->visualRect(current, QTreeViewPrivate::FullRow));
}
-#ifndef QT_NO_ACCESSIBILITY
- if (QAccessible::isActive() && current.isValid()) {
+#if QT_CONFIG(accessibility)
+ if (QAccessible::isActive() && current.isValid() && hasFocus()) {
Q_D(QTreeView);
QAccessibleEvent event(this, QAccessible::Focus);
@@ -3997,7 +4087,7 @@ void QTreeView::selectionChanged(const QItemSelection &selected,
const QItemSelection &deselected)
{
QAbstractItemView::selectionChanged(selected, deselected);
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (QAccessible::isActive()) {
Q_D(QTreeView);
diff --git a/src/widgets/itemviews/qtreeview.h b/src/widgets/itemviews/qtreeview.h
index bb00f1df07..1dd4650c5d 100644
--- a/src/widgets/itemviews/qtreeview.h
+++ b/src/widgets/itemviews/qtreeview.h
@@ -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
#ifndef QTREEVIEW_H
#define QTREEVIEW_H
@@ -143,8 +107,8 @@ public:
void doItemsLayout() override;
void reset() override;
-
- void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override;
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QList<int> &roles = QList<int>()) override;
void selectAll() override;
Q_SIGNALS:
@@ -157,10 +121,6 @@ public Q_SLOTS:
void expand(const QModelIndex &index);
void collapse(const QModelIndex &index);
void resizeColumnToContents(int column);
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTreeView::sortByColumn(int column, Qt::SortOrder order) instead")
- void sortByColumn(int column);
-#endif
void sortByColumn(int column, Qt::SortOrder order);
void expandAll();
void expandRecursively(const QModelIndex &index, int depth = -1);
@@ -189,6 +149,7 @@ protected:
QRegion visualRegionForSelection(const QItemSelection &selection) const override;
QModelIndexList selectedIndexes() const override;
+ void changeEvent(QEvent *event) override;
void timerEvent(QTimerEvent *event) override;
void paintEvent(QPaintEvent *event) override;
@@ -234,11 +195,6 @@ private:
Q_DECLARE_PRIVATE(QTreeView)
Q_DISABLE_COPY(QTreeView)
-#if QT_CONFIG(animation)
- Q_PRIVATE_SLOT(d_func(), void _q_endAnimatedOperation())
-#endif // animation
- Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset())
- Q_PRIVATE_SLOT(d_func(), void _q_sortIndicatorChanged(int column, Qt::SortOrder order))
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qtreeview_p.h b/src/widgets/itemviews/qtreeview_p.h
index 836d8f0c2d..d0afdf1223 100644
--- a/src/widgets/itemviews/qtreeview_p.h
+++ b/src/widgets/itemviews/qtreeview_p.h
@@ -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
#ifndef QTREEVIEW_P_H
#define QTREEVIEW_P_H
@@ -53,11 +17,13 @@
#include <QtWidgets/private/qtwidgetsglobal_p.h>
#include "private/qabstractitemview_p.h"
+#include <QtCore/qabstractitemmodel.h>
+#include <QtCore/qlist.h>
#if QT_CONFIG(animation)
#include <QtCore/qvariantanimation.h>
#endif
-#include <QtCore/qabstractitemmodel.h>
-#include <QtCore/qvector.h>
+
+#include <array>
QT_REQUIRE_CONFIG(treeview);
@@ -78,7 +44,7 @@ struct QTreeViewItem
int height : 16; // row height
};
-Q_DECLARE_TYPEINFO(QTreeViewItem, Q_MOVABLE_TYPE);
+Q_DECLARE_TYPEINFO(QTreeViewItem, Q_RELOCATABLE_TYPE);
class Q_WIDGETS_EXPORT QTreeViewPrivate : public QAbstractItemViewPrivate
{
@@ -98,6 +64,7 @@ public:
~QTreeViewPrivate() {}
void initialize();
+ void clearConnections();
int logicalIndexForTree() const;
inline bool isTreePosition(int logicalIndex) const
{
@@ -124,17 +91,18 @@ public:
void beginAnimatedOperation();
void drawAnimatedOperation(QPainter *painter) const;
QPixmap renderTreeToPixmapForAnimation(const QRect &rect) const;
- void _q_endAnimatedOperation();
+ void endAnimatedOperation();
#endif // animation
void expand(int item, bool emitSignal);
void collapse(int item, bool emitSignal);
- void _q_columnsAboutToBeRemoved(const QModelIndex &, int, int) override;
- void _q_columnsRemoved(const QModelIndex &, int, int) override;
- void _q_modelAboutToBeReset();
- void _q_sortIndicatorChanged(int column, Qt::SortOrder order);
- void _q_modelDestroyed() override;
+ void columnsAboutToBeRemoved(const QModelIndex &, int, int) override;
+ void columnsRemoved(const QModelIndex &, int, int) override;
+ void modelAboutToBeReset();
+ void sortIndicatorChanged(int column, Qt::SortOrder order);
+ void modelDestroyed() override;
+ QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const override;
void layout(int item, bool recusiveExpanding = false, bool afterIsUninitialized = false);
@@ -169,8 +137,8 @@ public:
int itemDecorationAt(const QPoint &pos) const;
QRect itemDecorationRect(const QModelIndex &index) const;
-
- QVector<QPair<int, int> > columnRanges(const QModelIndex &topIndex, const QModelIndex &bottomIndex) const;
+ QList<QPair<int, int>> columnRanges(const QModelIndex &topIndex,
+ const QModelIndex &bottomIndex) const;
void select(const QModelIndex &start, const QModelIndex &stop, QItemSelectionModel::SelectionFlags command);
QPair<int,int> startAndEndColumns(const QRect &rect) const;
@@ -181,12 +149,29 @@ public:
// logicalIndices: vector of currently visibly logical indices
// itemPositions: vector of view item positions (beginning/middle/end/onlyone)
- void calcLogicalIndices(QVector<int> *logicalIndices, QVector<QStyleOptionViewItem::ViewItemPosition> *itemPositions, int left, int right) const;
+ void calcLogicalIndices(QList<int> *logicalIndices,
+ QList<QStyleOptionViewItem::ViewItemPosition> *itemPositions, int left,
+ int right) const;
int widthHintForIndex(const QModelIndex &index, int hint, const QStyleOptionViewItem &option, int i) const;
+
+ enum RectRule {
+ FullRow,
+ SingleSection,
+ AddRowIndicatorToFirstSection
+ };
+
+ // Base class will get the first visual rect including row indicator
+ QRect visualRect(const QModelIndex &index) const override
+ {
+ return visualRect(index, AddRowIndicatorToFirstSection);
+ }
+
+ QRect visualRect(const QModelIndex &index, RectRule rule) const;
+
QHeaderView *header;
int indent;
- mutable QVector<QTreeViewItem> viewItems;
+ mutable QList<QTreeViewItem> viewItems;
mutable int lastViewedItem;
int defaultItemHeight; // this is just a number; contentsHeight() / numItems
bool uniformRowHeights; // used when all rows have the same height
@@ -229,7 +214,7 @@ public:
}
inline bool isItemHiddenOrDisabled(int i) const {
- if (i < 0 || i >= viewItems.count())
+ if (i < 0 || i >= viewItems.size())
return false;
const QModelIndex index = viewItems.at(i).index;
return isRowHidden(index) || !isIndexEnabled(index);
@@ -238,7 +223,7 @@ public:
inline int above(int item) const
{ int i = item; while (isItemHiddenOrDisabled(--item)){} return item < 0 ? i : item; }
inline int below(int item) const
- { int i = item; while (isItemHiddenOrDisabled(++item)){} return item >= viewItems.count() ? i : item; }
+ { int i = item; while (isItemHiddenOrDisabled(++item)){} return item >= viewItems.size() ? i : item; }
inline void invalidateHeightCache(int item) const
{ viewItems[item].height = 0; }
@@ -261,7 +246,7 @@ public:
int autoExpandDelay;
QBasicTimer openTimer;
- // used for drawing hilighted expand/collapse indicators
+ // used for drawing highlighted expand/collapse indicators
mutable int hoverBranch;
// used for blocking recursion when calling setViewportMargins from updateGeometries
@@ -272,6 +257,18 @@ public:
// tree position
int treePosition;
+
+ // pending accessibility update
+#if QT_CONFIG(accessibility)
+ bool pendingAccessibilityUpdate = false;
+#endif
+ void updateAccessibility();
+
+ QMetaObject::Connection animationConnection;
+ QMetaObject::Connection selectionmodelConnection;
+ std::array<QMetaObject::Connection, 2> modelConnections;
+ std::array<QMetaObject::Connection, 5> headerConnections;
+ QMetaObject::Connection sortHeaderConnection;
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp
index 8575512078..8e46a0efbe 100644
--- a/src/widgets/itemviews/qtreewidget.cpp
+++ b/src/widgets/itemviews/qtreewidget.cpp
@@ -1,53 +1,18 @@
-/****************************************************************************
-**
-** Copyright (C) 2019 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) 2019 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 "qtreewidget.h"
#include <qheaderview.h>
#include <qpainter.h>
-#include <qitemdelegate.h>
#include <qstack.h>
#include <qdebug.h>
#include <private/qtreewidget_p.h>
#include <private/qwidgetitemdata_p.h>
#include <private/qtreewidgetitemiterator_p.h>
+#include <QtCore/private/qduplicatetracker_p.h>
+
#include <algorithm>
QT_BEGIN_NAMESPACE
@@ -110,8 +75,9 @@ public:
*/
QTreeModel::QTreeModel(int columns, QTreeWidget *parent)
- : QAbstractItemModel(parent), rootItem(new QTreeWidgetItem),
- headerItem(new QTreeWidgetItem), skipPendingSort(false)
+ : QAbstractItemModel(*new QTreeModelPrivate, parent),
+ rootItem(new QTreeWidgetItem),
+ headerItem(new QTreeWidgetItem)
{
rootItem->view = parent;
rootItem->itemFlags = Qt::ItemIsDropEnabled;
@@ -125,8 +91,7 @@ QTreeModel::QTreeModel(int columns, QTreeWidget *parent)
*/
QTreeModel::QTreeModel(QTreeModelPrivate &dd, QTreeWidget *parent)
- : QAbstractItemModel(dd, parent), rootItem(new QTreeWidgetItem),
- headerItem(new QTreeWidgetItem), skipPendingSort(false)
+ : QAbstractItemModel(dd, parent), rootItem(new QTreeWidgetItem), headerItem(new QTreeWidgetItem)
{
rootItem->view = parent;
rootItem->itemFlags = Qt::ItemIsDropEnabled;
@@ -144,7 +109,7 @@ QTreeModel::~QTreeModel()
clear();
headerItem->view = nullptr;
delete headerItem;
- rootItem->view = 0;
+ rootItem->view = nullptr;
delete rootItem;
}
@@ -160,8 +125,8 @@ void QTreeModel::clear()
beginResetModel();
for (int i = 0; i < rootItem->childCount(); ++i) {
QTreeWidgetItem *item = rootItem->children.at(i);
- item->par = 0;
- item->view = 0;
+ item->par = nullptr;
+ item->view = nullptr;
delete item;
}
rootItem->children.clear();
@@ -214,7 +179,7 @@ void QTreeModel::setColumnCount(int columns)
QTreeWidgetItem *QTreeModel::item(const QModelIndex &index) const
{
if (!index.isValid())
- return 0;
+ return nullptr;
return static_cast<QTreeWidgetItem*>(index.internalPointer());
}
@@ -238,7 +203,7 @@ QModelIndex QTreeModel::index(const QTreeWidgetItem *item, int column) const
int row;
int guess = item->d->rowGuess;
if (guess >= 0
- && par->children.count() > guess
+ && par->children.size() > guess
&& par->children.at(guess) == itm) {
row = guess;
} else {
@@ -390,7 +355,6 @@ bool QTreeModel::setData(const QModelIndex &index, const QVariant &value, int ro
return false;
}
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool QTreeModel::clearItemData(const QModelIndex &index)
{
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
@@ -406,10 +370,9 @@ bool QTreeModel::clearItemData(const QModelIndex &index)
}
itm->d->display[index.column()] = QVariant();
itm->values[index.column()].clear();
- emit dataChanged(index, index, QVector<int>{});
+ emit dataChanged(index, index, QList<int> {});
return true;
}
-#endif
QMap<int, QVariant> QTreeModel::itemData(const QModelIndex &index) const
{
@@ -417,8 +380,8 @@ QMap<int, QVariant> QTreeModel::itemData(const QModelIndex &index) const
QTreeWidgetItem *itm = item(index);
if (itm) {
int column = index.column();
- if (column < itm->values.count()) {
- for (int i = 0; i < itm->values.at(column).count(); ++i) {
+ if (column < itm->values.size()) {
+ for (int i = 0; i < itm->values.at(column).size(); ++i) {
roles.insert(itm->values.at(column).at(i).role,
itm->values.at(column).at(i).value);
}
@@ -487,11 +450,11 @@ bool QTreeModel::insertColumns(int column, int count, const QModelIndex &parent)
while (!itemstack.isEmpty()) {
QTreeWidgetItem *par = itemstack.pop();
QList<QTreeWidgetItem*> children = par ? par->children : rootItem->children;
- for (int row = 0; row < children.count(); ++row) {
+ for (int row = 0; row < children.size(); ++row) {
QTreeWidgetItem *child = children.at(row);
- if (child->children.count())
+ if (child->children.size())
itemstack.push(child);
- child->values.insert(column, count, QVector<QWidgetItemData>());
+ child->values.insert(column, count, QList<QWidgetItemData>());
}
}
@@ -506,22 +469,18 @@ bool QTreeModel::insertColumns(int column, int count, const QModelIndex &parent)
bool QTreeModel::removeRows(int row, int count, const QModelIndex &parent) {
if (count < 1 || row < 0 || (row + count) > rowCount(parent))
return false;
-
- beginRemoveRows(parent, row, row + count - 1);
-
- QSignalBlocker blocker(this);
-
- QTreeWidgetItem *itm = item(parent);
+ QTreeWidgetItem *parentItem = item(parent);
+ // if parentItem is valid, begin/end RemoveRows is handled by takeChild below
+ if (!parentItem)
+ beginRemoveRows(parent, row, row + count - 1);
for (int i = row + count - 1; i >= row; --i) {
- QTreeWidgetItem *child = itm ? itm->takeChild(i) : rootItem->children.takeAt(i);
+ QTreeWidgetItem *child = parentItem ? parentItem->takeChild(i) : rootItem->children.takeAt(i);
Q_ASSERT(child);
- child->view = 0;
+ child->view = nullptr;
delete child;
- child = 0;
}
- blocker.unblock();
-
- endRemoveRows();
+ if (!parentItem)
+ endRemoveRows();
return true;
}
@@ -622,7 +581,7 @@ void QTreeModel::ensureSorted(int column, Qt::SortOrder order,
QList<QTreeWidgetItem*> lst = itm->children;
int count = end - start + 1;
- QVector < QPair<QTreeWidgetItem*,int> > sorting(count);
+ QList<QPair<QTreeWidgetItem *, int>> sorting(count);
for (int i = 0; i < count; ++i) {
sorting[i].first = lst.at(start + i);
sorting[i].second = start + i;
@@ -667,7 +626,7 @@ void QTreeModel::ensureSorted(int column, Qt::SortOrder order,
else if (oldRow > otherRow && newRow <= otherRow)
++sorting[j].second;
}
- for (int k = 0; k < newPersistentIndexes.count(); ++k) {
+ for (int k = 0; k < newPersistentIndexes.size(); ++k) {
QModelIndex pi = newPersistentIndexes.at(k);
if (pi.parent() != parent)
continue;
@@ -738,7 +697,10 @@ QList<QTreeWidgetItem*>::iterator QTreeModel::sortedInsertionIterator(
QStringList QTreeModel::mimeTypes() const
{
- return view()->mimeTypes();
+ auto v = view();
+ if (v)
+ return v->mimeTypes();
+ return {};
}
QMimeData *QTreeModel::internalMimeData() const
@@ -780,7 +742,7 @@ Qt::DropActions QTreeModel::supportedDropActions() const
void QTreeModel::itemChanged(QTreeWidgetItem *item)
{
- SkipSorting skipSorting(this); //this is kind of wrong, but not doing this would kill performence
+ SkipSorting skipSorting(this); //this is kind of wrong, but not doing this would kill performance
QModelIndex left = index(item, 0);
QModelIndex right = index(item, item->columnCount() - 1);
emit dataChanged(left, right);
@@ -798,7 +760,7 @@ bool QTreeModel::isChanging() const
if column is -1 then all columns have changed
*/
-void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column, const QVector<int> &roles)
+void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column, const QList<int> &roles)
{
if (signalsBlocked())
return;
@@ -811,7 +773,7 @@ void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column, const QVecto
return;
}
- SkipSorting skipSorting(this); //This is a little bit wrong, but not doing it would kill performence
+ SkipSorting skipSorting(this); //This is a little bit wrong, but not doing it would kill performance
QModelIndex bottomRight, topLeft;
if (column == -1) {
@@ -843,7 +805,7 @@ void QTreeModel::beginRemoveItems(QTreeWidgetItem *parent, int row, int count)
if (!parent)
parent = rootItem;
// now update the iterators
- for (int i = 0; i < iterators.count(); ++i) {
+ for (int i = 0; i < iterators.size(); ++i) {
for (int j = 0; j < count; j++) {
QTreeWidgetItem *c = parent->child(row + j);
iterators[i]->d_func()->ensureValidIterator(c);
@@ -864,8 +826,8 @@ void QTreeModel::sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortO
return;
// store the original order of indexes
- QVector< QPair<QTreeWidgetItem*,int> > sorting(items->count());
- for (int i = 0; i < sorting.count(); ++i) {
+ QList<QPair<QTreeWidgetItem *, int>> sorting(items->size());
+ for (int i = 0; i < sorting.size(); ++i) {
sorting[i].first = items->at(i);
sorting[i].second = i;
}
@@ -877,7 +839,7 @@ void QTreeModel::sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortO
QModelIndexList fromList;
QModelIndexList toList;
int colCount = columnCount();
- for (int r = 0; r < sorting.count(); ++r) {
+ for (int r = 0; r < sorting.size(); ++r) {
int oldRow = sorting.at(r).second;
if (oldRow == r)
continue;
@@ -1254,22 +1216,6 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const
\sa font(), setText(), setForeground()
*/
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn QColor QTreeWidgetItem::backgroundColor(int column) const
- \obsolete
-
- This function is deprecated. Use background() instead.
-*/
-
-/*!
- \fn void QTreeWidgetItem::setBackgroundColor(int column, const QColor &color)
- \obsolete
-
- This function is deprecated. Use setBackground() instead.
-*/
-#endif
-
/*!
\fn QBrush QTreeWidgetItem::background(int column) const
\since 4.2
@@ -1294,21 +1240,6 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const
\sa setForeground()
*/
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- \fn QColor QTreeWidgetItem::textColor(int column) const
- \obsolete
-
- This function is deprecated. Use foreground() instead.
-*/
-
-/*!
- \fn void QTreeWidgetItem::setTextColor(int column, const QColor &color)
- \obsolete
-
- This function is deprecated. Use setForeground() instead.
-*/
-#endif
/*!
\fn QBrush QTreeWidgetItem::foreground(int column) const
@@ -1394,17 +1325,49 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const
*/
/*!
+ \if defined(qt7)
+
+ \fn Qt::Alignment QTreeWidgetItem::textAlignment(int column) const
+
+ Returns the text alignment for the label in the given \a column.
+
+ \else
+
\fn int QTreeWidgetItem::textAlignment(int column) const
- Returns the text alignment for the label in the given \a column
- (see \l{Qt::AlignmentFlag}).
+ Returns the text alignment for the label in the given \a column.
+
+ \note This function returns an int for historical reasons. It will
+ be corrected to return Qt::Alignment in Qt 7.
+
+ \sa Qt::Alignment
+
+ \endif
*/
/*!
+ \obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
+
\fn void QTreeWidgetItem::setTextAlignment(int column, int alignment)
Sets the text alignment for the label in the given \a column to
- the \a alignment specified (see \l{Qt::AlignmentFlag}).
+ the \a alignment specified.
+
+ \sa Qt::Alignment
+*/
+
+/*!
+ \since 6.4
+
+ \fn void QTreeWidgetItem::setTextAlignment(int column, Qt::Alignment alignment)
+
+ Sets the text alignment for the label in the given \a column to
+ the \a alignment specified.
+*/
+
+/*!
+ \fn void QTreeWidgetItem::setTextAlignment(int column, Qt::AlignmentFlag alignment)
+ \internal
*/
/*!
@@ -1419,16 +1382,7 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const
\sa type()
*/
-QTreeWidgetItem::QTreeWidgetItem(int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
-{
-}
-
+QTreeWidgetItem::QTreeWidgetItem(int type) : rtti(type), d(new QTreeWidgetItemPrivate(this)) { }
/*!
Constructs a tree widget item of the specified \a type. The item
@@ -1439,14 +1393,9 @@ QTreeWidgetItem::QTreeWidgetItem(int type)
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
-{
- for (int i = 0; i < strings.count(); ++i)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
+{
+ for (int i = 0; i < strings.size(); ++i)
setText(i, strings.at(i));
}
@@ -1460,12 +1409,7 @@ QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type)
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
{
// do not set this->view here otherwise insertChild() will fail
if (QTreeModel *model = treeModel(treeview)) {
@@ -1485,14 +1429,9 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, int type)
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
-{
- for (int i = 0; i < strings.count(); ++i)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
+{
+ for (int i = 0; i < strings.size(); ++i)
setText(i, strings.at(i));
// do not set this->view here otherwise insertChild() will fail
if (QTreeModel *model = treeModel(treeview)) {
@@ -1510,12 +1449,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strin
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
{
// do not set this->view here otherwise insertChild() will fail
if (QTreeModel *model = treeModel(treeview)) {
@@ -1531,12 +1465,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after,
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
{
if (parent)
parent->addChild(this);
@@ -1549,14 +1478,9 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type)
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
-{
- for (int i = 0; i < strings.count(); ++i)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
+{
+ for (int i = 0; i < strings.size(); ++i)
setText(i, strings.at(i));
if (parent)
parent->addChild(this);
@@ -1571,12 +1495,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &str
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type)
- : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
- itemFlags(Qt::ItemIsSelectable
- |Qt::ItemIsUserCheckable
- |Qt::ItemIsEnabled
- |Qt::ItemIsDragEnabled
- |Qt::ItemIsDropEnabled)
+ : rtti(type), d(new QTreeWidgetItemPrivate(this))
{
if (parent) {
int i = parent->children.indexOf(after) + 1;
@@ -1595,11 +1514,8 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after
QTreeWidgetItem::~QTreeWidgetItem()
{
QTreeModel *model = treeModel();
- bool wasSkipSort = false;
- if (model) {
- wasSkipSort = model->skipPendingSort;
- model->skipPendingSort = true;
- }
+ QTreeModel::SkipSorting skipSorting(model);
+
if (par) {
int i = par->children.indexOf(this);
if (i >= 0) {
@@ -1612,11 +1528,11 @@ QTreeWidgetItem::~QTreeWidgetItem()
}
} else if (model) {
if (this == model->headerItem) {
- model->headerItem = 0;
+ model->headerItem = nullptr;
} else {
int i = model->rootItem->children.indexOf(this);
if (i >= 0) {
- model->beginRemoveItems(0, i, 1);
+ model->beginRemoveItems(nullptr, i, 1);
// users _could_ do changes when connected to rowsAboutToBeRemoved,
// so we check again to make sure 'i' is valid
if (!model->rootItem->children.isEmpty() && model->rootItem->children.at(i) == this)
@@ -1627,20 +1543,17 @@ QTreeWidgetItem::~QTreeWidgetItem()
}
// at this point the persistent indexes for the children should also be invalidated
// since we invalidated the parent
- for (int i = 0; i < children.count(); ++i) {
+ for (int i = 0; i < children.size(); ++i) {
QTreeWidgetItem *child = children.at(i);
// make sure the child does not try to remove itself from our children list
- child->par = 0;
+ child->par = nullptr;
// make sure the child does not try to remove itself from the top level list
- child->view = 0;
+ child->view = nullptr;
delete child;
}
children.clear();
delete d;
- if (model) {
- model->skipPendingSort = wasSkipSort;
- }
}
/*!
@@ -1648,16 +1561,16 @@ QTreeWidgetItem::~QTreeWidgetItem()
*/
QTreeWidgetItem *QTreeWidgetItem::clone() const
{
- QTreeWidgetItem *copy = 0;
+ QTreeWidgetItem *copy = nullptr;
QStack<const QTreeWidgetItem*> stack;
QStack<QTreeWidgetItem*> parentStack;
stack.push(this);
parentStack.push(0);
- QTreeWidgetItem *root = 0;
- const QTreeWidgetItem *item = 0;
- QTreeWidgetItem *parent = 0;
+ QTreeWidgetItem *root = nullptr;
+ const QTreeWidgetItem *item = nullptr;
+ QTreeWidgetItem *parent = nullptr;
while (!stack.isEmpty()) {
// get current item, and copied parent
item = stack.pop();
@@ -1685,7 +1598,7 @@ QTreeWidgetItem *QTreeWidgetItem::clone() const
/*!
Sets the item indicator \a policy. This policy decides when the
tree branch expand/collapse indicator is shown.
- The default value is ShowForChildren.
+ The default value is DontShowIndicatorWhenChildless.
\sa childIndicatorPolicy()
*/
@@ -1738,7 +1651,7 @@ void QTreeWidgetItem::setFlags(Qt::ItemFlags flags)
parents.push(this);
while (!parents.isEmpty()) {
QTreeWidgetItem *parent = parents.pop();
- for (int i = 0; i < parent->children.count(); ++i) {
+ for (int i = 0; i < parent->children.size(); ++i) {
QTreeWidgetItem *child = parent->children.at(i);
if (!child->d->disabled) { // if not explicitly disabled
parents.push(child);
@@ -1767,7 +1680,7 @@ void QTreeWidgetItemPrivate::updateHiddenStatus(QTreeWidgetItem *item, bool inse
const QModelIndex index = model->index(parent, 0);
item->view->setRowHidden(index.row(), index.parent(), inserting);
}
- for (int i = 0; i < parent->children.count(); ++i) {
+ for (int i = 0; i < parent->children.size(); ++i) {
QTreeWidgetItem *child = parent->children.at(i);
parents.push(child);
}
@@ -1793,7 +1706,7 @@ void QTreeWidgetItemPrivate::propagateDisabled(QTreeWidgetItem *item)
parent->itemChanged();
}
- for (int i = 0; i < parent->children.count(); ++i) {
+ for (int i = 0; i < parent->children.size(); ++i) {
QTreeWidgetItem *child = parent->children.at(i);
parents.push(child);
}
@@ -1835,14 +1748,14 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
switch (role) {
case Qt::EditRole:
case Qt::DisplayRole: {
- if (values.count() <= column) {
+ if (values.size() <= column) {
if (model && this == model->headerItem)
model->setColumnCount(column + 1);
else
values.resize(column + 1);
}
- if (d->display.count() <= column) {
- for (int i = d->display.count() - 1; i < column - 1; ++i)
+ if (d->display.size() <= column) {
+ for (int i = d->display.size() - 1; i < column - 1; ++i)
d->display.append(QVariant());
d->display.append(value);
} else if (d->display[column] != value) {
@@ -1853,7 +1766,7 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
} break;
case Qt::CheckStateRole:
if ((itemFlags & Qt::ItemIsAutoTristate) && value != Qt::PartiallyChecked) {
- for (int i = 0; i < children.count(); ++i) {
+ for (int i = 0; i < children.size(); ++i) {
QTreeWidgetItem *child = children.at(i);
if (child->data(column, role).isValid()) {// has a CheckState
Qt::ItemFlags f = itemFlags; // a little hack to avoid multiple dataChanged signals
@@ -1865,10 +1778,10 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
}
Q_FALLTHROUGH();
default:
- if (column < values.count()) {
+ if (column < values.size()) {
bool found = false;
- const QVector<QWidgetItemData> column_values = values.at(column);
- for (int i = 0; i < column_values.count(); ++i) {
+ const QList<QWidgetItemData> column_values = values.at(column);
+ for (int i = 0; i < column_values.size(); ++i) {
if (column_values.at(i).role == role) {
if (column_values.at(i).value == value)
return; // value is unchanged
@@ -1889,9 +1802,9 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
}
if (model) {
- const QVector<int> roles((role == Qt::DisplayRole || role == Qt::EditRole) ?
- QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
- QVector<int>({role}));
+ const QList<int> roles((role == Qt::DisplayRole || role == Qt::EditRole)
+ ? QList<int>({ Qt::DisplayRole, Qt::EditRole })
+ : QList<int>({ role }));
model->emitDataChanged(this, column, roles);
if (role == Qt::CheckStateRole) {
QTreeWidgetItem *p;
@@ -1909,17 +1822,17 @@ QVariant QTreeWidgetItem::data(int column, int role) const
switch (role) {
case Qt::EditRole:
case Qt::DisplayRole:
- if (column >= 0 && column < d->display.count())
+ if (column >= 0 && column < d->display.size())
return d->display.at(column);
break;
case Qt::CheckStateRole:
// special case for check state in tristate
- if (children.count() && (itemFlags & Qt::ItemIsAutoTristate))
+ if (children.size() && (itemFlags & Qt::ItemIsAutoTristate))
return childrenCheckState(column);
Q_FALLTHROUGH();
default:
if (column >= 0 && column < values.size()) {
- const QVector<QWidgetItemData> &column_values = values.at(column);
+ const QList<QWidgetItemData> &column_values = values.at(column);
for (const auto &column_value : column_values) {
if (column_value.role == role)
return column_value.value;
@@ -1956,9 +1869,9 @@ void QTreeWidgetItem::read(QDataStream &in)
d->display.clear();
in >> values;
// move the display value over to the display string list
- for (int column = 0; column < values.count(); ++column) {
+ for (int column = 0; column < values.size(); ++column) {
d->display << QVariant();
- for (int i = 0; i < values.at(column).count(); ++i) {
+ for (int i = 0; i < values.at(column).size(); ++i) {
if (values.at(column).at(i).role == Qt::DisplayRole) {
d->display[column] = values.at(column).at(i).value;
values[column].remove(i--);
@@ -1992,8 +1905,9 @@ void QTreeWidgetItem::write(QDataStream &out) const
\sa data(), flags()
*/
QTreeWidgetItem::QTreeWidgetItem(const QTreeWidgetItem &other)
- : rtti(Type), values(other.values), view(0),
- d(new QTreeWidgetItemPrivate(this)), par(0),
+ : rtti(Type),
+ values(other.values),
+ d(new QTreeWidgetItemPrivate(this)),
itemFlags(other.itemFlags)
{
d->display = other.d->display;
@@ -2024,8 +1938,8 @@ QTreeWidgetItem &QTreeWidgetItem::operator=(const QTreeWidgetItem &other)
void QTreeWidgetItem::addChild(QTreeWidgetItem *child)
{
if (child) {
- insertChild(children.count(), child);
- child->d->rowGuess = children.count() - 1;
+ insertChild(children.size(), child);
+ child->d->rowGuess = children.size() - 1;
}
}
@@ -2036,14 +1950,13 @@ void QTreeWidgetItem::addChild(QTreeWidgetItem *child)
*/
void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child)
{
- if (index < 0 || index > children.count() || child == 0 || child->view != 0 || child->par != 0)
+ if (index < 0 || index > children.size() || child == nullptr || child->view != nullptr || child->par != nullptr)
return;
if (QTreeModel *model = treeModel()) {
- const bool wasSkipSort = model->skipPendingSort;
- model->skipPendingSort = true;
+ QTreeModel::SkipSorting skipSorting(model);
if (model->rootItem == this)
- child->par = 0;
+ child->par = nullptr;
else
child->par = this;
if (view->isSortingEnabled()) {
@@ -2059,13 +1972,12 @@ void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child)
QTreeWidgetItem *i = stack.pop();
i->view = view;
i->values.reserve(cols);
- for (int c = 0; c < i->children.count(); ++c)
+ for (int c = 0; c < i->children.size(); ++c)
stack.push(i->children.at(c));
}
children.insert(index, child);
d->updateHiddenStatus(child, true);
model->endInsertItems();
- model->skipPendingSort = wasSkipSort;
} else {
child->par = this;
children.insert(index, child);
@@ -2099,24 +2011,24 @@ QTreeWidgetItem *QTreeWidgetItem::takeChild(int index)
model->skipPendingSort = false;
model->executePendingSort();
}
- if (index >= 0 && index < children.count()) {
+ if (index >= 0 && index < children.size()) {
if (model) model->beginRemoveItems(this, index, 1);
d->updateHiddenStatus(children.at(index), false);
QTreeWidgetItem *item = children.takeAt(index);
- item->par = 0;
+ item->par = nullptr;
QStack<QTreeWidgetItem*> stack;
stack.push(item);
while (!stack.isEmpty()) {
QTreeWidgetItem *i = stack.pop();
- i->view = 0;
- for (int c = 0; c < i->children.count(); ++c)
+ i->view = nullptr;
+ for (int c = 0; c < i->children.size(); ++c)
stack.push(i->children.at(c));
}
d->propagateDisabled(item);
if (model) model->endRemoveRows();
return item;
}
- return 0;
+ return nullptr;
}
/*!
@@ -2128,7 +2040,7 @@ QTreeWidgetItem *QTreeWidgetItem::takeChild(int index)
*/
void QTreeWidgetItem::addChildren(const QList<QTreeWidgetItem*> &children)
{
- insertChildren(this->children.count(), children);
+ insertChildren(this->children.size(), children);
}
/*!
@@ -2140,18 +2052,18 @@ void QTreeWidgetItem::addChildren(const QList<QTreeWidgetItem*> &children)
*/
void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &children)
{
- if (index < 0 || index > this->children.count() || children.isEmpty())
+ if (index < 0 || index > this->children.size() || children.isEmpty())
return;
if (view && view->isSortingEnabled()) {
- for (int n = 0; n < children.count(); ++n)
+ for (int n = 0; n < children.size(); ++n)
insertChild(index, children.at(n));
return;
}
QTreeModel *model = treeModel();
QStack<QTreeWidgetItem*> stack;
QList<QTreeWidgetItem*> itemsToInsert;
- for (int n = 0; n < children.count(); ++n) {
+ for (int n = 0; n < children.size(); ++n) {
QTreeWidgetItem *child = children.at(n);
if (child->view || child->par)
continue;
@@ -2163,7 +2075,7 @@ void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &c
stack.push(child);
}
if (model && (model->rootItem == this))
- child->par = 0;
+ child->par = nullptr;
else
child->par = this;
}
@@ -2171,11 +2083,11 @@ void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &c
while (!stack.isEmpty()) {
QTreeWidgetItem *i = stack.pop();
i->view = view;
- for (int c = 0; c < i->children.count(); ++c)
+ for (int c = 0; c < i->children.size(); ++c)
stack.push(i->children.at(c));
}
- if (model) model->beginInsertItems(this, index, itemsToInsert.count());
- for (int n = 0; n < itemsToInsert.count(); ++n) {
+ if (model) model->beginInsertItems(this, index, itemsToInsert.size());
+ for (int n = 0; n < itemsToInsert.size(); ++n) {
QTreeWidgetItem *child = itemsToInsert.at(n);
this->children.insert(index + n, child);
if (child->par)
@@ -2194,7 +2106,7 @@ void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &c
QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren()
{
QList<QTreeWidgetItem*> removed;
- if (children.count() > 0) {
+ if (children.size() > 0) {
QTreeModel *model = treeModel();
if (model) {
// This will trigger a layoutChanged signal, thus we might want to optimize
@@ -2203,16 +2115,16 @@ QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren()
// is updated in case we take an item that is selected.
model->executePendingSort();
}
- if (model) model->beginRemoveItems(this, 0, children.count());
- for (int n = 0; n < children.count(); ++n) {
+ if (model) model->beginRemoveItems(this, 0, children.size());
+ for (int n = 0; n < children.size(); ++n) {
QTreeWidgetItem *item = children.at(n);
- item->par = 0;
+ item->par = nullptr;
QStack<QTreeWidgetItem*> stack;
stack.push(item);
while (!stack.isEmpty()) {
QTreeWidgetItem *i = stack.pop();
- i->view = 0;
- for (int c = 0; c < i->children.count(); ++c)
+ i->view = nullptr;
+ for (int c = 0; c < i->children.size(); ++c)
stack.push(i->children.at(c));
}
d->propagateDisabled(item);
@@ -2388,37 +2300,43 @@ QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item)
#endif // QT_NO_DATASTREAM
-void QTreeWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
+void QTreeWidgetPrivate::clearConnections()
+{
+ for (const QMetaObject::Connection &connection : connections)
+ QObject::disconnect(connection);
+}
+
+void QTreeWidgetPrivate::emitItemPressed(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemPressed(item(index), index.column());
}
-void QTreeWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemClicked(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemClicked(item(index), index.column());
}
-void QTreeWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemDoubleClicked(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemDoubleClicked(item(index), index.column());
}
-void QTreeWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemActivated(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemActivated(item(index), index.column());
}
-void QTreeWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemEntered(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemEntered(item(index), index.column());
}
-void QTreeWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemChanged(const QModelIndex &index)
{
Q_Q(QTreeWidget);
QTreeWidgetItem *indexItem = item(index);
@@ -2426,19 +2344,19 @@ void QTreeWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
emit q->itemChanged(indexItem, index.column());
}
-void QTreeWidgetPrivate::_q_emitItemExpanded(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemExpanded(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemExpanded(item(index));
}
-void QTreeWidgetPrivate::_q_emitItemCollapsed(const QModelIndex &index)
+void QTreeWidgetPrivate::emitItemCollapsed(const QModelIndex &index)
{
Q_Q(QTreeWidget);
emit q->itemCollapsed(item(index));
}
-void QTreeWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
+void QTreeWidgetPrivate::emitCurrentItemChanged(const QModelIndex &current,
const QModelIndex &previous)
{
Q_Q(QTreeWidget);
@@ -2447,7 +2365,7 @@ void QTreeWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
emit q->currentItemChanged(currentItem, previousItem);
}
-void QTreeWidgetPrivate::_q_sort()
+void QTreeWidgetPrivate::sort()
{
if (sortingEnabled) {
int column = header->sortIndicatorSection();
@@ -2456,19 +2374,19 @@ void QTreeWidgetPrivate::_q_sort()
}
}
-void QTreeWidgetPrivate::_q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
+void QTreeWidgetPrivate::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
Q_Q(QTreeWidget);
QModelIndexList indices = selected.indexes();
int i;
QTreeModel *m = treeModel();
- for (i = 0; i < indices.count(); ++i) {
+ for (i = 0; i < indices.size(); ++i) {
QTreeWidgetItem *item = m->item(indices.at(i));
item->d->selected = true;
}
indices = deselected.indexes();
- for (i = 0; i < indices.count(); ++i) {
+ for (i = 0; i < indices.size(); ++i) {
QTreeWidgetItem *item = m->item(indices.at(i));
item->d->selected = false;
}
@@ -2476,8 +2394,8 @@ void QTreeWidgetPrivate::_q_selectionChanged(const QItemSelection &selected, con
emit q->itemSelectionChanged();
}
-void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
- const QModelIndex &bottomRight)
+void QTreeWidgetPrivate::dataChanged(const QModelIndex &topLeft,
+ const QModelIndex &bottomRight)
{
if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()
&& !treeModel()->sortPendingTimer.isActive()) {
@@ -2535,7 +2453,7 @@ void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
whether sorting is enabled.
\sa QTreeWidgetItem, QTreeWidgetItemIterator, QTreeView,
- {Model/View Programming}, {Settings Editor Example}
+ {Model/View Programming}
*/
/*!
@@ -2596,9 +2514,6 @@ void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
This signal is emitted when the specified \a item is expanded so that
all of its children are displayed.
- \note This signal will not be emitted if an item changes its state when
- expandAll() is invoked.
-
\sa QTreeWidgetItem::isExpanded(), itemCollapsed(), expandItem()
*/
@@ -2660,31 +2575,34 @@ void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
QTreeWidget::QTreeWidget(QWidget *parent)
: QTreeView(*new QTreeWidgetPrivate(), parent)
{
+ Q_D(QTreeWidget);
QTreeView::setModel(new QTreeModel(1, this));
- connect(this, SIGNAL(pressed(QModelIndex)),
- SLOT(_q_emitItemPressed(QModelIndex)));
- connect(this, SIGNAL(clicked(QModelIndex)),
- SLOT(_q_emitItemClicked(QModelIndex)));
- connect(this, SIGNAL(doubleClicked(QModelIndex)),
- SLOT(_q_emitItemDoubleClicked(QModelIndex)));
- connect(this, SIGNAL(activated(QModelIndex)),
- SLOT(_q_emitItemActivated(QModelIndex)));
- connect(this, SIGNAL(entered(QModelIndex)),
- SLOT(_q_emitItemEntered(QModelIndex)));
- connect(this, SIGNAL(expanded(QModelIndex)),
- SLOT(_q_emitItemExpanded(QModelIndex)));
- connect(this, SIGNAL(collapsed(QModelIndex)),
- SLOT(_q_emitItemCollapsed(QModelIndex)));
- connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
- connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- this, SLOT(_q_emitItemChanged(QModelIndex)));
- connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- this, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
- connect(model(), SIGNAL(columnsRemoved(QModelIndex,int,int)),
- this, SLOT(_q_sort()));
- connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
- this, SLOT(_q_selectionChanged(QItemSelection,QItemSelection)));
+ d->connections = {
+ QObjectPrivate::connect(this, &QTreeWidget::pressed,
+ d, &QTreeWidgetPrivate::emitItemPressed),
+ QObjectPrivate::connect(this, &QTreeWidget::clicked,
+ d, &QTreeWidgetPrivate::emitItemClicked),
+ QObjectPrivate::connect(this, &QTreeWidget::doubleClicked,
+ d, &QTreeWidgetPrivate::emitItemDoubleClicked),
+ QObjectPrivate::connect(this, &QTreeWidget::activated,
+ d, &QTreeWidgetPrivate::emitItemActivated),
+ QObjectPrivate::connect(this, &QTreeWidget::entered,
+ d, &QTreeWidgetPrivate::emitItemEntered),
+ QObjectPrivate::connect(this, &QTreeWidget::expanded,
+ d, &QTreeWidgetPrivate::emitItemExpanded),
+ QObjectPrivate::connect(this, &QTreeWidget::collapsed,
+ d, &QTreeWidgetPrivate::emitItemCollapsed),
+ QObjectPrivate::connect(model(), &QAbstractItemModel::dataChanged,
+ d, &QTreeWidgetPrivate::emitItemChanged),
+ QObjectPrivate::connect(model(), &QAbstractItemModel::dataChanged,
+ d, &QTreeWidgetPrivate::dataChanged),
+ QObjectPrivate::connect(model(), &QAbstractItemModel::columnsRemoved,
+ d, &QTreeWidgetPrivate::sort),
+ QObjectPrivate::connect(selectionModel(), &QItemSelectionModel::currentChanged,
+ d, &QTreeWidgetPrivate::emitCurrentItemChanged),
+ QObjectPrivate::connect(selectionModel(), &QItemSelectionModel::selectionChanged,
+ d, &QTreeWidgetPrivate::selectionChanged)
+ };
header()->setSectionsClickable(false);
}
@@ -2694,10 +2612,12 @@ QTreeWidget::QTreeWidget(QWidget *parent)
QTreeWidget::~QTreeWidget()
{
+ Q_D(QTreeWidget);
+ d->clearConnections();
}
/*
- Retuns the number of header columns in the view.
+ Returns the number of header columns in the view.
\sa sortColumn(), currentColumn(), topLevelItemCount()
*/
@@ -2896,10 +2816,10 @@ void QTreeWidget::setHeaderItem(QTreeWidgetItem *item)
void QTreeWidget::setHeaderLabels(const QStringList &labels)
{
Q_D(QTreeWidget);
- if (columnCount() < labels.count())
- setColumnCount(labels.count());
+ if (columnCount() < labels.size())
+ setColumnCount(labels.size());
QTreeWidgetItem *item = d->treeModel()->headerItem;
- for (int i = 0; i < labels.count(); ++i)
+ for (int i = 0; i < labels.size(); ++i)
item->setText(i, labels.at(i));
}
@@ -3132,38 +3052,6 @@ void QTreeWidget::setItemWidget(QTreeWidgetItem *item, int column, QWidget *widg
QAbstractItemView::setIndexWidget(d->index(item, column), widget);
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- Returns \c true if the \a item is selected; otherwise returns \c false.
-
- \sa itemSelectionChanged()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::isSelected()} instead.
-*/
-bool QTreeWidget::isItemSelected(const QTreeWidgetItem *item) const
-{
- return ((item && item->treeWidget() == this) ? item->isSelected() : false);
-}
-
-/*!
- If \a select is true, the given \a item is selected; otherwise it is
- deselected.
-
- \sa itemSelectionChanged()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::setSelected()} instead.
-*/
-void QTreeWidget::setItemSelected(const QTreeWidgetItem *item, bool select)
-{
- if (item && item->treeWidget() == this)
- const_cast<QTreeWidgetItem*>(item)->setSelected(select);
-}
-#endif
-
/*!
Returns a list of all selected non-hidden items.
@@ -3174,14 +3062,12 @@ QList<QTreeWidgetItem*> QTreeWidget::selectedItems() const
Q_D(const QTreeWidget);
const QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QTreeWidgetItem*> items;
- items.reserve(indexes.count());
- QSet<QTreeWidgetItem *> seen;
- seen.reserve(indexes.count());
+ items.reserve(indexes.size());
+ QDuplicateTracker<QTreeWidgetItem *> seen(indexes.size());
for (const auto &index : indexes) {
QTreeWidgetItem *item = d->item(index);
- if (item->isHidden() || seen.contains(item))
+ if (item->isHidden() || seen.hasSeen(item))
continue;
- seen.insert(item);
items.append(item);
}
return items;
@@ -3203,99 +3089,6 @@ QList<QTreeWidgetItem*> QTreeWidget::findItems(const QString &text, Qt::MatchFla
return items;
}
-#if QT_DEPRECATED_SINCE(5, 13)
-/*!
- Returns \c true if the \a item is explicitly hidden, otherwise returns \c false.
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::isHidden()} instead.
-*/
-bool QTreeWidget::isItemHidden(const QTreeWidgetItem *item) const
-{
- return ((item && item->treeWidget() == this) ? item->isHidden() : false);
-}
-
-/*!
- Hides the given \a item if \a hide is true; otherwise shows the item.
-
- \sa itemChanged()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::setHidden()} instead.
-*/
-void QTreeWidget::setItemHidden(const QTreeWidgetItem *item, bool hide)
-{
- if (item && item->treeWidget() == this)
- const_cast<QTreeWidgetItem*>(item)->setHidden(hide);
-}
-
-/*!
- Returns \c true if the given \a item is open; otherwise returns \c false.
-
- \sa itemExpanded()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::isExpanded()} instead.
-*/
-bool QTreeWidget::isItemExpanded(const QTreeWidgetItem *item) const
-{
- return ((item && item->treeWidget() == this) ? item->isExpanded() : false);
-}
-
-/*!
- Sets the item referred to by \a item to either closed or opened,
- depending on the value of \a expand.
-
- \sa expandItem(), collapseItem(), itemExpanded()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::setExpanded()} instead.
-*/
-void QTreeWidget::setItemExpanded(const QTreeWidgetItem *item, bool expand)
-{
- if (item && item->treeWidget() == this)
- const_cast<QTreeWidgetItem*>(item)->setExpanded(expand);
-}
-
-/*!
- \since 4.3
-
- Returns \c true if the given \a item is set to show only one section over all columns;
- otherwise returns \c false.
-
- \sa setFirstItemColumnSpanned()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::isFirstColumnSpanned()} instead.
-*/
-bool QTreeWidget::isFirstItemColumnSpanned(const QTreeWidgetItem *item) const
-{
- return ((item && item->treeWidget() == this) ? item->isFirstColumnSpanned() : false);
-}
-
-/*!
- \since 4.3
-
- Sets the given \a item to only show one section for all columns if \a span is true;
- otherwise the item will show one section per column.
-
- \sa isFirstItemColumnSpanned()
-
- \obsolete
-
- This function is deprecated. Use \l{QTreeWidgetItem::setFirstColumnSpanned()} instead.
-*/
-void QTreeWidget::setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span)
-{
- if (item && item->treeWidget() == this)
- const_cast<QTreeWidgetItem*>(item)->setFirstColumnSpanned(span);
-}
-#endif
/*!
\since 4.3
@@ -3306,7 +3099,7 @@ QTreeWidgetItem *QTreeWidget::itemAbove(const QTreeWidgetItem *item) const
{
Q_D(const QTreeWidget);
if (item == d->treeModel()->headerItem)
- return 0;
+ return nullptr;
const QModelIndex index = d->index(item);
const QModelIndex above = indexAbove(index);
return d->item(above);
@@ -3321,7 +3114,7 @@ QTreeWidgetItem *QTreeWidget::itemBelow(const QTreeWidgetItem *item) const
{
Q_D(const QTreeWidget);
if (item == d->treeModel()->headerItem)
- return 0;
+ return nullptr;
const QModelIndex index = d->index(item);
const QModelIndex below = indexBelow(index);
return d->item(below);
@@ -3336,7 +3129,7 @@ void QTreeWidget::setSelectionModel(QItemSelectionModel *selectionModel)
QTreeView::setSelectionModel(selectionModel);
QItemSelection newSelection = selectionModel->selection();
if (!newSelection.isEmpty())
- d->_q_selectionChanged(newSelection, QItemSelection());
+ d->selectionChanged(newSelection, QItemSelection());
}
/*!
@@ -3412,11 +3205,7 @@ QStringList QTreeWidget::mimeTypes() const
If the list of items is empty, \nullptr is returned rather than a
serialized empty list.
*/
-#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem *> &items) const
-#else
-QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
-#endif
{
Q_D(const QTreeWidget);
if (d->treeModel()->cachedIndexes.isEmpty()) {
@@ -3424,14 +3213,14 @@ QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
for (const auto *item : items) {
if (Q_UNLIKELY(!item)) {
qWarning("QTreeWidget::mimeData: Null-item passed");
- return 0;
+ return nullptr;
}
- for (int c = 0; c < item->values.count(); ++c) {
+ for (int c = 0; c < item->values.size(); ++c) {
const QModelIndex index = indexFromItem(item, c);
if (Q_UNLIKELY(!index.isValid())) {
qWarning() << "QTreeWidget::mimeData: No index associated with item :" << item;
- return 0;
+ return nullptr;
}
indexes << index;
}
@@ -3470,18 +3259,6 @@ Qt::DropActions QTreeWidget::supportedDropActions() const
}
/*!
- \obsolete
- Returns an empty list
-
- \sa mimeData()
-*/
-QList<QTreeWidgetItem*> QTreeWidget::items(const QMimeData *data) const
-{
- Q_UNUSED(data);
- return QList<QTreeWidgetItem*>();
-}
-
-/*!
Returns the QModelIndex associated with the given \a item in the given \a column.
\note In Qt versions prior to 5.7, this function took a non-\c{const} \a item.
@@ -3494,17 +3271,6 @@ QModelIndex QTreeWidget::indexFromItem(const QTreeWidgetItem *item, int column)
return d->index(item, column);
}
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
-/*!
- \overload
- \internal
-*/
-QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem *item, int column) const
-{
- return indexFromItem(const_cast<const QTreeWidgetItem *>(item), column);
-}
-#endif
-
/*!
Returns a pointer to the QTreeWidgetItem associated with the given \a index.
@@ -3525,10 +3291,11 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
QModelIndex topIndex;
int col = -1;
int row = -1;
- if (d->dropOn(event, &row, &col, &topIndex)) {
+ // check whether a subclass has already accepted the event, ie. moved the data
+ if (!event->isAccepted() && d->dropOn(event, &row, &col, &topIndex)) {
const QList<QModelIndex> idxs = selectedIndexes();
QList<QPersistentModelIndex> indexes;
- const int indexesCount = idxs.count();
+ const int indexesCount = idxs.size();
indexes.reserve(indexesCount);
for (const auto &idx : idxs)
indexes.append(idx);
@@ -3551,7 +3318,7 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
}
// insert them back in at their new positions
- for (int i = 0; i < indexes.count(); ++i) {
+ for (int i = 0; i < indexes.size(); ++i) {
// Either at a specific point or appended
if (row == -1) {
if (topIndex.isValid()) {
@@ -3572,9 +3339,11 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
}
event->accept();
- // Don't want QAbstractItemView to delete it because it was "moved" we already did it
- event->setDropAction(Qt::CopyAction);
}
+ // either we or a subclass accepted the move event, so assume that the data was
+ // moved and that QAbstractItemView shouldn't remove the source when QDrag::exec returns
+ if (event->isAccepted())
+ d->dropEventMoved = true;
}
QTreeView::dropEvent(event);
@@ -3601,6 +3370,14 @@ bool QTreeWidget::event(QEvent *e)
return QTreeView::event(e);
}
+/*!
+ see QTBUG-94546
+*/
+void QTreeModelPrivate::executePendingOperations() const
+{
+ q_func()->executePendingSort();
+}
+
QT_END_NAMESPACE
#include "moc_qtreewidget.cpp"
diff --git a/src/widgets/itemviews/qtreewidget.h b/src/widgets/itemviews/qtreewidget.h
index b9543fb954..992f6cdb1c 100644
--- a/src/widgets/itemviews/qtreewidget.h
+++ b/src/widgets/itemviews/qtreewidget.h
@@ -1,50 +1,14 @@
-/****************************************************************************
-**
-** 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
#ifndef QTREEWIDGET_H
#define QTREEWIDGET_H
-#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qtreeview.h>
#include <QtWidgets/qtreewidgetitemiterator.h>
+#include <QtWidgets/qtwidgetsglobal.h>
+#include <QtCore/qlist.h>
#include <QtCore/qvariant.h>
-#include <QtCore/qvector.h>
QT_REQUIRE_CONFIG(treewidget);
@@ -113,7 +77,7 @@ public:
{ return data(column, Qt::StatusTipRole).toString(); }
inline void setStatusTip(int column, const QString &statusTip);
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
inline QString toolTip(int column) const
{ return data(column, Qt::ToolTipRole).toString(); }
inline void setToolTip(int column, const QString &toolTip);
@@ -129,41 +93,35 @@ public:
{ return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
inline void setFont(int column, const QFont &font);
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
inline int textAlignment(int column) const
{ return data(column, Qt::TextAlignmentRole).toInt(); }
+#else
+ inline Qt::Alignment textAlignment(int column) const
+ { return qvariant_cast<Qt::Alignment>(data(column, Qt::TextAlignmentRole)); }
+#endif
+#if QT_DEPRECATED_SINCE(6, 4)
+ QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
inline void setTextAlignment(int column, int alignment)
{ setData(column, Qt::TextAlignmentRole, alignment); }
-
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTreeWidgetItem::background() instead")
- inline QColor backgroundColor(int column) const
- { return qvariant_cast<QColor>(data(column, Qt::BackgroundRole)); }
- QT_DEPRECATED_X ("Use QTreeWidgetItem::setBackground() instead")
- inline void setBackgroundColor(int column, const QColor &color)
- { setData(column, Qt::BackgroundRole, color); }
+ inline void setTextAlignment(int column, Qt::AlignmentFlag alignment)
+ { setData(column, Qt::TextAlignmentRole, QVariant::fromValue(Qt::Alignment(alignment))); }
#endif
+ inline void setTextAlignment(int column, Qt::Alignment alignment)
+ { setData(column, Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
inline QBrush background(int column) const
{ return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
inline void setBackground(int column, const QBrush &brush)
{ setData(column, Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTreeWidgetItem::foreground() instead")
- inline QColor textColor(int column) const
- { return qvariant_cast<QColor>(data(column, Qt::ForegroundRole)); }
- QT_DEPRECATED_X ("Use QTreeWidgetItem::setForeground() instead")
- inline void setTextColor(int column, const QColor &color)
- { setData(column, Qt::ForegroundRole, color); }
-#endif
-
inline QBrush foreground(int column) const
{ return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); }
inline void setForeground(int column, const QBrush &brush)
{ setData(column, Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
inline Qt::CheckState checkState(int column) const
- { return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); }
+ { return qvariant_cast<Qt::CheckState>(data(column, Qt::CheckStateRole)); }
inline void setCheckState(int column, Qt::CheckState state)
{ setData(column, Qt::CheckStateRole, state); }
@@ -190,8 +148,8 @@ public:
executePendingSort();
return children.at(index);
}
- inline int childCount() const { return children.count(); }
- inline int columnCount() const { return values.count(); }
+ inline int childCount() const { return int(children.size()); }
+ inline int columnCount() const { return int(values.size()); }
inline int indexOfChild(QTreeWidgetItem *child) const;
void addChild(QTreeWidgetItem *child);
@@ -219,12 +177,15 @@ private:
int rtti;
// One item has a vector of column entries. Each column has a vector of (role, value) pairs.
- QVector< QVector<QWidgetItemData> > values;
- QTreeWidget *view;
+ QList<QList<QWidgetItemData>> values;
+ QTreeWidget *view = nullptr;
QTreeWidgetItemPrivate *d;
- QTreeWidgetItem *par;
+ QTreeWidgetItem *par = nullptr;
+ // ### Qt7: Move children to d-pointer and replace QList by a suitable data structure.
+ // to fix QTBUG-94546
QList<QTreeWidgetItem*> children;
- Qt::ItemFlags itemFlags;
+ Qt::ItemFlags itemFlags = Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled
+ | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
};
inline void QTreeWidgetItem::setText(int column, const QString &atext)
@@ -238,7 +199,7 @@ inline void QTreeWidgetItem::setStatusTip(int column, const QString &astatusTip)
{ setData(column, Qt::StatusTipRole, astatusTip); }
#endif
-#ifndef QT_NO_TOOLTIP
+#if QT_CONFIG(tooltip)
inline void QTreeWidgetItem::setToolTip(int column, const QString &atoolTip)
{ setData(column, Qt::ToolTipRole, atoolTip); }
#endif
@@ -252,7 +213,7 @@ inline void QTreeWidgetItem::setFont(int column, const QFont &afont)
{ setData(column, Qt::FontRole, afont); }
inline int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *achild) const
-{ executePendingSort(); return children.indexOf(achild); }
+{ executePendingSort(); return int(children.indexOf(achild)); }
#ifndef QT_NO_DATASTREAM
Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item);
@@ -315,36 +276,16 @@ public:
void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget);
inline void removeItemWidget(QTreeWidgetItem *item, int column);
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTreeWidgetItem::isSelected() instead")
- bool isItemSelected(const QTreeWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QTreeWidgetItem::setSelected() instead")
- void setItemSelected(const QTreeWidgetItem *item, bool select);
-#endif
QList<QTreeWidgetItem*> selectedItems() const;
QList<QTreeWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags,
int column = 0) const;
-#if QT_DEPRECATED_SINCE(5, 13)
- QT_DEPRECATED_X ("Use QTreeWidgetItem::isHidden() instead")
- bool isItemHidden(const QTreeWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QTreeWidgetItem::setHidden() instead")
- void setItemHidden(const QTreeWidgetItem *item, bool hide);
-
- QT_DEPRECATED_X ("Use QTreeWidgetItem::isExpanded() instead")
- bool isItemExpanded(const QTreeWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QTreeWidgetItem::setExpanded() instead")
- void setItemExpanded(const QTreeWidgetItem *item, bool expand);
-
- QT_DEPRECATED_X ("Use QTreeWidgetItem::isFirstColumnSpanned() instead")
- bool isFirstItemColumnSpanned(const QTreeWidgetItem *item) const;
- QT_DEPRECATED_X ("Use QTreeWidgetItem::setFirstColumnSpanned() instead")
- void setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span);
-#endif
-
QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const;
QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const;
+ QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const;
+ QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
+
void setSelectionModel(QItemSelectionModel *selectionModel) override;
public Q_SLOTS:
@@ -360,7 +301,6 @@ Q_SIGNALS:
void itemDoubleClicked(QTreeWidgetItem *item, int column);
void itemActivated(QTreeWidgetItem *item, int column);
void itemEntered(QTreeWidgetItem *item, int column);
- // ### Qt 6: add changed roles
void itemChanged(QTreeWidgetItem *item, int column);
void itemExpanded(QTreeWidgetItem *item);
void itemCollapsed(QTreeWidgetItem *item);
@@ -370,29 +310,11 @@ Q_SIGNALS:
protected:
bool event(QEvent *e) override;
virtual QStringList mimeTypes() const;
-#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
virtual QMimeData *mimeData(const QList<QTreeWidgetItem *> &items) const;
-#else
- virtual QMimeData *mimeData(const QList<QTreeWidgetItem*> items) const;
-#endif
virtual bool dropMimeData(QTreeWidgetItem *parent, int index,
const QMimeData *data, Qt::DropAction action);
virtual Qt::DropActions supportedDropActions() const;
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
-public:
-#else
-protected:
-#endif
- QList<QTreeWidgetItem*> items(const QMimeData *data) const;
-
- QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const;
-#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
- QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: remove
-#endif
- QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
-
-protected:
#if QT_CONFIG(draganddrop)
void dropEvent(QDropEvent *event) override;
#endif
@@ -401,19 +323,6 @@ private:
Q_DECLARE_PRIVATE(QTreeWidget)
Q_DISABLE_COPY(QTreeWidget)
-
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemExpanded(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitItemCollapsed(const QModelIndex &index))
- Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
- Q_PRIVATE_SLOT(d_func(), void _q_sort())
- Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
- Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
};
inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column)
diff --git a/src/widgets/itemviews/qtreewidget_p.h b/src/widgets/itemviews/qtreewidget_p.h
index 81e7e86203..53fdea3ca8 100644
--- a/src/widgets/itemviews/qtreewidget_p.h
+++ b/src/widgets/itemviews/qtreewidget_p.h
@@ -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
#ifndef QTREEWIDGET_P_H
#define QTREEWIDGET_P_H
@@ -59,6 +23,8 @@
#include <private/qtreeview_p.h>
#include <QtWidgets/qheaderview.h>
+#include <array>
+
QT_REQUIRE_CONFIG(treewidget);
QT_BEGIN_NAMESPACE
@@ -99,9 +65,7 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool clearItemData(const QModelIndex &index) override;
-#endif
QMap<int, QVariant> itemData(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
@@ -141,7 +105,7 @@ public:
protected:
QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = nullptr);
- void emitDataChanged(QTreeWidgetItem *item, int column, const QVector<int> &roles);
+ void emitDataChanged(QTreeWidgetItem *item, int column, const QList<int> &roles);
void beginInsertItems(QTreeWidgetItem *parent, int row, int count);
void endInsertItems();
void beginRemoveItems(QTreeWidgetItem *parent, int row, int count);
@@ -157,7 +121,7 @@ private:
QList<QTreeWidgetItemIterator*> iterators;
mutable QBasicTimer sortPendingTimer;
- mutable bool skipPendingSort; //while doing internal operation we don't care about sorting
+ mutable bool skipPendingSort = false; // no sorting during internal operations
bool inline executePendingSort() const;
bool isChanging() const;
@@ -169,9 +133,9 @@ public:
{
const QTreeModel * const model;
const bool previous;
- SkipSorting(const QTreeModel *m) : model(m), previous(model->skipPendingSort)
- { model->skipPendingSort = true; }
- ~SkipSorting() { model->skipPendingSort = previous; }
+ SkipSorting(const QTreeModel *m) : model(m), previous(model ? model->skipPendingSort : false)
+ { if (model) model->skipPendingSort = true; }
+ ~SkipSorting() { if (model) model->skipPendingSort = previous; }
};
friend struct SkipSorting;
};
@@ -183,6 +147,7 @@ QT_END_INCLUDE_NAMESPACE
class QTreeModelPrivate : public QAbstractItemModelPrivate
{
Q_DECLARE_PUBLIC(QTreeModel)
+ void executePendingOperations() const override;
};
class QTreeWidgetItemPrivate
@@ -223,26 +188,29 @@ class QTreeWidgetPrivate : public QTreeViewPrivate
Q_DECLARE_PUBLIC(QTreeWidget)
public:
QTreeWidgetPrivate() : QTreeViewPrivate(), explicitSortColumn(-1) {}
+ void clearConnections();
inline QTreeModel *treeModel() const { return qobject_cast<QTreeModel*>(model); }
inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const
{ return treeModel()->index(item, column); }
inline QTreeWidgetItem *item(const QModelIndex &index) const
{ return treeModel()->item(index); }
- void _q_emitItemPressed(const QModelIndex &index);
- void _q_emitItemClicked(const QModelIndex &index);
- void _q_emitItemDoubleClicked(const QModelIndex &index);
- void _q_emitItemActivated(const QModelIndex &index);
- void _q_emitItemEntered(const QModelIndex &index);
- void _q_emitItemChanged(const QModelIndex &index);
- void _q_emitItemExpanded(const QModelIndex &index);
- void _q_emitItemCollapsed(const QModelIndex &index);
- void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index);
- void _q_sort();
- void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
- void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
+ void emitItemPressed(const QModelIndex &index);
+ void emitItemClicked(const QModelIndex &index);
+ void emitItemDoubleClicked(const QModelIndex &index);
+ void emitItemActivated(const QModelIndex &index);
+ void emitItemEntered(const QModelIndex &index);
+ void emitItemChanged(const QModelIndex &index);
+ void emitItemExpanded(const QModelIndex &index);
+ void emitItemCollapsed(const QModelIndex &index);
+ void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index);
+ void sort();
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+ void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
// used by QTreeWidgetItem::sortChildren to make sure the column argument is used
int explicitSortColumn;
+
+ std::array<QMetaObject::Connection, 12> connections;
};
QT_END_NAMESPACE
diff --git a/src/widgets/itemviews/qtreewidgetitemiterator.cpp b/src/widgets/itemviews/qtreewidgetitemiterator.cpp
index 14c19fcb9c..1dd677e209 100644
--- a/src/widgets/itemviews/qtreewidgetitemiterator.cpp
+++ b/src/widgets/itemviews/qtreewidgetitemiterator.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 <private/qtreewidgetitemiterator_p.h>
#include "qtreewidget.h"
@@ -90,7 +54,7 @@ QTreeWidgetItemIterator::QTreeWidgetItemIterator(const QTreeWidgetItemIterator &
*/
QTreeWidgetItemIterator::QTreeWidgetItemIterator(QTreeWidget *widget, IteratorFlags flags)
-: current(0), flags(flags)
+: current(nullptr), flags(flags)
{
Q_ASSERT(widget);
QTreeModel *model = qobject_cast<QTreeModel*>(widget->model());
@@ -168,7 +132,7 @@ QTreeWidgetItemIterator &QTreeWidgetItemIterator::operator=(const QTreeWidgetIte
}
/*!
- The prefix ++ operator (++it) advances the iterator to the next matching item
+ The prefix \c{++} operator (\c{++it}) advances the iterator to the next matching item
and returns a reference to the resulting iterator.
Sets the current pointer to \nullptr if the current item is the last matching item.
*/
@@ -183,7 +147,7 @@ QTreeWidgetItemIterator &QTreeWidgetItemIterator::operator++()
}
/*!
- The prefix -- operator (--it) advances the iterator to the previous matching item
+ The prefix \c{--} operator (\c{--it}) advances the iterator to the previous matching item
and returns a reference to the resulting iterator.
Sets the current pointer to \nullptr if the current item is the first matching item.
*/
@@ -266,7 +230,7 @@ bool QTreeWidgetItemIterator::matchesFlags(const QTreeWidgetItem *item) const
QTreeWidgetItem* QTreeWidgetItemIteratorPrivate::nextSibling(const QTreeWidgetItem* item) const
{
Q_ASSERT(item);
- QTreeWidgetItem *next = 0;
+ QTreeWidgetItem *next = nullptr;
if (QTreeWidgetItem *par = item->parent()) {
int i = par->indexOfChild(const_cast<QTreeWidgetItem*>(item));
next = par->child(i + 1);
@@ -280,9 +244,9 @@ QTreeWidgetItem* QTreeWidgetItemIteratorPrivate::nextSibling(const QTreeWidgetIt
QTreeWidgetItem *QTreeWidgetItemIteratorPrivate::next(const QTreeWidgetItem *current)
{
- if (!current) return 0;
+ if (!current) return nullptr;
- QTreeWidgetItem *next = 0;
+ QTreeWidgetItem *next = nullptr;
if (current->childCount()) {
// walk the child
m_parentIndex.push(m_currentIndex);
@@ -307,9 +271,9 @@ QTreeWidgetItem *QTreeWidgetItemIteratorPrivate::next(const QTreeWidgetItem *cur
QTreeWidgetItem *QTreeWidgetItemIteratorPrivate::previous(const QTreeWidgetItem *current)
{
- if (!current) return 0;
+ if (!current) return nullptr;
- QTreeWidgetItem *prev = 0;
+ QTreeWidgetItem *prev = nullptr;
// walk the previous sibling
QTreeWidgetItem *parent = current->parent();
prev = parent ? parent->child(m_currentIndex - 1)
@@ -347,7 +311,7 @@ void QTreeWidgetItemIteratorPrivate::ensureValidIterator(const QTreeWidgetItem *
// we need to adjust the iterator.
if (nextItem == itemToBeRemoved) {
QTreeWidgetItem *parent = nextItem;
- nextItem = 0;
+ nextItem = nullptr;
while (parent && !nextItem) {
nextItem = nextSibling(parent);
parent = parent->parent();
@@ -358,7 +322,7 @@ void QTreeWidgetItemIteratorPrivate::ensureValidIterator(const QTreeWidgetItem *
if (!(q->matchesFlags(nextItem))) ++(*q);
} else {
// set it to null.
- q->current = 0;
+ q->current = nullptr;
m_parentIndex.clear();
return;
}
diff --git a/src/widgets/itemviews/qtreewidgetitemiterator.h b/src/widgets/itemviews/qtreewidgetitemiterator.h
index 39e0244776..5518b431fc 100644
--- a/src/widgets/itemviews/qtreewidgetitemiterator.h
+++ b/src/widgets/itemviews/qtreewidgetitemiterator.h
@@ -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
#ifndef QTREEWIDGETITEMITERATOR_H
#define QTREEWIDGETITEMITERATOR_H
diff --git a/src/widgets/itemviews/qtreewidgetitemiterator_p.h b/src/widgets/itemviews/qtreewidgetitemiterator_p.h
index e68f6103d3..c7a04a2f9b 100644
--- a/src/widgets/itemviews/qtreewidgetitemiterator_p.h
+++ b/src/widgets/itemviews/qtreewidgetitemiterator_p.h
@@ -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
#ifndef QTREEWIDGETITEMITERATOR_P_H
#define QTREEWIDGETITEMITERATOR_P_H
@@ -54,6 +18,7 @@
#include <QtCore/qstack.h>
#include "qtreewidgetitemiterator.h"
+#include "private/qglobal_p.h"
#if QT_CONFIG(treewidget)
QT_BEGIN_NAMESPACE
diff --git a/src/widgets/itemviews/qwidgetitemdata_p.h b/src/widgets/itemviews/qwidgetitemdata_p.h
index b7ed6ed862..f8c2d47085 100644
--- a/src/widgets/itemviews/qwidgetitemdata_p.h
+++ b/src/widgets/itemviews/qwidgetitemdata_p.h
@@ -1,47 +1,12 @@
-/****************************************************************************
-**
-** 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
#ifndef QWIDGETITEMDATA_P_H
#define QWIDGETITEMDATA_P_H
#include <QtWidgets/private/qtwidgetsglobal_p.h>
#include <QtCore/qdatastream.h>
+#include <QtCore/qvariant.h>
//
// W A R N I N G
@@ -65,7 +30,7 @@ public:
QVariant value;
inline bool operator==(const QWidgetItemData &other) const { return role == other.role && value == other.value; }
};
-Q_DECLARE_TYPEINFO(QWidgetItemData, Q_MOVABLE_TYPE);
+Q_DECLARE_TYPEINFO(QWidgetItemData, Q_RELOCATABLE_TYPE);
#ifndef QT_NO_DATASTREAM