Quantum GIS API Documentation  1.8
src/core/qgspluginlayerregistry.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                     qgspluginlayerregistry.cpp - class for
00003                     registering plugin layer creators
00004                              -------------------
00005     begin                : Mon Nov 30 2009
00006     copyright            : (C) 2009 by Mathias Walker, Sourcepole
00007     email                : mwa at sourcepole.ch
00008  ***************************************************************************/
00009 
00010 /***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "qgspluginlayerregistry.h"
00020 #include "qgslogger.h"
00021 #include "qgspluginlayer.h"
00022 #include "qgsmaplayerregistry.h"
00023 
00024 QgsPluginLayerType::QgsPluginLayerType( QString name )
00025     : mName( name )
00026 {
00027 }
00028 
00029 QgsPluginLayerType::~QgsPluginLayerType()
00030 {
00031 }
00032 
00033 QString QgsPluginLayerType::name()
00034 {
00035   return mName;
00036 }
00037 
00038 QgsPluginLayer* QgsPluginLayerType::createLayer()
00039 {
00040   return NULL;
00041 }
00042 
00043 bool QgsPluginLayerType::showLayerProperties( QgsPluginLayer *layer )
00044 {
00045   Q_UNUSED( layer );
00046   return false;
00047 }
00048 
00049 //=============================================================================
00050 
00052 QgsPluginLayerRegistry* QgsPluginLayerRegistry::_instance = NULL;
00053 QgsPluginLayerRegistry* QgsPluginLayerRegistry::instance()
00054 {
00055   if ( _instance == NULL )
00056   {
00057     _instance = new QgsPluginLayerRegistry();
00058   }
00059   return _instance;
00060 }
00061 
00062 
00063 QgsPluginLayerRegistry::QgsPluginLayerRegistry()
00064 {
00065 }
00066 
00067 QgsPluginLayerRegistry::~QgsPluginLayerRegistry()
00068 {
00069   if ( !mPluginLayerTypes.isEmpty() )
00070   {
00071     QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
00072     foreach( QString typeName, mPluginLayerTypes.keys() )
00073     removePluginLayerType( typeName );
00074   }
00075 }
00076 
00077 bool QgsPluginLayerRegistry::addPluginLayerType( QgsPluginLayerType* type )
00078 {
00079   if ( type == NULL )
00080     return false;
00081   if ( mPluginLayerTypes.contains( type->name() ) )
00082     return false;
00083 
00084   mPluginLayerTypes[type->name()] = type;
00085   return true;
00086 }
00087 
00088 
00089 bool QgsPluginLayerRegistry::removePluginLayerType( QString typeName )
00090 {
00091   if ( !mPluginLayerTypes.contains( typeName ) )
00092     return false;
00093 
00094   // remove all remaining layers of this type - to avoid invalid behaviour
00095   QList<QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers().values();
00096   foreach( QgsMapLayer* layer, layers )
00097   {
00098     if ( layer->type() == QgsMapLayer::PluginLayer )
00099     {
00100       QgsPluginLayer* pl = qobject_cast<QgsPluginLayer*>( layer );
00101       if ( pl->pluginLayerType() == typeName )
00102       {
00103         QgsMapLayerRegistry::instance()->removeMapLayers(
00104           QStringList() << layer->id() );
00105       }
00106     }
00107   }
00108 
00109   delete mPluginLayerTypes.take( typeName );
00110   return true;
00111 }
00112 
00113 QgsPluginLayerType* QgsPluginLayerRegistry::pluginLayerType( QString typeName )
00114 {
00115   return mPluginLayerTypes.value( typeName, NULL );
00116 }
00117 
00118 
00119 QgsPluginLayer* QgsPluginLayerRegistry::createLayer( QString typeName )
00120 {
00121   QgsPluginLayerType* type = pluginLayerType( typeName );
00122   if ( !type )
00123   {
00124     QgsDebugMsg( "Unknown plugin layer type: " + typeName );
00125     return NULL;
00126   }
00127 
00128   return type->createLayer();
00129 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines