Skip to content

Commit

Permalink
[FEATURE] Add a menu entry to zoom to a layer's scale range
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valsecchi committed Mar 9, 2016
1 parent a112dfe commit 556b621
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7938,6 +7938,31 @@ void QgisApp::setLayerScaleVisibility()
delete dlg;
}

void QgisApp::zoomToLayerScale()
{
if ( !mLayerTreeView )
return;

QList<QgsMapLayer*> layers = mLayerTreeView->selectedLayers();

if ( layers.length() < 1 )
return;

QgsMapLayer* layer = mLayerTreeView->currentLayer();
if ( layer && layer->hasScaleBasedVisibility() )
{
const double scale = mMapCanvas->scale();
if ( scale > layer->maximumScale() )
{
mMapCanvas->zoomScale( layer->maximumScale() * QGis::SCALE_PRECISION );
}
else if ( scale <= layer->minimumScale() )
{
mMapCanvas->zoomScale( layer->minimumScale() );
}
}
}

void QgisApp::setLayerCRS()
{
if ( !( mLayerTreeView && mLayerTreeView->currentLayer() ) )
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void duplicateLayers( const QList<QgsMapLayer *>& lyrList = QList<QgsMapLayer *>() );
//! Set Scale visibility of selected layers
void setLayerScaleVisibility();
void zoomToLayerScale();
//! Set CRS of a layer
void setLayerCRS();
//! Assign layer CRS to project
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "qgslayertreemodel.h"
#include "qgslayertreemodellegendnode.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerstyleguiutils.h"
#include "qgsmaplayerregistry.h"
#include "qgsproject.h"
Expand Down Expand Up @@ -105,6 +106,9 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
// set layer scale visibility
menu->addAction( tr( "&Set Layer Scale Visibility" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) );

if ( !vlayer->isInScaleRange( mCanvas->scale() ) )
menu->addAction( tr( "&Zoom to Layer Scale" ), QgisApp::instance(), SLOT( zoomToLayerScale() ) );

// set layer crs
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) );

Expand Down

0 comments on commit 556b621

Please sign in to comment.