Skip to content

Commit 9b5d9cd

Browse files
author
wonder
committed
- QgsPluginRegistry - metadata stored in QMap
- address plugins by their unique name: - library's basename for c++ plugins - module name for python plugins (before plugins were addressed by their name, causing ambiguity) - general cleanups in plugin handling git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9526 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e2ad982 commit 9b5d9cd

File tree

7 files changed

+146
-111
lines changed

7 files changed

+146
-111
lines changed

src/app/qgisapp.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#include "qgspastetransformations.h"
120120
#include "qgspluginitem.h"
121121
#include "qgspluginmanager.h"
122+
#include "qgspluginmetadata.h"
122123
#include "qgspluginregistry.h"
123124
#include "qgspoint.h"
124125
#include "qgsproject.h"
@@ -1846,7 +1847,7 @@ void QgisApp::restoreSessionPlugins( QString thePluginDirString )
18461847
{
18471848
QString myFullPath = thePluginDirString + "/" + myPluginDir[i];
18481849

1849-
1850+
QString baseName = QFileInfo(myFullPath).baseName();
18501851
QLibrary *myLib = new QLibrary( myFullPath );
18511852
bool loaded = myLib->load();
18521853
if ( loaded )
@@ -1858,15 +1859,15 @@ void QgisApp::restoreSessionPlugins( QString thePluginDirString )
18581859
if ( myName && myDescription && myVersion )
18591860
{
18601861
//check if the plugin was active on last session
1861-
QString myEntryName = myName();
1862+
18621863
// Windows stores a "true" value as a 1 in the registry so we
18631864
// have to use readBoolEntry in this function
18641865

1865-
if ( mySettings.value( "/Plugins/" + myEntryName ).toBool() )
1866+
if ( mySettings.value( "/Plugins/" + baseName ).toBool() )
18661867
{
18671868
//QgsDebugMsg("Loading plugin: " + myEntryName);
18681869

1869-
loadPlugin( myName(), myDescription(), myFullPath );
1870+
loadPlugin( myFullPath, myName() );
18701871
}
18711872
else
18721873
{
@@ -4142,7 +4143,7 @@ void QgisApp::showPluginManager()
41424143
}
41434144
else
41444145
{
4145-
loadPlugin( plugin.name(), plugin.description(), plugin.fullPath() );
4146+
loadPlugin( plugin.fullPath(), plugin.name() );
41464147
}
41474148
it++;
41484149
}
@@ -4209,17 +4210,17 @@ void QgisApp::loadPythonPlugin( QString packageName, QString pluginName )
42094210

42104211

42114212
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
4212-
4213+
42134214
// is loaded already?
4214-
if ( pRegistry->library( pluginName ).isEmpty() )
4215+
if ( ! pRegistry->isLoaded(packageName) )
42154216
{
42164217
mPythonUtils->loadPlugin( packageName );
42174218
mPythonUtils->startPlugin( packageName );
42184219

42194220
// TODO: test success
42204221

42214222
// add to plugin registry
4222-
pRegistry->addPythonPlugin( packageName, pluginName );
4223+
pRegistry->addPlugin( packageName, QgsPluginMetadata( packageName, pluginName, NULL, true) );
42234224

42244225
// add to settings
42254226
QSettings settings;
@@ -4230,13 +4231,15 @@ void QgisApp::loadPythonPlugin( QString packageName, QString pluginName )
42304231
}
42314232
}
42324233

