Skip to content
Permalink
Browse files

Make constructors for QgsVectorLayer and QgsRasterLayer more flexible

...by moving extra arguments to new LayerOptions structs. This allows
us to more easily add new layer constructor options without making
the API cumbersome to use.
  • Loading branch information
nyalldawson committed Nov 3, 2017
1 parent 61b2f74 commit ba62ffce2c7fa6f6331e33f751e4a901a26e8e45
@@ -2080,9 +2080,9 @@ QgsRasterInterface {#qgis_api_break_3_0_QgsRasterInterface}
QgsRasterLayer {#qgis_api_break_3_0_QgsRasterLayer}
--------------

- The constructor variant with loadDefaultStyleFlag as the 3rd parameter was removed. Use the
constructor variant which accepts a data provider string and loadDefaultStyleFlag as the
4th parameter instead.
- The QgsRasterLayer constructor now takes a QgsRasterLayer.LayerOptions argument instead of the old
loadDefaultStyle argument.
- The constructor variant with loadDefaultStyleFlag as the 3rd parameter was removed.
- setDrawingStyle() was removed. Use setRendererForDrawingStyle() or setRenderer() instead.
- previewAsPixmap() was removed. Use previewAsImage() instead.
- updateProgress() had no effect and was removed.
@@ -2551,6 +2551,8 @@ QgsVectorJoinInfo {#qgis_api_break_3_0_QgsVectorJoinInfo}
QgsVectorLayer {#qgis_api_break_3_0_QgsVectorLayer}
--------------

- The QgsVectorLayer constructor now takes a QgsVectorLayer.LayerOptions argument instead of the old
loadDefaultStyle argument.
- excludeAttributesWMS() and setExcludeAttributesWMS() have been renamed to excludeAttributesWms() and
setExcludeAttributesWms()
- excludeAttributesWFS() and setExcludeAttributesWFS() have been renamed to excludeAttributesWfs() and
@@ -314,22 +314,41 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
RemoveFromSelection,
};

QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
bool readExtentFromXml = false );
struct LayerOptions
{

explicit LayerOptions( bool loadDefaultStyle = true, bool readExtentFromXml = false );
%Docstring
Constructor for LayerOptions.
%End

bool loadDefaultStyle;
%Docstring
Set to true if the default layer style should be loaded
%End

bool readExtentFromXml;
%Docstring
If true, the layer extent will be read from XML (i.e. stored in the
project file). If false, the extent will be determined by the provider on layer load.
%End

};

explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );
%Docstring
Constructor - creates a vector layer

The QgsVectorLayer is constructed by instantiating a data provider. The provider
interprets the supplied path (url) of the data source to connect to and access the
data.

\param path The path or url of the parameter. Typically this encodes
\param path The path or url of the parameter. Typically this encodes
parameters used by the data provider as url query items.
\param baseName The name used to represent the layer in the legend
\param providerLib The name of the data provider, e.g., "memory", "postgres"
\param loadDefaultStyleFlag whether to load the default style
\param readExtentFromXml Read extent from XML if true or let provider determine it if false
\param baseName The name used to represent the layer in the legend
\param providerLib The name of the data provider, e.g., "memory", "postgres"
\param options layer load options
%End


@@ -146,10 +146,24 @@ class QgsRasterLayer : QgsMapLayer
Constructor. Provider is not set.
%End

QgsRasterLayer( const QString &uri,
const QString &baseName = QString(),
const QString &providerKey = "gdal",
bool loadDefaultStyleFlag = true );
struct LayerOptions
{

explicit LayerOptions( bool loadDefaultStyle = true );
%Docstring
Constructor for LayerOptions.
%End

bool loadDefaultStyle;
%Docstring
Set to true if the default layer style should be loaded
%End
};

explicit QgsRasterLayer( const QString &uri,
const QString &baseName = QString(),
const QString &providerKey = "gdal",
const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );
%Docstring
This is the constructor for the RasterLayer class.

@@ -239,10 +239,14 @@ def load_layer(self, id, param):
if filepath in self.vector_layer_params:
return self.vector_layer_params[filepath]

lyr = QgsVectorLayer(filepath, param['name'], 'ogr', False)
options = QgsVectorLayer.LayerOptions()
options.loadDefaultStyle = False
lyr = QgsVectorLayer(filepath, param['name'], 'ogr', options)
self.vector_layer_params[filepath] = lyr
elif param['type'] == 'raster':
lyr = QgsRasterLayer(filepath, param['name'], 'gdal', False)
options = QgsRasterLayer.LayerOptions()
options.loadDefaultStyle = False
lyr = QgsRasterLayer(filepath, param['name'], 'gdal', options)

self.assertTrue(lyr.isValid(), 'Could not load layer "{}" from param {}'.format(filepath, param))
QgsProject.instance().addMapLayer(lyr)
@@ -167,7 +167,9 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()

bool lblVisible = false;

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

lblMessage->setVisible( lblVisible );

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

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

if ( l->featureCount() == 0 )
@@ -4162,7 +4162,9 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin

// create the layer

QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), false );
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), options );
Q_CHECK_PTR( layer );

if ( ! layer )
@@ -4636,7 +4638,9 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
QString name = fileName + " " + def.layerName;
if ( !layerGeometryType.isEmpty() )
name += " " + layerGeometryType;
QgsVectorLayer *layer = new QgsVectorLayer( composedURI, name, QStringLiteral( "ogr" ), false );
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( composedURI, name, QStringLiteral( "ogr" ), options );
if ( layer && layer->isValid() )
{
myList << layer;
@@ -4714,7 +4718,9 @@ void QgisApp::addDatabaseLayers( QStringList const &layerPathList, QString const
// create the layer
QgsDataSourceUri uri( layerPath );

QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, false );
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, options );
Q_CHECK_PTR( layer );

