Skip to content

Commit

Permalink
Added "Group selected" layer tree view action
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 4162c21 commit f2b69e9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -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) );
}
Expand Down
47 changes: 41 additions & 6 deletions src/gui/layertree/qgslayertreeviewdefaultactions.cpp
Expand Up @@ -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) );
}

Expand Down Expand Up @@ -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();
Expand All @@ -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 changes: 5 additions & 0 deletions src/gui/layertree/qgslayertreeviewdefaultactions.h
Expand Up @@ -5,6 +5,7 @@

class QAction;

class QgsLayerTreeGroup;
class QgsLayerTreeView;
class QgsMapCanvas;
class QgsMapLayer;
Expand All @@ -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();
Expand All @@ -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;
};
Expand Down

0 comments on commit f2b69e9

Please sign in to comment.