forked from shelllet/cpp-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandardbutton.cpp
More file actions
153 lines (136 loc) · 4.15 KB
/
Copy pathstandardbutton.cpp
File metadata and controls
153 lines (136 loc) · 4.15 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "standardbutton.h"
#include "qstyleoption.h"
#include "qsvgrenderer.h"
#include "qpainter.h"
#include "qguiapplication.h"
#include "pugixml.hpp"
#include <array>
#include "qevent.h"
namespace shelllet {
namespace frameless {
static constexpr std::int32_t kNumTypes = 4;
enum class MouseStatus {
None,
Enter,
Leave,
Pressed,
Release
};
class StandardButtonPrivate : public ObjectPrivate {
Q_DECLARE_PUBLIC(StandardButton)
public:
MouseStatus status = {};
Qt::ApplicationState state = {};
std::array<pugi::xml_document, kNumTypes> xmls;
ButtonType type = {};
QByteArray getXmlData(const QColor& c) const {
std::ostringstream stream;
auto& doc = xmls[static_cast<int>(type)];
doc.document_element().first_child().attribute("fill") = c.name().toUtf8().constData();
doc.save(stream);
return QByteArray(stream.str().c_str());
}
const QColor hoverColor = "#686868";
const QColor pressColor = "#484848";
const QColor releaseColor = "#3a3a3a";
const QColor leaveColor = "#3a3a3a";
const QColor hoverColorForClose = "#E81123";
const QColor pressColorForClose = "#F1707A";
void init() {
Q_Q(StandardButton);
xmls[static_cast<int>(ButtonType::Close)].load_string(R"(<svg viewBox="0 0 10.2 10.2">
<polygon fill="#ffffff" points="10.2,0.7 9.5,0 5.1,4.4 0.7,0 0,0.7 4.4,5.1 0,9.5 0.7,10.2 5.1,5.8 9.5,10.2 10.2,9.5 5.8,5.1" />
</svg>)");
xmls[static_cast<int>(ButtonType::Restore)].load_string(R"(<svg viewBox="0 0 10 10">
<path fill="#ffffff" d="M2.1,0v2H0v8.1h8.2v-2h2V0H2.1z M7.2,9.2H1.1V3h6.1V9.2z M9.2,7.1h-1V2H3.1V1h6.1V7.1z" />
</svg>)");
xmls[static_cast<int>(ButtonType::Maximize)].load_string(R"(<svg viewBox="0 0 10 10">
<path fill="#ffffff" d="M0,0v10h10V0H0z M9,9H1V1h8V9z" />
</svg>)");
xmls[static_cast<int>(ButtonType::Minimize)].load_string(R"(<svg viewBox="0 0 10.2 2">
<rect fill="#ffffff" width="10.2" height="1" />
</svg>)");
}
};
}
}
shelllet::frameless::StandardButton::StandardButton(ButtonType t, QWidget* parent /*= nullptr*/)
: QSvgWidget(parent)
, Object(*new StandardButtonPrivate, nullptr)
{
Q_D(StandardButton);
d->type = t;
d->init();
load(d->getXmlData(Qt::white));
QObject::connect(qApp, &QGuiApplication::applicationStateChanged, this, &StandardButton::applicationStateChanged);
}
QSize shelllet::frameless::StandardButton::sizeHint() const
{
return { qRound(geometry().height() * 1.36) ,geometry().height() };
}
void shelllet::frameless::StandardButton::applicationStateChanged(Qt::ApplicationState state)
{
Q_D(StandardButton);
d->state = state;
if (state == Qt::ApplicationActive) {
load(d->getXmlData(Qt::white));
}
else {
load(d->getXmlData(Qt::gray));
}
update();
}
void shelllet::frameless::StandardButton::paintEvent(QPaintEvent* event)
{
Q_D(StandardButton);
QStyleOption opt;
opt.initFrom(this);
QPainter painter(this);
switch (d->status)
{
case MouseStatus::Enter:
painter.fillRect(rect(), d->type == ButtonType::Close ? d->hoverColorForClose : d->hoverColor);
break;
case MouseStatus::Pressed:
painter.fillRect(rect(), d->type == ButtonType::Close ? d->pressColorForClose : d->pressColor);
break;
case MouseStatus::Release:
painter.fillRect(rect(), d->releaseColor);
break;
case MouseStatus::Leave:
painter.fillRect(rect(), d->leaveColor);
break;
default:
//painter.fillRect(rect(), 0x3a3a3a);
break;
}
QRectF rc = QRectF({ .0f, .0f }, renderer()->defaultSize() * 1.0f);
rc.moveCenter(rect().center());
renderer()->render(&painter, rc);
}
void shelllet::frameless::StandardButton::mousePressEvent(QMouseEvent* event)
{
Q_D(StandardButton);
d->status = MouseStatus::Pressed;
update();
}
void shelllet::frameless::StandardButton::enterEvent(QEvent* event)
{
Q_D(StandardButton);
d->status = MouseStatus::Enter;
update();
}
void shelllet::frameless::StandardButton::leaveEvent(QEvent* event)
{
Q_D(StandardButton);
d->status = MouseStatus::Leave;
update();
}
void shelllet::frameless::StandardButton::mouseReleaseEvent(QMouseEvent* event)
{
Q_D(StandardButton);
d->status = MouseStatus::Release;
update();
if (event->button() == Qt::LeftButton && underMouse())
emit clicked();
}