diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-12-20 21:52:25 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-01-02 15:33:34 +0000 |
commit | f15468cf9e5c440518a7af9a265234bff8e6c627 (patch) | |
tree | 085fc7193116c9700540227186d23407afb1b93f /src/quicktemplates2 | |
parent | c4c1a5a2f229c6f14946dfde3b8344a4e9abb2ff (diff) |
Add DelayButton
[ChangeLog][Controls][DelayButton] Added DelayButton.
Change-Id: I94820dfb41ba9b90f0a29cda01ac476b54cf3de8
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2')
-rw-r--r-- | src/quicktemplates2/qquickabstractbutton.cpp | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickabstractbutton_p.h | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdelaybutton.cpp | 271 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdelaybutton_p.h | 98 | ||||
-rw-r--r-- | src/quicktemplates2/quicktemplates2.pri | 2 |
5 files changed, 373 insertions, 0 deletions
diff --git a/src/quicktemplates2/qquickabstractbutton.cpp b/src/quicktemplates2/qquickabstractbutton.cpp index c4fde34d..7ca776cd 100644 --- a/src/quicktemplates2/qquickabstractbutton.cpp +++ b/src/quicktemplates2/qquickabstractbutton.cpp @@ -400,6 +400,7 @@ void QQuickAbstractButton::setPressed(bool isPressed) d->pressed = isPressed; setAccessibleProperty("pressed", isPressed); emit pressedChanged(); + buttonChange(ButtonPressedChanged); if (!d->explicitDown) { setDown(d->pressed); diff --git a/src/quicktemplates2/qquickabstractbutton_p.h b/src/quicktemplates2/qquickabstractbutton_p.h index b8c9476f..8a1d960b 100644 --- a/src/quicktemplates2/qquickabstractbutton_p.h +++ b/src/quicktemplates2/qquickabstractbutton_p.h @@ -130,6 +130,7 @@ protected: ButtonAutoRepeatChange, ButtonCheckedChange, ButtonCheckableChange, + ButtonPressedChanged, ButtonTextChange }; virtual void buttonChange(ButtonChange change); diff --git a/src/quicktemplates2/qquickdelaybutton.cpp b/src/quicktemplates2/qquickdelaybutton.cpp new file mode 100644 index 00000000..3c8eaeb0 --- /dev/null +++ b/src/quicktemplates2/qquickdelaybutton.cpp @@ -0,0 +1,271 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 "qquickdelaybutton_p.h" +#include "qquickabstractbutton_p_p.h" + +#include <QtQuick/private/qquickanimation_p.h> +#include <QtQuick/private/qquicktransition_p.h> +#include <QtQuick/private/qquicktransitionmanager_p_p.h> + +QT_BEGIN_NAMESPACE + +/*! + \qmltype DelayButton + \inherits AbstractButton + \instantiates QQuickDelayButton + \inqmlmodule QtQuick.Controls + \since 5.9 + \ingroup qtquickcontrols2-buttons + \brief Check button that triggers when held down long enough. + + \image qtquickcontrols2-delaybutton.gif + + DelayButton is a checkable button that incorporates a delay before the + button becomes \l {AbstractButton::}{checked} and the \l activated() + signal is emitted. This delay prevents accidental presses. + + The current progress is expressed as a decimal value between \c 0.0 + and \c 1.0. The time it takes for \l activated() to be emitted is + measured in milliseconds, and can be set with the \l delay property. + + The progress is indicated by a progress indicator on the button. + + \sa {Customizing DelayButton}, {Button Controls} +*/ + +/*! + \qmlsignal QtQuick.Controls::DelayButton::activated() + + This signal is emitted when \l progress reaches \c 1.0. +*/ + +class QQuickDelayTransitionManager; + +class QQuickDelayButtonPrivate : public QQuickAbstractButtonPrivate +{ + Q_DECLARE_PUBLIC(QQuickDelayButton) + +public: + QQuickDelayButtonPrivate(); + + void beginTransition(qreal to); + void finishTransition(); + void cancelTransition(); + + int delay; + qreal progress; + QQuickTransition *transition; + QScopedPointer<QQuickDelayTransitionManager> transitionManager; +}; + +class QQuickDelayTransitionManager : public QQuickTransitionManager +{ +public: + QQuickDelayTransitionManager(QQuickDelayButton *button) : m_button(button) { } + + void transition(QQuickTransition *transition, qreal progress); + +protected: + void finished() override; + +private: + QQuickDelayButton *m_button; +}; + +void QQuickDelayTransitionManager::transition(QQuickTransition *transition, qreal progress) +{ + qmlExecuteDeferred(transition); + + QQmlProperty defaultTarget(m_button, QLatin1String("progress")); + QQmlListProperty<QQuickAbstractAnimation> animations = transition->animations(); + const int count = animations.count(&animations); + for (int i = 0; i < count; ++i) { + QQuickAbstractAnimation *anim = animations.at(&animations, i); + anim->setDefaultTarget(defaultTarget); + } + + QList<QQuickStateAction> actions; + actions << QQuickStateAction(m_button, QLatin1String("progress"), progress); + QQuickTransitionManager::transition(actions, transition, m_button); +} + +void QQuickDelayTransitionManager::finished() +{ + if (qFuzzyCompare(m_button->progress(), 1.0)) + emit m_button->activated(); +} + +QQuickDelayButtonPrivate::QQuickDelayButtonPrivate() + : delay(3000), + progress(0.0), + transition(nullptr) +{ +} + +void QQuickDelayButtonPrivate::beginTransition(qreal to) +{ + Q_Q(QQuickDelayButton); + if (!transition) { + q->setProgress(to); + finishTransition(); + return; + } + + if (!transitionManager) + transitionManager.reset(new QQuickDelayTransitionManager(q)); + + transitionManager->transition(transition, to); +} + +void QQuickDelayButtonPrivate::finishTransition() +{ + Q_Q(QQuickDelayButton); + if (qFuzzyCompare(progress, 1.0)) + emit q->activated(); +} + +void QQuickDelayButtonPrivate::cancelTransition() +{ + if (transitionManager) + transitionManager->cancel(); +} + +QQuickDelayButton::QQuickDelayButton(QQuickItem *parent) : + QQuickAbstractButton(*(new QQuickDelayButtonPrivate), parent) +{ + setCheckable(true); +} + +/*! + \qmlproperty int QtQuick.Controls::DelayButton::delay + + This property holds the time it takes (in milliseconds) for \l progress + to reach \c 1.0 and emit \l activated(). + + The default value is \c 3000 ms. +*/ +int QQuickDelayButton::delay() const +{ + Q_D(const QQuickDelayButton); + return d->delay; +} + +void QQuickDelayButton::setDelay(int delay) +{ + Q_D(QQuickDelayButton); + if (d->delay == delay) + return; + + d->delay = delay; + emit delayChanged(); +} + +/*! + \qmlproperty real QtQuick.Controls::DelayButton::progress + \readonly + + This property holds the current progress as displayed by the progress + indicator, in the range \c 0.0 - \c 1.0. +*/ +qreal QQuickDelayButton::progress() const +{ + Q_D(const QQuickDelayButton); + return d->progress; +} + +void QQuickDelayButton::setProgress(qreal progress) +{ + Q_D(QQuickDelayButton); + if (qFuzzyCompare(d->progress, progress)) + return; + + d->progress = progress; + emit progressChanged(); +} + +/*! + \qmlproperty Transition QtQuick.Controls::DelayButton::transition + + This property holds the transition that is applied on the \l progress + property when the button is pressed or released. +*/ +QQuickTransition *QQuickDelayButton::transition() const +{ + Q_D(const QQuickDelayButton); + return d->transition; +} + +void QQuickDelayButton::setTransition(QQuickTransition *transition) +{ + Q_D(QQuickDelayButton); + if (d->transition == transition) + return; + + d->transition = transition; + emit transitionChanged(); +} + +void QQuickDelayButton::buttonChange(ButtonChange change) +{ + Q_D(QQuickDelayButton); + switch (change) { + case ButtonCheckedChange: + d->cancelTransition(); + setProgress(d->checked ? 1.0 : 0.0); + break; + case ButtonPressedChanged: + if (!d->checked) + d->beginTransition(d->pressed ? 1.0 : 0.0); + break; + default: + QQuickAbstractButton::buttonChange(change); + break; + } +} + +void QQuickDelayButton::nextCheckState() +{ + Q_D(QQuickDelayButton); + setChecked(!d->checked && qFuzzyCompare(d->progress, 1.0)); +} + +QFont QQuickDelayButton::defaultFont() const +{ + return QQuickControlPrivate::themeFont(QPlatformTheme::PushButtonFont); +} + +QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickdelaybutton_p.h b/src/quicktemplates2/qquickdelaybutton_p.h new file mode 100644 index 00000000..5ac0885d --- /dev/null +++ b/src/quicktemplates2/qquickdelaybutton_p.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QQUICKDELAYBUTTON_P_H +#define QQUICKDELAYBUTTON_P_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/qquickabstractbutton_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickTransition; +class QQuickDelayButtonPrivate; + +class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickDelayButton : public QQuickAbstractButton +{ + Q_OBJECT + Q_PROPERTY(int delay READ delay WRITE setDelay NOTIFY delayChanged FINAL) + Q_PROPERTY(qreal progress READ progress WRITE setProgress NOTIFY progressChanged FINAL) + Q_PROPERTY(QQuickTransition *transition READ transition WRITE setTransition NOTIFY transitionChanged FINAL) + +public: + explicit QQuickDelayButton(QQuickItem *parent = nullptr); + + int delay() const; + void setDelay(int delay); + + qreal progress() const; + void setProgress(qreal progress); + + QQuickTransition *transition() const; + void setTransition(QQuickTransition *transition); + +Q_SIGNALS: + void activated(); + void delayChanged(); + void progressChanged(); + void transitionChanged(); + +protected: + void buttonChange(ButtonChange change) override; + void nextCheckState() override; + + QFont defaultFont() const override; + +private: + Q_DISABLE_COPY(QQuickDelayButton) + Q_DECLARE_PRIVATE(QQuickDelayButton) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickDelayButton) + +#endif // QQUICKDELAYBUTTON_P_H diff --git a/src/quicktemplates2/quicktemplates2.pri b/src/quicktemplates2/quicktemplates2.pri index fd92e9d1..e8f02d62 100644 --- a/src/quicktemplates2/quicktemplates2.pri +++ b/src/quicktemplates2/quicktemplates2.pri @@ -15,6 +15,7 @@ HEADERS += \ $$PWD/qquickcontainer_p_p.h \ $$PWD/qquickcontrol_p.h \ $$PWD/qquickcontrol_p_p.h \ + $$PWD/qquickdelaybutton_p.h \ $$PWD/qquickdial_p.h \ $$PWD/qquickdialog_p.h \ $$PWD/qquickdialog_p_p.h \ @@ -90,6 +91,7 @@ SOURCES += \ $$PWD/qquickcombobox.cpp \ $$PWD/qquickcontainer.cpp \ $$PWD/qquickcontrol.cpp \ + $$PWD/qquickdelaybutton.cpp \ $$PWD/qquickdial.cpp \ $$PWD/qquickdialog.cpp \ $$PWD/qquickdialogbuttonbox.cpp \ |