plugin manager now actually manages plugins

git-svn-id: http://svn.osgeo.org/qgis/trunk@726 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-02-07 04:08:10 +00:00
parent 28247471b2
commit 51f753a555
10 changed files with 203 additions and 8 deletions

View File

@ -95,7 +95,7 @@ void QgsPgGeoprocessing::initGui()
QPopupMenu *pluginMenu = new QPopupMenu(qgisMainWindow);
pluginMenu->insertItem("&Buffer Features", this, SLOT(buffer()));
pluginMenu->insertItem("&Unload Geoprocessing Plugin", this, SLOT(unload()));
// pluginMenu->insertItem("&Unload Geoprocessing Plugin", this, SLOT(unload()));
menu = ((QMainWindow *) qgisMainWindow)->menuBar();

View File

@ -41,6 +41,8 @@ public:
//! Plugin type, either UI or map layer
virtual int type()=0;
virtual void initGui()=0;
//! Unload the plugin and cleanup the gui
virtual void unload()=0;
//! Interface to gui element collection object
//virtual QgisPluginGui *gui()=0;
//! Element types that can be added to the interface

View File

@ -88,7 +88,7 @@ void QgsSpitPlugin::initGui()
QPopupMenu *pluginMenu = new QPopupMenu(qgisMainWindow);
pluginMenu->insertItem("&Import Shapefiles to PostgreSQL", this, SLOT(spit()));
pluginMenu->insertItem("&Unload SPIT Plugin", this, SLOT(unload()));
// pluginMenu->insertItem("&Unload SPIT Plugin", this, SLOT(unload()));
menu = ((QMainWindow *) qgisMainWindow)->menuBar();

View File

@ -102,6 +102,10 @@ qgis_SOURCES = qgisapp.cpp \
qgsproviderregistry.h \
qgsprovidermetadata.cpp \
qgsprovidermetadata.h \
qgspluginmetadata.cpp \
qgspluginmetadata.h \
qgspluginregistry.cpp \
qgspluginregistry.h \
qgsdlgvectorlayerproperties.cpp \
qgsdlgvectorlayerproperties.h \
$(qgis_UI) \

View File

