Skip to content
Permalink
Browse files

Merge pull request #2683 from SebDieBln/FixNonVectorQLR

[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 9dd6c0a4653e29b9189385d89f225e514bbe1d72
@@ -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
@@ -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();
@@ -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
@@ -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):

@@ -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.
You can’t perform that action at this time.