Skip to content
Permalink
Browse files
Applied patch #2739 from Marco - correct scaling of labeling from the…
… labeling plugin in composer.

Includes few tweaks and improvements, additionally the buffer is not considered for the label frame.


git-svn-id: http://svn.osgeo.org/qgis/trunk@13599 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 29, 2010
1 parent f9a5aec commit b1db818af5edbb1e172b0c606f329768f37b86dd
@@ -41,7 +41,7 @@ LabelingGui::LabelingGui( PalLabeling* lbl, QgsVectorLayer* layer, QWidget* pare
connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
connect( spinBufferSize, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
connect( spinBufferSize, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
connect( btnEngineSettings, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );

// set placement methods page based on geometry type
@@ -206,7 +206,7 @@
<item row="0" column="2">
<widget class="QLabel" name="label_9">
<property name="text">
<string>pixels</string>
<string>mm</string>
</property>
</widget>
</item>
@@ -312,7 +312,7 @@
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string>pixels</string>
<string>mm</string>
</property>
</widget>
</item>
@@ -472,9 +472,9 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBufferSize">
<property name="minimum">
<number>1</number>
<widget class="QDoubleSpinBox" name="spinBufferSize">
<property name="suffix">
<string> mm</string>
</property>
</widget>
</item>
@@ -771,7 +771,6 @@
<tabstop>btnChangeFont</tabstop>
<tabstop>btnTextColor</tabstop>
<tabstop>chkBuffer</tabstop>
<tabstop>spinBufferSize</tabstop>
<tabstop>btnBufferColor</tabstop>
<tabstop>sliderPriority</tabstop>
<tabstop>chkScaleBasedVisibility</tabstop>
@@ -15,9 +15,9 @@ void LabelPreview::setTextColor( QColor color )
update();
}

void LabelPreview::setBuffer( int size, QColor color )
void LabelPreview::setBuffer( double size, QColor color )
{
mBufferSize = size;
mBufferSize = size * 88 / 25.4; //assume standard dpi for preview
mBufferColor = color;
update();
}
@@ -10,7 +10,7 @@ class LabelPreview : public QLabel

void setTextColor( QColor color );

void setBuffer( int size, QColor color );
void setBuffer( double size, QColor color );

void paintEvent( QPaintEvent* e );

@@ -60,19 +60,19 @@ class MyLabel : public PalGeometry
const char* strId() { return mStrId.data(); }
QString text() { return mText; }

pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform )
pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform, double fontScale )
{
if ( mInfo ) return mInfo;

// create label info!
QgsPoint ptZero = xform->toMapCoordinates( 0, 0 );
QgsPoint ptSize = xform->toMapCoordinates( 0, -fm->height() );
QgsPoint ptSize = xform->toMapCoordinates( 0, ( int )( -fm->height() / fontScale ) );

mInfo = new pal::LabelInfo( mText.count(), ptSize.y() - ptZero.y() );
for ( int i = 0; i < mText.count(); i++ )
{
mInfo->char_info[i].chr = mText[i].unicode();
ptSize = xform->toMapCoordinates( fm->width( mText[i] ), 0 );
ptSize = xform->toMapCoordinates(( int )( fm->width( mText[i] ) / fontScale ) , 0 );
mInfo->char_info[i].width = ptSize.x() - ptZero.x();
}
return mInfo;
@@ -106,6 +106,8 @@ LayerSettings::LayerSettings()
labelPerPart = false;
mergeLines = false;
minFeatureSize = 0.0;
vectorScaleFactor = 1.0;
rasterCompressFactor = 1.0;
}

LayerSettings::LayerSettings( const LayerSettings& s )
@@ -127,6 +129,8 @@ LayerSettings::LayerSettings( const LayerSettings& s )
labelPerPart = s.labelPerPart;
mergeLines = s.mergeLines;
minFeatureSize = s.minFeatureSize;
vectorScaleFactor = s.vectorScaleFactor;
rasterCompressFactor = s.rasterCompressFactor;

fontMetrics = NULL;
ct = NULL;
@@ -251,11 +255,12 @@ bool LayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry*

void LayerSettings::calculateLabelSize( QString text, double& labelX, double& labelY )
{
//QFontMetrics fontMetrics(textFont);
QRect labelRect = /*QRect(0,0,20,20);*/ fontMetrics->boundingRect( text );
QRectF labelRect = fontMetrics->boundingRect( text );
double w = labelRect.width() / rasterCompressFactor;
double h = labelRect.height() / rasterCompressFactor;

QgsPoint ptSize = xform->toMapCoordinates( w, h );

// 2px border...
QgsPoint ptSize = xform->toMapCoordinates( labelRect.width() + 2, labelRect.height() + 2 );
labelX = fabs( ptSize.x() - ptZero.x() );
labelY = fabs( ptSize.y() - ptZero.y() );
}
@@ -295,11 +300,11 @@ void LayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& cont

