QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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#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( QStringLiteral( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" ) );
60 const QStringList keys = mPluginLayerTypes.keys();
61 for ( const QString &key : keys )
62 {
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
87{
88 if ( !mPluginLayerTypes.contains( typeName ) )
89 return false;
90
91 // remove all remaining layers of this type - to avoid invalid behavior
92 const QList<QgsMapLayer *> layers = QgsProject::instance()->mapLayers().values();
93 const auto constLayers = layers;
94 for ( QgsMapLayer *layer : constLayers )
95 {
96 if ( layer->type() == Qgis::LayerType::Plugin )
97 {
98 QgsPluginLayer *pl = qobject_cast<QgsPluginLayer *>( layer );
99 if ( pl->pluginLayerType() == typeName )
100 {
102 QStringList() << layer->id() );
103 }
104 }
105 }
106
107 delete mPluginLayerTypes.take( typeName );
108 return true;
109}
110
112{
113 return mPluginLayerTypes.value( typeName, nullptr );
114}
115
116
117QgsPluginLayer *QgsPluginLayerRegistry::createLayer( const QString &typeName, const QString &uri )
118{
120 if ( !type )
121 {
122 QgsDebugMsg( "Unknown plugin layer type: " + typeName );
123 return nullptr;
124 }
125
126 if ( !uri.isEmpty() )
127 return type->createLayer( uri );
128 else
129 return type->createLayer();
130}
Base class for all map layer types.
Definition: qgsmaplayer.h:73
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.
Class for creating plugin specific 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()
Returns plugin layer type (the same as used in QgsPluginLayerRegistry)
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
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 QgsDebugMsg(str)
Definition: qgslogger.h:38
const QString & typeName