Skip to content

Commit

Permalink
Applied patch #2739 from Marco - correct scaling of labeling from the…
Browse files Browse the repository at this point in the history
… 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/qgis@13599 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 29, 2010
1 parent 5ead969 commit 5ee6a71
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/plugins/labeling/labelinggui.cpp
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/plugins/labeling/labelingguibase.ui
Expand Up @@ -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>
Expand Down Expand Up @@ -312,7 +312,7 @@
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string>pixels</string>
<string>mm</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/labeling/labelpreview.cpp
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/labeling/labelpreview.h
Expand Up @@ -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 );

Expand Down
50 changes: 33 additions & 17 deletions src/plugins/labeling/pallabeling.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -106,6 +106,8 @@ LayerSettings::LayerSettings()
labelPerPart = false;
mergeLines = false;
minFeatureSize = 0.0;
vectorScaleFactor = 1.0;
rasterCompressFactor = 1.0;
}

LayerSettings::LayerSettings( const LayerSettings& s )
Expand All @@ -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;
Expand Down Expand Up @@ -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() );
}
Expand Down Expand Up @@ -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 );
}


Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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
{
Expand All @@ -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 );
Expand All @@ -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 );
}
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/labeling/pallabeling.h
Expand Up @@ -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;
Expand All @@ -81,7 +83,6 @@ class LayerSettings
pal::Layer* palLayer;
int fieldIndex;
QFontMetrics* fontMetrics;
int fontBaseline;
const QgsMapToPixel* xform;
const QgsCoordinateTransform* ct;
QgsPoint ptZero, ptOne;
Expand Down Expand Up @@ -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:

Expand Down

0 comments on commit 5ee6a71

Please sign in to comment.