-
Notifications
You must be signed in to change notification settings - Fork 1
/
active_button.cpp
70 lines (59 loc) · 1.47 KB
/
active_button.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
#include "stdafx.h"
#include "active_button.h"
namespace psv
{
active_button::active_button(const QPixmap& normal, const QPixmap& active, const QPixmap& hover, QWidget* parent) :
QAbstractButton(parent),
normal_(normal),
active_(active),
hover_(hover),
current_(&normal_)
{
setMouseTracking(true);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
set_current(normal_);
}
active_button::~active_button()
{
}
void active_button::enterEvent(QEvent* e)
{
set_current(hover_);
QAbstractButton::enterEvent(e);
}
void active_button::leaveEvent(QEvent* e)
{
set_current(normal_);
QAbstractButton::leaveEvent(e);
}
void active_button::mousePressEvent(QMouseEvent* e)
{
set_current(active_);
QAbstractButton::mousePressEvent(e);
}
void active_button::mouseReleaseEvent(QMouseEvent* e)
{
set_current(normal_);
QAbstractButton::mouseReleaseEvent(e);
}
void active_button::set_current(QPixmap& pix)
{
current_ = &pix;
resize(pix.size());
updateGeometry();
// For some unknown reasons if here is update and not repaint than paintEvent sometimes is not called
// This happens in situation when minimized button is pressed, window minimezes, after reopens to normal
// and buttons doesn't update on hover even if update is called
repaint();
}
void active_button::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawPixmap(0, 0, *current_);
}
QSize active_button::sizeHint() const
{
return current_->size();
}
}