Skip to content

Commit bf41a90

Browse files
committedMar 30, 2018
[needs-docs] add zoom to selection action to contextual menu of the layer
1 parent 1a74b0a commit bf41a90

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed
 

‎python/gui/layertree/qgslayertreeviewdefaultactions.sip.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ Action to check a group and all its parents
5050
%End
5151

5252
QAction *actionZoomToLayer( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;
53+
54+
QAction *actionZoomToSelection( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;
55+
%Docstring
56+
Action to zoom to selected features of a vector layer
57+
58+
.. versionadded:: 3.2
59+
%End
5360
QAction *actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent = 0 ) /Factory/;
5461

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

6572
void zoomToLayer( QgsMapCanvas *canvas );
73+
void zoomToSelection( QgsMapCanvas *canvas );
6674
void zoomToGroup( QgsMapCanvas *canvas );
6775

6876
public slots:
@@ -74,6 +82,13 @@ Action to enable/disable mutually exclusive flag of a group (only one child node
7482
void renameGroupOrLayer();
7583
void showFeatureCount();
7684
void zoomToLayer();
85+
86+
void zoomToSelection();
87+
%Docstring
88+
Slot to zoom to selected features of a vector layer
89+
90+
.. versionadded:: 3.2
91+
%End
7792
void zoomToGroup();
7893
void makeTopLevel();
7994
void groupSelected();

‎src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
129129
if ( layer && layer->isSpatial() )
130130
{
131131
menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) );
132+
if ( vlayer )
133+
{
134+
QAction *actionZoomSelected = actions->actionZoomToSelection( mCanvas, menu );
135+
actionZoomSelected->setEnabled( !vlayer->selectedFeatures().isEmpty() );
136+
menu->addAction( actionZoomSelected );
137+
}
132138
menu->addAction( actions->actionShowInOverview( menu ) );
133139
}
134140

‎src/gui/layertree/qgslayertreeviewdefaultactions.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ QAction *QgsLayerTreeViewDefaultActions::actionZoomToLayer( QgsMapCanvas *canvas
9797
return a;
9898
}
9999

100+
QAction *QgsLayerTreeViewDefaultActions::actionZoomToSelection( QgsMapCanvas *canvas, QObject *parent )
101+
{
102+
QAction *a = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomToSelected.svg" ) ),
103+
tr( "&Zoom to Selection" ), parent );
104+
a->setData( QVariant::fromValue( reinterpret_cast<void *>( canvas ) ) );
105+
connect( a, &QAction::triggered, this, static_cast<void ( QgsLayerTreeViewDefaultActions::* )()>( &QgsLayerTreeViewDefaultActions::zoomToSelection ) );
106+
return a;
107+
}
108+
100109
QAction *QgsLayerTreeViewDefaultActions::actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent )
101110
{
102111
QAction *a = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomToLayer.svg" ) ),
@@ -252,6 +261,15 @@ void QgsLayerTreeViewDefaultActions::zoomToLayer( QgsMapCanvas *canvas )
252261
zoomToLayers( canvas, layers );
253262
}
254263

264+
void QgsLayerTreeViewDefaultActions::zoomToSelection( QgsMapCanvas *canvas )
265+
{
266+
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mView->currentLayer() );
267+
if ( !layer )
268+
return;
269+
270+
canvas->zoomToSelected( layer );
271+
}
272+
255273
void QgsLayerTreeViewDefaultActions::zoomToGroup( QgsMapCanvas *canvas )
256274
{
257275
QgsLayerTreeGroup *groupNode = mView->currentGroupNode();
@@ -274,6 +292,15 @@ void QgsLayerTreeViewDefaultActions::zoomToLayer()
274292
QApplication::restoreOverrideCursor();
275293
}
276294

295+
void QgsLayerTreeViewDefaultActions::zoomToSelection()
296+
{
297+
QAction *s = qobject_cast<QAction *>( sender() );
298+
QgsMapCanvas *canvas = reinterpret_cast<QgsMapCanvas *>( s->data().value<void *>() );
299+
QApplication::setOverrideCursor( Qt::WaitCursor );
300+
zoomToSelection( canvas );
301+
QApplication::restoreOverrideCursor();
302+
}
303+
277304
void QgsLayerTreeViewDefaultActions::zoomToGroup()
278305
{
279306
QAction *s = qobject_cast<QAction *>( sender() );

‎src/gui/layertree/qgslayertreeviewdefaultactions.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
5858
QAction *actionCheckAndAllParents( QObject *parent = nullptr );
5959

6060
QAction *actionZoomToLayer( QgsMapCanvas *canvas, QObject *parent = nullptr ) SIP_FACTORY;
61+
62+
/**
63+
* Action to zoom to selected features of a vector layer
64+
* \since QGIS 3.2
65+
*/
66+
QAction *actionZoomToSelection( QgsMapCanvas *canvas, QObject *parent = nullptr ) SIP_FACTORY;
6167
QAction *actionZoomToGroup( QgsMapCanvas *canvas, QObject *parent = nullptr ) SIP_FACTORY;
62-
// TODO: zoom to selected
6368

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

7378
void zoomToLayer( QgsMapCanvas *canvas );
79+
void zoomToSelection( QgsMapCanvas *canvas );
7480
void zoomToGroup( QgsMapCanvas *canvas );
7581

7682
public slots:
@@ -82,6 +88,12 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
8288
void renameGroupOrLayer();
8389
void showFeatureCount();
8490
void zoomToLayer();
91+
92+
/**
93+
* Slot to zoom to selected features of a vector layer
94+
* \since QGIS 3.2
95+
*/
96+
void zoomToSelection();
8597
void zoomToGroup();
8698
void makeTopLevel();
8799
void groupSelected();

0 commit comments

Comments
 (0)
Please sign in to comment.