Skip to content

Commit

Permalink
Update QgsPalLabeling to work with new vector api
Browse files Browse the repository at this point in the history
- Switch data defined properties from field index- to name-based
- Add routine to migrate properties to new keys:  labeling/dataDefined/[enum name]
- Add QHash containing old-style data provider mapping of index to name for use in properties migration
- Update QgsLabelingGui, QgsMapToolLabel, QgsLabelPropertyDialog to work with name-based properties
  • Loading branch information
dakcarto committed Feb 7, 2013
1 parent 4e64d72 commit fbf9991
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 167 deletions.
14 changes: 9 additions & 5 deletions python/core/qgspallabeling.sip
Expand Up @@ -48,7 +48,7 @@ class QgsPalLayerSettings
MultiRight
};

// increment iterator in _writeDataDefinedPropertyMap() when adding more
// update mDataDefinedNames in constructor when adding new enum value
enum DataDefinedProperties
{
Size = 0,
Expand All @@ -71,8 +71,7 @@ class QgsPalLayerSettings
MaxScale,
FontTransp,
BufferTransp,
AlwaysShow,
PropertyCount, // keep last entry
AlwaysShow
};

QString fieldName;
Expand Down Expand Up @@ -157,11 +156,11 @@ class QgsPalLayerSettings
void writeToLayer( QgsVectorLayer* layer );

/**Set a property as data defined*/
void setDataDefinedProperty( DataDefinedProperties p, int attributeIndex );
void setDataDefinedProperty( DataDefinedProperties p, QString attributeName );
/**Set a property to static instead data defined*/
void removeDataDefinedProperty( DataDefinedProperties p );

/**Stores field indices for data defined layer properties*/
/**Stores field names for data defined layer properties*/
// QMap< DataDefinedProperties, int > dataDefinedProperties;

bool preserveRotation; // preserve predefined rotation data during label pin/unpin operations
Expand All @@ -172,6 +171,11 @@ class QgsPalLayerSettings
@param buffer whether it buffer size being calculated
@return font pixel size*/
int sizeToPixel( double size, const QgsRenderContext& c , bool buffer = false ) const;

/** List of data defined enum names
* @note adding in 1.9
*/
QList<QString> dataDefinedNames() const;
};

class QgsLabelCandidate
Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -215,6 +215,11 @@ class QgsVectorDataProvider : QgsDataProvider
*/
virtual QList<int> pkAttributeIndexes();

/**
* Return list of indexes to names for QgsPalLabeling fix
*/
virtual QHash<int, QString> palAttributeIndexNames() const;

