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

0001 #ifndef oxygentoolbar_data_h
0002 #define oxygentoolbar_data_h
0003 
0004 //////////////////////////////////////////////////////////////////////////////
0005 // oxygentoolbardata.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 "oxygenanimationdata.h"
0015 #include <QBasicTimer>
0016 
0017 namespace Oxygen
0018 {
0019 //* toolbar data
0020 class ToolBarData : public AnimationData
0021 {
0022     Q_OBJECT
0023     Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
0024     Q_PROPERTY(qreal progress READ progress WRITE setProgress)
0025 
0026 public:
0027     //* constructor
0028     ToolBarData(QObject *parent, QWidget *target, int duration);
0029 
0030     //* event filter
0031     bool eventFilter(QObject *, QEvent *) override;
0032 
0033     //* return animation associated to action at given position, if any
0034     const Animation::Pointer &animation(void) const
0035     {
0036         return _animation;
0037     }
0038 
0039     //* return animation associated to action at given position, if any
0040     const Animation::Pointer &progressAnimation(void) const
0041     {
0042         return _progressAnimation;
0043     }
0044 
0045     //* duration
0046     void setDuration(int duration) override
0047     {
0048         animation().data()->setDuration(duration);
0049     }
0050 
0051     //* duration
0052     void setFollowMouseDuration(int duration)
0053     {
0054         progressAnimation().data()->setDuration(duration);
0055     }
0056 
0057     //* return 'hover' rect position when widget is animated
0058     const QRect &animatedRect(void) const
0059     {
0060         return _animatedRect;
0061     }
0062 
0063     //* current rect
0064     const QRect &currentRect(void) const
0065     {
0066         return _currentRect;
0067     }
0068 
0069     //* timer
0070     const QBasicTimer &timer(void) const
0071     {
0072         return _timer;
0073     }
0074 
0075     //* animation opacity
0076     qreal opacity(void) const
0077     {
0078         return _opacity;
0079     }
0080 
0081     //* animation opacity
0082     void setOpacity(qreal value)
0083     {
0084         value = digitize(value);
0085         if (_opacity == value)
0086             return;
0087         _opacity = value;
0088         setDirty();
0089     }
0090 
0091     //* animation progress
0092     qreal progress(void) const
0093     {
0094         return _progress;
0095     }
0096 
0097     //* animation progress
0098     void setProgress(qreal value)
0099     {
0100         value = digitize(value);
0101         if (_progress == value)
0102             return;
0103         _progress = value;
0104         updateAnimatedRect();
0105     }
0106 
0107 protected:
0108     //* timer event
0109     void timerEvent(QTimerEvent *) override;
0110 
0111 private Q_SLOTS:
0112 
0113     //* updated animated rect
0114     void updateAnimatedRect(void);
0115 
0116 private:
0117     //*@name current object handling
0118     //@{
0119 
0120     //* object pointer
0121     /** there is no need to guard it because the object contents is never accessed */
0122     using ObjectPointer = const QObject *;
0123 
0124     //* current object
0125     const ObjectPointer &currentObject(void) const
0126     {
0127         return _currentObject;
0128     }
0129 
0130     //* current object
0131     void setCurrentObject(const QObject *object)
0132     {
0133         _currentObject = ObjectPointer(object);
0134     }
0135 
0136     //* current object
0137     void clearCurrentObject(void)
0138     {
0139         _currentObject = nullptr;
0140     }
0141 
0142     //@}
0143 
0144     //*@name rect handling
0145     //@{
0146 
0147     //* current rect
0148     void setCurrentRect(const QRect &rect)
0149     {
0150         _currentRect = rect;
0151     }
0152 
0153     //* current rect
0154     void clearCurrentRect(void)
0155     {
0156         _currentRect = QRect();
0157     }
0158 
0159     //* previous rect
0160     const QRect &previousRect(void) const
0161     {
0162         return _previousRect;
0163     }
0164 
0165     //* previous rect
0166     void setPreviousRect(const QRect &rect)
0167     {
0168         _previousRect = rect;
0169     }
0170 
0171     //* previous rect
0172     void clearPreviousRect(void)
0173     {
0174         _previousRect = QRect();
0175     }
0176 
0177     //* animated rect
0178     void clearAnimatedRect(void)
0179     {
0180         _animatedRect = QRect();
0181     }
0182 
0183     //@}
0184 
0185     //* toolbar enterEvent
0186     void enterEvent(const QObject *);
0187 
0188     //* toolbar enterEvent
0189     void leaveEvent(const QObject *);
0190 
0191     //* toolbutton added
0192     void childAddedEvent(QObject *);
0193 
0194     //* toolbutton enter event
0195     void childEnterEvent(const QObject *);
0196 
0197 private:
0198     //* fade animation
0199     Animation::Pointer _animation;
0200 
0201     //* progress animation
0202     Animation::Pointer _progressAnimation;
0203 
0204     //* opacity
0205     qreal _opacity = 0;
0206 
0207     //* opacity
0208     qreal _progress = 0;
0209 
0210     //* timer
0211     /** this allows to add some delay before starting leaveEvent animation */
0212     QBasicTimer _timer;
0213 
0214     //* current object
0215     ObjectPointer _currentObject;
0216 
0217     //* current rect
0218     QRect _currentRect;
0219 
0220     //* previous rect
0221     QRect _previousRect;
0222 
0223     //* animated rect
0224     QRect _animatedRect;
0225 
0226     //* true if toolbar was entered at least once (this prevents some initialization glitches)
0227     bool _entered = false;
0228 };
0229 }
0230 
0231 #endif