Skip to content
Permalink
Browse files

Add some utility functions to new QgsMapLayerFactory class for easier…

… reuse

(cherry picked from commit 63a365a)
  • Loading branch information
nyalldawson committed Mar 11, 2021
1 parent c85b986 commit 932814cb69d6975e0c8684b3154e707efffa051b
@@ -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 *
************************************************************************/
@@ -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
@@ -357,6 +357,7 @@ set(QGIS_CORE_SRCS
qgsmaphittest.cpp
qgsmaplayer.cpp
qgsmaplayerelevationproperties.cpp
qgsmaplayerfactory.cpp
qgsmaplayerlegend.cpp
qgsmaplayermodel.cpp
qgsmaplayerproxymodel.cpp
@@ -951,6 +952,7 @@ set(QGIS_CORE_HDRS
qgsmaphittest.h
qgsmaplayer.h
qgsmaplayerdependency.h
qgsmaplayerfactory.h
qgsmaplayerlegend.h
qgsmaplayermodel.h
qgsmaplayerproxymodel.h
@@ -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 )
@@ -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 )
@@ -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"
@@ -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 )
@@ -30,6 +30,7 @@
#include "qgspointcloudrendererregistry.h"
#include "qgspointcloudlayerelevationproperties.h"
#include "qgsmaplayerlegend.h"
#include "qgsmaplayerfactory.h"
#include <QUrl>

QgsPointCloudLayer::QgsPointCloudLayer( const QString &path,
@@ -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 )
{
@@ -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"
@@ -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" ) );
@@ -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();
}
@@ -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
@@ -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>
@@ -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

@@ -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"
@@ -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() ) );

0 comments on commit 932814c

Please sign in to comment.