Skip to content

Commit 2263435

Browse files
committed
Add min/max scale-based visibility to advanced labeling data defined columns
- Layer-level options, if set, will override data defined values (correct behavior IMHO) - Spinboxes for min and max added to Change Label tool's dialog - Seems to still be a bug in renderer scale-to-value comparison, where equal scales are not always equal if user directly enters values in the map canvas scale combobox
1 parent ad6c964 commit 2263435

File tree

7 files changed

+217
-36
lines changed

7 files changed

+217
-36
lines changed

src/app/qgslabelinggui.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
354354
setDataDefinedProperty( mLabelDistanceComboBox, QgsPalLayerSettings::LabelDistance, lyr );
355355
setDataDefinedProperty( mRotationComboBox, QgsPalLayerSettings::Rotation, lyr );
356356
setDataDefinedProperty( mShowLabelAttributeComboBox, QgsPalLayerSettings::Show, lyr );
357+
setDataDefinedProperty( mMinScaleAttributeComboBox, QgsPalLayerSettings::MinScale, lyr );
358+
setDataDefinedProperty( mMaxScaleAttributeComboBox, QgsPalLayerSettings::MaxScale, lyr );
357359

358360
return lyr;
359361
}
@@ -419,6 +421,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
419421
comboList << mLabelDistanceComboBox;
420422
comboList << mRotationComboBox;
421423
comboList << mShowLabelAttributeComboBox;
424+
comboList << mMinScaleAttributeComboBox;
425+
comboList << mMaxScaleAttributeComboBox;
422426

423427
QList<QComboBox*>::iterator comboIt = comboList.begin();
424428
for ( ; comboIt != comboList.end(); ++comboIt )
@@ -453,6 +457,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
453457
setCurrentComboValue( mLabelDistanceComboBox, s, QgsPalLayerSettings::LabelDistance );
454458
setCurrentComboValue( mRotationComboBox, s, QgsPalLayerSettings::Rotation );
455459
setCurrentComboValue( mShowLabelAttributeComboBox, s, QgsPalLayerSettings::Show );
460+
setCurrentComboValue( mMinScaleAttributeComboBox, s, QgsPalLayerSettings::MinScale );
461+
setCurrentComboValue( mMaxScaleAttributeComboBox, s, QgsPalLayerSettings::MaxScale );
456462
}
457463

458464
void QgsLabelingGui::changePreviewBackground()

src/app/qgslabelpropertydialog.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
9696
mBufferColorButton->setColor( layerSettings.textColor );
9797
mLabelDistanceSpinBox->setValue( layerSettings.dist );
9898
mBufferSizeSpinBox->setValue( layerSettings.bufferSize );
99+
mMinScaleSpinBox->setValue( layerSettings.scaleMin );
100+
mMaxScaleSpinBox->setValue( layerSettings.scaleMax );
99101
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( "Left" ) );
100102
mValiComboBox->setCurrentIndex( mValiComboBox->findText( "Bottom" ) );
101103

