diff options
author | Jan Arve Sæther <jan-arve.saether@qt.io> | 2020-10-29 17:01:27 +0100 |
---|---|---|
committer | Jan Arve Sæther <jan-arve.saether@qt.io> | 2020-12-10 16:21:21 +0100 |
commit | 64fd0b53b378ef91725d4950720c3bdfaee11498 (patch) | |
tree | 4860bf008c3437a41575e9a05dda8eba96f05848 /src/quicktemplates2 | |
parent | 8d061f542181cfe0ac9052948c869d87ce80589b (diff) |
Add support for ScrollBar arrow buttons
In order to achieve this, it separates out QQuickSpinButton into a
separate file (and renames it since it's not only purposed for SpinBox
anymore). This allows it to be also used by QQuickScrollBar.
Fixes: QTBUG-88115
Pick-to: 6.0
Change-Id: I2dea42b29750b7bc619031f40a43717fc10c177b
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2')
-rw-r--r-- | src/quicktemplates2/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickindicatorbutton_p.cpp | 157 | ||||
-rw-r--r-- | src/quicktemplates2/qquickindicatorbutton_p.h | 121 | ||||
-rw-r--r-- | src/quicktemplates2/qquickscrollbar.cpp | 125 | ||||
-rw-r--r-- | src/quicktemplates2/qquickscrollbar_p.h | 12 | ||||
-rw-r--r-- | src/quicktemplates2/qquickscrollbar_p_p.h | 7 | ||||
-rw-r--r-- | src/quicktemplates2/qquickspinbox.cpp | 147 | ||||
-rw-r--r-- | src/quicktemplates2/qquickspinbox_p.h | 56 | ||||
-rw-r--r-- | src/quicktemplates2/quicktemplates2.pri | 2 |
9 files changed, 438 insertions, 190 deletions
diff --git a/src/quicktemplates2/CMakeLists.txt b/src/quicktemplates2/CMakeLists.txt index 5b71e91f..ff9fdb5c 100644 --- a/src/quicktemplates2/CMakeLists.txt +++ b/src/quicktemplates2/CMakeLists.txt @@ -41,6 +41,7 @@ qt_internal_add_module(QuickTemplates2 qquickheaderview.cpp qquickheaderview_p.h qquickheaderview_p_p.h qquickicon.cpp qquickicon_p.h + qquickindicatorbutton_p.cpp qquickindicatorbutton_p.h qquickitemdelegate.cpp qquickitemdelegate_p.h qquickitemdelegate_p_p.h qquicklabel.cpp qquicklabel_p.h diff --git a/src/quicktemplates2/qquickindicatorbutton_p.cpp b/src/quicktemplates2/qquickindicatorbutton_p.cpp new file mode 100644 index 00000000..c4130da0 --- /dev/null +++ b/src/quicktemplates2/qquickindicatorbutton_p.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qquickindicatorbutton_p.h" +#include "qquickdeferredexecute_p_p.h" +#include "qquickcontrol_p_p.h" + +QT_BEGIN_NAMESPACE + +class QQuickIndicatorButton; + +static inline QString indicatorName() { return QStringLiteral("indicator"); } + +void QQuickIndicatorButtonPrivate::cancelIndicator() +{ + Q_Q(QQuickIndicatorButton); + quickCancelDeferred(q, indicatorName()); +} + +void QQuickIndicatorButtonPrivate::executeIndicator(bool complete) +{ + Q_Q(QQuickIndicatorButton); + if (indicator.wasExecuted()) + return; + + if (!indicator || complete) + quickBeginDeferred(q, indicatorName(), indicator); + if (complete) + quickCompleteDeferred(q, indicatorName(), indicator); +} + +QQuickIndicatorButton::QQuickIndicatorButton(QObject *parent) + : QObject(*(new QQuickIndicatorButtonPrivate), parent) +{ +} + +bool QQuickIndicatorButton::isPressed() const +{ + Q_D(const QQuickIndicatorButton); + return d->pressed; +} + +void QQuickIndicatorButton::setPressed(bool pressed) +{ + Q_D(QQuickIndicatorButton); + if (d->pressed == pressed) + return; + + d->pressed = pressed; + emit pressedChanged(); +} + +QQuickItem *QQuickIndicatorButton::indicator() const +{ + QQuickIndicatorButtonPrivate *d = const_cast<QQuickIndicatorButtonPrivate *>(d_func()); + if (!d->indicator) + d->executeIndicator(); + return d->indicator; +} + +void QQuickIndicatorButton::setIndicator(QQuickItem *indicator) +{ + Q_D(QQuickIndicatorButton); + if (d->indicator == indicator) + return; + + if (!d->indicator.isExecuting()) + d->cancelIndicator(); + + const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth(); + const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight(); + + QQuickControl *par = static_cast<QQuickControl *>(parent()); + + QQuickControlPrivate::get(par)->removeImplicitSizeListener(d->indicator); + QQuickControlPrivate::hideOldItem(d->indicator); + d->indicator = indicator; + + if (indicator) { + if (!indicator->parentItem()) + indicator->setParentItem(par); + QQuickControlPrivate::get(par)->addImplicitSizeListener(indicator); + } + + if (!qFuzzyCompare(oldImplicitIndicatorWidth, implicitIndicatorWidth())) + emit implicitIndicatorWidthChanged(); + if (!qFuzzyCompare(oldImplicitIndicatorHeight, implicitIndicatorHeight())) + emit implicitIndicatorHeightChanged(); + if (!d->indicator.isExecuting()) + emit indicatorChanged(); +} + +bool QQuickIndicatorButton::isHovered() const +{ + Q_D(const QQuickIndicatorButton); + return d->hovered; +} + +void QQuickIndicatorButton::setHovered(bool hovered) +{ + Q_D(QQuickIndicatorButton); + if (d->hovered == hovered) + return; + + d->hovered = hovered; + emit hoveredChanged(); +} + +qreal QQuickIndicatorButton::implicitIndicatorWidth() const +{ + Q_D(const QQuickIndicatorButton); + if (!d->indicator) + return 0; + return d->indicator->implicitWidth(); +} + +qreal QQuickIndicatorButton::implicitIndicatorHeight() const +{ + Q_D(const QQuickIndicatorButton); + if (!d->indicator) + return 0; + return d->indicator->implicitHeight(); +} + +QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickindicatorbutton_p.h b/src/quicktemplates2/qquickindicatorbutton_p.h new file mode 100644 index 00000000..44c5b0cf --- /dev/null +++ b/src/quicktemplates2/qquickindicatorbutton_p.h @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QQUICKINDICATORBUTTON_H +#define QQUICKINDICATORBUTTON_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQuickTemplates2/private/qquickcontrol_p.h> +#include <QtQml/qjsvalue.h> +#include "qquickdeferredpointer_p_p.h" + +QT_BEGIN_NAMESPACE + +class QQuickIndicatorButtonPrivate; + +class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickIndicatorButton : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool pressed READ isPressed WRITE setPressed NOTIFY pressedChanged FINAL) + 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(2, 1)) + // 2.5 (Qt 5.12) + Q_PROPERTY(qreal implicitIndicatorWidth READ implicitIndicatorWidth NOTIFY implicitIndicatorWidthChanged FINAL REVISION(2, 5)) + Q_PROPERTY(qreal implicitIndicatorHeight READ implicitIndicatorHeight NOTIFY implicitIndicatorHeightChanged FINAL REVISION(2, 5)) + Q_CLASSINFO("DeferredPropertyNames", "indicator") + QML_ANONYMOUS + QML_ADDED_IN_VERSION(2, 0) + +public: + explicit QQuickIndicatorButton(QObject *parent); + + bool isPressed() const; + void setPressed(bool pressed); + + QQuickItem *indicator() const; + void setIndicator(QQuickItem *indicator); + + bool isHovered() const; + void setHovered(bool hovered); + + qreal implicitIndicatorWidth() const; + qreal implicitIndicatorHeight() const; + +Q_SIGNALS: + void pressedChanged(); + void indicatorChanged(); + // 2.1 (Qt 5.8) + Q_REVISION(2, 1) void hoveredChanged(); + // 2.5 (Qt 5.12) + Q_REVISION(2, 5) void implicitIndicatorWidthChanged(); + Q_REVISION(2, 5) void implicitIndicatorHeightChanged(); + +private: + Q_DISABLE_COPY(QQuickIndicatorButton) + Q_DECLARE_PRIVATE(QQuickIndicatorButton) +}; + +class QQuickIndicatorButtonPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QQuickIndicatorButton) + +public: + static QQuickIndicatorButtonPrivate *get(QQuickIndicatorButton *button) + { + return button->d_func(); + } + + void cancelIndicator(); + void executeIndicator(bool complete = false); + + bool pressed = false; + bool hovered = false; + QQuickDeferredPointer<QQuickItem> indicator; +}; + +QT_END_NAMESPACE + +#endif // QQUICKINDICATORBUTTON_H diff --git a/src/quicktemplates2/qquickscrollbar.cpp b/src/quicktemplates2/qquickscrollbar.cpp index f7b403d7..4c89b330 100644 --- a/src/quicktemplates2/qquickscrollbar.cpp +++ b/src/quicktemplates2/qquickscrollbar.cpp @@ -254,10 +254,56 @@ void QQuickScrollBarPrivate::resizeContent() } } +void QQuickScrollBarPrivate::itemImplicitWidthChanged(QQuickItem *item) +{ + Q_Q(QQuickScrollBar); + QQuickControlPrivate::itemImplicitWidthChanged(item); + QQuickIndicatorButton *indicatorButton = q->decreaseVisual(); + if (!indicatorButton || item != indicatorButton->indicator()) { + indicatorButton = q->increaseVisual(); + if (!indicatorButton || item != indicatorButton->indicator()) + return; + } + if (indicatorButton) + emit indicatorButton->implicitIndicatorWidthChanged(); +} + +void QQuickScrollBarPrivate::itemImplicitHeightChanged(QQuickItem *item) +{ + Q_Q(QQuickScrollBar); + QQuickControlPrivate::itemImplicitHeightChanged(item); + QQuickIndicatorButton *indicatorButton = q->decreaseVisual(); + if (!indicatorButton || item != indicatorButton->indicator()) { + indicatorButton = q->increaseVisual(); + if (!indicatorButton || item != indicatorButton->indicator()) + return; + } + if (indicatorButton) + emit indicatorButton->implicitIndicatorHeightChanged(); +} + void QQuickScrollBarPrivate::handlePress(const QPointF &point) { Q_Q(QQuickScrollBar); QQuickControlPrivate::handlePress(point); + if (QQuickIndicatorButton *indicatorButton = q->decreaseVisual()) { + QQuickItem *decreaseArrow = indicatorButton->indicator(); + if (decreaseArrow && decreaseArrow->contains(q->mapToItem(decreaseArrow, point + QPointF(0.5, 0.5)))) { + indicatorButton->setPressed(true); + q->decrease(); + return; + } + } + + if (QQuickIndicatorButton *increaseObject = q->increaseVisual()) { + QQuickItem *increaseArrow = increaseObject->indicator(); + if (increaseArrow && increaseArrow->contains(q->mapToItem(increaseArrow, point + QPointF(0.5, 0.5)))) { + increaseObject->setPressed(true); + q->increase(); + return; + } + } + offset = positionAt(point) - position; qreal sz = qMax(size, logicalPosition(minimumSize)); if (offset < 0 || offset > sz) @@ -269,6 +315,16 @@ void QQuickScrollBarPrivate::handleMove(const QPointF &point) { Q_Q(QQuickScrollBar); QQuickControlPrivate::handleMove(point); + + /* + * handleMove() will be called as soon as you hold the mouse button down *anywhere* on the + * ScrollBar, including the increase/decrease button indicator areas. So without the following + * early return, it would move the scrollbar handle to one of its extremeties. That would + * ruin the behavior we would like when clicking e.g. the "increase button": To step the + * scrollbar gently. + */ + if (!pressed) + return; qreal pos = qBound<qreal>(0.0, positionAt(point) - offset, 1.0 - size); if (snapMode == QQuickScrollBar::SnapAlways) pos = snapPosition(pos); @@ -279,6 +335,14 @@ void QQuickScrollBarPrivate::handleRelease(const QPointF &point) { Q_Q(QQuickScrollBar); QQuickControlPrivate::handleRelease(point); + + if (orientation == Qt::Vertical) { + if (point.y() < q->topPadding() || point.y() >= (q->height() - q->bottomPadding())) + return; + } else /* orientation == Qt::Horizontal */{ + if (point.x() < q->leftPadding() || point.x() >= (q->width() - q->rightPadding())) + return; + } qreal pos = qBound<qreal>(0.0, positionAt(point) - offset, 1.0 - size); if (snapMode != QQuickScrollBar::NoSnap) pos = snapPosition(pos); @@ -304,6 +368,23 @@ void QQuickScrollBarPrivate::visualAreaChange(const VisualArea &newVisualArea, c emit q->visualPositionChanged(); } +void QQuickScrollBarPrivate::updateHover(const QPointF &pos, std::optional<bool> newHoverState) +{ + Q_Q(QQuickScrollBar); + auto updateHoverOnButton = [&](QQuickIndicatorButton *sbButton) { + if (sbButton) { + bool hovered = newHoverState.value_or(false); + if (!newHoverState.has_value()) { + if (QQuickItem *indicator = sbButton->indicator()) + hovered = indicator->contains(q->mapToItem(indicator, pos)); + } + sbButton->setHovered(hovered); + } + }; + updateHoverOnButton(q->decreaseVisual()); + updateHoverOnButton(q->increaseVisual()); +} + QQuickScrollBar::QQuickScrollBar(QQuickItem *parent) : QQuickControl(*(new QQuickScrollBarPrivate), parent) { @@ -451,6 +532,12 @@ bool QQuickScrollBar::isPressed() const void QQuickScrollBar::setPressed(bool pressed) { Q_D(QQuickScrollBar); + if (!pressed) { + if (QQuickIndicatorButton *button = decreaseVisual()) + button->setPressed(false); + if (QQuickIndicatorButton *button = increaseVisual()) + button->setPressed(false); + } if (d->pressed == pressed) return; @@ -681,6 +768,22 @@ qreal QQuickScrollBar::visualPosition() const return d->visualArea().position; } +QQuickIndicatorButton *QQuickScrollBar::decreaseVisual() +{ + Q_D(QQuickScrollBar); + if (!d->decreaseVisual) + d->decreaseVisual = new QQuickIndicatorButton(this); + return d->decreaseVisual; +} + +QQuickIndicatorButton *QQuickScrollBar::increaseVisual() +{ + Q_D(QQuickScrollBar); + if (!d->increaseVisual) + d->increaseVisual = new QQuickIndicatorButton(this); + return d->increaseVisual; +} + /*! \qmlmethod void QtQuick.Controls::ScrollBar::increase() @@ -730,6 +833,28 @@ void QQuickScrollBar::hoverChange() } #endif +void QQuickScrollBar::hoverEnterEvent(QHoverEvent *event) +{ + Q_D(QQuickScrollBar); + QQuickControl::hoverEnterEvent(event); + d->updateHover(event->position()); +} + +void QQuickScrollBar::hoverMoveEvent(QHoverEvent *event) +{ + Q_D(QQuickScrollBar); + QQuickControl::hoverMoveEvent(event); + d->updateHover(event->position()); +} + +void QQuickScrollBar::hoverLeaveEvent(QHoverEvent *event) +{ + Q_D(QQuickScrollBar); + QQuickControl::hoverLeaveEvent(event); + + d->updateHover(QPoint(), false); //position is not needed when we force it to unhover +} + #if QT_CONFIG(accessibility) void QQuickScrollBar::accessibilityActiveChanged(bool active) { diff --git a/src/quicktemplates2/qquickscrollbar_p.h b/src/quicktemplates2/qquickscrollbar_p.h index dee0b749..b35575b9 100644 --- a/src/quicktemplates2/qquickscrollbar_p.h +++ b/src/quicktemplates2/qquickscrollbar_p.h @@ -49,7 +49,7 @@ // #include <QtQuickTemplates2/private/qquickcontrol_p.h> - +#include <QtQuickTemplates2/private/qquickindicatorbutton_p.h> QT_BEGIN_NAMESPACE class QQuickScrollBarAttached; @@ -75,6 +75,10 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickScrollBar : public QQuickControl Q_PROPERTY(qreal minimumSize READ minimumSize WRITE setMinimumSize NOTIFY minimumSizeChanged FINAL REVISION(2, 4)) Q_PROPERTY(qreal visualSize READ visualSize NOTIFY visualSizeChanged FINAL REVISION(2, 4)) Q_PROPERTY(qreal visualPosition READ visualPosition NOTIFY visualPositionChanged FINAL REVISION(2, 4)) + + Q_PROPERTY(QQuickIndicatorButton *__decreaseVisual READ decreaseVisual CONSTANT FINAL) + Q_PROPERTY(QQuickIndicatorButton *__increaseVisual READ increaseVisual CONSTANT FINAL) + QML_NAMED_ELEMENT(ScrollBar) QML_ATTACHED(QQuickScrollBarAttached) QML_ADDED_IN_VERSION(2, 0) @@ -135,6 +139,9 @@ public: qreal visualSize() const; qreal visualPosition() const; + QQuickIndicatorButton *decreaseVisual(); + QQuickIndicatorButton *increaseVisual(); + public Q_SLOTS: void increase(); void decrease(); @@ -162,6 +169,9 @@ protected: #if QT_CONFIG(quicktemplates2_hover) void hoverChange() override; + void hoverEnterEvent(QHoverEvent *event) override; + void hoverMoveEvent(QHoverEvent *event) override; + void hoverLeaveEvent(QHoverEvent *event) override; #endif #if QT_CONFIG(accessibility) diff --git a/src/quicktemplates2/qquickscrollbar_p_p.h b/src/quicktemplates2/qquickscrollbar_p_p.h index 5c7628b9..4c4f72dd 100644 --- a/src/quicktemplates2/qquickscrollbar_p_p.h +++ b/src/quicktemplates2/qquickscrollbar_p_p.h @@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE class QQuickFlickable; +class QQuickIndicatorButton; class QQuickScrollBarPrivate : public QQuickControlPrivate { @@ -82,6 +83,8 @@ public: void setInteractive(bool interactive); void updateActive(); void resizeContent() override; + void itemImplicitWidthChanged(QQuickItem *item) override; + void itemImplicitHeightChanged(QQuickItem *item) override; void handlePress(const QPointF &point) override; void handleMove(const QPointF &point) override; @@ -90,6 +93,10 @@ public: void visualAreaChange(const VisualArea &newVisualArea, const VisualArea &oldVisualArea); + void updateHover(const QPointF &pos, std::optional<bool> newHoverState = {}); + + QQuickIndicatorButton *decreaseVisual = nullptr; + QQuickIndicatorButton *increaseVisual = nullptr; qreal size = 0; qreal position = 0; qreal stepSize = 0; diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp index 58c6abaf..d8c8b689 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 "qquickindicatorbutton_p.h" #include "qquickdeferredexecute_p_p.h" #include <QtGui/qguiapplication.h> @@ -149,32 +150,14 @@ public: int delayTimer = 0; int repeatTimer = 0; QString displayText; - QQuickSpinButton *up = nullptr; - QQuickSpinButton *down = nullptr; + QQuickIndicatorButton *up = nullptr; + QQuickIndicatorButton *down = nullptr; QValidator *validator = nullptr; mutable QJSValue textFromValue; mutable QJSValue valueFromText; Qt::InputMethodHints inputMethodHints = Qt::ImhDigitsOnly; }; -class QQuickSpinButtonPrivate : public QObjectPrivate -{ - Q_DECLARE_PUBLIC(QQuickSpinButton) - -public: - static QQuickSpinButtonPrivate *get(QQuickSpinButton *button) - { - return button->d_func(); - } - - void cancelIndicator(); - void executeIndicator(bool complete = false); - - bool pressed = false; - bool hovered = false; - QQuickDeferredPointer<QQuickItem> indicator; -}; - int QQuickSpinBoxPrivate::boundValue(int value, bool wrap) const { bool inverted = from > to; @@ -443,8 +426,8 @@ QQuickSpinBox::QQuickSpinBox(QQuickItem *parent) : QQuickControl(*(new QQuickSpinBoxPrivate), parent) { Q_D(QQuickSpinBox); - d->up = new QQuickSpinButton(this); - d->down = new QQuickSpinButton(this); + d->up = new QQuickIndicatorButton(this); + d->down = new QQuickIndicatorButton(this); setFlag(ItemIsFocusScope); setFiltersChildMouseEvents(true); @@ -741,7 +724,7 @@ void QQuickSpinBox::setValueFromText(const QJSValue &callback) \sa increase() */ -QQuickSpinButton *QQuickSpinBox::up() const +QQuickIndicatorButton *QQuickSpinBox::up() const { Q_D(const QQuickSpinBox); return d->up; @@ -761,7 +744,7 @@ QQuickSpinButton *QQuickSpinBox::up() const \sa decrease() */ -QQuickSpinButton *QQuickSpinBox::down() const +QQuickIndicatorButton *QQuickSpinBox::down() const { Q_D(const QQuickSpinBox); return d->down; @@ -1002,8 +985,8 @@ void QQuickSpinBox::classBegin() void QQuickSpinBox::componentComplete() { Q_D(QQuickSpinBox); - QQuickSpinButtonPrivate::get(d->up)->executeIndicator(true); - QQuickSpinButtonPrivate::get(d->down)->executeIndicator(true); + QQuickIndicatorButtonPrivate::get(d->up)->executeIndicator(true); + QQuickIndicatorButtonPrivate::get(d->down)->executeIndicator(true); QQuickControl::componentComplete(); if (!d->setValue(d->value, /* allowWrap = */ false, /* modified = */ false)) { @@ -1069,118 +1052,6 @@ void QQuickSpinBox::accessibilityActiveChanged(bool active) } #endif -static inline QString indicatorName() { return QStringLiteral("indicator"); } - -void QQuickSpinButtonPrivate::cancelIndicator() -{ - Q_Q(QQuickSpinButton); - quickCancelDeferred(q, indicatorName()); -} - -void QQuickSpinButtonPrivate::executeIndicator(bool complete) -{ - Q_Q(QQuickSpinButton); - if (indicator.wasExecuted()) - return; - - if (!indicator || complete) - quickBeginDeferred(q, indicatorName(), indicator); - if (complete) - quickCompleteDeferred(q, indicatorName(), indicator); -} - -QQuickSpinButton::QQuickSpinButton(QQuickSpinBox *parent) - : QObject(*(new QQuickSpinButtonPrivate), parent) -{ -} - -bool QQuickSpinButton::isPressed() const -{ - Q_D(const QQuickSpinButton); - return d->pressed; -} - -void QQuickSpinButton::setPressed(bool pressed) -{ - Q_D(QQuickSpinButton); - if (d->pressed == pressed) - return; - - d->pressed = pressed; - emit pressedChanged(); -} - -QQuickItem *QQuickSpinButton::indicator() const -{ - QQuickSpinButtonPrivate *d = const_cast<QQuickSpinButtonPrivate *>(d_func()); - if (!d->indicator) - d->executeIndicator(); - return d->indicator; -} - -void QQuickSpinButton::setIndicator(QQuickItem *indicator) -{ - Q_D(QQuickSpinButton); - if (d->indicator == indicator) - return; - - if (!d->indicator.isExecuting()) - d->cancelIndicator(); - - const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth(); - const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight(); - - QQuickSpinBox *spinBox = static_cast<QQuickSpinBox *>(parent()); - QQuickSpinBoxPrivate::get(spinBox)->removeImplicitSizeListener(d->indicator); - QQuickControlPrivate::hideOldItem(d->indicator); - d->indicator = indicator; - - if (indicator) { - if (!indicator->parentItem()) - indicator->setParentItem(spinBox); - QQuickSpinBoxPrivate::get(spinBox)->addImplicitSizeListener(indicator); - } - - if (!qFuzzyCompare(oldImplicitIndicatorWidth, implicitIndicatorWidth())) - emit implicitIndicatorWidthChanged(); - if (!qFuzzyCompare(oldImplicitIndicatorHeight, implicitIndicatorHeight())) - emit implicitIndicatorHeightChanged(); - if (!d->indicator.isExecuting()) - emit indicatorChanged(); -} - -bool QQuickSpinButton::isHovered() const -{ - Q_D(const QQuickSpinButton); - return d->hovered; -} - -void QQuickSpinButton::setHovered(bool hovered) -{ - Q_D(QQuickSpinButton); - if (d->hovered == hovered) - return; - - d->hovered = hovered; - emit hoveredChanged(); -} - -qreal QQuickSpinButton::implicitIndicatorWidth() const -{ - Q_D(const QQuickSpinButton); - if (!d->indicator) - return 0; - return d->indicator->implicitWidth(); -} - -qreal QQuickSpinButton::implicitIndicatorHeight() const -{ - Q_D(const QQuickSpinButton); - if (!d->indicator) - return 0; - return d->indicator->implicitHeight(); -} - QT_END_NAMESPACE #include "moc_qquickspinbox_p.cpp" diff --git a/src/quicktemplates2/qquickspinbox_p.h b/src/quicktemplates2/qquickspinbox_p.h index d90aa531..9e64e96e 100644 --- a/src/quicktemplates2/qquickspinbox_p.h +++ b/src/quicktemplates2/qquickspinbox_p.h @@ -54,9 +54,8 @@ QT_BEGIN_NAMESPACE class QValidator; -class QQuickSpinButton; -class QQuickSpinButtonPrivate; class QQuickSpinBoxPrivate; +class QQuickIndicatorButton; class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSpinBox : public QQuickControl { @@ -69,8 +68,8 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSpinBox : public QQuickControl Q_PROPERTY(QValidator *validator READ validator WRITE setValidator NOTIFY validatorChanged FINAL) Q_PROPERTY(QJSValue textFromValue READ textFromValue WRITE setTextFromValue NOTIFY textFromValueChanged FINAL) Q_PROPERTY(QJSValue valueFromText READ valueFromText WRITE setValueFromText NOTIFY valueFromTextChanged FINAL) - Q_PROPERTY(QQuickSpinButton *up READ up CONSTANT FINAL) - Q_PROPERTY(QQuickSpinButton *down READ down CONSTANT FINAL) + Q_PROPERTY(QQuickIndicatorButton *up READ up CONSTANT FINAL) + Q_PROPERTY(QQuickIndicatorButton *down READ down CONSTANT FINAL) // 2.2 (Qt 5.9) Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints NOTIFY inputMethodHintsChanged FINAL REVISION(2, 2)) Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged FINAL REVISION(2, 2)) @@ -109,8 +108,8 @@ public: QJSValue valueFromText() const; void setValueFromText(const QJSValue &callback); - QQuickSpinButton *up() const; - QQuickSpinButton *down() const; + QQuickIndicatorButton *up() const; + QQuickIndicatorButton *down() const; // 2.2 (Qt 5.9) Qt::InputMethodHints inputMethodHints() const; @@ -177,51 +176,6 @@ private: Q_DECLARE_PRIVATE(QQuickSpinBox) }; -class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSpinButton : public QObject -{ - Q_OBJECT - Q_PROPERTY(bool pressed READ isPressed WRITE setPressed NOTIFY pressedChanged FINAL) - 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(2, 1)) - // 2.5 (Qt 5.12) - Q_PROPERTY(qreal implicitIndicatorWidth READ implicitIndicatorWidth NOTIFY implicitIndicatorWidthChanged FINAL REVISION(2, 5)) - Q_PROPERTY(qreal implicitIndicatorHeight READ implicitIndicatorHeight NOTIFY implicitIndicatorHeightChanged FINAL REVISION(2, 5)) - Q_CLASSINFO("DeferredPropertyNames", "indicator") - QML_ANONYMOUS - QML_ADDED_IN_VERSION(2, 0) - -public: - explicit QQuickSpinButton(QQuickSpinBox *parent); - - bool isPressed() const; - void setPressed(bool pressed); - - QQuickItem *indicator() const; - void setIndicator(QQuickItem *indicator); - - // 2.1 (Qt 5.8) - bool isHovered() const; - void setHovered(bool hovered); - - // 2.5 (Qt 5.12) - qreal implicitIndicatorWidth() const; - qreal implicitIndicatorHeight() const; - -Q_SIGNALS: - void pressedChanged(); - void indicatorChanged(); - // 2.1 (Qt 5.8) - Q_REVISION(2, 1) void hoveredChanged(); - // 2.5 (Qt 5.12) - Q_REVISION(2, 5) void implicitIndicatorWidthChanged(); - Q_REVISION(2, 5) void implicitIndicatorHeightChanged(); - -private: - Q_DISABLE_COPY(QQuickSpinButton) - Q_DECLARE_PRIVATE(QQuickSpinButton) -}; - QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickSpinBox) diff --git a/src/quicktemplates2/quicktemplates2.pri b/src/quicktemplates2/quicktemplates2.pri index 1b15d208..fa377f78 100644 --- a/src/quicktemplates2/quicktemplates2.pri +++ b/src/quicktemplates2/quicktemplates2.pri @@ -34,6 +34,7 @@ HEADERS += \ $$PWD/qquickheaderview_p.h \ $$PWD/qquickheaderview_p_p.h \ $$PWD/qquickicon_p.h \ + $$PWD/qquickindicatorbutton_p.h \ $$PWD/qquickitemdelegate_p.h \ $$PWD/qquickitemdelegate_p_p.h \ $$PWD/qquicklabel_p.h \ @@ -122,6 +123,7 @@ SOURCES += \ $$PWD/qquickgroupbox.cpp \ $$PWD/qquickheaderview.cpp \ $$PWD/qquickicon.cpp \ + $$PWD/qquickindicatorbutton_p.cpp \ $$PWD/qquickitemdelegate.cpp \ $$PWD/qquicklabel.cpp \ $$PWD/qquickmenu.cpp \ |