QGIS API Documentation  2.99.0-Master (9ed189e)
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 "qgsproject.h"
23 
25  : mName( name )
26 {
27 }
28 
30 {
31  return mName;
32 }
33 
35 {
36  return nullptr;
37 }
38 
40 {
41  Q_UNUSED( uri );
42  return nullptr;
43 }
44 
46 {
47  Q_UNUSED( layer );
48  return false;
49 }
50 
51 //
52 // QgsPluginLayerRegistry
53 //
54 
56 {
57 }
58 
60 {
61  if ( !mPluginLayerTypes.isEmpty() )
62  {
63  QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
64  PluginLayerTypes::const_iterator it = mPluginLayerTypes.constBegin();
65  for ( ; it != mPluginLayerTypes.constEnd(); ++it )
66  {
67  removePluginLayerType( it.key() );
68  }
69  }
70 }
71 
73 {
74  return mPluginLayerTypes.keys();
75 }
76 
78 {
79  if ( !type )
80  return false;
81 
82  if ( mPluginLayerTypes.contains( type->name() ) )
83  return false;
84 
85  mPluginLayerTypes[type->name()] = type;
86  return true;
87 }
88 
89 
90 bool QgsPluginLayerRegistry::removePluginLayerType( const QString& typeName )
91 {
92  if ( !mPluginLayerTypes.contains( typeName ) )
93  return false;
94 
95  // remove all remaining layers of this type - to avoid invalid behavior
96  QList<QgsMapLayer*> layers = QgsProject::instance()->mapLayers().values();
97  Q_FOREACH ( QgsMapLayer* layer, layers )
98  {
99  if ( layer->type() == QgsMapLayer::PluginLayer )
100  {
101  QgsPluginLayer* pl = qobject_cast<QgsPluginLayer*>( layer );
102  if ( pl->pluginLayerType() == typeName )
103  {
105  QStringList() << layer->id() );
106  }
107  }
108  }
109 
110  delete mPluginLayerTypes.take( typeName );
111  return true;
112 }
113 
115 {
116  return mPluginLayerTypes.value( typeName, nullptr );
117 }
118 
119 
120 QgsPluginLayer* QgsPluginLayerRegistry::createLayer( const QString& typeName, const QString& uri )
121 {
122  QgsPluginLayerType* type = pluginLayerType( typeName );
123  if ( !type )
124  {
125  QgsDebugMsg( "Unknown plugin layer type: " + typeName );
126  return nullptr;
127  }
128 
129  if ( !uri.isEmpty() )
130  return type->createLayer( uri );
131  else
132  return type->createLayer();
133 }
Base class for all map layer types.
Definition: qgsmaplayer.h:52
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
Base class for plugin layers.
bool removePluginLayerType(const QString &typeName)
Remove plugin layer type and return true on success.
QgsPluginLayerType(const QString &name)
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all registered layers by layer ID.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
Definition: qgsmaplayer.cpp:96
virtual QgsPluginLayer * createLayer()
Return new layer of this type. Return NULL on error.
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.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
void removeMapLayers(const QStringList &theLayerIds)
Remove a set of layers from the registry by layer ID.
QString pluginLayerType()
Return plugin layer type (the same as used in QgsPluginLayerRegistry)
virtual bool showLayerProperties(QgsPluginLayer *layer)
Show plugin layer properties dialog. Return false if the dialog cannot be shown.
QStringList pluginLayerTypes()
List all known layer types.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:355
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