Skip to content
Permalink
Browse files
[FEATURE]: multiline labels for labeling-ng
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13945 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jul 20, 2010
1 parent fef94ad commit 40f67623f454cea5b9db348776ae74041ce4197b
Showing with 76 additions and 35 deletions.
  1. +2 −0 src/app/qgslabelinggui.cpp
  2. +61 −29 src/core/qgspallabeling.cpp
  3. +1 −0 src/core/qgspallabeling.h
  4. +12 −6 src/ui/qgslabelingguibase.ui
@@ -119,6 +119,7 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QWid
chkNoObstacle->setChecked( !lyr.obstacle );
chkLabelPerFeaturePart->setChecked( lyr.labelPerPart );
chkMergeLines->setChecked( lyr.mergeLines );
chkMultiLine->setChecked( lyr.multiLineLabels );
mMinSizeSpinBox->setValue( lyr.minFeatureSize );

bool scaleBased = ( lyr.scaleMin != 0 && lyr.scaleMax != 0 );
@@ -216,6 +217,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.obstacle = !chkNoObstacle->isChecked();
lyr.labelPerPart = chkLabelPerFeaturePart->isChecked();
lyr.mergeLines = chkMergeLines->isChecked();
lyr.multiLineLabels = chkMultiLine->isChecked();
if ( chkScaleBasedVisibility->isChecked() )
{
lyr.scaleMin = spinScaleMin->value();
@@ -122,6 +122,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
bufferColor = Qt::white;
labelPerPart = false;
mergeLines = false;
multiLineLabels = false;
minFeatureSize = 0.0;
vectorScaleFactor = 1.0;
rasterCompressFactor = 1.0;
@@ -145,6 +146,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
bufferColor = s.bufferColor;
labelPerPart = s.labelPerPart;
mergeLines = s.mergeLines;
multiLineLabels = s.multiLineLabels;
minFeatureSize = s.minFeatureSize;
vectorScaleFactor = s.vectorScaleFactor;
rasterCompressFactor = s.rasterCompressFactor;
@@ -201,6 +203,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
bufferColor = _readColor( layer, "labeling/bufferColor" );
labelPerPart = layer->customProperty( "labeling/labelPerPart" ).toBool();
mergeLines = layer->customProperty( "labeling/mergeLines" ).toBool();
multiLineLabels = layer->customProperty( "labeling/multiLineLabels" ).toBool();
minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble();
}

@@ -229,6 +232,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
_writeColor( layer, "labeling/bufferColor", bufferColor );
layer->setCustomProperty( "labeling/labelPerPart", labelPerPart );
layer->setCustomProperty( "labeling/mergeLines", mergeLines );
layer->setCustomProperty( "labeling/multiLineLabels", multiLineLabels );
layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize );
}

@@ -273,9 +277,27 @@ bool QgsPalLayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeo
void QgsPalLayerSettings::calculateLabelSize( QString text, double& labelX, double& labelY )
{
QRectF labelRect = fontMetrics->boundingRect( text );
double w = labelRect.width() / rasterCompressFactor;
double h = labelRect.height() / rasterCompressFactor;

double w, h;
if ( !multiLineLabels )
{
w = labelRect.width() / rasterCompressFactor;
h = labelRect.height() / rasterCompressFactor;
}
else
{
QStringList multiLineSplit = text.split( "\n" );
h = fontMetrics->height() * multiLineSplit.size() / rasterCompressFactor;
w = 0;
for ( int i = 0; i < multiLineSplit.size(); ++i )
{
double width = fontMetrics->width( multiLineSplit.at( i ) );
if ( width > w )
{
w = width;
}
w /= rasterCompressFactor;
}
}
QgsPoint ptSize = xform->toMapCoordinates( w, h );

labelX = fabs( ptSize.x() - ptZero.x() );
@@ -650,38 +672,48 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, co

//QgsDebugMsg( "drawLabel " + QString::number( drawBuffer ) + " " + txt );

painter->save();
painter->translate( QPointF( outPt.x(), outPt.y() ) );
painter->rotate( -label->getAlpha() * 180 / M_PI );

// scale down painter: the font size has been multiplied by raster scale factor
// to workaround a Qt font scaling bug with small font sizes
painter->scale( 1.0 / lyr.rasterCompressFactor, 1.0 / lyr.rasterCompressFactor );

painter->translate( QPointF( 0, - lyr.fontMetrics->descent() ) );

if ( drawBuffer )
QStringList multiLineList;
if ( lyr.multiLineLabels )
{
// we're drawing buffer
drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
multiLineList = txt.split( "\n" );
}
else
{
// we're drawing real label
/*painter->setFont( lyr.textFont );
painter->setPen( lyr.textColor );
painter->drawText((0,0, txt);*/

QPainterPath path;
path.addText( 0, 0, lyr.textFont, txt );
painter->setPen( Qt::NoPen );
painter->setBrush( lyr.textColor );
painter->drawPath( path );
multiLineList << txt;
}
painter->restore();

if ( label->getNextPart() )
drawLabel( label->getNextPart(), painter, xform, drawBuffer );
for ( int i = 0; i < multiLineList.size(); ++i )
{
painter->save();
painter->translate( QPointF( outPt.x(), outPt.y() ) );
painter->rotate( -label->getAlpha() * 180 / M_PI );

// scale down painter: the font size has been multiplied by raster scale factor
// to workaround a Qt font scaling bug with small font sizes
painter->scale( 1.0 / lyr.rasterCompressFactor, 1.0 / lyr.rasterCompressFactor );

double yMultiLineOffset = ( multiLineList.size() - 1 - i ) * lyr.fontMetrics->height();
painter->translate( QPointF( 0, - lyr.fontMetrics->descent() - yMultiLineOffset ) );

if ( drawBuffer )
{
// we're drawing buffer
drawLabelBuffer( painter, multiLineList.at( i ), lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
}
else
{
// we're drawing real label
QPainterPath path;
path.addText( 0, 0, lyr.textFont, multiLineList.at( i ) );
painter->setPen( Qt::NoPen );
painter->setBrush( lyr.textColor );
painter->drawPath( path );
}
painter->restore();

if ( label->getNextPart() )
drawLabel( label->getNextPart(), painter, xform, drawBuffer );
}
}


@@ -88,6 +88,7 @@ class CORE_EXPORT QgsPalLayerSettings
QColor bufferColor;
bool labelPerPart; // whether to label every feature's part or only the biggest one
bool mergeLines;
bool multiLineLabels; //draw labels on multiple lines if they contain '\n'
double minFeatureSize; // minimum feature size to be labelled (in mm)

// called from register feature hook
@@ -15,8 +15,7 @@
</property>
<property name="windowIcon">
<iconset>
<normaloff/>
</iconset>
<normaloff>../../../../.designer/backup</normaloff>../../../../.designer/backup</iconset>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
@@ -662,7 +661,7 @@
</property>
</widget>
</item>
<item row="6" column="0">
<item row="7" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QLabel" name="label_19">
@@ -680,7 +679,7 @@
</item>
</layout>
</item>
<item row="7" column="0">
<item row="8" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QCheckBox" name="chkNoObstacle">
@@ -717,7 +716,7 @@
</item>
</layout>
</item>
<item row="8" column="0">
<item row="9" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
@@ -730,7 +729,7 @@
</property>
</spacer>
</item>
<item row="9" column="0">
<item row="10" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -740,6 +739,13 @@
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QCheckBox" name="chkMultiLine">
<property name="text">
<string>multiline labels</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>

0 comments on commit 40f6762

Please sign in to comment.