4233-
void QgisApp::loadPlugin( QString name, QString description, QString theFullPathName )
4234+
void QgisApp::loadPlugin( QString theFullPathName, QString name )
42344235
{
42354236
QSettings settings;
42364237
// first check to see if its already loaded
42374238
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
4238-
QString lib = pRegistry->library( name );
4239-
if ( lib.length() > 0 )
4239+
4240+
QString baseName = QFileInfo(theFullPathName).baseName();
4241+
4242+
if ( pRegistry->isLoaded(baseName) )
42404243
{
42414244
// plugin is loaded
42424245
// QMessageBox::warning(this, "Already Loaded", description + " is already loaded");
@@ -4269,9 +4272,9 @@ void QgisApp::loadPlugin( QString name, QString description, QString theFullPath
42694272
{
42704273
pl->initGui();
42714274
// add it to the plugin registry
4272-
pRegistry->addPlugin( myLib->fileName(), name, pl );
4275+
pRegistry->addPlugin(baseName, QgsPluginMetadata(myLib->fileName(), name, pl) );
42734276
//add it to the qsettings file [ts]
4274-
settings.setValue( "/Plugins/" + name, true );
4277+
settings.setValue( "/Plugins/" + baseName, true );
42754278
}
42764279
else
42774280
{
@@ -4280,7 +4283,7 @@ void QgisApp::loadPlugin( QString name, QString description, QString theFullPath
42804283
"The following diagnostic information may help the QGIS developers resolve the issue:\n%1." ).arg
42814284
( myError ) );
42824285
//disable it to the qsettings file [ts]
4283-
settings.setValue( "/Plugins/" + name, false );
4286+
settings.setValue( "/Plugins/" + baseName, false );
42844287
}
42854288
}
42864289
else
@@ -4290,6 +4293,8 @@ void QgisApp::loadPlugin( QString name, QString description, QString theFullPath
42904293

42914294
}
42924295
break;
4296+
/*
4297+
// TODO: to be removed completely
42934298
case QgisPlugin::MAPLAYER:
42944299
{
42954300
// Map layer - requires interaction with the canvas
@@ -4320,6 +4325,7 @@ void QgisApp::loadPlugin( QString name, QString description, QString theFullPath
43204325
}
43214326
}
43224327
break;
4328+
*/
43234329
default:
43244330
// type is unknown
43254331
QgsDebugMsg( "Plugin " + theFullPathName + " did not return a valid type and cannot be loaded" );

src/app/qgisapp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ class QgisApp : public QMainWindow
385385
//! load python support if possible
386386
void loadPythonSupport();
387387
//! plugin loader
388-
void loadPlugin( QString name, QString description, QString mFullPath );
388+
void loadPlugin( QString mFullPath, QString name );
389389
//! python plugin loader
390390
void loadPythonPlugin( QString packageName, QString pluginName );
391391
//! Find the QMenu with the given name (ie the user visible text on the menu item)

src/app/qgspluginitem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
/* $Id$ */
1818
#include "qgspluginitem.h"
1919

20-
QgsPluginItem::QgsPluginItem( QString _name, QString _description, QString _fullPath, QString _type, bool _python ):
21-
m_name( _name ), m_description( _description ), m_fullPath( _fullPath ), m_type( _type ), m_python( _python )
20+
QgsPluginItem::QgsPluginItem( QString _name, QString _fullPath, QString _type, bool _python ):
21+
m_name( _name ), m_fullPath( _fullPath ), m_type( _type ), m_python( _python )
2222
{
2323

2424
}

src/app/qgspluginitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Class to contain information about a loadable plugin, including its name, descri
2626
class QgsPluginItem
2727
{
2828
public:
29-
QgsPluginItem( QString name = 0, QString description = 0, QString fullPath = 0, QString type = 0, bool python = false );
29+
QgsPluginItem( QString name = 0, QString fullPath = 0, QString type = 0, bool python = false );
3030
QString name();
3131
QString description();
3232
QString fullPath();

src/app/qgspluginmanager.cpp

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
const int PLUGIN_DATA_ROLE = Qt::UserRole;
5757
const int PLUGIN_LIBRARY_ROLE = Qt::UserRole + 1;
58-
const int PLUGIN_LIBRARY_NAME_ROLE = Qt::UserRole + 2;
58+
const int PLUGIN_BASE_NAME_ROLE = Qt::UserRole + 2;
5959

6060
QgsPluginManager::QgsPluginManager( QgsPythonUtils* pythonUtils, QWidget * parent, Qt::WFlags fl )
6161
: QDialog( parent, fl )
@@ -151,9 +151,9 @@ void QgsPluginManager::getPythonPluginDescriptions()
151151
// filtering will be done on the display role so give it name and desription
152152
// user wont see this text since we are using a custome delegate
153153
QStandardItem * mypDetailItem = new QStandardItem( pluginName + " - " + description );
154-
QString myLibraryName = "python:" + packageName;;
154+
QString myLibraryName = "python:" + packageName;
155155
mypDetailItem->setData( myLibraryName, PLUGIN_LIBRARY_ROLE ); //for loading libs later
156-
mypDetailItem->setData( pluginName, PLUGIN_LIBRARY_NAME_ROLE ); //for matching in registry later
156+
mypDetailItem->setData( packageName, PLUGIN_BASE_NAME_ROLE ); //for matching in registry later
157157
mypDetailItem->setCheckable( false );
158158
mypDetailItem->setEditable( false );
159159
// setData in the delegate with a variantised QgsDetailedItemData
@@ -167,19 +167,15 @@ void QgsPluginManager::getPythonPluginDescriptions()
167167

168168
// check to see if the plugin is loaded and set the checkbox accordingly
169169
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
170-
QString libName = pRegistry->library( pluginName );
171-
if ( libName.length() == 0 || !pRegistry->isPythonPlugin( pluginName ) )
170+
if ( pRegistry->isLoaded(packageName) && pRegistry->isPythonPlugin(packageName) )
172171
{
173-
QgsDebugMsg( "Couldn't find library name in the registry" );
172+
QgsDebugMsg( "Found plugin in the registry" );
173+
// set the checkbox
174+
myData.setChecked( true );
174175
}
175176
else
176177
{
177-
QgsDebugMsg( "Found library name in the registry" );
178-
if ( libName == packageName )
179-
{
180-
// set the checkbox
181-
myData.setChecked( true );
182-
}
178+
QgsDebugMsg( "Couldn't find plugin in the registry: " + packageName );
183179
}
184180
QVariant myVariant = qVariantFromValue( myData );
185181
mypDetailItem->setData( myVariant, PLUGIN_DATA_ROLE );
@@ -297,40 +293,40 @@ void QgsPluginManager::getPluginDescriptions()
297293
delete myLib;
298294
continue;
299295
}
296+
297+
QString pluginName = pName();
298+
QString pluginDesc = pDesc();
299+
QString pluginVersion = pVersion();
300+
QString baseName = QFileInfo(lib).baseName();
300301

301302
QString myLibraryName = pluginDir[i];
302303
// filtering will be done on the display role so give it name and desription
303304
// user wont see this text since we are using a custome delegate
304-
QStandardItem * mypDetailItem = new QStandardItem( pName() + " - " + pDesc() );
305+
QStandardItem * mypDetailItem = new QStandardItem( pluginName + " - " + pluginDesc );
305306
mypDetailItem->setData( myLibraryName, PLUGIN_LIBRARY_ROLE );
306-
mypDetailItem->setData( pName(), PLUGIN_LIBRARY_NAME_ROLE ); //for matching in registry later
307+
mypDetailItem->setData( baseName, PLUGIN_BASE_NAME_ROLE ); //for matching in registry later
307308
QgsDetailedItemData myData;
308-
myData.setTitle( pName() );
309-
myData.setDetail( pDesc() );
309+
myData.setTitle( pluginName );
310+
myData.setDetail( pluginDesc );
310311
myData.setRenderAsWidget( false );
311312
myData.setCheckable( true );
312313
myData.setChecked( false ); //start unchecked - we will check it later if needed
313314

314-
//round trip test - delete this...no need to uncomment
315-
//QgsDetailedItemData myData2 = qVariantValue<QgsDetailedItemData>(myVariant);
316-
//Q_ASSERT(myData.title() == myData2.title());
317-
//round trip test ends
318-
319315
QgsDebugMsg( "Getting an instance of the QgsPluginRegistry" );
320316

321317
// check to see if the plugin is loaded and set the checkbox accordingly
322318
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
323-
QString libName = pRegistry->library( pName() );
324319

325320
// get the library using the plugin description
326-
if ( libName.length() == 0 )
321+
if ( !pRegistry->isLoaded(baseName) )
327322
{
328-
QgsDebugMsg( "Couldn't find library name in the registry" );
323+
QgsDebugMsg( "Couldn't find plugin in the registry" );
329324
}
330325
else
331326
{
332-
QgsDebugMsg( "Found library name in the registry" );
333-
if ( libName == myLib->fileName() )
327+
QgsDebugMsg( "Found plugin in the registry" );
328+
// TODO: this check shouldn't be necessary, plugin base names must be unique
329+
if ( pRegistry->library(baseName) == myLib->fileName() )
334330
{
335331
// set the checkbox
336332
myData.setChecked( true );
@@ -367,32 +363,36 @@ void QgsPluginManager::unload()
367363
myIndex = mModelPlugins->index( row, 0 );
368364
// its off -- see if it is loaded and if so, unload it
369365
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
370-
QgsDebugMsg( QString( "Checking to see if %1 is loaded" ).arg( mModelPlugins->data( myIndex, PLUGIN_LIBRARY_NAME_ROLE ).toString() ) );
371-
QString pluginName = mModelPlugins->data( myIndex, PLUGIN_LIBRARY_NAME_ROLE ).toString();
372-
if ( pRegistry->isPythonPlugin( pluginName ) )
366+
367+
// is loaded?
368+
QString baseName = mModelPlugins->data( myIndex, PLUGIN_BASE_NAME_ROLE ).toString();
369+
if ( ! pRegistry->isLoaded( baseName ) )
370+
continue;
371+
372+
if ( pRegistry->isPythonPlugin( baseName ) )
373373
{
374374
if ( mPythonUtils && mPythonUtils->isEnabled() )
375375
{
376-
QString packageName = pRegistry->library( pluginName );
377-
mPythonUtils->unloadPlugin( packageName );
376+
mPythonUtils->unloadPlugin( baseName );
378377
//disable it to the qsettings file
379-
settings.setValue( "/PythonPlugins/" + packageName, false );
378+
settings.setValue( "/PythonPlugins/" + baseName, false );
380379
}
381380
}
382381
else // C++ plugin
383382
{
384-
QgisPlugin *plugin = pRegistry->plugin( pluginName );
383+
QgisPlugin *plugin = pRegistry->plugin( baseName );
385384
if ( plugin )
386385
{
387386
plugin->unload();
388387
}
389388
//disable it to the qsettings file [ts]
390-
settings.setValue( "/Plugins/" + pluginName, false );
389+
settings.setValue( "/Plugins/" + baseName, false );
391390
}
392391
// remove the plugin from the registry
393-
pRegistry->removePlugin( pluginName );
392+
pRegistry->removePlugin( baseName );
394393
}
395394
}
395+
QgsPluginRegistry::instance()->dump();
396396
}
397397

