Skip to content
Permalink
Browse files
Added "Group selected" layer tree view action
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 4162c21 commit f2b69e967ccdc7bb72d293dd6194aadd32a4e55d
@@ -2228,6 +2228,11 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
menu->addAction( actions->actionMakeTopLevel(menu) );
}

if (mView->selectedNodes(true).count() >= 2)
{
menu->addAction( actions->actionGroupSelected(menu) );
}

menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
menu->addAction( actions->actionRenameGroupOrLayer(menu) );
}
@@ -88,16 +88,18 @@ QAction* QgsLayerTreeViewDefaultActions::actionMakeTopLevel(QObject* parent)
return a;
}

QAction* QgsLayerTreeViewDefaultActions::actionGroupSelected(QObject* parent)
{
QAction* a = new QAction(tr("&Group Selected"), parent);
connect(a, SIGNAL(triggered()), this, SLOT(groupSelected()));
return a;
}

void QgsLayerTreeViewDefaultActions::addGroup()
{
QgsLayerTreeGroup* group = mView->currentGroupNode();
QString prefix = group == mView->layerTreeModel()->rootGroup() ? "group" : "sub-group";

QString newName = prefix + "1";
for ( int i = 2; group->findGroup(newName); ++i)
newName = prefix + QString::number(i);

QgsLayerTreeGroup* newGroup = group->addGroup(newName);
QgsLayerTreeGroup* newGroup = group->addGroup(uniqueGroupName(group));
mView->edit( mView->layerTreeModel()->node2index(newGroup) );
}

@@ -202,6 +204,16 @@ void QgsLayerTreeViewDefaultActions::zoomToLayers(QgsMapCanvas* canvas, const QL
}


QString QgsLayerTreeViewDefaultActions::uniqueGroupName(QgsLayerTreeGroup* parentGroup)
{
QString prefix = parentGroup == mView->layerTreeModel()->rootGroup() ? "group" : "sub-group";
QString newName = prefix + "1";
for ( int i = 2; parentGroup->findGroup(newName); ++i)
newName = prefix + QString::number(i);
return newName;
}


void QgsLayerTreeViewDefaultActions::makeTopLevel()
{
QgsLayerTreeNode* node = mView->currentNode();
@@ -217,3 +229,26 @@ void QgsLayerTreeViewDefaultActions::makeTopLevel()
rootGroup->addChildNode(clonedNode);
parentGroup->removeChildNode(node);
}


void QgsLayerTreeViewDefaultActions::groupSelected()
{
QList<QgsLayerTreeNode*> nodes = mView->selectedNodes(true);
if (nodes.count() < 2)
return;

QgsLayerTreeGroup* parentGroup = mView->layerTreeModel()->rootGroup();

QgsLayerTreeGroup* newGroup = new QgsLayerTreeGroup(uniqueGroupName(parentGroup));
foreach (QgsLayerTreeNode* node, nodes)
newGroup->addChildNode(node->clone());

parentGroup->addChildNode(newGroup);

foreach (QgsLayerTreeNode* node, nodes)
{
QgsLayerTreeGroup* group = qobject_cast<QgsLayerTreeGroup*>(node->parent());
if (group)
group->removeChildNode(node);
}
}
@@ -5,6 +5,7 @@

class QAction;

class QgsLayerTreeGroup;
class QgsLayerTreeView;
class QgsMapCanvas;
class QgsMapLayer;
@@ -26,6 +27,7 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
// TODO: zoom to selected

QAction* actionMakeTopLevel(QObject* parent = 0);
QAction* actionGroupSelected(QObject* parent = 0);

protected slots:
void addGroup();
@@ -36,10 +38,13 @@ protected slots:
void zoomToLayer();
void zoomToGroup();
void makeTopLevel();
void groupSelected();

protected:
void zoomToLayers(QgsMapCanvas* canvas, const QList<QgsMapLayer*>& layers);

QString uniqueGroupName(QgsLayerTreeGroup* parentGroup);

protected:
QgsLayerTreeView* mView;
};

0 comments on commit f2b69e9

Please sign in to comment.