Skip to content

Commit ba62ffc

Browse files
committedNov 9, 2017
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.
1 parent 61b2f74 commit ba62ffc

File tree

14 files changed

+217
-118
lines changed

14 files changed

+217
-118
lines changed
 

‎doc/api_break.dox

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,9 +2080,9 @@ QgsRasterInterface {#qgis_api_break_3_0_QgsRasterInterface}
20802080
QgsRasterLayer {#qgis_api_break_3_0_QgsRasterLayer}
20812081
--------------
20822082

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

2554+
- The QgsVectorLayer constructor now takes a QgsVectorLayer.LayerOptions argument instead of the old
2555+
loadDefaultStyle argument.
25542556
- excludeAttributesWMS() and setExcludeAttributesWMS() have been renamed to excludeAttributesWms() and
25552557
setExcludeAttributesWms()
25562558
- excludeAttributesWFS() and setExcludeAttributesWFS() have been renamed to excludeAttributesWfs() and

‎python/core/qgsvectorlayer.sip

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,41 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
314314
RemoveFromSelection,
315315
};
316316

317-
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
318-
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
319-
bool readExtentFromXml = false );
317+
struct LayerOptions
318+
{
319+
320+
explicit LayerOptions( bool loadDefaultStyle = true, bool readExtentFromXml = false );
321+
%Docstring
322+
Constructor for LayerOptions.
323+
%End
324+
325+
bool loadDefaultStyle;
326+
%Docstring
327+
Set to true if the default layer style should be loaded
328+
%End
329+
330+
bool readExtentFromXml;
331+
%Docstring
332+
If true, the layer extent will be read from XML (i.e. stored in the
333+
project file). If false, the extent will be determined by the provider on layer load.
334+
%End
335+
336+
};
337+
338+
explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
339+
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );
320340
%Docstring
321341
Constructor - creates a vector layer
322342

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

327-
\param path The path or url of the parameter. Typically this encodes
347+
\param path The path or url of the parameter. Typically this encodes
328348
parameters used by the data provider as url query items.
329-
\param baseName The name used to represent the layer in the legend
330-
\param providerLib The name of the data provider, e.g., "memory", "postgres"
331-
\param loadDefaultStyleFlag whether to load the default style
332-
\param readExtentFromXml Read extent from XML if true or let provider determine it if false
349+
\param baseName The name used to represent the layer in the legend
350+
\param providerLib The name of the data provider, e.g., "memory", "postgres"
351+
\param options layer load options
333352
%End
334353

335354

‎python/core/raster/qgsrasterlayer.sip

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,24 @@ class QgsRasterLayer : QgsMapLayer
146146
Constructor. Provider is not set.
147147
%End
148148

149-
QgsRasterLayer( const QString &uri,
150-
const QString &baseName = QString(),
151-
const QString &providerKey = "gdal",
152-
bool loadDefaultStyleFlag = true );
149+
struct LayerOptions
150+
{
151+
152+
explicit LayerOptions( bool loadDefaultStyle = true );
153+
%Docstring
154+
Constructor for LayerOptions.
155+
%End
156+
157+
bool loadDefaultStyle;
158+
%Docstring
159+
Set to true if the default layer style should be loaded
160+
%End
161+
};
162+
163+
explicit QgsRasterLayer( const QString &uri,
164+
const QString &baseName = QString(),
165+
const QString &providerKey = "gdal",
166+
const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );
153167
%Docstring
154168
This is the constructor for the RasterLayer class.
155169

‎python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,14 @@ def load_layer(self, id, param):
239239
if filepath in self.vector_layer_params:
240240
return self.vector_layer_params[filepath]
241241

242-
lyr = QgsVectorLayer(filepath, param['name'], 'ogr', False)
242+
options = QgsVectorLayer.LayerOptions()
243+
options.loadDefaultStyle = False
244+
lyr = QgsVectorLayer(filepath, param['name'], 'ogr', options)
243245
self.vector_layer_params[filepath] = lyr
244246
elif param['type'] == 'raster':
245-
lyr = QgsRasterLayer(filepath, param['name'], 'gdal', False)
247+
options = QgsRasterLayer.LayerOptions()
248+
options.loadDefaultStyle = False
249+
lyr = QgsRasterLayer(filepath, param['name'], 'gdal', options)
246250

