Quantum GIS API Documentation  1.7.4
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 /* $Id$ */
00019 
00020 #include "qgspluginlayerregistry.h"
00021 #include "qgslogger.h"
00022 #include "qgspluginlayer.h"
00023 #include "qgsmaplayerregistry.h"
00024 
00025 QgsPluginLayerType::QgsPluginLayerType( QString name )
00026     : mName( name )
00027 {
00028 }
00029 
00030 QgsPluginLayerType::~QgsPluginLayerType()
00031 {
00032 }
00033 
00034 QString QgsPluginLayerType::name()
00035 {
00036   return mName;
00037 }
00038 
00039 QgsPluginLayer* QgsPluginLayerType::createLayer()
00040 {
00041   return NULL;
00042 }
00043 
00044 bool QgsPluginLayerType::showLayerProperties( QgsPluginLayer* layer )
00045 {
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()->removeMapLayer( layer->id() );
00104       }
00105     }
00106   }
00107 
00108   delete mPluginLayerTypes.take( typeName );
00109   return true;
00110 }
00111 
00112 QgsPluginLayerType* QgsPluginLayerRegistry::pluginLayerType( QString typeName )
00113 {
00114   return mPluginLayerTypes.value( typeName, NULL );
00115 }
00116 
00117 
00118 QgsPluginLayer* QgsPluginLayerRegistry::createLayer( QString typeName )
00119 {
00120   QgsPluginLayerType* type = pluginLayerType( typeName );
00121   if ( !type )
00122   {
00123     QgsDebugMsg( "Unknown plugin layer type: " + typeName );
00124     return NULL;
00125   }
00126 
00127   return type->createLayer();
00128 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines