aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2018-04-10 15:19:11 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2018-04-10 18:25:28 +0000
commitcea175fca9e07d4b8bbb47508c49a8dac554797b (patch)
treeabc6bb31a43047be8a7bf84b6c0ddd4aa5674231
parent26f0a9d599baa1093e8ed26111fea7407e6c3f09 (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>
-rw-r--r--src/imports/controls/SwipeView.qml4
-rw-r--r--src/imports/controls/imagine/SwipeView.qml4
-rw-r--r--src/imports/controls/material/SwipeView.qml4
-rw-r--r--src/imports/templates/qtquicktemplates2plugin.cpp1
-rw-r--r--src/quicktemplates2/qquickswipeview.cpp179
-rw-r--r--src/quicktemplates2/qquickswipeview_p.h15
-rw-r--r--tests/auto/controls/data/tst_swipeview.qml31
7 files changed, 225 insertions, 13 deletions
diff --git a/src/imports/controls/SwipeView.qml b/src/imports/controls/SwipeView.qml
index e46c080e..ad8885d3 100644
--- a/src/imports/controls/SwipeView.qml
+++ b/src/imports/controls/SwipeView.qml
@@ -42,9 +42,9 @@ T.SwipeView {
id: control
implicitWidth: Math.max(background ? background.implicitWidth : 0,
- contentItem.implicitWidth + leftPadding + rightPadding)
+ contentWidth + leftPadding + rightPadding)
implicitHeight: Math.max(background ? background.implicitHeight : 0,
- contentItem.implicitHeight + topPadding + bottomPadding)
+ contentHeight + topPadding + bottomPadding)
contentItem: ListView {
model: control.contentModel
diff --git a/src/imports/controls/imagine/SwipeView.qml b/src/imports/controls/imagine/SwipeView.qml
index 6f7f2ada..982d8ae3 100644
--- a/src/imports/controls/imagine/SwipeView.qml
+++ b/src/imports/controls/imagine/SwipeView.qml
@@ -43,9 +43,9 @@ T.SwipeView {
id: control
implicitWidth: Math.max(background ? background.implicitWidth : 0,
- contentItem.implicitWidth + leftPadding + rightPadding)
+ contentWidth + leftPadding + rightPadding)
implicitHeight: Math.max(background ? background.implicitHeight : 0,
- contentItem.implicitHeight + topPadding + bottomPadding)
+ contentHeight + topPadding + bottomPadding)
topPadding: background ? background.topPadding : 0
leftPadding: background ? background.leftPadding : 0
diff --git a/src/imports/controls/material/SwipeView.qml b/src/imports/controls/material/SwipeView.qml
index eedb36e3..bbb3d9ee 100644
--- a/src/imports/controls/material/SwipeView.qml
+++ b/src/imports/controls/material/SwipeView.qml
@@ -42,9 +42,9 @@ T.SwipeView {
id: control
implicitWidth: Math.max(background ? background.implicitWidth : 0,
- contentItem.implicitWidth + leftPadding + rightPadding)
+ contentWidth + leftPadding + rightPadding)
implicitHeight: Math.max(background ? background.implicitHeight : 0,
- contentItem.implicitHeight + topPadding + bottomPadding)
+ contentHeight + topPadding + bottomPadding)
contentItem: ListView {
model: control.contentModel
diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp
index ace2b316..72e76b02 100644
--- a/src/imports/templates/qtquicktemplates2plugin.cpp
+++ b/src/imports/templates/qtquicktemplates2plugin.cpp
@@ -338,6 +338,7 @@ void QtQuickTemplates2Plugin::registerTypes(const char *uri)
qmlRegisterType<QQuickPopupAnchors>();
qmlRegisterType<QQuickRangeSlider, 5>(uri, 2, 5, "RangeSlider");
qmlRegisterType<QQuickSlider, 5>(uri, 2, 5, "Slider");
+ qmlRegisterType<QQuickSwipeView, 5>(uri, 2, 5, "SwipeView");
qmlRegisterType<QQuickTextArea, 5>(uri, 2, 5, "TextArea");
qmlRegisterType<QQuickTextField, 5>(uri, 2, 5, "TextField");
qmlRegisterType<QQuickToolTip, 5>(uri, 2, 5, "ToolTip");
diff --git a/src/quicktemplates2/qquickswipeview.cpp b/src/quicktemplates2/qquickswipeview.cpp
index 60bc9477..b1ca36bc 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);
diff --git a/src/quicktemplates2/qquickswipeview_p.h b/src/quicktemplates2/qquickswipeview_p.h
index 03f6cefa..f19ccce2 100644
--- a/src/quicktemplates2/qquickswipeview_p.h
+++ b/src/quicktemplates2/qquickswipeview_p.h
@@ -65,6 +65,9 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSwipeView : public QQuickContainer
// 2.3 (Qt 5.10)
Q_PROPERTY(bool horizontal READ isHorizontal NOTIFY orientationChanged FINAL REVISION 3)
Q_PROPERTY(bool vertical READ isVertical NOTIFY orientationChanged FINAL REVISION 3)
+ // 2.5 (Qt 5.12)
+ Q_PROPERTY(qreal contentWidth READ contentWidth WRITE setContentWidth RESET resetContentWidth NOTIFY contentWidthChanged FINAL REVISION 5)
+ Q_PROPERTY(qreal contentHeight READ contentHeight WRITE setContentHeight RESET resetContentHeight NOTIFY contentHeightChanged FINAL REVISION 5)
public:
explicit QQuickSwipeView(QQuickItem *parent = nullptr);
@@ -83,11 +86,23 @@ public:
bool isHorizontal() const;
bool isVertical() const;
+ // 2.5 (Qt 5.12)
+ qreal contentWidth() const;
+ void setContentWidth(qreal width);
+ void resetContentWidth();
+
+ qreal contentHeight() const;
+ void setContentHeight(qreal height);
+ void resetContentHeight();
+
Q_SIGNALS:
// 2.1 (Qt 5.8)
Q_REVISION(1) void interactiveChanged();
// 2.2 (Qt 5.9)
Q_REVISION(2) void orientationChanged();
+ // 2.5 (Qt 5.12)
+ Q_REVISION(5) void contentWidthChanged();
+ Q_REVISION(5) void contentHeightChanged();
protected:
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
diff --git a/tests/auto/controls/data/tst_swipeview.qml b/tests/auto/controls/data/tst_swipeview.qml
index 39311877..1f92bba4 100644
--- a/tests/auto/controls/data/tst_swipeview.qml
+++ b/tests/auto/controls/data/tst_swipeview.qml
@@ -85,43 +85,60 @@ TestCase {
compare(control.currentIndex, -1)
compare(control.currentItem, null)
- control.addItem(page.createObject(control, {text: "0"}))
+ var item0 = page.createObject(control, {text: "0"})
+ control.addItem(item0)
compare(control.count, 1)
compare(control.currentIndex, 0)
compare(control.currentItem.text, "0")
compare(currentItemChangedSpy.count, 1);
+ compare(control.contentWidth, item0.implicitWidth)
+ compare(control.contentHeight, item0.implicitHeight)
- control.addItem(page.createObject(control, {text: "1"}))
+ var item1 = page.createObject(control, {text: "11"})
+ control.addItem(item1)
compare(control.count, 2)
compare(control.currentIndex, 0)
compare(control.currentItem.text, "0")
compare(currentItemChangedSpy.count, 1);
+ compare(control.contentWidth, item0.implicitWidth)
+ compare(control.contentHeight, item0.implicitHeight)
- control.addItem(page.createObject(control, {text: "2"}))
+ var item2 = page.createObject(control, {text: "222"})
+ control.addItem(item2)
compare(control.count, 3)
compare(control.currentIndex, 0)
compare(control.currentItem.text, "0")
compare(currentItemChangedSpy.count, 1);
+ compare(control.contentWidth, item0.implicitWidth)
+ compare(control.contentHeight, item0.implicitHeight)
control.currentIndex = 1
compare(control.currentIndex, 1)
- compare(control.currentItem.text, "1")
+ compare(control.currentItem.text, "11")
compare(currentItemChangedSpy.count, 2);
+ compare(control.contentWidth, item1.implicitWidth)
+ compare(control.contentHeight, item1.implicitHeight)
control.currentIndex = 2
compare(control.currentIndex, 2)
- compare(control.currentItem.text, "2")
+ compare(control.currentItem.text, "222")
compare(currentItemChangedSpy.count, 3);
+ compare(control.contentWidth, item2.implicitWidth)
+ compare(control.contentHeight, item2.implicitHeight)
control.decrementCurrentIndex()
compare(control.currentIndex, 1)
- compare(control.currentItem.text, "1")
+ compare(control.currentItem.text, "11")
compare(currentItemChangedSpy.count, 4);
+ compare(control.contentWidth, item1.implicitWidth)
+ compare(control.contentHeight, item1.implicitHeight)
control.incrementCurrentIndex()
compare(control.currentIndex, 2)
- compare(control.currentItem.text, "2")
+ compare(control.currentItem.text, "222")
compare(currentItemChangedSpy.count, 5);
+ compare(control.contentWidth, item2.implicitWidth)
+ compare(control.contentHeight, item2.implicitHeight)
}
Component {