Skip to content
Permalink
Browse files

Allow to embed layers with dependencies

  • Loading branch information
mhugo committed Jan 7, 2016
1 parent 1a1af37 commit 7e5915ec793098a367667b8876dadd4ac825d66a
Showing with 53 additions and 6 deletions.
  1. +8 −0 python/core/qgslayerdefinition.sip
  2. +10 −3 src/app/qgisapp.cpp
  3. +25 −3 src/core/qgslayerdefinition.cpp
  4. +10 −0 src/core/qgslayerdefinition.h
@@ -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;

@@ -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 );
@@ -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++ )
@@ -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" );
@@ -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;
}
}
@@ -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;
}
@@ -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 );
}


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

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

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

0 comments on commit 7e5915e

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