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

0001 /*
0002  * Copyright (C) 2021 CutefishOS Team.
0003  *
0004  * Author:     revenmartin <revenmartin@gmail.com>
0005  *
0006  * This program is free software: you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation, either version 3 of the License, or
0009  * any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef WINDOWSHADOW_H
0021 #define WINDOWSHADOW_H
0022 
0023 #include "tileset.h"
0024 
0025 
0026 #include <QGuiApplication>
0027 #include <QMap>
0028 #include <QObject>
0029 #include <QPainter>
0030 #include <QPixmap>
0031 #include <QQmlEngine>
0032 #include <QQmlParserStatus>
0033 #include <QRect>
0034 #include <QWindow>
0035 #include <QVector>
0036 
0037 struct ShadowParams
0038 {
0039     ShadowParams() = default;
0040 
0041     ShadowParams(const QPoint &offset, int radius, qreal opacity):
0042         offset(offset),
0043         radius(radius),
0044         opacity(opacity)
0045     {}
0046 
0047     QPoint offset;
0048     int radius = 0;
0049     qreal opacity = 0;
0050 };
0051 
0052 struct CompositeShadowParams
0053 {
0054     CompositeShadowParams() = default;
0055 
0056     CompositeShadowParams(
0057             const QPoint &offset,
0058             const ShadowParams &shadow1,
0059             const ShadowParams &shadow2)
0060         : offset(offset)
0061         , shadow1(shadow1)
0062         , shadow2(shadow2) {}
0063 
0064     bool isNone() const
0065     { return qMax(shadow1.radius, shadow2.radius) == 0; }
0066 
0067     QPoint offset;
0068     ShadowParams shadow1;
0069     ShadowParams shadow2;
0070 };
0071 
0072 class WindowShadow : public QObject, public QQmlParserStatus
0073 {
0074     Q_OBJECT
0075     Q_INTERFACES(QQmlParserStatus)
0076     Q_PROPERTY(QWindow *view READ view WRITE setView NOTIFY viewChanged)
0077     Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry NOTIFY geometryChanged)
0078     Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged)
0079     Q_PROPERTY(qreal strength READ strength WRITE setStrength NOTIFY strengthChanged)
0080 
0081 public:
0082     WindowShadow(QObject *parent = nullptr) noexcept;
0083     ~WindowShadow() override;
0084 
0085     static CompositeShadowParams lookupShadowParams(int shadowSizeEnum);
0086 
0087     void classBegin() override;
0088     void componentComplete() override;
0089 
0090     void setView(QWindow *view);
0091     QWindow *view() const;
0092 
0093     void setGeometry(const QRect &rect);
0094     QRect geometry() const;
0095 
0096     void setRadius(qreal value);
0097     qreal radius() { return m_radius; }
0098 
0099     qreal strength() const;
0100     void setStrength(qreal strength);
0101 
0102 private slots:
0103     void onViewVisibleChanged(bool);
0104 
0105 private:
0106     void configureTiles();
0107     TileSet shadowTiles();
0108 
0109     QMargins shadowMargins(TileSet) const;
0110 
0111 signals:
0112     void geometryChanged();
0113     void enabledChanged();
0114     void viewChanged();
0115     void edgesChanged();
0116     void radiusChanged();
0117     void strengthChanged();
0118 
0119 private:
0120     QWindow *m_view;
0121     QRect m_rect;
0122     TileSet m_shadowTiles;
0123     qreal m_radius = 10;
0124     qreal m_strength = 1.2;
0125 };
0126 
0127 #endif