2018-12-29 18:59:54 +01:00
|
|
|
|
#ifndef IPLUGINCONTENTWIDGETCONTEXT_H
|
|
|
|
|
|
#define IPLUGINCONTENTWIDGETCONTEXT_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <QString>
|
2019-01-05 09:49:12 +01:00
|
|
|
|
#include <map>
|
2018-12-29 18:59:54 +01:00
|
|
|
|
#include <memory>
|
2019-01-05 09:49:12 +01:00
|
|
|
|
#include <typeindex>
|
2018-12-31 15:20:55 +01:00
|
|
|
|
#include "plugin_support/ModuleActionParameters.h"
|
2018-12-29 18:59:54 +01:00
|
|
|
|
|
|
|
|
|
|
class OpenDatabase;
|
2019-01-05 19:58:23 +01:00
|
|
|
|
class PluginModule;
|
2018-12-29 18:59:54 +01:00
|
|
|
|
class PluginContentWidget;
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
|
2018-12-29 18:59:54 +01:00
|
|
|
|
/** This class serves to isolate the plugin from the actual construct in which it is
|
|
|
|
|
|
* used.
|
|
|
|
|
|
*
|
2018-12-31 15:20:55 +01:00
|
|
|
|
* It provides interface for operating on the context without needing to many details.
|
|
|
|
|
|
* Actual default implementation is in PluginContentWidgetContextBase.
|
2019-01-05 09:49:12 +01:00
|
|
|
|
*
|
|
|
|
|
|
* objectRegistry implementation is from Igor Tandetnik answer to the following question
|
|
|
|
|
|
* https://stackoverflow.com/questions/35413745/using-shared-ptr-with-a-generic-registry-or-shared-object-storage-or-not
|
2018-12-29 18:59:54 +01:00
|
|
|
|
*/
|
|
|
|
|
|
class IPluginContentWidgetContext {
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~IPluginContentWidgetContext() = default;
|
|
|
|
|
|
/** Tells the context what to use as a caption for this content widget.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Depending on the context the caption might not be visible or used as the caption
|
|
|
|
|
|
* of a window or tab.
|
|
|
|
|
|
*/
|
|
|
|
|
|
virtual void setCaption(PluginContentWidget *content, const QString &caption, const QString &hint = {}) = 0;
|
|
|
|
|
|
/** Tells the context what icon to use.
|
|
|
|
|
|
*
|
|
|
|
|
|
* In general the icon is used in a similar place as the caption.
|
|
|
|
|
|
* \param iconname Assumed to be the name of an iconresource. The system will look for different
|
|
|
|
|
|
* sizes under :/icons/<size>/iconname
|
|
|
|
|
|
*/
|
|
|
|
|
|
virtual void setIcon(PluginContentWidget *content, const QString &iconname) = 0;
|
|
|
|
|
|
|
|
|
|
|
|
virtual void showStatusMessage(const QString &msg) = 0;
|
2018-12-31 15:20:55 +01:00
|
|
|
|
|
|
|
|
|
|
virtual void moduleAction(
|
|
|
|
|
|
const QString &module_identifier,
|
|
|
|
|
|
QString module_action,
|
|
|
|
|
|
const ModuleActionParameters &action_params
|
|
|
|
|
|
) = 0;
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
virtual void addContentWidget(PluginModule *module, PluginContentWidget *widget) = 0;
|
|
|
|
|
|
virtual void removeContentWidget(PluginContentWidget *widget) = 0;
|
2019-01-05 09:49:12 +01:00
|
|
|
|
|
2019-01-05 11:12:47 +01:00
|
|
|
|
/** Return a widget you can use as a parent
|
|
|
|
|
|
*/
|
|
|
|
|
|
virtual QWidget* container() = 0;
|
|
|
|
|
|
|
2019-01-05 09:49:12 +01:00
|
|
|
|
template<typename T, class...Args>
|
|
|
|
|
|
bool addObjects(Args&&...args);
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
bool registerObject(std::shared_ptr<T> object);
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
std::shared_ptr<T> getObject() const;
|
2019-01-05 19:58:23 +01:00
|
|
|
|
|
2019-01-05 09:49:12 +01:00
|
|
|
|
private:
|
|
|
|
|
|
std::map<std::type_index, std::shared_ptr<void> > m_objectRegistry;
|
|
|
|
|
|
|
2018-12-29 18:59:54 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2019-01-05 09:49:12 +01:00
|
|
|
|
template<typename T, class...Args>
|
|
|
|
|
|
bool IPluginContentWidgetContext::addObjects(Args&&...args)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::type_index key(typeid(T));
|
|
|
|
|
|
if (!m_objectRegistry.count(key)) {
|
|
|
|
|
|
auto p = std::make_shared<T>(std::forward<Args>(args)...);
|
|
|
|
|
|
m_objectRegistry[key] = p;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
bool IPluginContentWidgetContext::registerObject(std::shared_ptr<T> object)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_objectRegistry.emplace(std::type_index(typeid(T)), object).second;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
std::shared_ptr<T> IPluginContentWidgetContext::getObject() const
|
|
|
|
|
|
{
|
|
|
|
|
|
auto it = m_objectRegistry.find(typeid(T));
|
|
|
|
|
|
if (it == m_objectRegistry.end()) {
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
return std::static_pointer_cast<T>(it->second);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-29 18:59:54 +01:00
|
|
|
|
#endif // IPLUGINCONTENTWIDGETCONTEXT_H
|