Skip to content
Permalink
Browse files

Initially populate label properties dialog with data defined results

...even if the data defined property is set to an expression. Although
the values can't be altered for expression based properties, it's
still valuable to show the actual evaluated value for these
properties (for troubleshooting data defined expressions, etc).
  • Loading branch information
nyalldawson committed Mar 29, 2015
1 parent 0071593 commit 727ef9b55725e195b9db2181beed37e363d10cdb
Showing with 174 additions and 98 deletions.
  1. +171 −98 src/app/qgslabelpropertydialog.cpp
  2. +3 −0 src/app/qgslabelpropertydialog.h
@@ -111,6 +111,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
updateFont( mLabelFont, false );

//set all the gui elements to the default layer-level values
mShowLabelChkbx->setChecked( true );
mFontColorButton->setColor( layerSettings.textColor );
mBufferColorButton->setColor( layerSettings.bufferColor );
mLabelDistanceSpinBox->setValue( layerSettings.dist );
@@ -125,47 +126,104 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
disableGuiElements();

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

//set widget values from data defined results
setDataDefinedValues( layerSettings, vlayer );
//enable widgets connected to data defined fields
enableDataDefinedWidgets( vlayer );

blockElementSignals( false );
}

void QgsLabelPropertyDialog::disableGuiElements()
{
mShowLabelChkbx->setEnabled( false );
mAlwaysShowChkbx->setEnabled( false );
mMinScaleSpinBox->setEnabled( false );
mMaxScaleSpinBox->setEnabled( false );
mFontFamilyCmbBx->setEnabled( false );
mFontStyleCmbBx->setEnabled( false );
mFontUnderlineBtn->setEnabled( false );
mFontStrikethroughBtn->setEnabled( false );
mFontBoldBtn->setEnabled( false );
mFontItalicBtn->setEnabled( false );
mFontSizeSpinBox->setEnabled( false );
mBufferSizeSpinBox->setEnabled( false );
mFontColorButton->setEnabled( false );
mBufferColorButton->setEnabled( false );
mLabelDistanceSpinBox->setEnabled( false );
mXCoordSpinBox->setEnabled( false );
mYCoordSpinBox->setEnabled( false );
mHaliComboBox->setEnabled( false );
mValiComboBox->setEnabled( false );
mRotationSpinBox->setEnabled( false );
}

void QgsLabelPropertyDialog::blockElementSignals( bool block )
{
mShowLabelChkbx->blockSignals( block );
mAlwaysShowChkbx->blockSignals( block );
mMinScaleSpinBox->blockSignals( block );
mMaxScaleSpinBox->blockSignals( block );
mFontFamilyCmbBx->blockSignals( block );
mFontStyleCmbBx->blockSignals( block );
mFontUnderlineBtn->blockSignals( block );
mFontStrikethroughBtn->blockSignals( block );
mFontBoldBtn->blockSignals( block );
mFontItalicBtn->blockSignals( block );
mFontSizeSpinBox->blockSignals( block );
mBufferSizeSpinBox->blockSignals( block );
mFontColorButton->blockSignals( block );
mBufferColorButton->blockSignals( block );
mLabelDistanceSpinBox->blockSignals( block );
mXCoordSpinBox->blockSignals( block );
mYCoordSpinBox->blockSignals( block );
mHaliComboBox->blockSignals( block );
mValiComboBox->blockSignals( block );
mRotationSpinBox->blockSignals( block );
}

