diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-04-10 15:19:11 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-04-10 18:25:28 +0000 |
commit | cea175fca9e07d4b8bbb47508c49a8dac554797b (patch) | |
tree | abc6bb31a43047be8a7bf84b6c0ddd4aa5674231 /src/quicktemplates2/qquickswipeview.cpp | |
parent | 26f0a9d599baa1093e8ed26111fea7407e6c3f09 (diff) |
SwipeView: add contentWidth and contentHeight
[ChangeLog][Controls][SwipeView] Added contentWidth and contentHeight
properties.
Change-Id: I9c37583cb5fcfb1af2d98d5d3753277e17e82608
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickswipeview.cpp')
-rw-r--r-- | src/quicktemplates2/qquickswipeview.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/quicktemplates2/qquickswipeview.cpp b/src/quicktemplates2/qquickswipeview.cpp index 60bc947785..b1ca36bc71 100644 --- a/src/quicktemplates2/qquickswipeview.cpp +++ b/src/quicktemplates2/qquickswipeview.cpp @@ -110,8 +110,13 @@ class QQuickSwipeViewPrivate : public QQuickContainerPrivate public: QQuickSwipeViewPrivate() : interactive(true), + hasContentWidth(false), + hasContentHeight(false), + contentWidth(0), + contentHeight(0), orientation(Qt::Horizontal) { + changeTypes |= ImplicitWidth | ImplicitHeight; } void resizeItem(QQuickItem *item); @@ -119,7 +124,21 @@ public: static QQuickSwipeViewPrivate *get(QQuickSwipeView *view); + void itemImplicitWidthChanged(QQuickItem *item) override; + void itemImplicitHeightChanged(QQuickItem *item) override; + + qreal getContentWidth() const; + qreal getContentHeight() const; + + void updateContentWidth(); + void updateContentHeight(); + void updateContentSize(); + bool interactive; + bool hasContentWidth; + bool hasContentHeight; + qreal contentWidth; + qreal contentHeight; Qt::Orientation orientation; }; @@ -177,11 +196,92 @@ QQuickSwipeViewPrivate *QQuickSwipeViewPrivate::get(QQuickSwipeView *view) return view->d_func(); } +void QQuickSwipeViewPrivate::itemImplicitWidthChanged(QQuickItem *item) +{ + Q_Q(QQuickSwipeView); + if (item == q->currentItem()) + updateContentWidth(); +} + +void QQuickSwipeViewPrivate::itemImplicitHeightChanged(QQuickItem *item) +{ + Q_Q(QQuickSwipeView); + if (item == q->currentItem()) + updateContentHeight(); +} + +qreal QQuickSwipeViewPrivate::getContentWidth() const +{ + Q_Q(const QQuickSwipeView); + QQuickItem *currentItem = q->currentItem(); + return currentItem ? currentItem->implicitWidth() : 0; +} + +qreal QQuickSwipeViewPrivate::getContentHeight() const +{ + Q_Q(const QQuickSwipeView); + QQuickItem *currentItem = q->currentItem(); + return currentItem ? currentItem->implicitHeight() : 0; +} + +void QQuickSwipeViewPrivate::updateContentWidth() +{ + Q_Q(QQuickSwipeView); + if (hasContentWidth) + return; + + const qreal oldContentWidth = contentWidth; + contentWidth = getContentWidth(); + if (qFuzzyCompare(contentWidth, oldContentWidth)) + return; + + emit q->contentWidthChanged(); +} + +void QQuickSwipeViewPrivate::updateContentHeight() +{ + Q_Q(QQuickSwipeView); + if (hasContentHeight) + return; + + const qreal oldContentHeight = contentHeight; + contentHeight = getContentHeight(); + if (qFuzzyCompare(contentHeight, oldContentHeight)) + return; + + emit q->contentHeightChanged(); +} + +void QQuickSwipeViewPrivate::updateContentSize() +{ + Q_Q(QQuickSwipeView); + if (hasContentWidth && hasContentHeight) + return; + + const qreal oldContentWidth = contentWidth; + if (!hasContentWidth) + contentWidth = getContentWidth(); + + const qreal oldContentHeight = contentHeight; + if (!hasContentHeight) + contentHeight = getContentHeight(); + + const bool widthChanged = !qFuzzyCompare(contentWidth, oldContentWidth); + const bool heightChanged = !qFuzzyCompare(contentHeight, oldContentHeight); + + if (widthChanged) + emit q->contentWidthChanged(); + if (heightChanged) + emit q->contentHeightChanged(); +} + QQuickSwipeView::QQuickSwipeView(QQuickItem *parent) : QQuickContainer(*(new QQuickSwipeViewPrivate), parent) { + Q_D(QQuickSwipeView); setFlag(ItemIsFocusScope); setActiveFocusOnTab(true); + QObjectPrivate::connect(this, &QQuickContainer::currentItemChanged, d, &QQuickSwipeViewPrivate::updateContentSize); } /*! @@ -274,6 +374,85 @@ QQuickSwipeViewAttached *QQuickSwipeView::qmlAttachedProperties(QObject *object) return new QQuickSwipeViewAttached(object); } +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::SwipeView::contentWidth + + This property holds the content width. It is used for calculating the total + implicit width of the button box. + + Unless explicitly overridden, the content width is automatically calculated + based on the total implicit width of the buttons and the \l {Control::}{spacing} + of the button box. + + \sa contentHeight +*/ +qreal QQuickSwipeView::contentWidth() const +{ + Q_D(const QQuickSwipeView); + return d->contentWidth; +} + +void QQuickSwipeView::setContentWidth(qreal width) +{ + Q_D(QQuickSwipeView); + d->hasContentWidth = true; + if (qFuzzyCompare(d->contentWidth, width)) + return; + + d->contentWidth = width; + emit contentWidthChanged(); +} + +void QQuickSwipeView::resetContentWidth() +{ + Q_D(QQuickSwipeView); + if (!d->hasContentWidth) + return; + + d->hasContentWidth = false; + d->updateContentWidth(); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::SwipeView::contentHeight + + This property holds the content height. It is used for calculating the total + implicit height of the button box. + + Unless explicitly overridden, the content height is automatically calculated + based on the maximum implicit height of the buttons. + + \sa contentWidth +*/ +qreal QQuickSwipeView::contentHeight() const +{ + Q_D(const QQuickSwipeView); + return d->contentHeight; +} + +void QQuickSwipeView::setContentHeight(qreal height) +{ + Q_D(QQuickSwipeView); + d->hasContentHeight = true; + if (qFuzzyCompare(d->contentHeight, height)) + return; + + d->contentHeight = height; + emit contentHeightChanged(); +} + +void QQuickSwipeView::resetContentHeight() +{ + Q_D(QQuickSwipeView); + if (!d->hasContentHeight) + return; + + d->hasContentHeight = false; + d->updateContentHeight(); +} + void QQuickSwipeView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { Q_D(QQuickSwipeView); |