Skip to content
Permalink
Browse files
Added helper namespace QgsLayerTree
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent ebd8aea commit 56ee3f4040941b2545aaed2e85d8de20db75ba4e
@@ -135,9 +135,9 @@
#include "qgsgpsinformationwidget.h"
#include "qgsguivectorlayertools.h"
#include "qgslabelinggui.h"
#include "qgslayertree.h"
#include "qgslayertreemapcanvasbridge.h"
#include "qgslayertreemodel.h"
#include "qgslayertreenode.h"
#include "qgslayertreeregistrybridge.h"
#include "qgslayertreeutils.h"
#include "qgslayertreeview.h"
@@ -2251,8 +2251,8 @@ void QgisApp::layerTreeViewCurrentChanged(const QModelIndex& current, const QMod
if (QgsLayerTreeNode* currentNode = mLayerTreeView->currentNode())
{
QgsLayerTreeNode* parentNode = currentNode->parent();
if (parentNode && parentNode->nodeType() == QgsLayerTreeNode::NodeGroup)
parentGroup = static_cast<QgsLayerTreeGroup*>(parentNode);
if (QgsLayerTree::isGroup(parentNode))
parentGroup = QgsLayerTree::toGroup(parentNode);
}

index = current.row();
@@ -6770,17 +6770,17 @@ void QgisApp::setLayerCRS()

foreach ( QgsLayerTreeNode* node, mLayerTreeView->selectedNodes() )
{
if ( node->nodeType() == QgsLayerTreeNode::NodeGroup )
if ( QgsLayerTree::isGroup( node ) )
{
foreach ( QgsLayerTreeLayer* child, static_cast<QgsLayerTreeGroup*>( node )->findLayers() )
foreach ( QgsLayerTreeLayer* child, QgsLayerTree::toGroup( node )->findLayers() )
{
if ( child->layer() )
child->layer()->setCrs( crs );
}
}
else if ( node->nodeType() == QgsLayerTreeNode::NodeLayer )
else if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer* nodeLayer = static_cast<QgsLayerTreeLayer*>( node );
QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
if (nodeLayer->layer())
nodeLayer->layer()->setCrs( crs );
}
@@ -4,8 +4,8 @@
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsclipboard.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreenode.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsproject.h"
#include "qgsrasterlayer.h"
@@ -38,7 +38,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
else if (QgsLayerTreeNode* node = mView->layerTreeModel()->index2node(idx))
{
// layer or group selected
if (node->nodeType() == QgsLayerTreeNode::NodeGroup)
if (QgsLayerTree::isGroup(node))
{
menu->addAction( actions->actionZoomToGroup(mCanvas, menu) );
menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
@@ -53,9 +53,9 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()

menu->addAction( actions->actionAddGroup(menu) );
}
else if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
else if (QgsLayerTree::isLayer(node))
{
QgsMapLayer* layer = static_cast<QgsLayerTreeLayer*>(node)->layer();
QgsMapLayer* layer = QgsLayerTree::toLayer(node)->layer();

menu->addAction( actions->actionZoomToLayer(mCanvas, menu) );
menu->addAction( actions->actionShowInOverview(menu) );
@@ -17,8 +17,8 @@
#include "qgisapp.h"
#include "qgsapplication.h"

#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreenode.h"
#include "qgslayertreeutils.h"

#include <QDomDocument>
@@ -64,8 +64,8 @@ QStringList QgsProjectLayerGroupDialog::selectedGroups() const
foreach( QModelIndex index, mTreeView->selectionModel()->selectedIndexes() )
{
QgsLayerTreeNode* node = model->index2node( index );
if ( node->nodeType() == QgsLayerTreeNode::NodeGroup )
groups << static_cast<QgsLayerTreeGroup*>( node )->name();
if ( QgsLayerTree::isGroup(node) )
groups << QgsLayerTree::toGroup( node )->name();
}
return groups;
}
@@ -77,8 +77,8 @@ QStringList QgsProjectLayerGroupDialog::selectedLayerIds() const
foreach( QModelIndex index, mTreeView->selectionModel()->selectedIndexes() )
{
QgsLayerTreeNode* node = model->index2node( index );
if ( node->nodeType() == QgsLayerTreeNode::NodeLayer )
layerIds << static_cast<QgsLayerTreeLayer*>( node )->layerId();
if ( QgsLayerTree::isLayer(node) )
layerIds << QgsLayerTree::toLayer( node )->layerId();
}
return layerIds;
}
@@ -90,8 +90,8 @@ QStringList QgsProjectLayerGroupDialog::selectedLayerNames() const
foreach( QModelIndex index, mTreeView->selectionModel()->selectedIndexes() )
{
QgsLayerTreeNode* node = model->index2node( index );
if ( node->nodeType() == QgsLayerTreeNode::NodeLayer )
layerNames << static_cast<QgsLayerTreeLayer*>( node )->layerName();
if ( QgsLayerTree::isLayer(node) )
layerNames << QgsLayerTree::toLayer( node )->layerName();
}
return layerNames;
}
@@ -188,8 +188,8 @@ void QgsProjectLayerGroupDialog::removeEmbeddedNodes( QgsLayerTreeGroup* node )
{
if ( child->customProperty("embedded").toBool() )
childrenToRemove << child;
else if ( child->nodeType() == QgsLayerTreeNode::NodeGroup )
removeEmbeddedNodes( static_cast<QgsLayerTreeGroup*>( child ) );
else if ( QgsLayerTree::isGroup(child) )
removeEmbeddedNodes( QgsLayerTree::toGroup( child ) );
}
foreach ( QgsLayerTreeNode* childToRemove, childrenToRemove )
node->removeChildNode( childToRemove );
@@ -0,0 +1,28 @@
#ifndef QGSLAYERTREE_H
#define QGSLAYERTREE_H

