Skip to content

Commit

Permalink
Add some utility functions to new QgsMapLayerFactory class for easier…
Browse files Browse the repository at this point in the history
… reuse

(cherry picked from commit 63a365a)
  • Loading branch information
nyalldawson committed Mar 11, 2021
1 parent c85b986 commit 932814c
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 37 deletions.
54 changes: 54 additions & 0 deletions python/core/auto_generated/qgsmaplayerfactory.sip.in
@@ -0,0 +1,54 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmaplayerfactory.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsMapLayerFactory
{
%Docstring
Contains utility functions for creating map layers.

.. versionadded:: 3.20
%End

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

static QgsMapLayerType typeFromString( const QString &string, bool &ok /Out/ );
%Docstring
Returns the map layer type corresponding a ``string`` value.

:param string: string to convert to map layer type

:return: - converted map layer type
- ok: will be set to ``True`` if ``string`` was successfully converted to a map layer type


.. seealso:: :py:func:`typeToString`
%End

static QString typeToString( QgsMapLayerType type );
%Docstring
Converts a map layer ``type`` to a string value.

.. seealso:: :py:func:`typeFromString`
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmaplayerfactory.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 @@ -106,6 +106,7 @@
%Include auto_generated/qgsmaphittest.sip
%Include auto_generated/qgsmaplayer.sip
%Include auto_generated/qgsmaplayerdependency.sip
%Include auto_generated/qgsmaplayerfactory.sip
%Include auto_generated/qgsmaplayerlegend.sip
%Include auto_generated/qgsmaplayermodel.sip
%Include auto_generated/qgsmaplayerproxymodel.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -357,6 +357,7 @@ set(QGIS_CORE_SRCS
qgsmaphittest.cpp
qgsmaplayer.cpp
qgsmaplayerelevationproperties.cpp
qgsmaplayerfactory.cpp
qgsmaplayerlegend.cpp
qgsmaplayermodel.cpp
qgsmaplayerproxymodel.cpp
Expand Down Expand Up @@ -951,6 +952,7 @@ set(QGIS_CORE_HDRS
qgsmaphittest.h
qgsmaplayer.h
qgsmaplayerdependency.h
qgsmaplayerfactory.h
qgsmaplayerlegend.h
qgsmaplayermodel.h
qgsmaplayerproxymodel.h
Expand Down
3 changes: 2 additions & 1 deletion src/core/annotations/qgsannotationlayer.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgspainting.h"
#include "qgsmaplayerfactory.h"
#include <QUuid>

QgsAnnotationLayer::QgsAnnotationLayer( const QString &name, const LayerOptions &options )
Expand Down Expand Up @@ -168,7 +169,7 @@ bool QgsAnnotationLayer::writeXml( QDomNode &layer_node, QDomDocument &doc, cons
return false;
}

mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "annotation" ) );
mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( QgsMapLayerType::AnnotationLayer ) );

QDomElement itemsElement = doc.createElement( "items" );
for ( auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
Expand Down
3 changes: 2 additions & 1 deletion src/core/mesh/qgsmeshlayer.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgscolorramp.h"
#include "qgslogger.h"
#include "qgsmaplayerlegend.h"
#include "qgsmaplayerfactory.h"
#include "qgsmeshdataprovider.h"
#include "qgsmeshdatasetgroupstore.h"
#include "qgsmeshlayer.h"
Expand Down Expand Up @@ -1234,7 +1235,7 @@ bool QgsMeshLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const
return false;
}

mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "mesh" ) );
mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( QgsMapLayerType::MeshLayer ) );

// add provider node
if ( mDataProvider )
Expand Down
3 changes: 2 additions & 1 deletion src/core/pointcloud/qgspointcloudlayer.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgspointcloudrendererregistry.h"
#include "qgspointcloudlayerelevationproperties.h"
#include "qgsmaplayerlegend.h"
#include "qgsmaplayerfactory.h"
#include <QUrl>

QgsPointCloudLayer::QgsPointCloudLayer( const QString &path,
Expand Down Expand Up @@ -121,7 +122,7 @@ bool QgsPointCloudLayer::readXml( const QDomNode &layerNode, QgsReadWriteContext
bool QgsPointCloudLayer::writeXml( QDomNode &layerNode, QDomDocument &doc, const QgsReadWriteContext &context ) const
{
QDomElement mapLayerNode = layerNode.toElement();
mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "point-cloud" ) );
mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( QgsMapLayerType::PointCloudLayer ) );

