Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add docstrings and modernize code
  • Loading branch information
m-kuhn committed Feb 5, 2019
1 parent 4612521 commit 5f1cea1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
6 changes: 5 additions & 1 deletion python/core/auto_generated/qgspallabeling.sip.in
Expand Up @@ -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 );

Expand Down
18 changes: 8 additions & 10 deletions src/core/qgspallabeling.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/core/qgspallabeling.h
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayerlabelprovider.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit 5f1cea1

Please sign in to comment.