Skip to content

Commit b0c35ab

Browse files
committedJun 9, 2017
Unify scale API in rule based labeling, fix GUI issues
1 parent 9805782 commit b0c35ab

File tree

5 files changed

+85
-96
lines changed

5 files changed

+85
-96
lines changed
 

‎python/core/qgsrulebasedlabeling.sip

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class QgsRuleBasedLabeling : QgsAbstractVectorLayerLabeling
3333
#include "qgsrulebasedlabeling.h"
3434
%End
3535
public:
36-
Rule( QgsPalLayerSettings *settings /Transfer/, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
36+
Rule( QgsPalLayerSettings *settings /Transfer/, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
3737
%Docstring
3838
takes ownership of settings
3939
%End
@@ -61,26 +61,26 @@ takes ownership of settings
6161
:rtype: bool
6262
%End
6363

64-
int scaleMinDenom() const;
64+
double maximumScale() const;
6565
%Docstring
66-
The minimum scale at which this label rule should be applied
67-
68-
E.g. Denominator 1000 is a scale of 1:1000, where a rule with minimum denominator
69-
of 900 will not be applied while a rule with 2000 will be applied.
70-
71-
:return: The minimum scale denominator
72-
:rtype: int
66+
Returns the maximum map scale (i.e. most "zoomed in" scale) at which the label rule will be active.
67+
The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
68+
A scale of 0 indicates no maximum scale visibility.
69+
.. seealso:: minimumScale()
70+
.. seealso:: setMaximumScale()
71+
.. versionadded:: 3.0
72+
:rtype: float
7373
%End
7474

75-
int scaleMaxDenom() const;
75+
double minimumScale() const;
7676
%Docstring
77-
The maximum scale denominator at which this label rule should be applied
78-
79-
E.g. Denominator 1000 is a scale of 1:1000, where a rule with maximum denominator
80-
of 900 will be applied while a rule with 2000 will not be applied.
81-
82-
:return: The maximum scale denominator
83-
:rtype: int
77+
Returns the minimum map scale (i.e. most "zoomed out" scale) at which the label rule will be active.
78+
The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
79+
A scale of 0 indicates no minimum scale visibility.
80+
.. seealso:: maximumScale()
81+
.. seealso:: setMinimumScale()
82+
.. versionadded:: 3.0
83+
:rtype: float
8484
%End
8585

8686
QString filterExpression() const;
@@ -125,20 +125,22 @@ Unique rule identifier (for identification of rule within labeling, used as prov
125125
set new settings (or NULL). Deletes old settings if any.
126126
%End
127127

128-
void setScaleMinDenom( int scaleMinDenom );
128+
void setMinimumScale( double scale );
129129
%Docstring
130-
Set the minimum denominator for which this rule shall apply.
131-
E.g. 1000 if it shall be evaluated between 1:1000 and 1:100'000
132-
Set to 0 to disable the minimum check
133-
\param scaleMinDenom The minimum scale denominator for this rule
130+
Sets the minimum map ``scale`` (i.e. most "zoomed out" scale) at which the label rule will be active.
131+
The ``scale`` value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
132+
A ``scale`` of 0 indicates no minimum scale visibility.
133+
.. seealso:: minimumScale()
134+
.. seealso:: setMaximumScale()
134135
%End
135136

136-
void setScaleMaxDenom( int scaleMaxDenom );
137+
void setMaximumScale( double scale );
137138
%Docstring
138-
Set the maximum denominator for which this rule shall apply.
139-
E.g. 100'000 if it shall be evaluated between 1:1000 and 1:100'000
140-
Set to 0 to disable the maximum check
141-
\param scaleMaxDenom maximum scale denominator for this rule
139+
Sets the maximum map ``scale`` (i.e. most "zoomed in" scale) at which the rule will be active.
140+
The ``scale`` value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
141+
A ``scale`` of 0 indicates no maximum scale visibility.
142+
.. seealso:: maximumScale()
143+
.. seealso:: setMinimumScale()
142144
%End
143145

144146
void setFilterExpression( const QString &filterExp );

‎src/app/qgsrulebasedlabelingwidget.cpp

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,6 @@ QgsRuleBasedLabeling::Rule *QgsRuleBasedLabelingWidget::currentRule()
209209
return mModel->ruleForIndex( idx );
210210
}
211211

212-
213-
////
214-
215-
static QString _formatScale( int denom )
216-
{
217-
if ( denom != 0 )
218-
{
219-
QString txt = QStringLiteral( "1:%L1" ).arg( denom );
220-
return txt;
221-
}
222-
else
223-
return QString();
224-
}
225-
226212
////
227213

228214
QgsRuleBasedLabelingModel::QgsRuleBasedLabelingModel( QgsRuleBasedLabeling::Rule *rootRule, QObject *parent )
@@ -269,9 +255,9 @@ QVariant QgsRuleBasedLabelingModel::data( const QModelIndex &index, int role ) c
269255
return rule->filterExpression().isEmpty() ? tr( "(no filter)" ) : rule->filterExpression();
270256
}
271257
case 2:
272-
return rule->dependsOnScale() ? _formatScale( rule->scaleMaxDenom() ) : QVariant();
258+
return rule->dependsOnScale() ? QgsScaleComboBox::toString( rule->minimumScale() ) : QVariant();
273259
case 3:
274-
return rule->dependsOnScale() ? _formatScale( rule->scaleMinDenom() ) : QVariant();
260+
return rule->dependsOnScale() ? QgsScaleComboBox::toString( rule->maximumScale() ) : QVariant();
275261
case 4:
276262
return rule->settings() ? rule->settings()->fieldName : QVariant();
277263
default:
@@ -306,9 +292,9 @@ QVariant QgsRuleBasedLabelingModel::data( const QModelIndex &index, int role ) c
306292
case 1:
307293
return rule->filterExpression();
308294
case 2:
309-
return rule->scaleMaxDenom();
295+
return rule->minimumScale();
310296
case 3:
311-
return rule->scaleMinDenom();
297+
return rule->maximumScale();
312298
case 4:
313299
return rule->settings() ? rule->settings()->fieldName : QVariant();
314300
default:
@@ -406,10 +392,10 @@ bool QgsRuleBasedLabelingModel::setData( const QModelIndex &index, const QVarian
406392
rule->setFilterExpression( value.toString() );
407393
break;
408394
case 2: // scale min
409-
rule->setScaleMaxDenom( value.toInt() );
395+
rule->setMinimumScale( value.toDouble() );
410396
break;
411397
case 3: // scale max
412-
rule->setScaleMinDenom( value.toInt() );
398+
rule->setMaximumScale( value.toDouble() );
413399
break;
414400
case 4: // label text
415401
if ( !rule->settings() )
@@ -601,10 +587,8 @@ QgsLabelingRulePropsWidget::QgsLabelingRulePropsWidget( QgsRuleBasedLabeling::Ru
601587
{
602588
groupScale->setChecked( true );
603589
// caution: rule uses scale denom, scale widget uses true scales
604-
if ( rule->scaleMinDenom() > 0 )
605-
mScaleRangeWidget->setMaximumScale( rule->scaleMinDenom() );
606-
if ( rule->scaleMaxDenom() > 0 )
607-
mScaleRangeWidget->setMinimumScale( rule->scaleMaxDenom() );
590+
mScaleRangeWidget->setMaximumScale( qMax( rule->maximumScale(), 0.0 ) );
591+
mScaleRangeWidget->setMinimumScale( qMax( rule->minimumScale(), 0.0 ) );
608592
}
609593
mScaleRangeWidget->setMapCanvas( mMapCanvas );
610594

@@ -703,8 +687,7 @@ void QgsLabelingRulePropsWidget::apply()
703687
{
704688
mRule->setFilterExpression( editFilter->text() );
705689
mRule->setDescription( editDescription->text() );
706-
// caution: rule uses scale denom, scale widget uses true scales
707-
mRule->setScaleMinDenom( groupScale->isChecked() ? mScaleRangeWidget->minimumScale() : 0 );
708-
mRule->setScaleMaxDenom( groupScale->isChecked() ? mScaleRangeWidget->maximumScale() : 0 );
690+
mRule->setMinimumScale( groupScale->isChecked() ? mScaleRangeWidget->minimumScale() : 0 );
691+
mRule->setMaximumScale( groupScale->isChecked() ? mScaleRangeWidget->maximumScale() : 0 );
709692
mRule->setSettings( groupSettings->isChecked() ? new QgsPalLayerSettings( mLabelingGui->layerSettings() ) : nullptr );
710693
}

‎src/core/qgsrulebasedlabeling.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ QList<QgsAbstractLabelProvider *> QgsRuleBasedLabelProvider::subProviders()
6262
QgsRuleBasedLabeling::Rule::Rule( QgsPalLayerSettings *settings, int scaleMinDenom, int scaleMaxDenom, const QString &filterExp, const QString &description, bool elseRule )
6363
: mParent( nullptr )
6464
, mSettings( settings )
65-
, mScaleMinDenom( scaleMinDenom )
66-
, mScaleMaxDenom( scaleMaxDenom )
65+
, mMaximumScale( scaleMinDenom )
66+
, mMinimumScale( scaleMaxDenom )
6767
, mFilterExp( filterExp )
6868
, mDescription( description )
6969
, mElseRule( elseRule )
@@ -185,7 +185,7 @@ const QgsRuleBasedLabeling::Rule *QgsRuleBasedLabeling::Rule::findRuleByKey( con
185185
QgsRuleBasedLabeling::Rule *QgsRuleBasedLabeling::Rule::clone() const
186186
{
187187
QgsPalLayerSettings *s = mSettings ? new QgsPalLayerSettings( *mSettings ) : nullptr;
188-
Rule *newrule = new Rule( s, mScaleMinDenom, mScaleMaxDenom, mFilterExp, mDescription );
188+
Rule *newrule = new Rule( s, mMaximumScale, mMinimumScale, mFilterExp, mDescription );
189189
newrule->setActive( mIsActive );
190190
// clone children
191191
Q_FOREACH ( Rule *rule, mChildren )
@@ -243,10 +243,10 @@ QDomElement QgsRuleBasedLabeling::Rule::save( QDomDocument &doc, const QgsReadWr
243243
}
244244
if ( !mFilterExp.isEmpty() )
245245
ruleElem.setAttribute( QStringLiteral( "filter" ), mFilterExp );
246-
if ( mScaleMinDenom != 0 )
247-
ruleElem.setAttribute( QStringLiteral( "scalemindenom" ), mScaleMinDenom );
248-
if ( mScaleMaxDenom != 0 )
249-
ruleElem.setAttribute( QStringLiteral( "scalemaxdenom" ), mScaleMaxDenom );
246+
if ( mMaximumScale != 0 )
247+
ruleElem.setAttribute( QStringLiteral( "scalemindenom" ), mMaximumScale );
248+
if ( mMinimumScale != 0 )
249+
ruleElem.setAttribute( QStringLiteral( "scalemaxdenom" ), mMinimumScale );
250250
if ( !mDescription.isEmpty() )
251251
ruleElem.setAttribute( QStringLiteral( "description" ), mDescription );
252252
if ( !mIsActive )
@@ -364,11 +364,11 @@ bool QgsRuleBasedLabeling::Rule::isScaleOK( double scale ) const
364364
{
365365
if ( qgsDoubleNear( scale, 0.0 ) ) // so that we can count features in classes without scale context
366366
return true;
367-
if ( mScaleMinDenom == 0 && mScaleMaxDenom == 0 )
367+
if ( qgsDoubleNear( mMaximumScale, 0.0 ) && qgsDoubleNear( mMinimumScale, 0.0 ) )
368368
return true;
369-
if ( mScaleMinDenom != 0 && mScaleMinDenom > scale )
369+
if ( !qgsDoubleNear( mMaximumScale, 0.0 ) && mMaximumScale > scale )
370370
return false;
371-
if ( mScaleMaxDenom != 0 && mScaleMaxDenom < scale )
371+
if ( !qgsDoubleNear( mMinimumScale, 0.0 ) && mMinimumScale < scale )
372372
return false;
373373
return true;
374374
}

‎src/core/qgsrulebasedlabeling.h

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
5252
{
5353
public:
5454
//! takes ownership of settings
55-
Rule( QgsPalLayerSettings *settings SIP_TRANSFER, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
55+
Rule( QgsPalLayerSettings *settings SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
5656
~Rule();
5757

5858
//! Rules cannot be copied.
@@ -78,27 +78,27 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
7878
*
7979
* \returns True if scale based labeling is active
8080
*/
81-
bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
81+
bool dependsOnScale() const { return mMinimumScale != 0 || mMaximumScale != 0; }
8282

8383
/**
84-
* The minimum scale at which this label rule should be applied
85-
*
86-
* E.g. Denominator 1000 is a scale of 1:1000, where a rule with minimum denominator
87-
* of 900 will not be applied while a rule with 2000 will be applied.
88-
*
89-
* \returns The minimum scale denominator
84+
* Returns the maximum map scale (i.e. most "zoomed in" scale) at which the label rule will be active.
85+
* The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
86+
* A scale of 0 indicates no maximum scale visibility.
87+
* \see minimumScale()
88+
* \see setMaximumScale()
89+
* \since QGIS 3.0
9090
*/
91-
int scaleMinDenom() const { return mScaleMinDenom; }
91+
double maximumScale() const { return mMaximumScale; }
9292

9393
/**
94-
* The maximum scale denominator at which this label rule should be applied
95-
*
96-
* E.g. Denominator 1000 is a scale of 1:1000, where a rule with maximum denominator
97-
* of 900 will be applied while a rule with 2000 will not be applied.
98-
*
99-
* \returns The maximum scale denominator
94+
* Returns the minimum map scale (i.e. most "zoomed out" scale) at which the label rule will be active.
95+
* The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
96+
* A scale of 0 indicates no minimum scale visibility.
97+
* \see maximumScale()
98+
* \see setMinimumScale()
99+
* \since QGIS 3.0
100100
*/
101-
int scaleMaxDenom() const { return mScaleMaxDenom; }
101+
double minimumScale() const { return mMinimumScale; }
102102

103103
/**
104104
* A filter that will check if this rule applies
@@ -134,20 +134,22 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
134134
void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER );
135135

136136
/**
137-
* Set the minimum denominator for which this rule shall apply.
138-
* E.g. 1000 if it shall be evaluated between 1:1000 and 1:100'000
139-
* Set to 0 to disable the minimum check
140-
* \param scaleMinDenom The minimum scale denominator for this rule
137+
* Sets the minimum map \a scale (i.e. most "zoomed out" scale) at which the label rule will be active.
138+
* The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
139+
* A \a scale of 0 indicates no minimum scale visibility.
140+
* \see minimumScale()
141+
* \see setMaximumScale()
141142
*/
142-
void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
143+
void setMinimumScale( double scale ) { mMinimumScale = scale; }
143144

144145
/**
145-
* Set the maximum denominator for which this rule shall apply.
146-
* E.g. 100'000 if it shall be evaluated between 1:1000 and 1:100'000
147-
* Set to 0 to disable the maximum check
148-
* \param scaleMaxDenom maximum scale denominator for this rule
146+
* Sets the maximum map \a scale (i.e. most "zoomed in" scale) at which the rule will be active.
147+
* The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
148+
* A \a scale of 0 indicates no maximum scale visibility.
149+
* \see maximumScale()
150+
* \see setMinimumScale()
149151
*/
150-
void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
152+
void setMaximumScale( double scale ) { mMaximumScale = scale; }
151153

152154
/**
153155
* Set the expression used to check if a given feature shall be rendered with this rule
@@ -283,8 +285,9 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
283285
bool isFilterOK( QgsFeature &f, QgsRenderContext &context ) const;
284286

285287
/**
286-
* Check if this rule applies for a given scale
287-
* \param scale The scale to check. If set to 0, it will always return true.
288+
* Check if this rule applies for a given \a scale.
289+
* The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
290+
* If set to 0, it will always return true.
288291
*
289292
* \returns If the rule will be evaluated at this scale
290293
*/
@@ -303,7 +306,8 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
303306
private:
304307
Rule *mParent; // parent rule (NULL only for root rule)
305308
QgsPalLayerSettings *mSettings = nullptr;
306-
int mScaleMinDenom, mScaleMaxDenom;
309+
double mMaximumScale = 0;
310+
double mMinimumScale = 0;
307311
QString mFilterExp, mDescription;
308312
bool mElseRule;
309313
RuleList mChildren;

‎src/core/symbology-ng/qgsrulebasedrenderer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ bool QgsRuleBasedRenderer::Rule::isScaleOK( double scale ) const
283283
{
284284
if ( qgsDoubleNear( scale, 0.0 ) ) // so that we can count features in classes without scale context
285285
return true;
286-
if ( mMaximumScale == 0 && mMinimumScale == 0 )
286+
if ( qgsDoubleNear( mMaximumScale, 0.0 ) && qgsDoubleNear( mMinimumScale, 0.0 ) )
287287
return true;
288-
if ( mMaximumScale != 0 && mMaximumScale > scale )
288+
if ( !qgsDoubleNear( mMaximumScale, 0.0 ) && mMaximumScale > scale )
289289
return false;
290-
if ( mMinimumScale != 0 && mMinimumScale < scale )
290+
if ( !qgsDoubleNear( mMinimumScale, 0.0 ) && mMinimumScale < scale )
291291
return false;
292292
return true;
293293
}

0 commit comments

Comments
 (0)
Please sign in to comment.