@ -80,6 +80,7 @@
#include "qgisapp.h"
#include "qgspluginitem.h"
#include "qgsproviderregistry.h"
#include "qgspluginregistry.h"
#include "qgssinglesymrenderer.h"
//#include "qgssisydialog.h"
#include "../plugins/qgisplugin.h"
@ -1118,6 +1119,13 @@ void QgisApp::actionPluginManager_activated(){
}
void QgisApp::loadPlugin(QString name, QString description, QString fullPath){
// first check to see if its already loaded
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
QString lib = pRegistry->library(name);
if(lib.length() > 0){
// plugin is loaded
// QMessageBox::warning(this, "Already Loaded", description + " is already loaded");
}else{
QLibrary *myLib = new QLibrary(fullPath);
#ifdef DEBUG
std::cout << "Library name is " << myLib->library() << std::endl;
@ -1141,6 +1149,8 @@ void QgisApp::loadPlugin(QString name, QString description, QString fullPath){
QgisPlugin *pl = cf(this, qgisInterface);
if(pl){
pl->initGui();
// add it to the plugin registry
pRegistry->addPlugin(myLib->library(), name, pl);
}else{
// something went wrong
QMessageBox::warning(this, tr("Error Loading Plugin"), tr("There was an error loading %1."));
@ -1162,6 +1172,7 @@ void QgisApp::loadPlugin(QString name, QString description, QString fullPath){
// set the main window pointer for the plugin
pl->setQgisMainWindow(this);
pl->initGui();
}else{
// something went wrong
QMessageBox::warning(this, tr("Error Loading Plugin"), tr("There was an error loading %1."));
@ -1190,6 +1201,7 @@ void QgisApp::loadPlugin(QString name, QString description, QString fullPath){
std::cout << "Failed to load " << fullPath << "\n";
#endif
}
}
}
void QgisApp::testMapLayerPlugins(){
// map layer plugins live in their own directory (somewhere to be determined)

View File

@ -22,6 +22,7 @@
#include "qgspluginmanager.h"
#include "qgspluginitem.h"
#include "qgsproviderregistry.h"
#include "qgspluginregistry.h"
#define TESTLIB
#ifdef TESTLIB
@ -30,7 +31,8 @@
QgsPluginManager::QgsPluginManager(QWidget *parent, const char * name)
: QgsPluginManagerBase(parent, name)
{
// set the default lib dir to the qgis install directory/lib
// set the default lib dir to the qgis install directory/lib (this info is
// available from the provider registry so we use it here)
QgsProviderRegistry *pr = QgsProviderRegistry::instance();
/* char **argv = qApp->argv();
QString appDir = argv[0];
@ -86,10 +88,29 @@ QDir pluginDir(txtPluginDir->text(), "*.so*", QDir::Name | QDir::IgnoreCase, QDi
description_t *pDesc = (description_t *) myLib->resolve("description");
type_t *pType = (type_t *) myLib->resolve("type");
if(pName && pDesc && pType){
QCheckListItem *pl = new QCheckListItem(lstPlugins, pName(),QCheckListItem::CheckBox); //, pDesc(), pluginDir[i]);
QCheckListItem *pl = new QCheckListItem(lstPlugins, pName(),QCheckListItem::CheckBox); //, pDesc(), pluginDir[i])
pl->setText(1, pDesc());
pl->setText(2, pluginDir[i]);
pl->setText(3, QString().setNum(pType()));
#ifdef DEBUG
std::cout << "Getting an instance of the QgsPluginRegistry" << std::endl;
#endif
// check to see if the plugin is loaded and set the checkbox accordingly
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
// get the library using the plugin description
#ifdef DEBUG
std::cout << "Getting library name from the registry" << std::endl;
#endif
QString libName = pRegistry->library(pName());
if(libName.length() > 0){
#ifdef DEBUG
std::cout << "Found library name in the registry" << std::endl;
#endif
if(libName == myLib->library()){
// set the checkbox
pl->setState(QCheckListItem::On);
}
}
}else{
std::cout << "Failed to get name, description, or type for " << myLib->library() << std::endl;
}
@ -99,6 +120,32 @@ QDir pluginDir(txtPluginDir->text(), "*.so*", QDir::Name | QDir::IgnoreCase, QDi
}
}
}
void QgsPluginManager::apply(){
unload();
accept();
}
void QgsPluginManager::unload(){
#ifdef DEBUG
std::cout << "Checking for plugins to unload" << std::endl;
#endif
QCheckListItem *lvi = (QCheckListItem *)lstPlugins->firstChild();
while(lvi != 0){
if(!lvi->isOn()){
// its off -- see if it is loaded and if so, unload it
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
#ifdef DEBUG
std::cout << "Checking to see if " << lvi->text(0) << " is loaded" << std::endl;
#endif
QgisPlugin *plugin = pRegistry->plugin(lvi->text(0));
if(plugin){
plugin->unload();
// remove the plugin from the registry
pRegistry->removePlugin(lvi->text(0));
}
}
lvi = (QCheckListItem *)lvi->nextSibling();
}
}
std::vector<QgsPluginItem> QgsPluginManager::getSelectedPlugins(){
std::vector<QgsPluginItem> pis;
QCheckListItem *lvi = (QCheckListItem *)lstPlugins->firstChild();
@ -106,7 +153,9 @@ std::vector<QgsPluginItem> QgsPluginManager::getSelectedPlugins(){
if(lvi->isOn()){
pis.push_back(QgsPluginItem(lvi->text(0), lvi->text(1), txtPluginDir->text() + "/" + lvi->text(2)));
}
}else{
}
lvi = (QCheckListItem *)lvi->nextSibling();
}
return pis;

View File

@ -28,7 +28,10 @@ public:
~QgsPluginManager();
void browseFiles();
void getPluginDescriptions();
void unload();
std::vector<QgsPluginItem> getSelectedPlugins();
public slots:
void apply();
};
#endif

View File

@ -1,4 +1,4 @@
<!DOCTYPE UI><UI version="3.2" stdsetdef="1">
<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
<class>QgsPluginManagerBase</class>
<widget class="QDialog">
<property name="name">
@ -9,7 +9,7 @@
<x>0</x>
<y>0</y>
<width>564</width>
<height>355</height>
<height>368</height>
</rect>
</property>
<property name="caption">
@ -198,11 +198,12 @@
<sender>btnOk</sender>
<signal>clicked()</signal>
<receiver>QgsPluginManagerBase</receiver>
<slot>accept()</slot>
<slot>apply()</slot>
</connection>
</connections>
<slots>
<slot>browseFiles()</slot>
<slot>apply()</slot>
</slots>
<layoutdefaults spacing="6" margin="11"/>
</UI>

69
src/qgspluginregistry.cpp Normal file
View File

@ -0,0 +1,69 @@
/***************************************************************************
QgsPluginRegistry.cpp - Singleton class for
tracking registering plugins.
-------------------
begin : Fri Feb 7 2004
copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include <iostream>
#include <qmessagebox.h>
#include <qstring.h>
#include <qdir.h>
#include <qlibrary.h>
#include <qapplication.h>
#include "qgsprovidermetadata.h"
#include "qgspluginregistry.h"
QgsPluginRegistry* QgsPluginRegistry::_instance = 0;
QgsPluginRegistry* QgsPluginRegistry::instance ()
{
if (_instance == 0){
_instance = new QgsPluginRegistry();
}
return _instance;
}
QgsPluginRegistry::QgsPluginRegistry(){
// constructor does nothing
}
QString QgsPluginRegistry::library(QString pluginKey)
{
QgsPluginMetadata * pmd = plugins[pluginKey];
QString retval;
if(pmd){
retval = pmd->library();
}
return retval;
}
QgsPluginMetadata *QgsPluginRegistry::pluginMetadata(QString name)
{
return plugins[name];
}
QgisPlugin * QgsPluginRegistry::plugin(QString name)
{
QgsPluginMetadata * pmd = plugins[name];
QgisPlugin *retval = 0;
if(pmd){
retval = pmd->plugin();
}
return retval;
}
void QgsPluginRegistry::addPlugin(QString library, QString name, QgisPlugin *plugin)
{
plugins[name] = new QgsPluginMetadata(library, name, plugin);
}
void QgsPluginRegistry::removePlugin(QString name)
{
plugins.erase(name);
}

55
src/qgspluginregistry.h Normal file
View File

@ -0,0 +1,55 @@
/***************************************************************************
qgspluginregistry.h
Singleton class for keeping track of installed plugins.
-------------------
begin : Mon Jan 26 2004
copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#ifndef QGSPLUGINREGISTRY_H
#define QGSPLUGINREGISTRY_H
#include <map>
#include "qgspluginmetadata.h"
class QgsPluginMetadata;
class QgisPlugin;
class QString;
/**
* \class QgsPluginRegistry
* \brief This class tracks plugins that are currently loaded an provides
* a means to fetch a pointer to a plugin and unload it
*/
class QgsPluginRegistry
{
public:
//! Returns the instance pointer, creating the object on the first call
static QgsPluginRegistry* instance();
//! Return the full path to the plugins library using the plugin name as a key
QString library(QString pluginKey);
//! Retrieve the metadata for a plugin by name
QgsPluginMetadata * pluginMetadata(QString name);
//! Retrieve a pointer to a loaded plugin by name
QgisPlugin * plugin(QString name);
//! Add a plugin to the map of loaded plugins
void addPlugin(QString _library, QString _name, QgisPlugin * _plugin);
//! Remove a plugin from the list of loaded plugins
void removePlugin(QString name);
protected:
//! protected constructor
QgsPluginRegistry();
private:
static QgsPluginRegistry* _instance;
std::map<QString,QgsPluginMetadata*> plugins;
};
#endif //QgsPluginRegistry_H