Skip to content

Commit

Permalink
Add min/max scale-based visibility to advanced labeling data defined …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
dakcarto committed Aug 13, 2012
1 parent ad6c964 commit 2263435
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/app/qgslabelinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
setDataDefinedProperty( mLabelDistanceComboBox, QgsPalLayerSettings::LabelDistance, lyr );
setDataDefinedProperty( mRotationComboBox, QgsPalLayerSettings::Rotation, lyr );
setDataDefinedProperty( mShowLabelAttributeComboBox, QgsPalLayerSettings::Show, lyr );
setDataDefinedProperty( mMinScaleAttributeComboBox, QgsPalLayerSettings::MinScale, lyr );
setDataDefinedProperty( mMaxScaleAttributeComboBox, QgsPalLayerSettings::MaxScale, lyr );

return lyr;
}
Expand Down Expand Up @@ -419,6 +421,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
comboList << mLabelDistanceComboBox;
comboList << mRotationComboBox;
comboList << mShowLabelAttributeComboBox;
comboList << mMinScaleAttributeComboBox;
comboList << mMaxScaleAttributeComboBox;

QList<QComboBox*>::iterator comboIt = comboList.begin();
for ( ; comboIt != comboList.end(); ++comboIt )
Expand Down Expand Up @@ -453,6 +457,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
setCurrentComboValue( mLabelDistanceComboBox, s, QgsPalLayerSettings::LabelDistance );
setCurrentComboValue( mRotationComboBox, s, QgsPalLayerSettings::Rotation );
setCurrentComboValue( mShowLabelAttributeComboBox, s, QgsPalLayerSettings::Show );
setCurrentComboValue( mMinScaleAttributeComboBox, s, QgsPalLayerSettings::MinScale );
setCurrentComboValue( mMaxScaleAttributeComboBox, s, QgsPalLayerSettings::MaxScale );
}

void QgsLabelingGui::changePreviewBackground()
Expand Down
24 changes: 24 additions & 0 deletions src/app/qgslabelpropertydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
mBufferColorButton->setColor( layerSettings.textColor );
mLabelDistanceSpinBox->setValue( layerSettings.dist );
mBufferSizeSpinBox->setValue( layerSettings.bufferSize );
mMinScaleSpinBox->setValue( layerSettings.scaleMin );
mMaxScaleSpinBox->setValue( layerSettings.scaleMax );
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( "Left" ) );
mValiComboBox->setCurrentIndex( mValiComboBox->findText( "Bottom" ) );

