-
Notifications
You must be signed in to change notification settings - Fork 703
/
Copy pathqtmaterialsnackbar_internal.cpp
99 lines (78 loc) · 3 KB
/
qtmaterialsnackbar_internal.cpp
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
#include "qtmaterialsnackbar_internal.h"
#include <QPropertyAnimation>
#include "qtmaterialsnackbar.h"
#include "lib/qtmaterialstatetransition.h"
#include <QDebug>
QtMaterialSnackbarStateMachine::QtMaterialSnackbarStateMachine(QtMaterialSnackbar *parent)
: QStateMachine(parent),
m_snackbar(parent)
{
m_timer.setSingleShot(true);
QState *hiddenState = new QState;
QState *visibleState = new QState;
QState *finalState = new QState;
addState(hiddenState);
addState(visibleState);
addState(finalState);
setInitialState(hiddenState);
QtMaterialStateTransition *transition;
transition = new QtMaterialStateTransition(SnackbarShowTransition);
transition->setTargetState(visibleState);
hiddenState->addTransition(transition);
transition = new QtMaterialStateTransition(SnackbarHideTransition);
transition->setTargetState(visibleState);
hiddenState->addTransition(transition);
transition = new QtMaterialStateTransition(SnackbarHideTransition);
transition->setTargetState(finalState);
visibleState->addTransition(transition);
transition = new QtMaterialStateTransition(SnackbarWaitTransition);
transition->setTargetState(hiddenState);
finalState->addTransition(transition);
transition = new QtMaterialStateTransition(SnackbarNextTransition);
transition->setTargetState(visibleState);
finalState->addTransition(transition);
connect(visibleState, SIGNAL(propertiesAssigned()),
this, SLOT(snackbarShown()));
connect(finalState, SIGNAL(propertiesAssigned()),
m_snackbar, SLOT(dequeue()));
QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "offset", this);
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->setDuration(300);
addDefaultAnimation(animation);
hiddenState->assignProperty(this, "offset", 1);
visibleState->assignProperty(this, "offset", 0);
finalState->assignProperty(this, "offset", 1);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(progress()));
m_snackbar->installEventFilter(this);
}
QtMaterialSnackbarStateMachine::~QtMaterialSnackbarStateMachine()
{
}
bool QtMaterialSnackbarStateMachine::eventFilter(QObject *watched, QEvent *event)
{
if (QEvent::MouseButtonPress == event->type() && m_snackbar->clickToDismissMode()) {
progress();
}
return QStateMachine::eventFilter(watched, event);
}
void QtMaterialSnackbarStateMachine::setOffset(qreal offset)
{
m_offset = offset;
m_snackbar->update();
}
void QtMaterialSnackbarStateMachine::progress()
{
m_timer.stop();
postEvent(new QtMaterialStateTransitionEvent(SnackbarHideTransition));
if (m_snackbar->clickToDismissMode()) {
m_snackbar->setAttribute(Qt::WA_TransparentForMouseEvents, true);
}
}
void QtMaterialSnackbarStateMachine::snackbarShown()
{
m_timer.start(m_snackbar->autoHideDuration());
if (m_snackbar->clickToDismissMode()) {
m_snackbar->setAttribute(Qt::WA_TransparentForMouseEvents, false);
}
}