Skip to content
Permalink
Browse files
Forward the visibilityChanged() signal towards the parent node.
Listening to the signal is simpler as it is enough to listen to the parent
without having to connect to the whole tree and also follow the changes
withing the tree
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 1a61c15 commit bc49cd47b7e04fb4af55b32b70ce4fb5143426cb
@@ -9,6 +9,7 @@ QgsLayerTreeGroup::QgsLayerTreeGroup(const QString& name, Qt::CheckState checked
, mChecked(checked)
, mChangingChildVisibility(false)
{
connect(this, SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)), this, SLOT(nodeVisibilityChanged(QgsLayerTreeNode*)));
}

QgsLayerTreeGroup::QgsLayerTreeGroup(const QgsLayerTreeGroup& other)
@@ -17,6 +18,7 @@ QgsLayerTreeGroup::QgsLayerTreeGroup(const QgsLayerTreeGroup& other)
, mChecked(other.mChecked)
, mChangingChildVisibility(false)
{
connect(this, SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)), this, SLOT(nodeVisibilityChanged(QgsLayerTreeNode*)));
}


@@ -41,18 +43,6 @@ QgsLayerTreeLayer* QgsLayerTreeGroup::addLayer(QgsMapLayer* layer)
return ll;
}

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

connect(node, SIGNAL(visibilityChanged(Qt::CheckState)), this, SLOT(updateVisibilityFromChildren()));
}

void QgsLayerTreeGroup::insertChildNode(int index, QgsLayerTreeNode* node)
{
QList<QgsLayerTreeNode*> nodes;
@@ -65,9 +55,6 @@ void QgsLayerTreeGroup::insertChildNodes(int index, QList<QgsLayerTreeNode*> nod
// low-level insert
insertChildren(index, nodes);

foreach (QgsLayerTreeNode* node, nodes)
connectToChildNode(node);

updateVisibilityFromChildren();
}

@@ -234,7 +221,7 @@ void QgsLayerTreeGroup::setVisible(Qt::CheckState state)
return;

mChecked = state;
emit visibilityChanged(state);
emit visibilityChanged(this, state);

if (mChecked == Qt::Unchecked || mChecked == Qt::Checked)
{
@@ -273,6 +260,12 @@ void QgsLayerTreeGroup::layerDestroyed()
removeLayer(layer);
}

void QgsLayerTreeGroup::nodeVisibilityChanged(QgsLayerTreeNode* node)
{
if (mChildren.indexOf(node) != -1)
updateVisibilityFromChildren();
}

void QgsLayerTreeGroup::updateVisibilityFromChildren()
{
if (mChangingChildVisibility)
@@ -51,10 +51,10 @@ class QgsLayerTreeGroup : public QgsLayerTreeNode

protected slots:
void layerDestroyed();
void updateVisibilityFromChildren();
void nodeVisibilityChanged(QgsLayerTreeNode* node);

protected:
void connectToChildNode(QgsLayerTreeNode* node);
void updateVisibilityFromChildren();

protected:
QString mName;
@@ -54,7 +54,7 @@ void QgsLayerTreeLayer::setVisible(Qt::CheckState state)
return;

mVisible = state;
emit visibilityChanged(state);
emit visibilityChanged(this, state);
}

QgsLayerTreeLayer* QgsLayerTreeLayer::readXML(QDomElement& element)
@@ -103,6 +103,7 @@ void QgsLayerTreeNode::insertChildren(int index, QList<QgsLayerTreeNode*> nodes)
connect(nodes[i], SIGNAL(willRemoveChildren(QgsLayerTreeNode*,int,int)), this, SIGNAL(willRemoveChildren(QgsLayerTreeNode*,int,int)));
connect(nodes[i], SIGNAL(removedChildren(QgsLayerTreeNode*,int,int)), this, SIGNAL(removedChildren(QgsLayerTreeNode*,int,int)));
connect(nodes[i], SIGNAL(customPropertyChanged(QgsLayerTreeNode*,QString)), this, SIGNAL(customPropertyChanged(QgsLayerTreeNode*,QString)));
connect(nodes[i], SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)), this, SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)));
}
emit addedChildren(this, index, indexTo);
}
@@ -55,7 +55,7 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject
void willRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void removedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);

void visibilityChanged(Qt::CheckState state);
void visibilityChanged(QgsLayerTreeNode* node, Qt::CheckState state);

void customPropertyChanged(QgsLayerTreeNode* node, QString key);

@@ -10,10 +10,10 @@ QgsLayerTreeMapCanvasBridge::QgsLayerTreeMapCanvasBridge(QgsLayerTreeGroup *root
, mPendingCanvasUpdate(false)
, mHasCustomLayerOrder(false)
{
connectToNode(root);
connect(root, SIGNAL(addedChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeAddedChildren(QgsLayerTreeNode*,int,int)));
connect(root, SIGNAL(customPropertyChanged(QgsLayerTreeNode*,QString)), this, SLOT(nodeCustomPropertyChanged(QgsLayerTreeNode*,QString)));
connect(root, SIGNAL(removedChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeRemovedChildren()));
connect(root, SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)), this, SLOT(nodeVisibilityChanged()));

setCanvasLayers();
}
@@ -69,21 +69,6 @@ void QgsLayerTreeMapCanvasBridge::setCustomLayerOrder(const QStringList& order)
deferredSetCanvasLayers();
}