#include "qgslayertreenode.h"

namespace QgsLayerTree
{
inline bool isGroup(QgsLayerTreeNode* node)
{
return node && node->nodeType() == QgsLayerTreeNode::NodeGroup;
}
inline bool isLayer(QgsLayerTreeNode* node)
{
return node && node->nodeType() == QgsLayerTreeNode::NodeLayer;
}

inline QgsLayerTreeGroup* toGroup(QgsLayerTreeNode* node)
{
return static_cast<QgsLayerTreeGroup*>(node);
}
inline QgsLayerTreeLayer* toLayer(QgsLayerTreeNode* node)
{
return static_cast<QgsLayerTreeLayer*>(node);
}

}

#endif // QGSLAYERTREE_H
@@ -2,6 +2,7 @@

#include "qgsmaplayerregistry.h"

#include "qgslayertree.h"
#include "qgslayertreeutils.h"


@@ -163,10 +164,10 @@ QgsLayerTreeLayer* QgsLayerTreeGroup::addLayer(QgsMapLayer* layer)

void QgsLayerTreeGroup::connectToChildNode(QgsLayerTreeNode* node)
{
if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
if (QgsLayerTree::isLayer(node))
{
// TODO: this could be handled directly by LayerTreeLayer by listening to QgsMapLayerRegistry...
//QgsLayerTreeLayer* nodeLayer = static_cast<QgsLayerTreeLayer*>(node);
//QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer(node);
//connect(nodeLayer->layer(), SIGNAL(destroyed()), this, SLOT(layerDestroyed()));
}

@@ -209,9 +210,9 @@ void QgsLayerTreeGroup::removeLayer(QgsMapLayer* layer)
{
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == QgsLayerTreeNode::NodeLayer)
if (QgsLayerTree::isLayer(child))
{
QgsLayerTreeLayer* childLayer = static_cast<QgsLayerTreeLayer*>(child);
QgsLayerTreeLayer* childLayer = QgsLayerTree::toLayer(child);
if (childLayer->layer() == layer)
{
removeChildAt(mChildren.indexOf(child));
@@ -237,15 +238,15 @@ QgsLayerTreeLayer *QgsLayerTreeGroup::findLayer(const QString& layerId)
{
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == QgsLayerTreeNode::NodeLayer)
if (QgsLayerTree::isLayer(child))
{
QgsLayerTreeLayer* childLayer = static_cast<QgsLayerTreeLayer*>(child);
QgsLayerTreeLayer* childLayer = QgsLayerTree::toLayer(child);
if (childLayer->layerId() == layerId)
return childLayer;
}
else if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
else if (QgsLayerTree::isGroup(child))
{
QgsLayerTreeLayer* res = static_cast<QgsLayerTreeGroup*>(child)->findLayer(layerId);
QgsLayerTreeLayer* res = QgsLayerTree::toGroup(child)->findLayer(layerId);
if (res)
return res;
}
@@ -258,10 +259,10 @@ QList<QgsLayerTreeLayer*> QgsLayerTreeGroup::findLayers() const
QList<QgsLayerTreeLayer*> list;
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == QgsLayerTreeNode::NodeLayer)
list << static_cast<QgsLayerTreeLayer*>(child);
else if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
list << static_cast<QgsLayerTreeGroup*>(child)->findLayers();
if (QgsLayerTree::isLayer(child))
list << QgsLayerTree::toLayer(child);
else if (QgsLayerTree::isGroup(child))
list << QgsLayerTree::toGroup(child)->findLayers();
}
return list;
}
@@ -270,9 +271,9 @@ QgsLayerTreeGroup* QgsLayerTreeGroup::findGroup(const QString& name)
{
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
if (QgsLayerTree::isGroup(child))
{
QgsLayerTreeGroup* childGroup = static_cast<QgsLayerTreeGroup*>(child);
QgsLayerTreeGroup* childGroup = QgsLayerTree::toGroup(child);
if (childGroup->name() == name)
return childGroup;
else
@@ -365,10 +366,10 @@ void QgsLayerTreeGroup::setVisible(Qt::CheckState state)
// update children to have the correct visibility
foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == NodeGroup)
static_cast<QgsLayerTreeGroup*>(child)->setVisible(mChecked);
else if (child->nodeType() == NodeLayer)
static_cast<QgsLayerTreeLayer*>(child)->setVisible(mChecked == Qt::Checked);
if (QgsLayerTree::isGroup(child))
QgsLayerTree::toGroup(child)->setVisible(mChecked);
else if (QgsLayerTree::isLayer(child))
QgsLayerTree::toLayer(child)->setVisible(mChecked == Qt::Checked);
}

