QGIS API Documentation  2.13.0-Master
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 nullptr;
41 }
42 
44 {
45  Q_UNUSED( uri );
46  return nullptr;
47 }
48 
50 {
51  Q_UNUSED( layer );
52  return false;
53 }
54 
55 //=============================================================================
56 
58 QgsPluginLayerRegistry* QgsPluginLayerRegistry::_instance = nullptr;
60 {
61  if ( !_instance )
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  Q_FOREACH ( const QString& typeName, mPluginLayerTypes.keys() )
79  removePluginLayerType( typeName );
80  }
81 }
82 
84 {
85  return mPluginLayerTypes.keys();
86 }
87 
89 {
90  if ( !type )
91  return false;
92 
93  if ( mPluginLayerTypes.contains( type->name() ) )
94  return false;
95 
96  mPluginLayerTypes[type->name()] = type;
97  return true;
98 }
99 
100 
102 {
103  if ( !mPluginLayerTypes.contains( typeName ) )
104  return false;
105 
106  // remove all remaining layers of this type - to avoid invalid behaviour
108  Q_FOREACH ( QgsMapLayer* layer, layers )
109  {
110  if ( layer->type() == QgsMapLayer::PluginLayer )
111  {
112  QgsPluginLayer* pl = qobject_cast<QgsPluginLayer*>( layer );
113  if ( pl->pluginLayerType() == typeName )
114  {
116  QStringList() << layer->id() );
117  }
118  }
119  }
120 
121  delete mPluginLayerTypes.take( typeName );
122  return true;
123 }
124 
126 {
127  return mPluginLayerTypes.value( typeName, nullptr );
128 }
129 
130 
132 {
133  QgsPluginLayerType* type = pluginLayerType( typeName );
134  if ( !type )
135  {
136  QgsDebugMsg( "Unknown plugin layer type: " + typeName );
137  return nullptr;
138  }
139 
140  if ( !uri.isEmpty() )
141  return type->createLayer( uri );
142  else
143  return type->createLayer();
144 }
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
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.
bool removePluginLayerType(const QString &typeName)
Remove plugin layer type and return true on success.
QgsPluginLayerType(const QString &name)
void removeMapLayers(const QStringList &theLayerIds)
Remove a set of layers from the registry.
virtual QgsPluginLayer * createLayer()
Return new layer of this type.
QgsPluginLayerType * pluginLayerType(const QString &typeName)
Return plugin layer type metadata or NULL if doesn't exist.
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.
QString pluginLayerType()
Return plugin layer type (the same as used in QgsPluginLayerRegistry)
virtual bool showLayerProperties(QgsPluginLayer *layer)
Show plugin layer properties dialog.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QStringList pluginLayerTypes()
List all known layer types.
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)
QgsPluginLayer * createLayer(const QString &typeName, const QString &uri=QString())
Return new layer if corresponding plugin has been found, else return NULL.
const T value(const Key &key) const
class for creating plugin specific layers