pluginmanagerdialog.hh
Go to the documentation of this file.
1 // Copyright (c) 2015-2018, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of gepetto-viewer.
5 // gepetto-viewer is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // gepetto-viewer is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // gepetto-viewer not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef GEPETTO_GUI_PLUGINMANAGERDIALOG_HH
18 #define GEPETTO_GUI_PLUGINMANAGERDIALOG_HH
19 
20 #include <QDialog>
21 #include <QMap>
22 #include <QSignalMapper>
23 #include <QDir>
24 #include <QPluginLoader>
25 #include <QTableWidgetItem>
26 
28 
29 namespace Ui {
30  class PluginManagerDialog;
31 }
32 
33 namespace gepetto {
34  namespace gui {
35  class PluginManager {
36  public:
37  typedef QPair <QString, QPluginLoader*> Pair;
38  typedef QMap <QString, QPluginLoader*> Map;
39  typedef QPair <QString, QString> PyPair;
40  typedef QMap <QString, QString> PyMap;
41 
43  qDeleteAll (plugins_);
44  }
45 
46  const Map& plugins () const { return plugins_; }
47 
48  const PyMap& pyplugins () const { return pyplugins_; }
49 
50  template <typename Interface> Interface* getFirstOf ();
51 
52  template <typename Interface> QList <Interface*> get ();
53 
54  static QIcon icon (const QPluginLoader* pl);
55 
56  static QString status (const QPluginLoader* pl);
57 
58  static void addPluginDir (const QString& path);
59 
60  void declareAllPlugins (QWidget* parent = NULL);
61 
62  bool declarePlugin (const QString& name, QWidget* parent = NULL);
63 
64  bool loadPlugin (const QString& name);
65 
66  bool initPlugin (const QString& name);
67 
68  bool unloadPlugin (const QString& name);
69 
70  void clearPlugins ();
71 
72  void declareAllPyPlugins ();
73 
74  bool declarePyPlugin (const QString& name);
75 
76  bool loadPyPlugin (const QString& name);
77 
78  bool unloadPyPlugin (const QString& name);
79 
80  bool isPyPluginLoaded (const QString& name);
81 
82  void clearPyPlugins ();
83 
84  private:
85  template <typename Interface>
86  static const Interface* const_instance_cast (const QPluginLoader* pl);
87 
88  QMap <QString, QPluginLoader*> plugins_;
89  static QList <QDir> pluginDirs_;
90  QMap <QString, QString> pyplugins_;
91  };
92 
93  class PluginManagerDialog : public QDialog
94  {
95  Q_OBJECT
96 
97  public:
98  explicit PluginManagerDialog(PluginManager* pm, QWidget *parent = 0);
100 
101  public slots:
102  void onItemChanged (QTableWidgetItem* current, QTableWidgetItem* previous);
103  void contextMenu(const QPoint& pos);
104  void load (const QString& name);
105  void unload (const QString& name);
106 
107  //void pyOnItemChanged (QTableWidgetItem* current, QTableWidgetItem* previous);
108  void pyContextMenu(const QPoint& pos);
109  void pyLoad (const QString& name);
110  void pyUnload (const QString& name);
111 
112  void declareAll ();
113  void save ();
114 
115  private:
116  static const int P_NAME;
117  static const int P_FILE;
118  static const int P_FULLPATH;
119  static const int P_VERSION;
120 
121  void updateList ();
122 
123  ::Ui::PluginManagerDialog *ui_;
124 
125  PluginManager* pm_;
126  };
127 
128  template <typename Interface>
129  Interface* PluginManager::getFirstOf ()
130  {
131  foreach (QPluginLoader* p, plugins_) {
132  if (p->isLoaded()) {
133  PluginInterface* check = qobject_cast <PluginInterface*> (p->instance());
134  if (check && check->isInit ()) {
135  Interface* pi = qobject_cast <Interface*> (p->instance());
136  if (pi) return pi;
137  }
138  }
139  }
140  return NULL;
141  }
142 
143  template <typename Interface>
144  QList <Interface*> PluginManager::get ()
145  {
146  QList <Interface*> list;
147  foreach (QPluginLoader* p, plugins_) {
148  if (p->isLoaded()) {
149  PluginInterface* check = qobject_cast <PluginInterface*> (p->instance());
150  if (check && check->isInit ()) {
151  Interface* pi = qobject_cast <Interface*> (p->instance());
152  if (pi)
153  list.append(pi);
154  }
155  }
156  }
157  return list;
158  }
159 
160  template <typename Interface>
161  const Interface* PluginManager::const_instance_cast (const QPluginLoader* pl)
162  {
163  return (const Interface*) qobject_cast <Interface*> (const_cast <QPluginLoader*>(pl)->instance());
164  }
165  } // namespace gui
166 } // namespace gepetto
167 
168 #endif // GEPETTO_GUI_PLUGINMANAGERDIALOG_HH
Definition: pluginmanagerdialog.hh:93
QMap< QString, QPluginLoader * > Map
Definition: pluginmanagerdialog.hh:38
Definition: dialogloadenvironment.hh:23
bool isInit() const
Definition: plugin-interface.hh:54
Definition: plugin-interface.hh:33
Definition: action-search-bar.hh:27
QPair< QString, QString > PyPair
Definition: pluginmanagerdialog.hh:39
Definition: pluginmanagerdialog.hh:35
const Map & plugins() const
Definition: pluginmanagerdialog.hh:46
const PyMap & pyplugins() const
Definition: pluginmanagerdialog.hh:48
QPair< QString, QPluginLoader * > Pair
Definition: pluginmanagerdialog.hh:37
~PluginManager()
Definition: pluginmanagerdialog.hh:42
QMap< QString, QString > PyMap
Definition: pluginmanagerdialog.hh:40