Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #6721 from slarosa/zoom_selected_cm
[needs-docs] add zoom to selection action to contextual menu of the layer
  • Loading branch information
slarosa committed Apr 6, 2018
2 parents 62ba263 + 5f7358a commit 747955a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
22 changes: 22 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 @@ -71,6 +78,14 @@ Action to enable/disable mutually exclusive flag of a group (only one child node
%End

void zoomToLayer( QgsMapCanvas *canvas );

void zoomToSelection( QgsMapCanvas *canvas );
%Docstring

.. seealso:: :py:func:`zoomToSelection`

.. versionadded:: 3.2
%End
void zoomToGroup( QgsMapCanvas *canvas );

public slots:
Expand All @@ -82,6 +97,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();

Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -135,6 +135,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
26 changes: 26 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 @@ -259,6 +268,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 @@ -281,6 +299,14 @@ void QgsLayerTreeViewDefaultActions::zoomToLayer()
QApplication::restoreOverrideCursor();
}

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

void QgsLayerTreeViewDefaultActions::zoomToGroup()
{
QAction *s = qobject_cast<QAction *>( sender() );
Expand Down
19 changes: 18 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;

Expand All @@ -77,6 +82,12 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
QAction *actionMutuallyExclusiveGroup( QObject *parent = nullptr ) SIP_FACTORY;

void zoomToLayer( QgsMapCanvas *canvas );

/**
* \see zoomToSelection()
* \since QGIS 3.2
*/
void zoomToSelection( QgsMapCanvas *canvas );
void zoomToGroup( QgsMapCanvas *canvas );

public slots:
Expand All @@ -88,6 +99,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();

Expand Down

0 comments on commit 747955a

Please sign in to comment.