improved documentation and behavior of the example plugin

git-svn-id: http://svn.osgeo.org/qgis/trunk@522 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-01-19 20:22:02 +00:00
parent ab258797f4
commit f6d292064d
2 changed files with 181 additions and 98 deletions

View File

@ -1,119 +1,171 @@
/* Test plugin for QGis /* Example plugin for QGIS
* This code is an example plugin for QGIS and a demonstration of the API * This code is an example plugin for QGIS and a demonstration of the API
* All QGIS plugins must inherit from the abstract base class QgisPlugin. A * All QGIS plugins must inherit from the abstract base class QgisPlugin. A
* plugin must implement the virtual functions defined in QgisPlugin: * plugin must implement the virtual functions defined in QgisPlugin:
* *name * *name
* *version * *version
* *description * *description
* *type
* *
* This list may grow as the API is expanded.
*
* In addition, a plugin must implement a the classFactory and unload * In addition, a plugin must implement a the classFactory and unload
* functions. Note that these functions must be declared as extern "C" * functions. Note that these functions must be declared as extern "C"
*
* This plugin is not very useful. It installs a new menu with two items and
* illustrates how to connect the items to slots which handle menu events. It
* also installs a toolbar with one button. When clicked, the button zooms the
* map to the previous extent.
*/ */
// includes // includes
#include <iostream> #include <iostream>
#include "../../src/qgisapp.h" #include "../../src/qgisapp.h"
#include <qtoolbar.h>
#include "exampleplugin.h" #include <qmenubar.h>
#include <qmessagebox.h>
#include <qpopupmenu.h>
#include <qaction.h> #include <qaction.h>
#include "exampleplugin.h"
// xpm for creating the toolbar icon // xpm for creating the toolbar icon
#include "matrix1.xpm" #include "matrix1.xpm"
/** /**
* Constructor for the plugin * Constructor for the plugin. The plugin is passed a pointer to the main app
* and an interface object that provides access to exposed functions in QGIS.
* @param qgis Pointer to the QGIS main window
* @parma _qI Pointer to the QGIS interface object
*/ */
ExamplePlugin::ExamplePlugin(QgisApp *qgis, QgisIface *_qI) ExamplePlugin::ExamplePlugin(QgisApp * qgis, QgisIface * _qI):qgisMainWindow(qgis), qI(_qI)
: qgisMainWindow(qgis), qI(_qI){ {
pName = "Test Plugin"; /** Initialize the plugin and set the required attributes */
pVersion = "Version 0.0"; pName = "Example Plugin";
pDescription = "This test plugin does nothing but tell you its name, version, and description"; pVersion = "Version 0.0";
// instantiate a map layer pDescription = "This example plugin installs menu items and a toolbar";
//QgsMapLayer *mlyr = new QgsMapLayer();
// see if we can popup a message box in qgis on load
// see if we can popup a message box in qgis on load QMessageBox::information(qgisMainWindow, "Message From Plugin",
QMessageBox::information(qgisMainWindow,"Message From Plugin", "This message is from within the test plugin"); "This message is from within the example plugin.");
// call a function defined in the QgisIface class // Zoom the map canvas to the full extent of all layers
int foo = qI->getInt(); qI->zoomFull();
/*
QgisIface *qI2 = qgisMainWindow->getInterface(); QMessageBox::information(qgisMainWindow, "Message From Plugin", "Click Ok to zoom previous");
if(qI2) // zoom the map back to previous extent
std::cout << "qI2 pointer is good" << std::endl; qI->zoomPrevious();
else
std::cout << "qI2 pointer is bad" << std::endl; // call a function defined in the QgisIface class and send its value to stdout
*/ int foo = qI->getInt();
//zoomFullX(); std::cout << "Result of getInt is: " << foo << std::endl;
qI->zoomFull();
// qgisMainWindow->zoomFull();
QMessageBox::information(qgisMainWindow,"Message From Plugin", "Click Ok to zoom previous");
qI->zoomPrevious();
std::cout << "Result of getInt is: " << foo << std::endl;
} }
ExamplePlugin::~ExamplePlugin(){
ExamplePlugin::~ExamplePlugin()
{
} }
QString ExamplePlugin::name(){ /* Following functions return name, description, version, and type for the plugin */
return pName; QString ExamplePlugin::name()
} {
QString ExamplePlugin::version(){ return pName;
return pVersion;
}
QString ExamplePlugin::description(){
return pDescription;
}
int ExamplePlugin::type(){
return QgisPlugin::UI;
} }
void ExamplePlugin::initGui(){ QString ExamplePlugin::version()
// add a test menu {
QPopupMenu *pluginMenu = new QPopupMenu( qgisMainWindow ); return pVersion;
pluginMenu->insertItem("&Open", this, SLOT(open()));
pluginMenu->insertItem( "&New" , this, SLOT(newThing()));
// a test toolbar
QMenuBar *menu = ((QMainWindow *)qgisMainWindow)->menuBar();
menu->insertItem( "&PluginMenu", pluginMenu );
QAction *zoomPreviousAction = new QAction( "Zoom Previous",QIconSet(icon_matrix), "&Zoom Previous", CTRL+Key_S, qgisMainWindow, "zoomFull" );
connect( zoomPreviousAction, SIGNAL( activated() ) , this, SLOT( zoomPrevious() ) );
QToolBar * fileTools = new QToolBar( (QMainWindow *)qgisMainWindow, "zoom operations" );
fileTools->setLabel( "Zoom Operations" );
zoomPreviousAction->addTo(fileTools);
}
void ExamplePlugin::open(){
QMessageBox::information(qgisMainWindow, "Message from plugin", "You chose the open menu");
}
void ExamplePlugin::newThing(){
QMessageBox::information(qgisMainWindow, "Message from plugin", "You chose the new menu");
} }
void ExamplePlugin::zoomPrevious(){ QString ExamplePlugin::description()
qI->zoomPrevious(); {
return pDescription;
} }
extern "C" QgisPlugin * classFactory(QgisApp *qgis, QgisIface *qI){ int ExamplePlugin::type()
return new ExamplePlugin(qgis, qI); {
} return QgisPlugin::UI;
extern "C" QString name(){
return QString("Test Plugin");
}
extern "C" QString description(){
return QString("Default QGIS Test Plugin");
}
extern "C" int type(){
return QgisPlugin::UI;
}
extern "C" void unload(QgisPlugin *p){
delete p;
} }
/*
* Initialize the GUI interface for the plugin
*/
void ExamplePlugin::initGui()
{
// add a test menu with 3 items
QPopupMenu *pluginMenu = new QPopupMenu(qgisMainWindow);
pluginMenu->insertItem("&Open", this, SLOT(open()));
pluginMenu->insertItem("&New", this, SLOT(newThing()));
pluginMenu->insertItem("&Unload Example Plugin", this, SLOT(unload()));
menu = ((QMainWindow *) qgisMainWindow)->menuBar();
menuId = menu->insertItem("&ExamplePluginMenu", pluginMenu);
/* Add a test toolbar with one tool (a zoom previous tool) */
// Create the action for tool
QAction *zoomPreviousAction = new QAction("Zoom Previous", QIconSet(icon_matrix), "&Zoom Previous",
CTRL + Key_S, qgisMainWindow, "zoomPrevious");
// Connect the action to the zoomPrevous slot
connect(zoomPreviousAction, SIGNAL(activated()), this, SLOT(zoomPrevious()));
// Add the toolbar
toolBar = new QToolBar((QMainWindow *) qgisMainWindow, "zoom operations");
toolBar->setLabel("Zoom Operations");
// Add the zoom previous tool to the toolbar
zoomPreviousAction->addTo(toolBar);
}
// Slot called when open is selected on the menu
void ExamplePlugin::open()
{
QMessageBox::information(qgisMainWindow, "Message from plugin", "You chose the open menu");
}
// Slot called when new is selected on the menu
void ExamplePlugin::newThing()
{
QMessageBox::information(qgisMainWindow, "Message from plugin", "You chose the new menu");
}
// Slot called when the zoomPrevious button is clicked
void ExamplePlugin::zoomPrevious()
{
qI->zoomPrevious();
}
// Unload the plugin bye cleaning up the GUI
void ExamplePlugin::unload()
{
// remove the GUI
menu->removeItem(menuId);
// cleanup anything else that needs to be nuked
delete toolBar;
}
/**
* Required extern functions needed for every plugin
* These functions can be called prior to creating an instance
* of the plugin class
*/
// Class factory to return a new instance of the plugin class
extern "C" QgisPlugin * classFactory(QgisApp * qgis, QgisIface * qI)
{
return new ExamplePlugin(qgis, qI);
}
// Return the name of the plugin
extern "C" QString name()
{
return QString("Test Plugin");
}
// Return the description
extern "C" QString description()
{
return QString("Default QGIS Test Plugin");
}
// Return the type (either UI or MapLayer plugin)
extern "C" int type()
{
return QgisPlugin::UI;
}
// Delete ourself
extern "C" void unload(QgisPlugin * p)
{
delete p;
}

