Skip to content

Commit 8485b9c

Browse files
committed
set layer scale visibility for several layers
1 parent 0e79551 commit 8485b9c

9 files changed

+256
-29
lines changed

python/gui/gui.sip

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
%Include qgsrubberband.sip
7777
%Include qgsscalecombobox.sip
7878
%Include qgsscalerangewidget.sip
79+
%Include qgsscalevisibilitydialog.sip
7980
%Include qgssearchquerybuilder.sip
8081
%Include qgstextannotationitem.sip
8182
%Include qgsvertexmarker.sip
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class QgsScaleVisibilityDialog : QObject
2+
{
3+
%TypeHeaderCode
4+
#include <qgsscalevisibilitydialog.h>
5+
%End
6+
7+
public:
8+
explicit QgsScaleVisibilityDialog( QWidget *parent = 0, QString title = QString(), QgsMapCanvas* mapCanvas = 0 );
9+
10+
//! return if scale visibilty is enabled
11+
bool hasScaleVisibility();
12+
13+
//! return minimum scale (true scale, not scale denominator)
14+
double minimumScale();
15+
16+
//! return maximum scale (true scale, not scale denominator)
17+
double maximumScale();
18+
19+
20+
public slots:
21+
//! set if scale visibility is enabled
22+
void setScaleVisiblity( bool hasScaleVisibility );
23+
24+
//! set minimum scale (true scale, not scale denominator)
25+
void setMinimumScale( double minScale );
26+
27+
//! set maximum scale (true scale, not scale denominator)
28+
void setMaximumScale( double maxScale );
29+
30+
};

src/app/qgisapp.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
#include "qgsrasterlayersaveasdialog.h"
184184
#include "qgsrectangle.h"
185185
#include "qgsscalecombobox.h"
186+
#include "qgsscalevisibilitydialog.h"
186187
#include "qgsshortcutsmanager.h"
187188
#include "qgssinglebandgrayrenderer.h"
188189
#include "qgssnappingdialog.h"
@@ -1109,6 +1110,7 @@ void QgisApp::createActions()
11091110
connect( mActionSaveLayerDefinition, SIGNAL( triggered() ), this, SLOT( saveAsLayerDefinition() ) );
11101111
connect( mActionRemoveLayer, SIGNAL( triggered() ), this, SLOT( removeLayer() ) );
11111112
connect( mActionDuplicateLayer, SIGNAL( triggered() ), this, SLOT( duplicateLayers() ) );
1113+
connect( mActionSetLayerScaleVisibility, SIGNAL( triggered() ), this, SLOT( setLayerScaleVisibility() ) );
11121114
connect( mActionSetLayerCRS, SIGNAL( triggered() ), this, SLOT( setLayerCRS() ) );
11131115
connect( mActionSetProjectCRSFromLayer, SIGNAL( triggered() ), this, SLOT( setProjectCRSFromLayer() ) );
11141116
connect( mActionLayerProperties, SIGNAL( triggered() ), this, SLOT( layerProperties() ) );
@@ -6697,6 +6699,39 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
66976699
}
66986700
}
66996701

