Skip to content

Commit

Permalink
Propagate added/removed children signals to the root for easier manip…
Browse files Browse the repository at this point in the history
…ulation
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 36d8d88 commit 1a61c15
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 63 deletions.
2 changes: 0 additions & 2 deletions src/core/layertree/qgslayertreegroup.cpp
Expand Up @@ -51,8 +51,6 @@ void QgsLayerTreeGroup::connectToChildNode(QgsLayerTreeNode* node)
}

connect(node, SIGNAL(visibilityChanged(Qt::CheckState)), this, SLOT(updateVisibilityFromChildren()));
// forward the signal towards the root
connect(node, SIGNAL(customPropertyChanged(QgsLayerTreeNode*,QString)), this, SIGNAL(customPropertyChanged(QgsLayerTreeNode*,QString)));
}

void QgsLayerTreeGroup::insertChildNode(int index, QgsLayerTreeNode* node)
Expand Down
17 changes: 13 additions & 4 deletions src/core/layertree/qgslayertreenode.cpp
Expand Up @@ -92,10 +92,19 @@ void QgsLayerTreeNode::insertChildren(int index, QList<QgsLayerTreeNode*> nodes)
index = mChildren.count();

int indexTo = index+nodes.count()-1;
emit willAddChildren(index, indexTo);
emit willAddChildren(this, index, indexTo);
for (int i = 0; i < nodes.count(); ++i)
{
mChildren.insert(index+i, nodes[i]);
emit addedChildren(index, indexTo);

// forward the signal towards the root
connect(nodes[i], SIGNAL(willAddChildren(QgsLayerTreeNode*,int,int)), this, SIGNAL(willAddChildren(QgsLayerTreeNode*,int,int)));
connect(nodes[i], SIGNAL(addedChildren(QgsLayerTreeNode*,int,int)), this, SIGNAL(addedChildren(QgsLayerTreeNode*,int,int)));
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)));
}
emit addedChildren(this, index, indexTo);
}

void QgsLayerTreeNode::removeChildAt(int i)
Expand All @@ -109,12 +118,12 @@ void QgsLayerTreeNode::removeChildrenRange(int from, int count)
return;

int to = from+count-1;
emit willRemoveChildren(from, to);
emit willRemoveChildren(this, from, to);
while (--count >= 0)
{
QgsLayerTreeNode* node = mChildren.takeAt(from);
node->mParent = 0;
delete node;
}
emit removedChildren(from, to);
emit removedChildren(this, from, to);
}
8 changes: 4 additions & 4 deletions src/core/layertree/qgslayertreenode.h
Expand Up @@ -49,11 +49,11 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject

// low-level signals (mainly for the model)

void willAddChildren(int indexFrom, int indexTo);
void addedChildren(int indexFrom, int indexTo);
void willAddChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void addedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);

void willRemoveChildren(int indexFrom, int indexTo);
void removedChildren(int indexFrom, int indexTo);
void willRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void removedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);

void visibilityChanged(Qt::CheckState state);

Expand Down
36 changes: 5 additions & 31 deletions src/core/layertree/qgslayertreeregistrybridge.cpp
Expand Up @@ -16,7 +16,8 @@ QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root,
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersAdded(QList<QgsMapLayer*>)), this, SLOT(layersAdded(QList<QgsMapLayer*>)));
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersWillBeRemoved(QStringList)), this, SLOT(layersWillBeRemoved(QStringList)));

connectToGroup(mRoot);
connect(mRoot, SIGNAL(willRemoveChildren(QgsLayerTreeNode*,int,int)), this, SLOT(groupWillRemoveChildren(QgsLayerTreeNode*,int,int)));
connect(mRoot, SIGNAL(removedChildren(QgsLayerTreeNode*,int,int)), this, SLOT(groupRemovedChildren()));
}

void QgsLayerTreeRegistryBridge::setLayerInsertionPoint(QgsLayerTreeGroup* parentGroup, int index)
Expand Down Expand Up @@ -59,19 +60,6 @@ void QgsLayerTreeRegistryBridge::layersWillBeRemoved(QStringList layerIds)
}


