Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #4678 from nyalldawson/layer_scale
Browse files Browse the repository at this point in the history
Swap QgsMapLayer min/max scale API definitions (unify scale api, pt 2)
  • Loading branch information
nyalldawson committed Jun 8, 2017
2 parents b280c87 + cd9a802 commit 9f71156
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 105 deletions.
3 changes: 3 additions & 0 deletions doc/api_break.dox
Expand Up @@ -1516,6 +1516,9 @@ screenUpdateRequested() were removed. These members have had no effect for a num
- readXml() and writeXml() expect QgsReadWriteContext reference as the last argument
- the invalidTransformInput() slot was removed - calling this slot had no effect
- metadata() was renamed to htmlMetadata()
- setMaximumScale() and setMinimumScale(), maximumScale() and minimumScale() had the opposite meaning to other min/max scales in the API, and their definitions have now been swapped. setMaximumScale
now sets the maximum (i.e. largest scale, or most zoomed in) at which the layer will appear, and setMinimumScale now sets the minimum (i.e. smallest scale,
or most zoomed out) at which the layer will appear. The same is true for the maximumScale and minimumScale getters.


QgsMapLayerActionRegistry {#qgis_api_break_3_0_QgsMapLayerActionRegistry}
Expand Down
48 changes: 30 additions & 18 deletions python/core/qgsmaplayer.sip
Expand Up @@ -772,9 +772,12 @@ Return pointer to layer's undo stack

double minimumScale() const;
%Docstring
Returns the minimum scale denominator at which the layer is visible.
Scale based visibility is only used if hasScaleBasedVisibility is true.
:return: minimum scale denominator at which the layer will render
Returns the minimum map scale (i.e. most "zoomed out" scale) at which the layer will be visible.
The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
A scale of 0 indicates no minimum scale visibility.
.. note::

Scale based visibility is only used if setScaleBasedVisibility() is set to true.
.. seealso:: setMinimumScale()
.. seealso:: maximumScale()
.. seealso:: hasScaleBasedVisibility()
Expand All @@ -784,9 +787,12 @@ Return pointer to layer's undo stack

double maximumScale() const;
%Docstring
Returns the maximum scale denominator at which the layer is visible.
Scale based visibility is only used if hasScaleBasedVisibility is true.
:return: minimum scale denominator at which the layer will render
Returns the maximum map scale (i.e. most "zoomed in" scale) at which the layer will be visible.
The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
A scale of 0 indicates no maximum scale visibility.
.. note::

Scale based visibility is only used if setScaleBasedVisibility() is set to true.
.. seealso:: setMaximumScale()
.. seealso:: minimumScale()
.. seealso:: hasScaleBasedVisibility()
Expand Down Expand Up @@ -889,22 +895,28 @@ Time stamp of data source in the moment when data/metadata were loaded by provid

void setMinimumScale( double scale );
%Docstring
Sets the minimum scale denominator at which the layer will be visible.
Scale based visibility is only used if setScaleBasedVisibility is set to true.
\param scale minimum scale denominator at which the layer should render
.. seealso:: minimumScale
.. seealso:: setMaximumScale
.. seealso:: setScaleBasedVisibility
Sets the minimum map ``scale`` (i.e. most "zoomed out" scale) at which the layer will be visible.
The ``scale`` value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
A ``scale`` of 0 indicates no minimum scale visibility.
.. note::

Scale based visibility is only used if setScaleBasedVisibility() is set to true.
.. seealso:: minimumScale()
.. seealso:: setMaximumScale()
.. seealso:: setScaleBasedVisibility()
%End

void setMaximumScale( double scale );
%Docstring
Sets the maximum scale denominator at which the layer will be visible.
Scale based visibility is only used if setScaleBasedVisibility is set to true.
\param scale maximum scale denominator at which the layer should render
.. seealso:: maximumScale
.. seealso:: setMinimumScale
.. seealso:: setScaleBasedVisibility
Sets the maximum map ``scale`` (i.e. most "zoomed in" scale) at which the layer will be visible.
The ``scale`` value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
A ``scale`` of 0 indicates no maximum scale visibility.
.. note::

Scale based visibility is only used if setScaleBasedVisibility() is set to true.
.. seealso:: maximumScale()
.. seealso:: setMinimumScale()
.. seealso:: setScaleBasedVisibility()
%End

void setScaleBasedVisibility( const bool enabled );
Expand Down
16 changes: 8 additions & 8 deletions src/app/qgisapp.cpp
Expand Up @@ -8979,17 +8979,17 @@ void QgisApp::setLayerScaleVisibility()
if ( layer )
{
dlg->setScaleVisiblity( layer->hasScaleBasedVisibility() );
dlg->setMinimumScale( 1.0 / layer->maximumScale() );
dlg->setMaximumScale( 1.0 / layer->minimumScale() );
dlg->setMinimumScale( 1.0 / layer->minimumScale() );
dlg->setMaximumScale( 1.0 / layer->maximumScale() );
}
if ( dlg->exec() )
{
freezeCanvases();
Q_FOREACH ( QgsMapLayer *layer, layers )
{
layer->setScaleBasedVisibility( dlg->hasScaleVisibility() );
layer->setMinimumScale( 1.0 / dlg->maximumScale() );
layer->setMaximumScale( 1.0 / dlg->minimumScale() );
layer->setMaximumScale( 1.0 / dlg->maximumScale() );
layer->setMinimumScale( 1.0 / dlg->minimumScale() );
}
freezeCanvases( false );
refreshMapCanvas();
Expand All @@ -9011,13 +9011,13 @@ void QgisApp::zoomToLayerScale()
if ( layer && layer->hasScaleBasedVisibility() )
{
const double scale = mMapCanvas->scale();
if ( scale > layer->maximumScale() )
if ( scale > layer->minimumScale() )
{
mMapCanvas->zoomScale( layer->maximumScale() * Qgis::SCALE_PRECISION );
mMapCanvas->zoomScale( layer->minimumScale() * Qgis::SCALE_PRECISION );
}
else if ( scale <= layer->minimumScale() )
else if ( scale <= layer->maximumScale() )
{
mMapCanvas->zoomScale( layer->minimumScale() );
mMapCanvas->zoomScale( layer->maximumScale() );
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -205,7 +205,7 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
mIncreaseMinimumSizeLabel->setEnabled( false );
mBarWidthSpinBox->setValue( 5 );
mScaleVisibilityGroupBox->setChecked( layer->hasScaleBasedVisibility() );
mScaleRangeWidget->setScaleRange( 1.0 / layer->maximumScale(), 1.0 / layer->minimumScale() ); // caution: layer uses scale denoms, widget uses true scales
mScaleRangeWidget->setScaleRange( 1.0 / layer->minimumScale(), 1.0 / layer->maximumScale() ); // caution: layer uses scale denoms, widget uses true scales
mShowAllCheckBox->setChecked( true );
mCheckBoxAttributeLegend->setChecked( true );
mCheckBoxSizeLegend->setChecked( false );
Expand Down Expand Up @@ -266,8 +266,8 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
mPenWidthSpinBox->setValue( settingList.at( 0 ).penWidth );
mDiagramSizeSpinBox->setValue( ( size.width() + size.height() ) / 2.0 );
// caution: layer uses scale denoms, widget uses true scales
mScaleRangeWidget->setScaleRange( 1.0 / ( settingList.at( 0 ).maxScaleDenominator > 0 ? settingList.at( 0 ).maxScaleDenominator : layer->maximumScale() ),
1.0 / ( settingList.at( 0 ).minScaleDenominator > 0 ? settingList.at( 0 ).minScaleDenominator : layer->minimumScale() ) );
mScaleRangeWidget->setScaleRange( 1.0 / ( settingList.at( 0 ).maxScaleDenominator > 0 ? settingList.at( 0 ).maxScaleDenominator : layer->minimumScale() ),
1.0 / ( settingList.at( 0 ).minScaleDenominator > 0 ? settingList.at( 0 ).minScaleDenominator : layer->maximumScale() ) );
mScaleVisibilityGroupBox->setChecked( settingList.at( 0 ).scaleBasedVisibility );
mDiagramUnitComboBox->setUnit( settingList.at( 0 ).sizeType );
mDiagramUnitComboBox->setMapUnitScale( settingList.at( 0 ).sizeScale );
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -141,7 +141,7 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv
// set up the scale based layer visibility stuff....
mScaleRangeWidget->setMapCanvas( mMapCanvas );
chkUseScaleDependentRendering->setChecked( lyr->hasScaleBasedVisibility() );
mScaleRangeWidget->setScaleRange( 1.0 / lyr->maximumScale(), 1.0 / lyr->minimumScale() ); // caution: layer uses scale denoms, widget uses true scales
mScaleRangeWidget->setScaleRange( 1.0 / lyr->minimumScale(), 1.0 / lyr->maximumScale() ); // caution: layer uses scale denoms, widget uses true scales

leNoDataValue->setValidator( new QDoubleValidator( -std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), 1000, this ) );

Expand Down Expand Up @@ -911,8 +911,8 @@ void QgsRasterLayerProperties::apply()
// set up the scale based layer visibility stuff....
mRasterLayer->setScaleBasedVisibility( chkUseScaleDependentRendering->isChecked() );
// caution: layer uses scale denoms, widget uses true scales
mRasterLayer->setMaximumScale( 1.0 / mScaleRangeWidget->minimumScale() );
mRasterLayer->setMinimumScale( 1.0 / mScaleRangeWidget->maximumScale() );
mRasterLayer->setMinimumScale( 1.0 / mScaleRangeWidget->minimumScale() );
mRasterLayer->setMaximumScale( 1.0 / mScaleRangeWidget->maximumScale() );

mRasterLayer->setAutoRefreshInterval( mRefreshLayerIntervalSpinBox->value() * 1000.0 );
mRasterLayer->setAutoRefreshEnabled( mRefreshLayerCheckBox->isChecked() );
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -411,7 +411,7 @@ void QgsVectorLayerProperties::syncToLayer()
mDisplayExpressionWidget->setField( mLayer->displayExpression() );

// set up the scale based layer visibility stuff....
mScaleRangeWidget->setScaleRange( 1.0 / mLayer->maximumScale(), 1.0 / mLayer->minimumScale() ); // caution: layer uses scale denoms, widget uses true scales
mScaleRangeWidget->setScaleRange( 1.0 / mLayer->minimumScale(), 1.0 / mLayer->maximumScale() ); // caution: layer uses scale denoms, widget uses true scales
mScaleVisibilityGroupBox->setChecked( mLayer->hasScaleBasedVisibility() );
mScaleRangeWidget->setMapCanvas( QgisApp::instance()->mapCanvas() );

Expand Down Expand Up @@ -510,8 +510,8 @@ void QgsVectorLayerProperties::apply()
// set up the scale based layer visibility stuff....
mLayer->setScaleBasedVisibility( mScaleVisibilityGroupBox->isChecked() );
// caution: layer uses scale denoms, widget uses true scales
mLayer->setMaximumScale( mScaleRangeWidget->maximumScaleDenom() );
mLayer->setMinimumScale( mScaleRangeWidget->minimumScaleDenom() );
mLayer->setMinimumScale( mScaleRangeWidget->maximumScaleDenom() );
mLayer->setMaximumScale( mScaleRangeWidget->minimumScaleDenom() );

// provider-specific options
if ( mLayer->dataProvider() )
Expand Down
44 changes: 31 additions & 13 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -106,8 +106,8 @@ void QgsMapLayer::clone( QgsMapLayer *layer ) const
layer->setName( name() );
layer->setShortName( shortName() );
layer->setExtent( extent() );
layer->setMinimumScale( minimumScale() );
layer->setMaximumScale( maximumScale() );
layer->setMinimumScale( minimumScale() );
layer->setScaleBasedVisibility( hasScaleBasedVisibility() );
layer->setTitle( title() );
layer->setAbstract( abstract() );
Expand Down Expand Up @@ -460,8 +460,17 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, const QgsReadWr

// use scale dependent visibility flag
setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
setMinimumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMaximumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
{
// older element, when scales were reversed
setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
}
else
{
setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
}

setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), 0 ).toInt() );
setAutoRefreshEnabled( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() );
Expand Down Expand Up @@ -564,8 +573,8 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
{
// use scale dependent visibility flag
layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "minimumScale" ), QString::number( minimumScale() ) );
layerElement.setAttribute( QStringLiteral( "maximumScale" ), QString::number( maximumScale() ) );
layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );

if ( !mExtent.isNull() )
{
Expand Down Expand Up @@ -922,18 +931,18 @@ const QgsLayerMetadata &QgsMapLayer::metadata() const
return mMetadata;
}

void QgsMapLayer::setMinimumScale( double scale )
void QgsMapLayer::setMaximumScale( double scale )
{
mMinScale = scale;
}

double QgsMapLayer::minimumScale() const
double QgsMapLayer::maximumScale() const
{
return mMinScale;
}


void QgsMapLayer::setMaximumScale( double scale )
void QgsMapLayer::setMinimumScale( double scale )
{
mMaxScale = scale;
}
Expand All @@ -943,7 +952,7 @@ void QgsMapLayer::setScaleBasedVisibility( const bool enabled )
mScaleBasedVisibility = enabled;
}

double QgsMapLayer::maximumScale() const
double QgsMapLayer::minimumScale() const
{
return mMaxScale;
}
Expand Down Expand Up @@ -1191,8 +1200,17 @@ bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMe

// use scale dependent visibility flag
setScaleBasedVisibility( myRoot.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
setMinimumScale( myRoot.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMaximumScale( myRoot.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
if ( myRoot.hasAttribute( QStringLiteral( "minimumScale" ) ) )
{
//older scale element, when min/max were reversed
setMaximumScale( myRoot.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMinimumScale( myRoot.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
}
else
{
setMaximumScale( myRoot.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
setMinimumScale( myRoot.attribute( QStringLiteral( "minScale" ) ).toDouble() );
}

return readSymbology( myRoot, myErrorMessage, QgsReadWriteContext() ); // TODO: support relative paths in QML?
}
Expand All @@ -1208,8 +1226,8 @@ void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg ) const
myDocument.appendChild( myRootNode );

myRootNode.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
myRootNode.setAttribute( QStringLiteral( "minimumScale" ), QString::number( minimumScale() ) );
myRootNode.setAttribute( QStringLiteral( "maximumScale" ), QString::number( maximumScale() ) );
myRootNode.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
myRootNode.setAttribute( QStringLiteral( "mincale" ), QString::number( minimumScale() ) );

if ( !writeSymbology( myRootNode, myDocument, errorMsg, QgsReadWriteContext() ) ) // TODO: support relative paths in QML?
{
Expand Down
44 changes: 26 additions & 18 deletions src/core/qgsmaplayer.h
Expand Up @@ -689,19 +689,23 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
bool isInScaleRange( double scale ) const;

/** Returns the minimum scale denominator at which the layer is visible.
* Scale based visibility is only used if hasScaleBasedVisibility is true.
* \returns minimum scale denominator at which the layer will render
/**
* Returns the minimum map scale (i.e. most "zoomed out" scale) at which the layer will be visible.
* The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
* A scale of 0 indicates no minimum scale visibility.
* \note Scale based visibility is only used if setScaleBasedVisibility() is set to true.
* \see setMinimumScale()
* \see maximumScale()
* \see hasScaleBasedVisibility()
* \see isInScaleRange()
*/
double minimumScale() const;

/** Returns the maximum scale denominator at which the layer is visible.
* Scale based visibility is only used if hasScaleBasedVisibility is true.
* \returns minimum scale denominator at which the layer will render
/**
* Returns the maximum map scale (i.e. most "zoomed in" scale) at which the layer will be visible.
* The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
* A scale of 0 indicates no maximum scale visibility.
* \note Scale based visibility is only used if setScaleBasedVisibility() is set to true.
* \see setMaximumScale()
* \see minimumScale()
* \see hasScaleBasedVisibility()
Expand Down Expand Up @@ -792,21 +796,25 @@ class CORE_EXPORT QgsMapLayer : public QObject

public slots:

/** Sets the minimum scale denominator at which the layer will be visible.
* Scale based visibility is only used if setScaleBasedVisibility is set to true.
* \param scale minimum scale denominator at which the layer should render
* \see minimumScale
* \see setMaximumScale
* \see setScaleBasedVisibility
/**
* Sets the minimum map \a scale (i.e. most "zoomed out" scale) at which the layer will be visible.
* The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
* A \a scale of 0 indicates no minimum scale visibility.
* \note Scale based visibility is only used if setScaleBasedVisibility() is set to true.
* \see minimumScale()
* \see setMaximumScale()
* \see setScaleBasedVisibility()
*/
void setMinimumScale( double scale );

/** Sets the maximum scale denominator at which the layer will be visible.
* Scale based visibility is only used if setScaleBasedVisibility is set to true.
* \param scale maximum scale denominator at which the layer should render
* \see maximumScale
* \see setMinimumScale
* \see setScaleBasedVisibility
/**
* Sets the maximum map \a scale (i.e. most "zoomed in" scale) at which the layer will be visible.
* The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
* A \a scale of 0 indicates no maximum scale visibility.
* \note Scale based visibility is only used if setScaleBasedVisibility() is set to true.
* \see maximumScale()
* \see setMinimumScale()
* \see setScaleBasedVisibility()
*/
void setMaximumScale( double scale );

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -2214,7 +2214,7 @@ bool QgsVectorLayer::writeSld( QDomNode &node, QDomDocument &doc, QString &error
QgsStringMap localProps = QgsStringMap( props );
if ( hasScaleBasedVisibility() )
{
QgsSymbolLayerUtils::mergeScaleDependencies( minimumScale(), maximumScale(), localProps );
QgsSymbolLayerUtils::mergeScaleDependencies( maximumScale(), minimumScale(), localProps );
}

if ( hasGeometryType() )
Expand Down
7 changes: 2 additions & 5 deletions src/server/qgswmsconfigparser.cpp
Expand Up @@ -588,12 +588,9 @@ void QgsWmsConfigParser::setLayerIdsToLegendModel( QgsLegendModel *model, const
else
{
QgsMapLayer *layer = nodeLayer->layer();
if ( layer->hasScaleBasedVisibility() )
if ( !layer->isInScaleRange( scale ) )
{
if ( layer->minimumScale() > scale )
qobject_cast<QgsLayerTreeGroup *>( nodeLayer->parent() )->removeChildNode( nodeLayer );
else if ( layer->maximumScale() < scale )
qobject_cast<QgsLayerTreeGroup *>( nodeLayer->parent() )->removeChildNode( nodeLayer );
qobject_cast<QgsLayerTreeGroup *>( nodeLayer->parent() )->removeChildNode( nodeLayer );
}
}
}
Expand Down

0 comments on commit 9f71156

Please sign in to comment.