Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #33259 from signedav/reload_provider
reloadData / forceReload on data providers
  • Loading branch information
m-kuhn committed Jan 9, 2020
2 parents 7240052 + dd0cc93 commit 3a3b430
Show file tree
Hide file tree
Showing 29 changed files with 118 additions and 83 deletions.
6 changes: 4 additions & 2 deletions python/core/auto_generated/qgsdataprovider.sip.in
Expand Up @@ -265,8 +265,10 @@ databases and servers.

virtual void reloadData();
%Docstring
Reloads the data from the source. Needs to be implemented by providers with data caches to
synchronize with changes in the data source
Reloads the data from the source by calling reloadProviderData() implemented
by providers with data caches to synchronize, changes in the data source, feature
counts and other specific actions.
Emits the `dataChanged` signal
%End

virtual QDateTime timestamp() const;
Expand Down
10 changes: 4 additions & 6 deletions python/core/auto_generated/qgsvectordataprovider.sip.in
Expand Up @@ -546,13 +546,11 @@ providers will return ``None``.
Returns the transaction this data provider is included in, if any.
%End

virtual void forceReload();
virtual void forceReload() /Deprecated/;
%Docstring
Forces a reload of the underlying datasource if the provider implements this
method.
In particular on the OGR provider, a pooled connection will be invalidated.
This forces QGIS to reopen a file or connection.
This can be required if the underlying file is replaced.

.. deprecated::
QGIS 3.12 - will be removed in QGIS 4.0 - use reloadData instead
%End

virtual QSet<QgsMapLayerDependency> dependencies() const;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetabledialog.cpp
Expand Up @@ -573,7 +573,7 @@ void QgsAttributeTableDialog::mActionSaveEdits_triggered()

void QgsAttributeTableDialog::mActionReload_triggered()
{
mMainView->masterModel()->layer()->dataProvider()->forceReload();
mMainView->masterModel()->layer()->dataProvider()->reloadData();
}

void QgsAttributeTableDialog::mActionAddFeature_triggered()
Expand Down
2 changes: 1 addition & 1 deletion src/core/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -522,7 +522,7 @@ void QgsGdalProvider::closeDataset()
closeCachedGdalHandlesFor( this );
}

void QgsGdalProvider::reloadData()
void QgsGdalProvider::reloadProviderData()
{
QMutexLocker locker( mpMutex );
closeDataset();
Expand Down
7 changes: 5 additions & 2 deletions src/core/providers/gdal/qgsgdalprovider.h
Expand Up @@ -199,8 +199,6 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
bool setNoDataValue( int bandNo, double noDataValue ) override;
bool remove() override;

void reloadData() override;

QString validateCreationOptions( const QStringList &createOptions, const QString &format ) override;
QString validatePyramidsConfigOptions( QgsRaster::RasterPyramidsFormat pyramidsFormat,
const QStringList &configOptions, const QString &fileFormat ) override;
Expand Down Expand Up @@ -335,6 +333,11 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
bool worldToPixel( double x, double y, int &col, int &row ) const;

bool mStatisticsAreReliable = false;

/**
* Closes and reinits dataset
*/
void reloadProviderData() override;
};

/**
Expand Down
9 changes: 2 additions & 7 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -3919,11 +3919,6 @@ QByteArray QgsOgrProvider::quotedIdentifier( const QByteArray &field ) const
return QgsOgrProviderUtils::quotedIdentifier( field, mGDALDriverName );
}

void QgsOgrProvider::forceReload()
{
QgsOgrConnPool::instance()->invalidateConnections( QgsOgrProviderUtils::connectionPoolId( dataSourceUri( true ), mShareSameDatasetAmongLayers ) );
}

QString QgsOgrProviderUtils::connectionPoolId( const QString &dataSourceURI, bool shareSameDatasetAmongLayers )
{
if ( shareSameDatasetAmongLayers )
Expand Down Expand Up @@ -4598,11 +4593,11 @@ void QgsOgrProvider::close()
invalidateCachedExtent( false );
}

void QgsOgrProvider::reloadData()
void QgsOgrProvider::reloadProviderData()
{
mFeaturesCounted = QgsVectorDataProvider::Uncounted;
bool wasValid = mValid;
forceReload();
QgsOgrConnPool::instance()->invalidateConnections( QgsOgrProviderUtils::connectionPoolId( dataSourceUri( true ), mShareSameDatasetAmongLayers ) );
close();
open( OpenModeSameAsCurrent );
if ( !mValid && wasValid )
Expand Down
15 changes: 7 additions & 8 deletions src/core/providers/ogr/qgsogrprovider.h
Expand Up @@ -165,14 +165,6 @@ class QgsOgrProvider : public QgsVectorDataProvider

QByteArray quotedIdentifier( const QByteArray &field ) const;

/**
* A forced reload invalidates the underlying connection.
* E.g. in case a shapefile is replaced, the old file will be closed
* and the new file will be opened.
*/
void forceReload() override;
void reloadData() override;

