QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgslayeritem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayeritem.h.cpp
3 -------------------
4 begin : 2011-04-01
5 copyright : (C) 2011 Radim Blazek
6 email : radim dot blazek at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgslayeritem.h"
19#include "qgsmaplayer.h"
20#include "qgsvectorlayer.h"
21#include "qgsiconutils.h"
22
24{
26}
27
29{
31}
32
34{
36}
37
39{
41}
42
44{
46}
47
49{
51}
52
54{
56}
57
59{
61}
62
64{
66}
67
69{
70 if ( parent() )
71 {
72 return parent()->databaseConnection();
73 }
74 return nullptr;
75}
76
78{
79 return mLayerMetadata;
80}
81
83{
84 mLayerMetadata = metadata;
85}
86
88{
90}
91
92QgsLayerItem::QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path,
93 const QString &uri, Qgis::BrowserLayerType layerType, const QString &providerKey )
94 : QgsDataItem( Qgis::BrowserItemType::Layer, parent, name, path, providerKey )
95 , mUri( uri )
96 , mLayerType( layerType )
97{
98 mIconName = iconName( layerType );
99}
100
102{
103 switch ( mLayerType )
104 {
106 return Qgis::LayerType::Raster;
107
109 return Qgis::LayerType::Mesh;
110
112 return Qgis::LayerType::VectorTile;
113
115 return Qgis::LayerType::Plugin;
116
118 return Qgis::LayerType::PointCloud;
119
128 return Qgis::LayerType::Vector;
129 }
130
131 return Qgis::LayerType::Vector; // no warnings
132}
133
135{
136 switch ( layer->type() )
137 {
138 case Qgis::LayerType::Vector:
139 {
140 switch ( qobject_cast< QgsVectorLayer * >( layer )->geometryType() )
141 {
142 case Qgis::GeometryType::Point:
144
145 case Qgis::GeometryType::Line:
147
148 case Qgis::GeometryType::Polygon:
150
151 case Qgis::GeometryType::Null:
153
154 case Qgis::GeometryType::Unknown:
156 }
157
158 return Qgis::BrowserLayerType::Vector; // no warnings
159 }
160
161 case Qgis::LayerType::Raster:
163 case Qgis::LayerType::Plugin:
165 case Qgis::LayerType::Mesh:
167 case Qgis::LayerType::PointCloud:
169 case Qgis::LayerType::VectorTile:
171 case Qgis::LayerType::Annotation:
172 case Qgis::LayerType::Group:
173 return Qgis::BrowserLayerType::Vector; // will never happen!
174 }
175 return Qgis::BrowserLayerType::Vector; // no warnings
176}
177
179{
180 return qgsEnumValueToKey( layerType );
181}
182
184{
185 switch ( layerType )
186 {
188 return QStringLiteral( "/mIconPointLayer.svg" );
190 return QStringLiteral( "/mIconLineLayer.svg" );
192 return QStringLiteral( "/mIconPolygonLayer.svg" );
194 return QStringLiteral( "/mIconGeometryCollectionLayer.svg" );
197 return QStringLiteral( "/mIconTableLayer.svg" );
199 return QStringLiteral( "/mIconRaster.svg" );
201 return QStringLiteral( "/mIconMeshLayer.svg" );
203 return QStringLiteral( "/mIconPointCloudLayer.svg" );
204 default:
205 return QStringLiteral( "/mIconLayer.png" );
206 }
207}
208
210{
211 return false;
212}
213
215{
216 //QgsDebugMsg ( mPath + " x " + other->mPath );
217 if ( type() != other->type() )
218 {
219 return false;
220 }
221 //const QgsLayerItem *o = qobject_cast<const QgsLayerItem *> ( other );
222 const QgsLayerItem *o = qobject_cast<const QgsLayerItem *>( other );
223 if ( !o )
224 return false;
225
226 return ( mPath == o->mPath && mName == o->mName && mUri == o->mUri && mProviderKey == o->mProviderKey );
227}
228
230{
232
233 switch ( mapLayerType() )
234 {
235 case Qgis::LayerType::Vector:
236 u.layerType = QStringLiteral( "vector" );
237 switch ( mLayerType )
238 {
241 break;
244 break;
247 break;
250 break;
251
261 break;
262 }
263 break;
264 case Qgis::LayerType::Raster:
265 u.layerType = QStringLiteral( "raster" );
266 break;
267 case Qgis::LayerType::Mesh:
268 u.layerType = QStringLiteral( "mesh" );
269 break;
270 case Qgis::LayerType::VectorTile:
271 u.layerType = QStringLiteral( "vector-tile" );
272 break;
273 case Qgis::LayerType::PointCloud:
274 u.layerType = QStringLiteral( "pointcloud" );
275 break;
276 case Qgis::LayerType::Plugin:
277 u.layerType = QStringLiteral( "plugin" );
278 break;
279 case Qgis::LayerType::Group:
280 u.layerType = QStringLiteral( "group" );
281 break;
282 case Qgis::LayerType::Annotation:
283 u.layerType = QStringLiteral( "annotation" );
284 break;
285 }
286
288 u.name = layerName();
289 u.uri = uri();
292
294 {
295 u.filePath = path();
296 }
297
298 return { u };
299}
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:55
@ ItemRepresentsFile
Item's path() directly represents a file on disk (since QGIS 3.22)
BrowserLayerType
Browser item layer types.
Definition: qgis.h:573
@ Point
Vector point layer.
@ Plugin
Plugin based layer.
@ Line
Vector line layer.
@ Polygon
Vector polygon layer.
@ Table
Database table.
@ Vector
Generic vector layer.
@ VectorTile
Vector tile layer.
@ Raster
Raster layer.
@ TableLayer
Vector non-spatial layer.
@ Database
Database layer.
@ PointCloud
Point cloud layer.
LayerType
Types of layers that can be added to a map.
Definition: qgis.h:115
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:155
@ LineString
LineString.
@ Polygon
Polygon.
@ NoGeometry
No geometry.
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Base class for all items in the model.
Definition: qgsdataitem.h:46
QString mName
Definition: qgsdataitem.h:449
QString mPath
Definition: qgsdataitem.h:455
virtual QgsAbstractDatabaseProviderConnection * databaseConnection() const
For data items that represent a DB connection or one of its children, this method returns a connectio...
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QString mIconName
Definition: qgsdataitem.h:457
QString path() const
Definition: qgsdataitem.h:354
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:303
QString mProviderKey
Definition: qgsdataitem.h:450
static QIcon iconLine()
Returns an icon representing line geometries.
static QIcon iconPolygon()
Returns an icon representing polygon geometries.
static QIcon iconForWkbType(Qgis::WkbType type)
Returns the icon for a vector layer whose geometry type is provided.
static QIcon iconDefaultLayer()
Returns a default icon for layers, which aren't the standard raster/vector/... types.
static QIcon iconPointCloud()
Returns an icon representing point cloud layers.
static QIcon iconPoint()
Returns an icon representing point geometries.
static QIcon iconTable()
Returns an icon representing non-spatial layers (tables).
static QIcon iconRaster()
Returns an icon representing raster layers.
static QIcon iconMesh()
Returns an icon representing mesh layers.
static QIcon iconVectorTile()
Returns an icon representing vector tile layers.
Item that represents a layer that can be opened with one of the providers.
Definition: qgslayeritem.h:31
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgslayeritem.h:86
static QIcon iconPoint()
QString uri() const
Returns layer uri or empty string if layer cannot be created.
Definition: qgslayeritem.h:71
static Qgis::BrowserLayerType typeFromMapLayer(QgsMapLayer *layer)
Returns the layer item type corresponding to a QgsMapLayer layer.
virtual Q_DECL_DEPRECATED bool deleteLayer()
Delete this layer item Use QgsDataItemGuiProvider::deleteLayer instead.
static QIcon iconLine()
QString mUri
The URI.
Definition: qgslayeritem.h:116
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgslayeritem.h:80
static QIcon iconTable()
static QString layerTypeAsString(Qgis::BrowserLayerType layerType)
Returns the string representation of the given layerType.
static QIcon iconDefault()
QgsAbstractDatabaseProviderConnection * databaseConnection() const override
For data items that represent a DB connection or one of its children, this method returns a connectio...
QString providerKey() const
Returns provider key.
Definition: qgslayeritem.h:74
static QIcon iconRaster()
static QIcon iconMesh()
Returns icon for mesh layer type.
QgsLayerItem(QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, Qgis::BrowserLayerType layerType, const QString &providerKey)
Constructor for QgsLayerItem.
Qgis::BrowserLayerType mLayerType
The layer type.
Definition: qgslayeritem.h:118
static QIcon iconForWkbType(Qgis::WkbType type)
Returns the icon for a vector layer whose geometry type is provided.
Qgis::LayerType mapLayerType() const
Returns the associated map layer type.
static QIcon iconPolygon()
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
void setLayerMetadata(const QgsLayerMetadata &metadata)
Set layer's metadata.
static QIcon iconPointCloud()
Returns icon for point cloud layer.
static QIcon iconVectorTile()
Returns icon for vector tile layer.
const QgsLayerMetadata & layerMetadata() const
Returns layer's metadata, it may be a default constructed metadata if metadata is not explicitly set.
virtual QString layerName() const
Definition: qgslayeritem.h:145
bool equal(const QgsDataItem *other) override
Returns true if this item is equal to another item (by testing item type and path).
static QString iconName(Qgis::BrowserLayerType layerType)
Returns the icon name of the given layerType.
A structured metadata store for a map layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Qgis::LayerType type
Definition: qgsmaplayer.h:80
QList< QgsMimeDataUtils::Uri > UriList
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:3713
Qgis::WkbType wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
QString filePath
Path to file, if uri is associated with a file.
QString uri
Identifier of the data source recognized by its providerKey.
QString name
Human readable name to be used e.g. in layer tree.
QString providerKey
For "vector" / "raster" type: provider id.
QString layerType
Type of URI.