Merge pull request #33259 from signedav/reload_provider

reloadData / forceReload on data providers
This commit is contained in:
Matthias Kuhn 2020-01-09 10:57:14 +01:00 committed by GitHub
commit 3a3b430825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 118 additions and 83 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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()

View File

@ -522,7 +522,7 @@ void QgsGdalProvider::closeDataset()
closeCachedGdalHandlesFor( this );
}
void QgsGdalProvider::reloadData()
void QgsGdalProvider::reloadProviderData()
{
QMutexLocker locker( mpMutex );
closeDataset();

View File

@ -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;
@ -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;
};
/**

View File

@ -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 )
@ -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 )

View File

@ -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();
@ -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;

View File

@ -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 );

View File

@ -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; }
@ -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() {}
};

View File

@ -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();

View File

@ -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.
@ -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 )

View File

@ -328,7 +328,7 @@ QString QgsAfsProvider::dataComment() const
return mLayerDescription;
}
void QgsAfsProvider::reloadData()
void QgsAfsProvider::reloadProviderData()
{
mSharedData->clearCache();
}

View File

@ -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;
@ -89,6 +88,11 @@ class QgsAfsProvider : public QgsVectorDataProvider
QVariantMap mRendererDataMap;
QVariantList mLabelingDataList;
QgsStringMap mRequestHeaders;
/**
* Clears cache
*/
void reloadProviderData() override;
};
class QgsAfsProviderMetadata: public QgsProviderMetadata

View File

@ -392,7 +392,7 @@ void QgsAmsProvider::setSubLayerVisibility( const QString &name, bool vis )
}
}
void QgsAmsProvider::reloadData()
void QgsAmsProvider::reloadProviderData()
{
mCachedImage = QImage();
}

View File

@ -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;
@ -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

View File

@ -275,7 +275,7 @@ void QgsMdalProvider::loadData()
}
}
void QgsMdalProvider::reloadData()
void QgsMdalProvider::reloadProviderData()
{
if ( mMeshH )
MDAL_CloseMesh( mMeshH );

View File

@ -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
@ -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

View File

@ -343,6 +343,13 @@ void QgsPostgresProvider::setListening( bool isListening )
}
}
void QgsPostgresProvider::reloadProviderData()
{
mShared->setFeaturesCounted( -1 );
mLayerExtent.setMinimal();
}
QgsPostgresConn *QgsPostgresProvider::connectionRW()
{
if ( mTransaction )
@ -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;
}

View File

@ -240,6 +240,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
void setListening( bool isListening ) override;
private:
Relkind relkind() const;
@ -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;

View File

@ -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() )
{

View File

@ -60,7 +60,6 @@ class QgsVirtualLayerProvider: public QgsVectorDataProvider
QgsAttributeList pkAttributeIndexes() const override;
QSet<QgsMapLayerDependency> dependencies() const override;
bool cancelReload() override;
void reloadData() override;
private:
@ -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:

View File

@ -1592,7 +1592,7 @@ QString QgsWcsProvider::description() const
return WCS_DESCRIPTION;
}
void QgsWcsProvider::reloadData()
void QgsWcsProvider::reloadProviderData()
{
clearCache();
}

View File

@ -187,7 +187,6 @@ class QgsWcsProvider : public QgsRasterDataProvider, QgsGdalProviderBase
QString lastErrorFormat() override;
QString name() const override;
QString description() const override;
void reloadData() override;
QList<QgsColorRampShader::ColorRampItem> colorTable( int bandNo )const override;
int colorInterpretation( int bandNo ) const override;
@ -400,6 +399,11 @@ class QgsWcsProvider : public QgsRasterDataProvider, QgsGdalProviderBase
QNetworkRequest::CacheLoadControl mCacheLoadControl = QNetworkRequest::PreferNetwork;
/**
* Clears cache
*/
void reloadProviderData() override;
};
//! Handler for downloading of coverage data - output is written to mCachedData

View File