if ( mDataProvider )
{
Expand Down
81 changes: 50 additions & 31 deletions src/core/project/qgsproject.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgslayertreeregistrybridge.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgsmaplayerfactory.h"
#include "qgspluginlayer.h"
#include "qgspluginlayerregistry.h"
#include "qgsprojectfiletransform.h"
Expand Down Expand Up @@ -1174,41 +1175,59 @@ bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &broken
std::unique_ptr<QgsMapLayer> mapLayer;

QgsScopedRuntimeProfile profile( tr( "Create layer" ), QStringLiteral( "projectload" ) );
if ( type == QLatin1String( "vector" ) )
{
mapLayer = qgis::make_unique<QgsVectorLayer>();
// apply specific settings to vector layer
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer.get() ) )
{
vl->setReadExtentFromXml( mTrustLayerMetadata || ( flags & QgsProject::ReadFlag::FlagTrustLayerMetadata ) );
}
}
else if ( type == QLatin1String( "raster" ) )
{
mapLayer = qgis::make_unique<QgsRasterLayer>();
}
else if ( type == QLatin1String( "mesh" ) )
{
mapLayer = qgis::make_unique<QgsMeshLayer>();
}
else if ( type == QLatin1String( "vector-tile" ) )
{
mapLayer = qgis::make_unique<QgsVectorTileLayer>();
}
else if ( type == QLatin1String( "point-cloud" ) )
{
mapLayer = qgis::make_unique<QgsPointCloudLayer>();
}
else if ( type == QLatin1String( "plugin" ) )

bool ok = false;
const QgsMapLayerType layerType( QgsMapLayerFactory::typeFromString( type, ok ) );
if ( !ok )
{
QString typeName = layerElem.attribute( QStringLiteral( "name" ) );
mapLayer.reset( QgsApplication::pluginLayerRegistry()->createLayer( typeName ) );
QgsDebugMsg( QStringLiteral( "Unknown layer type \"%1\"" ).arg( type ) );
return false;
}
else if ( type == QLatin1String( "annotation" ) )

switch ( layerType )
{
QgsAnnotationLayer::LayerOptions options( mTransformContext );
mapLayer = qgis::make_unique<QgsAnnotationLayer>( QString(), options );
case QgsMapLayerType::VectorLayer:
{
mapLayer = qgis::make_unique<QgsVectorLayer>();
// apply specific settings to vector layer
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer.get() ) )
{
vl->setReadExtentFromXml( mTrustLayerMetadata || ( flags & QgsProject::ReadFlag::FlagTrustLayerMetadata ) );
}
break;
}

case QgsMapLayerType::RasterLayer:
mapLayer = qgis::make_unique<QgsRasterLayer>();
break;

case QgsMapLayerType::MeshLayer:
mapLayer = qgis::make_unique<QgsMeshLayer>();
break;

case QgsMapLayerType::VectorTileLayer:
mapLayer = qgis::make_unique<QgsVectorTileLayer>();
break;

case QgsMapLayerType::PointCloudLayer:
mapLayer = qgis::make_unique<QgsPointCloudLayer>();
break;

case QgsMapLayerType::PluginLayer:
{
QString typeName = layerElem.attribute( QStringLiteral( "name" ) );
mapLayer.reset( QgsApplication::pluginLayerRegistry()->createLayer( typeName ) );
break;
}

case QgsMapLayerType::AnnotationLayer:
{
QgsAnnotationLayer::LayerOptions options( mTransformContext );
mapLayer = qgis::make_unique<QgsAnnotationLayer>( QString(), options );
break;
}
}