247251
self.assertTrue(lyr.isValid(), 'Could not load layer "{}" from param {}'.format(filepath, param))
248252
QgsProject.instance().addMapLayer(lyr)

‎src/app/dwg/qgsdwgimportdialog.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
167167

168168
bool lblVisible = false;
169169

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

203205
lblMessage->setVisible( lblVisible );
204206

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

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

284288
if ( l->featureCount() == 0 )

‎src/app/qgisapp.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4162,7 +4162,9 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
41624162

41634163
// create the layer
41644164

4165-
QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), false );
4165+
QgsVectorLayer::LayerOptions options;
4166+
options.loadDefaultStyle = false;
4167+
QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), options );
41664168
Q_CHECK_PTR( layer );
41674169

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

4717-
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, false );
4721+
QgsVectorLayer::LayerOptions options;
4722+
options.loadDefaultStyle = false;
4723+
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, options );
47184724
Q_CHECK_PTR( layer );
47194725

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

99709976
// create the layer
9971-
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, false );
9977+
QgsVectorLayer::LayerOptions options;
9978+
options.loadDefaultStyle = false;
9979+
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, options );
99729980

99739981
if ( authok && layer && layer->isValid() )
99749982
{

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,16 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string )
174174
QString name = fi.baseName();
175175

176176
// brute force attempt to load a matching layer
177-
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), false ) );
177+
QgsVectorLayer::LayerOptions options;
178+
options.loadDefaultStyle = false;
179+
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), options ) );
178180
if ( layer->isValid() )
179181
{
180182
return layer.release();
181183
}
182-
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), false ) );
184+
QgsRasterLayer::LayerOptions rasterOptions;
185+
rasterOptions.loadDefaultStyle = false;
186+
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), rasterOptions ) );
183187
if ( rasterLayer->isValid() )
184188
{
185189
return rasterLayer.release();

‎src/core/qgsprojectfiletransform.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ void QgsProjectFileTransform::transform0110to1000()
340340
QString providerKey = providerNode.toElement().text();
341341

342342
//create the layer to get the provider for int->fieldName conversion
343-
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, false );
343+
QgsVectorLayer::LayerOptions options;
344+
options.loadDefaultStyle = false;
345+
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, options );
344346
if ( !layer->isValid() )
345347
{
346348
delete layer;

‎src/core/qgsvectorlayer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,12 @@ typedef bool deleteStyleById_t(
136136
QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
137137
const QString &baseName,
138138
const QString &providerKey,
139-
bool loadDefaultStyleFlag,
140-
bool readExtentFromXml )
139+
const LayerOptions &options )
141140
: QgsMapLayer( VectorLayer, baseName, vectorLayerPath )
142141
, mProviderKey( providerKey )
143142
, mAuxiliaryLayer( nullptr )
144143
, mAuxiliaryLayerKey( QString() )
145-
, mReadExtentFromXml( readExtentFromXml )
144+
, mReadExtentFromXml( options.readExtentFromXml )
146145
{
147146
mActions = new QgsActionManager( this );
148147
mConditionalStyles = new QgsConditionalLayerStyles();
@@ -153,7 +152,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
153152
// if we're given a provider type, try to create and bind one to this layer
154153
if ( !vectorLayerPath.isEmpty() && !mProviderKey.isEmpty() )
155154
{
156-
setDataSource( vectorLayerPath, baseName, providerKey, loadDefaultStyleFlag );
155+
setDataSource( vectorLayerPath, baseName, providerKey, options.loadDefaultStyle );
157156
}
158157

159158
connect( this, &QgsVectorLayer::selectionChanged, this, [ = ] { emit repaintRequested(); } );

‎src/core/qgsvectorlayer.h

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,24 +381,47 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
381381
RemoveFromSelection, //!< Remove from current selection
382382
};
383383

