Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 Apr 17, 2019
1 parent 6813a55 commit e930bee
Show file tree
Hide file tree
Showing 40 changed files with 134 additions and 70 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions python/core/auto_generated/raster/qgsrasterlayer.sip.in
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -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 );
Expand Down
10 changes: 5 additions & 5 deletions src/app/dwg/qgsdwgimportdialog.cpp
Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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 )
Expand Down
16 changes: 9 additions & 7 deletions src/app/qgisapp.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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() )
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgsappscreenshots.cpp
Expand Up @@ -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" ) );
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsnewspatialitelayerdialog.cpp
Expand Up @@ -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() ),
Expand Down
9 changes: 6 additions & 3 deletions src/app/qgsstatusbarcoordinateswidget.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemmapoverview.cpp
Expand Up @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -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() )
Expand All @@ -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();
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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() ) );
Expand All @@ -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 )
Expand Down
16 changes: 15 additions & 1 deletion src/core/processing/qgsprocessingutils.h
Expand Up @@ -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 );

Expand Down
3 changes: 1 addition & 2 deletions src/core/providers/memory/qgsmemoryproviderutils.cpp
Expand Up @@ -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" ) );
}
4 changes: 3 additions & 1 deletion src/core/qgsauxiliarystorage.cpp
Expand Up @@ -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 )
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgslayerdefinition.cpp
Expand Up @@ -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" ) )
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmimedatautils.cpp
Expand Up @@ -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
Expand Down

0 comments on commit e930bee

Please sign in to comment.