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

0001 //////////////////////////////////////////////////////////////////////////////
0002 // oxygenbusyindicatorengine.cpp
0003 // handle progress bar busy indicator
0004 // -------------------
0005 //
0006 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 //
0008 // SPDX-License-Identifier: MIT
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #include "oxygenbusyindicatorengine.h"
0012 
0013 #include <QVariant>
0014 
0015 namespace Oxygen
0016 {
0017 //_______________________________________________
0018 BusyIndicatorEngine::BusyIndicatorEngine(QObject *object)
0019     : BaseEngine(object)
0020 {
0021 }
0022 
0023 //_______________________________________________
0024 bool BusyIndicatorEngine::registerWidget(QObject *object)
0025 {
0026     // check widget validity
0027     if (!object)
0028         return false;
0029 
0030     // create new data class
0031     if (!_data.contains(object)) {
0032         _data.insert(object, new BusyIndicatorData(this));
0033 
0034         // connect destruction signal
0035         connect(object, SIGNAL(destroyed(QObject *)), this, SLOT(unregisterWidget(QObject *)), Qt::UniqueConnection);
0036     }
0037 
0038     return true;
0039 }
0040 
0041 //____________________________________________________________
0042 bool BusyIndicatorEngine::isAnimated(const QObject *object)
0043 {
0044     DataMap<BusyIndicatorData>::Value data(BusyIndicatorEngine::data(object));
0045     return data && data.data()->isAnimated();
0046 }
0047 
0048 //____________________________________________________________
0049 void BusyIndicatorEngine::setDuration(int value)
0050 {
0051     if (duration() == value)
0052         return;
0053     BaseEngine::setDuration(value);
0054 
0055     // restart timer with specified time
0056     if (_animation) {
0057         _animation.data()->setDuration(value * 100);
0058     }
0059 }
0060 
0061 //____________________________________________________________
0062 void BusyIndicatorEngine::setAnimated(const QObject *object, bool value)
0063 {
0064     DataMap<BusyIndicatorData>::Value data(BusyIndicatorEngine::data(object));
0065     if (data) {
0066         // update data
0067         data.data()->setAnimated(value);
0068 
0069         // start timer if needed
0070         if (value) {
0071             if (!_animation) {
0072                 // create animation if not already there
0073                 _animation = new Animation(duration(), this);
0074 
0075                 // setup
0076                 _animation.data()->setStartValue(0);
0077                 _animation.data()->setEndValue(1);
0078                 _animation.data()->setTargetObject(this);
0079                 _animation.data()->setPropertyName("value");
0080                 _animation.data()->setLoopCount(-1);
0081                 _animation.data()->setDuration(duration() * 100);
0082             }
0083 
0084             // start if  not already running
0085             if (!_animation.data()->isRunning()) {
0086                 _animation.data()->start();
0087             }
0088         }
0089     }
0090 
0091     return;
0092 }
0093 
0094 //____________________________________________________________
0095 DataMap<BusyIndicatorData>::Value BusyIndicatorEngine::data(const QObject *object)
0096 {
0097     return _data.find(object).data();
0098 }
0099 
0100 //_______________________________________________
0101 void BusyIndicatorEngine::setValue(qreal value)
0102 {
0103     // update
0104     _value = value;
0105 
0106     bool animated(false);
0107 
0108     // loop over objects in map
0109     for (DataMap<BusyIndicatorData>::iterator iter = _data.begin(); iter != _data.end(); ++iter) {
0110         if (iter.value().data()->isAnimated()) {
0111             // update animation flag
0112             animated = true;
0113 
0114             // emit update signal on object
0115             if (const_cast<QObject *>(iter.key())->inherits("QQuickStyleItem")) {
0116                 // QtQuickControls "rerender" method is updateItem
0117                 QMetaObject::invokeMethod(const_cast<QObject *>(iter.key()), "updateItem", Qt::QueuedConnection);
0118 
0119             } else {
0120                 QMetaObject::invokeMethod(const_cast<QObject *>(iter.key()), "update", Qt::QueuedConnection);
0121             }
0122         }
0123     }
0124 
0125     if (_animation && !animated) {
0126         _animation.data()->stop();
0127         _animation.data()->deleteLater();
0128         _animation.clear();
0129     }
0130 }
0131 
0132 //__________________________________________________________
0133 bool BusyIndicatorEngine::unregisterWidget(QObject *object)
0134 {
0135     const bool removed(_data.unregisterWidget(object));
0136     if (_animation && _data.isEmpty()) {
0137         _animation.data()->stop();
0138         _animation.data()->deleteLater();
0139         _animation.clear();
0140     }
0141 
0142     return removed;
0143 }
0144 }