Menu

[r560]: / src / controlbutton.cpp  Maximize  Restore  History

Download this file

99 lines (78 with data), 2.4 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "controlbutton.h"
ControlButton::ControlButton(QWidget * parent)
: QPushButton(parent), m_hovered(false), m_pressed(false), m_opacity(1.0)
{
}
ControlButton::ControlButton(const QIcon & icon, const QIcon &iconHovered, const QIcon &iconPressed, QWidget *parent)
: QPushButton(parent), m_icon(icon), m_iconHovered(iconHovered), m_iconPressed(iconPressed),
m_hovered(false), m_pressed(false), m_opacity(1.0)
{
}
ControlButton::~ControlButton(){}
void ControlButton::paintEvent(QPaintEvent * pe)
{
Q_UNUSED(pe);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//test for state changes
QColor button_color;
QIcon button_icon;
if(this->isEnabled())
{
if(m_hovered)
button_icon = m_iconHovered;
else
button_icon = m_icon;
if(m_pressed)
button_icon = m_iconPressed;
}
else
{
button_color = QColor(50, 50, 50);
}
QRect button_rect = this->geometry();
if(!button_icon.isNull())
{
QSize icon_size = this->iconSize();
QRect icon_position = this->calculateIconPosition(button_rect, icon_size);
painter.setOpacity(m_opacity);
painter.drawPixmap(icon_position, QPixmap(button_icon.pixmap(icon_size)));
}
}
void ControlButton::enterEvent(QEvent * e)
{
m_hovered = true;
this->repaint();
QPushButton::enterEvent(e);
}
void ControlButton::leaveEvent(QEvent * e)
{
m_hovered = false;
this->repaint();
QPushButton::leaveEvent(e);
}
void ControlButton::mousePressEvent(QMouseEvent * e)
{
m_pressed = true;
this->repaint();
QPushButton::mousePressEvent(e);
}
void ControlButton::mouseReleaseEvent(QMouseEvent * e)
{
m_pressed = false;
this->repaint();
QPushButton::mouseReleaseEvent(e);
}
QRect ControlButton::calculateIconPosition(QRect button_rect, QSize icon_size)
{
int x = (button_rect.width() / 2) - (icon_size.width() / 2);
int y = (button_rect.height() / 2) - (icon_size.height() / 2);
int width = icon_size.width();
int height = icon_size.height();
QRect icon_position;
icon_position.setX(x);
icon_position.setY(y);
icon_position.setWidth(width);
icon_position.setHeight(height);
return icon_position;
}