6702+
void QgisApp::setLayerScaleVisibility()
6703+
{
6704+
if ( !mLayerTreeView )
6705+
return;
6706+
6707+
QList<QgsMapLayer*> layers = mLayerTreeView->selectedLayers();
6708+
6709+
if ( layers.length() < 1 )
6710+
return;
6711+
6712+
QgsScaleVisibilityDialog* dlg = new QgsScaleVisibilityDialog( this, tr( "Set scale visibility for selected layers" ), mMapCanvas );
6713+
QgsMapLayer* layer = mLayerTreeView->currentLayer();
6714+
if ( layer )
6715+
{
6716+
dlg->setScaleVisiblity( layer->hasScaleBasedVisibility() );
6717+
dlg->setMinimumScale( 1.0 / layer->maximumScale() );
6718+
dlg->setMaximumScale( 1.0 / layer->minimumScale() );
6719+
}
6720+
if ( dlg->exec() )
6721+
{
6722+
mMapCanvas->freeze();
6723+
foreach ( QgsMapLayer* layer, layers )
6724+
{
6725+
layer->toggleScaleBasedVisibility( dlg->hasScaleVisibility() );
6726+
layer->setMinimumScale( 1.0 / dlg->maximumScale() );
6727+
layer->setMaximumScale( 1.0 / dlg->minimumScale() );
6728+
}
6729+
mMapCanvas->freeze( false );
6730+
mMapCanvas->refresh();
6731+
}
6732+
delete dlg;
6733+
}
6734+
67006735
void QgisApp::setLayerCRS()
67016736
{
67026737
if ( !( mLayerTreeView && mLayerTreeView->currentLayer() ) )
@@ -8483,6 +8518,7 @@ void QgisApp::legendLayerSelectionChanged( void )
84838518

84848519
mActionRemoveLayer->setEnabled( selectedLayers.count() > 0 );
84858520
mActionDuplicateLayer->setEnabled( selectedLayers.count() > 0 );
8521+
mActionSetLayerScaleVisibility->setEnabled( selectedLayers.count() > 0 );
84868522
mActionSetLayerCRS->setEnabled( selectedLayers.count() > 0 );
84878523
mActionSetProjectCRSFromLayer->setEnabled( selectedLayers.count() == 1 );
84888524

src/app/qgisapp.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
349349
QAction *actionRemoveLayer() { return mActionRemoveLayer; }
350350
/** @note added in 1.9 */
351351
QAction *actionDuplicateLayer() { return mActionDuplicateLayer; }
352+
/** @note added in 2.4 */
353+
QAction *actionSetLayerScaleVisibility() { return mActionSetLayerScaleVisibility; }
352354
QAction *actionSetLayerCRS() { return mActionSetLayerCRS; }
353355
QAction *actionSetProjectCRSFromLayer() { return mActionSetProjectCRSFromLayer; }
354356
QAction *actionLayerProperties() { return mActionLayerProperties; }
@@ -466,9 +468,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
466468
#endif
467469

468470
public slots:
469-
void layerTreeViewDoubleClicked(const QModelIndex& index);
470-
void layerTreeViewCurrentChanged(const QModelIndex& current, const QModelIndex& previous);
471-
void activeLayerChanged(QgsMapLayer* layer);
471+
void layerTreeViewDoubleClicked( const QModelIndex& index );
472+
void layerTreeViewCurrentChanged( const QModelIndex& current, const QModelIndex& previous );
473+
void activeLayerChanged( QgsMapLayer* layer );
472474
//! Zoom to full extent
473475
void zoomFull();
474476
//! Zoom to the previous extent
@@ -710,6 +712,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
710712
/** Duplicate map layer(s) in legend
711713
* @note added in 1.9 */
712714
void duplicateLayers( const QList<QgsMapLayer *> lyrList = QList<QgsMapLayer *>() );
715+
//! Set Scale visibility of selected layers
716+
void setLayerScaleVisibility();
713717
//! Set CRS of a layer
714718
void setLayerCRS();
715719
//! Assign layer CRS to project

src/app/qgsapplayertreeviewmenuprovider.cpp

+28-25
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include "qgsvectorlayer.h"
1414

1515

16-
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider(QgsLayerTreeView* view, QgsMapCanvas* canvas)
17-
: mView(view)
18-
, mCanvas(canvas)
16+
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView* view, QgsMapCanvas* canvas )
17+
: mView( view )
18+
, mCanvas( canvas )
1919
{
2020
}
2121

@@ -27,38 +27,38 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
2727
QgsLayerTreeViewDefaultActions* actions = mView->defaultActions();
2828

