QGIS API Documentation  2.17.0-Master (973e4b0)
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  PluginLayerTypes::const_iterator it = mPluginLayerTypes.constBegin();
79  for ( ; it != mPluginLayerTypes.constEnd(); ++it )
80  {
81  removePluginLayerType( it.key() );
82  }
83  }
84 }
85 
87 {
88  return mPluginLayerTypes.keys();
89 }
90 
92 {
93  if ( !type )
94  return false;
95 
96  if ( mPluginLayerTypes.contains( type->name() ) )
97  return false;
98 
99  mPluginLayerTypes[type->name()] = type;
100  return true;
101 }
102 
103 
105 {
106  if ( !mPluginLayerTypes.contains( typeName ) )
107  return false;
108 
109  // remove all remaining layers of this type - to avoid invalid behaviour
111  Q_FOREACH ( QgsMapLayer* layer, layers )
112  {
113  if ( layer->type() == QgsMapLayer::PluginLayer )
114  {
115  QgsPluginLayer* pl = qobject_cast<QgsPluginLayer*>( layer );
116  if ( pl->pluginLayerType() == typeName )
117  {
119  QStringList() << layer->id() );
120  }
121  }
122  }
123 
124  delete mPluginLayerTypes.take( typeName );
125  return true;
126 }
127 
129 {
130  return mPluginLayerTypes.value( typeName, nullptr );
131 }
132 
133 
135 {
136  QgsPluginLayerType* type = pluginLayerType( typeName );
137  if ( !type )
138  {
139  QgsDebugMsg( "Unknown plugin layer type: " + typeName );
140  return nullptr;
141  }
142 
143  if ( !uri.isEmpty() )
144  return type->createLayer( uri );
145  else
146  return type->createLayer();
147 }
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:97
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 by layer ID.
virtual QgsPluginLayer * createLayer()
Return new layer of this type.
QgsPluginLayerType * pluginLayerType(const QString &typeName)
Return plugin layer type metadata or NULL if doesn&#39;t exist.
bool addPluginLayerType(QgsPluginLayerType *pluginLayerType)
Add plugin layer type (take ownership) and return true on success.
bool isEmpty() const
QString id() const
Get this layer&#39;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)
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all registered layers by layer ID.
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.
static QgsPluginLayerRegistry * instance()
Means of accessing canonical single instance.
QgsPluginLayer * createLayer(const QString &typeName, const QString &uri=QString())
Return new layer if corresponding plugin has been found, else return NULL.
class for creating plugin specific layers