This repository was archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickButton.cpp
More file actions
81 lines (74 loc) · 1.52 KB
/
ClickButton.cpp
File metadata and controls
81 lines (74 loc) · 1.52 KB
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
#include "ClickButton.h"
ClickButton::ClickButton(QWidget* parent)
: RefreshableWidget(parent)
{
this->setMouseTracking(true);
}
ClickButton::~ClickButton()
{
}
void ClickButton::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
if (this->inWidget(event)) {
this->state = State::PressedState;
this->buttonPressed = true;
}
this->update();
}
}
void ClickButton::mouseMoveEvent(QMouseEvent* event)
{
if (this->inWidget(event)) {
if (this->buttonPressed) {
this->state = State::PressedState;
}
else {
this->state = State::HoverState;
}
}
else {
if (this->buttonPressed) {
this->state = State::HoverState;
}
else {
this->state = State::NormalState;
}
}
this->update();
}
void ClickButton::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton && this->buttonPressed) {
if (this->inWidget(event)) {
if (this->state == State::PressedState) {
this->state = State::NormalState;
this->buttonPressed = false;
this->update();
emit clicked();
return;
}
this->state = State::HoverState;
}
else {
this->state = State::NormalState;
}
this->buttonPressed = false;
this->update();
}
}
void ClickButton::leaveEvent(QEvent* event)
{
Q_UNUSED(event);
this->state = State::NormalState;
this->buttonPressed = false;
this->update();
}
bool ClickButton::inWidget(QMouseEvent* event)
{
return
event->pos().x() >= 0 &&
event->pos().x() <= this->width() &&
event->pos().y() >= 0 &&
event->pos().y() <= this->height();
}