-
Notifications
You must be signed in to change notification settings - Fork 9
/
MakePixmapWidget.cpp
323 lines (299 loc) · 10.1 KB
/
MakePixmapWidget.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* 去掉标题之后添加边框阴影
*
* MakePixmapWidget.cpp
* 构造出边框阴影QImage方法的cpp文件。
*
* FlyWM_
* GitHub: https://github.com/FlyWM
* CSDN: https://blog.csdn.net/a844651990
*
*/
#include <QVBoxLayout>
#include <QPainter>
#include "MakePixmapWidget.h"
inline unsigned char MakeAlpha(int i, double f, int nSize)
{
if (i == nSize)
f *= 1.2;
//
double f2 = 1 - cos((double)i / nSize * 3.14 / 2);
//
return int(fabs((i * f) * f2));
}
QImage MakeShadowImage(int shadowSize, bool activated)
{
int size = shadowSize * 2 + 10;
QImage image(size, size, QImage::Format_ARGB32);
image.fill(QColor(Qt::black));
//
double f = activated ? 4.0 : 1.0;
//
QSize szImage = image.size();
//
//left
for (int y = shadowSize; y < szImage.height() - shadowSize; y++) {
for (int x = 0; x < shadowSize; x++) {
int i = x + 1;
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//right
for (int y = shadowSize; y < szImage.height() - shadowSize; y++) {
for (int x = szImage.width() - shadowSize - 1; x < szImage.width(); x++) {
int i = szImage.width() - x;
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//top
for (int y = 0; y < shadowSize; y++) {
int i = y + 1;
for (int x = shadowSize; x < szImage.width() - shadowSize; x++) {
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
//
}
//bottom
for (int y = szImage.height() - shadowSize - 1; y < szImage.height(); y++) {
int i = szImage.height() - y;
for (int x = shadowSize; x < szImage.width() - shadowSize; x++) {
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//
int parentRoundSize = 3;
//
for (int x = 0; x < shadowSize + parentRoundSize; x++) {
for (int y = 0; y < shadowSize + parentRoundSize; y++) {
int xx = (shadowSize + parentRoundSize) - x;
int yy = (shadowSize + parentRoundSize) - y;
int i = int(sqrt(double(xx * xx + yy * yy)));
i = std::min<int>(shadowSize + parentRoundSize, i);
i -= parentRoundSize;
i = shadowSize - i;
//
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//
for (int x = szImage.width() - shadowSize - parentRoundSize; x < szImage.width(); x++) {
for (int y = 0; y < shadowSize + parentRoundSize; y++) {
int xx = (shadowSize + parentRoundSize) - (szImage.width() - x);
int yy = (shadowSize + parentRoundSize) - y;
int i = int(sqrt(double(xx * xx + yy * yy)));
i = std::min<int>(shadowSize + parentRoundSize, i);
i -= parentRoundSize;
i = shadowSize - i;
//
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//
for (int x = 0; x < shadowSize + parentRoundSize; x++) {
for (int y = szImage.height() - shadowSize - parentRoundSize; y < szImage.height(); y++) {
int xx = (shadowSize + parentRoundSize) - x;
int yy = (shadowSize + parentRoundSize) - (szImage.height() - y);
int i = int(sqrt(double(xx * xx + yy * yy)));
i = std::min<int>(shadowSize + parentRoundSize, i);
i -= parentRoundSize;
i = shadowSize - i;
//
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//
for (int x = szImage.width() - shadowSize - parentRoundSize; x < szImage.width(); x++) {
for (int y = szImage.height() - shadowSize - parentRoundSize; y < szImage.height(); y++) {
int xx = (shadowSize + parentRoundSize) - (szImage.width() - x);
int yy = (shadowSize + parentRoundSize) - (szImage.height() - y);
int i = int(sqrt(double(xx * xx + yy * yy)));
i = std::min<int>(shadowSize + parentRoundSize, i);
i -= parentRoundSize;
i = shadowSize - i;
//
int alpha = MakeAlpha(i, f, shadowSize);
image.setPixelColor(x, y, QColor(0, 0, 0, alpha));
}
}
//
int borderR = 165;
int borderG = 165;
int borderB = 165;
//
if (activated) {
borderR = 68;
borderG = 138;
borderB = 255;
// borderR = 0;
// borderG = 0;
// borderB = 0;
}
//
int borderSize = 1;
//left
for (int i = 0; i < borderSize; i++) {
for (int y = shadowSize - 1; y < szImage.height() - shadowSize + 1; y++) {
int x = shadowSize - i - 1;
image.setPixelColor(x, y, QColor(borderR, borderG, borderB, 255));
}
}
//right
for (int i = 0; i < borderSize; i++) {
for (int y = shadowSize - 1; y < szImage.height() - shadowSize + 1; y++) {
int x = szImage.width() - shadowSize - 1 + i + 1;
image.setPixelColor(x, y, QColor(borderR, borderG, borderB, 255));
}
}
//top
for (int i = 0; i < borderSize; i++) {
for (int x = shadowSize; x < szImage.width() - shadowSize; x++) {
int y = shadowSize - i - 1;
image.setPixelColor(x, y, QColor(borderR, borderG, borderB, 255));
}
}
//bottom
for (int i = 0; i < borderSize; i++) {
for (int x = shadowSize; x < szImage.width() - shadowSize; x++) {
int y = szImage.height() - shadowSize - 1 + i + 1;
image.setPixelColor(x, y, QColor(borderR, borderG, borderB, 255));
}
}
//
return image;
}
bool Skin9GridImage::clear()
{
if (!m_img.isNull()) {
m_img = QImage();
}
return true;
}
bool Skin9GridImage::splitRect(const QRect &rcSrc, QPoint ptTopLeft, QRect *parrayRect, int nArrayCount)
{
Q_ASSERT(nArrayCount == 9);
//
QRect* arrayRect = parrayRect;
//
int nWidth = rcSrc.width();
int nHeight = rcSrc.height();
//
if (ptTopLeft.x() <= 0)
return false;
if (ptTopLeft.y() <= 0)
return false;
if (ptTopLeft.x() >= nWidth / 2)
return false;
if (ptTopLeft.y() >= nHeight / 2)
return false;
//
int x1 = rcSrc.left() + 0;
int x2 = rcSrc.left() + ptTopLeft.x();
int x3 = rcSrc.left() + nWidth - ptTopLeft.x();
int x4 = rcSrc.left() + nWidth;
//
int y1 = rcSrc.top() + 0;
int y2 = rcSrc.top() + ptTopLeft.y();
int y3 = rcSrc.top() + nHeight - ptTopLeft.y();
int y4 = rcSrc.top() + nHeight;
//
arrayRect[0] = QRect(QPoint(x1, y1), QPoint(x2, y2));
arrayRect[1] = QRect(QPoint(x2 + 1, y1), QPoint(x3, y2));
arrayRect[2] = QRect(QPoint(x3 + 1, y1), QPoint(x4, y2));
arrayRect[3] = QRect(QPoint(x1, y2 + 1), QPoint(x2, y3));
arrayRect[4] = QRect(QPoint(x2 + 1, y2 + 1), QPoint(x3, y3));
arrayRect[5] = QRect(QPoint(x3 + 1, y2 + 1), QPoint(x4, y3));
arrayRect[6] = QRect(QPoint(x1, y3 + 1), QPoint(x2, y4));
arrayRect[7] = QRect(QPoint(x2 + 1, y3 + 1), QPoint(x3, y4));
arrayRect[8] = QRect(QPoint(x3 + 1, y3 + 1), QPoint(x4, y4));
//
return true;
}
bool Skin9GridImage::setImage(const QImage &image, QPoint ptTopLeft)
{
clear();
//
m_img = image;
//
int nImageWidth = m_img.width();
int nImageHeight = m_img.height();
//
return splitRect(QRect(0, 0, nImageWidth, nImageHeight), ptTopLeft, m_arrayImageGrid, 9);
}
void Skin9GridImage::drawBorder(QPainter *p, QRect rc) const
{
QRect arrayDest[9];
//
splitRect(rc, m_arrayImageGrid[0].bottomRight(), arrayDest, 9);
//
for (int i = 0; i < 9; i++) {
if (i == 4)
continue;
//
const QRect& rcSrc = m_arrayImageGrid[i];
const QRect& rcDest = arrayDest[i];
//
p->drawImage(rcDest, m_img, rcSrc);
}
}
ShadowWidget::ShadowWidget(int shadowSize, QWidget *parent)
: m_shadowSize(shadowSize)
, QWidget(parent)
, m_shadow(new Skin9GridImage())
{
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlag(Qt::FramelessWindowHint);
setMouseTracking(true);
//
QImage image = MakeShadowImage(shadowSize, true);
m_shadow->setImage(image, QPoint(shadowSize + 1, shadowSize + 1));
}
void ShadowWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e)
QPainter painter(this);
m_shadow->drawBorder(&painter, rect());
}
MakePixmapWidget::MakePixmapWidget(QWidget *parent)
: QWidget(parent)
{
resize(400, 300);
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);
setContentsMargins(0, 0, 0, 0);
QVBoxLayout *pLayout = new QVBoxLayout(this);
pLayout->setContentsMargins(0, 0, 0, 0);
pLayout->setSpacing(0);
int shadowSize = 20;
ShadowWidget *pShadowWidget = new ShadowWidget(shadowSize, this);
pShadowWidget->setContentsMargins(shadowSize, shadowSize, shadowSize, shadowSize);
pLayout->addWidget(pShadowWidget);
QLayout* rootLayout = new QBoxLayout(QBoxLayout::TopToBottom);
pShadowWidget->setLayout(rootLayout);
rootLayout->setContentsMargins(0, 0, 0, 0);
rootLayout->setSpacing(0);
QWidget* shadowClientWidget = new QWidget(pShadowWidget);
rootLayout->addWidget(shadowClientWidget);
//
QVBoxLayout* shadowClientLayout = new QVBoxLayout();
shadowClientLayout->setContentsMargins(0, 0, 0, 0);
shadowClientLayout->setSpacing(0);
shadowClientWidget->setLayout(shadowClientLayout);
shadowClientWidget->setAutoFillBackground(true);
shadowClientWidget->setCursor(QCursor(Qt::ArrowCursor));
//
QWidget *clientWidget = new QWidget(shadowClientWidget);
shadowClientLayout->addWidget(clientWidget);
//
QVBoxLayout *clientLayout = new QVBoxLayout();
clientWidget->setLayout(clientLayout);
//
clientLayout->setSpacing(0);
clientLayout->setContentsMargins(0, 0, 0, 0);
}