QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
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// QgsPluginLayerRegistry
57//
58
60{
61 if ( !mPluginLayerTypes.isEmpty() )
62 {
63 QgsDebugMsgLevel( u"QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty"_s, 2 );
64 const QStringList keys = mPluginLayerTypes.keys();
65 for ( const QString &key : keys )
66 {
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
90bool 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 const QList<QgsMapLayer *> layers = QgsProject::instance()->mapLayers().values(); // skip-keyword-check
97 const auto constLayers = layers;
98 for ( QgsMapLayer *layer : constLayers )
99 {
100 if ( layer->type() == Qgis::LayerType::Plugin )
101 {
102 QgsPluginLayer *pl = qobject_cast<QgsPluginLayer *>( layer );
103 if ( pl->pluginLayerType() == typeName )
104 {
105 QgsProject::instance()->removeMapLayers( // skip-keyword-check
106 QStringList() << layer->id() );
107 }
108 }
109 }
110
111 delete mPluginLayerTypes.take( typeName );
112 return true;
113}
114
116{
117 return mPluginLayerTypes.value( typeName, nullptr );
118}
119
120
121QgsPluginLayer *QgsPluginLayerRegistry::createLayer( const QString &typeName, const QString &uri )
122{
123 QgsPluginLayerType *type = pluginLayerType( typeName );
124 if ( !type )
125 {
126 QgsDebugError( "Unknown plugin layer type: " + typeName );
127 return nullptr;
128 }
129
130 if ( !uri.isEmpty() )
131 return type->createLayer( uri );
132 else
133 return type->createLayer();
134}
@ Plugin
Plugin based layer.
Definition qgis.h:209
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