File indexing completed on 2024-04-21 05:26:14

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 // Qt
0010 #include <QColor>
0011 #include <QImage>
0012 #include <QPoint>
0013 #include <QSize>
0014 
0015 namespace Breeze
0016 {
0017 class BoxShadowRenderer
0018 {
0019 public:
0020     // Compiler generated constructors & destructor are fine.
0021 
0022     /**
0023      * Set the size of the box.
0024      * @param size The size of the box.
0025      **/
0026     void setBoxSize(const QSize &size);
0027 
0028     /**
0029      * Set the radius of box' corners.
0030      * @param radius The border radius, in pixels.
0031      **/
0032     void setBorderRadius(qreal radius);
0033 
0034     /**
0035      * Add a shadow.
0036      * @param offset The offset of the shadow.
0037      * @param radius The blur radius.
0038      * @param color The color of the shadow.
0039      **/
0040     void addShadow(const QPoint &offset, int radius, const QColor &color);
0041 
0042     /**
0043      * Render the shadow.
0044      **/
0045     QImage render() const;
0046 
0047     /**
0048      * Calculate the minimum size of the box.
0049      *
0050      * This helper computes the minimum size of the box so the shadow behind it has
0051      * full its strength.
0052      *
0053      * @param radius The blur radius of the shadow.
0054      **/
0055     static QSize calculateMinimumBoxSize(int radius);
0056 
0057     /**
0058      * Calculate the minimum size of the shadow texture.
0059      *
0060      * This helper computes the minimum size of the resulting texture so the shadow
0061      * is not clipped.
0062      *
0063      * @param boxSize The size of the box.
0064      * @param radius The blur radius.
0065      * @param offset The offset of the shadow.
0066      **/
0067     static QSize calculateMinimumShadowTextureSize(const QSize &boxSize, int radius, const QPoint &offset);
0068 
0069 private:
0070     QSize m_boxSize;
0071     qreal m_borderRadius = 0.0;
0072 
0073     struct Shadow {
0074         QPoint offset;
0075         int radius;
0076         QColor color;
0077     };
0078 
0079     QVector<Shadow> m_shadows;
0080 };
0081 
0082 } // namespace Breeze