Add docstrings and modernize code

This commit is contained in:
Matthias Kuhn 2019-02-05 17:57:27 +01:00
parent 4612521c50
commit 5f1cea13c7
No known key found for this signature in database
GPG Key ID: 7A7F1A1C90C3E6A7
4 changed files with 19 additions and 13 deletions

View File

@ -386,7 +386,11 @@ Returns the QgsExpression for this label settings. May be None if isExpression i
double zIndex;
void calculateLabelSize( const QFontMetricsF *fm, QString text, double &labelX, double &labelY, const QgsFeature *f = 0, QgsRenderContext *context = 0 );
void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = 0, QgsRenderContext *context = 0 );
%Docstring
Calculates the space required to render the provided ``text`` in map units.
Results will be written to ``labelX`` and ``labelY``.
%End
void registerFeature( const QgsFeature &f, QgsRenderContext &context );

View File

@ -1038,13 +1038,15 @@ bool QgsPalLayerSettings::checkMinimumSizeMM( const QgsRenderContext &ct, const
return QgsPalLabeling::checkMinimumSizeMM( ct, geom, minSize );
}
void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, QString text, double &labelX, double &labelY, const QgsFeature *f, QgsRenderContext *context )
void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f, QgsRenderContext *context )
{
if ( !fm || !f )
{
return;
}
QString textCopy( text );
//try to keep < 2.12 API - handle no passed render context
std::unique_ptr< QgsRenderContext > scopedRc;
if ( !context )
@ -1150,29 +1152,25 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, QString t
if ( placeDirSymb == QgsPalLayerSettings::SymbolLeftRight )
{
text.append( dirSym );
textCopy.append( dirSym );
}
else
{
text.prepend( dirSym + QStringLiteral( "\n" ) ); // SymbolAbove or SymbolBelow
textCopy.prepend( dirSym + QStringLiteral( "\n" ) ); // SymbolAbove or SymbolBelow
}
}
double w = 0.0, h = 0.0;
QStringList multiLineSplit = QgsPalLabeling::splitToLines( text, wrapchr, evalAutoWrapLength, useMaxLineLengthForAutoWrap );
const QStringList multiLineSplit = QgsPalLabeling::splitToLines( textCopy, wrapchr, evalAutoWrapLength, useMaxLineLengthForAutoWrap );
int lines = multiLineSplit.size();
double labelHeight = fm->ascent() + fm->descent(); // ignore +1 for baseline
h += fm->height() + static_cast< double >( ( lines - 1 ) * labelHeight * multilineH );
for ( int i = 0; i < lines; ++i )
for ( const QString &line : multiLineSplit )
{
double width = fm->width( multiLineSplit.at( i ) );
if ( width > w )
{
w = width;
}
w = qMax( w, fm->width( line ) );
}
#if 0 // XXX strk

View File

@ -750,8 +750,11 @@ class CORE_EXPORT QgsPalLayerSettings
//! Z-Index of label, where labels with a higher z-index are rendered on top of labels with a lower z-index
double zIndex;
// called from register feature hook
void calculateLabelSize( const QFontMetricsF *fm, QString text, double &labelX, double &labelY, const QgsFeature *f = nullptr, QgsRenderContext *context = nullptr );
/**
* Calculates the space required to render the provided \a text in map units.
* Results will be written to \a labelX and \a labelY.
*/
void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = nullptr, QgsRenderContext *context = nullptr );
/**
* Register a feature for labeling.

View File

@ -261,6 +261,7 @@ QList<QgsLabelFeature *> QgsVectorLayerLabelProvider::labelFeatures( QgsRenderCo
void QgsVectorLayerLabelProvider::registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry )
{
QgsLabelFeature *label = nullptr;
mSettings.registerFeature( feature, context, &label, obstacleGeometry );
if ( label )
mLabels << label;