/* * SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa * * SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef breezedatamap_h #define breezedatamap_h #include "breeze.h" #include #include #include namespace Breeze { //* data map /** it maps templatized data object to associated object */ template class BaseDataMap : public QMap> { public: using Key = const K *; using Value = WeakPointer; //* constructor BaseDataMap() : QMap() , _enabled(true) , _lastKey(NULL) { } //* destructor virtual ~BaseDataMap() { } //* insertion virtual typename QMap::iterator insert(const Key &key, const Value &value, bool enabled = true) { if (value) { value.data()->setEnabled(enabled); } return QMap::insert(key, value); } //* find value Value find(Key key) { if (!(enabled() && key)) { return Value(); } if (key == _lastKey) { return _lastValue; } else { Value out; typename QMap::iterator iter(QMap::find(key)); if (iter != QMap::end()) { out = iter.value(); } _lastKey = key; _lastValue = out; return out; } } //* unregister widget bool unregisterWidget(Key key) { // check key if (!key) { return false; } // clear last value if needed if (key == _lastKey) { if (_lastValue) { _lastValue.clear(); } _lastKey = NULL; } // find key in map typename QMap::iterator iter(QMap::find(key)); if (iter == QMap::end()) { return false; } // delete value from map if found if (iter.value()) { iter.value().data()->deleteLater(); } QMap::erase(iter); return true; } //* maxFrame void setEnabled(bool enabled) { _enabled = enabled; foreach (const Value &value, *this) { if (value) { value.data()->setEnabled(enabled); } } } //* enability bool enabled() const { return _enabled; } //* duration void setDuration(int duration) const { foreach (const Value &value, *this) { if (value) { value.data()->setDuration(duration); } } } private: //* enability bool _enabled; //* last key Key _lastKey; //* last value Value _lastValue; }; //* standard data map, using QObject as a key template class DataMap : public BaseDataMap { public: //* constructor DataMap() { } //* destructor virtual ~DataMap() { } }; //* QPaintDevice based dataMap template class PaintDeviceDataMap : public BaseDataMap { public: //* constructor PaintDeviceDataMap() { } //* destructor virtual ~PaintDeviceDataMap() { } }; } #endif