mChangingChildVisibility = false;
@@ -380,10 +381,10 @@ 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();
if (QgsLayerTree::isGroup(child))
lst << QgsLayerTree::toGroup(child)->childLayerIds();
else if (QgsLayerTree::isLayer(child))
lst << QgsLayerTree::toLayer(child)->layerId();
}
return lst;
}
@@ -407,15 +408,15 @@ void QgsLayerTreeGroup::updateVisibilityFromChildren()

foreach (QgsLayerTreeNode* child, mChildren)
{
if (child->nodeType() == QgsLayerTreeNode::NodeLayer)
if (QgsLayerTree::isLayer(child))
{
bool layerVisible = static_cast<QgsLayerTreeLayer*>(child)->isVisible();
bool layerVisible = QgsLayerTree::toLayer(child)->isVisible();
if (layerVisible) hasVisible = true;
if (!layerVisible) hasHidden = true;
}
else if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
else if (QgsLayerTree::isGroup(child))
{
Qt::CheckState state = static_cast<QgsLayerTreeGroup*>(child)->isVisible();
Qt::CheckState state = QgsLayerTree::toGroup(child)->isVisible();
if (state == Qt::Checked || state == Qt::PartiallyChecked) hasVisible = true;
if (state == Qt::Unchecked || state == Qt::PartiallyChecked) hasHidden = true;
}
@@ -2,7 +2,7 @@

#include "qgsmaplayerregistry.h"

#include "qgslayertreenode.h"
#include "qgslayertree.h"

#include "qgsproject.h"

@@ -67,8 +67,8 @@ void QgsLayerTreeRegistryBridge::groupAddedChildren(int indexFrom, int indexTo)
for (int i = indexFrom; i <= indexTo; ++i)
{
QgsLayerTreeNode* child = group->children()[i];
if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
connectToGroup(static_cast<QgsLayerTreeGroup*>(child));
if (QgsLayerTree::isGroup(child))
connectToGroup(QgsLayerTree::toGroup(child));
}
}

@@ -77,13 +77,13 @@ static void _collectLayerIdsInGroup(QgsLayerTreeGroup* group, int indexFrom, int
for (int i = indexFrom; i <= indexTo; ++i)
{
QgsLayerTreeNode* child = group->children()[i];
if (child->nodeType() == QgsLayerTreeNode::NodeLayer)
if (QgsLayerTree::isLayer(child))
{
lst << static_cast<QgsLayerTreeLayer*>(child)->layerId();
lst << QgsLayerTree::toLayer(child)->layerId();
}
else if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
else if (QgsLayerTree::isGroup(child))
{
_collectLayerIdsInGroup(static_cast<QgsLayerTreeGroup*>(child), 0, child->children().count()-1, lst);
_collectLayerIdsInGroup(QgsLayerTree::toGroup(child), 0, child->children().count()-1, lst);
}
}
}
@@ -119,8 +119,8 @@ void QgsLayerTreeRegistryBridge::connectToGroup(QgsLayerTreeGroup* group)

foreach (QgsLayerTreeNode* child, group->children())
{
if (child->nodeType() == QgsLayerTreeNode::NodeGroup)
connectToGroup(static_cast<QgsLayerTreeGroup*>(child));
if (QgsLayerTree::isGroup(child))
connectToGroup(QgsLayerTree::toGroup(child));
}
}

@@ -22,7 +22,7 @@

#include "qgsdatasourceuri.h"
#include "qgsexception.h"
#include "qgslayertreenode.h"
#include "qgslayertree.h"
#include "qgslayertreeutils.h"
#include "qgslayertreeregistrybridge.h"
#include "qgslogger.h"
@@ -1768,7 +1768,7 @@ bool QgsProject::createEmbeddedGroup( const QString& groupName, const QString& p
}

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

@@ -1804,16 +1804,16 @@ void QgsProject::initializeEmbeddedSubtree( const QString& projectFilePath, QgsL
// all nodes in the subtree will have "embedded" custom property set
child->setCustomProperty( "embedded", true );

if ( child->nodeType() == QgsLayerTreeNode::NodeGroup )
if ( QgsLayerTree::isGroup( child ) )
{
initializeEmbeddedSubtree( projectFilePath, static_cast<QgsLayerTreeGroup*>( child ) );
initializeEmbeddedSubtree( projectFilePath, QgsLayerTree::toGroup( child ) );
}
else if ( child->nodeType() == QgsLayerTreeNode::NodeLayer )
else if ( QgsLayerTree::isLayer( child ) )
{
// load the layer into our project
QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
createEmbeddedLayer( static_cast<QgsLayerTreeLayer*>( child )->layerId(), projectFilePath, brokenNodes, vectorLayerList, false );
createEmbeddedLayer( QgsLayerTree::toLayer( child )->layerId(), projectFilePath, brokenNodes, vectorLayerList, false );
}
}
}

0 comments on commit 56ee3f4

Please sign in to comment.