Repository navigation
Expand file tree
/
Copy pathState_Button.cpp
More file actions
137 lines (115 loc) · 2.78 KB
/
Copy pathState_Button.cpp
File metadata and controls
137 lines (115 loc) · 2.78 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
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
#include "State_Button.h"
State_Button::State_Button(QWidget *parent)
:QPushButton(parent)
,status_(NORMAL)
,mouse_pressed_(false)
{
setStyleSheet("QPushButton{background:transparent;}");
}
State_Button::~State_Button()
{
}
void State_Button::load_pximap(const QString& pic_name, int state_count)
{
pixmap_type_ = FOREGROUND;
pixmap_.load(pic_name);
state_count_ = state_count;
width_ = pixmap_.width()/state_count;
height_ = pixmap_.height();
setFixedSize(width_, height_);
}
void State_Button::set_pixmap( const QPixmap& pixmap, int state_count )
{
pixmap_type_ = FOREGROUND;
pixmap_ = pixmap;
state_count_ = state_count;
width_ = pixmap_.width()/state_count;
height_ = pixmap_.height();
setFixedSize(width_, height_);
}
void State_Button::load_background( const QString& pic_name, int state_count/*=4*/ )
{
load_pximap(pic_name, state_count);
pixmap_type_ = BACKGROUND;
}
void State_Button::set_background( const QPixmap& pixmap, int state_count/*=4*/ )
{
set_pixmap(pixmap, state_count);
pixmap_type_ = BACKGROUND;
}
void State_Button::enterEvent(QEvent *)
{
status_ = HOVER;
update();
}
void State_Button::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
mouse_pressed_ = true;
status_ = PRESSED;
update();
}
QPushButton::mousePressEvent(event);
}
void State_Button::mouseReleaseEvent(QMouseEvent *event)
{
if (mouse_pressed_)
{
mouse_pressed_ = false;
if (this->rect().contains(event->pos()))
{
//if (isCheckable())
//{
// setChecked(!isChecked());
//}
//emit clicked();
status_ = HOVER;
}else{
status_ = NORMAL;
}
update();
}
QPushButton::mouseReleaseEvent(event);
}
void State_Button::leaveEvent(QEvent *)
{
status_ = NORMAL;
update();
}
void State_Button::paintEvent(QPaintEvent *e)
{
if (pixmap_type_ == BACKGROUND)
{
paint_pixmap();
}
QPushButton::paintEvent(e);
if (pixmap_type_ == FOREGROUND)
{
paint_pixmap();
}
}
void State_Button::paint_pixmap()
{
QPainter painter(this);
//根据状态显示图片
ButtonStatus status = status_;
if (!isEnabled())
{
status = DISABLED;
}
else if (isChecked())
{
status = CHECKED;
//没有checked图片,用hover代替
if (status >= state_count_)
{
status = HOVER;
}
}
if (status >= state_count_)
{
status = NORMAL;
}
painter.drawPixmap(rect(), pixmap_.copy(width_ * status, 0, width_, height_));
}