QGIS API Documentation  3.0.2-Girona (307d082)
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  if ( !mPluginLayerTypes.isEmpty() )
58  {
59  QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
60  PluginLayerTypes::const_iterator it = mPluginLayerTypes.constBegin();
61  for ( ; it != mPluginLayerTypes.constEnd(); ++it )
62  {
63  removePluginLayerType( it.key() );
64  }
65  }
66 }
67 
69 {
70  return mPluginLayerTypes.keys();
71 }
72 
74 {
75  if ( !type )
76  return false;
77 
78  if ( mPluginLayerTypes.contains( type->name() ) )
79  return false;
80 
81  mPluginLayerTypes[type->name()] = type;
82  return true;
83 }
84 
85 
86 bool QgsPluginLayerRegistry::removePluginLayerType( const QString &typeName )
87 {
88  if ( !mPluginLayerTypes.contains( typeName ) )
89  return false;
90 
91  // remove all remaining layers of this type - to avoid invalid behavior
92  QList<QgsMapLayer *> layers = QgsProject::instance()->mapLayers().values();
93  Q_FOREACH ( QgsMapLayer *layer, layers )
94  {
95  if ( layer->type() == QgsMapLayer::PluginLayer )
96  {
97  QgsPluginLayer *pl = qobject_cast<QgsPluginLayer *>( layer );
98  if ( pl->pluginLayerType() == typeName )
99  {
101  QStringList() << layer->id() );
102  }
103  }
104  }
105 
106  delete mPluginLayerTypes.take( typeName );
107  return true;
108 }
109 
111 {
112  return mPluginLayerTypes.value( typeName, nullptr );
113 }
114 
115 
116 QgsPluginLayer *QgsPluginLayerRegistry::createLayer( const QString &typeName, const QString &uri )
117 {
118  QgsPluginLayerType *type = pluginLayerType( typeName );
119  if ( !type )
120  {
121  QgsDebugMsg( "Unknown plugin layer type: " + typeName );
122  return nullptr;
123  }
124 
125  if ( !uri.isEmpty() )
126  return type->createLayer( uri );
127  else
128  return type->createLayer();
129 }
Base class for all map layer types.
Definition: qgsmaplayer.h:56
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
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.
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.
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:383
void removeMapLayers(const QStringList &layerIds)
Remove a set of layers from the registry by layer ID.
QgsPluginLayer * createLayer(const QString &typeName, const QString &uri=QString())
Returns new layer if corresponding plugin has been found else returns a nullptr.
class for creating plugin specific layers