QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgspluginlayerregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspluginlayerregistry.cpp - class for
3  registering plugin layer creators
4  -------------------
5  begin : Mon Nov 30 2009
6  copyright : (C) 2009 by Mathias Walker, Sourcepole
7  email : mwa at sourcepole.ch
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgspluginlayerregistry.h"
20 #include "qgslogger.h"
21 #include "qgspluginlayer.h"
22 #include "qgsmaplayerregistry.h"
23 
25  : mName( name )
26 {
27 }
28 
30 {
31 }
32 
34 {
35  return mName;
36 }
37 
39 {
40  return NULL;
41 }
42 
44 {
45  Q_UNUSED( layer );
46  return false;
47 }
48 
49 //=============================================================================
50 
54 {
55  if ( _instance == NULL )
56  {
58  }
59  return _instance;
60 }
61 
62 
64 {
65 }
66 
68 {
69  if ( !mPluginLayerTypes.isEmpty() )
70  {
71  QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
72  foreach ( QString typeName, mPluginLayerTypes.keys() )
73  removePluginLayerType( typeName );
74  }
75 }
76 
78 {
79  if ( type == NULL )
80  return false;
81  if ( mPluginLayerTypes.contains( type->name() ) )
82  return false;
83 
84  mPluginLayerTypes[type->name()] = type;
85  return true;
86 }
87 
88 
90 {
91  if ( !mPluginLayerTypes.contains( typeName ) )
92  return false;
93 
94  // remove all remaining layers of this type - to avoid invalid behaviour
95  QList<QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers().values();
96  foreach ( QgsMapLayer* layer, layers )
97  {
98  if ( layer->type() == QgsMapLayer::PluginLayer )
99  {
100  QgsPluginLayer* pl = qobject_cast<QgsPluginLayer*>( layer );
101  if ( pl->pluginLayerType() == typeName )
102  {
104  QStringList() << layer->id() );
105  }
106  }
107  }
108 
109  delete mPluginLayerTypes.take( typeName );
110  return true;
111 }
112 
114 {
115  return mPluginLayerTypes.value( typeName, NULL );
116 }
117 
118 
120 {
121  QgsPluginLayerType* type = pluginLayerType( typeName );
122  if ( !type )
123  {
124  QgsDebugMsg( "Unknown plugin layer type: " + typeName );
125  return NULL;
126  }
127 
128  return type->createLayer();
129 }
QgsPluginLayerType * pluginLayerType(QString typeName)
return plugin layer type metadata or NULL if doesn't exist
Base class for all map layer types.
Definition: qgsmaplayer.h:45
QgsPluginLayer * createLayer(QString typeName)
return new layer if corresponding plugin has been found, else return NULL
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:89
a registry of plugin layers types
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
Base class for plugin layers.
virtual QgsPluginLayer * createLayer()
return new layer of this type.
bool addPluginLayerType(QgsPluginLayerType *pluginLayerType)
add plugin layer type (take ownership) and return true on success
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
QString pluginLayerType()
return plugin layer type (the same as used in QgsPluginLayerRegistry)
static QgsPluginLayerRegistry * _instance
pointer to canonical Singleton object
virtual bool showLayerProperties(QgsPluginLayer *layer)
show plugin layer properties dialog.
bool removePluginLayerType(QString typeName)
remove plugin layer type and return true on success
QgsPluginLayerRegistry()
private since instance() creates it
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void removeMapLayers(QStringList theLayerIds)
Remove a set of layers from the registry.
const QMap< QString, QgsMapLayer * > & mapLayers()
Retrieve the mapLayers collection (mainly intended for use by projection)
static QgsPluginLayerRegistry * instance()
means of accessing canonical single instance
PluginLayerTypes mPluginLayerTypes
class for creating plugin specific layers