void QgsLayerTreeMapCanvasBridge::connectToNode(QgsLayerTreeNode *node)
{
connect(node, SIGNAL(visibilityChanged(Qt::CheckState)), this, SLOT(nodeVisibilityChanged()));

if (QgsLayerTree::isLayer(node))
{
QString layerId = QgsLayerTree::toLayer(node)->layerId();
if (!mCustomLayerOrder.contains(layerId))
mCustomLayerOrder.append(layerId);
}

foreach (QgsLayerTreeNode* child, node->children())
connectToNode(child);
}

void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
{
QList<QgsMapCanvasLayer> layers;
@@ -128,10 +113,30 @@ void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()

void QgsLayerTreeMapCanvasBridge::nodeAddedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
{
// connect to new children
Q_ASSERT(node);

// collect layer IDs that have been added in order to put them into custom layer order
QStringList layerIds;
QList<QgsLayerTreeNode*> children = node->children();
for (int i = indexFrom; i <= indexTo; ++i)
connectToNode(node->children()[i]);
{
QgsLayerTreeNode* child = children.at(i);
if (QgsLayerTree::isLayer(child))
{
layerIds << QgsLayerTree::toLayer(child)->layerId();
}
else if (QgsLayerTree::isGroup(child))
{
foreach (QgsLayerTreeLayer* nodeL, QgsLayerTree::toGroup(child)->findLayers())
layerIds << nodeL->layerId();
}
}

foreach (QString layerId, layerIds)
{
if (!mCustomLayerOrder.contains(layerId))
mCustomLayerOrder.append(layerId);
}

emit customLayerOrderChanged(mCustomLayerOrder);

@@ -29,9 +29,6 @@ public slots:
void customLayerOrderChanged(const QStringList& order);

protected:
void connectToNode(QgsLayerTreeNode* node);

// TODO: disconnectFromNode

void defaultLayerOrder(QgsLayerTreeNode* node, QStringList& order) const;

@@ -20,13 +20,11 @@ QgsLayerTreeModel::QgsLayerTreeModel(QgsLayerTreeGroup* rootNode, QObject *paren
{
Q_ASSERT(mRootNode);

// connect to all existing nodes
connectToNode(mRootNode);

connect(mRootNode, SIGNAL(willAddChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeWillAddChildren(QgsLayerTreeNode*,int,int)));
connect(mRootNode, SIGNAL(addedChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeAddedChildren(QgsLayerTreeNode*,int,int)));
connect(mRootNode, SIGNAL(willRemoveChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeWillRemoveChildren(QgsLayerTreeNode*,int,int)));
connect(mRootNode, SIGNAL(removedChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeRemovedChildren()));
connect(mRootNode, SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)), this, SLOT(nodeVisibilityChanged(QgsLayerTreeNode*)));
}

QgsLayerTreeModel::~QgsLayerTreeModel()
@@ -36,19 +34,6 @@ QgsLayerTreeModel::~QgsLayerTreeModel()
mSymbologyNodes.clear();
}

void QgsLayerTreeModel::connectToNode(QgsLayerTreeNode* node)
{
connect(node, SIGNAL(visibilityChanged(Qt::CheckState)), this, SLOT(nodeVisibilityChanded()));

if (QgsLayerTree::isLayer(node) && testFlag(ShowSymbology))
{
addSymbologyToLayer(QgsLayerTree::toLayer(node));
}

foreach (QgsLayerTreeNode* child, node->children())
connectToNode(child);
}

QgsLayerTreeNode* QgsLayerTreeModel::index2node(const QModelIndex& index) const
{
if (!index.isValid())
@@ -378,8 +363,23 @@ void QgsLayerTreeModel::nodeAddedChildren(QgsLayerTreeNode* node, int indexFrom,

endInsertRows();

for (int i = indexFrom; i <= indexTo; ++i)
connectToNode( node->children()[i] );
if (testFlag(ShowSymbology))
{
// collect layers for which we need to add symbology
QList<QgsLayerTreeLayer*> newLayerNodes;
QList<QgsLayerTreeNode*> children = node->children();
for (int i = indexFrom; i <= indexTo; ++i)
{
QgsLayerTreeNode* child = children.at(i);
if (QgsLayerTree::isLayer(child))
newLayerNodes << QgsLayerTree::toLayer(child);
else if (QgsLayerTree::isGroup(child))
newLayerNodes << QgsLayerTree::toGroup(child)->findLayers();
}

foreach (QgsLayerTreeLayer* newLayerNode, newLayerNodes)
addSymbologyToLayer(newLayerNode);
}
}

void QgsLayerTreeModel::nodeWillRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
@@ -398,9 +398,8 @@ void QgsLayerTreeModel::nodeRemovedChildren()
endRemoveRows();
}

void QgsLayerTreeModel::nodeVisibilityChanded()
void QgsLayerTreeModel::nodeVisibilityChanged(QgsLayerTreeNode* node)
{
QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sender());
Q_ASSERT(node);

QModelIndex index = node2index(node);
@@ -84,13 +84,12 @@ protected slots:
void nodeWillRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void nodeRemovedChildren();

void nodeVisibilityChanded();
void nodeVisibilityChanged(QgsLayerTreeNode* node);

void nodeLayerLoaded();
void layerRendererChanged();

protected:
void connectToNode(QgsLayerTreeNode* node);
void removeSymbologyFromSubtree(QgsLayerTreeNode* node);
void removeSymbologyFromLayer(QgsLayerTreeLayer* nodeLayer);
void addSymbologyToLayer(QgsLayerTreeLayer* nodeL);

0 comments on commit bc49cd4

Please sign in to comment.