398398
std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
@@ -406,7 +406,8 @@ std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
406406

407407
if ( myData.isChecked() )
408408
{
409-
QString pluginName = mModelPlugins->item( row, 0 )->data( PLUGIN_LIBRARY_NAME_ROLE ).toString();
409+
//QString baseName = mModelPlugins->item( row, 0 )->data( PLUGIN_BASE_NAME_ROLE ).toString();
410+
QString pluginName = mModelPlugins->item( row, 0 )->data( Qt::DisplayRole ).toString();
410411
bool pythonic = false;
411412

412413
QString library = mModelPlugins->item( row, 0 )->data( PLUGIN_LIBRARY_ROLE ).toString();
@@ -425,9 +426,7 @@ std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
425426
// QString fullPath=0,
426427
// QString type=0,
427428
// bool python=false);
428-
pis.push_back( QgsPluginItem( pluginName,
429-
mModelPlugins->item( row, 0 )->data( Qt::DisplayRole ).toString(), //display role
430-
library, 0, pythonic ) );
429+
pis.push_back( QgsPluginItem( pluginName, library, 0, pythonic ) );
431430
}
432431

433432
}
@@ -472,9 +471,10 @@ void QgsPluginManager::on_vwPlugins_clicked( const QModelIndex &theIndex )
472471
// the index row in the underlying model so we need to jump through this
473472
// little hoop to get the correct item
474473
//
475-
QStandardItem * mypItem =
476-
mModelPlugins->findItems( theIndex.data( Qt ::DisplayRole ).toString() ).first();
477-
QgsDetailedItemData myData =
474+
475+
QModelIndex realIndex = mModelProxy->mapToSource(theIndex);
476+
QStandardItem* mypItem = mModelPlugins->itemFromIndex(realIndex);
477+
QgsDetailedItemData myData =
478478
qVariantValue<QgsDetailedItemData>( mypItem->data( PLUGIN_DATA_ROLE ) );
479479
if ( myData.isChecked() )
480480
{

0 commit comments

Comments
 (0)