Skip to content

Commit

Permalink
Add support for embedded layers and groups into layer tree
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 2972768 commit c3315c4
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 94 additions & 3 deletions src/core/layertree/qgslayertreenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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()
{
Expand Down Expand Up @@ -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*>)));
}
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion src/core/layertree/qgslayertreenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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; }

Expand All @@ -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);
Expand Down Expand Up @@ -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());

Expand All @@ -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
Expand All @@ -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; }
Expand All @@ -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);
Expand All @@ -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();
Expand Down
12 changes: 11 additions & 1 deletion src/core/layertree/qgslayertreeregistrybridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "qgslayertreenode.h"

#include "qgsproject.h"

QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root, QObject *parent)
: QObject(parent)
, mRoot(root)
Expand All @@ -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);
}
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/core/layertree/qgslayertreeutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Loading

0 comments on commit c3315c4

Please sign in to comment.