aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/timelineeditor/timelinetoolbutton.cpp
blob: 8ebeae675f50a47c46e64f74313952d867b8eec1 (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
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "timelinetoolbutton.h"

#include "timelineconstants.h"

#include <QGraphicsSceneMouseEvent>
#include <QPainter>

#include <utils/stylehelper.h>
#include <utils/theme/theme.h>

#include <QAction>
#include <QCursor>
#include <QDebug>

namespace QmlDesigner {

TimelineToolButton::TimelineToolButton(QAction *action, QGraphicsItem *parent)
    : QGraphicsWidget(parent)
    , m_action(action)
{
    resize(TimelineConstants::toolButtonSize, TimelineConstants::toolButtonSize);
    setPreferredSize(size());
    setAcceptHoverEvents(true);
    connect(action, &QAction::changed, this, [this]() {
        setVisible(m_action->isVisible());
        update();
    });

    connect(this, &TimelineToolButton::clicked, action, &QAction::trigger);

    setEnabled(m_action->isEnabled());
    setVisible(m_action->isVisible());
    setCursor(Qt::ArrowCursor);
}

void TimelineToolButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->save();

    if (m_state == Normal)
        setOpacity(0.8);
    else if (m_state == Pressed)
        setOpacity(0.3);
    else
        setOpacity(1.0);

    if (!isEnabled())
        setOpacity(0.5);

    if (isCheckable()) {
        if (isChecked() || isDisabled())
            m_action->icon().paint(painter,
                                   rect().toRect(),
                                   Qt::AlignCenter,
                                   QIcon::Normal,
                                   QIcon::On);
        else
            m_action->icon().paint(painter,
                                   rect().toRect(),
                                   Qt::AlignCenter,
                                   QIcon::Normal,
                                   QIcon::Off);
    } else
        m_action->icon().paint(painter, rect().toRect());

    painter->restore();
}

QRectF TimelineToolButton::boundingRect() const
{
    return QRectF(0, 0, TimelineConstants::toolButtonSize, TimelineConstants::toolButtonSize);
}

bool TimelineToolButton::isCheckable() const
{
    return m_action->isCheckable();
}

bool TimelineToolButton::isChecked() const
{
    return m_action->isChecked();
}

bool TimelineToolButton::isDisabled() const
{
    return !m_action->isEnabled();
}

void TimelineToolButton::setChecked(bool b)
{
    m_action->setChecked(b);
    update();
}

void TimelineToolButton::setDisabled(bool b)
{
    m_action->setDisabled(b);
}

void TimelineToolButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    m_state = Hovered;

    QGraphicsObject::hoverEnterEvent(event);
    event->accept();
    update();
}

void TimelineToolButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    m_state = Normal;

    QGraphicsWidget::hoverLeaveEvent(event);
    event->accept();
    update();
}

void TimelineToolButton::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    m_state = Hovered;
    QGraphicsWidget::hoverMoveEvent(event);
    update();
}

void TimelineToolButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    m_state = Pressed;
    event->accept();
    update();
}

void TimelineToolButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    m_state = Normal;

    event->accept();
    emit clicked();
}

} // namespace QmlDesigner