Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make layer validity a property
Adds the possibility to react to layer validity at runtime
  • Loading branch information
m-kuhn committed Aug 8, 2020
1 parent bafd380 commit 1aa6697
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 51 deletions.
9 changes: 8 additions & 1 deletion python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -1584,6 +1584,13 @@ Emitted when a style has been loaded
.. versionadded:: 3.12
%End

void isValidChanged();
%Docstring
Emitted when the validity of this layer changed.

.. versionadded:: 3.16
%End


protected:

Expand All @@ -1603,7 +1610,7 @@ Sets the extent

void setValid( bool valid );
%Docstring
Sets whether layer is valid or not - should be used in constructor.
Sets whether layer is valid or not
%End

virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );
Expand Down
6 changes: 3 additions & 3 deletions src/core/mesh/qgsmeshlayer.cpp
Expand Up @@ -1163,7 +1163,7 @@ bool QgsMeshLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &con
mStaticVectorDatasetIndex = elemStaticDataset.attribute( QStringLiteral( "vector" ) ).toInt();
}

return mValid; // should be true if read successfully
return isValid(); // should be true if read successfully
}

bool QgsMeshLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
Expand Down Expand Up @@ -1259,8 +1259,8 @@ bool QgsMeshLayer::setDataProvider( QString const &provider, const QgsDataProvid
mDataProvider->setParent( this );
QgsDebugMsgLevel( QStringLiteral( "Instantiated the mesh data provider plugin" ), 2 );

mValid = mDataProvider->isValid();
if ( !mValid )
setValid( mDataProvider->isValid() );
if ( !isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid mesh provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -1769,7 +1769,11 @@ bool QgsMapLayer::isTemporary() const

void QgsMapLayer::setValid( bool valid )
{
if ( mValid == valid )
return;

mValid = valid;
emit isValidChanged();
}

void QgsMapLayer::setLegend( QgsMapLayerLegend *legend )
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsmaplayer.h
Expand Up @@ -88,6 +88,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
Q_PROPERTY( QgsLayerMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )
Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
Q_PROPERTY( QgsMapLayerType type READ type CONSTANT )
Q_PROPERTY( bool isValid READ isValid NOTIFY isValidChanged )

#ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE
Expand Down Expand Up @@ -1414,6 +1415,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
void styleLoaded( QgsMapLayer::StyleCategories categories );

/**
* Emitted when the validity of this layer changed.
*
* \since QGIS 3.16
*/
void isValidChanged();


private slots:

Expand All @@ -1431,7 +1439,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Sets the extent
virtual void setExtent( const QgsRectangle &rect );

//! Sets whether layer is valid or not - should be used in constructor.
//! Sets whether layer is valid or not
void setValid( bool valid );

/**
Expand Down
66 changes: 33 additions & 33 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -186,7 +186,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
mAttributeAliasMap.insert( field.name(), QString() );
}

if ( mValid )
if ( isValid() )
{
mTemporalProperties->setDefaultsFromDataProviderTemporalCapabilities( mDataProvider->temporalCapabilities() );
if ( !mTemporalProperties->isActive() )
Expand Down Expand Up @@ -216,7 +216,7 @@ QgsVectorLayer::~QgsVectorLayer()
{
emit willBeDeleted();

mValid = false;
setValid( false );

delete mDataProvider;
delete mEditBuffer;
Expand Down Expand Up @@ -642,7 +642,7 @@ QgsMapLayerTemporalProperties *QgsVectorLayer::temporalProperties()

void QgsVectorLayer::setProviderEncoding( const QString &encoding )
{
if ( mValid && mDataProvider && mDataProvider->encoding() != encoding )
if ( isValid() && mDataProvider && mDataProvider->encoding() != encoding )
{
mDataProvider->setEncoding( encoding );
updateFields();
Expand All @@ -669,7 +669,7 @@ QgsWkbTypes::Type QgsVectorLayer::wkbType() const

QgsRectangle QgsVectorLayer::boundingBoxOfSelected() const
{
if ( !mValid || !isSpatial() || mSelectedFeatureIds.isEmpty() || !mDataProvider ) //no selected features
if ( !isValid() || !isSpatial() || mSelectedFeatureIds.isEmpty() || !mDataProvider ) //no selected features
{
return QgsRectangle( 0, 0, 0, 0 );
}
Expand Down Expand Up @@ -772,7 +772,7 @@ QgsVectorLayerFeatureCounter *QgsVectorLayer::countSymbolFeatures( bool storeSym
mSymbolFeatureCountMap.clear();
mSymbolFeatureIdMap.clear();

if ( !mValid )
if ( !isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer" ), 3 );
return mFeatureCounter;
Expand Down Expand Up @@ -863,7 +863,7 @@ QgsRectangle QgsVectorLayer::extent() const
if ( mValidExtent )
return QgsMapLayer::extent();

if ( !mValid || !mDataProvider )
if ( !isValid() || !mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
return rect;
Expand Down Expand Up @@ -933,7 +933,7 @@ QgsRectangle QgsVectorLayer::sourceExtent() const

QString QgsVectorLayer::subsetString() const
{
if ( !mValid || !mDataProvider )
if ( !isValid() || !mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
return customProperty( QStringLiteral( "storedSubsetString" ) ).toString();
Expand All @@ -943,7 +943,7 @@ QString QgsVectorLayer::subsetString() const

bool QgsVectorLayer::setSubsetString( const QString &subset )
{
if ( !mValid || !mDataProvider )
if ( !isValid() || !mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider or while editing" ), 3 );
setCustomProperty( QStringLiteral( "storedSubsetString" ), subset );
Expand Down Expand Up @@ -976,7 +976,7 @@ bool QgsVectorLayer::setSubsetString( const QString &subset )

bool QgsVectorLayer::simplifyDrawingCanbeApplied( const QgsRenderContext &renderContext, QgsVectorSimplifyMethod::SimplifyHint simplifyHint ) const
{
if ( mValid && mDataProvider && !mEditBuffer && ( isSpatial() && geometryType() != QgsWkbTypes::PointGeometry ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
if ( isValid() && mDataProvider && !mEditBuffer && ( isSpatial() && geometryType() != QgsWkbTypes::PointGeometry ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
{
double maximumSimplificationScale = mSimplifyMethod.maximumScale();

Expand All @@ -993,7 +993,7 @@ QgsConditionalLayerStyles *QgsVectorLayer::conditionalStyles() const

QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest &request ) const
{
if ( !mValid || !mDataProvider )
if ( !isValid() || !mDataProvider )
return QgsFeatureIterator();

return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
Expand All @@ -1011,7 +1011,7 @@ QgsGeometry QgsVectorLayer::getGeometry( QgsFeatureId fid ) const

bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return false;


Expand Down Expand Up @@ -1095,7 +1095,7 @@ bool QgsVectorLayer::updateFeature( QgsFeature &updatedFeature, bool skipDefault

bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return false;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1108,7 +1108,7 @@ bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId,

bool QgsVectorLayer::insertVertex( const QgsPoint &point, QgsFeatureId atFeatureId, int beforeVertex )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return false;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1121,7 +1121,7 @@ bool QgsVectorLayer::insertVertex( const QgsPoint &point, QgsFeatureId atFeature

bool QgsVectorLayer::moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return false;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1134,7 +1134,7 @@ bool QgsVectorLayer::moveVertex( double x, double y, QgsFeatureId atFeatureId, i

bool QgsVectorLayer::moveVertex( const QgsPoint &p, QgsFeatureId atFeatureId, int atVertex )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return false;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1147,7 +1147,7 @@ bool QgsVectorLayer::moveVertex( const QgsPoint &p, QgsFeatureId atFeatureId, in

QgsVectorLayer::EditResult QgsVectorLayer::deleteVertex( QgsFeatureId featureId, int vertex )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsVectorLayer::InvalidLayer;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1161,7 +1161,7 @@ QgsVectorLayer::EditResult QgsVectorLayer::deleteVertex( QgsFeatureId featureId,

bool QgsVectorLayer::deleteSelectedFeatures( int *deletedCount, QgsVectorLayer::DeleteContext *context )
{
if ( !mValid || !mDataProvider || !( mDataProvider->capabilities() & QgsVectorDataProvider::DeleteFeatures ) )
if ( !isValid() || !mDataProvider || !( mDataProvider->capabilities() & QgsVectorDataProvider::DeleteFeatures ) )
{
return false;
}
Expand Down Expand Up @@ -1211,7 +1211,7 @@ QgsGeometry::OperationResult QgsVectorLayer::addRing( const QVector<QgsPointXY>

QgsGeometry::OperationResult QgsVectorLayer::addRing( const QgsPointSequence &ring, QgsFeatureId *featureId )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsGeometry::OperationResult::LayerNotEditable;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1234,7 +1234,7 @@ QgsGeometry::OperationResult QgsVectorLayer::addRing( const QgsPointSequence &ri

QgsGeometry::OperationResult QgsVectorLayer::addRing( QgsCurve *ring, QgsFeatureId *featureId )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
{
delete ring;
return QgsGeometry::OperationResult::LayerNotEditable;
Expand Down Expand Up @@ -1288,7 +1288,7 @@ QgsGeometry::OperationResult QgsVectorLayer::addPart( const QVector<QgsPointXY>

QgsGeometry::OperationResult QgsVectorLayer::addPart( const QgsPointSequence &points )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsGeometry::OperationResult::LayerNotEditable;

//number of selected features must be 1
Expand All @@ -1314,7 +1314,7 @@ QgsGeometry::OperationResult QgsVectorLayer::addPart( const QgsPointSequence &po

QgsGeometry::OperationResult QgsVectorLayer::addPart( QgsCurve *ring )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsGeometry::OperationResult::LayerNotEditable;

//number of selected features must be 1
Expand All @@ -1340,7 +1340,7 @@ QgsGeometry::OperationResult QgsVectorLayer::addPart( QgsCurve *ring )

int QgsVectorLayer::translateFeature( QgsFeatureId featureId, double dx, double dy )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsGeometry::OperationResult::LayerNotEditable;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1357,7 +1357,7 @@ QgsGeometry::OperationResult QgsVectorLayer::splitParts( const QVector<QgsPointX
}
QgsGeometry::OperationResult QgsVectorLayer::splitParts( const QgsPointSequence &splitLine, bool topologicalEditing )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsGeometry::OperationResult::LayerNotEditable;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1376,7 +1376,7 @@ QgsGeometry::OperationResult QgsVectorLayer::splitFeatures( const QgsPointSequen

QgsGeometry::OperationResult QgsVectorLayer::splitFeatures( const QgsCurve *curve, bool preserveCircular, bool topologicalEditing )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return QgsGeometry::OperationResult::LayerNotEditable;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1385,7 +1385,7 @@ QgsGeometry::OperationResult QgsVectorLayer::splitFeatures( const QgsCurve *curv

int QgsVectorLayer::addTopologicalPoints( const QgsGeometry &geom )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return -1;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1399,7 +1399,7 @@ int QgsVectorLayer::addTopologicalPoints( const QgsPointXY &p )

int QgsVectorLayer::addTopologicalPoints( const QgsPoint &p )
{
if ( !mValid || !mEditBuffer || !mDataProvider )
if ( !isValid() || !mEditBuffer || !mDataProvider )
return -1;

QgsVectorLayerEditUtils utils( this );
Expand All @@ -1417,7 +1417,7 @@ void QgsVectorLayer::setLabeling( QgsAbstractVectorLayerLabeling *labeling )

bool QgsVectorLayer::startEditing()
{
if ( !mValid || !mDataProvider )
if ( !isValid() || !mDataProvider )
{
return false;
}
Expand Down Expand Up @@ -1607,7 +1607,7 @@ bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c
// QGIS Server WMS Dimensions
mServerProperties->readXml( layer_node );

return mValid; // should be true if read successfully
return isValid(); // should be true if read successfully

} // void QgsVectorLayer::readXml

Expand All @@ -1626,7 +1626,7 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
setName( baseName );
setDataProvider( provider, options );

if ( !mValid )
if ( !isValid() )
{
emit dataSourceChanged();
return;
Expand All @@ -1636,7 +1636,7 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
setCoordinateSystem();

// reset style if loading default style, style is missing, or geometry type is has changed (and layer is valid)
if ( !renderer() || !legend() || ( mValid && geomType != geometryType() ) || loadDefaultStyleFlag )
if ( !renderer() || !legend() || ( isValid() && geomType != geometryType() ) || loadDefaultStyleFlag )
{
std::unique_ptr< QgsScopedRuntimeProfile > profile;
if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
Expand Down Expand Up @@ -1732,7 +1732,7 @@ bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProv
mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options ) );
if ( !mDataProvider )
{
mValid = false;
setValid( false );
QgsDebugMsgLevel( QStringLiteral( "Unable to get data provider" ), 2 );
return false;
}
Expand All @@ -1742,8 +1742,8 @@ bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProv

QgsDebugMsgLevel( QStringLiteral( "Instantiated the data provider plugin" ), 2 );

mValid = mDataProvider->isValid();
if ( !mValid )
setValid( mDataProvider->isValid() );
if ( !isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
return false;
Expand Down

0 comments on commit 1aa6697

Please sign in to comment.