From 1a61c154ab164cc11c9b19c5539c82bdde91675c Mon Sep 17 00:00:00 2001 From: Martin Dobias Date: Sat, 17 May 2014 23:53:18 +0700 Subject: [PATCH] Propagate added/removed children signals to the root for easier manipulation --- src/core/layertree/qgslayertreegroup.cpp | 2 -- src/core/layertree/qgslayertreenode.cpp | 17 ++++++--- src/core/layertree/qgslayertreenode.h | 8 ++--- .../layertree/qgslayertreeregistrybridge.cpp | 36 +++---------------- .../layertree/qgslayertreeregistrybridge.h | 5 ++- .../layertree/qgslayertreemapcanvasbridge.cpp | 9 +++-- .../layertree/qgslayertreemapcanvasbridge.h | 2 +- src/gui/layertree/qgslayertreemodel.cpp | 18 +++++----- src/gui/layertree/qgslayertreemodel.h | 6 ++-- 9 files changed, 40 insertions(+), 63 deletions(-) diff --git a/src/core/layertree/qgslayertreegroup.cpp b/src/core/layertree/qgslayertreegroup.cpp index 68fea384744..269a65084c5 100644 --- a/src/core/layertree/qgslayertreegroup.cpp +++ b/src/core/layertree/qgslayertreegroup.cpp @@ -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) diff --git a/src/core/layertree/qgslayertreenode.cpp b/src/core/layertree/qgslayertreenode.cpp index f66e87d5dd2..6acf1314758 100644 --- a/src/core/layertree/qgslayertreenode.cpp +++ b/src/core/layertree/qgslayertreenode.cpp @@ -92,10 +92,19 @@ void QgsLayerTreeNode::insertChildren(int index, QList 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) @@ -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); } diff --git a/src/core/layertree/qgslayertreenode.h b/src/core/layertree/qgslayertreenode.h index 1bf5495532e..6d92e8ee00b 100644 --- a/src/core/layertree/qgslayertreenode.h +++ b/src/core/layertree/qgslayertreenode.h @@ -49,11 +49,11 @@ signals: // 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); diff --git a/src/core/layertree/qgslayertreeregistrybridge.cpp b/src/core/layertree/qgslayertreeregistrybridge.cpp index 73b11d03469..1c375ef8361 100644 --- a/src/core/layertree/qgslayertreeregistrybridge.cpp +++ b/src/core/layertree/qgslayertreeregistrybridge.cpp @@ -16,7 +16,8 @@ QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root, connect(QgsMapLayerRegistry::instance(), SIGNAL(layersAdded(QList)), this, SLOT(layersAdded(QList))); 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) @@ -59,19 +60,6 @@ void QgsLayerTreeRegistryBridge::layersWillBeRemoved(QStringList layerIds) } -void QgsLayerTreeRegistryBridge::groupAddedChildren(int indexFrom, int indexTo) -{ - QgsLayerTreeGroup* group = qobject_cast(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) @@ -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(sender()); - Q_ASSERT(group); + Q_ASSERT(QgsLayerTree::isGroup(node)); + QgsLayerTreeGroup* group = QgsLayerTree::toGroup(node); _collectLayerIdsInGroup(group, indexFrom, indexTo, mLayerIdsForRemoval); } @@ -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)); - } -} - diff --git a/src/core/layertree/qgslayertreeregistrybridge.h b/src/core/layertree/qgslayertreeregistrybridge.h index 3228cbfea10..4148ac487da 100644 --- a/src/core/layertree/qgslayertreeregistrybridge.h +++ b/src/core/layertree/qgslayertreeregistrybridge.h @@ -5,6 +5,7 @@ #include class QgsLayerTreeGroup; +class QgsLayerTreeNode; class QgsMapLayer; @@ -30,12 +31,10 @@ protected slots: void layersAdded(QList 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; diff --git a/src/gui/layertree/qgslayertreemapcanvasbridge.cpp b/src/gui/layertree/qgslayertreemapcanvasbridge.cpp index 7d7cce458fb..17bea9104e8 100644 --- a/src/gui/layertree/qgslayertreemapcanvasbridge.cpp +++ b/src/gui/layertree/qgslayertreemapcanvasbridge.cpp @@ -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(); } @@ -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)) @@ -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(sender())); - QgsLayerTreeNode* node = qobject_cast(sender()); + Q_ASSERT(node); for (int i = indexFrom; i <= indexTo; ++i) connectToNode(node->children()[i]); diff --git a/src/gui/layertree/qgslayertreemapcanvasbridge.h b/src/gui/layertree/qgslayertreemapcanvasbridge.h index f54dce64207..ff0f059a4a8 100644 --- a/src/gui/layertree/qgslayertreemapcanvasbridge.h +++ b/src/gui/layertree/qgslayertreemapcanvasbridge.h @@ -40,7 +40,7 @@ protected: 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); diff --git a/src/gui/layertree/qgslayertreemodel.cpp b/src/gui/layertree/qgslayertreemodel.cpp index a8477d9fcc2..b5e07cf20e8 100644 --- a/src/gui/layertree/qgslayertreemodel.cpp +++ b/src/gui/layertree/qgslayertreemodel.cpp @@ -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() @@ -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)) @@ -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(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(sender()); Q_ASSERT(node); endInsertRows(); @@ -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(sender()); Q_ASSERT(node); beginRemoveRows(node2index(node), indexFrom, indexTo); diff --git a/src/gui/layertree/qgslayertreemodel.h b/src/gui/layertree/qgslayertreemodel.h index 20594d959dc..ce7eabf7304 100644 --- a/src/gui/layertree/qgslayertreemodel.h +++ b/src/gui/layertree/qgslayertreemodel.h @@ -79,9 +79,9 @@ public: 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();