/**
* Set whether provider should also return features that don't have
* associated geometry. false by default
Expand Down
13 changes: 4 additions & 9 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -574,12 +574,7 @@ void QgsLabelingGui::setDataDefinedProperty( const QComboBox* c, QgsPalLayerSett
{
return;
}

QVariant propertyField = c->itemData( c->currentIndex() );
if ( propertyField.isValid() )
{
lyr.setDataDefinedProperty( p, propertyField.toInt() );
}
lyr.setDataDefinedProperty( p, c->currentText() ); // "" value effectively clears setting
}

void QgsLabelingGui::setCurrentComboValue( QComboBox* c, const QgsPalLayerSettings& s, QgsPalLayerSettings::DataDefinedProperties p )
Expand All @@ -589,14 +584,14 @@ void QgsLabelingGui::setCurrentComboValue( QComboBox* c, const QgsPalLayerSettin
return;
}

QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator it = s.dataDefinedProperties.find( p );
if ( it == s.dataDefinedProperties.constEnd() )
QMap< QgsPalLayerSettings::DataDefinedProperties, QString >::const_iterator it = s.dataDefinedProperties.find( p );
if ( it == s.dataDefinedProperties.constEnd() || it.value().isEmpty() )
{
c->setCurrentIndex( 0 );
}
else
{
c->setCurrentIndex( c->findData( it.value() ) );
c->setCurrentIndex( c->findText( it.value() ) );
}
}

Expand Down
63 changes: 31 additions & 32 deletions src/app/qgslabelpropertydialog.cpp
Expand Up @@ -23,7 +23,7 @@
#include <QFontDialog>

QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QString& labelText, QgsMapRenderer* renderer, QWidget * parent, Qt::WindowFlags f ):
QDialog( parent, f ), mMapRenderer( renderer ), mCurrentLabelField( -1 )
QDialog( parent, f ), mMapRenderer( renderer ), mCurLabelField( -1 )
{
setupUi( this );
fillHaliComboBox();
Expand All @@ -49,12 +49,11 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
return;
}

QgsFeature f;
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( mCurLabelFeat ) )
{
return;
}
const QgsAttributes& attributeValues = f.attributes();
const QgsAttributes& attributeValues = mCurLabelFeat.attributes();

//get layerproperties. Problem: only for pallabeling...
QgsPalLabeling* lbl = dynamic_cast<QgsPalLabeling*>( mMapRenderer->labelingEngine() );
Expand All @@ -79,10 +78,10 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
QString labelFieldName = vlayer->customProperty( "labeling/fieldName" ).toString();
if ( !labelFieldName.isEmpty() )
{
mCurrentLabelField = vlayer->fieldNameIndex( labelFieldName );
mLabelTextLineEdit->setText( attributeValues[mCurrentLabelField].toString() );
mCurLabelField = vlayer->fieldNameIndex( labelFieldName );
mLabelTextLineEdit->setText( attributeValues[mCurLabelField].toString() );
const QgsFields& layerFields = vlayer->pendingFields();
switch ( layerFields[mCurrentLabelField].type() )
switch ( layerFields[mCurLabelField].type() )
{
case QVariant::Double:
mLabelTextLineEdit->setValidator( new QDoubleValidator( this ) );
Expand Down Expand Up @@ -114,85 +113,85 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
disableGuiElements();

mDataDefinedProperties = layerSettings.dataDefinedProperties;
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator propIt = mDataDefinedProperties.constBegin();
QMap< QgsPalLayerSettings::DataDefinedProperties, QString >::const_iterator propIt = mDataDefinedProperties.constBegin();

for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
{
switch ( propIt.key() )
{
case QgsPalLayerSettings::Show:
mShowLabelChkbx->setEnabled( true );
mShowLabelChkbx->setChecked( attributeValues[propIt.value()].toInt() != 0 );
mShowLabelChkbx->setChecked( mCurLabelFeat.attribute( propIt.value() ).toInt() != 0 );
break;
case QgsPalLayerSettings::AlwaysShow:
mAlwaysShowChkbx->setEnabled( true );
mAlwaysShowChkbx->setChecked( attributeValues[propIt.value()].toBool() );
mAlwaysShowChkbx->setChecked( mCurLabelFeat.attribute( propIt.value() ).toBool() );
break;
case QgsPalLayerSettings::MinScale:
mMinScaleSpinBox->setEnabled( true );
mMinScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
mMinScaleSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toInt() );
break;
case QgsPalLayerSettings::MaxScale:
mMaxScaleSpinBox->setEnabled( true );
mMaxScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
mMaxScaleSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toInt() );
break;
case QgsPalLayerSettings::Size:
mFontSizeSpinBox->setEnabled( true );
mLabelFont.setPointSizeF( attributeValues[propIt.value()].toDouble() );
mFontSizeSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
mLabelFont.setPointSizeF( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
mFontSizeSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
break;
case QgsPalLayerSettings::BufferSize:
mBufferSizeSpinBox->setEnabled( true );
mBufferSizeSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
mBufferSizeSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
break;
case QgsPalLayerSettings::PositionX:
mXCoordSpinBox->setEnabled( true );
mXCoordSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
mXCoordSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
break;
case QgsPalLayerSettings::PositionY:
mYCoordSpinBox->setEnabled( true );
mYCoordSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
mYCoordSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
break;
case QgsPalLayerSettings::LabelDistance:
mLabelDistanceSpinBox->setEnabled( true );
mLabelDistanceSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
mLabelDistanceSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
break;
case QgsPalLayerSettings::Hali:
mHaliComboBox->setEnabled( true );
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( attributeValues[propIt.value()].toString() ) );
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( mCurLabelFeat.attribute( propIt.value() ).toString() ) );
break;
case QgsPalLayerSettings::Vali:
mValiComboBox->setEnabled( true );
mValiComboBox->setCurrentIndex( mValiComboBox->findText( attributeValues[propIt.value()].toString() ) );
mValiComboBox->setCurrentIndex( mValiComboBox->findText( mCurLabelFeat.attribute( propIt.value() ).toString() ) );
break;
case QgsPalLayerSettings::BufferColor:
mBufferColorButton->setEnabled( true );
mBufferColorButton->setColor( QColor( attributeValues[propIt.value()].toString() ) );
mBufferColorButton->setColor( QColor( mCurLabelFeat.attribute( propIt.value() ).toString() ) );
break;
case QgsPalLayerSettings::Color:
mFontColorButton->setEnabled( true );
mFontColorButton->setColor( QColor( attributeValues[propIt.value()].toString() ) );
mFontColorButton->setColor( QColor( mCurLabelFeat.attribute( propIt.value() ).toString() ) );
break;
case QgsPalLayerSettings::Rotation:
mRotationSpinBox->setEnabled( true );
mRotationSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
mRotationSpinBox->setValue( mCurLabelFeat.attribute( propIt.value() ).toDouble() );
break;

//font related properties
case QgsPalLayerSettings::Bold:
mLabelFont.setBold( attributeValues[propIt.value()].toBool() );
mLabelFont.setBold( mCurLabelFeat.attribute( propIt.value() ).toBool() );
break;
case QgsPalLayerSettings::Italic:
mLabelFont.setItalic( attributeValues[propIt.value()].toBool() );
mLabelFont.setItalic( mCurLabelFeat.attribute( propIt.value() ).toBool() );
break;
case QgsPalLayerSettings::Underline:
mLabelFont.setUnderline( attributeValues[propIt.value()].toBool() );
mLabelFont.setUnderline( mCurLabelFeat.attribute( propIt.value() ).toBool() );
break;
case QgsPalLayerSettings::Strikeout:
mLabelFont.setStrikeOut( attributeValues[propIt.value()].toBool() );
mLabelFont.setStrikeOut( mCurLabelFeat.attribute( propIt.value() ).toBool() );
break;
case QgsPalLayerSettings::Family:
mLabelFont.setFamily( attributeValues[propIt.value()].toString() );
mLabelFont.setFamily( mCurLabelFeat.attribute( propIt.value() ).toString() );
break;
default:
break;
Expand Down Expand Up @@ -363,18 +362,18 @@ void QgsLabelPropertyDialog::on_mValiComboBox_currentIndexChanged( const QString

void QgsLabelPropertyDialog::on_mLabelTextLineEdit_textChanged( const QString& text )
{
if ( mCurrentLabelField != -1 )
if ( mCurLabelField != -1 )
{
mChangedProperties.insert( mCurrentLabelField, text );
mChangedProperties.insert( mCurLabelField, text );
}
}

void QgsLabelPropertyDialog::insertChangedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant value )
{
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator ddIt = mDataDefinedProperties.find( p );
QMap< QgsPalLayerSettings::DataDefinedProperties, QString >::const_iterator ddIt = mDataDefinedProperties.find( p );
if ( ddIt != mDataDefinedProperties.constEnd() )
{
mChangedProperties.insert( ddIt.value(), value );
mChangedProperties.insert( mCurLabelFeat.fieldNameIndex( ddIt.value() ), value );
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/app/qgslabelpropertydialog.h
Expand Up @@ -73,11 +73,16 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog
QgsMapRenderer* mMapRenderer;

QgsAttributeMap mChangedProperties;
QMap< QgsPalLayerSettings::DataDefinedProperties, int > mDataDefinedProperties;
QMap< QgsPalLayerSettings::DataDefinedProperties, QString > mDataDefinedProperties;
QFont mLabelFont;

/**Label field for the current layer (or -1 if none)*/
int mCurrentLabelField;
int mCurLabelField;

/** Current feature
* @note added in 1.9
*/
QgsFeature mCurLabelFeat;
};

#endif // QGSLAYERPROPERTYDIALOG_H

0 comments on commit fbf9991

Please sign in to comment.