From 174a1e96350e8b128e856e97392a9b57c8c08ebb Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 13 Jan 2018 21:19:31 +0100 Subject: QHeaderView: make use of LayoutChangeHint in _q_sectionsChanged() When _q_sectionsChanged() is called from e.g. QSortFilterProxyModel the LayoutChangeHint is set which can be used to avoid useless work in this function. Change-Id: I034db3fcc7a5f9ea7ebc0fa3ffd7429edb154eb7 Reviewed-by: Richard Moe Gustavsen --- src/widgets/itemviews/qheaderview.cpp | 66 +++++++++++++++++++++++++---------- src/widgets/itemviews/qheaderview.h | 9 +++-- src/widgets/itemviews/qheaderview_p.h | 8 +++-- 3 files changed, 61 insertions(+), 22 deletions(-) (limited to 'src/widgets/itemviews') diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index cd5961dc91..5cbf642802 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -361,9 +361,9 @@ void QHeaderView::setModel(QAbstractItemModel *model) 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_sectionsAboutToBeChanged())); + this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); QObject::disconnect(d->model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)), - this, SLOT(_q_sectionsChanged())); + 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))); @@ -372,16 +372,16 @@ void QHeaderView::setModel(QAbstractItemModel *model) 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_sectionsAboutToBeChanged())); + this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); QObject::disconnect(d->model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), - this, SLOT(_q_sectionsChanged())); + 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()), - this, SLOT(_q_sectionsAboutToBeChanged())); - QObject::disconnect(d->model, SIGNAL(layoutChanged()), - this, SLOT(_q_sectionsChanged())); + QObject::disconnect(d->model, SIGNAL(layoutAboutToBeChanged(QList,QAbstractItemModel::LayoutChangeHint)), + this, SLOT(_q_sectionsAboutToBeChanged(QList,QAbstractItemModel::LayoutChangeHint))); + QObject::disconnect(d->model, SIGNAL(layoutChanged(QList,QAbstractItemModel::LayoutChangeHint)), + this, SLOT(_q_sectionsChanged(QList,QAbstractItemModel::LayoutChangeHint))); } if (model && model != QAbstractItemModelPrivate::staticEmptyModel()) { @@ -393,9 +393,9 @@ void QHeaderView::setModel(QAbstractItemModel *model) 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_sectionsAboutToBeChanged())); + this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); QObject::connect(model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)), - this, SLOT(_q_sectionsChanged())); + this, SLOT(_q_sectionsMoved(QModelIndex,int,int,QModelIndex,int))); } else { QObject::connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int))); @@ -404,16 +404,16 @@ void QHeaderView::setModel(QAbstractItemModel *model) 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_sectionsAboutToBeChanged())); + this, SLOT(_q_sectionsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); QObject::connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), - this, SLOT(_q_sectionsChanged())); + 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()), - this, SLOT(_q_sectionsAboutToBeChanged())); - QObject::connect(model, SIGNAL(layoutChanged()), - this, SLOT(_q_sectionsChanged())); + QObject::connect(model, SIGNAL(layoutAboutToBeChanged(QList,QAbstractItemModel::LayoutChangeHint)), + this, SLOT(_q_sectionsAboutToBeChanged(QList,QAbstractItemModel::LayoutChangeHint))); + QObject::connect(model, SIGNAL(layoutChanged(QList,QAbstractItemModel::LayoutChangeHint)), + this, SLOT(_q_sectionsChanged(QList,QAbstractItemModel::LayoutChangeHint))); } d->state = QHeaderViewPrivate::NoClear; @@ -2150,8 +2150,33 @@ void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent, viewport->update(); } -void QHeaderViewPrivate::_q_sectionsAboutToBeChanged() +void QHeaderViewPrivate::_q_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(); +} + +void QHeaderViewPrivate::_q_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(); +} + +void QHeaderViewPrivate::_q_sectionsAboutToBeChanged(const QList &, + QAbstractItemModel::LayoutChangeHint hint) +{ + if ((hint == QAbstractItemModel::VerticalSortHint && orientation == Qt::Horizontal) || + (hint == QAbstractItemModel::HorizontalSortHint && orientation == Qt::Vertical)) + return; + //if there is no row/column we can't have mapping for columns //because no QModelIndex in the model would be valid // ### this is far from being bullet-proof and we would need a real system to @@ -2190,8 +2215,13 @@ void QHeaderViewPrivate::_q_sectionsAboutToBeChanged() } } -void QHeaderViewPrivate::_q_sectionsChanged() +void QHeaderViewPrivate::_q_sectionsChanged(const QList &, + QAbstractItemModel::LayoutChangeHint hint) { + if ((hint == QAbstractItemModel::VerticalSortHint && orientation == Qt::Horizontal) || + (hint == QAbstractItemModel::HorizontalSortHint && orientation == Qt::Vertical)) + return; + Q_Q(QHeaderView); viewport->update(); diff --git a/src/widgets/itemviews/qheaderview.h b/src/widgets/itemviews/qheaderview.h index cfe2d24453..c09081cd80 100644 --- a/src/widgets/itemviews/qheaderview.h +++ b/src/widgets/itemviews/qheaderview.h @@ -251,9 +251,14 @@ protected: 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_sectionsAboutToBeChanged()) - Q_PRIVATE_SLOT(d_func(), void _q_sectionsChanged()) + 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 &parents = QList(), + QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint)) + Q_PRIVATE_SLOT(d_func(), void _q_sectionsChanged(const QList &parents = QList(), + QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint)) 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 24dc3bf075..d9fc1baec5 100644 --- a/src/widgets/itemviews/qheaderview_p.h +++ b/src/widgets/itemviews/qheaderview_p.h @@ -120,8 +120,12 @@ public: void updateHiddenSections(int logicalFirst, int logicalLast); void resizeSections(QHeaderView::ResizeMode globalMode, bool useGlobalMode = false); void _q_sectionsRemoved(const QModelIndex &,int,int); - void _q_sectionsAboutToBeChanged(); - void _q_sectionsChanged(); + 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 &parents = QList(), + QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint); + void _q_sectionsChanged(const QList &parents = QList(), + QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint); bool isSectionSelected(int section) const; bool isFirstVisibleSection(int section) const; -- cgit v1.2.3