Debian Codegraph @

avogadro/qtplugins/pluginmanager.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTGUI_PLUGINMANAGER_H
7 #define AVOGADRO_QTGUI_PLUGINMANAGER_H
8
9 #include "avogadroqtpluginsexport.h"
10
11 #include <QtCore/QList>
12 #include <QtCore/QObject>
13 #include <QtCore/QStringList>
14
15 namespace Avogadro {
16 namespace QtPlugins {
17
18 class ScenePluginFactory;
19 class ExtensionPluginFactory;
20
21 /**
22 * @class PluginManager pluginmanager.h <avogadro/qtplugins/pluginmanager.h>
23 * @brief This class takes care of finding and loading Avogadro plugins.
24 * @author Marcus D. Hanwell
25 *
26 * This class will find and load Avogadro plugins. Once loaded you can use an
27 * instance of this class to query and construct plugin instances. By default
28 * plugins are loaded from
29 * QApplication::applicationDirPath()../<libdir>/avogadro/plugins but this can
30 * be changed, or more paths can be added.
31 *
32 * The load methods can be called multiple times, and will load any new plugins
33 * while ignoring plugins that have already been loaded.
34 */
35
36 class AVOGADROQTPLUGINS_EXPORT PluginManager : public QObject
37 {
38 Q_OBJECT
39
40 public:
41 /**
42 * Get the singleton instance of the plugin manager. This instance should not
43 * be deleted.
44 */
45 static PluginManager* instance();
46
47 /**
48 * Get a reference to the plugin directory path list. Modifying this before
49 * calling load will allow you to add, remove or append to the search paths.
50 */
51 QStringList& pluginDirList() { return m_pluginDirs; }
52
53 /** Load all plugins available in the specified plugin directories. */
54 void load();
55 void load(const QString& dir);
56
57 /**
58 * Let the user request plugins with a certain type, this must use the Qt
59 * mechanisms as qobject_cast is used in conjunction with interfaces.
60 *
61 * @code
62 * factory =
63 * pluginManager->pluginFactories<Avogadro::QtGui::ScenePluginFactory>();
64 * @endcode
65 */
66 template <typename T>
67 QList<T*> pluginFactories() const;
68
69 /**
70 * Let the user request a plugin by name, this must use the Qt
71 * mechanisms as qobject_cast is used in conjunction with interfaces.
72 *
73 * @code
74 factory = pluginManager->
75 pluginFactories<Avogadro::QtGui::ScenePluginFactory>("name");
76 * @endcode
77 *
78 * @param id The identifier of the plugin factory.
79 * @return The plugin factory if the plugin was found, nullptr otherwise.
80 */
81 template <typename T>
82 T* pluginFactory(const QString& id) const;
83
84 private:
85 // Hide the constructor, destructor, copy and assignment operator.
86 PluginManager(QObject* parent = nullptr);
87 ~PluginManager() override;
88 PluginManager(const PluginManager&); // Not implemented.
89 PluginManager& operator=(const PluginManager&); // Not implemented.
90
91 QStringList m_pluginDirs;
92 QString m_relativeToApp;
93
94 bool m_staticPluginsLoaded;
95
96 // Storage for the loaded plugin instances.
97 QList<QObject*> m_plugins;
98 };
99
100 template <typename T>
101 QList<T*> PluginManager::pluginFactories() const
102 {
103 QList<T*> factories;
104 foreach (QObject* plugin, m_plugins) {
105 T* factory = qobject_cast<T*>(plugin);
106 if (factory)
107 factories.append(factory);
108 }
109 return factories;
110 }
111
112 template <typename T>
113 T* PluginManager::pluginFactory(const QString& id) const
114 {
115 T* factory;
116 foreach (QObject* plugin, m_plugins) {
117 factory = qobject_cast<T*>(plugin);
118 if (factory && factory->identifier() == id)
119 break;
120 }
121 return factory;
122 }
123
124 } // namespace QtPlugins
125 } // namespace Avogadro
126
127 #endif // AVOGADRO_QTGUI_PLUGINMANAGER_H