aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates/qquickdelaybutton.cpp
blob: 6c1b5e93784576ba2096c93ef8f67a8a9cf98a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#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 qtquickcontrols-buttons
    \brief Check button that triggers when held down long enough.

    \image qtquickcontrols-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:
    void beginTransition(qreal to);
    void finishTransition();
    void cancelTransition();

    QPalette defaultPalette() const override { return QQuickTheme::palette(QQuickTheme::Button); }

    int delay = 300;
    qreal progress = 0.0;
    QQuickTransition *transition = nullptr;
    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 = nullptr;
};

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(), qreal(1.0)))
        emit m_button->activated();
}

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, qreal(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

    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, qreal(1.0)));
}

QFont QQuickDelayButton::defaultFont() const
{
    return QQuickTheme::font(QQuickTheme::Button);
}

QT_END_NAMESPACE

#include "moc_qquickdelaybutton_p.cpp"