Skip to content

Commit 727ef9b

Browse files
committedMar 29, 2015
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).
1 parent 0071593 commit 727ef9b

File tree

2 files changed

+174
-98
lines changed

2 files changed

+174
-98
lines changed
 

‎src/app/qgslabelpropertydialog.cpp

Lines changed: 171 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
111111
updateFont( mLabelFont, false );
112112

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

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

130+
//set widget values from data defined results
131+
setDataDefinedValues( layerSettings, vlayer );
132+
//enable widgets connected to data defined fields
133+
enableDataDefinedWidgets( vlayer );
134+
135+
blockElementSignals( false );
136+
}
137+
138+
void QgsLabelPropertyDialog::disableGuiElements()
139+
{
140+
mShowLabelChkbx->setEnabled( false );
141+
mAlwaysShowChkbx->setEnabled( false );
142+
mMinScaleSpinBox->setEnabled( false );
143+
mMaxScaleSpinBox->setEnabled( false );
144+
mFontFamilyCmbBx->setEnabled( false );
145+
mFontStyleCmbBx->setEnabled( false );
146+
mFontUnderlineBtn->setEnabled( false );
147+
mFontStrikethroughBtn->setEnabled( false );
148+
mFontBoldBtn->setEnabled( false );
149+
mFontItalicBtn->setEnabled( false );
150+
mFontSizeSpinBox->setEnabled( false );
151+
mBufferSizeSpinBox->setEnabled( false );
152+
mFontColorButton->setEnabled( false );
153+
mBufferColorButton->setEnabled( false );
154+
mLabelDistanceSpinBox->setEnabled( false );
155+
mXCoordSpinBox->setEnabled( false );
156+
mYCoordSpinBox->setEnabled( false );
157+
mHaliComboBox->setEnabled( false );
158+
mValiComboBox->setEnabled( false );
159+
mRotationSpinBox->setEnabled( false );
160+
}
161+
162+
void QgsLabelPropertyDialog::blockElementSignals( bool block )
163+
{
164+
mShowLabelChkbx->blockSignals( block );
165+
mAlwaysShowChkbx->blockSignals( block );
166+
mMinScaleSpinBox->blockSignals( block );
167+
mMaxScaleSpinBox->blockSignals( block );
168+
mFontFamilyCmbBx->blockSignals( block );
169+
mFontStyleCmbBx->blockSignals( block );
170+
mFontUnderlineBtn->blockSignals( block );
171+
mFontStrikethroughBtn->blockSignals( block );
172+
mFontBoldBtn->blockSignals( block );
173+
mFontItalicBtn->blockSignals( block );
174+
mFontSizeSpinBox->blockSignals( block );
175+
mBufferSizeSpinBox->blockSignals( block );
176+
mFontColorButton->blockSignals( block );
177+
mBufferColorButton->blockSignals( block );
178+
mLabelDistanceSpinBox->blockSignals( block );
179+
mXCoordSpinBox->blockSignals( block );
180+
mYCoordSpinBox->blockSignals( block );
181+
mHaliComboBox->blockSignals( block );
182+
mValiComboBox->blockSignals( block );
183+
mRotationSpinBox->blockSignals( block );
184+
}
185+
186+
void QgsLabelPropertyDialog::setDataDefinedValues( QgsPalLayerSettings layerSettings, QgsVectorLayer* vlayer )
187+
{
188+
//loop through data defined properties and set all the GUI widget values. We can do this
189+
//even if the data defined property is set to an expression, as it's useful to show
190+
//users what the evaluated property is...
191+
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* >::const_iterator propIt = mDataDefinedProperties.constBegin();
130192
for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
131193
{
132194
QgsDataDefined* dd = propIt.value();
133-
if ( !dd )
195+
if ( !dd || !dd->isActive() )
134196
{
135197
continue;
136198
}
137-
QString ddField = dd->field();
138-
if ( !dd->isActive() || dd->useExpression() || ddField.isEmpty() )
199+
200+
if ( !dd->expressionIsPrepared() )
139201
{
140-
continue; // can only modify attributes with an active data definition of a mapped field
202+
dd->prepareExpression( vlayer );
141203
}
142204

143-
int ddIndx = vlayer->fieldNameIndex( ddField );
144-
if ( ddIndx == -1 )
205+
QVariant result = layerSettings.dataDefinedValue( propIt.key(), mCurLabelFeat, vlayer->pendingFields() );
206+
if ( !result.isValid() )
145207
{
208+
//could not evaluate data defined value
146209
continue;
147210
}
148211

149-
QgsDebugMsg( QString( "ddField: %1" ).arg( ddField ) );
150-
151212
bool ok = false;
152213
switch ( propIt.key() )
153214
{
154215
case QgsPalLayerSettings::Show:
155-
{ // new scope to assign variables
156-
mShowLabelChkbx->setEnabled( true );
157-
int showLabel = mCurLabelFeat.attribute( ddIndx ).toInt( &ok );
216+
{
217+
int showLabel = result.toInt( &ok );
158218
mShowLabelChkbx->setChecked( !ok || showLabel != 0 );
159219
break;
160220
}
161221
case QgsPalLayerSettings::AlwaysShow:
162-
mAlwaysShowChkbx->setEnabled( true );
163-
mAlwaysShowChkbx->setChecked( mCurLabelFeat.attribute( ddIndx ).toBool() );
222+
mAlwaysShowChkbx->setChecked( result.toBool() );
164223
break;
165224
case QgsPalLayerSettings::MinScale:
166225
{
167-
mMinScaleSpinBox->setEnabled( true );
168-
int minScale = mCurLabelFeat.attribute( ddIndx ).toInt( &ok );
226+
int minScale = result.toInt( &ok );
169227
if ( ok )
170228
{
171229
mMinScaleSpinBox->setValue( minScale );
@@ -174,8 +232,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
174232
}
175233
case QgsPalLayerSettings::MaxScale:
176234
{
177-
mMaxScaleSpinBox->setEnabled( true );
178-
int maxScale = mCurLabelFeat.attribute( ddIndx ).toInt( &ok );
235+
int maxScale = result.toInt( &ok );
179236
if ( ok )
180237
{
181238
mMaxScaleSpinBox->setValue( maxScale );
@@ -184,8 +241,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
184241
}
185242
case QgsPalLayerSettings::BufferSize:
186243
{
187-
mBufferSizeSpinBox->setEnabled( true );
188-
double bufferSize = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
244+
double bufferSize = result.toDouble( &ok );
189245
if ( ok )
190246
{
191247
mBufferSizeSpinBox->setValue( bufferSize );
@@ -194,8 +250,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
194250
}
195251
case QgsPalLayerSettings::PositionX:
196252
{
197-
mXCoordSpinBox->setEnabled( true );
198-
double posX = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
253+
double posX = result.toDouble( &ok );
199254
if ( ok )
200255
{
201256
mXCoordSpinBox->setValue( posX );
@@ -204,8 +259,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
204259
}
205260
case QgsPalLayerSettings::PositionY:
206261
{
207-
mYCoordSpinBox->setEnabled( true );
208-
double posY = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
262+
double posY = result.toDouble( &ok );
209263
if ( ok )
210264
{
211265
mYCoordSpinBox->setValue( posY );
@@ -214,42 +268,122 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
214268
}
215269
case QgsPalLayerSettings::LabelDistance:
216270
{
217-
mLabelDistanceSpinBox->setEnabled( true );
218-
double labelDist = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
271+
double labelDist = result.toDouble( &ok );
219272
if ( ok )
220273
{
221274
mLabelDistanceSpinBox->setValue( labelDist );
222275
}
223276
break;
224277
}
225278
case QgsPalLayerSettings::Hali:
226-
mHaliComboBox->setEnabled( true );
227-
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( mCurLabelFeat.attribute( ddIndx ).toString(), Qt::MatchFixedString ) );
279+
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( result.toString(), Qt::MatchFixedString ) );
228280
break;
229281
case QgsPalLayerSettings::Vali:
230-
mValiComboBox->setEnabled( true );
231-
mValiComboBox->setCurrentIndex( mValiComboBox->findText( mCurLabelFeat.attribute( ddIndx ).toString(), Qt::MatchFixedString ) );
282+
mValiComboBox->setCurrentIndex( mValiComboBox->findText( result.toString(), Qt::MatchFixedString ) );
232283
break;
233284
case QgsPalLayerSettings::BufferColor:
234-
mBufferColorButton->setEnabled( true );
235-
mBufferColorButton->setColor( QColor( mCurLabelFeat.attribute( ddIndx ).toString() ) );
285+
mBufferColorButton->setColor( QColor( result.toString() ) );
236286
break;
237287
case QgsPalLayerSettings::Color:
238-
mFontColorButton->setEnabled( true );
239-
mFontColorButton->setColor( QColor( mCurLabelFeat.attribute( ddIndx ).toString() ) );
288+
mFontColorButton->setColor( QColor( result.toString() ) );
240289
break;
241290
case QgsPalLayerSettings::Rotation:
242291
{
243-
mRotationSpinBox->setEnabled( true );
244-
double rot = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
292+
double rot = result.toDouble( &ok );
245293
if ( ok )
246294
{
247295
mRotationSpinBox->setValue( rot );
248296
}
249297
break;
250298
}
251299

252-
//font related properties
300+
case QgsPalLayerSettings::Size:
301+
{
302+
double size = result.toDouble( &ok );
303+
if ( ok )
304+
{
305+
mFontSizeSpinBox->setValue( size );
306+
}
307+
else
308+
{
309+
mFontSizeSpinBox->setValue( 0 );
310+
}
311+
break;
312+
}
313+
default:
314+
break;
315+
}
316+
}
317+
}
318+
319+
void QgsLabelPropertyDialog::enableDataDefinedWidgets( QgsVectorLayer* vlayer )
320+
{
321+
//loop through data defined properties, this time setting whether or not the widgets are enabled
322+
//this can only be done for properties which are assigned to fields
323+
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* >::const_iterator propIt = mDataDefinedProperties.constBegin();
324+
for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
325+
{
326+
QgsDataDefined* dd = propIt.value();
327+
if ( !dd )
328+
{
329+
continue;
330+
}
331+
QString ddField = dd->field();
332+
if ( !dd->isActive() || dd->useExpression() || ddField.isEmpty() )
333+
{
334+
continue; // can only modify attributes with an active data definition of a mapped field
335+
}
336+
337+
int ddIndx = vlayer->fieldNameIndex( ddField );
338+
if ( ddIndx == -1 )
339+
{
340+
continue;
341+
}
342+
343+
QgsDebugMsg( QString( "ddField: %1" ).arg( ddField ) );
344+
345+
switch ( propIt.key() )
346+
{
347+
case QgsPalLayerSettings::Show:
348+
mShowLabelChkbx->setEnabled( true );
349+
break;
350+
case QgsPalLayerSettings::AlwaysShow:
351+
mAlwaysShowChkbx->setEnabled( true );
352+
break;
353+
case QgsPalLayerSettings::MinScale:
354+
mMinScaleSpinBox->setEnabled( true );
355+
break;
356+
case QgsPalLayerSettings::MaxScale:
357+
mMaxScaleSpinBox->setEnabled( true );
358+
break;
359+
case QgsPalLayerSettings::BufferSize:
360+
mBufferSizeSpinBox->setEnabled( true );
361+
break;
362+
case QgsPalLayerSettings::PositionX:
363+
mXCoordSpinBox->setEnabled( true );
364+
break;
365+
case QgsPalLayerSettings::PositionY:
366+
mYCoordSpinBox->setEnabled( true );
367+
break;
368+
case QgsPalLayerSettings::LabelDistance:
369+
mLabelDistanceSpinBox->setEnabled( true );
370+
break;
371+
case QgsPalLayerSettings::Hali:
372+
mHaliComboBox->setEnabled( true );
373+
break;
374+
case QgsPalLayerSettings::Vali:
375+
mValiComboBox->setEnabled( true );
376+
break;
377+
case QgsPalLayerSettings::BufferColor:
378+
mBufferColorButton->setEnabled( true );
379+
break;
380+
case QgsPalLayerSettings::Color:
381+
mFontColorButton->setEnabled( true );
382+
break;
383+
case QgsPalLayerSettings::Rotation:
384+
mRotationSpinBox->setEnabled( true );
385+
break;
386+
//font related properties
253387
case QgsPalLayerSettings::Family:
254388
mFontFamilyCmbBx->setEnabled( true );
255389
break;
@@ -269,72 +403,11 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
269403
mFontItalicBtn->setEnabled( true );
270404
break;
271405
case QgsPalLayerSettings::Size:
272-
{
273406
mFontSizeSpinBox->setEnabled( true );
274-
double size = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
275-
if ( ok )
276-
{
277-
mFontSizeSpinBox->setValue( size );
278-
}
279-
else
280-
{
281-
mFontSizeSpinBox->setValue( 0 );
282-
}
283-
break;
284-
}
285407
default:
286408
break;
287409
}
288410
}
289-
blockElementSignals( false );
290-
}
291-
292-
void QgsLabelPropertyDialog::disableGuiElements()
293-
{
294-
mShowLabelChkbx->setEnabled( false );
295-
mAlwaysShowChkbx->setEnabled( false );
296-
mMinScaleSpinBox->setEnabled( false );
297-
mMaxScaleSpinBox->setEnabled( false );
298-
mFontFamilyCmbBx->setEnabled( false );
299-
mFontStyleCmbBx->setEnabled( false );
300-
mFontUnderlineBtn->setEnabled( false );
301-
mFontStrikethroughBtn->setEnabled( false );
302-
mFontBoldBtn->setEnabled( false );
303-
mFontItalicBtn->setEnabled( false );
304-
mFontSizeSpinBox->setEnabled( false );
305-
mBufferSizeSpinBox->setEnabled( false );
306-
mFontColorButton->setEnabled( false );
307-
mBufferColorButton->setEnabled( false );
308-
mLabelDistanceSpinBox->setEnabled( false );
309-
mXCoordSpinBox->setEnabled( false );
310-
mYCoordSpinBox->setEnabled( false );
311-
mHaliComboBox->setEnabled( false );
312-
mValiComboBox->setEnabled( false );
313-
mRotationSpinBox->setEnabled( false );
314-
}
315-
316-
void QgsLabelPropertyDialog::blockElementSignals( bool block )
317-
{
318-
mShowLabelChkbx->blockSignals( block );
319-
mAlwaysShowChkbx->blockSignals( block );
320-
mMinScaleSpinBox->blockSignals( block );
321-
mMaxScaleSpinBox->blockSignals( block );
322-
mFontFamilyCmbBx->blockSignals( block );
323-
mFontStyleCmbBx->blockSignals( block );
324-
mFontUnderlineBtn->blockSignals( block );
325-
mFontStrikethroughBtn->blockSignals( block );
326-
mFontBoldBtn->blockSignals( block );
327-
mFontItalicBtn->blockSignals( block );
328-
mFontSizeSpinBox->blockSignals( block );
329-
mBufferSizeSpinBox->blockSignals( block );
330-
mFontColorButton->blockSignals( block );
331-
mBufferColorButton->blockSignals( block );
332-
mLabelDistanceSpinBox->blockSignals( block );
333-
mXCoordSpinBox->blockSignals( block );
334-
mYCoordSpinBox->blockSignals( block );
335-
mHaliComboBox->blockSignals( block );
336-
mValiComboBox->blockSignals( block );
337-
mRotationSpinBox->blockSignals( block );
338411
}
339412

340413
void QgsLabelPropertyDialog::updateFont( const QFont& font, bool block )

‎src/app/qgslabelpropertydialog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
6565
/**Block / unblock all input element signals*/
6666
void blockElementSignals( bool block );
6767

68+
void setDataDefinedValues( QgsPalLayerSettings layerSettings, QgsVectorLayer* vlayer );
69+
void enableDataDefinedWidgets( QgsVectorLayer* vlayer );
70+
6871
/** Updates font when family or style is updated */
6972
void updateFont( const QFont& font, bool block = true );
7073

0 commit comments

Comments
 (0)
Please sign in to comment.