Skip to content
Permalink
Browse files

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 8, 2020
1 parent 986930d commit 00ce537e41ce64d89fab9cc3ff90011096cbfd12
Showing with 42 additions and 2 deletions.
  1. +11 −0 src/app/qgisapp.cpp
  2. +8 −2 src/core/qgsvectorlayer.cpp
  3. +23 −0 tests/src/python/test_qgsvectorlayer.py
@@ -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() ) )
@@ -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() );
@@ -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;
}

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

0 comments on commit 00ce537

Please sign in to comment.
You can’t perform that action at this time.