-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQNeumorphism.cpp
188 lines (155 loc) · 5.75 KB
/
QNeumorphism.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
#include "QNeumorphism.h"
#include <QPainter>
QNeumorphism::QNeumorphism(qreal blurRadius, qreal distance, qreal strength, qreal angle, bool inset)
:blurRadius_(blurRadius)
, distance_(distance)
, strength_(strength)
, angle_(angle)
, inset_(inset)
{
}
QNeumorphism::~QNeumorphism()
{
}
qreal QNeumorphism::strength() const
{
return strength_;
}
void QNeumorphism::setStrength(const qreal& strength)
{
strength_ = strength;
update();
}
qreal QNeumorphism::blurRadius() const
{
return blurRadius_;
}
void QNeumorphism::setBlurRadius(const qreal& blurRadius)
{
blurRadius_ = blurRadius;
update();
}
qreal QNeumorphism::distance() const
{
return distance_;
}
void QNeumorphism::setDistance(const qreal& distance)
{
distance_ = distance;
update();
}
qreal QNeumorphism::angle() const
{
return angle_;
}
void QNeumorphism::setAngle(const qreal& angle)
{
angle_ = angle;
update();
}
QRectF QNeumorphism::boundingRectFor(const QRectF& rect) const
{
if (inset_)
return rect.united(rect.translated(0, 0));
return rect.united(rect.translated(0, 0).adjusted(-blurRadius_ - distance_, -blurRadius_ - distance_, blurRadius_ + distance_, blurRadius_ + distance_));
}
void QNeumorphism::draw(QPainter* painter)
{
PixmapPadMode mode = PadToEffectiveBoundingRect;
QPoint pos;
const QPixmap px = sourcePixmap(Qt::DeviceCoordinates, &pos, mode);
if (px.isNull())
return;
QTransform restoreTransform = painter->worldTransform();
painter->setWorldTransform(QTransform());
if (px.isNull())
return;
QImage shadow1(px.size(), QImage::Format_ARGB32_Premultiplied);
shadow1.setDevicePixelRatio(px.devicePixelRatioF());
shadow1.fill(0);
QImage shadow2(px.size(), QImage::Format_ARGB32_Premultiplied);
shadow2.setDevicePixelRatio(px.devicePixelRatioF());
shadow2.fill(0);
QPoint offset;
qreal radian = angle_ / 180 * M_PI;
offset.setX(distance_ * qCos(radian));
offset.setY(distance_ * qSin(radian));
QT_BEGIN_NAMESPACE
extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter * p, QImage & blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
QT_END_NAMESPACE
if (inset_) {
QPainter shadow1Painter(&shadow1);
shadow1Painter.drawPixmap(0, 0, px);
shadow1Painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
shadow1Painter.fillRect(shadow1.rect(), QColor::fromRgbF(0.0, 0.0, 0.0, strength_));
shadow1Painter.end();
QPainter shadow2Painter(&shadow2);
shadow2Painter.drawPixmap(0, 0, px);
shadow2Painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
shadow2Painter.fillRect(shadow1.rect(), QColor::fromRgbF(1.0, 1.0, 1.0, strength_));
shadow2Painter.end();
QPainter shadowPainter(&shadow1);
shadowPainter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
shadowPainter.drawImage(offset, shadow2);
shadowPainter.setCompositionMode(QPainter::CompositionMode_Clear);
offset.setX(qAbs(offset.x()));
offset.setY(qAbs(offset.y()));
QRect rect;
rect.setWidth(shadow1.width() / px.devicePixelRatioF() / px.devicePixelRatioF());
rect.setHeight(shadow1.height() / px.devicePixelRatioF() / px.devicePixelRatioF());
rect.adjust(offset.x(), offset.y(), -offset.x(), -offset.y());
shadowPainter.fillRect(rect, Qt::transparent);
shadowPainter.end();
QImage blurred(px.size(), QImage::Format_ARGB32);
blurred.setDevicePixelRatio(px.devicePixelRatioF());
blurred.fill(0);
QPainter blurPainter(&blurred);
qt_blurImage(&blurPainter, shadow1, blurRadius_, false, false);
blurPainter.end();
QImage image = px.toImage();
QPainter lastPainter(&image);
lastPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
lastPainter.drawImage(blurred.rect(), blurred);
lastPainter.end();
painter->drawImage(pos, image);
}
else {
QPainter shadowPainter(&shadow1);
shadowPainter.setCompositionMode(QPainter::CompositionMode_Source);
shadowPainter.drawPixmap(0, 0, px);
shadowPainter.end();
QImage blurred(shadow1.size(), QImage::Format_ARGB32_Premultiplied);
blurred.setDevicePixelRatio(px.devicePixelRatioF());
blurred.fill(0);
QPainter blurPainter(&blurred);
qt_blurImage(&blurPainter, shadow1, blurRadius_, false, true);
blurPainter.end();
shadow1 = std::move(blurred);
// blacken the image...
shadowPainter.begin(&shadow1);
shadowPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
shadowPainter.fillRect(shadow1.rect(), QColor::fromRgbF(0.0, 0.0, 0.0, strength_));
shadowPainter.end();
shadow2 = shadow1;
// blacken the image...
shadowPainter.begin(&shadow2);
shadowPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
shadowPainter.fillRect(shadow2.rect(), QColor::fromRgbF(1.0, 1.0, 1.0, strength_));
shadowPainter.end();
// draw the blurred drop shadow...
painter->drawImage(pos + offset, shadow1);
painter->drawImage(pos - offset, shadow2);
// Draw the actual pixmap...
painter->drawPixmap(pos, px);
}
painter->setWorldTransform(restoreTransform);
}
bool QNeumorphism::inset() const
{
return inset_;
}
void QNeumorphism::setInset(bool inset)
{
inset_ = inset;
update();
}