Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix loss of vector layer subset strings IF a layer path was invalid
at the time the subset string was originally applied
  • Loading branch information
nyalldawson committed Jan 9, 2020
1 parent 986930d commit 00ce537
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -7682,6 +7682,13 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
{
subsetString = vlayer->dataProvider()->subsetString();
}
if ( subsetString.isEmpty() )
{
// actually -- the above isn't true in all situations. If a layer was invalid at the time
// that the subset string was set, then ONLY the layer has knowledge of this subset string!
subsetString = vlayer->subsetString();
}

layer->setDataSource( uri.uri, layer->name(), uri.providerKey, QgsDataProvider::ProviderOptions() );
// Re-apply original style and subset string when fixing bad layers
if ( !( layerWasValid || layer->originalXmlProperties().isEmpty() ) )
Expand Down Expand Up @@ -7712,6 +7719,10 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
.arg( errorMsg ) );
}
}
else if ( !subsetString.isEmpty() )
{
vlayer->setSubsetString( subsetString );
}

// All the following code is necessary to refresh the layer
QgsLayerTreeModel *model = qobject_cast<QgsLayerTreeModel *>( mLayerTreeView->model() );
Expand Down
10 changes: 8 additions & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -904,16 +904,22 @@ QString QgsVectorLayer::subsetString() const
if ( !mValid || !mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
return QString();
return customProperty( QStringLiteral( "storedSubsetString" ) ).toString();
}
return mDataProvider->subsetString();
}

bool QgsVectorLayer::setSubsetString( const QString &subset )
{
if ( !mValid || !mDataProvider || mEditBuffer )
if ( !mValid || !mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider or while editing" ), 3 );
setCustomProperty( QStringLiteral( "storedSubsetString" ), subset );
return false;
}
else if ( mEditBuffer )
{
QgsDebugMsgLevel( QStringLiteral( "invoked while editing" ), 3 );
return false;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Expand Up @@ -3412,6 +3412,29 @@ def testTransformContextIsSyncedFromProject(self):
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
self.assertTrue(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

def testSubsetStringInvalidLayer(self):
"""
Test that subset strings can be set on invalid layers, and retrieved later...
"""
vl = QgsVectorLayer(
'nope',
'test', 'no')
self.assertFalse(vl.isValid())
self.assertIsNone(vl.dataProvider())
vl.setSubsetString('xxxxxxxxx')
self.assertEqual(vl.subsetString(), 'xxxxxxxxx')

# invalid layer subset strings must be persisted via xml
doc = QDomDocument("testdoc")
elem = doc.createElement("maplayer")
self.assertTrue(vl.writeXml(elem, doc, QgsReadWriteContext()))

vl2 = QgsVectorLayer(
'nope',
'test', 'no')
vl2.readXml(elem, QgsReadWriteContext())
self.assertEqual(vl2.subsetString(), 'xxxxxxxxx')


# TODO:
# - fetch rect: feat with changed geometry: 1. in rect, 2. out of rect
Expand Down

0 comments on commit 00ce537

Please sign in to comment.