1 /*
2  * SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 
7 #pragma once
8 
9 // own
10 #include "breezecommon_export.h"
11 
12 // Qt
13 #include <QColor>
14 #include <QImage>
15 #include <QPoint>
16 #include <QSize>
17 
18 namespace Breeze
19 {
20 
21 class BREEZECOMMON_EXPORT BoxShadowRenderer
22 {
23 public:
24     // Compiler generated constructors & destructor are fine.
25 
26     /**
27      * Set the size of the box.
28      * @param size The size of the box.
29      **/
30     void setBoxSize(const QSize &size);
31 
32     /**
33      * Set the radius of box' corners.
34      * @param radius The border radius, in pixels.
35      **/
36     void setBorderRadius(qreal radius);
37 
38     /**
39      * Set the device pixel ratio of the resulting shadow texture.
40      * @param dpr The device pixel ratio.
41      **/
42     void setDevicePixelRatio(qreal dpr);
43 
44     /**
45      * Add a shadow.
46      * @param offset The offset of the shadow.
47      * @param radius The blur radius.
48      * @param color The color of the shadow.
49      **/
50     void addShadow(const QPoint &offset, int radius, const QColor &color);
51 
52     /**
53      * Render the shadow.
54      **/
55     QImage render() const;
56 
57     /**
58      * Calculate the minimum size of the box.
59      *
60      * This helper computes the minimum size of the box so the shadow behind it has
61      * full its strength.
62      *
63      * @param radius The blur radius of the shadow.
64      **/
65     static QSize calculateMinimumBoxSize(int radius);
66 
67     /**
68      * Calculate the minimum size of the shadow texture.
69      *
70      * This helper computes the minimum size of the resulting texture so the shadow
71      * is not clipped.
72      *
73      * @param boxSize The size of the box.
74      * @param radius The blur radius.
75      * @param offset The offset of the shadow.
76      **/
77     static QSize calculateMinimumShadowTextureSize(const QSize &boxSize, int radius, const QPoint &offset);
78 
79 private:
80     QSize m_boxSize;
81     qreal m_borderRadius = 0.0;
82     qreal m_dpr = 1.0;
83 
84     struct Shadow {
85         QPoint offset;
86         int radius;
87         QColor color;
88     };
89 
90     QVector<Shadow> m_shadows;
91 };
92 
93 } // namespace Breeze
94