-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathstyledtoolbutton.cpp
141 lines (114 loc) · 4.73 KB
/
styledtoolbutton.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
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
// This file is a part of "grblControl" application.
// Copyright 2015 Hayrullin Denis Ravilevich
#include "styledtoolbutton.h"
#include <QDebug>
#include <QEvent>
StyledToolButton::StyledToolButton(QWidget *parent) : QAbstractButton(parent)
{
m_hovered = false;
m_backColor = palette().color(QPalette::Button);
m_foreColor = palette().color(QPalette::ButtonText);
m_highlightColor = QColor(127, 211, 255).darker(120);
}
bool StyledToolButton::isHover()
{
return m_hovered;
}
void StyledToolButton::enterEvent(QEvent *e)
{
m_hovered = true;
}
void StyledToolButton::leaveEvent(QEvent *e)
{
m_hovered = false;
}
void StyledToolButton::paintEvent(QPaintEvent *e)
{
const int borderWidth = 4;
const int borderRadius = 5;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);
// Highlight
QPen highlightPen;
if ((!this->isEnabled() && !this->isChecked()) || (!this->isDown() && !this->isChecked() && !this->isHover())) {
highlightPen.setColor(Qt::white);
} else if (this->isDown() || this->isChecked()) {
highlightPen.setColor(m_highlightColor);
} else if (this->isHover()) {
highlightPen.setColor(m_highlightColor.lighter(120));
}
highlightPen.setWidth(2);
painter.setPen(highlightPen);
painter.drawRoundedRect(1, 1, this->width() - 2, this->height() - 2, borderRadius - 1, borderRadius - 1);
// Border
QPen pen(this->isEnabled() ? palette().color(QPalette::Shadow) : palette().color(QPalette::Mid));
if ((this->isDown() || this->isChecked()) && this->isEnabled()) pen.setColor(Qt::black);
pen.setWidth(2);
pen.setCapStyle(Qt::SquareCap);
painter.setPen(pen);
painter.drawLine(borderRadius, 0, width() - borderRadius, 0);
painter.drawLine(borderRadius, height(), width() - borderRadius, height());
painter.drawLine(0, borderRadius, 0, height() - borderRadius);
painter.drawLine(width(), borderRadius, width(), height() - borderRadius);
pen.setWidth(1);
painter.setPen(pen);
painter.drawArc(0, 0, borderRadius * 2, borderRadius * 2, 90 * 16, 90 * 16);
painter.drawArc(width() - borderRadius * 2, 0, borderRadius * 2, borderRadius * 2, 0 * 16, 90 * 16);
painter.drawArc(0, height() - borderRadius * 2, borderRadius * 2, borderRadius * 2, 180 * 16, 90 * 16);
painter.drawArc(width() - borderRadius * 2, height() - borderRadius * 2, borderRadius * 2, borderRadius * 2, 270 * 16, 90 * 16);
// Background border
QLinearGradient backGradient(width() / 2, height() / 2, width() / 2, height());
backGradient.setColorAt(0, this->isEnabled() ? m_backColor : palette().color(QPalette::Button));
backGradient.setColorAt(1, this->isEnabled() ? m_backColor.darker(130) : palette().color(QPalette::Button).darker(130));
QBrush backBrush(backGradient);
painter.setBrush(backBrush);
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(borderWidth - 1, borderWidth - 1, width() - borderWidth * 2 + 2, height() - borderWidth * 2 + 2, 2, 2);
// Background
painter.setBrush(this->isEnabled() ? m_backColor : palette().color(QPalette::Button));
painter.setPen(Qt::NoPen);
painter.drawRect(borderWidth, borderWidth, width() - borderWidth * 2, height() - borderWidth * 2);
// Icon/text rect
QRect innerRect(borderWidth, borderWidth, width() - borderWidth * 2, height() - borderWidth * 2);
if (this->isDown() || this->isChecked()) {
innerRect.setLeft(innerRect.left() + 2);
innerRect.setTop(innerRect.top() + 2);
}
// Icon
if (!this->icon().isNull()) {
QSize iconSize = this->icon().actualSize(this->iconSize());
painter.drawPixmap(QRect(innerRect.x() + (innerRect.width() - iconSize.width()) / 2,
innerRect.y() + (innerRect.height() - iconSize.height()) / 2,
iconSize.width(), iconSize.height()),
this->icon().pixmap(iconSize, this->isEnabled() ? QIcon::Normal : QIcon::Disabled));
} else {
// Text
painter.setPen(this->isEnabled() ? m_foreColor : palette().color(QPalette::Mid));
painter.drawText(innerRect, Qt::AlignCenter, this->text());
}
}
QColor StyledToolButton::highlightColor() const
{
return m_highlightColor;
}
void StyledToolButton::setHighlightColor(const QColor &highlightColor)
{
m_highlightColor = highlightColor;
}
QColor StyledToolButton::foreColor() const
{
return m_foreColor;
}
void StyledToolButton::setForeColor(const QColor &foreColor)
{
m_foreColor = foreColor;
}
QColor StyledToolButton::backColor() const
{
return m_backColor;
}
void StyledToolButton::setBackColor(const QColor &backColor)
{
m_backColor = backColor;
}