Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cleanup] Limit number of hardcoded strings for layer type by leveraging QgsMapLayerFactory::typeToString() #60360

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/PyQt6/core/auto_additions/qgsmimedatautils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The following has been generated automatically from src/core/qgsmimedatautils.h
try:
QgsMimeDataUtils.Uri.__attribute_docs__ = {'layerType': 'Type of URI.\n\nRecognized types include\n\n- "vector": vector layers\n- "raster": raster layers\n- "mesh": mesh layers\n- "pointcloud": point cloud layers\n- "vector-tile": vector tile layers\n- "tiled-scene": tiled scene layers\n- "plugin": plugin layers\n- "custom": custom types\n- "project": QGS/QGZ project file\n- "directory": directory path\n\nMime data from plugins may use additional custom layer types.', 'providerKey': 'For "vector" / "raster" type: provider id.\nFor "plugin" type: plugin layer type name.\nFor "custom" type: key of its :py:class:`QgsCustomDropHandler`\nFor "project" and "directory" types: unused', 'name': 'Human readable name to be used e.g. in layer tree', 'uri': 'Identifier of the data source recognized by its providerKey', 'layerId': 'Layer ID, if uri is associated with a layer from a :py:class:`QgsProject`.\n\n.. versionadded:: 3.8', 'pId': 'Unique ID associated with application instance. Can be used to identify\nif mime data was created inside the current application instance or not.\n\n.. versionadded:: 3.8', 'wkbType': 'WKB type, if associated with a vector layer, or :py:class:`QgsWkbTypes`.Unknown if not\nyet known.\n\n.. versionadded:: 3.8', 'filePath': 'Path to file, if uri is associated with a file.\n\n.. versionadded:: 3.22'}
QgsMimeDataUtils.Uri.__attribute_docs__ = {'layerType': 'Type of URI.\n\nRecognized types include\n\n- "vector": vector layers\n- "raster": raster layers\n- "mesh": mesh layers\n- "point-cloud": point cloud layers (spelled with a dash since QGIS 3.42.0. In prior versions, there was no dash)\n- "vector-tile": vector tile layers\n- "tiled-scene": tiled scene layers\n- "annotation": annotation layers\n- "group": group layers\n- "plugin": plugin layers\n- "custom": custom types\n- "project": QGS/QGZ project file\n- "directory": directory path\n\nNote: use :py:func:`QgsMapLayerFactory.typeToString()` to convert from a\n:py:class:`Qgis`.LayerType to a string (except for "custom", "project" and\n"directory")\n\nMime data from plugins may use additional custom layer types.', 'providerKey': 'For "vector" / "raster" type: provider id.\nFor "plugin" type: plugin layer type name.\nFor "custom" type: key of its :py:class:`QgsCustomDropHandler`\nFor "project" and "directory" types: unused', 'name': 'Human readable name to be used e.g. in layer tree', 'uri': 'Identifier of the data source recognized by its providerKey', 'layerId': 'Layer ID, if uri is associated with a layer from a :py:class:`QgsProject`.\n\n.. versionadded:: 3.8', 'pId': 'Unique ID associated with application instance. Can be used to identify\nif mime data was created inside the current application instance or not.\n\n.. versionadded:: 3.8', 'wkbType': 'WKB type, if associated with a vector layer, or :py:class:`QgsWkbTypes`.Unknown if not\nyet known.\n\n.. versionadded:: 3.8', 'filePath': 'Path to file, if uri is associated with a file.\n\n.. versionadded:: 3.22'}
except (NameError, AttributeError):
pass
try:
Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_additions/qgsmimedatautils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The following has been generated automatically from src/core/qgsmimedatautils.h
try:
QgsMimeDataUtils.Uri.__attribute_docs__ = {'layerType': 'Type of URI.\n\nRecognized types include\n\n- "vector": vector layers\n- "raster": raster layers\n- "mesh": mesh layers\n- "pointcloud": point cloud layers\n- "vector-tile": vector tile layers\n- "tiled-scene": tiled scene layers\n- "plugin": plugin layers\n- "custom": custom types\n- "project": QGS/QGZ project file\n- "directory": directory path\n\nMime data from plugins may use additional custom layer types.', 'providerKey': 'For "vector" / "raster" type: provider id.\nFor "plugin" type: plugin layer type name.\nFor "custom" type: key of its :py:class:`QgsCustomDropHandler`\nFor "project" and "directory" types: unused', 'name': 'Human readable name to be used e.g. in layer tree', 'uri': 'Identifier of the data source recognized by its providerKey', 'layerId': 'Layer ID, if uri is associated with a layer from a :py:class:`QgsProject`.\n\n.. versionadded:: 3.8', 'pId': 'Unique ID associated with application instance. Can be used to identify\nif mime data was created inside the current application instance or not.\n\n.. versionadded:: 3.8', 'wkbType': 'WKB type, if associated with a vector layer, or :py:class:`QgsWkbTypes`.Unknown if not\nyet known.\n\n.. versionadded:: 3.8', 'filePath': 'Path to file, if uri is associated with a file.\n\n.. versionadded:: 3.22'}
QgsMimeDataUtils.Uri.__attribute_docs__ = {'layerType': 'Type of URI.\n\nRecognized types include\n\n- "vector": vector layers\n- "raster": raster layers\n- "mesh": mesh layers\n- "point-cloud": point cloud layers (spelled with a dash since QGIS 3.42.0. In prior versions, there was no dash)\n- "vector-tile": vector tile layers\n- "tiled-scene": tiled scene layers\n- "annotation": annotation layers\n- "group": group layers\n- "plugin": plugin layers\n- "custom": custom types\n- "project": QGS/QGZ project file\n- "directory": directory path\n\nNote: use :py:func:`QgsMapLayerFactory.typeToString()` to convert from a\n:py:class:`Qgis`.LayerType to a string (except for "custom", "project" and\n"directory")\n\nMime data from plugins may use additional custom layer types.', 'providerKey': 'For "vector" / "raster" type: provider id.\nFor "plugin" type: plugin layer type name.\nFor "custom" type: key of its :py:class:`QgsCustomDropHandler`\nFor "project" and "directory" types: unused', 'name': 'Human readable name to be used e.g. in layer tree', 'uri': 'Identifier of the data source recognized by its providerKey', 'layerId': 'Layer ID, if uri is associated with a layer from a :py:class:`QgsProject`.\n\n.. versionadded:: 3.8', 'pId': 'Unique ID associated with application instance. Can be used to identify\nif mime data was created inside the current application instance or not.\n\n.. versionadded:: 3.8', 'wkbType': 'WKB type, if associated with a vector layer, or :py:class:`QgsWkbTypes`.Unknown if not\nyet known.\n\n.. versionadded:: 3.8', 'filePath': 'Path to file, if uri is associated with a file.\n\n.. versionadded:: 3.22'}
except (NameError, AttributeError):
pass
try:
Expand Down
17 changes: 10 additions & 7 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#include "qgsauxiliarystorage.h"
#include "qgsvectortileutils.h"
#include "qgsscaleutils.h"
#include "qgsmaplayerfactory.h"

