File indexing completed on 2024-05-19 05:35:21

0001 #ifndef oxygenmenuengine_h
0002 #define oxygenmenuengine_h
0003 
0004 //////////////////////////////////////////////////////////////////////////////
0005 // oxygenmenuengine.h
0006 // stores event filters and maps widgets to timelines for animations
0007 // -------------------
0008 //
0009 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0010 //
0011 // SPDX-License-Identifier: MIT
0012 //////////////////////////////////////////////////////////////////////////////
0013 
0014 #include "oxygenbaseengine.h"
0015 #include "oxygendatamap.h"
0016 #include "oxygenmenudata.h"
0017 
0018 namespace Oxygen
0019 {
0020 //* stores menu hovered action and timeLine
0021 class MenuBaseEngine : public BaseEngine
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     //* constructor
0027     explicit MenuBaseEngine(QObject *parent)
0028         : BaseEngine(parent)
0029     {
0030     }
0031 
0032     //* register menubar
0033     virtual bool registerWidget(QWidget *) = 0;
0034 
0035     //* true if widget is animated
0036     virtual bool isAnimated(const QObject *, WidgetIndex)
0037     {
0038         return false;
0039     }
0040 
0041     //* opacity
0042     virtual qreal opacity(const QObject *, WidgetIndex)
0043     {
0044         return -1;
0045     }
0046 
0047     //* return 'hover' rect position when widget is animated
0048     virtual QRect currentRect(const QObject *, WidgetIndex)
0049     {
0050         return QRect();
0051     }
0052 
0053     //* return 'hover' rect position when widget is animated
0054     virtual QRect animatedRect(const QObject *)
0055     {
0056         return QRect();
0057     }
0058 
0059     //* timer associated to the data
0060     virtual bool isTimerActive(const QObject *)
0061     {
0062         return false;
0063     }
0064 
0065     //* enable state
0066     void setEnabled(bool value) override = 0;
0067 
0068     //* duration
0069     void setDuration(int) override = 0;
0070 
0071     //* duration
0072     virtual void setFollowMouseDuration(int)
0073     {
0074     }
0075 };
0076 
0077 //* stores menu hovered action and timeLine
0078 class MenuEngineV1 : public MenuBaseEngine
0079 {
0080     Q_OBJECT
0081 
0082 public:
0083     //* constructor
0084     explicit MenuEngineV1(QObject *parent)
0085         : MenuBaseEngine(parent)
0086     {
0087     }
0088 
0089     //* constructor
0090     MenuEngineV1(QObject *parent, MenuBaseEngine *other);
0091 
0092     //* register menubar
0093     bool registerWidget(QWidget *) override;
0094 
0095     //* true if widget is animated
0096     bool isAnimated(const QObject *object, WidgetIndex index) override;
0097 
0098     //* animation opacity
0099     qreal opacity(const QObject *object, WidgetIndex index) override
0100     {
0101         if (!isAnimated(object, index))
0102             return AnimationData::OpacityInvalid;
0103         else
0104             return _data.find(object).data()->opacity(index);
0105     }
0106 
0107     //* return 'hover' rect position when widget is animated
0108     QRect currentRect(const QObject *object, WidgetIndex index) override
0109     {
0110         if (!isAnimated(object, index))
0111             return QRect();
0112         else
0113             return _data.find(object).data()->currentRect(index);
0114     }
0115 
0116     //* enable state
0117     void setEnabled(bool value) override
0118     {
0119         BaseEngine::setEnabled(value);
0120         _data.setEnabled(value);
0121     }
0122 
0123     //* duration
0124     void setDuration(int duration) override
0125     {
0126         BaseEngine::setDuration(duration);
0127         _data.setDuration(duration);
0128     }
0129 
0130     //* return list of registered widgets
0131     WidgetList registeredWidgets(void) const override;
0132 
0133 public Q_SLOTS:
0134 
0135     //* remove widget from map
0136     bool unregisterWidget(QObject *object) override
0137     {
0138         return _data.unregisterWidget(object);
0139     }
0140 
0141 private:
0142     //* data map
0143     DataMap<MenuDataV1> _data;
0144 };
0145 
0146 //* stores menu hovered action and timeLine
0147 class MenuEngineV2 : public MenuBaseEngine
0148 {
0149     Q_OBJECT
0150 
0151 public:
0152     //* constructor
0153     explicit MenuEngineV2(QObject *parent)
0154         : MenuBaseEngine(parent)
0155     {
0156     }
0157 
0158     //* constructor
0159     MenuEngineV2(QObject *parent, MenuBaseEngine *other);
0160 
0161     //* register menu
0162     bool registerWidget(QWidget *) override;
0163 
0164     //* return timeLine associated to action at given position, if any
0165     bool isAnimated(const QObject *, WidgetIndex) override;
0166 
0167     //* animation opacity
0168     qreal opacity(const QObject *object, WidgetIndex index) override
0169     {
0170         if (!isAnimated(object, index))
0171             return AnimationData::OpacityInvalid;
0172         else
0173             return _data.find(object).data()->opacity();
0174     }
0175 
0176     //* return 'hover' rect position when widget is animated
0177     QRect currentRect(const QObject *object, WidgetIndex index) override;
0178 
0179     //* return 'hover' rect position when widget is animated
0180     QRect animatedRect(const QObject *) override;
0181 
0182     //* timer associated to the data
0183     bool isTimerActive(const QObject *) override;
0184 
0185     //* enable state
0186     void setEnabled(bool value) override
0187     {
0188         BaseEngine::setEnabled(value);
0189         _data.setEnabled(value);
0190     }
0191 
0192     //* duration
0193     void setDuration(int value) override
0194     {
0195         BaseEngine::setDuration(value);
0196         _data.setDuration(value);
0197     }
0198 
0199     //* duration
0200     virtual int followMouseDuration(void) const
0201     {
0202         return _followMouseDuration;
0203     }
0204 
0205     //* duration
0206     void setFollowMouseDuration(int duration) override
0207     {
0208         _followMouseDuration = duration;
0209         for (const DataMap<MenuDataV2>::Value &value : std::as_const(_data)) {
0210             if (value)
0211                 value.data()->setFollowMouseDuration(duration);
0212         }
0213     }
0214 
0215     //* return list of registered widgets
0216     WidgetList registeredWidgets(void) const override;
0217 
0218 protected Q_SLOTS:
0219 
0220     //* remove widget from map
0221     bool unregisterWidget(QObject *object) override
0222     {
0223         return _data.unregisterWidget(object);
0224     }
0225 
0226 private:
0227     //* follow mouse animation duration
0228     int _followMouseDuration;
0229 
0230     //* data map
0231     DataMap<MenuDataV2> _data;
0232 };
0233 }
0234 
0235 #endif