if ( !mapLayer )
{
QgsDebugMsg( QStringLiteral( "Unable to create layer" ) );
Expand Down
62 changes: 62 additions & 0 deletions src/core/qgsmaplayerfactory.cpp
@@ -0,0 +1,62 @@
/***************************************************************************
qgsmaplayerfactory.cpp
--------------------------------------
Date : March 2021
Copyright : (C) 2021 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmaplayerfactory.h"

QgsMapLayerType QgsMapLayerFactory::typeFromString( const QString &string, bool &ok )
{
ok = true;
if ( string.compare( QLatin1String( "vector" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::VectorLayer;
else if ( string.compare( QLatin1String( "raster" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::RasterLayer;
else if ( string.compare( QLatin1String( "mesh" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::MeshLayer;
else if ( string.compare( QLatin1String( "vector-tile" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::VectorTileLayer;
else if ( string.compare( QLatin1String( "point-cloud" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::PointCloudLayer;
else if ( string.compare( QLatin1String( "plugin" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::PluginLayer;
else if ( string.compare( QLatin1String( "annotation" ), Qt::CaseInsensitive ) == 0 )
return QgsMapLayerType::AnnotationLayer;

ok = false;
return QgsMapLayerType::VectorLayer;
}

QString QgsMapLayerFactory::typeToString( QgsMapLayerType type )
{
switch ( type )
{
case QgsMapLayerType::VectorLayer:
return QStringLiteral( "vector" );
case QgsMapLayerType::RasterLayer:
return QStringLiteral( "raster" );
case QgsMapLayerType::PluginLayer:
return QStringLiteral( "plugin" );
case QgsMapLayerType::MeshLayer:
return QStringLiteral( "mesh" );
case QgsMapLayerType::VectorTileLayer:
return QStringLiteral( "vector-tile" );
case QgsMapLayerType::AnnotationLayer:
return QStringLiteral( "annotation" );
case QgsMapLayerType::PointCloudLayer:
return QStringLiteral( "point-cloud" );
}
return QString();
}
57 changes: 57 additions & 0 deletions src/core/qgsmaplayerfactory.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgsmaplayerfactory.h
--------------------------------------
Date : March 2021
Copyright : (C) 2021 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSMAPLAYERFACTORY_H
#define QGSMAPLAYERFACTORY_H

#include "qgis_core.h"
#include "qgsmaplayer.h"

#include <QString>

/**
* \ingroup core
* \brief Contains utility functions for creating map layers.
*
* \since QGIS 3.20
*/
class CORE_EXPORT QgsMapLayerFactory
{
public:

/**
* Returns the map layer type corresponding a \a string value.
*
* \param string string to convert to map layer type
* \param ok will be set to TRUE if \a string was successfully converted to a map layer type
*
* \returns converted map layer type
*
* \see typeToString()
*/
static QgsMapLayerType typeFromString( const QString &string, bool &ok SIP_OUT );

/**
* Converts a map layer \a type to a string value.
*
* \see typeFromString()
*/
static QString typeToString( QgsMapLayerType type );

};

#endif // QGSMAPLAYERFACTORY_H
3 changes: 2 additions & 1 deletion src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -55,6 +55,7 @@ email : tim at linfiniti.com
#include "qgscubicrasterresampler.h"
#include "qgsrasterlayertemporalproperties.h"
#include "qgsruntimeprofiler.h"
#include "qgsmaplayerfactory.h"

#include <cmath>
#include <cstdio>
Expand Down Expand Up @@ -2182,7 +2183,7 @@ bool QgsRasterLayer::writeXml( QDomNode &layer_node,
return false;
}

mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "raster" ) );
mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( QgsMapLayerType::RasterLayer ) );

// add provider node

Expand Down
3 changes: 2 additions & 1 deletion src/core/vector/qgsvectorlayer.cpp
Expand Up @@ -56,6 +56,7 @@
#include "qgsfeature.h"
#include "qgsfeaturerequest.h"
#include "qgsfields.h"
#include "qgsmaplayerfactory.h"
#include "qgsgeometry.h"
#include "qgslayermetadataformatter.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -1867,7 +1868,7 @@ bool QgsVectorLayer::writeXml( QDomNode &layer_node,
return false;
}

mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "vector" ) );
mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( QgsMapLayerType::VectorLayer ) );

// set the geometry type
mapLayerNode.setAttribute( QStringLiteral( "geometry" ), QgsWkbTypes::geometryDisplayString( geometryType() ) );
Expand Down

0 comments on commit 932814c

Please sign in to comment.