#include "qgsbrowserwidget.h"
#include "annotations/qgsannotationitempropertieswidget.h"
Expand Down Expand Up @@ -2447,6 +2448,8 @@ QList<QgsMapLayer *> QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList
const QgsMimeDataUtils::Uri &u = lst.at( i );

QString uri = crsAndFormatAdjustedLayerUri( u.uri, u.supportedCrs, u.supportedFormats );
bool ok = false;
Qgis::LayerType layerType = QgsMapLayerFactory::typeFromString( u.layerType, ok );

if ( u.layerType == QLatin1String( "collection" ) )
{
Expand All @@ -2455,41 +2458,41 @@ QList<QgsMapLayer *> QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList
if ( ok )
addedLayers.append( collectionLayers );
}
else if ( u.layerType == QLatin1String( "vector" ) )
else if ( ok && layerType == Qgis::LayerType::Vector )
{
const QList<QgsVectorLayer *> layerList { QgsAppLayerHandling::addVectorLayer( uri, u.name, u.providerKey, addToLegend ) };
for ( QgsVectorLayer *layer : std::as_const( layerList ) )
{
addedLayers << layer;
}
}
else if ( u.layerType == QLatin1String( "raster" ) )
else if ( ok && layerType == Qgis::LayerType::Raster )
{
const QList<QgsRasterLayer *> layerList { QgsAppLayerHandling::addRasterLayer( uri, u.name, u.providerKey, addToLegend ) };
for ( QgsRasterLayer *layer : std::as_const( layerList ) )
{
addedLayers << layer;
}
}
else if ( u.layerType == QLatin1String( "mesh" ) )
else if ( ok && layerType == Qgis::LayerType::Mesh )
{
const QList<QgsMeshLayer *> layerList { QgsAppLayerHandling::addMeshLayer( uri, u.name, u.providerKey, addToLegend ) };
for ( QgsMeshLayer *layer : std::as_const( layerList ) )
{
addedLayers << layer;
}
}
else if ( u.layerType == QLatin1String( "pointcloud" ) )
else if ( ok && layerType == Qgis::LayerType::PointCloud )
{
if ( QgsMapLayer *layer = QgsAppLayerHandling::addLayer<QgsPointCloudLayer>( uri, u.name, u.providerKey, addToLegend ) )
addedLayers << layer;
}
else if ( u.layerType == QLatin1String( "tiled-scene" ) )
else if ( ok && layerType == Qgis::LayerType::TiledScene )
{
if ( QgsMapLayer *layer = QgsAppLayerHandling::addLayer<QgsTiledSceneLayer>( uri, u.name, u.providerKey, addToLegend ) )
addedLayers << layer;
}
else if ( u.layerType == QLatin1String( "vector-tile" ) )
else if ( ok && layerType == Qgis::LayerType::VectorTile )
{
QgsTemporaryCursorOverride busyCursor( Qt::WaitCursor );

Expand Down Expand Up @@ -2587,7 +2590,7 @@ QList<QgsMapLayer *> QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList
addedLayers << layer;
}
}
else if ( u.layerType == QLatin1String( "plugin" ) )
else if ( ok && layerType == Qgis::LayerType::Plugin )
{
QgsMapLayer *layer = QgsAppLayerHandling::addLayer<QgsPluginLayer>( uri, u.name, u.providerKey, addToLegend, false );
if ( layer )
Expand Down
18 changes: 2 additions & 16 deletions src/core/browser/qgslayeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgslayeritem.h"
#include "moc_qgslayeritem.cpp"
#include "qgsmaplayer.h"
#include "qgsmaplayerfactory.h"
#include "qgsvectorlayer.h"
#include "qgsiconutils.h"

Expand Down Expand Up @@ -243,11 +244,11 @@ bool QgsLayerItem::equal( const QgsDataItem *other )
QgsMimeDataUtils::UriList QgsLayerItem::mimeUris() const
{
QgsMimeDataUtils::Uri u;
u.layerType = QgsMapLayerFactory::typeToString( mapLayerType() );

switch ( mapLayerType() )
{
case Qgis::LayerType::Vector:
u.layerType = QStringLiteral( "vector" );
switch ( mLayerType )
{
case Qgis::BrowserLayerType::Point:
Expand Down Expand Up @@ -277,28 +278,13 @@ QgsMimeDataUtils::UriList QgsLayerItem::mimeUris() const
}
break;
case Qgis::LayerType::Raster:
u.layerType = QStringLiteral( "raster" );
break;
case Qgis::LayerType::Mesh:
u.layerType = QStringLiteral( "mesh" );
break;
case Qgis::LayerType::VectorTile:
u.layerType = QStringLiteral( "vector-tile" );
break;
case Qgis::LayerType::PointCloud:
u.layerType = QStringLiteral( "pointcloud" );
break;
case Qgis::LayerType::TiledScene:
u.layerType = QStringLiteral( "tiled-scene" );
break;
case Qgis::LayerType::Plugin:
u.layerType = QStringLiteral( "plugin" );
break;
case Qgis::LayerType::Group:
u.layerType = QStringLiteral( "group" );
break;
case Qgis::LayerType::Annotation:
u.layerType = QStringLiteral( "annotation" );
break;
}

Expand Down
17 changes: 1 addition & 16 deletions src/core/providers/qgsprovidersublayerdetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,20 @@ QgsMapLayer *QgsProviderSublayerDetails::toLayer( const LayerOptions &options )
QgsMimeDataUtils::Uri QgsProviderSublayerDetails::toMimeUri() const
{
QgsMimeDataUtils::Uri u;
u.layerType = QgsMapLayerFactory::typeToString( mType );
switch ( mType )
{
case Qgis::LayerType::Vector:
u.layerType = QStringLiteral( "vector" );
u.wkbType = mWkbType;
break;
case Qgis::LayerType::Raster:
u.layerType = QStringLiteral( "raster" );
break;
case Qgis::LayerType::Mesh:
u.layerType = QStringLiteral( "mesh" );
break;
case Qgis::LayerType::VectorTile:
u.layerType = QStringLiteral( "vector-tile" );
break;
case Qgis::LayerType::PointCloud:
u.layerType = QStringLiteral( "pointcloud" );
break;
case Qgis::LayerType::Plugin:
u.layerType = QStringLiteral( "plugin" );
break;
case Qgis::LayerType::Group:
u.layerType = QStringLiteral( "group" );
break;
case Qgis::LayerType::Annotation:
u.layerType = QStringLiteral( "annotation" );
break;
case Qgis::LayerType::TiledScene:
u.layerType = QStringLiteral( "tiled-scene" );
break;
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsmaplayerfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ Qgis::LayerType QgsMapLayerFactory::typeFromString( const QString &string, bool
return Qgis::LayerType::Mesh;
else if ( string.compare( QLatin1String( "vector-tile" ), Qt::CaseInsensitive ) == 0 )
return Qgis::LayerType::VectorTile;
else if ( string.compare( QLatin1String( "point-cloud" ), Qt::CaseInsensitive ) == 0 )
else if ( string.compare( QLatin1String( "point-cloud" ), Qt::CaseInsensitive ) == 0 ||
// We accept "pointcloud" for backward compatibility with the
// MIME related code, which spelled it that way before 3.42.0 where
// we have delegated to QgsMapLayerFactory::typeToString()
string.compare( QLatin1String( "pointcloud" ), Qt::CaseInsensitive ) == 0 )
return Qgis::LayerType::PointCloud;
else if ( string.compare( QLatin1String( "plugin" ), Qt::CaseInsensitive ) == 0 )
return Qgis::LayerType::Plugin;
Expand Down
21 changes: 2 additions & 19 deletions src/core/qgsmimedatautils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgslogger.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayerfactory.h"
#include "qgsmeshlayer.h"

#include <QRegularExpression>
Expand Down Expand Up @@ -71,38 +72,20 @@ QgsMimeDataUtils::Uri::Uri( QgsMapLayer *layer )
, layerId( layer->id() )
, pId( QString::number( QCoreApplication::applicationPid() ) )
{
layerType = QgsMapLayerFactory::typeToString( layer->type() );
switch ( layer->type() )
{
case Qgis::LayerType::Vector:
{
layerType = QStringLiteral( "vector" );
wkbType = qobject_cast< QgsVectorLayer *>( layer )->wkbType();
break;
}
case Qgis::LayerType::Raster:
{
layerType = QStringLiteral( "raster" );
break;
}

case Qgis::LayerType::Mesh:
{
layerType = QStringLiteral( "mesh" );
break;
}
case Qgis::LayerType::PointCloud:
{
layerType = QStringLiteral( "pointcloud" );
break;
}
case Qgis::LayerType::VectorTile:
{
layerType = QStringLiteral( "vector-tile" );
break;
}
case Qgis::LayerType::TiledScene:
{
layerType = QStringLiteral( "tiled-scene" );
break;
}

Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsmimedatautils.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,20 @@ class CORE_EXPORT QgsMimeDataUtils
* - "vector": vector layers
* - "raster": raster layers
* - "mesh": mesh layers
* - "pointcloud": point cloud layers
* - "point-cloud": point cloud layers (spelled with a dash since QGIS 3.42.0. In prior versions, there was no dash)
* - "vector-tile": vector tile layers
* - "tiled-scene": tiled scene layers
* - "annotation": annotation layers
* - "group": group layers
* - "plugin": plugin layers
* - "custom": custom types
* - "project": QGS/QGZ project file
* - "directory": directory path
*
* Note: use QgsMapLayerFactory::typeToString() to convert from a
* Qgis::LayerType to a string (except for "custom", "project" and
* "directory")
*
* Mime data from plugins may use additional custom layer types.
*/
QString layerType;
Expand Down
11 changes: 6 additions & 5 deletions src/gui/processing/qgsprocessingmultipleselectiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgssettings.h"
#include "qgsfileutils.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayerfactory.h"
#include "qgsmeshlayer.h"
#include "qgsrasterlayer.h"
#include "qgspluginlayer.h"
Expand Down Expand Up @@ -460,7 +461,7 @@ QStringList QgsProcessingMultipleInputPanelWidget::compatibleUrisFromMimeData( c
|| parameter->layerType() == Qgis::ProcessingSourceType::VectorLine
|| parameter->layerType() == Qgis::ProcessingSourceType::VectorPoint
|| parameter->layerType() == Qgis::ProcessingSourceType::VectorPolygon )
&& u.layerType == QLatin1String( "vector" ) )
&& u.layerType == QgsMapLayerFactory::typeToString( Qgis::LayerType::Vector ) )
{
bool acceptable = false;
switch ( QgsWkbTypes::geometryType( u.wkbType ) )
Expand Down Expand Up @@ -493,16 +494,16 @@ QStringList QgsProcessingMultipleInputPanelWidget::compatibleUrisFromMimeData( c
res.append( u.providerKey != QLatin1String( "ogr" ) ? QgsProcessingUtils::encodeProviderKeyAndUri( u.providerKey, u.uri ) : u.uri );
}
else if ( ( parameter->layerType() == Qgis::ProcessingSourceType::MapLayer || parameter->layerType() == Qgis::ProcessingSourceType::Raster )
&& u.layerType == QLatin1String( "raster" ) && u.providerKey == QLatin1String( "gdal" ) )
&& u.layerType == QgsMapLayerFactory::typeToString( Qgis::LayerType::Raster ) && u.providerKey == QLatin1String( "gdal" ) )
res.append( u.uri );
else if ( ( parameter->layerType() == Qgis::ProcessingSourceType::MapLayer || parameter->layerType() == Qgis::ProcessingSourceType::Mesh )
&& u.layerType == QLatin1String( "mesh" ) && u.providerKey == QLatin1String( "mdal" ) )
&& u.layerType == QgsMapLayerFactory::typeToString( Qgis::LayerType::Mesh ) && u.providerKey == QLatin1String( "mdal" ) )
res.append( u.uri );
else if ( ( parameter->layerType() == Qgis::ProcessingSourceType::MapLayer || parameter->layerType() == Qgis::ProcessingSourceType::PointCloud )
&& u.layerType == QLatin1String( "pointcloud" ) )
&& u.layerType == QgsMapLayerFactory::typeToString( Qgis::LayerType::PointCloud ) )
res.append( u.uri );
else if ( ( parameter->layerType() == Qgis::ProcessingSourceType::MapLayer || parameter->layerType() == Qgis::ProcessingSourceType::VectorTile )
&& u.layerType == QLatin1String( "vector-tile" ) )
&& u.layerType == QgsMapLayerFactory::typeToString( Qgis::LayerType::VectorTile ) )
res.append( u.uri );
// NOLINTEND(bugprone-branch-clone)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsprovidersublayerdetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_to_mime(self):

details.setType(QgsMapLayerType.PointCloudLayer)
uri = details.toMimeUri()
self.assertEqual(uri.layerType, "pointcloud")
self.assertEqual(uri.layerType, "point-cloud")

details.setType(QgsMapLayerType.PluginLayer)
uri = details.toMimeUri()
Expand Down
Loading