From 47e37635f50c09b4f9a9ee7699e3120bab3e4088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 10 Apr 2016 04:29:29 +0200 Subject: NOISSUE split GUI stuff from logic library --- libraries/logic/resources/Resource.cpp | 155 +++++++++++++++++++++++ libraries/logic/resources/Resource.h | 132 +++++++++++++++++++ libraries/logic/resources/ResourceHandler.cpp | 28 ++++ libraries/logic/resources/ResourceHandler.h | 36 ++++++ libraries/logic/resources/ResourceObserver.cpp | 55 ++++++++ libraries/logic/resources/ResourceObserver.h | 73 +++++++++++ libraries/logic/resources/ResourceProxyModel.cpp | 89 +++++++++++++ libraries/logic/resources/ResourceProxyModel.h | 39 ++++++ 8 files changed, 607 insertions(+) create mode 100644 libraries/logic/resources/Resource.cpp create mode 100644 libraries/logic/resources/Resource.h create mode 100644 libraries/logic/resources/ResourceHandler.cpp create mode 100644 libraries/logic/resources/ResourceHandler.h create mode 100644 libraries/logic/resources/ResourceObserver.cpp create mode 100644 libraries/logic/resources/ResourceObserver.h create mode 100644 libraries/logic/resources/ResourceProxyModel.cpp create mode 100644 libraries/logic/resources/ResourceProxyModel.h (limited to 'libraries/logic/resources') diff --git a/libraries/logic/resources/Resource.cpp b/libraries/logic/resources/Resource.cpp new file mode 100644 index 00000000..e95675d7 --- /dev/null +++ b/libraries/logic/resources/Resource.cpp @@ -0,0 +1,155 @@ +#include "Resource.h" + +#include + +#include "ResourceObserver.h" +#include "ResourceHandler.h" + +// definition of static members of Resource +QMap(const QString &)>> Resource::m_handlers; +QMap, std::function> Resource::m_transfomers; +QMap> Resource::m_resources; + +struct NullResourceResult {}; +Q_DECLARE_METATYPE(NullResourceResult) +class NullResourceHandler : public ResourceHandler +{ +public: + explicit NullResourceHandler() + { + setResult(QVariant::fromValue(NullResourceResult())); + } +}; + +Resource::Resource(const QString &resource) + : m_resource(resource) +{ + if (!resource.isEmpty()) + { + // a valid resource identifier has the format : + Q_ASSERT(resource.contains(':')); + // "parse" the resource identifier into id and data + const QString resourceId = resource.left(resource.indexOf(':')); + const QString resourceData = resource.mid(resource.indexOf(':') + 1); + + // create and set up the handler + Q_ASSERT(m_handlers.contains(resourceId)); + m_handler = m_handlers.value(resourceId)(resourceData); + } + else + { + m_handler = std::make_shared(); + } + + Q_ASSERT(m_handler); + m_handler->init(m_handler); + m_handler->setResource(this); +} +Resource::~Resource() +{ + qDeleteAll(m_observers); +} + +Resource::Ptr Resource::create(const QString &resource, Ptr placeholder) +{ + const QString storageId = storageIdentifier(resource, placeholder); + + // do we already have a resource? even if m_resources contains it it might not be valid any longer (weak_ptr) + Resource::Ptr ptr = m_resources.contains(storageId) + ? m_resources.value(storageId).lock() + : nullptr; + // did we have one? and is it still valid? + if (!ptr) + { + /* We don't want Resource to have a public constructor, but std::make_shared needs it, + * so we create a subclass of Resource here that exposes the constructor as public. + * The alternative would be making the allocator for std::make_shared a friend, but it + * differs between different STL implementations, so that would be a pain. + */ + struct ConstructableResource : public Resource + { + explicit ConstructableResource(const QString &resource) + : Resource(resource) {} + }; + ptr = std::make_shared(resource); + ptr->m_placeholder = placeholder; + m_resources.insert(storageId, ptr); + } + return ptr; +} + +Resource::Ptr Resource::applyTo(ResourceObserver *observer) +{ + m_observers.append(observer); + observer->setSource(shared_from_this()); // give the observer a shared_ptr for us so we don't get deleted + observer->resourceUpdated(); // ask the observer to poll us immediently, we might already have data + return shared_from_this(); // allow chaining +} +Resource::Ptr Resource::applyTo(QObject *target, const char *property) +{ + // the cast to ResourceObserver* is required to ensure the right overload gets choosen, + // since QObjectResourceObserver also inherits from QObject + return applyTo(static_cast(new QObjectResourceObserver(target, property))); +} + +QVariant Resource::getResourceInternal(const int typeId) const +{ + // no result (yet), but a placeholder? delegate to the placeholder. + if (m_handler->result().isNull() && m_placeholder) + { + return m_placeholder->getResourceInternal(typeId); + } + const QVariant variant = m_handler->result(); + const auto typePair = qMakePair(int(variant.type()), typeId); + + // do we have an explicit transformer? use it. + if (m_transfomers.contains(typePair)) + { + return m_transfomers.value(typePair)(variant); + } + else + { + // we do not have an explicit transformer, so we just pass the QVariant, which will automatically + // transform some types for us (different numbers to each other etc.) + return variant; + } +} + +void Resource::reportResult() +{ + for (ResourceObserver *observer : m_observers) + { + observer->resourceUpdated(); + } +} +void Resource::reportFailure(const QString &reason) +{ + for (ResourceObserver *observer : m_observers) + { + observer->setFailure(reason); + } +} +void Resource::reportProgress(const int progress) +{ + for (ResourceObserver *observer : m_observers) + { + observer->setProgress(progress); + } +} + +void Resource::notifyObserverDeleted(ResourceObserver *observer) +{ + m_observers.removeAll(observer); +} + +QString Resource::storageIdentifier(const QString &id, Resource::Ptr placeholder) +{ + if (placeholder) + { + return id + '#' + storageIdentifier(placeholder->m_resource, placeholder->m_placeholder); + } + else + { + return id; + } +} diff --git a/libraries/logic/resources/Resource.h b/libraries/logic/resources/Resource.h new file mode 100644 index 00000000..63e97b88 --- /dev/null +++ b/libraries/logic/resources/Resource.h @@ -0,0 +1,132 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "ResourceObserver.h" +#include "TypeMagic.h" + +#include "multimc_logic_export.h" + +class ResourceHandler; + +/** Frontend class for resources + * + * Usage: + * Resource::create("icon:noaccount")->applyTo(accountsAction); + * Resource::create("web:http://asdf.com/image.png")->applyTo(imageLbl)->placeholder(Resource::create("icon:loading")); + * + * Memory management: + * Resource caches ResourcePtrs using weak pointers, so while a resource is still existing + * when a new resource is created the resources will be the same (including the same handler). + * + * ResourceObservers keep a shared pointer to the resource, as does the Resource itself to it's + * placeholder (if present). This means a resource stays valid while it's still used ("applied to" etc.) + * by something. When nothing uses it anymore it gets deleted. + * + * @note Always pass resource around using Resource::Ptr! Copy and move constructors are disabled for a reason. + */ +class MULTIMC_LOGIC_EXPORT Resource : public std::enable_shared_from_this +{ + // only allow creation from Resource::create and disallow passing around non-pointers + explicit Resource(const QString &resource); + Resource(const Resource &) = delete; + Resource(Resource &&) = delete; +public: + using Ptr = std::shared_ptr; + + ~Resource(); + + /// The returned pointer needs to be stored until either Resource::applyTo or Resource::then is called, or it is passed as + /// a placeholder to Resource::create itself. + static Ptr create(const QString &resource, Ptr placeholder = nullptr); + + /// Use these functions to specify what should happen when e.g. the resource changes + Ptr applyTo(ResourceObserver *observer); + Ptr applyTo(QObject *target, const char *property = nullptr); + template + Ptr then(Func &&func) + { + // Arg will be the functions argument with references and cv-qualifiers (const, volatile) removed + using Arg = TypeMagic::CleanType::Argument>; + // Ret will be the functions return type + using Ret = typename TypeMagic::Function::ReturnType; + + // FunctionResourceObserver + return applyTo(new FunctionResourceObserver(std::forward(func))); + } + + /// Retrieve the currently active resource. If it's type is different from T a conversion will be attempted. + template + T getResource() const { return getResourceInternal(qMetaTypeId()).template value(); } + + /// @internal Used by ResourceObserver and ResourceProxyModel + QVariant getResourceInternal(const int typeId) const; + + /** Register a new ResourceHandler. T needs to inherit from ResourceHandler + * Usage: Resource::registerHandler("myid"); + */ + template + static void registerHandler(const QString &id) + { + m_handlers.insert(id, [](const QString &res) { return std::make_shared(res); }); + } + /** Register a new resource transformer + * Resource transformers are functions that are responsible for converting between different types, + * for example converting from a QByteArray to a QPixmap. They are registered "externally" because not + * all types might be available in this library, for example gui types like QPixmap. + * + * Usage: Resource::registerTransformer([](const InputType &type) { return OutputType(type); }); + * This assumes that OutputType has a constructor that takes InputType as an argument. More + * complicated transformers can of course also be registered. + * + * When a ResourceObserver requests a type that's different from the actual resource type, a matching + * transformer will be looked up from the list of transformers. + * @note Only one-stage transforms will be performed (you can't registerTransformers for QString => int + * and int => float and expect QString to automatically be transformed into a float. + */ + template + static void registerTransformer(Func &&func) + { + using Out = typename TypeMagic::Function::ReturnType; + using In = TypeMagic::CleanType::Argument>; + static_assert(!std::is_same::value, "It does not make sense to transform a value to itself"); + m_transfomers.insert(qMakePair(qMetaTypeId(), qMetaTypeId()), [func](const QVariant &in) + { + return QVariant::fromValue(func(in.value())); + }); + } + +private: // half private, implementation details + friend class ResourceHandler; + // the following three functions are called by ResourceHandlers + /** Notifies the observers. They will call Resource::getResourceInternal which will call ResourceHandler::result + * or delegate to it's placeholder. + */ + void reportResult(); + void reportFailure(const QString &reason); + void reportProgress(const int progress); + + friend class ResourceObserver; + /// Removes observer from the list of observers so that we don't attempt to notify something that doesn't exist + void notifyObserverDeleted(ResourceObserver *observer); + +private: // truly private + QList m_observers; + std::shared_ptr m_handler = nullptr; + Ptr m_placeholder = nullptr; + const QString m_resource; + + static QString storageIdentifier(const QString &id, Ptr placeholder = nullptr); + QString storageIdentifier() const; + + // a list of resource handler factories, registered using registerHandler + static QMap(const QString &)>> m_handlers; + // a list of resource transformers, registered using registerTransformer + static QMap, std::function> m_transfomers; + // a list of resources so that we can reuse them + static QMap> m_resources; +}; diff --git a/libraries/logic/resources/ResourceHandler.cpp b/libraries/logic/resources/ResourceHandler.cpp new file mode 100644 index 00000000..46a4422c --- /dev/null +++ b/libraries/logic/resources/ResourceHandler.cpp @@ -0,0 +1,28 @@ +#include "ResourceHandler.h" + +#include "Resource.h" + +void ResourceHandler::setResult(const QVariant &result) +{ + m_result = result; + if (m_resource) + { + m_resource->reportResult(); + } +} + +void ResourceHandler::setFailure(const QString &reason) +{ + if (m_resource) + { + m_resource->reportFailure(reason); + } +} + +void ResourceHandler::setProgress(const int progress) +{ + if (m_resource) + { + m_resource->reportProgress(progress); + } +} diff --git a/libraries/logic/resources/ResourceHandler.h b/libraries/logic/resources/ResourceHandler.h new file mode 100644 index 00000000..f09d8904 --- /dev/null +++ b/libraries/logic/resources/ResourceHandler.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include "multimc_logic_export.h" + +class Resource; + +/** Base class for things that can retrieve a resource. + * + * Subclass, provide a constructor that takes a single QString as argument, and + * call Resource::registerHandler(""), where is the + * prefix of the resource ("web", "icon", etc.) + */ +class MULTIMC_LOGIC_EXPORT ResourceHandler +{ +public: + virtual ~ResourceHandler() {} + + void setResource(Resource *resource) { m_resource = resource; } + /// reimplement this if you need to do something after you have been put in a shared pointer + // we do this instead of inheriting from std::enable_shared_from_this + virtual void init(std::shared_ptr&) {} + + QVariant result() const { return m_result; } + +protected: // use these methods to notify the resource of changes + void setResult(const QVariant &result); + void setFailure(const QString &reason); + void setProgress(const int progress); + +private: + QVariant m_result; + Resource *m_resource = nullptr; +}; diff --git a/libraries/logic/resources/ResourceObserver.cpp b/libraries/logic/resources/ResourceObserver.cpp new file mode 100644 index 00000000..4f168fd2 --- /dev/null +++ b/libraries/logic/resources/ResourceObserver.cpp @@ -0,0 +1,55 @@ +#include "ResourceObserver.h" + +#include + +#include "Resource.h" + +static const char *defaultPropertyForTarget(QObject *target) +{ + if (target->inherits("QLabel")) + { + return "pixmap"; + } + else if (target->inherits("QAction") || + target->inherits("QMenu") || + target->inherits("QAbstractButton")) + { + return "icon"; + } + // for unit tests + else if (target->inherits("DummyObserverObject")) + { + return "property"; + } + else + { + Q_ASSERT_X(false, "ResourceObserver.cpp: defaultPropertyForTarget", "Unrecognized QObject subclass"); + return nullptr; + } +} + +QObjectResourceObserver::QObjectResourceObserver(QObject *target, const char *property) + : QObject(target), m_target(target) +{ + const QMetaObject *mo = m_target->metaObject(); + m_property = mo->property(mo->indexOfProperty( + property ? + property + : defaultPropertyForTarget(target))); +} +void QObjectResourceObserver::resourceUpdated() +{ + m_property.write(m_target, getInternal(m_property.type())); +} + + +ResourceObserver::~ResourceObserver() +{ + m_resource->notifyObserverDeleted(this); +} + +QVariant ResourceObserver::getInternal(const int typeId) const +{ + Q_ASSERT(m_resource); + return m_resource->getResourceInternal(typeId); +} diff --git a/libraries/logic/resources/ResourceObserver.h b/libraries/logic/resources/ResourceObserver.h new file mode 100644 index 00000000..c42e41ba --- /dev/null +++ b/libraries/logic/resources/ResourceObserver.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include + +#include +#include +#include "multimc_logic_export.h" + +class QVariant; +class Resource; + +/// Base class for things that can use a resource +class MULTIMC_LOGIC_EXPORT ResourceObserver +{ +public: + virtual ~ResourceObserver(); + +protected: // these methods are called by the Resource when something changes + virtual void resourceUpdated() = 0; + virtual void setFailure(const QString &) {} + virtual void setProgress(const int) {} + +private: + friend class Resource; + void setSource(std::shared_ptr resource) { m_resource = resource; } + +protected: + template + T get() const { return getInternal(qMetaTypeId()).template value(); } + QVariant getInternal(const int typeId) const; + +private: + std::shared_ptr m_resource; +}; + +/** Observer for QObject properties + * + * Give it a target and the name of a property, and that property will be set when the resource changes. + * + * If no name is given an attempt to find a default property for some common classes is done. + */ +class MULTIMC_LOGIC_EXPORT QObjectResourceObserver : public QObject, public ResourceObserver +{ +public: + explicit QObjectResourceObserver(QObject *target, const char *property = nullptr); + + void resourceUpdated() override; + +private: + QObject *m_target; + QMetaProperty m_property; +}; + +/** Observer for functions, lambdas etc. + * Template arguments: + * * We need Ret and Arg in order to create the std::function + * * We need Func in order to std::forward the function + */ +template +class FunctionResourceObserver : public ResourceObserver +{ + std::function m_function; +public: + template + explicit FunctionResourceObserver(T &&func) + : m_function(std::forward(func)) {} + + void resourceUpdated() override + { + m_function(get()); + } +}; diff --git a/libraries/logic/resources/ResourceProxyModel.cpp b/libraries/logic/resources/ResourceProxyModel.cpp new file mode 100644 index 00000000..f026d9a9 --- /dev/null +++ b/libraries/logic/resources/ResourceProxyModel.cpp @@ -0,0 +1,89 @@ +#include "ResourceProxyModel.h" + +#include + +#include "Resource.h" +#include "ResourceObserver.h" + +class ModelResourceObserver : public ResourceObserver +{ +public: + explicit ModelResourceObserver(const QModelIndex &index, const int role) + : m_index(index), m_role(role) + { + qRegisterMetaType>("QVector"); + } + + void resourceUpdated() override + { + if (m_index.isValid()) + { + // the resource changed, pretend to be the model and notify the views of the update. they will re-poll the model which will return the new resource value + QMetaObject::invokeMethod(const_cast(m_index.model()), + "dataChanged", Qt::QueuedConnection, + Q_ARG(QModelIndex, m_index), Q_ARG(QModelIndex, m_index), Q_ARG(QVector, QVector() << m_role)); + } + } + +private: + QPersistentModelIndex m_index; + int m_role; +}; + +ResourceProxyModel::ResourceProxyModel(const int resultTypeId, QObject *parent) + : QIdentityProxyModel(parent), m_resultTypeId(resultTypeId) +{ +} + +QVariant ResourceProxyModel::data(const QModelIndex &proxyIndex, int role) const +{ + const QModelIndex mapped = mapToSource(proxyIndex); + // valid cell that's a Qt::DecorationRole and that contains a non-empty string + if (mapped.isValid() && role == Qt::DecorationRole && !mapToSource(proxyIndex).data(role).toString().isEmpty()) + { + // do we already have a resource for this index? + if (!m_resources.contains(mapped)) + { + Resource::Ptr placeholder; + const QVariant placeholderIdentifier = mapped.data(PlaceholderRole); + if (!placeholderIdentifier.isNull() && placeholderIdentifier.type() == QVariant::String) + { + placeholder = Resource::create(placeholderIdentifier.toString()); + } + + // create the Resource and apply the observer for models + Resource::Ptr res = Resource::create(mapToSource(proxyIndex).data(role).toString(), placeholder) + ->applyTo(new ModelResourceObserver(proxyIndex, role)); + + m_resources.insert(mapped, res); + } + + return m_resources.value(mapped)->getResourceInternal(m_resultTypeId); + } + // otherwise fall back to the source model + return mapped.data(role); +} + +void ResourceProxyModel::setSourceModel(QAbstractItemModel *model) +{ + if (sourceModel()) + { + disconnect(sourceModel(), 0, this, 0); + } + if (model) + { + connect(model, &QAbstractItemModel::dataChanged, this, [this](const QModelIndex &tl, const QModelIndex &br, const QVector &roles) + { + // invalidate resources so that they will be re-created + if (roles.contains(Qt::DecorationRole) || roles.contains(PlaceholderRole) || roles.isEmpty()) + { + const QItemSelectionRange range(tl, br); + for (const QModelIndex &index : range.indexes()) + { + m_resources.remove(index); + } + } + }); + } + QIdentityProxyModel::setSourceModel(model); +} diff --git a/libraries/logic/resources/ResourceProxyModel.h b/libraries/logic/resources/ResourceProxyModel.h new file mode 100644 index 00000000..98a3dbd1 --- /dev/null +++ b/libraries/logic/resources/ResourceProxyModel.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +#include "multimc_logic_export.h" + +/// Convenience proxy model that transforms resource identifiers (strings) for Qt::DecorationRole into other types. +class MULTIMC_LOGIC_EXPORT ResourceProxyModel : public QIdentityProxyModel +{ + Q_OBJECT +public: + // resultTypeId is found using qMetaTypeId() + explicit ResourceProxyModel(const int resultTypeId, QObject *parent = nullptr); + + enum + { + // provide this role from your model if you want to show a placeholder + PlaceholderRole = Qt::UserRole + 0xabc // some random offset to not collide with other stuff + }; + + QVariant data(const QModelIndex &proxyIndex, int role) const override; + void setSourceModel(QAbstractItemModel *model) override; + + /// Helper function, usage: m_view->setModel(ResourceProxyModel::mixin(m_model)); + template + static QAbstractItemModel *mixin(QAbstractItemModel *model) + { + ResourceProxyModel *proxy = new ResourceProxyModel(qMetaTypeId(), model); + proxy->setSourceModel(model); + return proxy; + } + +private: + // mutable because it needs to be available from the const data() + mutable QMap> m_resources; + + const int m_resultTypeId; +}; -- cgit v1.2.3