Skip to content
Permalink
Browse files
Merge pull request #46411 from troopa81/fix_embedded_group
Correctly resolveReference after we load/create embedded group
  • Loading branch information
troopa81 committed Jan 4, 2022
2 parents c2363a3 + 2508228 commit e8a971156d9ed23963503e39d5f0ae03e37ef8d8
@@ -13630,52 +13630,61 @@ void QgisApp::addMapLayer( QgsMapLayer *mapLayer )
}
}


void QgisApp::embedLayers()
{
//dialog to select groups/layers from other project files
QgsProjectLayerGroupDialog d( this );
if ( d.exec() == QDialog::Accepted && d.isValid() )
{
QgsCanvasRefreshBlocker refreshBlocker;
addEmbeddedItems( d.selectedProjectFile(), d.selectedGroups(), d.selectedLayerIds() );
}
}

QString projectFile = d.selectedProjectFile();
void QgisApp::addEmbeddedItems( const QString &projectFile, const QStringList &groups, const QStringList &layerIds )
{
QgsCanvasRefreshBlocker refreshBlocker;

//groups
QStringList groups = d.selectedGroups();
QStringList::const_iterator groupIt = groups.constBegin();
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
QgsLayerTreeGroup *newGroup = QgsProject::instance()->createEmbeddedGroup( *groupIt, projectFile, QStringList() );
//groups
QStringList::const_iterator groupIt = groups.constBegin();
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
QgsLayerTreeGroup *newGroup = QgsProject::instance()->createEmbeddedGroup( *groupIt, projectFile, QStringList() );

if ( newGroup )
QgsProject::instance()->layerTreeRoot()->addChildNode( newGroup );
}
if ( newGroup )
QgsProject::instance()->layerTreeRoot()->addChildNode( newGroup );
}

//layer ids
QList<QDomNode> brokenNodes;
//layer ids
QList<QDomNode> brokenNodes;

// resolve dependencies
QgsLayerDefinition::DependencySorter depSorter( projectFile );
QStringList sortedIds = depSorter.sortedLayerIds();
QStringList layerIds = d.selectedLayerIds();
const auto constSortedIds = sortedIds;
for ( const QString &id : constSortedIds )
// resolve dependencies
QgsLayerDefinition::DependencySorter depSorter( projectFile );
QStringList sortedIds = depSorter.sortedLayerIds();
const auto constSortedIds = sortedIds;
for ( const QString &id : constSortedIds )
{
const auto constLayerIds = layerIds;
for ( const QString &selId : constLayerIds )
{
const auto constLayerIds = layerIds;
for ( const QString &selId : constLayerIds )
{
if ( selId == id )
QgsProject::instance()->createEmbeddedLayer( selId, projectFile, brokenNodes );
}
if ( selId == id )
QgsProject::instance()->createEmbeddedLayer( selId, projectFile, brokenNodes );
}
}

// fix broken relations and dependencies
for ( const QString &id : constSortedIds )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( id ) );
if ( vlayer )
vectorLayerStyleLoaded( vlayer, QgsMapLayer::AllStyleCategories );
}
// fix broken relations and dependencies
for ( const QString &id : constSortedIds )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( id ) );
if ( vlayer )
vectorLayerStyleLoaded( vlayer, QgsMapLayer::AllStyleCategories );
}

// Resolve references to other layers
const QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
for ( QMap<QString, QgsMapLayer *>::const_iterator it = layers.constBegin(); it != layers.constEnd(); ++it )
{
it.value()->resolveReferences( QgsProject::instance() );
}
}

@@ -1562,8 +1562,17 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void showMeshCalculator();
//! Open dialog to align raster layers
void showAlignRasterTool();

/**
* Called whenever user wants to embed layers
*/
void embedLayers();

/**
* Embed \a groups and \a layerIds items from \a projectFile
*/
void addEmbeddedItems( const QString &projectFile, const QStringList &groups, const QStringList &layerIds );

//! Creates a new map canvas view
void newMapCanvas();
//! Creates a new 3D map canvas view
@@ -2739,6 +2748,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
friend class QgsCanvasRefreshBlocker;

friend class TestQgisAppPython;
friend class TestQgisApp;
friend class QgisAppInterface;
friend class QgsAppScreenShots;
};
@@ -1640,6 +1640,10 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
mMainAnnotationLayer->readLayerXml( doc->documentElement().firstChildElement( QStringLiteral( "main-annotation-layer" ) ), context );
mMainAnnotationLayer->setTransformContext( mTransformContext );

// load embedded groups and layers
profile.switchTask( tr( "Loading embedded layers" ) );
loadEmbeddedNodes( mRootGroup, flags );

// Resolve references to other layers
// Needs to be done here once all dependent layers are loaded
profile.switchTask( tr( "Resolving layer references" ) );
@@ -1651,10 +1655,6 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags

mLayerTreeRegistryBridge->setEnabled( true );

// load embedded groups and layers
profile.switchTask( tr( "Loading embedded layers" ) );
loadEmbeddedNodes( mRootGroup, flags );

// now that layers are loaded, we can resolve layer tree's references to the layers
profile.switchTask( tr( "Resolving references" ) );
mRootGroup->resolveReferences( this );
@@ -42,6 +42,7 @@ class TestQgisApp : public QObject
void addVectorLayerGeopackageSingleLayer();
void addVectorLayerGeopackageSingleLayerAlreadyLayername();
void addVectorLayerInvalid();
void addEmbeddedGroup();

private:
QgisApp *mQgisApp = nullptr;
@@ -137,5 +138,22 @@ void TestQgisApp::addVectorLayerInvalid()
QVERIFY( !layer );
}

void TestQgisApp::addEmbeddedGroup()
{
const QString projectPath = QString( TEST_DATA_DIR ) + QStringLiteral( "/embedded_groups/joins1.qgs" );

QCOMPARE( QgsProject::instance()->layers<QgsVectorLayer *>().count(), 0 );

mQgisApp->addEmbeddedItems( projectPath, QStringList() << QStringLiteral( "GROUP" ), QStringList() );

QgsVectorLayer *vl = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QStringLiteral( "polys_with_id_32002f94_eebe_40a5_a182_44198ba1bc5a" ) );
QCOMPARE( vl->fields().count(), 5 );

// cleanup
QgsProject::instance()->clear();
}



QGSTEST_MAIN( TestQgisApp )
#include "testqgisapp.moc"
@@ -59,6 +59,7 @@ class TestQgsProject : public QObject
void testAttachmentsQgs();
void testAttachmentsQgz();
void testAttachmentIdentifier();
void testEmbeddedGroupWithJoins();
};

void TestQgsProject::init()
@@ -920,5 +921,18 @@ void TestQgsProject::testAttachmentIdentifier()
}


void TestQgsProject::testEmbeddedGroupWithJoins()
{
const QString projectPath = QString( TEST_DATA_DIR ) + QStringLiteral( "/embedded_groups/joins2.qgz" );
QgsProject p;
p.read( projectPath );

QCOMPARE( p.layers<QgsVectorLayer *>().count(), 2 );

QgsVectorLayer *vl = p.mapLayer<QgsVectorLayer *>( QStringLiteral( "polys_with_id_32002f94_eebe_40a5_a182_44198ba1bc5a" ) );
QCOMPARE( vl->fields().count(), 5 );
}


QGSTEST_MAIN( TestQgsProject )
#include "testqgsproject.moc"

0 comments on commit e8a9711

Please sign in to comment.