From 89495f409fcb04f5eeb1c1e06096fcb54065c969 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 2 May 2018 13:54:08 +0200 Subject: Label: add support for background insets Same as 5adce042 for QQuickControl. [ChangeLog][Controls][Label] Added topInset, bottomInset, leftInset, and rightInset properties to control the geometry of the background similarly to how paddings control the geometry of the contentItem. Change-Id: I853efcdd6be1d3acfb067128b6195c253350de8d Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquicklabel.cpp | 216 ++++++++++++++++++++++++++++++--- src/quicktemplates2/qquicklabel_p.h | 25 ++++ src/quicktemplates2/qquicklabel_p_p.h | 23 ++++ tests/auto/controls/data/tst_label.qml | 152 +++++++++++++++++++++++ 4 files changed, 402 insertions(+), 14 deletions(-) diff --git a/src/quicktemplates2/qquicklabel.cpp b/src/quicktemplates2/qquicklabel.cpp index dc35f6b8..a798d4e9 100644 --- a/src/quicktemplates2/qquicklabel.cpp +++ b/src/quicktemplates2/qquicklabel.cpp @@ -94,20 +94,74 @@ QQuickLabelPrivate::~QQuickLabelPrivate() #endif } -void QQuickLabelPrivate::resizeBackground() +void QQuickLabelPrivate::setTopInset(qreal value, bool reset) { Q_Q(QQuickLabel); - if (background) { - QQuickItemPrivate *p = QQuickItemPrivate::get(background); - if (!p->widthValid) { - background->setWidth(q->width()); - p->widthValid = false; - } - if (!p->heightValid) { - background->setHeight(q->height()); - p->heightValid = false; - } + const QMarginsF oldInset = getInset(); + extra.value().topInset = value; + extra.value().hasTopInset = !reset; + if (!qFuzzyCompare(oldInset.top(), value)) { + emit q->topInsetChanged(); + q->insetChange(getInset(), oldInset); + } +} + +void QQuickLabelPrivate::setLeftInset(qreal value, bool reset) +{ + Q_Q(QQuickLabel); + const QMarginsF oldInset = getInset(); + extra.value().leftInset = value; + extra.value().hasLeftInset = !reset; + if (!qFuzzyCompare(oldInset.left(), value)) { + emit q->leftInsetChanged(); + q->insetChange(getInset(), oldInset); + } +} + +void QQuickLabelPrivate::setRightInset(qreal value, bool reset) +{ + Q_Q(QQuickLabel); + const QMarginsF oldInset = getInset(); + extra.value().rightInset = value; + extra.value().hasRightInset = !reset; + if (!qFuzzyCompare(oldInset.right(), value)) { + emit q->rightInsetChanged(); + q->insetChange(getInset(), oldInset); + } +} + +void QQuickLabelPrivate::setBottomInset(qreal value, bool reset) +{ + Q_Q(QQuickLabel); + const QMarginsF oldInset = getInset(); + extra.value().bottomInset = value; + extra.value().hasBottomInset = !reset; + if (!qFuzzyCompare(oldInset.bottom(), value)) { + emit q->bottomInsetChanged(); + q->insetChange(getInset(), oldInset); + } +} + +void QQuickLabelPrivate::resizeBackground() +{ + if (!background) + return; + + resizingBackground = true; + + QQuickItemPrivate *p = QQuickItemPrivate::get(background); + if (((!p->widthValid || !extra.isAllocated() || !extra->hasBackgroundWidth) && qFuzzyIsNull(background->x())) + || (extra.isAllocated() && (extra->hasLeftInset || extra->hasRightInset))) { + background->setX(getLeftInset()); + background->setWidth(width - getLeftInset() - getRightInset()); } + if (((!p->heightValid || !extra.isAllocated() || !extra->hasBackgroundHeight) && qFuzzyIsNull(background->y())) + || (extra.isAllocated() && (extra->hasTopInset || extra->hasBottomInset))) { + background->setY(getTopInset()); + background->setHeight(height - getTopInset() - getBottomInset()); + } + + resizingBackground = false; } /*! @@ -239,6 +293,18 @@ void QQuickLabelPrivate::executeBackground(bool complete) quickCompleteDeferred(q, backgroundName(), background); } +void QQuickLabelPrivate::itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) +{ + Q_UNUSED(diff); + if (resizingBackground || item != background || !change.sizeChange()) + return; + + QQuickItemPrivate *p = QQuickItemPrivate::get(item); + extra.value().hasBackgroundWidth = p->widthValid; + extra.value().hasBackgroundHeight = p->heightValid; + resizeBackground(); +} + void QQuickLabelPrivate::itemImplicitWidthChanged(QQuickItem *item) { Q_Q(QQuickLabel); @@ -273,7 +339,7 @@ QQuickLabel::QQuickLabel(QQuickItem *parent) QQuickLabel::~QQuickLabel() { Q_D(QQuickLabel); - QQuickControlPrivate::removeImplicitSizeListener(d->background, d); + QQuickControlPrivate::removeImplicitSizeListener(d->background, d, QQuickControlPrivate::ImplicitSizeChanges | QQuickItemPrivate::Geometry); } QFont QQuickLabel::font() const @@ -322,7 +388,12 @@ void QQuickLabel::setBackground(QQuickItem *background) const qreal oldImplicitBackgroundWidth = implicitBackgroundWidth(); const qreal oldImplicitBackgroundHeight = implicitBackgroundHeight(); - QQuickControlPrivate::removeImplicitSizeListener(d->background, d); + if (d->extra.isAllocated()) { + d->extra.value().hasBackgroundWidth = false; + d->extra.value().hasBackgroundHeight = false; + } + + QQuickControlPrivate::removeImplicitSizeListener(d->background, d, QQuickControlPrivate::ImplicitSizeChanges | QQuickItemPrivate::Geometry); delete d->background; d->background = background; @@ -332,7 +403,12 @@ void QQuickLabel::setBackground(QQuickItem *background) background->setZ(-1); if (isComponentComplete()) d->resizeBackground(); - QQuickControlPrivate::addImplicitSizeListener(background, d); + QQuickItemPrivate *p = QQuickItemPrivate::get(background); + if (p->widthValid || p->heightValid) { + d->extra.value().hasBackgroundWidth = p->widthValid; + d->extra.value().hasBackgroundHeight = p->heightValid; + } + QQuickControlPrivate::addImplicitSizeListener(background, d, QQuickControlPrivate::ImplicitSizeChanges | QQuickItemPrivate::Geometry); } if (!qFuzzyCompare(oldImplicitBackgroundWidth, implicitBackgroundWidth())) @@ -413,6 +489,110 @@ qreal QQuickLabel::implicitBackgroundHeight() const return d->background->implicitHeight(); } +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::Label::topInset + + This property holds the top inset for the background. + + \sa {Control Layout}, bottomInset +*/ +qreal QQuickLabel::topInset() const +{ + Q_D(const QQuickLabel); + return d->getTopInset(); +} + +void QQuickLabel::setTopInset(qreal inset) +{ + Q_D(QQuickLabel); + d->setTopInset(inset); +} + +void QQuickLabel::resetTopInset() +{ + Q_D(QQuickLabel); + d->setTopInset(0, true); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::Label::leftInset + + This property holds the left inset for the background. + + \sa {Control Layout}, rightInset +*/ +qreal QQuickLabel::leftInset() const +{ + Q_D(const QQuickLabel); + return d->getLeftInset(); +} + +void QQuickLabel::setLeftInset(qreal inset) +{ + Q_D(QQuickLabel); + d->setLeftInset(inset); +} + +void QQuickLabel::resetLeftInset() +{ + Q_D(QQuickLabel); + d->setLeftInset(0, true); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::Label::rightInset + + This property holds the right inset for the background. + + \sa {Control Layout}, leftInset +*/ +qreal QQuickLabel::rightInset() const +{ + Q_D(const QQuickLabel); + return d->getRightInset(); +} + +void QQuickLabel::setRightInset(qreal inset) +{ + Q_D(QQuickLabel); + d->setRightInset(inset); +} + +void QQuickLabel::resetRightInset() +{ + Q_D(QQuickLabel); + d->setRightInset(0, true); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::Label::bottomInset + + This property holds the bottom inset for the background. + + \sa {Control Layout}, topInset +*/ +qreal QQuickLabel::bottomInset() const +{ + Q_D(const QQuickLabel); + return d->getBottomInset(); +} + +void QQuickLabel::setBottomInset(qreal inset) +{ + Q_D(QQuickLabel); + d->setBottomInset(inset); +} + +void QQuickLabel::resetBottomInset() +{ + Q_D(QQuickLabel); + d->setBottomInset(0, true); +} + void QQuickLabel::classBegin() { Q_D(QQuickLabel); @@ -460,4 +640,12 @@ void QQuickLabel::geometryChanged(const QRectF &newGeometry, const QRectF &oldGe d->resizeBackground(); } +void QQuickLabel::insetChange(const QMarginsF &newInset, const QMarginsF &oldInset) +{ + Q_D(QQuickLabel); + Q_UNUSED(newInset); + Q_UNUSED(oldInset); + d->resizeBackground(); +} + QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquicklabel_p.h b/src/quicktemplates2/qquicklabel_p.h index ad7f3272..89c9c77b 100644 --- a/src/quicktemplates2/qquicklabel_p.h +++ b/src/quicktemplates2/qquicklabel_p.h @@ -66,6 +66,10 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickLabel : public QQuickText // 2.5 (Qt 5.12) Q_PROPERTY(qreal implicitBackgroundWidth READ implicitBackgroundWidth NOTIFY implicitBackgroundWidthChanged FINAL REVISION 5) Q_PROPERTY(qreal implicitBackgroundHeight READ implicitBackgroundHeight NOTIFY implicitBackgroundHeightChanged FINAL REVISION 5) + Q_PROPERTY(qreal topInset READ topInset WRITE setTopInset RESET resetTopInset NOTIFY topInsetChanged FINAL REVISION 5) + Q_PROPERTY(qreal leftInset READ leftInset WRITE setLeftInset RESET resetLeftInset NOTIFY leftInsetChanged FINAL REVISION 5) + Q_PROPERTY(qreal rightInset READ rightInset WRITE setRightInset RESET resetRightInset NOTIFY rightInsetChanged FINAL REVISION 5) + Q_PROPERTY(qreal bottomInset READ bottomInset WRITE setBottomInset RESET resetBottomInset NOTIFY bottomInsetChanged FINAL REVISION 5) Q_CLASSINFO("DeferredPropertyNames", "background") public: @@ -87,6 +91,22 @@ public: qreal implicitBackgroundWidth() const; qreal implicitBackgroundHeight() const; + qreal topInset() const; + void setTopInset(qreal inset); + void resetTopInset(); + + qreal leftInset() const; + void setLeftInset(qreal inset); + void resetLeftInset(); + + qreal rightInset() const; + void setRightInset(qreal inset); + void resetRightInset(); + + qreal bottomInset() const; + void setBottomInset(qreal inset); + void resetBottomInset(); + Q_SIGNALS: void fontChanged(); void backgroundChanged(); @@ -95,6 +115,10 @@ Q_SIGNALS: // 2.5 (Qt 5.12) Q_REVISION(5) void implicitBackgroundWidthChanged(); Q_REVISION(5) void implicitBackgroundHeightChanged(); + Q_REVISION(5) void topInsetChanged(); + Q_REVISION(5) void leftInsetChanged(); + Q_REVISION(5) void rightInsetChanged(); + Q_REVISION(5) void bottomInsetChanged(); protected: void classBegin() override; @@ -102,6 +126,7 @@ protected: void itemChange(ItemChange change, const ItemChangeData &value) override; void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; + virtual void insetChange(const QMarginsF &newInset, const QMarginsF &oldInset); private: Q_DISABLE_COPY(QQuickLabel) diff --git a/src/quicktemplates2/qquicklabel_p_p.h b/src/quicktemplates2/qquicklabel_p_p.h index 00fea1d3..e1010698 100644 --- a/src/quicktemplates2/qquicklabel_p_p.h +++ b/src/quicktemplates2/qquicklabel_p_p.h @@ -75,6 +75,17 @@ public: return static_cast(QObjectPrivate::get(item)); } + inline QMarginsF getInset() const { return QMarginsF(getLeftInset(), getTopInset(), getRightInset(), getBottomInset()); } + inline qreal getTopInset() const { return extra.isAllocated() ? extra->topInset : 0; } + inline qreal getLeftInset() const { return extra.isAllocated() ? extra->leftInset : 0; } + inline qreal getRightInset() const { return extra.isAllocated() ? extra->rightInset : 0; } + inline qreal getBottomInset() const { return extra.isAllocated() ? extra->bottomInset : 0; } + + void setTopInset(qreal value, bool reset = false); + void setLeftInset(qreal value, bool reset = false); + void setRightInset(qreal value, bool reset = false); + void setBottomInset(qreal value, bool reset = false); + void resizeBackground(); void resolveFont(); @@ -105,16 +116,28 @@ public: void cancelBackground(); void executeBackground(bool complete = false); + 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; struct ExtraData { + bool hasTopInset = false; + bool hasLeftInset = false; + bool hasRightInset = false; + bool hasBottomInset = false; + bool hasBackgroundWidth = false; + bool hasBackgroundHeight = false; + qreal topInset = 0; + qreal leftInset = 0; + qreal rightInset = 0; + qreal bottomInset = 0; QFont requestedFont; QPalette requestedPalette; }; QLazilyAllocated extra; + bool resizingBackground = false; QPalette resolvedPalette; QQuickDeferredPointer background; }; diff --git a/tests/auto/controls/data/tst_label.qml b/tests/auto/controls/data/tst_label.qml index 69d273a7..8183f088 100644 --- a/tests/auto/controls/data/tst_label.qml +++ b/tests/auto/controls/data/tst_label.qml @@ -138,5 +138,157 @@ TestCase { control.background = rectangle.createObject(control) compare(control.background.width, control.width) compare(control.background.height, control.height) + + // change implicit size (QTBUG-66455) + control.background.implicitWidth = 160 + control.background.implicitHeight = 120 + compare(control.background.width, control.width) + compare(control.background.height, control.height) + } + + function test_inset() { + var control = createTemporaryObject(label, testCase, {background: rectangle.createObject(control)}) + verify(control) + + var topInsetSpy = createTemporaryObject(signalSpy, testCase, {target: control, signalName: "topInsetChanged"}) + verify(topInsetSpy.valid) + + var leftInsetSpy = createTemporaryObject(signalSpy, testCase, {target: control, signalName: "leftInsetChanged"}) + verify(leftInsetSpy.valid) + + var rightInsetSpy = createTemporaryObject(signalSpy, testCase, {target: control, signalName: "rightInsetChanged"}) + verify(rightInsetSpy.valid) + + var bottomInsetSpy = createTemporaryObject(signalSpy, testCase, {target: control, signalName: "bottomInsetChanged"}) + verify(bottomInsetSpy.valid) + + var topInsetChanges = 0 + var leftInsetChanges = 0 + var rightInsetChanges = 0 + var bottomInsetChanges = 0 + + compare(control.topInset, 0) + compare(control.leftInset, 0) + compare(control.rightInset, 0) + compare(control.bottomInset, 0) + + control.width = 100 + control.height = 100 + compare(control.background.x, 0) + compare(control.background.y, 0) + compare(control.background.width, 100) + compare(control.background.height, 100) + + control.topInset = 10 + compare(control.topInset, 10) + compare(control.leftInset, 0) + compare(control.rightInset, 0) + compare(control.bottomInset, 0) + compare(topInsetSpy.count, ++topInsetChanges) + compare(leftInsetSpy.count, leftInsetChanges) + compare(rightInsetSpy.count, rightInsetChanges) + compare(bottomInsetSpy.count, bottomInsetChanges) + compare(control.background.x, 0) + compare(control.background.y, 10) + compare(control.background.width, 100) + compare(control.background.height, 90) + + control.leftInset = 20 + compare(control.topInset, 10) + compare(control.leftInset, 20) + compare(control.rightInset, 0) + compare(control.bottomInset, 0) + compare(topInsetSpy.count, topInsetChanges) + compare(leftInsetSpy.count, ++leftInsetChanges) + compare(rightInsetSpy.count, rightInsetChanges) + compare(bottomInsetSpy.count, bottomInsetChanges) + compare(control.background.x, 20) + compare(control.background.y, 10) + compare(control.background.width, 80) + compare(control.background.height, 90) + + control.rightInset = 30 + compare(control.topInset, 10) + compare(control.leftInset, 20) + compare(control.rightInset, 30) + compare(control.bottomInset, 0) + compare(topInsetSpy.count, topInsetChanges) + compare(leftInsetSpy.count, leftInsetChanges) + compare(rightInsetSpy.count, ++rightInsetChanges) + compare(bottomInsetSpy.count, bottomInsetChanges) + compare(control.background.x, 20) + compare(control.background.y, 10) + compare(control.background.width, 50) + compare(control.background.height, 90) + + control.bottomInset = 40 + compare(control.topInset, 10) + compare(control.leftInset, 20) + compare(control.rightInset, 30) + compare(control.bottomInset, 40) + compare(topInsetSpy.count, topInsetChanges) + compare(leftInsetSpy.count, leftInsetChanges) + compare(rightInsetSpy.count, rightInsetChanges) + compare(bottomInsetSpy.count, ++bottomInsetChanges) + compare(control.background.x, 20) + compare(control.background.y, 10) + compare(control.background.width, 50) + compare(control.background.height, 50) + + control.topInset = undefined + compare(control.topInset, 0) + compare(control.leftInset, 20) + compare(control.rightInset, 30) + compare(control.bottomInset, 40) + compare(topInsetSpy.count, ++topInsetChanges) + compare(leftInsetSpy.count, leftInsetChanges) + compare(rightInsetSpy.count, rightInsetChanges) + compare(bottomInsetSpy.count, bottomInsetChanges) + compare(control.background.x, 20) + compare(control.background.y, 0) + compare(control.background.width, 50) + compare(control.background.height, 60) + + control.leftInset = undefined + compare(control.topInset, 0) + compare(control.leftInset, 0) + compare(control.rightInset, 30) + compare(control.bottomInset, 40) + compare(topInsetSpy.count, topInsetChanges) + compare(leftInsetSpy.count, ++leftInsetChanges) + compare(rightInsetSpy.count, rightInsetChanges) + compare(bottomInsetSpy.count, bottomInsetChanges) + compare(control.background.x, 0) + compare(control.background.y, 0) + compare(control.background.width, 70) + compare(control.background.height, 60) + + control.rightInset = undefined + compare(control.topInset, 0) + compare(control.leftInset, 0) + compare(control.rightInset, 0) + compare(control.bottomInset, 40) + compare(topInsetSpy.count, topInsetChanges) + compare(leftInsetSpy.count, leftInsetChanges) + compare(rightInsetSpy.count, ++rightInsetChanges) + compare(bottomInsetSpy.count, bottomInsetChanges) + compare(control.background.x, 0) + compare(control.background.y, 0) + compare(control.background.width, 100) + compare(control.background.height, 60) + + control.bottomInset = undefined + compare(control.topInset, 0) + compare(control.leftInset, 0) + compare(control.rightInset, 0) + compare(control.bottomInset, 0) + compare(topInsetSpy.count, topInsetChanges) + compare(leftInsetSpy.count, leftInsetChanges) + compare(rightInsetSpy.count, rightInsetChanges) + compare(bottomInsetSpy.count, ++bottomInsetChanges) + compare(control.background.x, 0) + compare(control.background.y, 0) + compare(control.background.width, 100) + compare(control.background.height, 100) } } -- cgit v1.2.3