Skip to content
Permalink
Browse files
Loading of embedded groups and layer from project file
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent f83a11f commit 0e8b79f4460d0cb520e4fa52183b222febbfc1a3
@@ -7512,7 +7512,10 @@ void QgisApp::embedLayers()
QStringList::const_iterator groupIt = groups.constBegin();
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
QgsProject::instance()->createEmbeddedGroup( *groupIt, projectFile );
QgsLayerTreeGroup* newGroup = QgsProject::instance()->createEmbeddedGroup( *groupIt, projectFile );

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

//layer ids
@@ -60,7 +60,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
menu->addAction( actions->actionZoomToLayer(mCanvas, menu) );
menu->addAction( actions->actionShowInOverview(menu) );

if ( layer->type() == QgsMapLayer::RasterLayer )
if ( layer && layer->type() == QgsMapLayer::RasterLayer )
{
menu->addAction( tr( "&Zoom to Best Scale (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) );

@@ -82,7 +82,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()

menu->addSeparator();

if ( layer->type() == QgsMapLayer::VectorLayer )
if ( layer && layer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer );

@@ -127,12 +127,12 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()

menu->addSeparator();
}
else if ( layer->type() == QgsMapLayer::RasterLayer )
else if ( layer && layer->type() == QgsMapLayer::RasterLayer )
{
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsRasterFile() ) );
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
}
else if ( layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 )
else if ( layer && layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 )
{
// disable duplication of plugin layers
duplicateLayersAction->setEnabled( false );
@@ -186,7 +186,7 @@ void QgsProjectLayerGroupDialog::removeEmbeddedNodes( QgsLayerTreeGroup* node )
QList<QgsLayerTreeNode*> childrenToRemove;
foreach ( QgsLayerTreeNode* child, node->children() )
{
if ( child->customProperty("embedded").toBool() )
if ( child->customProperty("embedded").toInt() )
childrenToRemove << child;
else if ( QgsLayerTree::isGroup(child) )
removeEmbeddedNodes( QgsLayerTree::toGroup( child ) );
@@ -40,7 +40,7 @@ void QgsLayerTreeRegistryBridge::layersAdded(QList<QgsMapLayer*> layers)
QString projectFile = QgsProject::instance()->layerIsEmbedded(nodeLayer->layerId());
if (!projectFile.isEmpty())
{
nodeLayer->setCustomProperty("embedded", true);
nodeLayer->setCustomProperty("embedded", 1);
nodeLayer->setCustomProperty("embedded_project", projectFile);
}
}
@@ -62,7 +62,7 @@ void QgsLayerTreeUtils::addLegendGroupToTreeWidget( const QDomElement& groupElem

if (groupElem.attribute("embedded") == "1")
{
groupNode->setCustomProperty("embedded", true);
groupNode->setCustomProperty("embedded", 1);
groupNode->setCustomProperty("embedded_project", groupElem.attribute("project"));
}

@@ -88,7 +88,7 @@ void QgsLayerTreeUtils::addLegendLayerToTreeWidget( const QDomElement& layerElem
layerNode->setVisible(checkStateFromXml(layerElem.attribute("checked")));

if (layerElem.attribute("embedded") == "1")
layerNode->setCustomProperty("embedded", true);
layerNode->setCustomProperty("embedded", 1);

// TODO: is in overview, drawing order, show feature count

@@ -939,7 +939,8 @@ bool QgsProject::read()

mLayerTreeRegistryBridge->setEnabled( true );

// TODO: load embedded groups / layers
// load embedded groups and layers
loadEmbeddedNodes(mRootGroup);

// read the project: used by map canvas and legend
emit readProject( *doc );
@@ -953,6 +954,57 @@ bool QgsProject::read()
} // QgsProject::read


void QgsProject::loadEmbeddedNodes(QgsLayerTreeGroup* group)
{
foreach (QgsLayerTreeNode* child, group->children())
{
if (QgsLayerTree::isGroup(child))
{
QgsLayerTreeGroup* childGroup = QgsLayerTree::toGroup(child);
if (childGroup->customProperty("embedded").toInt())
{
QgsLayerTreeGroup* newGroup = createEmbeddedGroup(childGroup->name(), childGroup->customProperty("embedded_project").toString());
if (newGroup)
{
QList<QgsLayerTreeNode*> clonedChildren;
foreach (QgsLayerTreeNode* newGroupChild, newGroup->children())
clonedChildren << newGroupChild->clone();
delete newGroup;

childGroup->insertChildNodes(0, clonedChildren);
}
}
else
{
loadEmbeddedNodes(childGroup);
}
}
else if (QgsLayerTree::isLayer(child))
{
if (child->customProperty("embedded").toInt())
{
QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
createEmbeddedLayer(QgsLayerTree::toLayer(child)->layerId(), child->customProperty("embedded_project").toString(), brokenNodes, vectorLayerList);
}
}

}
}

void QgsProject::removeChildrenOfEmbeddedGroups(QgsLayerTreeGroup* group)
{
foreach (QgsLayerTreeNode* child, group->children())
{
if (QgsLayerTree::isGroup(child))
{
if (child->customProperty("embedded").toInt())
QgsLayerTree::toGroup(child)->removeAllChildren();
else
removeChildrenOfEmbeddedGroups(QgsLayerTree::toGroup(child));
}
}
}



@@ -1033,8 +1085,11 @@ bool QgsProject::write()
QDomText titleText = doc->createTextNode( title() ); // XXX why have title TWICE?
titleNode.appendChild( titleText );

// write layer tree
mRootGroup->writeXML( qgisNode );
// write layer tree - make sure it is without embedded subgroups
QgsLayerTreeNode* clonedRoot = mRootGroup->clone();
removeChildrenOfEmbeddedGroups(QgsLayerTree::toGroup(clonedRoot));
clonedRoot->writeXML( qgisNode );
delete clonedRoot;

// let map canvas and legend write their information
emit writeProject( *doc );
@@ -1720,19 +1775,19 @@ bool QgsProject::createEmbeddedLayer( const QString& layerId, const QString& pro
}


bool QgsProject::createEmbeddedGroup( const QString& groupName, const QString& projectFilePath )
QgsLayerTreeGroup* QgsProject::createEmbeddedGroup( const QString& groupName, const QString& projectFilePath )
{
//open project file, get layer ids in group, add the layers
QFile projectFile( projectFilePath );
if ( !projectFile.open( QIODevice::ReadOnly ) )
{
return false;
return 0;
}

QDomDocument projectDocument;
if ( !projectDocument.setContent( &projectFile ) )
{
return false;
return 0;
}

//store identify disabled layers of the embedded project
@@ -1764,15 +1819,15 @@ bool QgsProject::createEmbeddedGroup( const QString& groupName, const QString& p
{
// embedded groups cannot be embedded again
delete root;
return false;
return 0;
}

// clone the group sub-tree (it is used already in a tree, we cannot just tear it off)
QgsLayerTreeGroup* newGroup = QgsLayerTree::toGroup( group->clone() );
delete root;
root = 0;

newGroup->setCustomProperty( "embedded", true );
newGroup->setCustomProperty( "embedded", 1 );
newGroup->setCustomProperty( "embedded_project", projectFilePath );

// set "embedded" to all children + load embedded layers
@@ -1791,18 +1846,15 @@ bool QgsProject::createEmbeddedGroup( const QString& groupName, const QString& p
}
}

// add to the project
mRootGroup->addChildNode( newGroup );

return true;
return newGroup;
}

void QgsProject::initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group )
{
foreach ( QgsLayerTreeNode* child, group->children() )
{
// all nodes in the subtree will have "embedded" custom property set
child->setCustomProperty( "embedded", true );
child->setCustomProperty( "embedded", 1 );

if ( QgsLayerTree::isGroup( child ) )
{
@@ -305,7 +305,7 @@ class CORE_EXPORT QgsProject : public QObject
/** Create layer group instance defined in an arbitrary project file.
* @note: added in version 2.4
*/
bool createEmbeddedGroup( const QString& groupName, const QString& projectFilePath );
QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath );

/** Convenience function to set snap settings per layer
@note added in version 1.9*/
@@ -355,6 +355,10 @@ class CORE_EXPORT QgsProject : public QObject

void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );

void loadEmbeddedNodes(QgsLayerTreeGroup* group);

void removeChildrenOfEmbeddedGroups(QgsLayerTreeGroup* group);

signals:
//! emitted when project is being read
void readProject( const QDomDocument & );
@@ -209,7 +209,7 @@ QVariant QgsLayerTreeModel::data(const QModelIndex &index, int role) const
else if ( role == Qt::FontRole )
{
QFont f;
if (node->customProperty("embedded", false).toBool())
if (node->customProperty("embedded").toInt())
f.setItalic(true);
if (QgsLayerTree::isLayer(node))
f.setBold(true);

0 comments on commit 0e8b79f

Please sign in to comment.