View File

@ -3,22 +3,36 @@
#include "../qgisplugin.h" #include "../qgisplugin.h"
#include <qwidget.h> #include <qwidget.h>
#include <qmainwindow.h> #include <qmainwindow.h>
#include <qmessagebox.h>
#include <qpopupmenu.h> class QMessageBox;
#include <qmenubar.h> class QToolBar;
class QMenuBar;
class QPopupMenu;
//#include "qgsworkerclass.h" //#include "qgsworkerclass.h"
#include "../../src/qgisapp.h" #include "../../src/qgisapp.h"
/** /**
* \class ExamplePlugin * \class ExamplePlugin
* \brief An example QGIS plugin, illustrating how to add menu items, a toolbar, * \brief Example plugin for QGIS
* and perform an operation on the map canvas.
* *
* When loaded, this plugin adds a new menu to QGIS with two items. It also * This code is an example plugin for QGIS and a demonstration of the API
* adds a toolbar that has one button. When clicked, the button zooms the * All QGIS plugins must inherit from the abstract base class QgisPlugin. A
* map to the previous view. * plugin must implement the virtual functions defined in QgisPlugin:
* *name
* *version
* *description
* *type
* *
* This class must inherit from QObject and QgisPlugin. * In addition, a plugin must implement a the classFactory and unload
* functions. Note that these functions must be declared as extern "C"
*
* This plugin is not very useful. When loaded, it installs a new menu with two
* items and illustrates how to connect the items to slots which handle menu events.
* It also installs a toolbar with one button. When clicked, the button zooms the
* map to the previous extent.
*
* After the UI elements are initialized the plugin zooms the map canvas to the
* full extent of all layers.
*/ */
class ExamplePlugin : public QObject, public QgisPlugin{ class ExamplePlugin : public QObject, public QgisPlugin{
Q_OBJECT Q_OBJECT
@ -52,15 +66,32 @@ public:
//! Destructor //! Destructor
virtual ~ExamplePlugin(); virtual ~ExamplePlugin();
public slots: public slots:
//! open something
void open(); void open();
//! create something new
void newThing(); void newThing();
//! zoom the map to the previous extent
void zoomPrevious(); void zoomPrevious();
//! unload the plugin
void unload();
private: private:
//! Name of the plugin
QString pName; QString pName;
//! Version
QString pVersion; QString pVersion;
//! Descrption of the plugin
QString pDescription; QString pDescription;
//! Plugin type as defined in QgisPlugin::PLUGINTYPE
int ptype; int ptype;
//! Id of the plugin's menu. Used for unloading
int menuId;
//! Pointer to our toolbar
QToolBar *toolBar;
//! Pointer to our menu
QMenuBar *menu;
//! Pionter to QGIS main application object
QgisApp *qgisMainWindow; QgisApp *qgisMainWindow;
//! Pointer to the QGIS interface object
QgisIface *qI; QgisIface *qI;
}; };