@ -232,10 +232,10 @@ QgsRectangle QgsOapifProvider::extent() const
return mShared->consolidatedExtent();
}
void QgsOapifProvider::reloadData()
void QgsOapifProvider::reloadProviderData()
{
mUpdateFeatureCountAtNextFeatureCountRequest = true;
mShared->invalidateCache();
QgsVectorDataProvider::reloadData();
}
bool QgsOapifProvider::isValid() const
@ -302,13 +302,16 @@ bool QgsOapifProvider::setSubsetString( const QString &filter, bool updateFeatur
if ( !mShared->computeServerFilter( errorMsg ) )
QgsMessageLog::logMessage( errorMsg, tr( "OAPIF" ) );
reloadData();
if ( updateFeatureCount )
{
mUpdateFeatureCountAtNextFeatureCountRequest = true;
reloadData();
}
else
{
mShared->invalidateCache();
emit dataChanged();
}
emit dataChanged();
return true;
}

View File

@ -87,10 +87,6 @@ class QgsOapifProvider : public QgsVectorDataProvider
//! For QgsWFSSourceSelect::buildQuery()
const QString &clientSideFilterExpression() const;
public slots:
void reloadData() override;
private slots:
void pushErrorSlot( const QString &errorMsg );
@ -112,6 +108,11 @@ class QgsOapifProvider : public QgsVectorDataProvider
//! Initial requests
bool init();
/**
* Invalidates cache of shared object
*/
void reloadProviderData() override;
};
class QgsOapifProviderMetadata: public QgsProviderMetadata

View File

@ -714,6 +714,8 @@ QString QgsWFSProvider::subsetString() const
bool QgsWFSProvider::setSubsetString( const QString &theSQL, bool updateFeatureCount )
{
Q_UNUSED( updateFeatureCount )
QgsDebugMsgLevel( QStringLiteral( "theSql = '%1'" ).arg( theSQL ), 4 );
if ( theSQL == mSubsetString )
@ -755,11 +757,8 @@ bool QgsWFSProvider::setSubsetString( const QString &theSQL, bool updateFeatureC
QString errorMsg;
if ( !mShared->computeFilter( errorMsg ) )
QgsMessageLog::logMessage( errorMsg, tr( "WFS" ) );
reloadData();
if ( updateFeatureCount )
featureCount();
emit dataChanged();
reloadData();
return true;
}
@ -773,10 +772,9 @@ QgsAbstractFeatureSource *QgsWFSProvider::featureSource() const
return fs;
}
void QgsWFSProvider::reloadData()
void QgsWFSProvider::reloadProviderData()
{
mShared->invalidateCache();
QgsVectorDataProvider::reloadData();
}
QgsWkbTypes::Type QgsWFSProvider::wkbType() const

View File

@ -120,20 +120,22 @@ class QgsWFSProvider : public QgsVectorDataProvider
bool empty() const override;
public slots:
void reloadData() override;
private slots:
void featureReceivedAnalyzeOneFeature( QVector<QgsFeatureUniqueIdPair> );
void pushErrorSlot( const QString &errorMsg );
private:
//! Mutable data shared between provider and feature sources
std::shared_ptr<QgsWFSSharedData> mShared;
/**
* Invalidates cache of shared object
*/
void reloadProviderData() override;
friend class QgsWFSFeatureSource;
protected:

View File

@ -3273,10 +3273,6 @@ QString QgsWmsProvider::description() const
return WMS_DESCRIPTION;
}
void QgsWmsProvider::reloadData()
{
}
bool QgsWmsProvider::renderInPreview( const QgsDataProvider::PreviewContext &context )
{
if ( mSettings.mTiled || mSettings.mXyz )

View File

@ -213,7 +213,6 @@ class QgsWmsProvider : public QgsRasterDataProvider
QString name() const override;
static QString providerKey();
QString description() const override;
void reloadData() override;
bool renderInPreview( const QgsDataProvider::PreviewContext &context ) override;
QList< double > nativeResolutions() const override;
@ -441,7 +440,6 @@ class QgsWmsProvider : public QgsRasterDataProvider
*/
QString mError;
/**
* The mime type of the message
*/