Skip to content

Commit 3efc73b

Browse files
author
Patrick Valsecchi
committed
Made layers/labels visibility more consistent
Some places forgot to make the max scale inclusive.
1 parent e08130d commit 3efc73b

18 files changed

Lines changed: 77 additions & 19 deletions

python/core/qgis.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ class QGis
235235
/** Default highlight line/outline minimum width in mm.
236236
* @note added in 2.3 */
237237
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM;
238+
239+
/** Fudge factor used to compare two scales. The code is often going from scale to scale
240+
* denominator. So it looses precision and, when a limit is inclusive, can lead to errors.
241+
* To avoid that, use this factor instead of using <= or >=.
242+
* @note added in 2.15*/
243+
static double SCALE_PRECISION;
238244
};
239245

240246

python/core/qgslabel.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class QgsLabel
106106
void setScaleBasedVisibility( bool theVisibilityFlag );
107107
bool scaleBasedVisibility() const;
108108

109+
/** Return true if the label is visible at the given scale */
110+
bool isInScaleRange( double scale ) const;
111+
109112
private:
110113
QgsLabel (); // pretend that constructor is private for now
111114
QgsLabel( const QgsLabel& rh );

python/core/qgsmaplayer.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,11 @@ class QgsMapLayer : QObject
492492
*/
493493
QgsMapLayerStyleManager* styleManager() const;
494494

495+
/**
496+
* @returns true if the layer is visible at the given scale.
497+
*/
498+
bool isInScaleRange( double scale ) const;
499+
495500
/** Returns the minimum scale denominator at which the layer is visible.
496501
* Scale based visibility is only used if hasScaleBasedVisibility is true.
497502
* @returns minimum scale denominator at which the layer will render

src/core/dxf/qgsdxfexport.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,11 +4109,10 @@ bool QgsDxfExport::layerIsScaleBasedVisible( const QgsMapLayer* layer ) const
41094109
if ( !layer )
41104110
return false;
41114111

4112-
if ( mSymbologyExport == QgsDxfExport::NoSymbology || !layer->hasScaleBasedVisibility() )
4112+
if ( mSymbologyExport == QgsDxfExport::NoSymbology )
41134113
return true;
41144114

4115-
return layer->minimumScale() < mSymbologyScaleDenominator &&
4116-
layer->maximumScale() > mSymbologyScaleDenominator;
4115+
return layer->isInScaleRange( mSymbologyScaleDenominator );
41174116
}
41184117

41194118
QString QgsDxfExport::layerName( const QString &id, const QgsFeature &f ) const

src/core/qgis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ double QGis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
8787

8888
double QGis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
8989

90+
double QGis::SCALE_PRECISION = 0.9999999999;
91+
9092
// description strings for units
9193
// Order must match enum indices
9294
const char* QGis::qgisUnitTypes[] =

src/core/qgis.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ class CORE_EXPORT QGis
246246
* @note added in 2.3 */
247247
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM;
248248

249+
/** Fudge factor used to compare two scales. The code is often going from scale to scale
250+
* denominator. So it looses precision and, when a limit is inclusive, can lead to errors.
251+
* To avoid that, use this factor instead of using <= or >=.
252+
* @note added in 2.15*/
253+
static double SCALE_PRECISION;
254+
249255
private:
250256
// String representation of unit types (set in qgis.cpp)
251257
static const char *qgisUnitTypes[];

src/core/qgslabel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,3 +1414,9 @@ float QgsLabel::maxScale() const
14141414
{
14151415
return mMaxScale;
14161416
}
1417+
1418+
bool QgsLabel::isInScaleRange( double scale ) const
1419+
{
1420+
return !mScaleBasedVisibility ||
1421+
( mMinScale * QGis::SCALE_PRECISION < scale && scale * QGis::SCALE_PRECISION < mMaxScale );
1422+
}

src/core/qgslabel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class CORE_EXPORT QgsLabel
146146
void setScaleBasedVisibility( bool theVisibilityFlag );
147147
bool scaleBasedVisibility() const;
148148

149+
/** Return true if the label is visible at the given scale */
150+
bool isInScaleRange( double scale ) const;
151+
149152
private:
150153
/** Does the actual rendering of a label at the given point */
151154
void renderLabel( QgsRenderContext &renderContext, QgsPoint point,

src/core/qgsmaphittest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void QgsMapHitTest::run()
5959

6060
if ( !mOnlyExpressions )
6161
{
62-
if ( vl->hasScaleBasedVisibility() && ( mSettings.scale() < vl->minimumScale() || mSettings.scale() > vl->maximumScale() ) )
62+
if ( !vl->isInScaleRange( mSettings.scale() ) )
6363
{
6464
mHitTest[vl] = SymbolV2Set(); // no symbols -> will not be shown
6565
mHitTestRuleKey[vl] = SymbolV2Set();

src/core/qgsmaplayer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,10 @@ void QgsMapLayer::connectNotify( const char * signal )
925925
} // QgsMapLayer::connectNotify
926926
#endif
927927

928+
bool QgsMapLayer::isInScaleRange( double scale ) const
929+
{
930+
return !mScaleBasedVisibility || ( mMinScale * QGis::SCALE_PRECISION < scale && scale < mMaxScale );
931+
}
928932

929933
void QgsMapLayer::toggleScaleBasedVisibility( bool theVisibilityFlag )
930934
{

0 commit comments

Comments
 (0)