384+
/**
385+
* Setting options for loading vector layers.
386+
* \since QGIS 3.0
387+
*/
388+
struct LayerOptions
389+
{
390+
391+
/**
392+
* Constructor for LayerOptions.
393+
*/
394+
explicit LayerOptions( bool loadDefaultStyle = true, bool readExtentFromXml = false )
395+
: loadDefaultStyle( loadDefaultStyle )
396+
, readExtentFromXml( readExtentFromXml )
397+
{}
398+
399+
//! Set to true if the default layer style should be loaded
400+
bool loadDefaultStyle = true;
401+
402+
/**
403+
* If true, the layer extent will be read from XML (i.e. stored in the
404+
* project file). If false, the extent will be determined by the provider on layer load.
405+
*/
406+
bool readExtentFromXml = false;
407+
408+
};
409+
384410
/**
385411
* Constructor - creates a vector layer
386412
*
387413
* The QgsVectorLayer is constructed by instantiating a data provider. The provider
388414
* interprets the supplied path (url) of the data source to connect to and access the
389415
* data.
390416
*
391-
* \param path The path or url of the parameter. Typically this encodes
417+
* \param path The path or url of the parameter. Typically this encodes
392418
* parameters used by the data provider as url query items.
393-
* \param baseName The name used to represent the layer in the legend
394-
* \param providerLib The name of the data provider, e.g., "memory", "postgres"
395-
* \param loadDefaultStyleFlag whether to load the default style
396-
* \param readExtentFromXml Read extent from XML if true or let provider determine it if false
397-
*
419+
* \param baseName The name used to represent the layer in the legend
420+
* \param providerLib The name of the data provider, e.g., "memory", "postgres"
421+
* \param options layer load options
398422
*/
399-
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
400-
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
401-
bool readExtentFromXml = false );
423+
explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
424+
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );
402425

403426

404427
virtual ~QgsVectorLayer();

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ QgsRasterLayer::QgsRasterLayer()
108108
QgsRasterLayer::QgsRasterLayer( const QString &uri,
109109
const QString &baseName,
110110
const QString &providerKey,
111-
bool loadDefaultStyleFlag )
111+
const LayerOptions &options )
112112
: QgsMapLayer( RasterLayer, baseName, uri )
113113
// Constant that signals property not used.
114114
, QSTRING_NOT_SET( QStringLiteral( "Not Set" ) )
@@ -122,7 +122,7 @@ QgsRasterLayer::QgsRasterLayer( const QString &uri,
122122

123123
// load default style
124124
bool defaultLoadedFlag = false;
125-
if ( mValid && loadDefaultStyleFlag )
125+
if ( mValid && options.loadDefaultStyle )
126126
{
127127
loadDefaultStyle( defaultLoadedFlag );
128128
}

‎src/core/raster/qgsrasterlayer.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
166166
//! \brief Constructor. Provider is not set.
167167
QgsRasterLayer();
168168

169+
/**
170+
* Setting options for loading raster layers.
171+
* \since QGIS 3.0
172+
*/
173+
struct LayerOptions
174+
{
175+
176+
/**
177+
* Constructor for LayerOptions.
178+
*/
179+
explicit LayerOptions( bool loadDefaultStyle = true )
180+
: loadDefaultStyle( loadDefaultStyle )
181+
{}
182+
183+
//! Set to true if the default layer style should be loaded
184+
bool loadDefaultStyle = true;
185+
};
186+
169187
/**
170188
* \brief This is the constructor for the RasterLayer class.
171189
*
@@ -183,10 +201,10 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
183201
*
184202
* -
185203
* */
186-
QgsRasterLayer( const QString &uri,
187-
const QString &baseName = QString(),
188-
const QString &providerKey = "gdal",
189-
bool loadDefaultStyleFlag = true );
204+
explicit QgsRasterLayer( const QString &uri,
205+
const QString &baseName = QString(),
206+
const QString &providerKey = "gdal",
207+
const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );
190208

191209
~QgsRasterLayer();
192210

‎src/providers/ogr/qgsogrdataitems.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
229229
}
230230
}
231231
// Raster layers
232-
QgsRasterLayer rlayer( path, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), false );
232+
QgsRasterLayer::LayerOptions options;
233+
options.loadDefaultStyle = false;
234+
QgsRasterLayer rlayer( path, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), options );
233235
if ( !rlayer.dataProvider()->subLayers( ).empty() )
234236
{
235237
const QStringList layers( rlayer.dataProvider()->subLayers( ) );

‎tests/src/python/test_provider_virtual.py

Lines changed: 71 additions & 71 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.