diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-05-02 14:04:53 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-05-02 13:43:20 +0000 |
commit | 6858d4e9873644c255b19d251e5522c7cf4b7160 (patch) | |
tree | 93d5c83e841f084aea0a67ddfab6897d4e9052ec /src/quicktemplates2 | |
parent | 89495f409fcb04f5eeb1c1e06096fcb54065c969 (diff) |
TextField: add support for background insets
Same as 5adce042 for QQuickControl.
[ChangeLog][Controls][TextField] 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: Ic1e4c15fd4b06a58f229b5156668c9a986f7bc3f
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2')
-rw-r--r-- | src/quicktemplates2/qquicktextfield.cpp | 215 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextfield_p.h | 26 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextfield_p_p.h | 23 |
3 files changed, 250 insertions, 14 deletions
diff --git a/src/quicktemplates2/qquicktextfield.cpp b/src/quicktemplates2/qquicktextfield.cpp index e2e57eff..df297936 100644 --- a/src/quicktemplates2/qquicktextfield.cpp +++ b/src/quicktemplates2/qquicktextfield.cpp @@ -133,20 +133,74 @@ QQuickTextFieldPrivate::~QQuickTextFieldPrivate() #endif } -void QQuickTextFieldPrivate::resizeBackground() +void QQuickTextFieldPrivate::setTopInset(qreal value, bool reset) { Q_Q(QQuickTextField); - if (background) { - QQuickItemPrivate *p = QQuickItemPrivate::get(background); - if (!p->widthValid && qFuzzyIsNull(background->x())) { - background->setWidth(q->width()); - p->widthValid = false; - } - if (!p->heightValid && qFuzzyIsNull(background->y())) { - 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 QQuickTextFieldPrivate::setLeftInset(qreal value, bool reset) +{ + Q_Q(QQuickTextField); + 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 QQuickTextFieldPrivate::setRightInset(qreal value, bool reset) +{ + Q_Q(QQuickTextField); + 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 QQuickTextFieldPrivate::setBottomInset(qreal value, bool reset) +{ + Q_Q(QQuickTextField); + 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 QQuickTextFieldPrivate::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; } /*! @@ -332,6 +386,18 @@ void QQuickTextFieldPrivate::executeBackground(bool complete) quickCompleteDeferred(q, backgroundName(), background); } +void QQuickTextFieldPrivate::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 QQuickTextFieldPrivate::itemImplicitWidthChanged(QQuickItem *item) { Q_Q(QQuickTextField); @@ -374,7 +440,7 @@ QQuickTextField::QQuickTextField(QQuickItem *parent) QQuickTextField::~QQuickTextField() { Q_D(QQuickTextField); - QQuickControlPrivate::removeImplicitSizeListener(d->background, d); + QQuickControlPrivate::removeImplicitSizeListener(d->background, d, QQuickControlPrivate::ImplicitSizeChanges | QQuickItemPrivate::Geometry); } QFont QQuickTextField::font() const @@ -421,7 +487,12 @@ void QQuickTextField::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; @@ -431,7 +502,12 @@ void QQuickTextField::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())) @@ -667,6 +743,110 @@ qreal QQuickTextField::implicitBackgroundHeight() const return d->background->implicitHeight(); } +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::TextField::topInset + + This property holds the top inset for the background. + + \sa {Control Layout}, bottomInset +*/ +qreal QQuickTextField::topInset() const +{ + Q_D(const QQuickTextField); + return d->getTopInset(); +} + +void QQuickTextField::setTopInset(qreal inset) +{ + Q_D(QQuickTextField); + d->setTopInset(inset); +} + +void QQuickTextField::resetTopInset() +{ + Q_D(QQuickTextField); + d->setTopInset(0, true); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::TextField::leftInset + + This property holds the left inset for the background. + + \sa {Control Layout}, rightInset +*/ +qreal QQuickTextField::leftInset() const +{ + Q_D(const QQuickTextField); + return d->getLeftInset(); +} + +void QQuickTextField::setLeftInset(qreal inset) +{ + Q_D(QQuickTextField); + d->setLeftInset(inset); +} + +void QQuickTextField::resetLeftInset() +{ + Q_D(QQuickTextField); + d->setLeftInset(0, true); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::TextField::rightInset + + This property holds the right inset for the background. + + \sa {Control Layout}, leftInset +*/ +qreal QQuickTextField::rightInset() const +{ + Q_D(const QQuickTextField); + return d->getRightInset(); +} + +void QQuickTextField::setRightInset(qreal inset) +{ + Q_D(QQuickTextField); + d->setRightInset(inset); +} + +void QQuickTextField::resetRightInset() +{ + Q_D(QQuickTextField); + d->setRightInset(0, true); +} + +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlproperty real QtQuick.Controls::TextField::bottomInset + + This property holds the bottom inset for the background. + + \sa {Control Layout}, topInset +*/ +qreal QQuickTextField::bottomInset() const +{ + Q_D(const QQuickTextField); + return d->getBottomInset(); +} + +void QQuickTextField::setBottomInset(qreal inset) +{ + Q_D(QQuickTextField); + d->setBottomInset(inset); +} + +void QQuickTextField::resetBottomInset() +{ + Q_D(QQuickTextField); + d->setBottomInset(0, true); +} + void QQuickTextField::componentComplete() { Q_D(QQuickTextField); @@ -714,6 +894,13 @@ void QQuickTextField::geometryChanged(const QRectF &newGeometry, const QRectF &o d->resizeBackground(); } +void QQuickTextField::insetChange(const QMarginsF &newInset, const QMarginsF &oldInset) +{ + Q_D(QQuickTextField); + Q_UNUSED(newInset); + Q_UNUSED(oldInset); + d->resizeBackground(); +} QSGNode *QQuickTextField::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) { QQuickDefaultClipNode *clipNode = static_cast<QQuickDefaultClipNode *>(oldNode); diff --git a/src/quicktemplates2/qquicktextfield_p.h b/src/quicktemplates2/qquicktextfield_p.h index b1f2c974..d0d25d70 100644 --- a/src/quicktemplates2/qquicktextfield_p.h +++ b/src/quicktemplates2/qquicktextfield_p.h @@ -76,6 +76,10 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickTextField : public QQuickTextInput Q_PROPERTY(QColor placeholderTextColor READ placeholderTextColor WRITE setPlaceholderTextColor NOTIFY placeholderTextColorChanged FINAL REVISION 5) 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: @@ -114,6 +118,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 implicitWidthChanged3(); @@ -133,6 +153,10 @@ Q_SIGNALS: Q_REVISION(5) void placeholderTextColorChanged(); 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; @@ -140,6 +164,8 @@ 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); + QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override; void focusInEvent(QFocusEvent *event) override; diff --git a/src/quicktemplates2/qquicktextfield_p_p.h b/src/quicktemplates2/qquicktextfield_p_p.h index c9ccc943..f4a8983d 100644 --- a/src/quicktemplates2/qquicktextfield_p_p.h +++ b/src/quicktemplates2/qquicktextfield_p_p.h @@ -76,6 +76,17 @@ public: static QQuickTextFieldPrivate *get(QQuickTextField *item) { return static_cast<QQuickTextFieldPrivate *>(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(); @@ -117,6 +128,7 @@ 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; @@ -127,11 +139,22 @@ public: #endif 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<ExtraData> extra; + bool resizingBackground = false; QPalette resolvedPalette; QQuickDeferredPointer<QQuickItem> background; QString placeholder; |