Commit 41cd2663 authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud

Add a factory to handle plugin widget for composer.

Remove dtkCorePluginTuner stuff.
Add also a widget manager.
parent f7f43b7e
......@@ -30,19 +30,15 @@ class dtkComposerExtension;
class DTKCOMPOSER_EXPORT dtkComposerNodeFactory : public dtkCorePluginFactory<dtkComposerNode>
{
Q_OBJECT
public:
dtkComposerNodeFactory(void);
virtual ~dtkComposerNodeFactory(void);
dtkComposerNodeFactory(void);
~dtkComposerNodeFactory(void);
public slots:
public:
dtkComposerNode *create(const QString& node) const;
public:
void record(const QString& metadata_file_path, creator func);
public:
void extend(dtkComposerExtension *extension);
public:
......
......@@ -50,6 +50,8 @@ public:
bool implementationHasChanged(const QString& implementation) const;
QWidget *editor(void);
protected:
QString m_implementation;
T *m_object;
......
......@@ -12,6 +12,7 @@
// Code:
#include <dtkCore/dtkCorePluginWidgetManager>
#include <dtkCore/dtkCorePluginFactory>
#include <dtkMeta/dtkMeta>
......@@ -19,7 +20,7 @@
// dtkComposerNodeObject implementation
// ///////////////////////////////////////////////////////////////////
template <typename T> inline dtkComposerNodeObject<T>::dtkComposerNodeObject(void) : dtkComposerNodeLeafObject(), m_implementation(QString()), m_object(0), m_factory(0)
template <typename T> inline dtkComposerNodeObject<T>::dtkComposerNodeObject(void) : dtkComposerNodeLeafObject(), m_implementation(QString()), m_object(nullptr), m_factory(nullptr)
{
}
......@@ -74,7 +75,7 @@ template <typename T> inline bool dtkComposerNodeObject<T>::createObject(const Q
m_implementation = implementation;
}
return (m_object != NULL);
return (m_object != nullptr);
}
template <typename T> inline QStringList dtkComposerNodeObject<T>::implementations(void) const
......@@ -96,5 +97,23 @@ template <typename T> inline bool dtkComposerNodeObject<T>::implementationHasCha
return (m_implementation != implementation);
}
template <typename T> inline QWidget *dtkComposerNodeObject<T>::editor(void)
{
if (!m_factory) {
return nullptr;
}
QVariant v = dtkMetaType::variantFromValue(this->object());
QWidget *w = dtkCorePluginWidgetManager::instance().widget(v);
if (!w) {
w = m_factory->createWidget(m_implementation);
if (w) {
dtkCorePluginWidgetManager::instance().add(v, w);
}
}
return w;
}
//
// dtkComposerNodeObject.tpp ends here
......@@ -50,9 +50,8 @@ set(${PROJECT_NAME}_HEADERS
dtkCorePluginManagerHandler.h
dtkCorePluginManagerRecorder
dtkCorePluginManagerRecorder.h
dtkCorePluginTuner
dtkCorePluginTuner.h
dtkCorePluginTuner.tpp
dtkCorePluginWidgetManager
dtkCorePluginWidgetManager.h
dtkCoreSettings
dtkCoreSettings.h
dtkHash
......@@ -76,6 +75,7 @@ set(${PROJECT_NAME}_SOURCES
dtkCorePluginFactoryRecorder.cpp
dtkCorePluginManagerHandler.cpp
dtkCorePluginManagerRecorder.cpp
dtkCorePluginWidgetManager.cpp
dtkObjectManager.cpp)
## #################################################################
......
......@@ -13,7 +13,7 @@
#include "dtkCorePluginManager.h"
#include "dtkCorePluginManagerHandler.h"
#include "dtkCorePluginManagerRecorder.h"
#include "dtkCorePluginTuner.h"
#include "dtkCorePluginWidgetManager.h"
#include "dtkCoreSettings.h"
#include "dtkHash.h"
#include "dtkIterator.h"
......
......@@ -9,10 +9,10 @@ class DTKCORE_EXPORT dtkCorePluginBase : public QObject
Q_OBJECT
public:
virtual ~dtkCorePluginBase(void) {}
virtual ~dtkCorePluginBase(void) = default;
public:
virtual void initialize(void) = 0;
virtual void uninitialize(void) = 0;
virtual void *create(void) {return nullptr;};
virtual void initialize(void) = 0;
virtual void uninitialize(void) = 0;
virtual void *create(void) { return nullptr; };
};
......@@ -18,13 +18,17 @@
#include <QtCore>
#include <functional>
class QWidget;
// ///////////////////////////////////////////////////////////////////
// dtkCorePluginFactoryBase
// ///////////////////////////////////////////////////////////////////
class dtkObjectManager;
class DTKCORE_EXPORT dtkCorePluginFactoryBase : public QObject
class DTKCORE_EXPORT dtkCorePluginFactoryBase
{
public:
virtual void connect(dtkObjectManager *manager) const = 0;
......@@ -46,16 +50,17 @@ public:
public:
typedef T *(*creator) ();
typedef dtkCorePluginTuner<T> *(*tunerCreator) ();
typedef std::function<QWidget *()> widget_creator;
public:
void record(const QString& key, creator func);
void recordPlugin(const QString& key, dtkCorePluginBase *plugin);
void recordTuner(const QString& key, tunerCreator func);
void record(const QString& key, widget_creator func);
public:
T *create(const QString& key) const;
dtkCorePluginTuner<T> *createTuner(const QString& key) const;
QWidget *createWidget(const QString& key) const;
public:
QStringList keys(void) const;
......@@ -67,7 +72,7 @@ public:
private:
QHash<QString, creator> creators;
QHash<QString, dtkCorePluginBase *> creators_plugins;
QHash<QString, tunerCreator> tuner_creators;
QHash<QString, widget_creator> widget_creators;
private:
class dtkCorePluginFactoryPrivate *m_d;
......
......@@ -58,14 +58,14 @@ template <typename T> inline void dtkCorePluginFactory<T>::recordPlugin(const QS
this->creators_plugins.insert(key, plugin);
}
template <typename T> inline void dtkCorePluginFactory<T>::recordTuner(const QString& key, tunerCreator func)
template <typename T> inline void dtkCorePluginFactory<T>::record(const QString& key, widget_creator func)
{
if (this->tuner_creators.contains(key)) {
qDebug() << Q_FUNC_INFO << "Factory already contains key" << key << ". Nothing is done";
if (this->widget_creators.contains(key)) {
dtkTrace() << Q_FUNC_INFO << "Factory already contains key" << key << ". Nothing is done";
return;
}
this->tuner_creators.insert(key, func);
this->widget_creators.insert(key, func);
}
// /////////////////////////////////////////////////////////////////
......@@ -77,9 +77,14 @@ template <typename T> inline T *dtkCorePluginFactory<T>::create(const QString& k
T *obj = nullptr;
if (this->creators.contains(key)) {
obj = this->creators.value(key)();
} else if (this->creators_plugins.contains(key)) {
void *o = this->creators_plugins.value(key)->create();
obj = static_cast<T*>(o);
if (!o) {
dtkWarn() << Q_FUNC_INFO << "Nullptr is returned by plugin creator.";
return nullptr;
}
obj = static_cast<T*>(o);
}
if (obj) {
......@@ -89,15 +94,18 @@ template <typename T> inline T *dtkCorePluginFactory<T>::create(const QString& k
return obj;
}
template <typename T> inline dtkCorePluginTuner<T> *dtkCorePluginFactory<T>::createTuner(const QString& key) const
{
if (!this->tuner_creators.contains(key))
return NULL;
// ///////////////////////////////////////////////////////////////////
// Widget creation
// ///////////////////////////////////////////////////////////////////
return this->tuner_creators.value(key)();
template <typename T> inline QWidget *dtkCorePluginFactory<T>::createWidget(const QString& key) const
{
if (this->widget_creators.contains(key)) {
return this->widget_creators[key]();
}
return nullptr;
}
// /////////////////////////////////////////////////////////////////
// Type creator inspection
// /////////////////////////////////////////////////////////////////
......
#include "dtkCorePluginTuner.h"
\ No newline at end of file
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#pragma once
#include <QVariant>
// /////////////////////////////////////////////////////////////////
// dtkCorePluginTuner interface
// /////////////////////////////////////////////////////////////////
template < typename T > class dtkCorePluginTuner
{
public:
dtkCorePluginTuner(void);
virtual ~dtkCorePluginTuner(void);
void setObject(T *object);
void setMap(const QVariantHash& map);
virtual void update(void) = 0;
T *object(void) const;
const QVariantHash& map(void) const;
private:
T *m_object;
QVariantHash m_map;
};
// /////////////////////////////////////////////////////////////////
#include "dtkCorePluginTuner.tpp"
//
// dtkCorePluginTuner.h ends here
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#pragma once
// /////////////////////////////////////////////////////////////////
// dtkCorePluginTuner implementation
// /////////////////////////////////////////////////////////////////
template <typename T> inline dtkCorePluginTuner<T>::dtkCorePluginTuner(void) : m_object(nullptr)
{
}
template <typename T> inline dtkCorePluginTuner<T>::~dtkCorePluginTuner(void)
{
}
template <typename T> inline void dtkCorePluginTuner<T>::setObject(T *object)
{
m_object = object;
}
template <typename T> inline void dtkCorePluginTuner<T>::setMap(const QVariantHash& map)
{
m_map = map;
}
template <typename T> inline T *dtkCorePluginTuner<T>::object(void) const
{
return m_object;
}
template <typename T> inline const QVariantHash& dtkCorePluginTuner<T>::map(void) const
{
return m_map;
}
//
// dtkCorePluginTuner.tpp ends here
#include "dtkCorePluginWidgetManager.h"
\ No newline at end of file
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#include "dtkCorePluginWidgetManager.h"
// ///////////////////////////////////////////////////////////////////
class dtkCorePluginWidgetManagerPrivate
{
public:
QMap<QVariant, QWidget *> widgets;
};
// ///////////////////////////////////////////////////////////////////
dtkCorePluginWidgetManager& dtkCorePluginWidgetManager::instance(void)
{
if(!s_instance) {
s_instance = new dtkCorePluginWidgetManager;
}
return *s_instance;
}
void dtkCorePluginWidgetManager::add(const QVariant& v, QWidget *w)
{
d->widgets.insert(v, w);
emit added(v, w);
}
int dtkCorePluginWidgetManager::remove(const QVariant& v)
{
return d->widgets.remove(v);
}
int dtkCorePluginWidgetManager::remove(QWidget *w)
{
QList<QVariant> keys;
for (auto it = d->widgets.begin(), end = d->widgets.end(); it != end; ++it) {
if (*it == w) {
keys << it.key();
}
}
for (auto&& k : keys) {
d->widgets.remove(k);
}
return keys.size();
}
void dtkCorePluginWidgetManager::clear(void)
{
d->widgets.clear();
}
QWidget *dtkCorePluginWidgetManager::widget(const QVariant& v) const
{
const QWidget *w = d->widgets.value(v, nullptr);
if (w) {
return const_cast<QWidget *>(w);
}
return nullptr;
}
QString dtkCorePluginWidgetManager::description(void) const
{
QString msg("dtkCorePluginWidgetManager contains : \n");
QString content("[ ");
int i = 0;
int e = d->widgets.count()-1;
for (auto it = d->widgets.cbegin(), end = d->widgets.cend(); it != end; ++it, ++i) {
content += it.key().typeName() + QStringLiteral(" : ") + reinterpret_cast<const QObject *>(it.value())->objectName();
if (i < e) {
content += "\n";
} else {
content += " ]";
}
}
return (msg + content);
}
dtkCorePluginWidgetManager::dtkCorePluginWidgetManager(void) : d(new dtkCorePluginWidgetManagerPrivate)
{
}
dtkCorePluginWidgetManager::~dtkCorePluginWidgetManager(void)
{
delete d;
}
QDebug operator<<(QDebug dbg, dtkCorePluginWidgetManager& m)
{
dbg.nospace() << m.description();
return dbg.space();
}
dtkCorePluginWidgetManager *dtkCorePluginWidgetManager::s_instance = nullptr;
//
// dtkCorePluginWidgetManager.cpp ends here
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#pragma once
#include <dtkCoreExport>
#include <QtCore>
class QWidget;
class DTKCORE_EXPORT dtkCorePluginWidgetManager : public QObject
{
Q_OBJECT
public:
static dtkCorePluginWidgetManager& instance(void);
public:
void add(const QVariant&, QWidget *);
int remove(const QVariant&);
int remove(QWidget *);
void clear(void);
QWidget *widget(const QVariant&) const;
QString description(void) const;
signals:
void added(const QVariant&, QWidget *) const;
protected:
static dtkCorePluginWidgetManager *s_instance;
private:
dtkCorePluginWidgetManager(void);
~dtkCorePluginWidgetManager(void);
private:
class dtkCorePluginWidgetManagerPrivate *d;
};
DTKCORE_EXPORT QDebug operator<<(QDebug, dtkCorePluginWidgetManager&);
//
// dtkCorePluginWidgetManager.h ends here
......@@ -14,8 +14,6 @@
#include "dtkObjectManager.h"
#include <algorithm>
class dtkObjectManagerPrivate
{
public:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment