diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-03-15 16:34:20 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-04-05 13:59:44 +0000 |
commit | 92e75ae5ec628f0902b096d16cd2cc3c29d89e4c (patch) | |
tree | aa9a66d479bfee8973424d33191a073950bc03de /src/quicktemplates2 | |
parent | 3c4942f12fd33e91973cfa55aef8d675f4e32126 (diff) |
QQuickScrollView: inherit QQuickPane
This allows us to remove duplicate properties, and ScrollView gets
Pane's automatic content size calculation, which allows us to remove
the respective QML bindings.
Change-Id: I96ba98c12d0bf294f19b2c2b3617bfc88326bb41
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2')
-rw-r--r-- | src/quicktemplates2/qquickpane.cpp | 39 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpane_p.h | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpane_p_p.h | 5 | ||||
-rw-r--r-- | src/quicktemplates2/qquickscrollview.cpp | 126 | ||||
-rw-r--r-- | src/quicktemplates2/qquickscrollview_p.h | 21 |
5 files changed, 72 insertions, 120 deletions
diff --git a/src/quicktemplates2/qquickpane.cpp b/src/quicktemplates2/qquickpane.cpp index 47a96126..7721704a 100644 --- a/src/quicktemplates2/qquickpane.cpp +++ b/src/quicktemplates2/qquickpane.cpp @@ -118,6 +118,14 @@ QQuickPanePrivate::QQuickPanePrivate() { } +QList<QQuickItem *> QQuickPanePrivate::contentChildItems() const +{ + if (!contentItem) + return QList<QQuickItem *>(); + + return contentItem->childItems(); +} + QQuickItem *QQuickPanePrivate::getContentItem() { Q_Q(QQuickPane); @@ -164,7 +172,7 @@ void QQuickPanePrivate::itemDestroyed(QQuickItem *item) void QQuickPanePrivate::contentChildrenChange() { Q_Q(QQuickPane); - QQuickItem *newFirstChild = contentItem->childItems().value(0); + QQuickItem *newFirstChild = contentChildItems().value(0); if (newFirstChild != firstChild) { if (firstChild) removeImplicitSizeListener(firstChild); @@ -186,7 +194,7 @@ qreal QQuickPanePrivate::getContentWidth() const if (!qFuzzyIsNull(cw)) return cw; - const auto contentChildren = contentItem->childItems(); + const auto contentChildren = contentChildItems(); if (contentChildren.count() == 1) return contentChildren.first()->implicitWidth(); @@ -202,7 +210,7 @@ qreal QQuickPanePrivate::getContentHeight() const if (!qFuzzyIsNull(ch)) return ch; - const auto contentChildren = contentItem->childItems(); + const auto contentChildren = contentChildItems(); if (contentChildren.count() == 1) return contentChildren.first()->implicitHeight(); @@ -220,6 +228,7 @@ void QQuickPanePrivate::updateContentWidth() if (qFuzzyCompare(contentWidth, oldContentWidth)) return; + q->contentSizeChange(QSizeF(contentWidth, contentHeight), QSizeF(oldContentWidth, contentHeight)); emit q->contentWidthChanged(); } @@ -234,6 +243,7 @@ void QQuickPanePrivate::updateContentHeight() if (qFuzzyCompare(contentHeight, oldContentHeight)) return; + q->contentSizeChange(QSizeF(contentWidth, contentHeight), QSizeF(contentWidth, oldContentHeight)); emit q->contentHeightChanged(); } @@ -254,6 +264,9 @@ void QQuickPanePrivate::updateContentSize() const bool widthChanged = !qFuzzyCompare(contentWidth, oldContentWidth); const bool heightChanged = !qFuzzyCompare(contentHeight, oldContentHeight); + if (widthChanged || heightChanged) + q->contentSizeChange(QSizeF(contentWidth, contentHeight), QSizeF(oldContentWidth, oldContentHeight)); + if (widthChanged) emit q->contentWidthChanged(); if (heightChanged) @@ -310,7 +323,9 @@ void QQuickPane::setContentWidth(qreal width) if (qFuzzyCompare(d->contentWidth, width)) return; + const qreal oldWidth = d->contentWidth; d->contentWidth = width; + contentSizeChange(QSizeF(width, d->contentHeight), QSizeF(oldWidth, d->contentHeight)); emit contentWidthChanged(); } @@ -347,7 +362,9 @@ void QQuickPane::setContentHeight(qreal height) if (qFuzzyCompare(d->contentHeight, height)) return; + const qreal oldHeight = d->contentHeight; d->contentHeight = height; + contentSizeChange(QSizeF(d->contentWidth, height), QSizeF(d->contentWidth, oldHeight)); emit contentHeightChanged(); } @@ -421,21 +438,19 @@ void QQuickPane::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) QQuickControl::contentItemChange(newItem, oldItem); if (oldItem) { d->removeImplicitSizeListener(oldItem); - if (d->firstChild) { - d->removeImplicitSizeListener(d->firstChild); - d->firstChild = nullptr; - } QObjectPrivate::disconnect(oldItem, &QQuickItem::childrenChanged, d, &QQuickPanePrivate::contentChildrenChange); } if (newItem) { d->addImplicitSizeListener(newItem); - d->firstChild = newItem->childItems().value(0); - if (d->firstChild) - d->addImplicitSizeListener(d->firstChild); QObjectPrivate::connect(newItem, &QQuickItem::childrenChanged, d, &QQuickPanePrivate::contentChildrenChange); } - d->updateContentSize(); - emit contentChildrenChanged(); + d->contentChildrenChange(); +} + +void QQuickPane::contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize) +{ + Q_UNUSED(newSize) + Q_UNUSED(oldSize) } #if QT_CONFIG(accessibility) diff --git a/src/quicktemplates2/qquickpane_p.h b/src/quicktemplates2/qquickpane_p.h index 46615d6b..86b2c9f2 100644 --- a/src/quicktemplates2/qquickpane_p.h +++ b/src/quicktemplates2/qquickpane_p.h @@ -87,6 +87,7 @@ protected: void componentComplete() override; void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) override; + virtual void contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize); #if QT_CONFIG(accessibility) QAccessible::Role accessibleRole() const override; diff --git a/src/quicktemplates2/qquickpane_p_p.h b/src/quicktemplates2/qquickpane_p_p.h index 3c94810e..a78ce990 100644 --- a/src/quicktemplates2/qquickpane_p_p.h +++ b/src/quicktemplates2/qquickpane_p_p.h @@ -62,8 +62,9 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickPanePrivate : public QQuickControlP public: QQuickPanePrivate(); - QQmlListProperty<QObject> contentData(); - QQmlListProperty<QQuickItem> contentChildren(); + virtual QQmlListProperty<QObject> contentData(); + virtual QQmlListProperty<QQuickItem> contentChildren(); + virtual QList<QQuickItem *> contentChildItems() const; QQuickItem *getContentItem() override; diff --git a/src/quicktemplates2/qquickscrollview.cpp b/src/quicktemplates2/qquickscrollview.cpp index 1c81eb70..2b348272 100644 --- a/src/quicktemplates2/qquickscrollview.cpp +++ b/src/quicktemplates2/qquickscrollview.cpp @@ -35,7 +35,7 @@ ****************************************************************************/ #include "qquickscrollview_p.h" -#include "qquickcontrol_p_p.h" +#include "qquickpane_p_p.h" #include "qquickscrollbar_p_p.h" #include <QtQuick/private/qquickflickable_p.h> @@ -44,7 +44,7 @@ QT_BEGIN_NAMESPACE /*! \qmltype ScrollView - \inherits Control + \inherits Pane \instantiates QQuickScrollView \inqmlmodule QtQuick.Controls \since 5.9 @@ -101,15 +101,16 @@ QT_BEGIN_NAMESPACE {Focus Management in Qt Quick Controls 2} */ -class QQuickScrollViewPrivate : public QQuickControlPrivate +class QQuickScrollViewPrivate : public QQuickPanePrivate { Q_DECLARE_PUBLIC(QQuickScrollView) public: QQuickScrollViewPrivate(); - QQmlListProperty<QObject> contentData(); - QQmlListProperty<QQuickItem> contentChildren(); + QQmlListProperty<QObject> contentData() override; + QQmlListProperty<QQuickItem> contentChildren() override; + QList<QQuickItem *> contentChildItems() const override; QQuickItem *getContentItem() override; @@ -135,20 +136,26 @@ public: static void contentChildren_clear(QQmlListProperty<QQuickItem> *prop); bool wasTouched; - qreal contentWidth; - qreal contentHeight; QQuickFlickable *flickable; }; QQuickScrollViewPrivate::QQuickScrollViewPrivate() : wasTouched(false), - contentWidth(-1), - contentHeight(-1), flickable(nullptr) { + contentWidth = -1; + contentHeight = -1; wheelEnabled = true; } +QList<QQuickItem *> QQuickScrollViewPrivate::contentChildItems() const +{ + if (!flickable) + return QList<QQuickItem *>(); + + return flickable->contentItem()->childItems(); +} + QQuickItem *QQuickScrollViewPrivate::getContentItem() { if (!contentItem) @@ -178,7 +185,7 @@ bool QQuickScrollViewPrivate::setFlickable(QQuickFlickable *item, bool content) if (attached) QQuickScrollBarAttachedPrivate::get(attached)->setFlickable(nullptr); - QObject::disconnect(flickable->contentItem(), &QQuickItem::childrenChanged, q, &QQuickScrollView::contentChildrenChanged); + QObjectPrivate::disconnect(flickable->contentItem(), &QQuickItem::childrenChanged, this, &QQuickPanePrivate::contentChildrenChange); QObjectPrivate::disconnect(flickable, &QQuickFlickable::contentWidthChanged, this, &QQuickScrollViewPrivate::updateContentWidth); QObjectPrivate::disconnect(flickable, &QQuickFlickable::contentHeightChanged, this, &QQuickScrollViewPrivate::updateContentHeight); } @@ -189,19 +196,19 @@ bool QQuickScrollViewPrivate::setFlickable(QQuickFlickable *item, bool content) if (flickable) { flickable->installEventFilter(q); - if (contentWidth > 0) - item->setContentWidth(contentWidth); + if (hasContentWidth) + flickable->setContentWidth(contentWidth); else updateContentWidth(); - if (contentHeight > 0) - item->setContentHeight(contentHeight); + if (hasContentHeight) + flickable->setContentHeight(contentHeight); else updateContentHeight(); if (attached) QQuickScrollBarAttachedPrivate::get(attached)->setFlickable(flickable); - QObject::connect(flickable->contentItem(), &QQuickItem::childrenChanged, q, &QQuickScrollView::contentChildrenChanged); + QObjectPrivate::connect(flickable->contentItem(), &QQuickItem::childrenChanged, this, &QQuickPanePrivate::contentChildrenChange); QObjectPrivate::connect(flickable, &QQuickFlickable::contentWidthChanged, this, &QQuickScrollViewPrivate::updateContentWidth); QObjectPrivate::connect(flickable, &QQuickFlickable::contentHeightChanged, this, &QQuickScrollViewPrivate::updateContentHeight); } @@ -357,74 +364,13 @@ void QQuickScrollViewPrivate::contentChildren_clear(QQmlListProperty<QQuickItem> } QQuickScrollView::QQuickScrollView(QQuickItem *parent) - : QQuickControl(*(new QQuickScrollViewPrivate), parent) + : QQuickPane(*(new QQuickScrollViewPrivate), parent) { - setFlag(ItemIsFocusScope); setActiveFocusOnTab(true); setFiltersChildMouseEvents(true); } /*! - \qmlproperty real QtQuick.Controls::ScrollView::contentWidth - - This property holds the width of the scrollable content. - - If only a single item is used within a ScrollView, the content size is - automatically calculated based on the implicit size of its contained item. - - \sa contentHeight -*/ -qreal QQuickScrollView::contentWidth() const -{ - Q_D(const QQuickScrollView); - return d->contentWidth; -} - -void QQuickScrollView::setContentWidth(qreal width) -{ - Q_D(QQuickScrollView); - if (qFuzzyCompare(d->contentWidth, width)) - return; - - if (d->flickable) { - d->flickable->setContentWidth(width); - } else { - d->contentWidth = width; - emit contentWidthChanged(); - } -} - -/*! - \qmlproperty real QtQuick.Controls::ScrollView::contentHeight - - This property holds the height of the scrollable content. - - If only a single item is used within a ScrollView, the content size is - automatically calculated based on the implicit size of its contained item. - - \sa contentWidth -*/ -qreal QQuickScrollView::contentHeight() const -{ - Q_D(const QQuickScrollView); - return d->contentHeight; -} - -void QQuickScrollView::setContentHeight(qreal height) -{ - Q_D(QQuickScrollView); - if (qFuzzyCompare(d->contentHeight, height)) - return; - - if (d->flickable) { - d->flickable->setContentHeight(height); - } else { - d->contentHeight = height; - emit contentHeightChanged(); - } -} - -/*! \qmlproperty list<Object> QtQuick.Controls::ScrollView::contentData \default @@ -516,13 +462,13 @@ bool QQuickScrollView::eventFilter(QObject *object, QEvent *event) if (!d->wheelEnabled) return true; } - return QQuickControl::eventFilter(object, event); + return QQuickPane::eventFilter(object, event); } void QQuickScrollView::keyPressEvent(QKeyEvent *event) { Q_D(QQuickScrollView); - QQuickControl::keyPressEvent(event); + QQuickPane::keyPressEvent(event); switch (event->key()) { case Qt::Key_Up: if (QQuickScrollBar *vbar = d->verticalScrollBar()) { @@ -557,22 +503,26 @@ void QQuickScrollView::keyPressEvent(QKeyEvent *event) void QQuickScrollView::componentComplete() { Q_D(QQuickScrollView); - QQuickControl::componentComplete(); - if (!d->contentItem) { + QQuickPane::componentComplete(); + if (!d->contentItem) d->ensureFlickable(true); - } else { - if (d->contentWidth <= 0) - d->updateContentWidth(); - if (d->contentHeight <= 0) - d->updateContentHeight(); - } } void QQuickScrollView::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) { Q_D(QQuickScrollView); - QQuickControl::contentItemChange(newItem, oldItem); d->setFlickable(qobject_cast<QQuickFlickable *>(newItem), false); + QQuickPane::contentItemChange(newItem, oldItem); +} + +void QQuickScrollView::contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize) +{ + Q_D(QQuickScrollView); + QQuickPane::contentSizeChange(newSize, oldSize); + if (d->flickable) { + d->flickable->setContentWidth(newSize.width()); + d->flickable->setContentHeight(newSize.height()); + } } #if QT_CONFIG(accessibility) diff --git a/src/quicktemplates2/qquickscrollview_p.h b/src/quicktemplates2/qquickscrollview_p.h index 2e8f19db..2b8d260e 100644 --- a/src/quicktemplates2/qquickscrollview_p.h +++ b/src/quicktemplates2/qquickscrollview_p.h @@ -48,36 +48,20 @@ // We mean it. // -#include <QtQuickTemplates2/private/qquickcontrol_p.h> +#include <QtQuickTemplates2/private/qquickpane_p.h> #include <QtQml/qqmllist.h> QT_BEGIN_NAMESPACE class QQuickScrollViewPrivate; -class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickScrollView : public QQuickControl +class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickScrollView : public QQuickPane { Q_OBJECT - Q_PROPERTY(qreal contentWidth READ contentWidth WRITE setContentWidth NOTIFY contentWidthChanged FINAL) - Q_PROPERTY(qreal contentHeight READ contentHeight WRITE setContentHeight NOTIFY contentHeightChanged FINAL) - Q_PRIVATE_PROPERTY(QQuickScrollView::d_func(), QQmlListProperty<QObject> contentData READ contentData FINAL) - Q_PRIVATE_PROPERTY(QQuickScrollView::d_func(), QQmlListProperty<QQuickItem> contentChildren READ contentChildren NOTIFY contentChildrenChanged FINAL) - Q_CLASSINFO("DefaultProperty", "contentData") public: explicit QQuickScrollView(QQuickItem *parent = nullptr); - qreal contentWidth() const; - void setContentWidth(qreal width); - - qreal contentHeight() const; - void setContentHeight(qreal height); - -Q_SIGNALS: - void contentWidthChanged(); - void contentHeightChanged(); - void contentChildrenChanged(); - protected: bool childMouseEventFilter(QQuickItem *item, QEvent *event) override; bool eventFilter(QObject *object, QEvent *event) override; @@ -85,6 +69,7 @@ protected: void componentComplete() override; void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) override; + void contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize) override; #if QT_CONFIG(accessibility) QAccessible::Role accessibleRole() const override; |