Skip to content
Permalink
Browse files

Add mandatory options to QgsVectorLayer ctor

... and deprecate the rest.

In QGIS 4 we'll have to get rid of the deprecated default ctor and
create a new one which takes no arguments.
  • Loading branch information
elpaso authored and nyalldawson committed Mar 27, 2019
1 parent 6813a55 commit e930bee9e25c57bda8ea17c363e1fd8f459e77dc
Showing with 134 additions and 70 deletions.
  1. +1 −0 python/core/auto_generated/qgsvectorlayer.sip.in
  2. +11 −2 python/core/auto_generated/raster/qgsrasterlayer.sip.in
  3. +2 −1 src/app/browser/qgsinbuiltdataitemproviders.cpp
  4. +5 −5 src/app/dwg/qgsdwgimportdialog.cpp
  5. +9 −7 src/app/qgisapp.cpp
  6. +3 −2 src/app/qgsappscreenshots.cpp
  7. +2 −1 src/app/qgsnewspatialitelayerdialog.cpp
  8. +6 −3 src/app/qgsstatusbarcoordinateswidget.cpp
  9. +1 −1 src/core/layout/qgslayoutitemmapoverview.cpp
  10. +7 −7 src/core/processing/qgsprocessingutils.cpp
  11. +15 −1 src/core/processing/qgsprocessingutils.h
  12. +1 −2 src/core/providers/memory/qgsmemoryproviderutils.cpp
  13. +3 −1 src/core/qgsauxiliarystorage.cpp
  14. +2 −1 src/core/qgslayerdefinition.cpp
  15. +2 −1 src/core/qgsmimedatautils.cpp
  16. +6 −4 src/core/qgsofflineediting.cpp
  17. +0 −1 src/core/qgsproject.cpp
  18. +2 −2 src/core/qgsprojectfiletransform.cpp
  19. +2 −2 src/core/qgsvectorlayer.cpp
  20. +3 −0 src/core/qgsvectorlayer.h
  21. +2 −1 src/core/qgsvirtuallayertask.cpp
  22. +2 −1 src/gui/qgsbrowserdockwidget_p.cpp
  23. +2 −1 src/gui/qgsnewgeopackagelayerdialog.cpp
  24. +2 −1 src/gui/qgsprojectionselectiontreewidget.cpp
  25. +2 −1 src/gui/qgsrasterlayersaveasdialog.cpp
  26. +4 −2 src/gui/symbology/qgsdatadefinedsizelegendwidget.cpp
  27. +3 −1 src/plugins/geometry_checker/qgsgeometrycheckerresulttab.cpp
  28. +2 −2 src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp
  29. +3 −1 src/providers/db2/qgsdb2dataitems.cpp
  30. +3 −1 src/providers/db2/qgsdb2sourceselect.cpp
  31. +3 −1 src/providers/mssql/qgsmssqldataitems.cpp
  32. +3 −2 src/providers/mssql/qgsmssqlsourceselect.cpp
  33. +2 −1 src/providers/ogr/qgsogrdataitems.cpp
  34. +3 −1 src/providers/ogr/qgsogrdbsourceselect.cpp
  35. +3 −1 src/providers/postgres/qgspgsourceselect.cpp
  36. +3 −1 src/providers/spatialite/qgsspatialitesourceselect.cpp
  37. +2 −1 src/providers/virtual/qgsvirtuallayersourceselect.cpp
  38. +2 −2 src/server/services/wms/qgswmsrenderer.cpp
  39. +3 −3 tests/src/core/testqgsvectordataprovider.cpp
  40. +2 −1 tests/src/core/testziplayer.cpp
@@ -352,6 +352,7 @@ Constructor for LayerOptions.

};


explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() ) /Deprecated/;
%Docstring
@@ -104,12 +104,21 @@ Constructor. Provider is not set.
struct LayerOptions
{

explicit LayerOptions( bool loadDefaultStyle = true,
const QgsCoordinateTransformContext &transformContext = QgsCoordinateTransformContext() );
explicit LayerOptions( bool loadDefaultStyle = true ) /Deprecated/;
%Docstring
Constructor for LayerOptions.

.. deprecated:: Use version with transformContext argument instead
%End

explicit LayerOptions( const QgsCoordinateTransformContext &transformContext, bool loadDefaultStyle = true );
%Docstring
Constructor for LayerOptions.

.. versionadded:: 3.10
%End


bool loadDefaultStyle;

QgsCoordinateTransformContext transformContext;
@@ -363,7 +363,8 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
{
case QgsMapLayerType::VectorLayer:
{
std::unique_ptr<QgsVectorLayer> layer( new QgsVectorLayer( layerItem->uri(), layerItem->name(), layerItem->providerKey() ) );
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
std::unique_ptr<QgsVectorLayer> layer( new QgsVectorLayer( options, layerItem->uri(), layerItem->name(), layerItem->providerKey() ) );
if ( layer && layer->isValid() )
{
QgisApp::instance()->saveAsFile( layer.get(), false, false );
@@ -148,9 +148,9 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()

bool lblVisible = false;

QgsVectorLayer::LayerOptions options;
QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
options.loadDefaultStyle = false;
std::unique_ptr<QgsVectorLayer> d( new QgsVectorLayer( QStringLiteral( "%1|layername=drawing" ).arg( mDatabaseFileWidget->filePath() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
std::unique_ptr<QgsVectorLayer> d( new QgsVectorLayer( options, QStringLiteral( "%1|layername=drawing" ).arg( mDatabaseFileWidget->filePath() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ) ) );
if ( d && d->isValid() )
{
int idxPath = d->fields().lookupField( QStringLiteral( "path" ) );
@@ -185,7 +185,7 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()

lblMessage->setVisible( lblVisible );

std::unique_ptr<QgsVectorLayer> l( new QgsVectorLayer( QStringLiteral( "%1|layername=layers" ).arg( mDatabaseFileWidget->filePath() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
std::unique_ptr<QgsVectorLayer> l( new QgsVectorLayer( options, QStringLiteral( "%1|layername=layers" ).arg( mDatabaseFileWidget->filePath() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ) ) );
if ( l && l->isValid() )
{
int idxName = l->fields().lookupField( QStringLiteral( "name" ) );
@@ -263,9 +263,9 @@ void QgsDwgImportDialog::pbImportDrawing_clicked()

QgsVectorLayer *QgsDwgImportDialog::layer( QgsLayerTreeGroup *layerGroup, const QString &layerFilter, const QString &table )
{
QgsVectorLayer::LayerOptions options;
QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
options.loadDefaultStyle = false;
QgsVectorLayer *l = new QgsVectorLayer( QStringLiteral( "%1|layername=%2" ).arg( mDatabaseFileWidget->filePath(), table ), table, QStringLiteral( "ogr" ), options );
QgsVectorLayer *l = new QgsVectorLayer( options, QStringLiteral( "%1|layername=%2" ).arg( mDatabaseFileWidget->filePath(), table ), table, QStringLiteral( "ogr" ) );
l->setSubsetString( QStringLiteral( "%1space=0 AND block=-1" ).arg( layerFilter ) );

if ( l->featureCount() == 0 )
@@ -4638,7 +4638,7 @@ static void setupVectorLayer( const QString &vectorLayerPath,
QString composedURI( uriParts.value( QStringLiteral( "path" ) ).toString() );
composedURI += "|layername=" + rawLayerName;

auto newLayer = qgis::make_unique<QgsVectorLayer>( composedURI, layer->name(), QStringLiteral( "ogr" ), options );
auto newLayer = qgis::make_unique<QgsVectorLayer>( options, composedURI, layer->name(), QStringLiteral( "ogr" ) );
if ( newLayer && newLayer->isValid() )
{
delete layer;
@@ -4716,7 +4716,7 @@ bool QgisApp::addVectorLayersPrivate( const QStringList &layerQStringList, const
QApplication::setOverrideCursor( Qt::WaitCursor );
qApp->processEvents();
}
QgsVectorLayer *layer = new QgsVectorLayer( src, baseName, QStringLiteral( "ogr" ), options );
QgsVectorLayer *layer = new QgsVectorLayer( options, src, baseName, QStringLiteral( "ogr" ) );
Q_CHECK_PTR( layer );
if ( isVsiCurl || isRemoteUrl )
{
@@ -5256,7 +5256,7 @@ QList<QgsMapLayer *> QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
QString name = fileName + " " + def.layerName;
QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( composedURI, name, QStringLiteral( "ogr" ), options );
QgsVectorLayer *layer = new QgsVectorLayer( options, composedURI, name, QStringLiteral( "ogr" ) );
if ( layer && layer->isValid() )
{
result << layer;
@@ -5320,7 +5320,7 @@ void QgisApp::addDatabaseLayers( QStringList const &layerPathList, QString const

QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, options );
QgsVectorLayer *layer = new QgsVectorLayer( options, uri.uri( false ), uri.table(), providerKey );
Q_CHECK_PTR( layer );

if ( ! layer )
@@ -5397,7 +5397,8 @@ void QgisApp::replaceSelectedVectorLayer( const QString &oldId, const QString &u
if ( !old )
return;
QgsVectorLayer *oldLayer = static_cast<QgsVectorLayer *>( old );
QgsVectorLayer *newLayer = new QgsVectorLayer( uri, layerName, provider );
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *newLayer = new QgsVectorLayer( options, uri, layerName, provider );
if ( !newLayer || !newLayer->isValid() )
return;

@@ -9853,7 +9854,8 @@ void QgisApp::layerSubsetString()
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
{
QgsVirtualLayerDefinition def = QgsVirtualLayerDefinitionUtils::fromJoinedLayer( vlayer );
QgsVectorLayer *newLayer = new QgsVectorLayer( def.toString(), vlayer->name() + " (virtual)", QStringLiteral( "virtual" ) );
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *newLayer = new QgsVectorLayer( options, def.toString(), vlayer->name() + " (virtual)", QStringLiteral( "virtual" ) );
if ( newLayer->isValid() )
{
duplicateVectorStyle( vlayer, newLayer );
@@ -11111,7 +11113,7 @@ QgsVectorLayer *QgisApp::addVectorLayerPrivate( const QString &vectorLayerPath,
QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
// Default style is loaded later in this method
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, options );
QgsVectorLayer *layer = new QgsVectorLayer( options, vectorLayerPath, baseName, providerKey );

if ( authok && layer && layer->isValid() )
{
@@ -42,9 +42,10 @@ QgsAppScreenShots::QgsAppScreenShots( const QString &saveDirectory )
: mSaveDirectory( saveDirectory )
{
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=fk_polygon:integer&field=my_double:double&key=pk" );
mLineLayer = new QgsVectorLayer( layerDef, QStringLiteral( "Line Layer" ), QStringLiteral( "memory" ) );
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
mLineLayer = new QgsVectorLayer( options, layerDef, QStringLiteral( "Line Layer" ), QStringLiteral( "memory" ) );
layerDef = QStringLiteral( "Polygon?crs=epsg:2056&field=pk:integer&field=my_text:string&field=my_integer:integer&field=height:double&key=pk" );
mPolygonLayer = new QgsVectorLayer( layerDef, QStringLiteral( "Polygon Layer" ), QStringLiteral( "memory" ) );
mPolygonLayer = new QgsVectorLayer( options, layerDef, QStringLiteral( "Polygon Layer" ), QStringLiteral( "memory" ) );

QString dataPath( TEST_DATA_DIR ); //defined in CmakeLists.txt
mRasterLayer = new QgsRasterLayer( dataPath + "/raster/with_color_table.tif", QStringLiteral( "raster" ), QStringLiteral( "gdal" ) );
@@ -454,7 +454,8 @@ bool QgsNewSpatialiteLayerDialog::apply()
}
}

QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "dbname='%1' table='%2'%3 sql=" )
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *layer = new QgsVectorLayer( options, QStringLiteral( "dbname='%1' table='%2'%3 sql=" )
.arg( mDatabaseComboBox->currentText(),
leLayerName->text(),
mGeometryTypeBox->currentIndex() != 0 ? QStringLiteral( "(%1)" ).arg( leGeometryColumn->text() ) : QString() ),
@@ -212,7 +212,8 @@ void QgsStatusBarCoordinatesWidget::contributors()
}
QString fileName = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/contributors.json" );
QFileInfo fileInfo = QFileInfo( fileName );
QgsVectorLayer *layer = new QgsVectorLayer( fileInfo.absoluteFilePath(),
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *layer = new QgsVectorLayer( options, fileInfo.absoluteFilePath(),
tr( "QGIS Contributors" ), QStringLiteral( "ogr" ) );
// Register this layer with the layers registry
QgsProject::instance()->addMapLayer( layer );
@@ -228,7 +229,8 @@ void QgsStatusBarCoordinatesWidget::world()
}
QString fileName = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.shp" );
QFileInfo fileInfo = QFileInfo( fileName );
QgsVectorLayer *layer = new QgsVectorLayer( fileInfo.absoluteFilePath(),
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *layer = new QgsVectorLayer( options, fileInfo.absoluteFilePath(),
tr( "World Map" ), QStringLiteral( "ogr" ) );
// Register this layer with the layers registry
QgsProject::instance()->addMapLayer( layer );
@@ -242,7 +244,8 @@ void QgsStatusBarCoordinatesWidget::hackfests()
}
QString fileName = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/qgis-hackfests.json" );
QFileInfo fileInfo = QFileInfo( fileName );
QgsVectorLayer *layer = new QgsVectorLayer( fileInfo.absoluteFilePath(),
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *layer = new QgsVectorLayer( options, fileInfo.absoluteFilePath(),
tr( "QGIS Hackfests" ), QStringLiteral( "ogr" ) );
// Register this layer with the layers registry
QgsProject::instance()->addMapLayer( layer );
@@ -33,7 +33,7 @@

QgsLayoutItemMapOverview::QgsLayoutItemMapOverview( const QString &name, QgsLayoutItemMap *map )
: QgsLayoutItemMapItem( name, map )
, mExtentLayer( qgis::make_unique< QgsVectorLayer >( QStringLiteral( "Polygon?crs=EPSG:4326" ), QStringLiteral( "overview" ), QStringLiteral( "memory" ) ) )
, mExtentLayer( qgis::make_unique< QgsVectorLayer >( QgsVectorLayer::LayerOptions( QgsCoordinateTransformContext() ), QStringLiteral( "Polygon?crs=EPSG:4326" ), QStringLiteral( "overview" ), QStringLiteral( "memory" ) ) )
{
createDefaultFrameSymbol();
}
@@ -214,7 +214,7 @@ class ProjectionSettingRestorer
};
///@endcond PRIVATE

QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string, LayerHint typeHint )
QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint )
{
QStringList components = string.split( '|' );
if ( components.isEmpty() )
@@ -237,9 +237,9 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string,
// brute force attempt to load a matching layer
if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Vector )
{
QgsVectorLayer::LayerOptions options;
QgsVectorLayer::LayerOptions options { transformContext };
options.loadDefaultStyle = false;
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), options ) );
std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique<QgsVectorLayer>( options, string, name, QStringLiteral( "ogr" ) );
if ( layer->isValid() )
{
return layer.release();
@@ -288,7 +288,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
if ( !allowLoadingNewLayers )
return nullptr;

layer = loadMapLayerFromString( string, typeHint );
layer = loadMapLayerFromString( string, context.transformContext(), typeHint );
if ( layer )
{
context.temporaryLayerStore()->addMapLayer( layer );
@@ -601,7 +601,8 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
else
{
//create empty layer
std::unique_ptr< QgsVectorLayerExporter > exporter( new QgsVectorLayerExporter( uri, providerKey, newFields, geometryType, crs, true, options, sinkFlags ) );
const QgsVectorLayer::LayerOptions layerOptions { context.transformContext() };
std::unique_ptr< QgsVectorLayerExporter > exporter = qgis::make_unique<QgsVectorLayerExporter>( uri, providerKey, newFields, geometryType, crs, true, options, sinkFlags );
if ( exporter->errorCode() )
{
throw QgsProcessingException( QObject::tr( "Could not create layer %1: %2" ).arg( destination, exporter->errorMessage() ) );
@@ -610,15 +611,14 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
// use destination string as layer name (eg "postgis:..." )
if ( !layerName.isEmpty() )
uri += QStringLiteral( "|layername=%1" ).arg( layerName );
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( uri, destination, providerKey ) );
std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique<QgsVectorLayer>( layerOptions, uri, destination, providerKey );
// update destination to layer ID
destination = layer->id();

context.temporaryLayerStore()->addMapLayer( layer.release() );
return new QgsProcessingFeatureSink( exporter.release(), destination, context, true );
}
}
return nullptr;
}

void QgsProcessingUtils::createFeatureSinkPython( QgsFeatureSink **sink, QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &options )
@@ -310,13 +310,27 @@ class CORE_EXPORT QgsProcessingUtils
*/
static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );

/**
* Interprets a string as a map layer. The method will attempt to
* load a layer matching the passed \a string using the given coordinate
* \a transformContext.
* E.g. if the string is a file path,
* then the layer at this file path will be loaded.
* The caller takes responsibility for deleting the returned map layer.
*
* \since QGIS 3.8
*/
static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = UnknownType );

/**
* Interprets a string as a map layer. The method will attempt to
* load a layer matching the passed \a string. E.g. if the string is a file path,
* then the layer at this file path will be loaded.
* The caller takes responsibility for deleting the returned map layer.
*
* \deprecated use mapLayerFromString() that takes QgsCoordinateTransformContext as an argument instead
*/
static QgsMapLayer *loadMapLayerFromString( const QString &string, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = UnknownType ) SIP_DEPRECATED ;

static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );

@@ -74,6 +74,5 @@ QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name,
}

QString uri = geomType + '?' + parts.join( '&' );

return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ) );
return new QgsVectorLayer( QgsVectorLayer::LayerOptions( QgsCoordinateTransformContext() ), uri, name, QStringLiteral( "memory" ) );
}
@@ -61,7 +61,9 @@ const QVector<QgsPalLayerSettings::Property> palHiddenProperties
//

QgsAuxiliaryLayer::QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer )
: QgsVectorLayer( QString( "%1|layername=%2" ).arg( filename, table ), QString( "%1_auxiliarystorage" ).arg( table ), "ogr" )
: QgsVectorLayer( QgsVectorLayer::LayerOptions( QgsCoordinateTransformContext( ) ),
QString( "%1|layername=%2" ).arg( filename, table ),
QString( "%1_auxiliarystorage" ).arg( table ), "ogr" )
, mFileName( filename )
, mTable( table )
, mLayer( vlayer )
@@ -265,7 +265,8 @@ QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayers( QDomDocument

if ( type == QLatin1String( "vector" ) )
{
layer = new QgsVectorLayer;
const QgsVectorLayer::LayerOptions options { QgsCoordinateTransformContext() };
layer = new QgsVectorLayer( options );
}
else if ( type == QLatin1String( "raster" ) )
{
@@ -94,7 +94,8 @@ QgsVectorLayer *QgsMimeDataUtils::Uri::vectorLayer( bool &owner, QString &error
return vectorLayer;
}
owner = true;
return new QgsVectorLayer( uri, name, providerKey );
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
return new QgsVectorLayer( options, uri, name, providerKey );
}

QgsRasterLayer *QgsMimeDataUtils::Uri::rasterLayer( bool &owner, QString &error ) const

0 comments on commit e930bee

Please sign in to comment.
You can’t perform that action at this time.