Add convenience method to directly convert QgsProviderSublayerDetails

object to a QgsMapLayer
This commit is contained in:
Nyall Dawson 2021-06-22 12:12:38 +10:00
parent 00db2820cd
commit 69debff405
5 changed files with 89 additions and 1 deletions

View File

@ -73,6 +73,24 @@ Returns the layer's URI.
Sets the layer's ``uri``.
.. seealso:: :py:func:`uri`
%End
struct LayerOptions
{
explicit LayerOptions( const QgsCoordinateTransformContext &transformContext );
%Docstring
Constructor for LayerOptions with ``transformContext``.
%End
QgsCoordinateTransformContext transformContext;
};
QgsMapLayer *toLayer( const LayerOptions &options ) const /Factory/;
%Docstring
Creates a new :py:class:`QgsMapLayer` object associated with the sublayer.
Caller takes ownership of the returned layer.
%End
QString name() const;

View File

@ -215,6 +215,7 @@ set(QGIS_CORE_SRCS
providers/qgsdataprovider.cpp
providers/qgsprovidermetadata.cpp
providers/qgsproviderregistry.cpp
providers/qgsprovidersublayerdetails.cpp
providers/arcgis/qgsarcgisportalutils.cpp
providers/arcgis/qgsarcgisrestquery.cpp

View File

@ -0,0 +1,27 @@
/***************************************************************************
qgsprovidersublayerdetails.cpp
----------------------------
begin : May 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 "qgsprovidersublayerdetails.h"
#include "qgsmaplayerfactory.h"
QgsProviderSublayerDetails::LayerOptions::LayerOptions( const QgsCoordinateTransformContext &transformContext )
: transformContext( transformContext )
{}
QgsMapLayer *QgsProviderSublayerDetails::toLayer( const LayerOptions &options ) const
{
return QgsMapLayerFactory::createLayer( mUri, mName, mType, mProviderKey, options.transformContext );
}

View File

@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgswkbtypes.h"
#include "qgscoordinatetransformcontext.h"
#include <QString>
#include <QStringList>
@ -86,6 +87,27 @@ class CORE_EXPORT QgsProviderSublayerDetails
*/
void setUri( const QString &uri ) { mUri = uri; }
/**
* Setting options for loading layers.
*/
struct LayerOptions
{
/**
* Constructor for LayerOptions with \a transformContext.
*/
explicit LayerOptions( const QgsCoordinateTransformContext &transformContext );
QgsCoordinateTransformContext transformContext;
};
/**
* Creates a new QgsMapLayer object associated with the sublayer.
*
* Caller takes ownership of the returned layer.
*/
QgsMapLayer *toLayer( const LayerOptions &options ) const SIP_FACTORY;
/**
* Returns the layer's name.
*

View File

@ -11,15 +11,19 @@ __date__ = '16/03/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'
import qgis # NOQA
import os
from qgis.core import (
QgsProviderRegistry,
QgsMapLayerType,
QgsWkbTypes,
QgsProviderSublayerDetails,
Qgis
Qgis,
QgsCoordinateTransformContext,
QgsVectorLayer
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
# Convenience instances in case you may need them
# to find the srs.db
@ -65,6 +69,22 @@ class TestQgsProviderSublayerDetails(unittest.TestCase):
d.setLayerNumber(13)
self.assertEqual(d.layerNumber(), 13)
def test_to_layer(self):
"""
Test converting sub layer details to a layer
"""
details = QgsProviderSublayerDetails()
details.setUri(os.path.join(unitTestDataPath(), 'lines.shp'))
details.setName('my sub layer')
details.setType(QgsMapLayerType.VectorLayer)
details.setProviderKey('ogr')
options = QgsProviderSublayerDetails.LayerOptions(QgsCoordinateTransformContext())
ml = details.toLayer(options)
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsVectorLayer)
self.assertEqual(ml.name(), 'my sub layer')
if __name__ == '__main__':
unittest.main()