File indexing completed on 2024-12-22 04:31:05

0001 /*
0002  * Copyright (C) 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017  */
0018 
0019 #pragma once
0020 
0021 // Qt
0022 #include <QColor>
0023 #include <QImage>
0024 #include <QPoint>
0025 #include <QSize>
0026 
0027 class BoxShadowRenderer
0028 {
0029 public:
0030     // Compiler generated constructors & destructor are fine.
0031 
0032     /**
0033      * Set the size of the box.
0034      * @param size The size of the box.
0035      **/
0036     void setBoxSize(const QSize &size);
0037 
0038     /**
0039      * Set the radius of box' corners.
0040      * @param radius The border radius, in pixels.
0041      **/
0042     void setBorderRadius(qreal radius);
0043 
0044     /**
0045      * Set the device pixel ratio of the resulting shadow texture.
0046      * @param dpr The device pixel ratio.
0047      **/
0048     void setDevicePixelRatio(qreal dpr);
0049 
0050     /**
0051      * Add a shadow.
0052      * @param offset The offset of the shadow.
0053      * @param radius The blur radius.
0054      * @param color The color of the shadow.
0055      **/
0056     void addShadow(const QPoint &offset, int radius, const QColor &color);
0057 
0058     /**
0059      * Render the shadow.
0060      **/
0061     QImage render() const;
0062 
0063     /**
0064      * Calculate the minimum size of the box.
0065      *
0066      * This helper computes the minimum size of the box so the shadow behind it has
0067      * full its strength.
0068      *
0069      * @param radius The blur radius of the shadow.
0070      **/
0071     static QSize calculateMinimumBoxSize(int radius);
0072 
0073     /**
0074      * Calculate the minimum size of the shadow texture.
0075      *
0076      * This helper computes the minimum size of the resulting texture so the shadow
0077      * is not clipped.
0078      *
0079      * @param boxSize The size of the box.
0080      * @param radius The blur radius.
0081      * @param offset The offset of the shadow.
0082      **/
0083     static QSize calculateMinimumShadowTextureSize(const QSize &boxSize, int radius, const QPoint &offset);
0084 
0085 private:
0086     QSize m_boxSize;
0087     qreal m_borderRadius = 0.0;
0088     qreal m_dpr = 1.0;
0089 
0090     struct Shadow {
0091         QPoint offset;
0092         int radius;
0093         QColor color;
0094     };
0095 
0096     QVector<Shadow> m_shadows;
0097 };