Expand All @@ -112,6 +114,14 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
mShowLabelChkbx->setEnabled( true );
mShowLabelChkbx->setChecked( attributeValues[propIt.value()].toInt() != 0 );
break;
case QgsPalLayerSettings::MinScale:
mMinScaleSpinBox->setEnabled( true );
mMinScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
break;
case QgsPalLayerSettings::MaxScale:
mMaxScaleSpinBox->setEnabled( true );
mMaxScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
break;
case QgsPalLayerSettings::Size:
mFontSizeSpinBox->setEnabled( true );
mLabelFont.setPointSizeF( attributeValues[propIt.value()].toDouble() );
Expand Down Expand Up @@ -181,6 +191,8 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
void QgsLabelPropertyDialog::disableGuiElements()
{
mShowLabelChkbx->setEnabled( false );
mMinScaleSpinBox->setEnabled( false );
mMaxScaleSpinBox->setEnabled( false );
mFontSizeSpinBox->setEnabled( false );
mBufferSizeSpinBox->setEnabled( false );
mFontPushButton->setEnabled( false );
Expand All @@ -197,6 +209,8 @@ void QgsLabelPropertyDialog::disableGuiElements()
void QgsLabelPropertyDialog::blockElementSignals( bool block )
{
mShowLabelChkbx->blockSignals( block );
mMinScaleSpinBox->blockSignals( block );
mMaxScaleSpinBox->blockSignals( block );
mFontSizeSpinBox->blockSignals( block );
mBufferSizeSpinBox->blockSignals( block );
mFontPushButton->blockSignals( block );
Expand Down Expand Up @@ -229,6 +243,16 @@ void QgsLabelPropertyDialog::on_mShowLabelChkbx_toggled( bool chkd )
insertChangedValue( QgsPalLayerSettings::Show, ( chkd ? 1 : 0 ) );
}

void QgsLabelPropertyDialog::on_mMinScaleSpinBox_valueChanged( int i )
{
insertChangedValue( QgsPalLayerSettings::MinScale, i );
}

void QgsLabelPropertyDialog::on_mMaxScaleSpinBox_valueChanged(int i )
{
insertChangedValue( QgsPalLayerSettings::MaxScale, i );
}

void QgsLabelPropertyDialog::on_mLabelDistanceSpinBox_valueChanged( double d )
{
insertChangedValue( QgsPalLayerSettings::LabelDistance, d );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgslabelpropertydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog

private slots:
void on_mShowLabelChkbx_toggled( bool chkd );
void on_mMinScaleSpinBox_valueChanged(int i );
void on_mMaxScaleSpinBox_valueChanged( int i );
void on_mLabelDistanceSpinBox_valueChanged( double d );
void on_mXCoordSpinBox_valueChanged( double d );
void on_mYCoordSpinBox_valueChanged( double d );
Expand Down
38 changes: 37 additions & 1 deletion src/core/qgspallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void _writeDataDefinedPropertyMap( QgsVectorLayer* layer, const QMap< Qgs
{
return;
}
for ( int i = 0; i < 16; ++i )
for ( int i = 0; i < 18; ++i )
{
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator it = propertyMap.find(( QgsPalLayerSettings::DataDefinedProperties )i );
QVariant propertyValue;
Expand Down Expand Up @@ -301,6 +301,8 @@ static void _readDataDefinedPropertyMap( QgsVectorLayer* layer, QMap< QgsPalLaye
_readDataDefinedProperty( layer, QgsPalLayerSettings::LabelDistance, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::Rotation, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::Show, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::MinScale, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::MaxScale, propertyMap );
}

void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
Expand Down Expand Up @@ -486,6 +488,40 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
}
}

// data defined min scale?
QMap< DataDefinedProperties, int >::const_iterator minScaleIt = dataDefinedProperties.find( QgsPalLayerSettings::MinScale );
if ( minScaleIt != dataDefinedProperties.constEnd() )
{
QVariant minScaleValue = f.attributeMap().value( *minScaleIt );
if ( minScaleValue.isValid() )
{
bool conversionOk;
int minScale = minScaleValue.toInt( &conversionOk );
// TODO: occasional floating point issues?
if ( conversionOk && int( context.rendererScale() ) < minScale )
{
return;
}
}
}

// data defined max scale?
QMap< DataDefinedProperties, int >::const_iterator maxScaleIt = dataDefinedProperties.find( QgsPalLayerSettings::MaxScale );
if ( maxScaleIt != dataDefinedProperties.constEnd() )
{
QVariant maxScaleValue = f.attributeMap().value( *maxScaleIt );
if ( maxScaleValue.isValid() )
{
bool conversionOk;
int maxScale = maxScaleValue.toInt( &conversionOk );
// TODO: occasional floating point issues?
if ( conversionOk && int( context.rendererScale() ) > maxScale )
{
return;
}
}
}

QString labelText;
// Check to see if we are a expression string.
if ( isExpression )
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgspallabeling.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class CORE_EXPORT QgsPalLayerSettings
Vali, //vertical alignment for data defined label position (Bottom, Base, Half, Cap, Top)
LabelDistance,
Rotation, //data defined rotation (only useful in connection with data defined position)
Show
Show,
MinScale,
MaxScale
};

QString fieldName;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/qgslabelingguibase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@
<item row="1" column="1">
<widget class="QComboBox" name="mMinScaleAttributeComboBox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
Expand Down Expand Up @@ -2085,7 +2085,7 @@
<item row="2" column="1">
<widget class="QComboBox" name="mMaxScaleAttributeComboBox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
Expand Down
Loading

0 comments on commit 2263435

Please sign in to comment.