File indexing completed on 2024-05-05 05:35:25

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