@@ -112,6 +114,14 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
112114
mShowLabelChkbx->setEnabled( true );
113115
mShowLabelChkbx->setChecked( attributeValues[propIt.value()].toInt() != 0 );
114116
break;
117+
case QgsPalLayerSettings::MinScale:
118+
mMinScaleSpinBox->setEnabled( true );
119+
mMinScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
120+
break;
121+
case QgsPalLayerSettings::MaxScale:
122+
mMaxScaleSpinBox->setEnabled( true );
123+
mMaxScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
124+
break;
115125
case QgsPalLayerSettings::Size:
116126
mFontSizeSpinBox->setEnabled( true );
117127
mLabelFont.setPointSizeF( attributeValues[propIt.value()].toDouble() );
@@ -181,6 +191,8 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
181191
void QgsLabelPropertyDialog::disableGuiElements()
182192
{
183193
mShowLabelChkbx->setEnabled( false );
194+
mMinScaleSpinBox->setEnabled( false );
195+
mMaxScaleSpinBox->setEnabled( false );
184196
mFontSizeSpinBox->setEnabled( false );
185197
mBufferSizeSpinBox->setEnabled( false );
186198
mFontPushButton->setEnabled( false );
@@ -197,6 +209,8 @@ void QgsLabelPropertyDialog::disableGuiElements()
197209
void QgsLabelPropertyDialog::blockElementSignals( bool block )
198210
{
199211
mShowLabelChkbx->blockSignals( block );
212+
mMinScaleSpinBox->blockSignals( block );
213+
mMaxScaleSpinBox->blockSignals( block );
200214
mFontSizeSpinBox->blockSignals( block );
201215
mBufferSizeSpinBox->blockSignals( block );
202216
mFontPushButton->blockSignals( block );
@@ -229,6 +243,16 @@ void QgsLabelPropertyDialog::on_mShowLabelChkbx_toggled( bool chkd )
229243
insertChangedValue( QgsPalLayerSettings::Show, ( chkd ? 1 : 0 ) );
230244
}
231245

246+
void QgsLabelPropertyDialog::on_mMinScaleSpinBox_valueChanged( int i )
247+
{
248+
insertChangedValue( QgsPalLayerSettings::MinScale, i );
249+
}
250+
251+
void QgsLabelPropertyDialog::on_mMaxScaleSpinBox_valueChanged(int i )
252+
{
253+
insertChangedValue( QgsPalLayerSettings::MaxScale, i );
254+
}
255+
232256
void QgsLabelPropertyDialog::on_mLabelDistanceSpinBox_valueChanged( double d )
233257
{
234258
insertChangedValue( QgsPalLayerSettings::LabelDistance, d );

src/app/qgslabelpropertydialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog
3838

3939
private slots:
4040
void on_mShowLabelChkbx_toggled( bool chkd );
41+
void on_mMinScaleSpinBox_valueChanged(int i );
42+
void on_mMaxScaleSpinBox_valueChanged( int i );
4143
void on_mLabelDistanceSpinBox_valueChanged( double d );
4244
void on_mXCoordSpinBox_valueChanged( double d );
4345
void on_mYCoordSpinBox_valueChanged( double d );

src/core/qgspallabeling.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static void _writeDataDefinedPropertyMap( QgsVectorLayer* layer, const QMap< Qgs
245245
{
246246
return;
247247
}
248-
for ( int i = 0; i < 16; ++i )
248+
for ( int i = 0; i < 18; ++i )
249249
{
250250
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator it = propertyMap.find(( QgsPalLayerSettings::DataDefinedProperties )i );
251251
QVariant propertyValue;
@@ -301,6 +301,8 @@ static void _readDataDefinedPropertyMap( QgsVectorLayer* layer, QMap< QgsPalLaye
301301
_readDataDefinedProperty( layer, QgsPalLayerSettings::LabelDistance, propertyMap );
302302
_readDataDefinedProperty( layer, QgsPalLayerSettings::Rotation, propertyMap );
303303
_readDataDefinedProperty( layer, QgsPalLayerSettings::Show, propertyMap );
304+
_readDataDefinedProperty( layer, QgsPalLayerSettings::MinScale, propertyMap );
305+
_readDataDefinedProperty( layer, QgsPalLayerSettings::MaxScale, propertyMap );
304306
}
305307

306308
void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
@@ -486,6 +488,40 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
486488
}
487489
}
488490

491+
// data defined min scale?
492+
QMap< DataDefinedProperties, int >::const_iterator minScaleIt = dataDefinedProperties.find( QgsPalLayerSettings::MinScale );
493+
if ( minScaleIt != dataDefinedProperties.constEnd() )
494+
{
495+
QVariant minScaleValue = f.attributeMap().value( *minScaleIt );
496+
if ( minScaleValue.isValid() )
497+
{
498+
bool conversionOk;
499+
int minScale = minScaleValue.toInt( &conversionOk );
500+
// TODO: occasional floating point issues?
501+
if ( conversionOk && int( context.rendererScale() ) < minScale )
502+
{
503+
return;
504+
}
505+
}
506+
}
507+
508+
// data defined max scale?
509+
QMap< DataDefinedProperties, int >::const_iterator maxScaleIt = dataDefinedProperties.find( QgsPalLayerSettings::MaxScale );
510+
if ( maxScaleIt != dataDefinedProperties.constEnd() )
511+
{
512+
QVariant maxScaleValue = f.attributeMap().value( *maxScaleIt );
513+
if ( maxScaleValue.isValid() )
514+
{
515+
bool conversionOk;
516+
int maxScale = maxScaleValue.toInt( &conversionOk );
517+
// TODO: occasional floating point issues?
518+
if ( conversionOk && int( context.rendererScale() ) > maxScale )
519+
{
520+
return;
521+
}
522+
}
523+
}
524+
489525
QString labelText;
490526
// Check to see if we are a expression string.
491527
if ( isExpression )

src/core/qgspallabeling.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ class CORE_EXPORT QgsPalLayerSettings
9898
Vali, //vertical alignment for data defined label position (Bottom, Base, Half, Cap, Top)
9999
LabelDistance,
100100
Rotation, //data defined rotation (only useful in connection with data defined position)
101-
Show
101+
Show,
102+
MinScale,
103+
MaxScale
102104
};
103105

104106
QString fieldName;

src/ui/qgslabelingguibase.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,7 +2047,7 @@
20472047
<item row="1" column="1">
20482048
<widget class="QComboBox" name="mMinScaleAttributeComboBox">
20492049
<property name="enabled">
2050-
<bool>false</bool>
2050+
<bool>true</bool>
20512051
</property>
20522052
</widget>
20532053
</item>
@@ -2085,7 +2085,7 @@
20852085
<item row="2" column="1">
20862086
<widget class="QComboBox" name="mMaxScaleAttributeComboBox">
20872087
<property name="enabled">
2088-
<bool>false</bool>
2088+
<bool>true</bool>
20892089
</property>
20902090
</widget>
20912091
</item>

0 commit comments

Comments
 (0)