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 "breeze.h" 0010 0011 #include <QMap> 0012 #include <QObject> 0013 #include <QPaintDevice> 0014 0015 namespace Breeze 0016 { 0017 //* data map 0018 /** it maps an opaque pointer an associated QPointer<object> */ 0019 template<typename T> 0020 class DataMap : public QMap<const void *, WeakPointer<T>> 0021 { 0022 public: 0023 using Key = const void *; 0024 using Value = WeakPointer<T>; 0025 0026 //* insertion 0027 typename QMap<Key, Value>::iterator insert(const Key &key, const Value &value, bool enabled = true) 0028 { 0029 if (value) { 0030 value.data()->setEnabled(enabled); 0031 } 0032 return QMap<Key, Value>::insert(key, value); 0033 } 0034 0035 //* find value 0036 Value find(Key key) 0037 { 0038 if (!(enabled() && key)) { 0039 return Value(); 0040 } 0041 if (key == _lastKey) { 0042 return _lastValue; 0043 } else { 0044 Value out; 0045 typename QMap<Key, Value>::iterator iter(QMap<Key, Value>::find(key)); 0046 if (iter != QMap<Key, Value>::end()) { 0047 out = iter.value(); 0048 } 0049 _lastKey = key; 0050 _lastValue = out; 0051 return out; 0052 } 0053 } 0054 0055 //* unregister widget 0056 bool unregisterWidget(Key key) 0057 { 0058 // check key 0059 if (!key) { 0060 return false; 0061 } 0062 0063 // clear last value if needed 0064 if (key == _lastKey) { 0065 if (_lastValue) { 0066 _lastValue.clear(); 0067 } 0068 _lastKey = nullptr; 0069 } 0070 0071 // find key in map 0072 typename QMap<Key, Value>::iterator iter(QMap<Key, Value>::find(key)); 0073 if (iter == QMap<Key, Value>::end()) { 0074 return false; 0075 } 0076 0077 // delete value from map if found 0078 if (iter.value()) { 0079 iter.value().data()->deleteLater(); 0080 } 0081 QMap<Key, Value>::erase(iter); 0082 0083 return true; 0084 } 0085 0086 //* maxFrame 0087 void setEnabled(bool enabled) 0088 { 0089 _enabled = enabled; 0090 for (const Value &value : std::as_const(*this)) { 0091 if (value) { 0092 value.data()->setEnabled(enabled); 0093 } 0094 } 0095 } 0096 0097 //* enability 0098 bool enabled() const 0099 { 0100 return _enabled; 0101 } 0102 0103 //* duration 0104 void setDuration(int duration) const 0105 { 0106 for (const Value &value : std::as_const(*this)) { 0107 if (value) { 0108 value.data()->setDuration(duration); 0109 } 0110 } 0111 } 0112 0113 private: 0114 //* enability 0115 bool _enabled = true; 0116 0117 //* last key 0118 Key _lastKey = nullptr; 0119 0120 //* last value 0121 Value _lastValue; 0122 }; 0123 }