diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-12-13 16:10:51 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-12-14 18:06:58 +0000 |
commit | a60a28f8225709bfca338416630a0493853264e1 (patch) | |
tree | 4c3dc08c42db3d9dd67092bd8abc4da668d6ac06 /src | |
parent | 3e3c57188c868d2c3d434c2e3b876ac89b40ae2f (diff) |
SpinBox: use deferred execution
tst_controls::SpinBox::test_initialFocus() caught an issue that focus
was not transferred as expected when the creation of the content item
was deferred.
Task-number: QTBUG-50992
Change-Id: I6b9f5684ab7141fa4ebfe4c7fe3e32528553b96d
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/quicktemplates2/qquickspinbox.cpp | 85 | ||||
-rw-r--r-- | src/quicktemplates2/qquickspinbox_p.h | 3 |
2 files changed, 73 insertions, 15 deletions
diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp index 1989d768..d390f570 100644 --- a/src/quicktemplates2/qquickspinbox.cpp +++ b/src/quicktemplates2/qquickspinbox.cpp @@ -36,6 +36,7 @@ #include "qquickspinbox_p.h" #include "qquickcontrol_p_p.h" +#include "qquickdeferredexecute_p_p.h" #include <QtGui/qguiapplication.h> #include <QtGui/qstylehints.h> @@ -144,6 +145,8 @@ public: void handleRelease(const QPointF &point) override; void handleUngrab() override; + QQuickItem *getContentItem() override; + bool editable; int from; int to; @@ -159,6 +162,30 @@ public: Qt::InputMethodHints inputMethodHints; }; +class QQuickSpinButtonPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QQuickSpinButton) + +public: + QQuickSpinButtonPrivate() + : pressed(false), + hovered(false), + indicator(nullptr) + { + } + + static QQuickSpinButtonPrivate *get(QQuickSpinButton *button) + { + return button->d_func(); + } + + void executeIndicator(bool complete = false); + + bool pressed; + bool hovered; + QQuickDeferredPointer<QQuickItem> indicator; +}; + int QQuickSpinBoxPrivate::boundValue(int value) const { return from > to ? qBound(to, value, from) : qBound(from, value, to); @@ -347,6 +374,13 @@ void QQuickSpinBoxPrivate::handleUngrab() stopPressRepeat(); } +QQuickItem *QQuickSpinBoxPrivate::getContentItem() +{ + if (!contentItem) + executeContentItem(); + return contentItem; +} + QQuickSpinBox::QQuickSpinBox(QQuickItem *parent) : QQuickControl(*(new QQuickSpinBoxPrivate), parent) { @@ -833,9 +867,26 @@ void QQuickSpinBox::wheelEvent(QWheelEvent *event) } #endif +void QQuickSpinBox::classBegin() +{ + Q_D(QQuickSpinBox); + QQuickControl::classBegin(); + + QQmlContext *context = qmlContext(this); + if (context) { + QQmlEngine::setContextForObject(d->up, context); + QQmlEngine::setContextForObject(d->down, context); + } +} + void QQuickSpinBox::componentComplete() { Q_D(QQuickSpinBox); + QQuickSpinButtonPrivate::get(d->up)->executeIndicator(true); + QQuickSpinButtonPrivate::get(d->down)->executeIndicator(true); + d->executeBackground(true); + d->executeContentItem(true); + QQuickControl::componentComplete(); if (!d->setValue(d->value, false)) { d->updateUpEnabled(); @@ -859,6 +910,8 @@ void QQuickSpinBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) if (newItem) { newItem->setActiveFocusOnTab(true); + if (d->activeFocus) + newItem->forceActiveFocus(d->focusReason); #if QT_CONFIG(cursor) if (d->editable) newItem->setCursor(Qt::IBeamCursor); @@ -890,20 +943,19 @@ void QQuickSpinBox::accessibilityActiveChanged(bool active) } #endif -class QQuickSpinButtonPrivate : public QObjectPrivate +static inline QString indicatorName() { return QStringLiteral("indicator"); } + +void QQuickSpinButtonPrivate::executeIndicator(bool complete) { -public: - QQuickSpinButtonPrivate() - : pressed(false), - hovered(false), - indicator(nullptr) - { - } + Q_Q(QQuickSpinButton); + if (indicator.wasExecuted()) + return; - bool pressed; - bool hovered; - QQuickItem *indicator; -}; + if (!indicator) + quickBeginDeferred(q, indicatorName(), indicator); + if (complete) + quickCompleteDeferred(q, indicatorName(), indicator); +} QQuickSpinButton::QQuickSpinButton(QQuickSpinBox *parent) : QObject(*(new QQuickSpinButtonPrivate), parent) @@ -928,7 +980,9 @@ void QQuickSpinButton::setPressed(bool pressed) QQuickItem *QQuickSpinButton::indicator() const { - Q_D(const QQuickSpinButton); + QQuickSpinButtonPrivate *d = const_cast<QQuickSpinButtonPrivate *>(d_func()); + if (!d->indicator) + d->executeIndicator(); return d->indicator; } @@ -938,14 +992,15 @@ void QQuickSpinButton::setIndicator(QQuickItem *indicator) if (d->indicator == indicator) return; - QQuickControlPrivate::destroyDelegate(d->indicator, d->parent); + delete d->indicator; d->indicator = indicator; if (indicator) { if (!indicator->parentItem()) indicator->setParentItem(static_cast<QQuickItem *>(parent())); } - emit indicatorChanged(); + if (!d->indicator.isExecuting()) + emit indicatorChanged(); } bool QQuickSpinButton::isHovered() const diff --git a/src/quicktemplates2/qquickspinbox_p.h b/src/quicktemplates2/qquickspinbox_p.h index e62f8614..7bb23e05 100644 --- a/src/quicktemplates2/qquickspinbox_p.h +++ b/src/quicktemplates2/qquickspinbox_p.h @@ -74,6 +74,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSpinBox : public QQuickControl // 2.2 (Qt 5.9) Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints NOTIFY inputMethodHintsChanged FINAL REVISION 2) Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged FINAL REVISION 2) + Q_CLASSINFO("DeferredPropertyNames", "background,contentItem") public: explicit QQuickSpinBox(QQuickItem *parent = nullptr); @@ -141,6 +142,7 @@ protected: void wheelEvent(QWheelEvent *event) override; #endif + void classBegin() override; void componentComplete() override; void itemChange(ItemChange change, const ItemChangeData &value) override; void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) override; @@ -164,6 +166,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSpinButton : public QObject Q_PROPERTY(QQuickItem *indicator READ indicator WRITE setIndicator NOTIFY indicatorChanged FINAL) // 2.1 (Qt 5.8) Q_PROPERTY(bool hovered READ isHovered WRITE setHovered NOTIFY hoveredChanged FINAL REVISION 1) + Q_CLASSINFO("DeferredPropertyNames", "indicator") public: explicit QQuickSpinButton(QQuickSpinBox *parent); |