QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsapplication.h"
20 #include "qgsmaplayer.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsiconutils.h"
23 
25 {
27 }
28 
30 {
31  return QgsIconUtils::iconPoint();
32 }
33 
35 {
36  return QgsIconUtils::iconLine();
37 }
38 
40 {
42 }
43 
45 {
46  return QgsIconUtils::iconTable();
47 }
48 
50 {
51  return QgsIconUtils::iconRaster();
52 }
53 
55 {
56  return QgsIconUtils::iconMesh();
57 }
58 
60 {
62 }
63 
65 {
67 }
68 
70 {
71  if ( parent() )
72  {
73  return parent()->databaseConnection();
74  }
75  return nullptr;
76 }
77 
79 {
81 }
82 
83 QgsLayerItem::QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path,
84  const QString &uri, Qgis::BrowserLayerType layerType, const QString &providerKey )
85  : QgsDataItem( Qgis::BrowserItemType::Layer, parent, name, path, providerKey )
86  , mUri( uri )
87  , mLayerType( layerType )
88 {
89  mIconName = iconName( layerType );
90 }
91 
93 {
94  switch ( mLayerType )
95  {
98 
101 
104 
107 
110 
120  }
121 
122  return QgsMapLayerType::VectorLayer; // no warnings
123 }
124 
126 {
127  switch ( layer->type() )
128  {
130  {
131  switch ( qobject_cast< QgsVectorLayer * >( layer )->geometryType() )
132  {
135 
138 
141 
144 
147  }
148 
149  return Qgis::BrowserLayerType::Vector; // no warnings
150  }
151 
164  return Qgis::BrowserLayerType::Vector; // will never happen!
165  }
166  return Qgis::BrowserLayerType::Vector; // no warnings
167 }
168 
170 {
171  return qgsEnumValueToKey( layerType );
172 }
173 
175 {
176  switch ( layerType )
177  {
179  return QStringLiteral( "/mIconPointLayer.svg" );
181  return QStringLiteral( "/mIconLineLayer.svg" );
183  return QStringLiteral( "/mIconPolygonLayer.svg" );
185  return QStringLiteral( "/mIconGeometryCollectionLayer.svg" );
188  return QStringLiteral( "/mIconTableLayer.svg" );
190  return QStringLiteral( "/mIconRaster.svg" );
192  return QStringLiteral( "/mIconMeshLayer.svg" );
194  return QStringLiteral( "/mIconPointCloudLayer.svg" );
195  default:
196  return QStringLiteral( "/mIconLayer.png" );
197  }
198 }
199 
201 {
202  return false;
203 }
204 
205 bool QgsLayerItem::equal( const QgsDataItem *other )
206 {
207  //QgsDebugMsg ( mPath + " x " + other->mPath );
208  if ( type() != other->type() )
209  {
210  return false;
211  }
212  //const QgsLayerItem *o = qobject_cast<const QgsLayerItem *> ( other );
213  const QgsLayerItem *o = qobject_cast<const QgsLayerItem *>( other );
214  if ( !o )
215  return false;
216 
217  return ( mPath == o->mPath && mName == o->mName && mUri == o->mUri && mProviderKey == o->mProviderKey );
218 }
219 
221 {
223 
224  switch ( mapLayerType() )
225  {
227  u.layerType = QStringLiteral( "vector" );
228  switch ( mLayerType )
229  {
232  break;
235  break;
238  break;
241  break;
242 
252  break;
253  }
254  break;
256  u.layerType = QStringLiteral( "raster" );
257  break;
259  u.layerType = QStringLiteral( "mesh" );
260  break;
262  u.layerType = QStringLiteral( "vector-tile" );
263  break;
265  u.layerType = QStringLiteral( "pointcloud" );
266  break;
268  u.layerType = QStringLiteral( "plugin" );
269  break;
271  u.layerType = QStringLiteral( "group" );
272  break;
274  u.layerType = QStringLiteral( "annotation" );
275  break;
276  }
277 
278  u.providerKey = providerKey();
279  u.name = layerName();
280  u.uri = uri();
283 
285  {
286  u.filePath = path();
287  }
288 
289  return { u };
290 }
QgsLayerItem::mapLayerType
QgsMapLayerType mapLayerType() const
Returns QgsMapLayerType.
Definition: qgslayeritem.cpp:92
QgsMimeDataUtils::Uri::wkbType
QgsWkbTypes::Type wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
Definition: qgsmimedatautils.h:148
QgsMimeDataUtils::Uri::name
QString name
Human readable name to be used e.g. in layer tree.
Definition: qgsmimedatautils.h:123
QgsMimeDataUtils::Uri::uri
QString uri
Identifier of the data source recognized by its providerKey.
Definition: qgsmimedatautils.h:125
QgsLayerItem::iconLine
static QIcon iconLine()
Definition: qgslayeritem.cpp:34
QgsIconUtils::iconRaster
static QIcon iconRaster()
Returns an icon representing raster layers.
Definition: qgsiconutils.cpp:69
QgsIconUtils::iconTable
static QIcon iconTable()
Returns an icon representing non-spatial layers (tables).
Definition: qgsiconutils.cpp:64
qgsEnumValueToKey
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:2440
QgsLayerItem::iconPointCloud
static QIcon iconPointCloud()
Returns icon for point cloud layer.
Definition: qgslayeritem.cpp:64
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:72
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:354
QgsMimeDataUtils::Uri::supportedFormats
QStringList supportedFormats
Definition: qgsmimedatautils.h:127
QgsMapLayerType::MeshLayer
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
QgsWkbTypes::NullGeometry
@ NullGeometry
Definition: qgswkbtypes.h:146
QgsIconUtils::iconPolygon
static QIcon iconPolygon()
Returns an icon representing polygon geometries.
Definition: qgsiconutils.cpp:54
QgsMapLayerType::VectorLayer
@ VectorLayer
Vector layer.
QgsLayerItem::iconDefault
static QIcon iconDefault()
Definition: qgslayeritem.cpp:78
Qgis::BrowserLayerType::Table
@ Table
Database table.
QgsMimeDataUtils::Uri::layerType
QString layerType
Type of URI.
Definition: qgsmimedatautils.h:112
QgsMapLayerType::AnnotationLayer
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
QgsMapLayerType
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgis.h:46
QgsWkbTypes::LineString
@ LineString
Definition: qgswkbtypes.h:73
QgsLayerItem::iconRaster
static QIcon iconRaster()
Definition: qgslayeritem.cpp:49
Qgis::BrowserLayerType::PointCloud
@ PointCloud
Point cloud layer.
QgsIconUtils::iconLine
static QIcon iconLine()
Returns an icon representing line geometries.
Definition: qgsiconutils.cpp:49
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsLayerItem::QgsLayerItem
QgsLayerItem(QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, Qgis::BrowserLayerType layerType, const QString &providerKey)
Constructor for QgsLayerItem.
Definition: qgslayeritem.cpp:83
QgsIconUtils::iconDefaultLayer
static QIcon iconDefaultLayer()
Returns a default icon for layers, which aren't the standard raster/vector/...
Definition: qgsiconutils.cpp:89
QgsLayerItem::layerTypeAsString
static QString layerTypeAsString(Qgis::BrowserLayerType layerType)
Returns the string representation of the given layerType.
Definition: qgslayeritem.cpp:169
QgsMimeDataUtils::UriList
QList< QgsMimeDataUtils::Uri > UriList
Definition: qgsmimedatautils.h:164
QgsDataItem::mIconName
QString mIconName
Definition: qgsdataitem.h:457
Qgis::BrowserLayerType
BrowserLayerType
Browser item layer types.
Definition: qgis.h:402
QgsLayerItem::deleteLayer
virtual Q_DECL_DEPRECATED bool deleteLayer()
Delete this layer item Use QgsDataItemGuiProvider::deleteLayer instead.
Definition: qgslayeritem.cpp:200
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsDataItem::databaseConnection
virtual QgsAbstractDatabaseProviderConnection * databaseConnection() const
For data items that represent a DB connection or one of its children, this method returns a connectio...
Definition: qgsdataitem.cpp:157
Qgis::BrowserLayerType::Mesh
@ Mesh
Mesh layer.
qgsapplication.h
Qgis::BrowserLayerType::Plugin
@ Plugin
Plugin based layer.
qgslayeritem.h
Qgis::BrowserItemCapability::ItemRepresentsFile
@ ItemRepresentsFile
Item's path() directly represents a file on disk (since QGIS 3.22)
QgsLayerItem::iconPoint
static QIcon iconPoint()
Definition: qgslayeritem.cpp:29
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
QgsMapLayerType::GroupLayer
@ GroupLayer
Composite group layer. Added in QGIS 3.24.
QgsLayerItem::iconName
static QString iconName(Qgis::BrowserLayerType layerType)
Returns the icon name of the given layerType.
Definition: qgslayeritem.cpp:174
QgsLayerItem::iconVectorTile
static QIcon iconVectorTile()
Returns icon for vector tile layer.
Definition: qgslayeritem.cpp:59
qgsmaplayer.h
QgsMapLayerType::RasterLayer
@ RasterLayer
Raster layer.
QgsLayerItem::mUri
QString mUri
The URI.
Definition: qgslayeritem.h:126
QgsIconUtils::iconPointCloud
static QIcon iconPointCloud()
Returns an icon representing point cloud layers.
Definition: qgsiconutils.cpp:84
QgsMimeDataUtils::Uri::filePath
QString filePath
Path to file, if uri is associated with a file.
Definition: qgsmimedatautils.h:154
Qgis::BrowserLayerType::NoType
@ NoType
No type.
QgsIconUtils::iconPoint
static QIcon iconPoint()
Returns an icon representing point geometries.
Definition: qgsiconutils.cpp:44
QgsDataItem::mName
QString mName
Definition: qgsdataitem.h:449
QgsMimeDataUtils::Uri
Definition: qgsmimedatautils.h:40
QgsMimeDataUtils::Uri::supportedCrs
QStringList supportedCrs
Definition: qgsmimedatautils.h:126
QgsLayerItem::iconForWkbType
static QIcon iconForWkbType(QgsWkbTypes::Type type)
Returns the icon for a vector layer whose geometry type is provided.
Definition: qgslayeritem.cpp:24
Qgis::BrowserLayerType::Database
@ Database
Database layer.
QgsLayerItem::iconTable
static QIcon iconTable()
Definition: qgslayeritem.cpp:44
QgsLayerItem::iconPolygon
static QIcon iconPolygon()
Definition: qgslayeritem.cpp:39
QgsMimeDataUtils::Uri::providerKey
QString providerKey
For "vector" / "raster" type: provider id.
Definition: qgsmimedatautils.h:120
qgsvectorlayer.h
Qgis::BrowserLayerType::VectorTile
@ VectorTile
Vector tile layer.
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
QgsWkbTypes::NoGeometry
@ NoGeometry
Definition: qgswkbtypes.h:85
QgsDataItem::mPath
QString mPath
Definition: qgsdataitem.h:455
QgsLayerItem::iconMesh
static QIcon iconMesh()
Returns icon for mesh layer type.
Definition: qgslayeritem.cpp:54
QgsDataItem::mProviderKey
QString mProviderKey
Definition: qgsdataitem.h:450
QgsLayerItem::typeFromMapLayer
static Qgis::BrowserLayerType typeFromMapLayer(QgsMapLayer *layer)
Returns the layer item type corresponding to a QgsMapLayer layer.
Definition: qgslayeritem.cpp:125
QgsIconUtils::iconVectorTile
static QIcon iconVectorTile()
Returns an icon representing vector tile layers.
Definition: qgsiconutils.cpp:79
QgsIconUtils::iconForWkbType
static QIcon iconForWkbType(QgsWkbTypes::Type type)
Returns the icon for a vector layer whose geometry type is provided.
Definition: qgsiconutils.cpp:25
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsWkbTypes::Polygon
@ Polygon
Definition: qgswkbtypes.h:74
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
QgsLayerItem::equal
bool equal(const QgsDataItem *other) override
Returns true if this item is equal to another item (by testing item type and path).
Definition: qgslayeritem.cpp:205
QgsLayerItem::mimeUris
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
Definition: qgslayeritem.cpp:220
QgsWkbTypes::UnknownGeometry
@ UnknownGeometry
Definition: qgswkbtypes.h:145
QgsLayerItem::mLayerType
Qgis::BrowserLayerType mLayerType
The layer type.
Definition: qgslayeritem.h:128
qgsiconutils.h
Qgis::BrowserLayerType::Line
@ Line
Vector line layer.
QgsLayerItem::supportedCrs
QStringList supportedCrs() const
Returns the supported CRS.
Definition: qgslayeritem.h:90
QgsLayerItem
Item that represents a layer that can be opened with one of the providers.
Definition: qgslayeritem.h:29
Qgis::BrowserLayerType::Raster
@ Raster
Raster layer.
Qgis::BrowserLayerType::Point
@ Point
Vector point layer.
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
QgsDataItem::capabilities2
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:303
Qgis::BrowserLayerType::TableLayer
@ TableLayer
Vector non-spatial layer.
QgsLayerItem::providerKey
QString providerKey() const
Returns provider key.
Definition: qgslayeritem.h:84
QgsIconUtils::iconMesh
static QIcon iconMesh()
Returns an icon representing mesh layers.
Definition: qgsiconutils.cpp:74
QgsLayerItem::layerName
virtual QString layerName() const
Definition: qgslayeritem.h:155
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
QgsLayerItem::supportedFormats
QStringList supportedFormats() const
Returns the supported formats.
Definition: qgslayeritem.h:96
QgsMapLayerType::PointCloudLayer
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
QgsDataItem::type
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:44
QgsMapLayerType::PluginLayer
@ PluginLayer
Plugin based layer.
Qgis::BrowserLayerType::Polygon
@ Polygon
Vector polygon layer.
Qgis::BrowserLayerType::Vector
@ Vector
Generic vector layer.
QgsLayerItem::databaseConnection
QgsAbstractDatabaseProviderConnection * databaseConnection() const override
For data items that represent a DB connection or one of its children, this method returns a connectio...
Definition: qgslayeritem.cpp:69
QgsLayerItem::uri
QString uri() const
Returns layer uri or empty string if layer cannot be created.
Definition: qgslayeritem.h:81
QgsMapLayer::type
QgsMapLayerType type
Definition: qgsmaplayer.h:80