2929
QModelIndex idx = mView->currentIndex();
30-
if (!idx.isValid())
30+
if ( !idx.isValid() )
3131
{
3232
// global menu
33-
menu->addAction( actions->actionAddGroup(menu) );
33+
menu->addAction( actions->actionAddGroup( menu ) );
3434

3535
// TODO: expand all, collapse all
3636
// TODO: update drawing order
3737
}
38-
else if (QgsLayerTreeNode* node = mView->layerTreeModel()->index2node(idx))
38+
else if ( QgsLayerTreeNode* node = mView->layerTreeModel()->index2node( idx ) )
3939
{
4040
// layer or group selected
41-
if (QgsLayerTree::isGroup(node))
41+
if ( QgsLayerTree::isGroup( node ) )
4242
{
43-
menu->addAction( actions->actionZoomToGroup(mCanvas, menu) );
44-
menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
43+
menu->addAction( actions->actionZoomToGroup( mCanvas, menu ) );
44+
menu->addAction( actions->actionRemoveGroupOrLayer( menu ) );
4545

4646
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ),
4747
tr( "&Set Group CRS" ), QgisApp::instance(), SLOT( legendGroupSetCRS() ) );
4848

49-
menu->addAction( actions->actionRenameGroupOrLayer(menu) );
49+
menu->addAction( actions->actionRenameGroupOrLayer( menu ) );
5050

51-
if (mView->selectedNodes(true).count() >= 2)
52-
menu->addAction( actions->actionGroupSelected(menu) );
51+
if ( mView->selectedNodes( true ).count() >= 2 )
52+
menu->addAction( actions->actionGroupSelected( menu ) );
5353

54-
menu->addAction( actions->actionAddGroup(menu) );
54+
menu->addAction( actions->actionAddGroup( menu ) );
5555
}
56-
else if (QgsLayerTree::isLayer(node))
56+
else if ( QgsLayerTree::isLayer( node ) )
5757
{
58-
QgsMapLayer* layer = QgsLayerTree::toLayer(node)->layer();
58+
QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer();
5959

60-
menu->addAction( actions->actionZoomToLayer(mCanvas, menu) );
61-
menu->addAction( actions->actionShowInOverview(menu) );
60+
menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) );
61+
menu->addAction( actions->actionShowInOverview( menu ) );
6262

6363
if ( layer && layer->type() == QgsMapLayer::RasterLayer )
6464
{
@@ -69,11 +69,14 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
6969
menu->addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
7070
}
7171

72-
menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
72+
menu->addAction( actions->actionRemoveGroupOrLayer( menu ) );
7373

7474
// duplicate layer
7575
QAction* duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );
7676

77+
// set layer scale visibility
78+
menu->addAction( tr( "&Set Layer Scale Visibility" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) );
79+
7780
// set layer crs
7881
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) );
7982

@@ -123,7 +126,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
123126
if ( !vlayer->isEditable() && vlayer->dataProvider()->supportsSubsetString() && vlayer->vectorJoins().isEmpty() )
124127
menu->addAction( tr( "&Filter..." ), QgisApp::instance(), SLOT( layerSubsetString() ) );
125128

126-
menu->addAction( actions->actionShowFeatureCount(menu) );
129+
menu->addAction( actions->actionShowFeatureCount( menu ) );
127130

128131
menu->addSeparator();
129132
}
@@ -140,16 +143,16 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
140143

141144
// TODO: custom actions
142145

143-
if (layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
146+
if ( layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
144147
menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) );
145148

146-
if (node->parent() != mView->layerTreeModel()->rootGroup())
147-
menu->addAction( actions->actionMakeTopLevel(menu) );
149+
if ( node->parent() != mView->layerTreeModel()->rootGroup() )
150+
menu->addAction( actions->actionMakeTopLevel( menu ) );
148151

149-
menu->addAction( actions->actionRenameGroupOrLayer(menu) );
152+
menu->addAction( actions->actionRenameGroupOrLayer( menu ) );
150153

151-
if (mView->selectedNodes(true).count() >= 2)
152-
menu->addAction( actions->actionGroupSelected(menu) );
154+
if ( mView->selectedNodes( true ).count() >= 2 )
155+
menu->addAction( actions->actionGroupSelected( menu ) );
153156

