File indexing completed on 2024-05-12 05:28:37

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 #pragma once
0008 
0009 #include "config-breeze.h"
0010 
0011 #include "breezeanimation.h"
0012 
0013 #if BREEZE_HAVE_QTQUICK
0014 #include <QQuickItem>
0015 #endif
0016 
0017 #include <QEvent>
0018 #include <QObject>
0019 #include <QWidget>
0020 
0021 #include <cmath>
0022 
0023 namespace Breeze
0024 {
0025 //* base class
0026 class AnimationData : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     //* constructor
0032     AnimationData(QObject *parent, QObject *target)
0033         : QObject(parent)
0034         , _target(target)
0035     {
0036     }
0037 
0038     //* duration
0039     virtual void setDuration(int) = 0;
0040 
0041     //* steps
0042     static void setSteps(int value)
0043     {
0044         _steps = value;
0045     }
0046 
0047     //* enability
0048     virtual bool enabled() const
0049     {
0050         return _enabled;
0051     }
0052 
0053     //* enability
0054     virtual void setEnabled(bool value)
0055     {
0056         _enabled = value;
0057     }
0058 
0059     //* target
0060     const WeakPointer<QObject> &target() const
0061     {
0062         return _target;
0063     }
0064 
0065     //* invalid opacity
0066     static const qreal OpacityInvalid;
0067 
0068 protected:
0069     //* setup animation
0070     virtual void setupAnimation(const Animation::Pointer &animation, const QByteArray &property);
0071 
0072     //* apply step
0073     virtual qreal digitize(const qreal &value) const
0074     {
0075         if (_steps > 0) {
0076             return std::floor(value * _steps) / _steps;
0077         } else {
0078             return value;
0079         }
0080     }
0081 
0082     //* trigger target update
0083     virtual void setDirty() const
0084     {
0085         if (auto widget = qobject_cast<QWidget *>(_target)) {
0086             widget->update();
0087         }
0088 #if BREEZE_HAVE_QTQUICK
0089         else if (auto item = qobject_cast<QQuickItem *>(_target)) {
0090             // Note: Calling polish() instead of update() because that's where
0091             // Breeze would repaint its image for texture.
0092             item->polish();
0093         }
0094 #endif
0095     }
0096 
0097 private:
0098     //* guarded target
0099     WeakPointer<QObject> _target;
0100 
0101     //* enability
0102     bool _enabled = true;
0103 
0104     //* steps
0105     static int _steps;
0106 };
0107 
0108 }