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

0001 #ifndef oxygendatamap_h
0002 #define oxygendatamap_h
0003 
0004 //////////////////////////////////////////////////////////////////////////////
0005 // oxygendatamap.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 "oxygen.h"
0015 
0016 #include <QMap>
0017 #include <QObject>
0018 
0019 #include <QPaintDevice>
0020 
0021 namespace Oxygen
0022 {
0023 //* data map
0024 /** it maps templatized data object to associated object */
0025 template<typename K, typename T>
0026 class BaseDataMap : public QMap<const K *, WeakPointer<T>>
0027 {
0028 public:
0029     using Key = const K *;
0030     using Value = WeakPointer<T>;
0031 
0032     //* constructor
0033     BaseDataMap(void)
0034         : QMap<Key, Value>()
0035         , _enabled(true)
0036         , _lastKey(nullptr)
0037     {
0038     }
0039 
0040     //* destructor
0041     virtual ~BaseDataMap(void) = default;
0042 
0043     //* insertion
0044     typename QMap<Key, Value>::iterator insert(const Key &key, const Value &value, bool enabled = true)
0045     {
0046         if (value)
0047             value.data()->setEnabled(enabled);
0048         return QMap<Key, Value>::insert(key, value);
0049     }
0050 
0051     //* find value
0052     Value find(Key key)
0053     {
0054         if (!(enabled() && key))
0055             return Value();
0056         if (key == _lastKey)
0057             return _lastValue;
0058         else {
0059             Value out;
0060             typename QMap<Key, Value>::iterator iter(QMap<Key, Value>::find(key));
0061             if (iter != QMap<Key, Value>::end())
0062                 out = iter.value();
0063             _lastKey = key;
0064             _lastValue = out;
0065             return out;
0066         }
0067     }
0068 
0069     //* unregister widget
0070     bool unregisterWidget(Key key)
0071     {
0072         // check key
0073         if (!key)
0074             return false;
0075 
0076         // clear last value if needed
0077         if (key == _lastKey) {
0078             if (_lastValue)
0079                 _lastValue.clear();
0080             _lastKey = nullptr;
0081         }
0082 
0083         // find key in map
0084         typename QMap<Key, Value>::iterator iter(QMap<Key, Value>::find(key));
0085         if (iter == QMap<Key, Value>::end())
0086             return false;
0087 
0088         // delete value from map if found
0089         if (iter.value())
0090             iter.value().data()->deleteLater();
0091         QMap<Key, Value>::erase(iter);
0092 
0093         return true;
0094     }
0095 
0096     //* maxFrame
0097     void setEnabled(bool enabled) const
0098     {
0099         _enabled = enabled;
0100         for (const Value &value : *this) {
0101             if (value)
0102                 value.data()->setEnabled(enabled);
0103         }
0104     }
0105 
0106     //* enability
0107     bool enabled(void) const
0108     {
0109         return _enabled;
0110     }
0111 
0112     //* duration
0113     void setDuration(int duration) const
0114     {
0115         for (const Value &value : *this) {
0116             if (value)
0117                 value.data()->setDuration(duration);
0118         }
0119     }
0120 
0121 private:
0122     //* enability
0123     mutable bool _enabled;
0124 
0125     //* last key
0126     Key _lastKey;
0127 
0128     //* last value
0129     Value _lastValue;
0130 };
0131 
0132 //* standard data map, using QObject as a key
0133 template<typename T>
0134 class DataMap : public BaseDataMap<QObject, T>
0135 {
0136 public:
0137     //* constructor
0138     DataMap(void)
0139     {
0140     }
0141 
0142     //* destructor
0143     virtual ~DataMap(void)
0144     {
0145     }
0146 };
0147 
0148 //* QPaintDevice based dataMap
0149 template<typename T>
0150 class PaintDeviceDataMap : public BaseDataMap<QPaintDevice, T>
0151 {
0152 public:
0153     //* constructor
0154     PaintDeviceDataMap(void)
0155     {
0156     }
0157 
0158     //* destructor
0159     virtual ~PaintDeviceDataMap(void)
0160     {
0161     }
0162 };
0163 }
0164 
0165 #endif