QGIS API Documentation  2.11.0-Master
 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( uri );
46  return NULL;
47 }
48 
50 {
51  Q_UNUSED( layer );
52  return false;
53 }
54 
55 //=============================================================================
56 
58 QgsPluginLayerRegistry* QgsPluginLayerRegistry::_instance = NULL;
60 {
61  if ( _instance == NULL )
62  {
63  _instance = new QgsPluginLayerRegistry();
64  }
65  return _instance;
66 }
67 
68 
69 QgsPluginLayerRegistry::QgsPluginLayerRegistry()
70 {
71 }
72 
74 {
75  if ( !mPluginLayerTypes.isEmpty() )
76  {
77  QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
78  foreach ( QString typeName, mPluginLayerTypes.keys() )
79  removePluginLayerType( typeName );
80  }
81 }
82 
84 {
85  return mPluginLayerTypes.keys();
86 }
87 
89 {
90  if ( type == NULL )
91  return false;
92  if ( mPluginLayerTypes.contains( type->name() ) )
93  return false;
94 
95  mPluginLayerTypes[type->name()] = type;
96  return true;
97 }
98 
99 
101 {
102  if ( !mPluginLayerTypes.contains( typeName ) )
103  return false;
104 
105  // remove all remaining layers of this type - to avoid invalid behaviour
107  foreach ( QgsMapLayer* layer, layers )
108  {
109  if ( layer->type() == QgsMapLayer::PluginLayer )
110  {
111  QgsPluginLayer* pl = qobject_cast<QgsPluginLayer*>( layer );
112  if ( pl->pluginLayerType() == typeName )
113  {
115  QStringList() << layer->id() );
116  }
117  }
118  }
119 
120  delete mPluginLayerTypes.take( typeName );
121  return true;
122 }
123 
125 {
126  return mPluginLayerTypes.value( typeName, NULL );
127 }
128 
129 
131 {
132  QgsPluginLayerType* type = pluginLayerType( typeName );
133  if ( !type )
134  {
135  QgsDebugMsg( "Unknown plugin layer type: " + typeName );
136  return NULL;
137  }
138 
139  if ( !uri.isEmpty() )
140  return type->createLayer( uri );
141  else
142  return type->createLayer();
143 }
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:49
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:93
bool contains(const Key &key) const
a registry of plugin layers types
QList< T > values() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
Base class for plugin layers.
virtual QgsPluginLayer * createLayer()
Return new layer of this type.
QgsPluginLayer * createLayer(QString typeName, const QString &uri=QString())
Return new layer if corresponding plugin has been found, else return NULL.
QList< Key > keys() const
bool addPluginLayerType(QgsPluginLayerType *pluginLayerType)
Add plugin layer type (take ownership) and return true on success.
bool isEmpty() const
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:99
QString pluginLayerType()
Return plugin layer type (the same as used in QgsPluginLayerRegistry)
virtual bool showLayerProperties(QgsPluginLayer *layer)
Show plugin layer properties dialog.
bool removePluginLayerType(QString typeName)
Remove plugin layer type and return true on success.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QStringList pluginLayerTypes()
List all known layer types.
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)
bool isEmpty() const
static QgsPluginLayerRegistry * instance()
Means of accessing canonical single instance.
T take(const Key &key)
const T value(const Key &key) const
class for creating plugin specific layers