protected:
//! Loads fields from input file to member attributeFields
void loadFields();
Expand Down Expand Up @@ -335,6 +327,13 @@ class QgsOgrProvider : public QgsVectorDataProvider
QgsOgrTransaction *mTransaction = nullptr;

void setTransaction( QgsTransaction *transaction ) override;

/**
* Invalidates and reopens the file and resets the feature count
* E.g. in case a shapefile is replaced, the old file will be closed
* and the new file will be opened.
*/
void reloadProviderData() override;
};

class QgsOgrDataset;
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsdataprovider.cpp
Expand Up @@ -24,6 +24,12 @@ QgsDataProvider::QgsDataProvider( const QString &uri, const QgsDataProvider::Pro
{
}

void QgsDataProvider::reloadData()
{
reloadProviderData();
emit dataChanged();
}

void QgsDataProvider::setProviderProperty( QgsDataProvider::ProviderProperty property, const QVariant &value )
{
mProviderProperties.insert( property, value );
Expand Down
13 changes: 10 additions & 3 deletions src/core/qgsdataprovider.h
Expand Up @@ -356,10 +356,12 @@ class CORE_EXPORT QgsDataProvider : public QObject
}

/**
* Reloads the data from the source. Needs to be implemented by providers with data caches to
* synchronize with changes in the data source
* Reloads the data from the source by calling reloadProviderData() implemented
* by providers with data caches to synchronize, changes in the data source, feature
* counts and other specific actions.
* Emits the `dataChanged` signal
*/
virtual void reloadData() {}
virtual void reloadData();

//! Time stamp of data source in the moment when data/metadata were loaded by provider
virtual QDateTime timestamp() const { return mTimestamp; }
Expand Down Expand Up @@ -614,6 +616,11 @@ class CORE_EXPORT QgsDataProvider : public QObject
*/
mutable QMutex mOptionsMutex;

/**
* Reloads the data according to the provider
* \since QGIS 3.12
*/
virtual void reloadProviderData() {}
};


Expand Down
5 changes: 0 additions & 5 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -596,11 +596,6 @@ QgsTransaction *QgsVectorDataProvider::transaction() const
return nullptr;
}

void QgsVectorDataProvider::forceReload()
{
emit dataChanged();
}

static bool _compareEncodings( const QString &s1, const QString &s2 )
{
return s1.toLower() < s2.toLower();
Expand Down
9 changes: 2 additions & 7 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -549,13 +549,9 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
virtual QgsTransaction *transaction() const;

/**
* Forces a reload of the underlying datasource if the provider implements this
* method.
* In particular on the OGR provider, a pooled connection will be invalidated.
* This forces QGIS to reopen a file or connection.
* This can be required if the underlying file is replaced.
* \deprecated QGIS 3.12 - will be removed in QGIS 4.0 - use reloadData instead
*/
virtual void forceReload();
Q_DECL_DEPRECATED virtual void forceReload() SIP_DEPRECATED { reloadData(); }

/**
* Gets the list of layer ids on which this layer depends. This in particular determines the order of layer loading.
Expand Down Expand Up @@ -685,7 +681,6 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
* Includes this data provider in the specified transaction. Ownership of transaction is not transferred.
*/
virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
Expand Down
2 changes: 1 addition & 1 deletion src/providers/arcgisrest/qgsafsprovider.cpp
Expand Up @@ -328,7 +328,7 @@ QString QgsAfsProvider::dataComment() const
return mLayerDescription;
}

void QgsAfsProvider::reloadData()
void QgsAfsProvider::reloadProviderData()
{
mSharedData->clearCache();
}
Expand Down
6 changes: 5 additions & 1 deletion src/providers/arcgisrest/qgsafsprovider.h
Expand Up @@ -74,7 +74,6 @@ class QgsAfsProvider : public QgsVectorDataProvider
QString name() const override;
QString description() const override;
QString dataComment() const override;
void reloadData() override;
QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const override;
QgsAbstractVectorLayerLabeling *createLabeling( const QVariantMap &configuration = QVariantMap() ) const override;
bool renderInPreview( const QgsDataProvider::PreviewContext &context ) override;
Expand All @@ -89,6 +88,11 @@ class QgsAfsProvider : public QgsVectorDataProvider
QVariantMap mRendererDataMap;
QVariantList mLabelingDataList;
QgsStringMap mRequestHeaders;

/**
* Clears cache
*/
void reloadProviderData() override;
};

class QgsAfsProviderMetadata: public QgsProviderMetadata
Expand Down
2 changes: 1 addition & 1 deletion src/providers/arcgisrest/qgsamsprovider.cpp
Expand Up @@ -392,7 +392,7 @@ void QgsAmsProvider::setSubLayerVisibility( const QString &name, bool vis )
}
}

void QgsAmsProvider::reloadData()
void QgsAmsProvider::reloadProviderData()
{
mCachedImage = QImage();
}
Expand Down
6 changes: 5 additions & 1 deletion src/providers/arcgisrest/qgsamsprovider.h
Expand Up @@ -85,7 +85,6 @@ class QgsAmsProvider : public QgsRasterDataProvider
QStringList subLayerStyles() const override;
void setLayerOrder( const QStringList &layers ) override;
void setSubLayerVisibility( const QString &name, bool vis ) override;
void reloadData() override;
bool renderInPreview( const QgsDataProvider::PreviewContext &context ) override;
QgsLayerMetadata layerMetadata() const override;

