File indexing completed on 2024-05-12 13:30:20

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef breezemdiwindowshadow_h
0008 #define breezemdiwindowshadow_h
0009 
0010 #include <QEvent>
0011 #include <QObject>
0012 #include <QSet>
0013 
0014 #include <QPaintEvent>
0015 #include <QPointer>
0016 #include <QWidget>
0017 
0018 #include "breezetileset.h"
0019 
0020 namespace Breeze
0021 {
0022 class ShadowHelper;
0023 
0024 //* frame shadow
0025 /** this allows the shadow to be painted over the widgets viewport */
0026 class MdiWindowShadow : public QWidget
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     //* constructor
0032     explicit MdiWindowShadow(QWidget *, const TileSet &);
0033 
0034     //* update geometry
0035     void updateGeometry();
0036 
0037     //* update ZOrder
0038     void updateZOrder();
0039 
0040     //* set associated window
0041     void setWidget(QWidget *value)
0042     {
0043         _widget = value;
0044     }
0045 
0046     //* associated window
0047     QWidget *widget() const
0048     {
0049         return _widget;
0050     }
0051 
0052 protected:
0053     //* painting
0054     void paintEvent(QPaintEvent *) override;
0055 
0056 private:
0057     //* associated widget
0058     QWidget *_widget = nullptr;
0059 
0060     //* tileset rect, used for painting
0061     QRect _shadowTilesRect;
0062 
0063     //* tileset used to draw shadow
0064     TileSet _shadowTiles;
0065 };
0066 
0067 //* shadow manager
0068 class MdiWindowShadowFactory : public QObject
0069 {
0070     Q_OBJECT
0071 
0072 public:
0073     //* constructor
0074     explicit MdiWindowShadowFactory(QObject *);
0075 
0076     //* set shadow helper
0077     void setShadowHelper(ShadowHelper *shadowHelper)
0078     {
0079         _shadowHelper = shadowHelper;
0080     }
0081 
0082     //* register widget
0083     bool registerWidget(QWidget *);
0084 
0085     //* unregister
0086     void unregisterWidget(QWidget *);
0087 
0088     //* true if widget is registered
0089     bool isRegistered(const QObject *widget) const
0090     {
0091         return _registeredWidgets.contains(widget);
0092     }
0093 
0094     //* event filter
0095     bool eventFilter(QObject *, QEvent *) override;
0096 
0097 protected:
0098     //* find shadow matching a given object
0099     MdiWindowShadow *findShadow(QObject *) const;
0100 
0101     //* install shadows on given widget
0102     void installShadow(QObject *);
0103 
0104     //* remove shadows from widget
0105     void removeShadow(QObject *);
0106 
0107     //* hide shadows
0108     void hideShadows(QObject *object) const
0109     {
0110         if (MdiWindowShadow *windowShadow = findShadow(object)) {
0111             windowShadow->hide();
0112         }
0113     }
0114 
0115     //* update ZOrder
0116     void updateShadowZOrder(QObject *object) const
0117     {
0118         if (MdiWindowShadow *windowShadow = findShadow(object)) {
0119             if (!windowShadow->isVisible()) {
0120                 windowShadow->show();
0121             }
0122             windowShadow->updateZOrder();
0123         }
0124     }
0125 
0126     //* update shadows geometry
0127     void updateShadowGeometry(QObject *object) const
0128     {
0129         if (MdiWindowShadow *windowShadow = findShadow(object)) {
0130             windowShadow->updateGeometry();
0131         }
0132     }
0133 
0134     //* update shadows
0135     void update(QObject *object) const
0136     {
0137         if (MdiWindowShadow *windowShadow = findShadow(object)) {
0138             windowShadow->update();
0139         }
0140     }
0141 
0142 protected Q_SLOTS:
0143 
0144     //* triggered by object destruction
0145     void widgetDestroyed(QObject *);
0146 
0147 private:
0148     //* set of registered widgets
0149     QSet<const QObject *> _registeredWidgets;
0150 
0151     //* shadow helper used to generate the shadows
0152     QPointer<ShadowHelper> _shadowHelper;
0153 };
0154 
0155 }
0156 
0157 #endif