Skip to content
Permalink
Browse files
Add support for embedded layers and groups into layer tree
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 2972768 commit c3315c419472ecb9efaa78fdb1f1011a27a15728
@@ -7320,7 +7320,7 @@ void QgisApp::embedLayers()
QStringList::const_iterator groupIt = groups.constBegin();
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
mMapLegend->addEmbeddedGroup( *groupIt, projectFile );
QgsProject::instance()->createEmbeddedGroup( *groupIt, projectFile );
}

//layer ids
@@ -5,6 +5,26 @@
#include "qgslayertreeutils.h"


QgsLayerTreeNode::QgsLayerTreeNode(QgsLayerTreeNode::NodeType t)
: mNodeType(t)
, mParent(0)
, mExpanded(true)
{
}

QgsLayerTreeNode::QgsLayerTreeNode(const QgsLayerTreeNode& other)
: QObject()
, mNodeType(other.mNodeType)
, mParent(0)
, mExpanded(other.mExpanded)
, mProperties(other.mProperties)
{
QList<QgsLayerTreeNode*> clonedChildren;
foreach (QgsLayerTreeNode* child, other.mChildren)
clonedChildren << child->clone();
insertChildren(-1, clonedChildren);
}

QgsLayerTreeNode* QgsLayerTreeNode::readXML(QDomElement& element)
{
QgsLayerTreeNode* node = 0;
@@ -109,6 +129,14 @@ QgsLayerTreeGroup::QgsLayerTreeGroup(const QString& name, Qt::CheckState checked
{
}

QgsLayerTreeGroup::QgsLayerTreeGroup(const QgsLayerTreeGroup& other)
: QgsLayerTreeNode(other)
, mName(other.mName)
, mChecked(other.mChecked)
, mChangingChildVisibility(false)
{
}


QgsLayerTreeGroup* QgsLayerTreeGroup::addGroup(const QString &name)
{
@@ -214,6 +242,26 @@ QgsLayerTreeLayer *QgsLayerTreeGroup::findLayer(const QString& layerId)
return 0;
}

QgsLayerTreeGroup* QgsLayerTreeGroup::findGroup(const QString& name)
{
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
{
QgsLayerTreeGroup* childGroup = static_cast<QgsLayerTreeGroup*>(child);
if (childGroup->name() == name)
return childGroup;
else
{
QgsLayerTreeGroup* grp = childGroup->findGroup(name);
if (grp)
return grp;
}
}
}
return 0;
}

QgsLayerTreeGroup* QgsLayerTreeGroup::readXML(QDomElement& element)
{
if (element.tagName() != "layer-tree-group")
@@ -273,6 +321,11 @@ QString QgsLayerTreeGroup::dump() const
return header + childrenDump.join( "\n" );
}

QgsLayerTreeNode* QgsLayerTreeGroup::clone() const
{
return new QgsLayerTreeGroup(*this);
}

void QgsLayerTreeGroup::setVisible(Qt::CheckState state)
{
if (mChecked == state)
@@ -298,6 +351,19 @@ void QgsLayerTreeGroup::setVisible(Qt::CheckState state)
}
}

QStringList QgsLayerTreeGroup::childLayerIds() const
{
QStringList lst;
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == NodeGroup)
lst << static_cast<QgsLayerTreeGroup*>(child)->childLayerIds();
else if (child->nodeType() == NodeLayer)
lst << static_cast<QgsLayerTreeLayer*>(child)->layerId();
}
return lst;
}


void QgsLayerTreeGroup::layerDestroyed()
{
@@ -353,18 +419,38 @@ QgsLayerTreeLayer::QgsLayerTreeLayer(QgsMapLayer *layer)
}

QgsLayerTreeLayer::QgsLayerTreeLayer(QString layerId, QString name)
: QgsLayerTreeNode(NodeLayer), mLayerId(layerId), mLayer(0), mVisible(true)
: QgsLayerTreeNode(NodeLayer)
, mLayerId(layerId)
, mLayerName(name)
, mLayer(0)
, mVisible(true)
{
attachToLayer();
}

QgsLayerTreeLayer::QgsLayerTreeLayer(const QgsLayerTreeLayer& other)
: QgsLayerTreeNode(other)
, mLayerId(other.mLayerId)
, mLayerName(other.mLayerName)
, mLayer(0)
, mVisible(other.mVisible)
{
attachToLayer();
}

void QgsLayerTreeLayer::attachToLayer()
{
// layer is not necessarily already loaded
QgsMapLayer* l = QgsMapLayerRegistry::instance()->mapLayer(layerId);
QgsMapLayer* l = QgsMapLayerRegistry::instance()->mapLayer(mLayerId);
if (l)
{
mLayer = l;
mLayerName = l->name();
}
else
{
mLayerName = name.isEmpty() ? "(?)" : name;
if (mLayerName.isEmpty())
mLayerName = "(?)";
// wait for the layer to be eventually loaded
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersAdded(QList<QgsMapLayer*>)), this, SLOT(registryLayersAdded(QList<QgsMapLayer*>)));
}
@@ -425,6 +511,11 @@ QString QgsLayerTreeLayer::dump() const
return QString( "LAYER: %1 visible=%2 expanded=%3 id=%4\n" ).arg( layerName() ).arg( mVisible ).arg( mExpanded ).arg( layerId() );
}

