QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
20
21#include "qgslogger.h"
22#include "qgspluginlayer.h"
23#include "qgsproject.h"
24
26 : mName( name )
27{
28}
29
31{
32 return mName;
33}
34
36{
37 return nullptr;
38}
39
41{
42 Q_UNUSED( uri )
43 return nullptr;
44}
45
47{
48 Q_UNUSED( layer )
49 return false;
50}
51
52//
53// QgsPluginLayerRegistry
54//
55
57{
58 if ( !mPluginLayerTypes.isEmpty() )
59 {
60 QgsDebugMsgLevel( QStringLiteral( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" ), 2 );
61 const QStringList keys = mPluginLayerTypes.keys();
62 for ( const QString &key : keys )
63 {
65 }
66 }
67}
68
70{
71 return mPluginLayerTypes.keys();
72}
73
75{
76 if ( !type )
77 return false;
78
79 if ( mPluginLayerTypes.contains( type->name() ) )
80 return false;
81
82 mPluginLayerTypes[type->name()] = type;
83 return true;
84}
85
86
87bool QgsPluginLayerRegistry::removePluginLayerType( const QString &typeName )
88{
89 if ( !mPluginLayerTypes.contains( typeName ) )
90 return false;
91
92 // remove all remaining layers of this type - to avoid invalid behavior
93 const QList<QgsMapLayer *> layers = QgsProject::instance()->mapLayers().values(); // skip-keyword-check
94 const auto constLayers = layers;
95 for ( QgsMapLayer *layer : constLayers )
96 {
97 if ( layer->type() == Qgis::LayerType::Plugin )
98 {
99 QgsPluginLayer *pl = qobject_cast<QgsPluginLayer *>( layer );
100 if ( pl->pluginLayerType() == typeName )
101 {
102 QgsProject::instance()->removeMapLayers( // skip-keyword-check
103 QStringList() << layer->id() );
104 }
105 }
106 }
107
108 delete mPluginLayerTypes.take( typeName );
109 return true;
110}
111
113{
114 return mPluginLayerTypes.value( typeName, nullptr );
115}
116
117
118QgsPluginLayer *QgsPluginLayerRegistry::createLayer( const QString &typeName, const QString &uri )
119{
120 QgsPluginLayerType *type = pluginLayerType( typeName );
121 if ( !type )
122 {
123 QgsDebugError( "Unknown plugin layer type: " + typeName );
124 return nullptr;
125 }
126
127 if ( !uri.isEmpty() )
128 return type->createLayer( uri );
129 else
130 return type->createLayer();
131}
@ Plugin
Plugin based layer.
Definition qgis.h:193
Base class for all map layer types.
Definition qgsmaplayer.h:80
bool addPluginLayerType(QgsPluginLayerType *pluginLayerType)
Add plugin layer type (take ownership) and return true on success.
bool removePluginLayerType(const QString &typeName)
Remove plugin layer type and return true on success.
QStringList pluginLayerTypes()
List all known layer types.
QgsPluginLayerType * pluginLayerType(const QString &typeName)
Returns plugin layer type metadata or nullptr if doesn't exist.
QgsPluginLayer * createLayer(const QString &typeName, const QString &uri=QString())
Returns new layer if corresponding plugin has been found else returns nullptr.
Responsible for creating plugin specific map layers.
virtual bool showLayerProperties(QgsPluginLayer *layer)
Show plugin layer properties dialog. Return false if the dialog cannot be shown.
QgsPluginLayerType(const QString &name)
virtual QgsPluginLayer * createLayer()
Returns new layer of this type. Return nullptr on error.
Base class for plugin layers.
QString pluginLayerType() const
Returns plugin layer type (the same as used in QgsPluginLayerRegistry).
static QgsProject * instance()
Returns the QgsProject singleton instance.
void removeMapLayers(const QStringList &layerIds)
Remove a set of layers from the registry by layer ID.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61
#define QgsDebugError(str)
Definition qgslogger.h:57