Skip to content

Commit

Permalink
[FEATURE][Needs-docs] Move layer or group to top of layer panel
Browse files Browse the repository at this point in the history
A feature that moves the layer(s) or group(s) to the top of the layer panel.
  • Loading branch information
MieWinstrup authored and nyalldawson committed Apr 5, 2018
1 parent cae5f81 commit 924e2ec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/gui/layertree/qgslayertreeviewdefaultactions.sip.in
Expand Up @@ -53,6 +53,7 @@ Action to check a group and all its parents
QAction *actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;

QAction *actionMakeTopLevel( QObject *parent = 0 ) /Factory/;
QAction *actionMoveToTop( QObject *parent = 0 ) /Factory/;
QAction *actionGroupSelected( QObject *parent = 0 ) /Factory/;

QAction *actionMutuallyExclusiveGroup( QObject *parent = 0 ) /Factory/;
Expand All @@ -76,6 +77,7 @@ Action to enable/disable mutually exclusive flag of a group (only one child node
void zoomToLayer();
void zoomToGroup();
void makeTopLevel();
void moveToTop();
void groupSelected();

void mutuallyExclusiveGroup();
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -118,6 +118,9 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QAction *actionSaveAsDefinitionGroup = new QAction( tr( "Save as Layer Definition File…" ), menuExportGroup );
connect( actionSaveAsDefinitionGroup, &QAction::triggered, QgisApp::instance(), &QgisApp::saveAsLayerDefinition );
menuExportGroup->addAction( actionSaveAsDefinitionGroup );

if ( !( mView->selectedNodes( true ).count() == 1 && idx.row() == 0 ) )
menu->addAction( actions->actionMoveToTop( menu ) );
menu->addMenu( menuExportGroup );
}
else if ( QgsLayerTree::isLayer( node ) )
Expand Down Expand Up @@ -292,6 +295,9 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()

QgisApp *app = QgisApp::instance();
menuStyleManager->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );

if ( !( mView->selectedNodes( true ).count() == 1 && idx.row() == 0 ) )
menu->addAction( actions->actionMoveToTop( menu ) );
if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menuStyleManager->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
Expand Down
28 changes: 28 additions & 0 deletions src/gui/layertree/qgslayertreeviewdefaultactions.cpp
Expand Up @@ -113,6 +113,13 @@ QAction *QgsLayerTreeViewDefaultActions::actionMakeTopLevel( QObject *parent )
return a;
}

QAction *QgsLayerTreeViewDefaultActions::actionMoveToTop( QObject *parent )
{
QAction *a = new QAction( tr( "Move to &Top" ), parent );
connect( a, &QAction::triggered, this, &QgsLayerTreeViewDefaultActions::moveToTop );
return a;
}

QAction *QgsLayerTreeViewDefaultActions::actionGroupSelected( QObject *parent )
{
QAction *a = new QAction( tr( "&Group Selected" ), parent );
Expand Down Expand Up @@ -352,6 +359,27 @@ void QgsLayerTreeViewDefaultActions::makeTopLevel()
}
}

void QgsLayerTreeViewDefaultActions::moveToTop()
{
QMap <QgsLayerTreeGroup *, int> groupInsertIdx;
int insertIdx;
Q_FOREACH ( QgsLayerTreeNode *n, mView->selectedNodes() )
{
QgsLayerTreeGroup *parentGroup = qobject_cast<QgsLayerTreeGroup *>( n->parent() );
QgsLayerTreeNode *clonedNode = n->clone();
if ( groupInsertIdx.contains( parentGroup ) )
{
insertIdx = groupInsertIdx.value( parentGroup );
}
else
{
insertIdx = 0;
}
parentGroup->insertChildNode( insertIdx, clonedNode );
parentGroup->removeChildNode( n );
groupInsertIdx.insert( parentGroup, insertIdx + 1 );
}
}

void QgsLayerTreeViewDefaultActions::groupSelected()
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/layertree/qgslayertreeviewdefaultactions.h
Expand Up @@ -62,6 +62,7 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
// TODO: zoom to selected

QAction *actionMakeTopLevel( QObject *parent = nullptr ) SIP_FACTORY;
QAction *actionMoveToTop( QObject *parent = nullptr ) SIP_FACTORY;
QAction *actionGroupSelected( QObject *parent = nullptr ) SIP_FACTORY;

/**
Expand All @@ -84,6 +85,7 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
void zoomToLayer();
void zoomToGroup();
void makeTopLevel();
void moveToTop();
void groupSelected();

/**
Expand Down

0 comments on commit 924e2ec

Please sign in to comment.