QgsLayerTreeNode* QgsLayerTreeLayer::clone() const
{
return new QgsLayerTreeLayer(*this);
}

void QgsLayerTreeLayer::registryLayersAdded(QList<QgsMapLayer*> layers)
{
foreach (QgsMapLayer* l, layers)
@@ -20,7 +20,6 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject
NodeLayer
};

QgsLayerTreeNode(NodeType t): mNodeType(t), mParent(0), mExpanded(true) {}
~QgsLayerTreeNode() { qDeleteAll(mChildren); }

NodeType nodeType() { return mNodeType; }
@@ -32,6 +31,8 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject

virtual QString dump() const = 0;

virtual QgsLayerTreeNode* clone() const = 0;

bool isExpanded() const { return mExpanded; }
void setExpanded(bool expanded) { mExpanded = expanded; }

@@ -58,6 +59,9 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject

protected:

QgsLayerTreeNode(NodeType t);
QgsLayerTreeNode(const QgsLayerTreeNode& other);

// low-level utility functions

void readCommonXML(QDomElement& element);
@@ -95,6 +99,7 @@ class QgsLayerTreeLayer : public QgsLayerTreeNode
Q_OBJECT
public:
explicit QgsLayerTreeLayer(QgsMapLayer* layer);
QgsLayerTreeLayer(const QgsLayerTreeLayer& other);

explicit QgsLayerTreeLayer(QString layerId, QString name = QString());

@@ -113,10 +118,13 @@ class QgsLayerTreeLayer : public QgsLayerTreeNode

virtual QString dump() const;

virtual QgsLayerTreeNode* clone() const;

protected slots:
void registryLayersAdded(QList<QgsMapLayer*> layers);

protected:
void attachToLayer();

QString mLayerId;
QString mLayerName; // only used if layer does not exist
@@ -130,6 +138,7 @@ class QgsLayerTreeGroup : public QgsLayerTreeNode
Q_OBJECT
public:
QgsLayerTreeGroup(const QString& name = QString(), Qt::CheckState checked = Qt::Checked);
QgsLayerTreeGroup(const QgsLayerTreeGroup& other);

QString name() const { return mName; }
void setName(const QString& n) { mName = n; }
@@ -150,6 +159,7 @@ class QgsLayerTreeGroup : public QgsLayerTreeNode
void removeAllChildren();

QgsLayerTreeLayer* findLayer(const QString& layerId);
QgsLayerTreeGroup* findGroup(const QString& name);

static QgsLayerTreeGroup* readXML(QDomElement& element);
virtual void writeXML(QDomElement& parentElement);
@@ -158,9 +168,13 @@ class QgsLayerTreeGroup : public QgsLayerTreeNode

virtual QString dump() const;

virtual QgsLayerTreeNode* clone() const;

Qt::CheckState isVisible() const { return mChecked; }
void setVisible(Qt::CheckState state);

QStringList childLayerIds() const;

protected slots:
void layerDestroyed();
void updateVisibilityFromChildren();
@@ -4,6 +4,8 @@

#include "qgslayertreenode.h"

#include "qgsproject.h"

QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root, QObject *parent)
: QObject(parent)
, mRoot(root)
@@ -22,7 +24,15 @@ void QgsLayerTreeRegistryBridge::layersAdded(QList<QgsMapLayer*> layers)

foreach (QgsMapLayer* layer, layers)
{
mRoot->addLayer(layer);
QgsLayerTreeLayer* nodeLayer = mRoot->addLayer(layer);

// check whether the layer is marked as embedded
QString projectFile = QgsProject::instance()->layerIsEmbedded(nodeLayer->layerId());
if (!projectFile.isEmpty())
{
nodeLayer->setCustomProperty("embedded", true);
nodeLayer->setCustomProperty("embedded_project", projectFile);
}
}
}

@@ -53,13 +53,17 @@ void QgsLayerTreeUtils::addLegendGroupToTreeWidget( const QDomElement& groupElem
{
QDomNodeList groupChildren = groupElem.childNodes();

// TODO: embedded
//if ( !mShowEmbeddedContent && groupElem.attribute( "embedded" ) == "1" )
// return;

QgsLayerTreeGroup* groupNode = new QgsLayerTreeGroup(groupElem.attribute( "name" ));
parent->addChildNode(groupNode);

groupNode->setVisible(checkStateFromXml(groupElem.attribute("checked")));

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

for ( int i = 0; i < groupChildren.size(); ++i )
{
QDomElement currentChildElem = groupChildren.at( i ).toElement();
@@ -76,11 +80,15 @@ void QgsLayerTreeUtils::addLegendGroupToTreeWidget( const QDomElement& groupElem

void QgsLayerTreeUtils::addLegendLayerToTreeWidget( const QDomElement& layerElem, QgsLayerTreeGroup* parent )
{
// TODO: embedded
//if ( !mShowEmbeddedContent && layerElem.attribute( "embedded" ) == "1" )
// return;

QString layerId = layerElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" );
QgsLayerTreeLayer* layerNode = new QgsLayerTreeLayer(layerId, layerElem.attribute( "name" ) );

layerNode->setVisible(checkStateFromXml(layerElem.attribute("checked")));

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

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

parent->addChildNode(layerNode);
}

0 comments on commit c3315c4

Please sign in to comment.