aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickcontainer.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2018-04-10 15:52:01 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2018-04-11 11:12:57 +0000
commit086dc4316b1008453ac190bab414a1c46cde79ef (patch)
treedf469b5166244af0239451812a18b64f99c6443f /src/quicktemplates2/qquickcontainer.cpp
parent52990db1f07bdf8f236dcb9b2ac06480c39437f7 (diff)
Promote contentWidth and contentHeight to QQuickContainer
Now we have contentWidth and contentHeight promoted/unified to QQuickPane and QQuickContainer, and all relevant types inherit the properties from there. The next step is to promote read-only versions all the way up to the QQuickControl base class. Change-Id: Ic6ed5d7b7852b0c7faaa59b9a261c360bc63fb6a Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickcontainer.cpp')
-rw-r--r--src/quicktemplates2/qquickcontainer.cpp163
1 files changed, 160 insertions, 3 deletions
diff --git a/src/quicktemplates2/qquickcontainer.cpp b/src/quicktemplates2/qquickcontainer.cpp
index 048cfdfa..06c1b092 100644
--- a/src/quicktemplates2/qquickcontainer.cpp
+++ b/src/quicktemplates2/qquickcontainer.cpp
@@ -193,7 +193,11 @@ static QQuickItem *effectiveContentItem(QQuickItem *item)
}
QQuickContainerPrivate::QQuickContainerPrivate()
- : contentModel(nullptr),
+ : hasContentWidth(false),
+ hasContentHeight(false),
+ contentWidth(0),
+ contentHeight(0),
+ contentModel(nullptr),
currentIndex(-1),
updatingCurrent(false),
changeTypes(Destroyed | Parent | SiblingOrder)
@@ -379,6 +383,18 @@ void QQuickContainerPrivate::itemDestroyed(QQuickItem *item)
removeItem(index, item);
}
+void QQuickContainerPrivate::itemImplicitWidthChanged(QQuickItem *item)
+{
+ if (item == contentItem)
+ updateContentWidth();
+}
+
+void QQuickContainerPrivate::itemImplicitHeightChanged(QQuickItem *item)
+{
+ if (item == contentItem)
+ updateContentHeight();
+}
+
void QQuickContainerPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObject *obj)
{
QQuickContainer *q = static_cast<QQuickContainer *>(prop->object);
@@ -436,6 +452,67 @@ void QQuickContainerPrivate::contentChildren_clear(QQmlListProperty<QQuickItem>
return QQuickContainerPrivate::get(q)->contentModel->clear();
}
+qreal QQuickContainerPrivate::getContentWidth() const
+{
+ return contentItem ? contentItem->implicitWidth() : 0;
+}
+
+qreal QQuickContainerPrivate::getContentHeight() const
+{
+ return contentItem ? contentItem->implicitHeight() : 0;
+}
+
+void QQuickContainerPrivate::updateContentWidth()
+{
+ Q_Q(QQuickContainer);
+ if (hasContentWidth)
+ return;
+
+ const qreal oldContentWidth = contentWidth;
+ contentWidth = getContentWidth();
+ if (qFuzzyCompare(contentWidth, oldContentWidth))
+ return;
+
+ emit q->contentWidthChanged();
+}
+
+void QQuickContainerPrivate::updateContentHeight()
+{
+ Q_Q(QQuickContainer);
+ if (hasContentHeight)
+ return;
+
+ const qreal oldContentHeight = contentHeight;
+ contentHeight = getContentHeight();
+ if (qFuzzyCompare(contentHeight, oldContentHeight))
+ return;
+
+ emit q->contentHeightChanged();
+}
+
+void QQuickContainerPrivate::updateContentSize()
+{
+ Q_Q(QQuickContainer);
+ 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();
+}
+
QQuickContainer::QQuickContainer(QQuickItem *parent)
: QQuickControl(*(new QQuickContainerPrivate), parent)
{
@@ -751,6 +828,84 @@ QQuickItem *QQuickContainer::currentItem() const
return itemAt(d->currentIndex);
}
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Container::contentWidth
+
+ This property holds the content width. It is used for calculating the total
+ implicit width of the container.
+
+ Unless explicitly overridden, the content width is automatically calculated
+ based on the implicit width of the items in the container.
+
+ \sa contentHeight
+*/
+qreal QQuickContainer::contentWidth() const
+{
+ Q_D(const QQuickContainer);
+ return d->contentWidth;
+}
+
+void QQuickContainer::setContentWidth(qreal width)
+{
+ Q_D(QQuickContainer);
+ d->hasContentWidth = true;
+ if (qFuzzyCompare(d->contentWidth, width))
+ return;
+
+ d->contentWidth = width;
+ emit contentWidthChanged();
+}
+
+void QQuickContainer::resetContentWidth()
+{
+ Q_D(QQuickContainer);
+ if (!d->hasContentWidth)
+ return;
+
+ d->hasContentWidth = false;
+ d->updateContentWidth();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Container::contentHeight
+
+ This property holds the content height. It is used for calculating the total
+ implicit height of the container.
+
+ Unless explicitly overridden, the content height is automatically calculated
+ based on the implicit height of the items in the container.
+
+ \sa contentWidth
+*/
+qreal QQuickContainer::contentHeight() const
+{
+ Q_D(const QQuickContainer);
+ return d->contentHeight;
+}
+
+void QQuickContainer::setContentHeight(qreal height)
+{
+ Q_D(QQuickContainer);
+ d->hasContentHeight = true;
+ if (qFuzzyCompare(d->contentHeight, height))
+ return;
+
+ d->contentHeight = height;
+ emit contentHeightChanged();
+}
+
+void QQuickContainer::resetContentHeight()
+{
+ Q_D(QQuickContainer);
+ if (!d->hasContentHeight)
+ return;
+
+ d->hasContentHeight = false;
+ d->updateContentHeight();
+}
+
void QQuickContainer::componentComplete()
{
Q_D(QQuickContainer);
@@ -776,7 +931,7 @@ void QQuickContainer::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem
static const int slotIndex = metaObject()->indexOfSlot("_q_currentIndexChanged()");
if (oldItem) {
- QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::Children);
+ QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::Children | QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight);
QQuickItem *oldContentItem = effectiveContentItem(oldItem);
if (oldContentItem != oldItem)
QQuickItemPrivate::get(oldContentItem)->removeItemChangeListener(d, QQuickItemPrivate::Children);
@@ -787,7 +942,7 @@ void QQuickContainer::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem
}
if (newItem) {
- QQuickItemPrivate::get(newItem)->addItemChangeListener(d, QQuickItemPrivate::Children);
+ QQuickItemPrivate::get(newItem)->addItemChangeListener(d, QQuickItemPrivate::Children | QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight);
QQuickItem *newContentItem = effectiveContentItem(newItem);
if (newContentItem != newItem)
QQuickItemPrivate::get(newContentItem)->addItemChangeListener(d, QQuickItemPrivate::Children);
@@ -796,6 +951,8 @@ void QQuickContainer::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem
if (signalIndex != -1)
QMetaObject::connect(newItem, signalIndex, this, slotIndex);
}
+
+ d->updateContentSize();
}
bool QQuickContainer::isContent(QQuickItem *item) const