QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30 : mName( name )
31{
32}
33
35{
36 return mName;
37}
38
40{
41 return nullptr;
42}
43
45{
46 Q_UNUSED( uri )
47 return nullptr;
48}
49
51{
52 Q_UNUSED( layer )
53 return false;
54}
55
56//
57// QgsPluginLayerRegistry
58//
59
61{
62 if ( !mPluginLayerTypes.isEmpty() )
63 {
64 QgsDebugMsgLevel( u"QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty"_s, 2 );
65 const QStringList keys = mPluginLayerTypes.keys();
66 for ( const QString &key : keys )
67 {
69 }
70 }
71}
72
74{
75 return mPluginLayerTypes.keys();
76}
77
79{
80 if ( !type )
81 return false;
82
83 if ( mPluginLayerTypes.contains( type->name() ) )
84 return false;
85
86 mPluginLayerTypes[type->name()] = type;
87 return true;
88}
89
90
91bool QgsPluginLayerRegistry::removePluginLayerType( const QString &typeName )
92{
93 if ( !mPluginLayerTypes.contains( typeName ) )
94 return false;
95
96 // remove all remaining layers of this type - to avoid invalid behavior
97 const QList<QgsMapLayer *> layers = QgsProject::instance()->mapLayers().values(); // skip-keyword-check
98 const auto constLayers = layers;
99 for ( QgsMapLayer *layer : constLayers )
100 {
101 if ( layer->type() == Qgis::LayerType::Plugin )
102 {
103 QgsPluginLayer *pl = qobject_cast<QgsPluginLayer *>( layer );
104 if ( pl->pluginLayerType() == typeName )
105 {
106 QgsProject::instance()->removeMapLayers( // skip-keyword-check
107 QStringList() << layer->id() );
108 }
109 }
110 }
111
112 delete mPluginLayerTypes.take( typeName );
113 return true;
114}
115
117{
118 return mPluginLayerTypes.value( typeName, nullptr );
119}
120
121
122QgsPluginLayer *QgsPluginLayerRegistry::createLayer( const QString &typeName, const QString &uri )
123{
124 QgsPluginLayerType *type = pluginLayerType( typeName );
125 if ( !type )
126 {
127 QgsDebugError( "Unknown plugin layer type: " + typeName );
128 return nullptr;
129 }
130
131 if ( !uri.isEmpty() )
132 return type->createLayer( uri );
133 else
134 return type->createLayer();
135}
@ Plugin
Plugin based layer.
Definition qgis.h:196
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:63
#define QgsDebugError(str)
Definition qgslogger.h:59