summary refs log tree commit diff
path: root/src/ui/RaisedButton.cpp
blob: 42afdd37325dcc25482a57ad71934ed226bcef51 (plain) (blame)
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
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QEventTransition>
#include <QPropertyAnimation>

#include "RaisedButton.h"

void
RaisedButton::init()
{
    shadow_state_machine_ = new QStateMachine(this);
    normal_state_         = new QState;
    pressed_state_        = new QState;
    effect_               = new QGraphicsDropShadowEffect;

    effect_->setBlurRadius(7);
    effect_->setOffset(QPointF(0, 2));
    effect_->setColor(QColor(0, 0, 0, 75));

    setBackgroundMode(Qt::OpaqueMode);
    setMinimumHeight(42);
    setGraphicsEffect(effect_);
    setBaseOpacity(0.3);

    shadow_state_machine_->addState(normal_state_);
    shadow_state_machine_->addState(pressed_state_);

    normal_state_->assignProperty(effect_, "offset", QPointF(0, 2));
    normal_state_->assignProperty(effect_, "blurRadius", 7);

    pressed_state_->assignProperty(effect_, "offset", QPointF(0, 5));
    pressed_state_->assignProperty(effect_, "blurRadius", 29);

    QAbstractTransition *transition;

    transition = new QEventTransition(this, QEvent::MouseButtonPress);
    transition->setTargetState(pressed_state_);
    normal_state_->addTransition(transition);

    transition = new QEventTransition(this, QEvent::MouseButtonDblClick);
    transition->setTargetState(pressed_state_);
    normal_state_->addTransition(transition);

    transition = new QEventTransition(this, QEvent::MouseButtonRelease);
    transition->setTargetState(normal_state_);
    pressed_state_->addTransition(transition);

    QPropertyAnimation *animation;

    animation = new QPropertyAnimation(effect_, "offset", this);
    animation->setDuration(100);
    shadow_state_machine_->addDefaultAnimation(animation);

    animation = new QPropertyAnimation(effect_, "blurRadius", this);
    animation->setDuration(100);
    shadow_state_machine_->addDefaultAnimation(animation);

    shadow_state_machine_->setInitialState(normal_state_);
    shadow_state_machine_->start();
}

RaisedButton::RaisedButton(QWidget *parent)
  : FlatButton(parent)
{
    init();
}

RaisedButton::RaisedButton(const QString &text, QWidget *parent)
  : FlatButton(parent)
{
    init();
    setText(text);
}

bool
RaisedButton::event(QEvent *event)
{
    if (QEvent::EnabledChange == event->type()) {
        if (isEnabled()) {
            shadow_state_machine_->start();
            effect_->setEnabled(true);
        } else {
            shadow_state_machine_->stop();
            effect_->setEnabled(false);
        }
    }

    return FlatButton::event(event);
}