2024-02-17 22:51:19 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
2024-08-09 01:20:25 +00:00
|
|
|
#pragma once
|
2024-02-17 22:51:19 +00:00
|
|
|
|
|
|
|
#include "breeze.h"
|
|
|
|
|
|
|
|
#include <QMap>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QPaintDevice>
|
|
|
|
|
|
|
|
namespace Breeze
|
|
|
|
{
|
|
|
|
//* data map
|
2024-08-09 01:20:25 +00:00
|
|
|
/** it maps an opaque pointer an associated QPointer<object> */
|
|
|
|
template<typename T>
|
|
|
|
class DataMap : public QMap<const void *, WeakPointer<T>>
|
2024-02-17 22:51:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2024-08-09 01:20:25 +00:00
|
|
|
using Key = const void *;
|
2024-02-17 22:51:19 +00:00
|
|
|
using Value = WeakPointer<T>;
|
|
|
|
|
|
|
|
//* insertion
|
2024-08-09 01:20:25 +00:00
|
|
|
typename QMap<Key, Value>::iterator insert(const Key &key, const Value &value, bool enabled = true)
|
2024-02-17 22:51:19 +00:00
|
|
|
{
|
|
|
|
if (value) {
|
|
|
|
value.data()->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
return QMap<Key, Value>::insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//* find value
|
|
|
|
Value find(Key key)
|
|
|
|
{
|
|
|
|
if (!(enabled() && key)) {
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
if (key == _lastKey) {
|
|
|
|
return _lastValue;
|
|
|
|
} else {
|
|
|
|
Value out;
|
|
|
|
typename QMap<Key, Value>::iterator iter(QMap<Key, Value>::find(key));
|
|
|
|
if (iter != QMap<Key, Value>::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();
|
|
|
|
}
|
2024-08-09 01:20:25 +00:00
|
|
|
_lastKey = nullptr;
|
2024-02-17 22:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find key in map
|
|
|
|
typename QMap<Key, Value>::iterator iter(QMap<Key, Value>::find(key));
|
|
|
|
if (iter == QMap<Key, Value>::end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete value from map if found
|
|
|
|
if (iter.value()) {
|
|
|
|
iter.value().data()->deleteLater();
|
|
|
|
}
|
|
|
|
QMap<Key, Value>::erase(iter);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//* maxFrame
|
|
|
|
void setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_enabled = enabled;
|
2024-08-09 01:20:25 +00:00
|
|
|
for (const Value &value : std::as_const(*this)) {
|
2024-02-17 22:51:19 +00:00
|
|
|
if (value) {
|
|
|
|
value.data()->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//* enability
|
|
|
|
bool enabled() const
|
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
//* duration
|
|
|
|
void setDuration(int duration) const
|
|
|
|
{
|
2024-08-09 01:20:25 +00:00
|
|
|
for (const Value &value : std::as_const(*this)) {
|
2024-02-17 22:51:19 +00:00
|
|
|
if (value) {
|
|
|
|
value.data()->setDuration(duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
//* enability
|
2024-08-09 01:20:25 +00:00
|
|
|
bool _enabled = true;
|
2024-02-17 22:51:19 +00:00
|
|
|
|
|
|
|
//* last key
|
2024-08-09 01:20:25 +00:00
|
|
|
Key _lastKey = nullptr;
|
2024-02-17 22:51:19 +00:00
|
|
|
|
|
|
|
//* last value
|
|
|
|
Value _lastValue;
|
|
|
|
};
|
|
|
|
}
|