Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #53 from NathanW2/expression-labels-patch
Expression label patches
  • Loading branch information
wonder-sk committed Oct 31, 2011
2 parents ed20ff3 + a739750 commit e573ce9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/core/qgspallabeling.cpp
Expand Up @@ -152,7 +152,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
plusSign = false;
labelPerPart = false;
mergeLines = false;
multiLineLabels = false;
multiLineLabels = true;
minFeatureSize = 0.0;
vectorScaleFactor = 1.0;
rasterCompressFactor = 1.0;
Expand Down Expand Up @@ -434,10 +434,11 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
{
text.append( ">" );
}
QRectF labelRect = fm->boundingRect( text );

double w, h;
if ( !multiLineLabels )
{
QRectF labelRect = fm->boundingRect( text );
w = labelRect.width() / rasterCompressFactor;
h = labelRect.height() / rasterCompressFactor;
}
Expand All @@ -452,9 +453,9 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
if ( width > w )
{
w = width;
}
w /= rasterCompressFactor;
}
}
w /= rasterCompressFactor;
}
QgsPoint ptSize = xform->toMapCoordinatesF( w, h );

Expand All @@ -471,14 +472,13 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
QgsExpression* exp = getLabelExpression();
if ( exp->hasParserError() )
{
QgsDebugMsg( "PASER HAS ERROR:" + exp->parserErrorString() );
QgsDebugMsg( "Expression parser error:" + exp->parserErrorString() );
return;
}
QVariant result = exp->evaluate( &f, layer->dataProvider()->fields() );
QgsDebugMsg( "VALUE = " + result.toString() );
if ( exp->hasEvalError() )
{
QgsDebugMsg( "Expression Label Error = " + exp->evalErrorString() );
QgsDebugMsg( "Expression parser eval error:" + exp->evalErrorString() );
return;
}
labelText = result.toString();
Expand Down
25 changes: 18 additions & 7 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -230,13 +230,24 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
// Maybe just calling exp.evaluate()?
if ( mLayer )
{
// TODO We should really cache the feature.
QgsFeature feature;
mLayer->featureAtId( 0 , feature );
QVariant value = exp.evaluate( &feature, mLayer->pendingFields() );

if ( !exp.hasEvalError() )
lblPreview->setText( value.toString() );
if ( !mFeature.isValid() )
{
mLayer->select( mLayer->pendingAllAttributesList() );
mLayer->nextFeature( mFeature );
}

if ( mFeature.isValid() )
{
QVariant value = exp.evaluate( &mFeature, mLayer->pendingFields() );
if ( !exp.hasEvalError() )
lblPreview->setText( value.toString() );
}
else
{
// The feautre is invaild because we don't have one but that doesn't mean user can't
// build a expression string. They just get no preview.
lblPreview->setText("");
}
}

if ( exp.hasParserError() || exp.hasEvalError() )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -164,7 +164,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QStandardItemModel *mModel;
QgsExpressionItemSearchProxy *mProxyModel;
QMap<QString, QgsExpressionItem*> mExpressionGroups;
QgsFeature* mFeature;
QgsFeature mFeature;
};

#endif // QGSEXPRESSIONBUILDER_H

0 comments on commit e573ce9

Please sign in to comment.