Skip to content

Commit

Permalink
Move QgsLayerItem icon related static methods to new QgsIconUtils class
Browse files Browse the repository at this point in the history
These utilities are used in a LOT of places in QGIS, but the
qgslayeritem.h include is a very heavy dependency. It makes sense
to have these utilities in a nice lightweight generic class.
  • Loading branch information
nyalldawson committed May 20, 2021
1 parent b5aace6 commit 0684462
Show file tree
Hide file tree
Showing 38 changed files with 362 additions and 138 deletions.
85 changes: 85 additions & 0 deletions python/core/auto_generated/qgsiconutils.sip.in
@@ -0,0 +1,85 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsiconutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsIconUtils
{
%Docstring(signature="appended")
Contains utility functions for working with icons.

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgsiconutils.h"
%End
public:

static QIcon iconForWkbType( QgsWkbTypes::Type type );
%Docstring
Returns the icon for a vector layer whose geometry ``type`` is provided.
%End

static QIcon iconPoint();
%Docstring
Returns an icon representing point geometries.
%End

static QIcon iconLine();
%Docstring
Returns an icon representing line geometries.
%End

static QIcon iconPolygon();
%Docstring
Returns an icon representing polygon geometries.
%End

static QIcon iconTable();
%Docstring
Returns an icon representing non-spatial layers (tables).
%End

static QIcon iconRaster();
%Docstring
Returns an icon representing raster layers.
%End

static QIcon iconDefaultLayer();
%Docstring
Returns a default icon for layers, which aren't the standard raster/vector/... types.
%End

static QIcon iconMesh();
%Docstring
Returns an icon representing mesh layers.
%End

static QIcon iconVectorTile();
%Docstring
Returns an icon representing vector tile layers.
%End

static QIcon iconPointCloud();
%Docstring
Returns an icon representing point cloud layers.
%End

};



/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsiconutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -75,6 +75,7 @@
%Include auto_generated/qgshistogram.sip
%Include auto_generated/qgshstoreutils.sip
%Include auto_generated/qgshtmlutils.sip
%Include auto_generated/qgsiconutils.sip
%Include auto_generated/qgsidentifycontext.sip
%Include auto_generated/qgsimagecache.sip
%Include auto_generated/qgsinterval.sip
Expand Down
5 changes: 2 additions & 3 deletions src/app/qgsalignrasterdialog.cpp
Expand Up @@ -17,12 +17,11 @@
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsalignraster.h"
#include "qgsdataitem.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayercombobox.h"
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgslayeritem.h"
#include "qgsiconutils.h"

