diff options
Diffstat (limited to 'src/quicktemplates2/qquickscrollindicator.cpp')
-rw-r--r-- | src/quicktemplates2/qquickscrollindicator.cpp | 72 |
1 files changed, 49 insertions, 23 deletions
diff --git a/src/quicktemplates2/qquickscrollindicator.cpp b/src/quicktemplates2/qquickscrollindicator.cpp index e858e7c1..347c2a68 100644 --- a/src/quicktemplates2/qquickscrollindicator.cpp +++ b/src/quicktemplates2/qquickscrollindicator.cpp @@ -126,13 +126,20 @@ QT_BEGIN_NAMESPACE \sa ScrollBar, {Customizing ScrollIndicator}, {Indicator Controls} */ +static const QQuickItemPrivate::ChangeTypes changeTypes = QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed; +static const QQuickItemPrivate::ChangeTypes horizontalChangeTypes = changeTypes | QQuickItemPrivate::ImplicitHeight; +static const QQuickItemPrivate::ChangeTypes verticalChangeTypes = changeTypes | QQuickItemPrivate::ImplicitWidth; + class QQuickScrollIndicatorPrivate : public QQuickControlPrivate { Q_DECLARE_PUBLIC(QQuickScrollIndicator) public: - QQuickScrollIndicatorPrivate() : size(0), position(0), - active(false), orientation(Qt::Vertical) + QQuickScrollIndicatorPrivate() + : size(0), + position(0), + active(false), + orientation(Qt::Vertical) { } @@ -164,18 +171,14 @@ void QQuickScrollIndicatorPrivate::resizeContent() } } -QQuickScrollIndicator::QQuickScrollIndicator(QQuickItem *parent) : - QQuickControl(*(new QQuickScrollIndicatorPrivate), parent) +QQuickScrollIndicator::QQuickScrollIndicator(QQuickItem *parent) + : QQuickControl(*(new QQuickScrollIndicatorPrivate), parent) { } QQuickScrollIndicatorAttached *QQuickScrollIndicator::qmlAttachedProperties(QObject *object) { - QQuickFlickable *flickable = qobject_cast<QQuickFlickable *>(object); - if (!flickable) - qmlInfo(object) << "ScrollIndicator must be attached to a Flickable"; - - return new QQuickScrollIndicatorAttached(flickable); + return new QQuickScrollIndicatorAttached(object); } /*! @@ -295,7 +298,12 @@ void QQuickScrollIndicator::setOrientation(Qt::Orientation orientation) class QQuickScrollIndicatorAttachedPrivate : public QObjectPrivate, public QQuickItemChangeListener { public: - QQuickScrollIndicatorAttachedPrivate(QQuickFlickable *flickable) : flickable(flickable), horizontal(nullptr), vertical(nullptr) { } + QQuickScrollIndicatorAttachedPrivate() + : flickable(nullptr), + horizontal(nullptr), + vertical(nullptr) + { + } void activateHorizontal(); void activateVertical(); @@ -304,6 +312,8 @@ public: void layoutVertical(bool move = true); void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override; + void itemImplicitWidthChanged(QQuickItem *item) override; + void itemImplicitHeightChanged(QQuickItem *item) override; void itemDestroyed(QQuickItem *item) override; QQuickFlickable *flickable; @@ -363,6 +373,18 @@ void QQuickScrollIndicatorAttachedPrivate::itemGeometryChanged(QQuickItem *item, } } +void QQuickScrollIndicatorAttachedPrivate::itemImplicitWidthChanged(QQuickItem *item) +{ + if (item == vertical) + layoutVertical(true); +} + +void QQuickScrollIndicatorAttachedPrivate::itemImplicitHeightChanged(QQuickItem *item) +{ + if (item == horizontal) + layoutHorizontal(true); +} + void QQuickScrollIndicatorAttachedPrivate::itemDestroyed(QQuickItem *item) { if (item == horizontal) @@ -371,14 +393,15 @@ void QQuickScrollIndicatorAttachedPrivate::itemDestroyed(QQuickItem *item) vertical = nullptr; } -QQuickScrollIndicatorAttached::QQuickScrollIndicatorAttached(QQuickFlickable *flickable) : - QObject(*(new QQuickScrollIndicatorAttachedPrivate(flickable)), flickable) +QQuickScrollIndicatorAttached::QQuickScrollIndicatorAttached(QObject *parent) + : QObject(*(new QQuickScrollIndicatorAttachedPrivate), parent) { Q_D(QQuickScrollIndicatorAttached); - if (flickable) { - QQuickItemPrivate *p = QQuickItemPrivate::get(flickable); - p->updateOrAddGeometryChangeListener(d, QQuickGeometryChange::Size); - } + d->flickable = qobject_cast<QQuickFlickable *>(parent); + if (d->flickable) + QQuickItemPrivate::get(d->flickable)->updateOrAddGeometryChangeListener(d, QQuickGeometryChange::Size); + else if (parent) + qmlWarning(parent) << "ScrollIndicator must be attached to a Flickable"; } QQuickScrollIndicatorAttached::~QQuickScrollIndicatorAttached() @@ -386,9 +409,12 @@ QQuickScrollIndicatorAttached::~QQuickScrollIndicatorAttached() Q_D(QQuickScrollIndicatorAttached); if (d->flickable) { if (d->horizontal) - QQuickItemPrivate::get(d->horizontal)->removeItemChangeListener(d, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed); + QQuickItemPrivate::get(d->horizontal)->removeItemChangeListener(d, horizontalChangeTypes); if (d->vertical) - QQuickItemPrivate::get(d->vertical)->removeItemChangeListener(d,QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed); + QQuickItemPrivate::get(d->vertical)->removeItemChangeListener(d,verticalChangeTypes); + // NOTE: Use removeItemChangeListener(Geometry) instead of updateOrRemoveGeometryChangeListener(Size). + // The latter doesn't remove the listener but only resets its types. Thus, it leaves behind a dangling + // pointer on destruction. QQuickItemPrivate::get(d->flickable)->removeItemChangeListener(d, QQuickItemPrivate::Geometry); } } @@ -420,7 +446,7 @@ void QQuickScrollIndicatorAttached::setHorizontal(QQuickScrollIndicator *horizon return; if (d->horizontal && d->flickable) { - QQuickItemPrivate::get(d->horizontal)->removeItemChangeListener(d, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed); + QQuickItemPrivate::get(d->horizontal)->removeItemChangeListener(d, horizontalChangeTypes); QObjectPrivate::disconnect(d->flickable, &QQuickFlickable::movingHorizontallyChanged, d, &QQuickScrollIndicatorAttachedPrivate::activateHorizontal); // TODO: export QQuickFlickableVisibleArea @@ -436,7 +462,7 @@ void QQuickScrollIndicatorAttached::setHorizontal(QQuickScrollIndicator *horizon horizontal->setParentItem(d->flickable); horizontal->setOrientation(Qt::Horizontal); - QQuickItemPrivate::get(horizontal)->addItemChangeListener(d, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed); + QQuickItemPrivate::get(horizontal)->addItemChangeListener(d, horizontalChangeTypes); QObjectPrivate::connect(d->flickable, &QQuickFlickable::movingHorizontallyChanged, d, &QQuickScrollIndicatorAttachedPrivate::activateHorizontal); // TODO: export QQuickFlickableVisibleArea @@ -478,7 +504,7 @@ void QQuickScrollIndicatorAttached::setVertical(QQuickScrollIndicator *vertical) return; if (d->vertical && d->flickable) { - QQuickItemPrivate::get(d->vertical)->removeItemChangeListener(d, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed); + QQuickItemPrivate::get(d->vertical)->removeItemChangeListener(d, verticalChangeTypes); QObjectPrivate::disconnect(d->flickable, &QQuickFlickable::movingVerticallyChanged, d, &QQuickScrollIndicatorAttachedPrivate::activateVertical); // TODO: export QQuickFlickableVisibleArea @@ -494,7 +520,7 @@ void QQuickScrollIndicatorAttached::setVertical(QQuickScrollIndicator *vertical) vertical->setParentItem(d->flickable); vertical->setOrientation(Qt::Vertical); - QQuickItemPrivate::get(vertical)->addItemChangeListener(d, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed); + QQuickItemPrivate::get(vertical)->addItemChangeListener(d, verticalChangeTypes); QObjectPrivate::connect(d->flickable, &QQuickFlickable::movingVerticallyChanged, d, &QQuickScrollIndicatorAttachedPrivate::activateVertical); // TODO: export QQuickFlickableVisibleArea @@ -509,7 +535,7 @@ void QQuickScrollIndicatorAttached::setVertical(QQuickScrollIndicator *vertical) emit verticalChanged(); } -#ifndef QT_NO_ACCESSIBILITY +#if QT_CONFIG(accessibility) QAccessible::Role QQuickScrollIndicator::accessibleRole() const { return QAccessible::Indicator; |