void QgsLayerTreeRegistryBridge::groupAddedChildren(int indexFrom, int indexTo)
{
QgsLayerTreeGroup* group = qobject_cast<QgsLayerTreeGroup*>(sender());
Q_ASSERT(group);

for (int i = indexFrom; i <= indexTo; ++i)
{
QgsLayerTreeNode* child = group->children()[i];
if (QgsLayerTree::isGroup(child))
connectToGroup(QgsLayerTree::toGroup(child));
}
}

static void _collectLayerIdsInGroup(QgsLayerTreeGroup* group, int indexFrom, int indexTo, QStringList& lst)
{
for (int i = indexFrom; i <= indexTo; ++i)
Expand All @@ -88,12 +76,12 @@ static void _collectLayerIdsInGroup(QgsLayerTreeGroup* group, int indexFrom, int
}
}

void QgsLayerTreeRegistryBridge::groupWillRemoveChildren(int indexFrom, int indexTo)
void QgsLayerTreeRegistryBridge::groupWillRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
{
Q_ASSERT(mLayerIdsForRemoval.isEmpty());

QgsLayerTreeGroup* group = qobject_cast<QgsLayerTreeGroup*>(sender());
Q_ASSERT(group);
Q_ASSERT(QgsLayerTree::isGroup(node));
QgsLayerTreeGroup* group = QgsLayerTree::toGroup(node);

_collectLayerIdsInGroup(group, indexFrom, indexTo, mLayerIdsForRemoval);
}
Expand All @@ -110,17 +98,3 @@ void QgsLayerTreeRegistryBridge::groupRemovedChildren()

QgsMapLayerRegistry::instance()->removeMapLayers(toRemove);
}

void QgsLayerTreeRegistryBridge::connectToGroup(QgsLayerTreeGroup* group)
{
connect(group, SIGNAL(addedChildren(int,int)), this, SLOT(groupAddedChildren(int,int)));
connect(group, SIGNAL(willRemoveChildren(int,int)), this, SLOT(groupWillRemoveChildren(int,int)));
connect(group, SIGNAL(removedChildren(int,int)), this, SLOT(groupRemovedChildren()));

foreach (QgsLayerTreeNode* child, group->children())
{
if (QgsLayerTree::isGroup(child))
connectToGroup(QgsLayerTree::toGroup(child));
}
}

5 changes: 2 additions & 3 deletions src/core/layertree/qgslayertreeregistrybridge.h
Expand Up @@ -5,6 +5,7 @@
#include <QStringList>

class QgsLayerTreeGroup;
class QgsLayerTreeNode;

class QgsMapLayer;

Expand All @@ -30,12 +31,10 @@ protected slots:
void layersAdded(QList<QgsMapLayer*> layers);
void layersWillBeRemoved(QStringList layerIds);

void groupAddedChildren(int indexFrom, int indexTo);
void groupWillRemoveChildren(int indexFrom, int indexTo);
void groupWillRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void groupRemovedChildren();

protected:
void connectToGroup(QgsLayerTreeGroup* group);

protected:
QgsLayerTreeGroup* mRoot;
Expand Down
9 changes: 4 additions & 5 deletions src/gui/layertree/qgslayertreemapcanvasbridge.cpp
Expand Up @@ -11,7 +11,9 @@ QgsLayerTreeMapCanvasBridge::QgsLayerTreeMapCanvasBridge(QgsLayerTreeGroup *root
, 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()));

setCanvasLayers();
}
Expand Down Expand Up @@ -69,8 +71,6 @@ void QgsLayerTreeMapCanvasBridge::setCustomLayerOrder(const QStringList& order)

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

if (QgsLayerTree::isLayer(node))
Expand Down Expand Up @@ -126,11 +126,10 @@ void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()
QMetaObject::invokeMethod(this, "setCanvasLayers", Qt::QueuedConnection);
}

