QGIS API Documentation 4.3.0-Master (91d4a333d5d)
Loading...
Searching...
No Matches
qgsiconutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsiconutils.cpp
3 -------------------
4 begin : May 2021
5 copyright : (C) 2021 Nyall Dawson
6 email : nyall dot dawson 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 "qgsiconutils.h"
19
20#include "qgsapplication.h"
21#include "qgsmaplayer.h"
22#include "qgspluginlayer.h"
23#include "qgsvectorlayer.h"
24
25#include <QIcon>
26#include <QPainter>
27#include <QPixmap>
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
33{
34 const Qgis::GeometryType geomType = QgsWkbTypes::geometryType( type );
35 return iconForGeometryType( geomType );
36}
37
39{
40 switch ( typeGroup )
41 {
43 return iconTable();
45 return iconPoint();
47 return iconLine();
49 return iconPolygon();
52 }
53 return iconDefaultLayer();
54}
55
57{
58 return QgsApplication::getThemeIcon( u"/mIconPointLayer.svg"_s );
59}
60
62{
63 return QgsApplication::getThemeIcon( u"/mIconLineLayer.svg"_s );
64}
65
67{
68 return QgsApplication::getThemeIcon( u"/mIconPolygonLayer.svg"_s );
69}
70
72{
73 return QgsApplication::getThemeIcon( u"/mIconGeometryCollectionLayer.svg"_s );
74}
75
77{
78 return QgsApplication::getThemeIcon( u"/mIconTableLayer.svg"_s );
79}
80
82{
83 return QgsApplication::getThemeIcon( u"/mIconRaster.svg"_s );
84}
85
87{
88 return QgsApplication::getThemeIcon( u"/mIconMeshLayer.svg"_s );
89}
90
92{
93 return QgsApplication::getThemeIcon( u"/mIconVectorTileLayer.svg"_s );
94}
95
97{
98 return QgsApplication::getThemeIcon( u"/mIconPointCloudLayer.svg"_s );
99}
100
102{
103 return QgsApplication::getThemeIcon( u"/mIconTiledSceneLayer.svg"_s );
104}
105
107{
108 return QgsApplication::getThemeIcon( u"/mIconLayer.png"_s );
109}
110
112{
113 if ( layer )
114 {
115 switch ( layer->type() )
116 {
124 {
125 return QgsIconUtils::iconForLayerType( layer->type() );
126 }
128 {
129 if ( const QgsPluginLayer *pl = qobject_cast<const QgsPluginLayer *>( layer ) )
130 {
131 const QIcon icon = pl->icon();
132 if ( !icon.isNull() )
133 return icon;
134 }
135 // fallback to default icon if layer did not provide a specific icon
136 return QgsIconUtils::iconForLayerType( layer->type() );
137 }
139 {
140 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer );
141 if ( !vl )
142 {
143 return QIcon();
144 }
145 const Qgis::GeometryType geomType = vl->geometryType();
146 switch ( geomType )
147 {
149 {
151 }
153 {
155 }
157 {
158 return QgsIconUtils::iconLine();
159 }
161 {
163 }
165 {
167 }
168 }
169 }
170 }
171 }
172 return QIcon();
173}
174
176{
177 switch ( type )
178 {
181
183 return QgsIconUtils::iconMesh();
184
187
190
193
196
198 return QgsApplication::getThemeIcon( u"/mIconAnnotationLayer.svg"_s );
199
202 break;
203 }
204 return QIcon();
205}
206
207QIcon QgsIconUtils::addOverlay( const QIcon &icon, const QString &overlayPath, QSize size )
208{
209 if ( icon.isNull() )
210 return icon;
211
212 size = icon.actualSize( size );
213 const qreal dpr = icon.pixmap( size ).devicePixelRatio();
214
215 // Render through a QIcon so the SVG scales correctly and honours device pixel ratio
216 // (QPixmap loaded directly from a file ignores the size argument when no colors are passed).
217 const QIcon overlayIcon( overlayPath );
218
219 QPixmap pixmap( size * dpr );
220 pixmap.setDevicePixelRatio( dpr );
221 pixmap.fill( Qt::transparent );
222
223 QPainter painter( &pixmap );
224 painter.drawPixmap( 0, 0, icon.pixmap( size, dpr ) );
225 painter.drawPixmap( 0, 0, overlayIcon.pixmap( size, dpr ) );
226 painter.end();
227
228 return QIcon( pixmap );
229}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:399
@ Point
Points.
Definition qgis.h:400
@ Line
Lines.
Definition qgis.h:401
@ Polygon
Polygons.
Definition qgis.h:402
@ Unknown
Unknown types.
Definition qgis.h:403
@ Null
No geometry.
Definition qgis.h:404
LayerType
Types of layers that can be added to a map.
Definition qgis.h:206
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
@ Plugin
Plugin based layer.
Definition qgis.h:209
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:215
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:212
@ Vector
Vector layer.
Definition qgis.h:207
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:211
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:210
@ Raster
Raster layer.
Definition qgis.h:208
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:213
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:314
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QIcon iconForLayerType(Qgis::LayerType type)
Returns the default icon for the specified layer type.
static QIcon addOverlay(const QIcon &icon, const QString &overlayPath, QSize size=QSize(16, 16))
Returns a copy of icon with an overlay icon rendered from the image file at overlayPath.
static QIcon iconLine()
Returns an icon representing line geometries.
static QIcon iconPolygon()
Returns an icon representing polygon geometries.
static QIcon iconTiledScene()
Returns an icon representing tiled scene layers.
static QIcon iconForWkbType(Qgis::WkbType type)
Returns the icon for a vector layer whose geometry type is provided.
static QIcon iconGeometryCollection()
Returns an icon representing geometry collections.
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 iconForGeometryType(Qgis::GeometryType typeGroup)
Returns the icon for a vector layer whose geometry typeGroup is provided.
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 iconForLayer(const QgsMapLayer *layer)
Returns the icon corresponding to a specified map layer.
static QIcon iconMesh()
Returns an icon representing mesh layers.
static QIcon iconVectorTile()
Returns an icon representing vector tile layers.
Base class for all map layer types.
Definition qgsmaplayer.h:84
Qgis::LayerType type
Definition qgsmaplayer.h:94
Base class for plugin layers.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...