154157
if ( mView->selectedLayerNodes().count() == 1 )
155158
{

src/gui/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ qgsrelationmanagerdialog.cpp
146146
qgsrubberband.cpp
147147
qgsscalecombobox.cpp
148148
qgsscalerangewidget.cpp
149+
qgsscalevisibilitydialog.cpp
149150
qgssearchquerybuilder.cpp
150151
qgssublayersdialog.cpp
151152
qgssvgannotationitem.cpp
@@ -292,6 +293,7 @@ qgsrelationeditor.h
292293
qgsrelationmanagerdialog.h
293294
qgsscalecombobox.h
294295
qgsscalerangewidget.h
296+
qgsscalevisibilitydialog.h
295297
qgssearchquerybuilder.h
296298
qgssublayersdialog.h
297299
qgsunitselectionwidget.h
@@ -359,6 +361,7 @@ qgsrelationeditor.h
359361
qgsrubberband.h
360362
qgsscalecombobox.h
361363
qgsscalerangewidget.h
364+
qgsscalevisibilitydialog.h
362365
qgssearchquerybuilder.h
363366
qgssublayersdialog.h
364367
qgsvectorlayertools.h

src/gui/qgsscalevisibilitydialog.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/***************************************************************************
2+
qgsscalevisibilitydialog.cpp
3+
--------------------------------------
4+
Date : 20.05.2014
5+
Copyright : (C) 2014 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QGridLayout>
17+
#include <QDialogButtonBox>
18+
19+
20+
#include "qgsscalevisibilitydialog.h"
21+
22+
23+
24+
QgsScaleVisibilityDialog::QgsScaleVisibilityDialog( QWidget *parent, QString title, QgsMapCanvas* mapCanvas ) :
25+
QDialog( parent )
26+
{
27+
if ( !title.isEmpty() )
28+
{
29+
setWindowTitle( title );
30+
}
31+
32+
QGridLayout* dlgLayout = new QGridLayout( this );
33+
//dlgLayout->setContentsMargins( 0, 0, 0, 0 );
34+
35+
mGroupBox = new QGroupBox( this );
36+
mGroupBox->setCheckable( true );
37+
mGroupBox->setTitle( tr( "Scale visibility " ) );
38+
39+
QGridLayout* gbLayout = new QGridLayout( this );
40+
//gbLayout->setContentsMargins( 0, 0, 0, 0 );
41+
42+
mScaleWidget = new QgsScaleRangeWidget( this );
43+
if ( mapCanvas )
44+
{
45+
mScaleWidget->setMapCanvas( mapCanvas );
46+
}
47+
gbLayout->addWidget( mScaleWidget, 0, 0 );
48+
mGroupBox->setLayout( gbLayout );
49+
50+
QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal, this );
51+
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
52+
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
53+
54+
dlgLayout->addWidget( mGroupBox, 0, 0 );
55+
dlgLayout->addWidget( buttonBox, 1, 0 );
56+
}
57+
58+
void QgsScaleVisibilityDialog::setScaleVisiblity( bool hasScaleVisibility )
59+
{
60+
mGroupBox->setChecked( hasScaleVisibility );
61+
}
62+
63+
bool QgsScaleVisibilityDialog::hasScaleVisibility()
64+
{
65+
return mGroupBox->isChecked();
66+
}
67+
68+
void QgsScaleVisibilityDialog::setMinimumScale( double minScale )
69+
{
70+
mScaleWidget->setMinimumScale( minScale );
71+
}
72+
73+
double QgsScaleVisibilityDialog::minimumScale()
74+
{
75+
return mScaleWidget->minimumScale();
76+
}
77+
78+
void QgsScaleVisibilityDialog::setMaximumScale( double maxScale )
79+
{
80+
mScaleWidget->setMaximumScale( maxScale );
81+
}
82+
83+
double QgsScaleVisibilityDialog::maximumScale()
84+
{
85+
return mScaleWidget->maximumScale();
86+
}

0 commit comments

Comments
 (0)