Propagate added/removed children signals to the root for easier manipulation

This commit is contained in:
Martin Dobias 2014-05-17 23:53:18 +07:00
parent 36d8d88e09
commit 1a61c154ab
9 changed files with 40 additions and 63 deletions

View File

@ -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)

View File

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

View File

@ -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);

View File

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

View File

@ -5,6 +5,7 @@
#include <QStringList>
class QgsLayerTreeGroup;
class QgsLayerTreeNode;
class QgsMapLayer;
@ -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;

View File

@ -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<QgsLayerTreeNode*>(sender()));
QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sender());
Q_ASSERT(node);
for (int i = indexFrom; i <= indexTo; ++i)
connectToNode(node->children()[i]);

View File

@ -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);

View File

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

View File

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