if ( ! layer )
@@ -9968,7 +9974,9 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
}

// create the layer
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, false );
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, options );

if ( authok && layer && layer->isValid() )
{
@@ -174,12 +174,16 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string )
QString name = fi.baseName();

// brute force attempt to load a matching layer
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), false ) );
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), options ) );
if ( layer->isValid() )
{
return layer.release();
}
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), false ) );
QgsRasterLayer::LayerOptions rasterOptions;
rasterOptions.loadDefaultStyle = false;
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), rasterOptions ) );
if ( rasterLayer->isValid() )
{
return rasterLayer.release();
@@ -340,7 +340,9 @@ void QgsProjectFileTransform::transform0110to1000()
QString providerKey = providerNode.toElement().text();

//create the layer to get the provider for int->fieldName conversion
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, false );
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, options );
if ( !layer->isValid() )
{
delete layer;
@@ -136,13 +136,12 @@ typedef bool deleteStyleById_t(
QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
const QString &baseName,
const QString &providerKey,
bool loadDefaultStyleFlag,
bool readExtentFromXml )
const LayerOptions &options )
: QgsMapLayer( VectorLayer, baseName, vectorLayerPath )
, mProviderKey( providerKey )
, mAuxiliaryLayer( nullptr )
, mAuxiliaryLayerKey( QString() )
, mReadExtentFromXml( readExtentFromXml )
, mReadExtentFromXml( options.readExtentFromXml )
{
mActions = new QgsActionManager( this );
mConditionalStyles = new QgsConditionalLayerStyles();
@@ -153,7 +152,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
// if we're given a provider type, try to create and bind one to this layer
if ( !vectorLayerPath.isEmpty() && !mProviderKey.isEmpty() )
{
setDataSource( vectorLayerPath, baseName, providerKey, loadDefaultStyleFlag );
setDataSource( vectorLayerPath, baseName, providerKey, options.loadDefaultStyle );
}

connect( this, &QgsVectorLayer::selectionChanged, this, [ = ] { emit repaintRequested(); } );
@@ -381,24 +381,47 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
RemoveFromSelection, //!< Remove from current selection
};

/**
* Setting options for loading vector layers.
* \since QGIS 3.0
*/
struct LayerOptions
{

/**
* Constructor for LayerOptions.
*/
explicit LayerOptions( bool loadDefaultStyle = true, bool readExtentFromXml = false )
: loadDefaultStyle( loadDefaultStyle )
, readExtentFromXml( readExtentFromXml )
{}

//! Set to true if the default layer style should be loaded
bool loadDefaultStyle = true;

/**
* If true, the layer extent will be read from XML (i.e. stored in the
* project file). If false, the extent will be determined by the provider on layer load.
*/
bool readExtentFromXml = false;

};

/**
* Constructor - creates a vector layer
*
* The QgsVectorLayer is constructed by instantiating a data provider. The provider
* interprets the supplied path (url) of the data source to connect to and access the
* data.
*
* \param path The path or url of the parameter. Typically this encodes
* \param path The path or url of the parameter. Typically this encodes
* parameters used by the data provider as url query items.
* \param baseName The name used to represent the layer in the legend
* \param providerLib The name of the data provider, e.g., "memory", "postgres"
* \param loadDefaultStyleFlag whether to load the default style
* \param readExtentFromXml Read extent from XML if true or let provider determine it if false
*
* \param baseName The name used to represent the layer in the legend
* \param providerLib The name of the data provider, e.g., "memory", "postgres"
* \param options layer load options
*/
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
bool readExtentFromXml = false );
explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );


virtual ~QgsVectorLayer();
@@ -108,7 +108,7 @@ QgsRasterLayer::QgsRasterLayer()
QgsRasterLayer::QgsRasterLayer( const QString &uri,
const QString &baseName,
const QString &providerKey,
bool loadDefaultStyleFlag )
const LayerOptions &options )
: QgsMapLayer( RasterLayer, baseName, uri )
// Constant that signals property not used.
, QSTRING_NOT_SET( QStringLiteral( "Not Set" ) )
@@ -122,7 +122,7 @@ QgsRasterLayer::QgsRasterLayer( const QString &uri,

// load default style
bool defaultLoadedFlag = false;
if ( mValid && loadDefaultStyleFlag )
if ( mValid && options.loadDefaultStyle )
{
loadDefaultStyle( defaultLoadedFlag );
}
@@ -166,6 +166,24 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
//! \brief Constructor. Provider is not set.
QgsRasterLayer();

/**
* Setting options for loading raster layers.
* \since QGIS 3.0
*/
struct LayerOptions
{

/**
* Constructor for LayerOptions.
*/
explicit LayerOptions( bool loadDefaultStyle = true )
: loadDefaultStyle( loadDefaultStyle )
{}

//! Set to true if the default layer style should be loaded
bool loadDefaultStyle = true;
};

/**
* \brief This is the constructor for the RasterLayer class.
*
@@ -183,10 +201,10 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
*
* -
* */
QgsRasterLayer( const QString &uri,
const QString &baseName = QString(),
const QString &providerKey = "gdal",
bool loadDefaultStyleFlag = true );
explicit QgsRasterLayer( const QString &uri,
const QString &baseName = QString(),
const QString &providerKey = "gdal",
const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );

~QgsRasterLayer();

@@ -229,7 +229,9 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
}
}
// Raster layers
QgsRasterLayer rlayer( path, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), false );
QgsRasterLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsRasterLayer rlayer( path, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), options );
if ( !rlayer.dataProvider()->subLayers( ).empty() )
{
const QStringList layers( rlayer.dataProvider()->subLayers( ) );

0 comments on commit ba62ffc

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