- Added PluginInterface and SearchPlugin
[qtrapids] / src / plugins / PluginInterface.h
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifndef PLUGININTERFACE_H
21 #define PLUGININTERFACE_H
22
23 #include <QObject>
24 #include <QPluginLoader>
25
26 namespace qtrapids
27 {
28         
29         /** @class PluginHostInterface
30                 * @brief Defines interface for plugins to access the host application. 
31                 * A Host is an application that is extended by implementing Plugins, 
32                 * that implement the additional functionality
33         */
34         class PluginHostInterface : public QObject {
35                 public:
36                          
37                         /// @brief Sets the plugin GUI element to host application
38                         /// @note It is up to the host application to decide how to manage
39                         /// and show the actual widget.
40                         virtual bool setGui(QWidget* widget) = 0;
41                         
42                         /// @brief Adds additional plugin wigdets to the host application.
43                         /// This functio can be called by the plugin recursively, i.e. when GUI events occur
44                         /// The host application must handle placing the additional widgets.
45                         /// @todo Could we implement this using in a more manageable way, e.g. signal-slot?
46                         virtual void addPluginWidget(QWidget* widget) = 0;
47                         virtual void addToolbar() = 0;
48                         virtual void addToolItem() = 0;
49                         virtual void addMenu() = 0;
50                         virtual void addMenuItem() = 0;
51         };
52
53
54         /** @class PluginInterface 
55                 * @brief Defines interface for a plugin instance.
56                 * The host application uses PluginInterface interface for calling the plugins
57                 * that extend the Host functionality
58         */
59         class PluginInterface : public QObject {
60                 public:
61                         /// @brief Initializes the plugin instance.
62                         virtual void initialize(PluginHostInterface* host) = 0;
63                         virtual QWidget* getGui() = 0;
64         };
65
66 } //namespace qtrapids
67
68
69 // Declare the interfaces for the Qt framework.
70 Q_DECLARE_INTERFACE(qtrapids::PluginInterface,
71                     "com.ixonos.qtrapids.PluginInterface/1.0")
72 Q_DECLARE_INTERFACE(qtrapids::PluginHostInterface,
73                     "com.ixonos.qtrapids.PluginHostInterface/1.0")
74                                                                                 
75                                                                                 
76 //////////////// EXAMPLE PLUGIN DECLARATION /////////////////////////
77 // A simple plugin example using the PluginInterface
78 // For more info, see Qt documentation: "How to Create Qt Plugins"
79 //
80 // namespace qtrapids
81 // {
82 // 
83 //      class MyPlugin : public PluginInterface {
84 //              Q_OBJECT
85 //              // NOTE: This macro tells Qt which interfaces the plugin implements (i.e. inherits):
86 //              Q_INTERFACES(qtrapids::PluginInterface)
87 //              
88 //              public:
89 //                      MyPlugin();
90 //                      virtual void initialize(PluginHostInterface* host);
91 //                      virtual QWidget* getGui();
92 //              
93 // // Additional plugin-specific signals and slots.
94 //              signals: 
95 //                      void searchResult(QWidget* resultwidget);
96 //                      
97 //              private slots: 
98 //                      void on_button_clicked();
99 //                      void on_result(QWidget* resultWidget);
100 //                      
101 //              private:
102 //      };
103 // 
104 //
105 //      MyPlugin::MyPlugin(): host_(NULL) {}
106 // 
107 //              void SearchPlugin::initialize(AbstractPluginHost* host)
108 //              {
109 //                      host_ = host;
110 //                      
111 //                      if (host_ != NULL) {
112 //                              QWidget *pluginWidget = new QWidget;
113 //                              QVBoxLayout *vbox = new QVBoxLayout;
114 //                              QPushButton *searchButton = new QPushButton("Search");
115 //                              vbox->addWidget(searchButton);
116 //                              pluginWidget->setLayout(vbox);
117 //              
118 //                              connect(searchButton, SIGNAL(clicked()), this, SLOT(on_searchButton_clicked()));
119 //                              //connect(this, SIGNAL(searchResult(QWidget*)), this, SLOT(on_searchResult(QWidget*)));
120 //                              
121 //                              // Call host interface function to set the plugin GUI. Host handles the setup 
122 //                              // to it's own policy
123 //                              host_->setGui(pluginWidget);
124 //                      }
125 //              }
126 // } // namespace qtrapids 
127 //
128 //// NOTE: Remember to export the actual plugin to be visible for Qt:
129 //// Q_EXPORT_PLUGIN2(myplugin, qtrapids::MyPlugin)
130 //
131 ///////////////// END OF EXAMPLE PLUGIN ///////////////////////////////
132
133 #endif