Skip to content

Commit

Permalink
Allow to embed layers with dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Jan 7, 2016
1 parent 1a1af37 commit 7e5915e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
8 changes: 8 additions & 0 deletions python/core/qgslayerdefinition.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ class QgsLayerDefinition
*/
DependencySorter( QDomDocument doc );

/** Constructor
* @param fileName The filename where the XML document is stored
*/
DependencySorter( const QString& fileName );

/** Get the layer nodes in an order where they can be loaded incrementally without dependency break */
QVector<QDomNode> sortedLayerNodes() const;

/** Get the layer IDs in an order where they can be loaded incrementally without dependency break */
QStringList sortedLayerIds() const;

/** Whether some cyclic dependency has been detected */
bool hasCycle() const;

Expand Down
13 changes: 10 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8467,11 +8467,18 @@ void QgisApp::embedLayers()
//layer ids
QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;

// resolve dependencies
QgsLayerDefinition::DependencySorter depSorter( projectFile );
QStringList sortedIds = depSorter.sortedLayerIds();
QStringList layerIds = d.selectedLayerIds();
QStringList::const_iterator layerIt = layerIds.constBegin();
for ( ; layerIt != layerIds.constEnd(); ++layerIt )
foreach ( QString id, sortedIds )
{
QgsProject::instance()->createEmbeddedLayer( *layerIt, projectFile, brokenNodes, vectorLayerList );
foreach ( QString selId, layerIds )
{
if ( selId == id )
QgsProject::instance()->createEmbeddedLayer( selId, projectFile, brokenNodes, vectorLayerList );
}
}

mMapCanvas->freeze( false );
Expand Down
28 changes: 25 additions & 3 deletions src/core/qgslayerdefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ bool QgsLayerDefinition::exportLayerDefinition( QDomDocument doc, const QList<Qg
return true;
}

QgsLayerDefinition::DependencySorter::DependencySorter( QDomDocument doc ) :
mHasCycle( false ), mHasMissingDependency( false )
void QgsLayerDefinition::DependencySorter::init( QDomDocument doc )
{
// Determine a loading order of layers based on a graph of dependencies
QMap< QString, QVector< QString > > dependencies;
QVector<QString> sortedLayers;
QStringList sortedLayers;
QList< QPair<QString, QDomNode> > layersToSort;
QStringList layerIds;

QDomNodeList nl = doc.elementsByTagName( "maplayer" );
for ( int i = 0; i < nl.count(); i++ )
Expand All @@ -202,6 +202,7 @@ QgsLayerDefinition::DependencySorter::DependencySorter( QDomDocument doc ) :
QDomElement element = node.toElement();

QString id = node.namedItem( "id" ).toElement().text();
layerIds << id;

// dependencies for this layer
QDomElement layerDependenciesElem = node.firstChildElement( "layerDependencies" );
Expand Down Expand Up @@ -234,6 +235,9 @@ QgsLayerDefinition::DependencySorter::DependencySorter( QDomDocument doc ) :
{
// some dependencies are not satisfied
mHasMissingDependency = true;
for ( int i = 0; i < nl.size(); i++ )
mSortedLayerNodes << nl.at( i );
mSortedLayerIds = layerIds;
return;
}
}
Expand Down Expand Up @@ -264,6 +268,7 @@ QgsLayerDefinition::DependencySorter::DependencySorter( QDomDocument doc ) :
{
sortedLayers << idToSort;
mSortedLayerNodes << node;
mSortedLayerIds << idToSort;
it = layersToSort.erase( it ); // erase and go to the next
mHasCycle = false;
}
Expand All @@ -275,3 +280,20 @@ QgsLayerDefinition::DependencySorter::DependencySorter( QDomDocument doc ) :
}
}

QgsLayerDefinition::DependencySorter::DependencySorter( QDomDocument doc ) :
mHasCycle( false ), mHasMissingDependency( false )
{
init( doc );
}

QgsLayerDefinition::DependencySorter::DependencySorter( const QString& fileName ) :
mHasCycle( false ), mHasMissingDependency( false )
{
QDomDocument doc;
QFile pFile( fileName );
pFile.open( QIODevice::ReadOnly );
doc.setContent( &pFile );
init( doc );
}


10 changes: 10 additions & 0 deletions src/core/qgslayerdefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ class CORE_EXPORT QgsLayerDefinition
*/
DependencySorter( QDomDocument doc );

/** Constructor
* @param fileName The filename where the XML document is stored
*/
DependencySorter( const QString& fileName );

/** Get the layer nodes in an order where they can be loaded incrementally without dependency break */
QVector<QDomNode> sortedLayerNodes() const { return mSortedLayerNodes; }

/** Get the layer IDs in an order where they can be loaded incrementally without dependency break */
QStringList sortedLayerIds() const { return mSortedLayerIds; }

/** Whether some cyclic dependency has been detected */
bool hasCycle() const { return mHasCycle; }

Expand All @@ -44,8 +52,10 @@ class CORE_EXPORT QgsLayerDefinition

private:
QVector<QDomNode> mSortedLayerNodes;
QStringList mSortedLayerIds;
bool mHasCycle;
bool mHasMissingDependency;
void init( QDomDocument doc );
};
};

Expand Down

0 comments on commit 7e5915e

Please sign in to comment.