Skip to content

Commit

Permalink
[needs-docs] add zoom to selection action to contextual menu of the l…
Browse files Browse the repository at this point in the history
…ayer
  • Loading branch information
slarosa committed Mar 30, 2018
1 parent 1a74b0a commit bf41a90
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions python/gui/layertree/qgslayertreeviewdefaultactions.sip.in
Expand Up @@ -50,6 +50,13 @@ Action to check a group and all its parents
%End

QAction *actionZoomToLayer( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;

QAction *actionZoomToSelection( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;
%Docstring
Action to zoom to selected features of a vector layer

.. versionadded:: 3.2
%End
QAction *actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;

QAction *actionMakeTopLevel( QObject *parent = 0 ) /Factory/;
Expand All @@ -63,6 +70,7 @@ Action to enable/disable mutually exclusive flag of a group (only one child node
%End

void zoomToLayer( QgsMapCanvas *canvas );
void zoomToSelection( QgsMapCanvas *canvas );
void zoomToGroup( QgsMapCanvas *canvas );

public slots:
Expand All @@ -74,6 +82,13 @@ Action to enable/disable mutually exclusive flag of a group (only one child node
void renameGroupOrLayer();
void showFeatureCount();
void zoomToLayer();

void zoomToSelection();
%Docstring
Slot to zoom to selected features of a vector layer

.. versionadded:: 3.2
%End
void zoomToGroup();
void makeTopLevel();
void groupSelected();
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -129,6 +129,12 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
if ( layer && layer->isSpatial() )
{
menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) );
if ( vlayer )
{
QAction *actionZoomSelected = actions->actionZoomToSelection( mCanvas, menu );
actionZoomSelected->setEnabled( !vlayer->selectedFeatures().isEmpty() );
menu->addAction( actionZoomSelected );
}
menu->addAction( actions->actionShowInOverview( menu ) );
}

Expand Down
27 changes: 27 additions & 0 deletions src/gui/layertree/qgslayertreeviewdefaultactions.cpp
Expand Up @@ -97,6 +97,15 @@ QAction *QgsLayerTreeViewDefaultActions::actionZoomToLayer( QgsMapCanvas *canvas
return a;
}

QAction *QgsLayerTreeViewDefaultActions::actionZoomToSelection( QgsMapCanvas *canvas, QObject *parent )
{
QAction *a = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomToSelected.svg" ) ),
tr( "&Zoom to Selection" ), parent );
a->setData( QVariant::fromValue( reinterpret_cast<void *>( canvas ) ) );
connect( a, &QAction::triggered, this, static_cast<void ( QgsLayerTreeViewDefaultActions::* )()>( &QgsLayerTreeViewDefaultActions::zoomToSelection ) );
return a;
}

QAction *QgsLayerTreeViewDefaultActions::actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent )
{
QAction *a = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomToLayer.svg" ) ),
Expand Down Expand Up @@ -252,6 +261,15 @@ void QgsLayerTreeViewDefaultActions::zoomToLayer( QgsMapCanvas *canvas )
zoomToLayers( canvas, layers );
}

void QgsLayerTreeViewDefaultActions::zoomToSelection( QgsMapCanvas *canvas )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mView->currentLayer() );
if ( !layer )
return;

canvas->zoomToSelected( layer );
}

void QgsLayerTreeViewDefaultActions::zoomToGroup( QgsMapCanvas *canvas )
{
QgsLayerTreeGroup *groupNode = mView->currentGroupNode();
Expand All @@ -274,6 +292,15 @@ void QgsLayerTreeViewDefaultActions::zoomToLayer()
QApplication::restoreOverrideCursor();
}

void QgsLayerTreeViewDefaultActions::zoomToSelection()
{
QAction *s = qobject_cast<QAction *>( sender() );
QgsMapCanvas *canvas = reinterpret_cast<QgsMapCanvas *>( s->data().value<void *>() );
QApplication::setOverrideCursor( Qt::WaitCursor );
zoomToSelection( canvas );
QApplication::restoreOverrideCursor();
}

void QgsLayerTreeViewDefaultActions::zoomToGroup()
{
QAction *s = qobject_cast<QAction *>( sender() );
Expand Down
14 changes: 13 additions & 1 deletion src/gui/layertree/qgslayertreeviewdefaultactions.h
Expand Up @@ -58,8 +58,13 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
QAction *actionCheckAndAllParents( QObject *parent = nullptr );

QAction *actionZoomToLayer( QgsMapCanvas *canvas, QObject *parent = nullptr ) SIP_FACTORY;

/**
* Action to zoom to selected features of a vector layer
* \since QGIS 3.2
*/
QAction *actionZoomToSelection( QgsMapCanvas *canvas, QObject *parent = nullptr ) SIP_FACTORY;
QAction *actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent = nullptr ) SIP_FACTORY;
// TODO: zoom to selected

QAction *actionMakeTopLevel( QObject *parent = nullptr ) SIP_FACTORY;
QAction *actionGroupSelected( QObject *parent = nullptr ) SIP_FACTORY;
Expand All @@ -71,6 +76,7 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
QAction *actionMutuallyExclusiveGroup( QObject *parent = nullptr ) SIP_FACTORY;

void zoomToLayer( QgsMapCanvas *canvas );
void zoomToSelection( QgsMapCanvas *canvas );
void zoomToGroup( QgsMapCanvas *canvas );

public slots:
Expand All @@ -82,6 +88,12 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
void renameGroupOrLayer();
void showFeatureCount();
void zoomToLayer();

/**
* Slot to zoom to selected features of a vector layer
* \since QGIS 3.2
*/
void zoomToSelection();
void zoomToGroup();
void makeTopLevel();
void groupSelected();
Expand Down

0 comments on commit bf41a90

Please sign in to comment.