Expand Down Expand Up @@ -158,6 +157,11 @@ class QgsAmsProvider : public QgsRasterDataProvider
int mMaxImageHeight = 4096;
QgsLayerMetadata mLayerMetadata;
QList< double > mResolutions;

/**
* Resets cached image
*/
void reloadProviderData() override;
};

//! Handler for tiled MapServer requests, the data are written to the given image
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mdal/qgsmdalprovider.cpp
Expand Up @@ -275,7 +275,7 @@ void QgsMdalProvider::loadData()
}
}

void QgsMdalProvider::reloadData()
void QgsMdalProvider::reloadProviderData()
{
if ( mMeshH )
MDAL_CloseMesh( mMeshH );
Expand Down
7 changes: 5 additions & 2 deletions src/providers/mdal/qgsmdalprovider.h
Expand Up @@ -83,8 +83,6 @@ class QgsMdalProvider : public QgsMeshDataProvider
const QVector<double> &times
) override;

void reloadData() override;

/**
* Returns file filters for meshes and datasets to be used in Open File Dialogs
* \param fileMeshFiltersString file mesh filters
Expand Down Expand Up @@ -114,6 +112,11 @@ class QgsMdalProvider : public QgsMeshDataProvider
MeshH mMeshH;
QStringList mExtraDatasetUris;
QgsCoordinateReferenceSystem mCrs;

/**
* Closes and reloads dataset
*/
void reloadProviderData() override;
};

class QgsMdalProviderMetadata: public QgsProviderMetadata
Expand Down
17 changes: 13 additions & 4 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -343,6 +343,13 @@ void QgsPostgresProvider::setListening( bool isListening )
}
}

void QgsPostgresProvider::reloadProviderData()
{
mShared->setFeaturesCounted( -1 );
mLayerExtent.setMinimal();
}


QgsPostgresConn *QgsPostgresProvider::connectionRW()
{
if ( mTransaction )
Expand Down Expand Up @@ -3323,11 +3330,13 @@ bool QgsPostgresProvider::setSubsetString( const QString &theSQL, bool updateFea

if ( updateFeatureCount )
{
mShared->setFeaturesCounted( -1 );
reloadData();
}
else
{
mLayerExtent.setMinimal();
emit dataChanged();
}
mLayerExtent.setMinimal();

emit dataChanged();

return true;
}
Expand Down
9 changes: 9 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -240,6 +240,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
void setListening( bool isListening ) override;


private:
Relkind relkind() const;

Expand Down Expand Up @@ -325,6 +326,14 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
static QList<QgsVectorLayer *> searchLayers( const QList<QgsVectorLayer *> &layers, const QString &connectionInfo, const QString &schema, const QString &tableName );

/**
* Effect a reload including resetting the feature count
* and setting the layer extent to minimal
*
* \since QGIS 3.12
*/
void reloadProviderData() override;

//! Old-style mapping of index to name for QgsPalLabeling fix
QgsAttrPalIndexNameHash mAttrPalIndexName;

Expand Down
2 changes: 1 addition & 1 deletion src/providers/virtual/qgsvirtuallayerprovider.cpp
Expand Up @@ -96,7 +96,7 @@ QgsVirtualLayerProvider::QgsVirtualLayerProvider( QString const &uri, const QgsD
}
}

void QgsVirtualLayerProvider::reloadData()
void QgsVirtualLayerProvider::reloadProviderData()
{
if ( mDefinition.sourceLayers().empty() && !mDefinition.filePath().isEmpty() && mDefinition.query().isEmpty() )
{
Expand Down
6 changes: 5 additions & 1 deletion src/providers/virtual/qgsvirtuallayerprovider.h
Expand Up @@ -60,7 +60,6 @@ class QgsVirtualLayerProvider: public QgsVectorDataProvider
QgsAttributeList pkAttributeIndexes() const override;
QSet<QgsMapLayerDependency> dependencies() const override;
bool cancelReload() override;
void reloadData() override;

private:

Expand Down Expand Up @@ -118,6 +117,11 @@ class QgsVirtualLayerProvider: public QgsVectorDataProvider
bool loadSourceLayers();
void createVirtualTable( QgsVectorLayer *vlayer, const QString &name );

/**
* Opens or creates file
*/
void reloadProviderData() override;

friend class QgsVirtualLayerFeatureSource;

private slots:
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wcs/qgswcsprovider.cpp
Expand Up @@ -1592,7 +1592,7 @@ QString QgsWcsProvider::description() const
return WCS_DESCRIPTION;
}

void QgsWcsProvider::reloadData()
void QgsWcsProvider::reloadProviderData()
{
clearCache();
}
Expand Down

0 comments on commit 3a3b430

Please sign in to comment.