#include <QCheckBox>
#include <QFileDialog>
Expand Down Expand Up @@ -146,7 +145,7 @@ void QgsAlignRasterDialog::populateLayersView()
{
QString layerName = _rasterLayerName( item.inputFilename );

QStandardItem *si = new QStandardItem( QgsLayerItem::iconRaster(), layerName );
QStandardItem *si = new QStandardItem( QgsIconUtils::iconRaster(), layerName );
model->appendRow( si );

if ( i == refLayerIndex )
Expand Down
5 changes: 2 additions & 3 deletions src/app/qgsnewspatialitelayerdialog.cpp
Expand Up @@ -24,7 +24,6 @@
#include "qgsapplication.h"
#include "qgsabstractdatabaseproviderconnection.h"
#include "qgisapp.h" // <- for theme icons
#include "qgsdataitem.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"
#include "qgscoordinatereferencesystem.h"
Expand All @@ -37,7 +36,7 @@
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgslayeritem.h"
#include "qgsiconutils.h"

#include <QPushButton>
#include <QLineEdit>
Expand All @@ -54,7 +53,7 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W

const auto addGeomItem = [this]( QgsWkbTypes::Type type, const QString & sqlType )
{
mGeometryTypeBox->addItem( QgsLayerItem::iconForWkbType( type ), QgsWkbTypes::translatedDisplayString( type ), sqlType );
mGeometryTypeBox->addItem( QgsIconUtils::iconForWkbType( type ), QgsWkbTypes::translatedDisplayString( type ), sqlType );
};

addGeomItem( QgsWkbTypes::NoGeometry, QString() );
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -339,6 +339,7 @@ set(QGIS_CORE_SRCS
qgshistogram.cpp
qgshstoreutils.cpp
qgshtmlutils.cpp
qgsiconutils.cpp
qgsidentifycontext.cpp
qgsimagecache.cpp
qgsinterval.cpp
Expand Down Expand Up @@ -951,6 +952,7 @@ set(QGIS_CORE_HDRS
qgshistogram.h
qgshstoreutils.h
qgshtmlutils.h
qgsiconutils.h
qgsidentifycontext.h
qgsimagecache.h
qgsindexedfeature.h
Expand Down
12 changes: 6 additions & 6 deletions src/core/browser/qgsfieldsitem.cpp
Expand Up @@ -16,7 +16,7 @@
***************************************************************************/

#include "qgsfieldsitem.h"
#include "qgslayeritem.h"
#include "qgsiconutils.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -163,20 +163,20 @@ QIcon QgsFieldItem::icon()
{
if ( mField.typeName() == QLatin1String( "raster" ) )
{
return QgsLayerItem::iconRaster();
return QgsIconUtils::iconRaster();
}
const QgsWkbTypes::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
switch ( geomType )
{
case QgsWkbTypes::GeometryType::LineGeometry:
return QgsLayerItem::iconLine();
return QgsIconUtils::iconLine();
case QgsWkbTypes::GeometryType::PointGeometry:
return QgsLayerItem::iconPoint();
return QgsIconUtils::iconPoint();
case QgsWkbTypes::GeometryType::PolygonGeometry:
return QgsLayerItem::iconPolygon();
return QgsIconUtils::iconPolygon();
case QgsWkbTypes::GeometryType::UnknownGeometry:
case QgsWkbTypes::GeometryType::NullGeometry:
return QgsLayerItem::iconDefault();
return QgsIconUtils::iconDefaultLayer();
}
}
const QIcon icon { QgsFields::iconForFieldType( mField.type() ) };
Expand Down
35 changes: 11 additions & 24 deletions src/core/browser/qgslayeritem.cpp
Expand Up @@ -19,69 +19,56 @@
#include "qgsapplication.h"
#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgsiconutils.h"

QIcon QgsLayerItem::iconForWkbType( QgsWkbTypes::Type type )
{
QgsWkbTypes::GeometryType geomType = QgsWkbTypes::geometryType( QgsWkbTypes::Type( type ) );
switch ( geomType )
{
case QgsWkbTypes::NullGeometry:
return iconTable();
case QgsWkbTypes::PointGeometry:
return iconPoint();
case QgsWkbTypes::LineGeometry:
return iconLine();
case QgsWkbTypes::PolygonGeometry:
return iconPolygon();
default:
break;
}
return iconDefault();
return QgsIconUtils::iconForWkbType( type );
}

QIcon QgsLayerItem::iconPoint()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) );
return QgsIconUtils::iconPoint();
}

QIcon QgsLayerItem::iconLine()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) );
return QgsIconUtils::iconLine();
}

QIcon QgsLayerItem::iconPolygon()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) );
return QgsIconUtils::iconPolygon();
}

QIcon QgsLayerItem::iconTable()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) );
return QgsIconUtils::iconTable();
}

QIcon QgsLayerItem::iconRaster()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconRaster.svg" ) );
return QgsIconUtils::iconRaster();
}

QIcon QgsLayerItem::iconMesh()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconMeshLayer.svg" ) );
return QgsIconUtils::iconMesh();
}

QIcon QgsLayerItem::iconVectorTile()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconVectorTileLayer.svg" ) );
return QgsIconUtils::iconVectorTile();
}

QIcon QgsLayerItem::iconPointCloud()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointCloudLayer.svg" ) );
return QgsIconUtils::iconPointCloud();
}

QIcon QgsLayerItem::iconDefault()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconLayer.png" ) );
return QgsIconUtils::iconDefaultLayer();
}