void QgsLayerTreeMapCanvasBridge::nodeAddedChildren(int indexFrom, int indexTo)
void QgsLayerTreeMapCanvasBridge::nodeAddedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
{
// connect to new children
Q_ASSERT(sender() && qobject_cast<QgsLayerTreeNode*>(sender()));
QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sender());
Q_ASSERT(node);
for (int i = indexFrom; i <= indexTo; ++i)
connectToNode(node->children()[i]);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/layertree/qgslayertreemapcanvasbridge.h
Expand Up @@ -40,7 +40,7 @@ public slots:
void deferredSetCanvasLayers();

protected slots:
void nodeAddedChildren(int indexFrom, int indexTo);
void nodeAddedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void nodeRemovedChildren();
void nodeVisibilityChanged();
void nodeCustomPropertyChanged(QgsLayerTreeNode* node, QString key);
Expand Down
18 changes: 8 additions & 10 deletions src/gui/layertree/qgslayertreemodel.cpp
Expand Up @@ -22,6 +22,11 @@ QgsLayerTreeModel::QgsLayerTreeModel(QgsLayerTreeGroup* rootNode, QObject *paren

// 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()));
}

QgsLayerTreeModel::~QgsLayerTreeModel()
Expand All @@ -33,10 +38,6 @@ QgsLayerTreeModel::~QgsLayerTreeModel()

void QgsLayerTreeModel::connectToNode(QgsLayerTreeNode* node)
{
connect(node, SIGNAL(willAddChildren(int,int)), this, SLOT(nodeWillAddChildren(int,int)));
connect(node, SIGNAL(addedChildren(int,int)), this, SLOT(nodeAddedChildren(int,int)));
connect(node, SIGNAL(willRemoveChildren(int,int)), this, SLOT(nodeWillRemoveChildren(int,int)));
connect(node, SIGNAL(removedChildren(int,int)), this, SLOT(nodeRemovedChildren()));
connect(node, SIGNAL(visibilityChanged(Qt::CheckState)), this, SLOT(nodeVisibilityChanded()));

if (QgsLayerTree::isLayer(node) && testFlag(ShowSymbology))
Expand Down Expand Up @@ -365,16 +366,14 @@ void QgsLayerTreeModel::refreshLayerSymbology(QgsLayerTreeLayer* nodeLayer)
addSymbologyToLayer(nodeLayer);
}

void QgsLayerTreeModel::nodeWillAddChildren(int indexFrom, int indexTo)
void QgsLayerTreeModel::nodeWillAddChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
{
QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sender());
Q_ASSERT(node);
beginInsertRows(node2index(node), indexFrom, indexTo);
}

void QgsLayerTreeModel::nodeAddedChildren(int indexFrom, int indexTo)
void QgsLayerTreeModel::nodeAddedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
{
QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sender());
Q_ASSERT(node);

endInsertRows();
Expand All @@ -383,9 +382,8 @@ void QgsLayerTreeModel::nodeAddedChildren(int indexFrom, int indexTo)
connectToNode( node->children()[i] );
}

void QgsLayerTreeModel::nodeWillRemoveChildren(int indexFrom, int indexTo)
void QgsLayerTreeModel::nodeWillRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo)
{
QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sender());
Q_ASSERT(node);

beginRemoveRows(node2index(node), indexFrom, indexTo);
Expand Down
6 changes: 3 additions & 3 deletions src/gui/layertree/qgslayertreemodel.h
Expand Up @@ -79,9 +79,9 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
signals:

protected slots:
void nodeWillAddChildren(int indexFrom, int indexTo);
void nodeAddedChildren(int indexFrom, int indexTo);
void nodeWillRemoveChildren(int indexFrom, int indexTo);
void nodeWillAddChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void nodeAddedChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void nodeWillRemoveChildren(QgsLayerTreeNode* node, int indexFrom, int indexTo);
void nodeRemovedChildren();

void nodeVisibilityChanded();
Expand Down

0 comments on commit 1a61c15

Please sign in to comment.