Skip to content

Commit

Permalink
Forward the visibilityChanged() signal towards the parent node.
Browse files Browse the repository at this point in the history
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 bc49cd4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 63 deletions.
25 changes: 9 additions & 16 deletions src/core/layertree/qgslayertreegroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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*)));
}


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

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/core/layertree/qgslayertreegroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/core/layertree/qgslayertreenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
41 changes: 23 additions & 18 deletions src/gui/layertree/qgslayertreemapcanvasbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 0 additions & 3 deletions src/gui/layertree/qgslayertreemapcanvasbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
39 changes: 19 additions & 20 deletions src/gui/layertree/qgslayertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/gui/layertree/qgslayertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bc49cd4

Please sign in to comment.