QgsLayerItem::QgsLayerItem( QgsDataItem *parent, const QString &name, const QString &path,
Expand Down
22 changes: 11 additions & 11 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -23,7 +23,6 @@
#include "qgslayertreeutils.h"
#include "qgslayertreemodellegendnode.h"
#include "qgsproject.h"
#include "qgsdataitem.h"
#include "qgsmaphittest.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerlegend.h"
Expand All @@ -34,7 +33,8 @@
#include "qgssymbollayerutils.h"
#include "qgsvectorlayer.h"
#include "qgslayerdefinition.h"
#include "qgslayeritem.h"
#include "qgsiconutils.h"
#include "qgsmimedatautils.h"

#include <QPalette>

Expand Down Expand Up @@ -203,34 +203,34 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
switch ( layer->type() )
{
case QgsMapLayerType::RasterLayer:
icon = QgsLayerItem::iconRaster();
icon = QgsIconUtils::iconRaster();
break;

case QgsMapLayerType::MeshLayer:
icon = QgsLayerItem::iconMesh();
icon = QgsIconUtils::iconMesh();
break;

case QgsMapLayerType::VectorTileLayer:
icon = QgsLayerItem::iconVectorTile();
icon = QgsIconUtils::iconVectorTile();
break;

case QgsMapLayerType::PointCloudLayer:
icon = QgsLayerItem::iconPointCloud();
icon = QgsIconUtils::iconPointCloud();
break;

case QgsMapLayerType::VectorLayer:
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer->geometryType() == QgsWkbTypes::PointGeometry )
icon = QgsLayerItem::iconPoint();
icon = QgsIconUtils::iconPoint();
else if ( vlayer->geometryType() == QgsWkbTypes::LineGeometry )
icon = QgsLayerItem::iconLine();
icon = QgsIconUtils::iconLine();
else if ( vlayer->geometryType() == QgsWkbTypes::PolygonGeometry )
icon = QgsLayerItem::iconPolygon();
icon = QgsIconUtils::iconPolygon();
else if ( vlayer->geometryType() == QgsWkbTypes::NullGeometry )
icon = QgsLayerItem::iconTable();
icon = QgsIconUtils::iconTable();
else
icon = QgsLayerItem::iconDefault();
icon = QgsIconUtils::iconDefaultLayer();
break;
}

Expand Down
1 change: 0 additions & 1 deletion src/core/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -28,7 +28,6 @@
#include "qgsapplication.h"
#include "qgsauthmanager.h"
#include "qgscoordinatetransform.h"
#include "qgsdataitem.h"
#include "qgsdataitemprovider.h"
#include "qgsdatasourceuri.h"
#include "qgsgdaldataitems.h"
Expand Down
1 change: 0 additions & 1 deletion src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -29,7 +29,6 @@ email : sherman at mrcc.com
#include "qgsapplication.h"
#include "qgsauthmanager.h"
#include "qgscplhttpfetchoverrider.h"
#include "qgsdataitem.h"
#include "qgsdataprovider.h"
#include "qgsfeature.h"
#include "qgsfields.h"
Expand Down
13 changes: 6 additions & 7 deletions src/core/qgsdatabasetablemodel.cpp
Expand Up @@ -16,8 +16,7 @@
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgsabstractdatabaseproviderconnection.h"
#include "qgsdataitem.h"
#include "qgslayeritem.h"
#include "qgsiconutils.h"

QgsDatabaseTableModel::QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema, QObject *parent )
: QAbstractItemModel( parent )
Expand Down Expand Up @@ -109,7 +108,7 @@ QVariant QgsDatabaseTableModel::data( const QModelIndex &index, int role ) const
if ( table.geometryColumnTypes().empty() )
{
if ( role == Qt::DecorationRole )
return QgsLayerItem::iconTable();
return QgsIconUtils::iconTable();
else
return QVariant();
}
Expand All @@ -121,21 +120,21 @@ QVariant QgsDatabaseTableModel::data( const QModelIndex &index, int role ) const
{
case QgsWkbTypes::PointGeometry:
{
return QgsLayerItem::iconPoint();
return QgsIconUtils::iconPoint();
}
case QgsWkbTypes::PolygonGeometry:
{
return QgsLayerItem::iconPolygon();
return QgsIconUtils::iconPolygon();
}
case QgsWkbTypes::LineGeometry:
{
return QgsLayerItem::iconLine();
return QgsIconUtils::iconLine();
}
default:
break;
}

return QgsLayerItem::iconTable();
return QgsIconUtils::iconTable();
}
else if ( role == RoleWkbType )
return table.geometryColumnTypes().at( 0 ).wkbType;
Expand Down

0 comments on commit 0684462

Please sign in to comment.