void QgsLabelPropertyDialog::setDataDefinedValues( QgsPalLayerSettings layerSettings, QgsVectorLayer* vlayer )
{
//loop through data defined properties and set all the GUI widget values. We can do this
//even if the data defined property is set to an expression, as it's useful to show
//users what the evaluated property is...
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* >::const_iterator propIt = mDataDefinedProperties.constBegin();
for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
{
QgsDataDefined* dd = propIt.value();
if ( !dd )
if ( !dd || !dd->isActive() )
{
continue;
}
QString ddField = dd->field();
if ( !dd->isActive() || dd->useExpression() || ddField.isEmpty() )

if ( !dd->expressionIsPrepared() )
{
continue; // can only modify attributes with an active data definition of a mapped field
dd->prepareExpression( vlayer );
}

int ddIndx = vlayer->fieldNameIndex( ddField );
if ( ddIndx == -1 )
QVariant result = layerSettings.dataDefinedValue( propIt.key(), mCurLabelFeat, vlayer->pendingFields() );
if ( !result.isValid() )
{
//could not evaluate data defined value
continue;
}

QgsDebugMsg( QString( "ddField: %1" ).arg( ddField ) );

bool ok = false;
switch ( propIt.key() )
{
case QgsPalLayerSettings::Show:
{ // new scope to assign variables
mShowLabelChkbx->setEnabled( true );
int showLabel = mCurLabelFeat.attribute( ddIndx ).toInt( &ok );
{
int showLabel = result.toInt( &ok );
mShowLabelChkbx->setChecked( !ok || showLabel != 0 );
break;
}
case QgsPalLayerSettings::AlwaysShow:
mAlwaysShowChkbx->setEnabled( true );
mAlwaysShowChkbx->setChecked( mCurLabelFeat.attribute( ddIndx ).toBool() );
mAlwaysShowChkbx->setChecked( result.toBool() );
break;
case QgsPalLayerSettings::MinScale:
{
mMinScaleSpinBox->setEnabled( true );
int minScale = mCurLabelFeat.attribute( ddIndx ).toInt( &ok );
int minScale = result.toInt( &ok );
if ( ok )
{
mMinScaleSpinBox->setValue( minScale );
@@ -174,8 +232,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
}
case QgsPalLayerSettings::MaxScale:
{
mMaxScaleSpinBox->setEnabled( true );
int maxScale = mCurLabelFeat.attribute( ddIndx ).toInt( &ok );
int maxScale = result.toInt( &ok );
if ( ok )
{
mMaxScaleSpinBox->setValue( maxScale );
@@ -184,8 +241,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
}
case QgsPalLayerSettings::BufferSize:
{
mBufferSizeSpinBox->setEnabled( true );
double bufferSize = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
double bufferSize = result.toDouble( &ok );
if ( ok )
{
mBufferSizeSpinBox->setValue( bufferSize );
@@ -194,8 +250,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
}
case QgsPalLayerSettings::PositionX:
{
mXCoordSpinBox->setEnabled( true );
double posX = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
double posX = result.toDouble( &ok );
if ( ok )
{
mXCoordSpinBox->setValue( posX );
@@ -204,8 +259,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
}
case QgsPalLayerSettings::PositionY:
{
mYCoordSpinBox->setEnabled( true );
double posY = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
double posY = result.toDouble( &ok );
if ( ok )
{
mYCoordSpinBox->setValue( posY );
@@ -214,42 +268,122 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
}
case QgsPalLayerSettings::LabelDistance:
{
mLabelDistanceSpinBox->setEnabled( true );
double labelDist = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
double labelDist = result.toDouble( &ok );
if ( ok )
{
mLabelDistanceSpinBox->setValue( labelDist );
}
break;
}
case QgsPalLayerSettings::Hali:
mHaliComboBox->setEnabled( true );
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( mCurLabelFeat.attribute( ddIndx ).toString(), Qt::MatchFixedString ) );
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( result.toString(), Qt::MatchFixedString ) );
break;
case QgsPalLayerSettings::Vali:
mValiComboBox->setEnabled( true );
mValiComboBox->setCurrentIndex( mValiComboBox->findText( mCurLabelFeat.attribute( ddIndx ).toString(), Qt::MatchFixedString ) );
mValiComboBox->setCurrentIndex( mValiComboBox->findText( result.toString(), Qt::MatchFixedString ) );
break;
case QgsPalLayerSettings::BufferColor:
mBufferColorButton->setEnabled( true );
mBufferColorButton->setColor( QColor( mCurLabelFeat.attribute( ddIndx ).toString() ) );
mBufferColorButton->setColor( QColor( result.toString() ) );
break;
case QgsPalLayerSettings::Color:
mFontColorButton->setEnabled( true );
mFontColorButton->setColor( QColor( mCurLabelFeat.attribute( ddIndx ).toString() ) );
mFontColorButton->setColor( QColor( result.toString() ) );
break;
case QgsPalLayerSettings::Rotation:
{
mRotationSpinBox->setEnabled( true );
double rot = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
double rot = result.toDouble( &ok );
if ( ok )
{
mRotationSpinBox->setValue( rot );
}
break;
}

//font related properties
case QgsPalLayerSettings::Size:
{
double size = result.toDouble( &ok );
if ( ok )
{
mFontSizeSpinBox->setValue( size );
}
else
{
mFontSizeSpinBox->setValue( 0 );
}
break;
}
default:
break;
}
}
}

void QgsLabelPropertyDialog::enableDataDefinedWidgets( QgsVectorLayer* vlayer )
{
//loop through data defined properties, this time setting whether or not the widgets are enabled
//this can only be done for properties which are assigned to fields
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* >::const_iterator propIt = mDataDefinedProperties.constBegin();
for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
{
QgsDataDefined* dd = propIt.value();
if ( !dd )
{
continue;
}
QString ddField = dd->field();
if ( !dd->isActive() || dd->useExpression() || ddField.isEmpty() )
{
continue; // can only modify attributes with an active data definition of a mapped field
}

int ddIndx = vlayer->fieldNameIndex( ddField );
if ( ddIndx == -1 )
{
continue;
}

QgsDebugMsg( QString( "ddField: %1" ).arg( ddField ) );

switch ( propIt.key() )
{
case QgsPalLayerSettings::Show:
mShowLabelChkbx->setEnabled( true );
break;
case QgsPalLayerSettings::AlwaysShow:
mAlwaysShowChkbx->setEnabled( true );
break;
case QgsPalLayerSettings::MinScale:
mMinScaleSpinBox->setEnabled( true );
break;
case QgsPalLayerSettings::MaxScale:
mMaxScaleSpinBox->setEnabled( true );
break;
case QgsPalLayerSettings::BufferSize:
mBufferSizeSpinBox->setEnabled( true );
break;
case QgsPalLayerSettings::PositionX:
mXCoordSpinBox->setEnabled( true );
break;
case QgsPalLayerSettings::PositionY:
mYCoordSpinBox->setEnabled( true );
break;
case QgsPalLayerSettings::LabelDistance:
mLabelDistanceSpinBox->setEnabled( true );
break;
case QgsPalLayerSettings::Hali:
mHaliComboBox->setEnabled( true );
break;
case QgsPalLayerSettings::Vali:
mValiComboBox->setEnabled( true );
break;
case QgsPalLayerSettings::BufferColor:
mBufferColorButton->setEnabled( true );
break;
case QgsPalLayerSettings::Color:
mFontColorButton->setEnabled( true );
break;
case QgsPalLayerSettings::Rotation:
mRotationSpinBox->setEnabled( true );
break;
//font related properties
case QgsPalLayerSettings::Family:
mFontFamilyCmbBx->setEnabled( true );
break;
@@ -269,72 +403,11 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
mFontItalicBtn->setEnabled( true );
break;
case QgsPalLayerSettings::Size:
{
mFontSizeSpinBox->setEnabled( true );
double size = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
if ( ok )
{
mFontSizeSpinBox->setValue( size );
}
else
{
mFontSizeSpinBox->setValue( 0 );
}
break;
}
default:
break;
}
}
blockElementSignals( false );
}

void QgsLabelPropertyDialog::disableGuiElements()
{
mShowLabelChkbx->setEnabled( false );
mAlwaysShowChkbx->setEnabled( false );
mMinScaleSpinBox->setEnabled( false );
mMaxScaleSpinBox->setEnabled( false );
mFontFamilyCmbBx->setEnabled( false );
mFontStyleCmbBx->setEnabled( false );
mFontUnderlineBtn->setEnabled( false );
mFontStrikethroughBtn->setEnabled( false );
mFontBoldBtn->setEnabled( false );
mFontItalicBtn->setEnabled( false );
mFontSizeSpinBox->setEnabled( false );
mBufferSizeSpinBox->setEnabled( false );
mFontColorButton->setEnabled( false );
mBufferColorButton->setEnabled( false );
mLabelDistanceSpinBox->setEnabled( false );
mXCoordSpinBox->setEnabled( false );
mYCoordSpinBox->setEnabled( false );
mHaliComboBox->setEnabled( false );
mValiComboBox->setEnabled( false );
mRotationSpinBox->setEnabled( false );
}

void QgsLabelPropertyDialog::blockElementSignals( bool block )
{
mShowLabelChkbx->blockSignals( block );
mAlwaysShowChkbx->blockSignals( block );
mMinScaleSpinBox->blockSignals( block );
mMaxScaleSpinBox->blockSignals( block );
mFontFamilyCmbBx->blockSignals( block );
mFontStyleCmbBx->blockSignals( block );
mFontUnderlineBtn->blockSignals( block );
mFontStrikethroughBtn->blockSignals( block );
mFontBoldBtn->blockSignals( block );
mFontItalicBtn->blockSignals( block );
mFontSizeSpinBox->blockSignals( block );
mBufferSizeSpinBox->blockSignals( block );
mFontColorButton->blockSignals( block );
mBufferColorButton->blockSignals( block );
mLabelDistanceSpinBox->blockSignals( block );
mXCoordSpinBox->blockSignals( block );
mYCoordSpinBox->blockSignals( block );
mHaliComboBox->blockSignals( block );
mValiComboBox->blockSignals( block );
mRotationSpinBox->blockSignals( block );
}

void QgsLabelPropertyDialog::updateFont( const QFont& font, bool block )
@@ -65,6 +65,9 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
/**Block / unblock all input element signals*/
void blockElementSignals( bool block );

void setDataDefinedValues( QgsPalLayerSettings layerSettings, QgsVectorLayer* vlayer );
void enableDataDefinedWidgets( QgsVectorLayer* vlayer );

/** Updates font when family or style is updated */
void updateFont( const QFont& font, bool block = true );

0 comments on commit 727ef9b

Please sign in to comment.
You can’t perform that action at this time.