Skip to content

Commit

Permalink
Merge pull request #2683 from SebDieBln/FixNonVectorQLR
Browse files Browse the repository at this point in the history
[Regression] make QLR files containing non-vector layers work again (fixes #14091)
  • Loading branch information
jef-n committed Jan 16, 2016
2 parents 1cd4570 + 52442f5 commit 9dd6c0a
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/core/qgslayerdefinition.cpp
Expand Up @@ -66,16 +66,11 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsLayerTreeGrou
clonedSorted << node.cloneNode();
}
QDomNode layersNode = doc.elementsByTagName( "maplayers" ).at( 0 );
// remove old children
// replace old children with new ones
QDomNodeList childNodes = layersNode.childNodes();
for ( int i = 0; i < childNodes.size(); i++ )
{
layersNode.removeChild( childNodes.at( i ) );
}
// replace with new ones
foreach ( QDomNode node, clonedSorted )
{
layersNode.appendChild( node );
layersNode.replaceChild( clonedSorted.at( i ), childNodes.at( i ) );
}
}
// if a dependency is missing, we still try to load layers, since dependencies may already be loaded
Expand Down Expand Up @@ -131,7 +126,7 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsLayerTreeGrou
// Now that all layers are loaded, refresh the vectorjoins to get the joined fields
Q_FOREACH ( QgsMapLayer* layer, layers )
{
QgsVectorLayer* vlayer = static_cast< QgsVectorLayer * >( layer );
QgsVectorLayer* vlayer = dynamic_cast< QgsVectorLayer * >( layer );
if ( vlayer )
{
vlayer->createJoinCaches();
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -384,7 +384,7 @@ void TestVectorLayerJoinBuffer::testJoinLayerDefinitionFile()
QList<QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayersByName( "layerB" );
QCOMPARE( mapLayers.count(), 1 );

QgsVectorLayer* vLayer = static_cast<QgsVectorLayer*>( mapLayers.value( 0 ) );
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>( mapLayers.value( 0 ) );
QVERIFY( vLayer );

// Check for vector join
Expand Down
20 changes: 19 additions & 1 deletion tests/src/python/test_qgslayerdefinition.py
Expand Up @@ -15,14 +15,19 @@
import qgis

from qgis.core import (QGis,
QgsProject,
QgsMapLayerRegistry,
QgsLayerDefinition
)

from utilities import (TestCase, unittest)
from utilities import (TestCase, unittest, getQgisTestApp, unitTestDataPath)

from PyQt4.QtCore import QVariant
from PyQt4.QtXml import QDomDocument

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath()


class TestQgsLayerDefinition(TestCase):

Expand Down Expand Up @@ -93,5 +98,18 @@ def testCyclicDependency(self):
nodes = dep.sortedLayerNodes()
self.assertTrue(dep.hasCycle())

def testVectorAndRaster(self):
# Load a simple QLR containing a vector layer and a raster layer.
QgsMapLayerRegistry.instance().removeAllMapLayers()
layers = QgsMapLayerRegistry.instance().mapLayers()
self.assertEqual(len(layers), 0)

(result, errMsg) = QgsLayerDefinition.loadLayerDefinition(TEST_DATA_DIR + '/vector_and_raster.qlr', QgsProject.instance().layerTreeRoot())
self.assertTrue(result)

layers = QgsMapLayerRegistry.instance().mapLayers()
self.assertEqual(len(layers), 2)
QgsMapLayerRegistry.instance().removeAllMapLayers()

if __name__ == '__main__':
unittest.main()

0 comments on commit 9dd6c0a

Please sign in to comment.