// TODO: only for placement which needs character info
pal::Feature* feat = palLayer->getFeature( lbl->strId() );
feat->setLabelInfo( lbl->info( fontMetrics, xform ) );
feat->setLabelInfo( lbl->info( fontMetrics, xform, rasterCompressFactor ) );

// TODO: allow layer-wide feature dist in PAL...?
if ( dist != 0 )
feat->setDistLabel( fabs( ptOne.x() - ptZero.x() )* dist );
feat->setDistLabel( fabs( ptOne.x() - ptZero.x() )* dist * vectorScaleFactor );
}


@@ -402,14 +407,19 @@ int PalLabeling::prepareLayer( QgsVectorLayer* layer, int& attrIndex, QgsRenderC
l->setMergeConnectedLines( lyr.mergeLines );

// set font size from points to output size
double size = 0.3527 * lyr.textFont.pointSizeF() * ctx.scaleFactor(); //* ctx.rasterScaleFactor();
lyr.textFont.setPixelSize(( int )size );
double size = 0.3527 * lyr.textFont.pointSizeF() * ctx.scaleFactor();
// request larger font and then scale down painter (to avoid Qt font scale bug)
lyr.textFont.setPixelSize(( int )( size*ctx.rasterScaleFactor() + 0.5 ) );

//raster and vector scale factors
lyr.vectorScaleFactor = ctx.scaleFactor();
lyr.rasterCompressFactor = ctx.rasterScaleFactor();

// save the pal layer to our layer context (with some additional info)
lyr.palLayer = l;
lyr.fieldIndex = fldIndex;
lyr.fontMetrics = new QFontMetrics( lyr.textFont );
lyr.fontBaseline = lyr.fontMetrics->boundingRect( "X" ).bottom(); // dummy text to find out how many pixels of the text are below the baseline

lyr.xform = mMapRenderer->coordinateTransform();
if ( mMapRenderer->hasCrsTransformEnabled() )
lyr.ct = new QgsCoordinateTransform( layer->srs(), mMapRenderer->destinationSrs() );
@@ -623,16 +633,20 @@ void PalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, const

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

// shift by one as we have 2px border
painter->save();
painter->translate( QPointF( outPt.x(), outPt.y() ) );
painter->rotate( -label->getAlpha() * 180 / M_PI );
painter->translate( QPointF( 1, -1 - lyr.fontBaseline ) );

// 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 )
{
// we're drawing buffer
drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize, lyr.bufferColor );
drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
}
else
{
@@ -654,7 +668,7 @@ void PalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, const
}


void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font, int size, QColor color )
void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color )
{
/*
p->setFont( font );
@@ -666,7 +680,9 @@ void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font,

QPainterPath path;
path.addText( 0, 0, font, text );
p->setPen( QPen( color, size ) );
QPen pen( color );
pen.setWidthF( size );
p->setPen( pen );
p->setBrush( color );
p->drawPath( path );
}
@@ -60,9 +60,11 @@ class LayerSettings
bool enabled;
int priority; // 0 = low, 10 = high
bool obstacle; // whether it's an obstacle
double dist; // distance from the feature (in pixels)
double dist; // distance from the feature (in mm)
double vectorScaleFactor; //scale factor painter units->pixels
double rasterCompressFactor; //pixel resolution scale factor
int scaleMin, scaleMax; // disabled if both are zero
int bufferSize;
double bufferSize; //buffer size (in mm)
QColor bufferColor;
bool labelPerPart; // whether to label every feature's part or only the biggest one
bool mergeLines;
@@ -81,7 +83,6 @@ class LayerSettings
pal::Layer* palLayer;
int fieldIndex;
QFontMetrics* fontMetrics;
int fontBaseline;
const QgsMapToPixel* xform;
const QgsCoordinateTransform* ct;
QgsPoint ptZero, ptOne;
@@ -145,7 +146,7 @@ class PalLabeling : public QgsLabelingEngineInterface

void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
void drawLabel( pal::LabelPosition* label, QPainter* painter, const QgsMapToPixel* xform, bool drawBuffer = false );
static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, int size, QColor color );
static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color );

protected:

0 comments on commit b1db818

Please sign in to comment.