| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kshadowhelper.h" |
| 24 |
#include <KWindowShadow> |
| 25 |
#include <KWindowShadowTile> |
| 26 |
#include <QLinearGradient> |
| 27 |
#include <QMap> |
| 28 |
#include <QPainter> |
| 29 |
#include <QPalette> |
| 30 |
#include <QPixmap> |
| 31 |
|
| 32 |
extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed); |
| 33 |
|
| 34 |
namespace kdk |
| 35 |
{ |
| 36 |
namespace effects |
| 37 |
{ |
| 38 |
|
| 39 |
class KShadowHelperPrivate : public QObject |
| 40 |
{ |
| 41 |
Q_DECLARE_PUBLIC(KShadowHelper) |
| 42 |
Q_OBJECT |
| 43 |
|
| 44 |
public: |
| 45 |
KShadowHelperPrivate(KShadowHelper *parent); |
| 46 |
QPixmap getShadowPixmap(QColor color, |
| 47 |
int shadowBorder, |
| 48 |
qreal darkness, |
| 49 |
int borderRadius = 0); |
| 50 |
KWindowShadow *getShadow(QColor color, int shadowBorder, |
| 51 |
qreal darkness, |
| 52 |
int borderRadius = 0); |
| 53 |
|
| 54 |
protected: |
| 55 |
bool eventFilter(QObject *watched, QEvent *event); |
| 56 |
|
| 57 |
private: |
| 58 |
KShadowHelper *q_ptr; |
| 59 |
QMap<const QWidget *, KWindowShadow *> m_shadows; |
| 60 |
}; |
| 61 |
|
| 62 |
static KShadowHelper *g_kshadowHelper = nullptr; |
| 63 |
|
| 64 |
KShadowHelper *KShadowHelper::self() |
| 65 |
{ |
| 66 |
if (g_kshadowHelper) |
| 67 |
return g_kshadowHelper; |
| 68 |
g_kshadowHelper = new KShadowHelper(); |
| 69 |
return g_kshadowHelper; |
| 70 |
} |
| 71 |
|
| 72 |
void KShadowHelper::setWidget(QWidget *widget, int borderRadius, int shadowWidth, qreal darkness) |
| 73 |
{ |
| 74 |
Q_D(KShadowHelper); |
| 75 |
KWindowShadow *shadow = d->getShadow(QColor(26, 26, 26), shadowWidth, darkness, borderRadius); |
| 76 |
shadow->setPadding(QMargins(shadowWidth, shadowWidth, shadowWidth, shadowWidth)); |
| 77 |
|
| 78 |
widget->installEventFilter(d); |
| 79 |
d->m_shadows.insert(widget, shadow); |
| 80 |
connect(widget, &QWidget::destroyed, this, [=]() { |
| 81 |
if (auto shadowToBeDelete = d->m_shadows.value(widget)) { |
| 82 |
if (shadowToBeDelete->isCreated()) |
| 83 |
shadowToBeDelete->destroy(); |
| 84 |
shadowToBeDelete->deleteLater(); |
| 85 |
d->m_shadows.remove(widget); |
| 86 |
} |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
KShadowHelper::KShadowHelper(QObject *parent) |
| 91 |
: QObject(parent) |
| 92 |
, d_ptr(new KShadowHelperPrivate(this)) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
KShadowHelperPrivate::KShadowHelperPrivate(KShadowHelper *parent) |
| 97 |
: q_ptr(parent) |
| 98 |
{ |
| 99 |
} |
| 100 |
|
| 101 |
QPixmap KShadowHelperPrivate::getShadowPixmap(QColor color, int shadowBorder, qreal darkness, int borderRadius) |
| 102 |
{ |
| 103 |
QPixmap pixmap(QSize(100, 100)); |
| 104 |
pixmap.fill(Qt::transparent); |
| 105 |
QPainter painter(&pixmap); |
| 106 |
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
| 107 |
painter.setBrush(color); |
| 108 |
painter.setPen(Qt::NoPen); |
| 109 |
painter.drawRoundedRect(pixmap.rect().adjusted(shadowBorder, shadowBorder, -shadowBorder, -shadowBorder), borderRadius, borderRadius); |
| 110 |
|
| 111 |
QImage rawImg = pixmap.toImage(); |
| 112 |
qt_blurImage(rawImg, shadowBorder, true, true); |
| 113 |
|
| 114 |
for (int x = 0; x < rawImg.width(); x++) { |
| 115 |
for (int y = 0; y < rawImg.height(); y++) { |
| 116 |
auto color = rawImg.pixelColor(x, y); |
| 117 |
if (color.alpha() == 0) |
| 118 |
continue; |
| 119 |
color.setAlphaF(darkness * color.alphaF()); |
| 120 |
rawImg.setPixelColor(x, y, color); |
| 121 |
} |
| 122 |
} |
| 123 |
QPixmap target = QPixmap::fromImage(rawImg); |
| 124 |
QPainter painter2(&target); |
| 125 |
painter2.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
| 126 |
painter2.setCompositionMode(QPainter::CompositionMode_Clear); |
| 127 |
painter2.setBrush(Qt::transparent); |
| 128 |
painter2.setPen(Qt::NoPen); |
| 129 |
painter2.drawRoundedRect(target.rect().adjusted(shadowBorder, shadowBorder, -shadowBorder, -shadowBorder), borderRadius, borderRadius); |
| 130 |
return target; |
| 131 |
} |
| 132 |
|
| 133 |
KWindowShadow *KShadowHelperPrivate::getShadow(QColor color, int shadowBorder, qreal darkness, int borderRadius) |
| 134 |
{ |
| 135 |
QPixmap pixmap = getShadowPixmap(color, shadowBorder, darkness, borderRadius); |
| 136 |
int parm = shadowBorder * 2; |
| 137 |
QPixmap topLeftPixmap = pixmap.copy(0, 0, parm, parm); |
| 138 |
QPixmap topPixmap = pixmap.copy(parm, 0, pixmap.width() - parm * 2, parm); |
| 139 |
QPixmap topRightPixmap = pixmap.copy(pixmap.width() - parm, 0, parm, parm); |
| 140 |
QPixmap leftPixmap = pixmap.copy(0, parm, parm, pixmap.height() - parm * 2); |
| 141 |
QPixmap rightPixmap = pixmap.copy(pixmap.width() - parm, parm, parm, pixmap.height() - parm * 2); |
| 142 |
QPixmap bottomLeftPixmap = pixmap.copy(0, pixmap.height() - parm, parm, parm); |
| 143 |
QPixmap bottomPixmap = pixmap.copy(parm, pixmap.height() - parm, pixmap.width() - parm * 2, parm); |
| 144 |
QPixmap bottomRightPixmap = pixmap.copy(pixmap.width() - parm, pixmap.height() - parm, parm, parm); |
| 145 |
|
| 146 |
KWindowShadow *shadow = new KWindowShadow; |
| 147 |
KWindowShadowTile::Ptr topLeftTile = KWindowShadowTile::Ptr::create(); |
| 148 |
topLeftTile.get()->setImage(topLeftPixmap.toImage()); |
| 149 |
shadow->setTopLeftTile(topLeftTile); |
| 150 |
KWindowShadowTile::Ptr topTile = KWindowShadowTile::Ptr::create(); |
| 151 |
topTile.get()->setImage(topPixmap.toImage()); |
| 152 |
shadow->setTopTile(topTile); |
| 153 |
KWindowShadowTile::Ptr topRightTile = KWindowShadowTile::Ptr::create(); |
| 154 |
topRightTile.get()->setImage(topRightPixmap.toImage()); |
| 155 |
shadow->setTopRightTile(topRightTile); |
| 156 |
KWindowShadowTile::Ptr leftTile = KWindowShadowTile::Ptr::create(); |
| 157 |
leftTile.get()->setImage(leftPixmap.toImage()); |
| 158 |
shadow->setLeftTile(leftTile); |
| 159 |
KWindowShadowTile::Ptr rightTile = KWindowShadowTile::Ptr::create(); |
| 160 |
rightTile.get()->setImage(rightPixmap.toImage()); |
| 161 |
shadow->setRightTile(rightTile); |
| 162 |
KWindowShadowTile::Ptr bottomLeftTile = KWindowShadowTile::Ptr::create(); |
| 163 |
bottomLeftTile.get()->setImage(bottomLeftPixmap.toImage()); |
| 164 |
shadow->setBottomLeftTile(bottomLeftTile); |
| 165 |
KWindowShadowTile::Ptr bottomTile = KWindowShadowTile::Ptr::create(); |
| 166 |
bottomTile.get()->setImage(bottomPixmap.toImage()); |
| 167 |
shadow->setBottomTile(bottomTile); |
| 168 |
KWindowShadowTile::Ptr bottomRightTile = KWindowShadowTile::Ptr::create(); |
| 169 |
bottomRightTile.get()->setImage(bottomRightPixmap.toImage()); |
| 170 |
shadow->setBottomRightTile(bottomRightTile); |
| 171 |
return shadow; |
| 172 |
} |
| 173 |
|
| 174 |
bool KShadowHelperPrivate::eventFilter(QObject *watched, QEvent *event) |
| 175 |
{ |
| 176 |
if (watched->isWidgetType()) { |
| 177 |
auto widget = qobject_cast<QWidget *>(watched); |
| 178 |
if (widget->isTopLevel() && event->type() == QEvent::Show) { |
| 179 |
if (auto shadow = m_shadows.value(widget)) { |
| 180 |
shadow->setWindow(widget->windowHandle()); |
| 181 |
shadow->create(); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
return QObject::eventFilter(watched, event); |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
} |
| 190 |
#include "kshadowhelper.moc" |
| 191 |
#include "moc_kshadowhelper.cpp" |