Skip to content

Commit

Permalink
Avoid signalling repaint / data signals when a setSubsetString doesn'…
Browse files Browse the repository at this point in the history
…t actually change (#7217)

Behavior across providers harmonized & test added.
  • Loading branch information
nirvn committed Jun 11, 2018
1 parent 6e3ee46 commit 0a325f1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/core/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -565,6 +565,9 @@ bool QgsMemoryProvider::setSubsetString( const QString &theSQL, bool updateFeatu
return false;
}

if ( theSQL == mSubsetString )
return true;

mSubsetString = theSQL;
clearMinMaxCache();
mExtent.setMinimal();
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -890,6 +890,9 @@ bool QgsVectorLayer::setSubsetString( const QString &subset )
return false;
}

if ( subset == mDataProvider->subsetString() )
return true;

bool res = mDataProvider->setSubsetString( subset );

// get the updated data source string from the provider
Expand Down
3 changes: 3 additions & 0 deletions src/providers/oracle/qgsoracleprovider.cpp
Expand Up @@ -2095,6 +2095,9 @@ bool QgsOracleProvider::setSubsetString( const QString &theSQL, bool updateFeatu
if ( !mConnection )
return false;

if ( theSQL.trimmed() == mSqlWhereClause )
return true;

QString prevWhere = mSqlWhereClause;

mSqlWhereClause = theSQL.trimmed();
Expand Down
3 changes: 3 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3098,6 +3098,9 @@ QgsVectorDataProvider::Capabilities QgsPostgresProvider::capabilities() const

bool QgsPostgresProvider::setSubsetString( const QString &theSQL, bool updateFeatureCount )
{
if ( theSQL.trimmed() == mSqlWhereClause )
return true;

QString prevWhere = mSqlWhereClause;

mSqlWhereClause = theSQL.trimmed();
Expand Down
9 changes: 6 additions & 3 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -604,9 +604,10 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri, const Provider
<< QgsVectorDataProvider::NativeType( tr( "Array of decimal numbers (double)" ), SPATIALITE_ARRAY_PREFIX.toUpper() + "REAL" + SPATIALITE_ARRAY_SUFFIX.toUpper(), QVariant::List, 0, 0, 0, 0, QVariant::Double )
<< QgsVectorDataProvider::NativeType( tr( "Array of whole numbers (integer)" ), SPATIALITE_ARRAY_PREFIX.toUpper() + "INTEGER" + SPATIALITE_ARRAY_SUFFIX.toUpper(), QVariant::List, 0, 0, 0, 0, QVariant::LongLong )
);

// Update extent and feature count
if ( ! mSubsetString.isEmpty() )
setSubsetString( mSubsetString, true );
getTableSummary();

mValid = true;
}
Expand Down Expand Up @@ -3450,6 +3451,9 @@ QString QgsSpatiaLiteProvider::subsetString() const

bool QgsSpatiaLiteProvider::setSubsetString( const QString &theSQL, bool updateFeatureCount )
{
if ( theSQL == mSubsetString )
return true;

QString prevSubsetString = mSubsetString;
mSubsetString = theSQL;

Expand All @@ -3461,6 +3465,7 @@ bool QgsSpatiaLiteProvider::setSubsetString( const QString &theSQL, bool updateF
// update feature count and extents
if ( updateFeatureCount && getTableSummary() )
{
emit dataChanged();
return true;
}

Expand All @@ -3473,8 +3478,6 @@ bool QgsSpatiaLiteProvider::setSubsetString( const QString &theSQL, bool updateF

getTableSummary();

emit dataChanged();

return false;
}

Expand Down
6 changes: 6 additions & 0 deletions src/providers/virtual/qgsvirtuallayerprovider.cpp
Expand Up @@ -503,10 +503,16 @@ QString QgsVirtualLayerProvider::subsetString() const

bool QgsVirtualLayerProvider::setSubsetString( const QString &subset, bool updateFeatureCount )
{
if ( subset == mSubset )
return true;

mSubset = subset;
clearMinMaxCache();
if ( updateFeatureCount )
updateStatistics();

emit dataChanged();

return true;
}

Expand Down
7 changes: 7 additions & 0 deletions src/providers/wfs/qgswfsprovider.cpp
Expand Up @@ -679,6 +679,9 @@ bool QgsWFSProvider::setSubsetString( const QString &theSQL, bool updateFeatureC
{
QgsDebugMsg( QString( "theSql = '%1'" ).arg( theSQL ) );

if ( theSQL == mSubsetString )
return true;

// Invalid and cancel current download before altering fields, etc...
// (crashes might happen if not done at the beginning)
mShared->invalidateCache();
Expand Down Expand Up @@ -710,13 +713,17 @@ bool QgsWFSProvider::setSubsetString( const QString &theSQL, bool updateFeatureC
mShared->mURI.setSql( QString() );
mShared->mURI.setFilter( theSQL );
}

setDataSourceUri( mShared->mURI.uri() );
QString errorMsg;
if ( !mShared->computeFilter( errorMsg ) )
QgsMessageLog::logMessage( errorMsg, tr( "WFS" ) );
reloadData();
if ( updateFeatureCount )
featureCount();

emit dataChanged();

return true;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/provider_python.py
Expand Up @@ -373,10 +373,13 @@ def subsetString(self):
return self._subset_string

def setSubsetString(self, subsetString):
if subsetString == self._subset_string:
return True
self._subset_string = subsetString
self.updateExtents()
self.clearMinMaxCache()
self.dataChanged.emit()
return True

def supportsSubsetString(self):
return True
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/providertestbase.py
Expand Up @@ -30,6 +30,7 @@
QgsTestUtils,
NULL
)
from qgis.PyQt.QtTest import QSignalSpy

from utilities import compareWkt
from featuresourcetestbase import FeatureSourceTestCase
Expand Down Expand Up @@ -157,9 +158,16 @@ def testSubsetString(self):
print('Provider does not support subset strings')
return

changed_spy = QSignalSpy(self.source.dataChanged)
subset = self.getSubsetString()
self.source.setSubsetString(subset)
self.assertEqual(self.source.subsetString(), subset)
self.assertEqual(len(changed_spy), 1)

# No signal should be emitted if the subset string is not modified
self.source.setSubsetString(subset)
self.assertEqual(len(changed_spy), 1)

result = set([f['pk'] for f in self.source.getFeatures()])
all_valid = (all(f.isValid() for f in self.source.getFeatures()))
self.source.setSubsetString(None)
Expand Down

0 comments on commit 0a325f1

Please sign in to comment.