Skip to content

Commit

Permalink
Fixed drawing of buffers for curved labels, better(?) drawing of buff…
Browse files Browse the repository at this point in the history
…er (painter path instead of size^2 calls to drawText)

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11179 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jul 26, 2009
1 parent 6b93874 commit 3d04984
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/plugins/labeling/labelpreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ void LabelPreview::paintEvent(QPaintEvent* e)
{
QPainter p(this);

p.setRenderHint(QPainter::Antialiasing);
p.setFont(font());
p.setPen(mTextColor);
p.translate(10, 20); // uhm...

if (mBufferSize != 0)
PalLabeling::drawLabelBuffer(&p, text(), mBufferSize, mBufferColor);
PalLabeling::drawLabelBuffer(&p, text(), font(), mBufferSize, mBufferColor);

p.setPen(mTextColor);
p.drawText(0,0, text());
}
47 changes: 33 additions & 14 deletions src/plugins/labeling/pallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ void PalLabeling::doLabeling(QPainter* painter, QgsRectangle extent)
std::list<LabelPosition*>::iterator it = labels->begin();
for ( ; it != labels->end(); ++it)
{
const LayerSettings& lyr = layer((*it)->getLayerName());

if (lyr.bufferSize != 0)
drawLabel( *it, painter, xform, true);

drawLabel( *it, painter, xform );
}

Expand Down Expand Up @@ -455,43 +460,57 @@ void PalLabeling::drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* pain
drawLabelCandidateRect(lp->getNextPart(), painter, xform);
}

#include "qgslogger.h"

void PalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, const QgsMapToPixel* xform)
void PalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, const QgsMapToPixel* xform, bool drawBuffer)
{
QgsPoint outPt = xform->transform(label->getX(), label->getY());

// TODO: optimize access :)
const LayerSettings& lyr = layer(label->getLayerName());

QString text = ((MyLabel*)label->getFeature()->getUserGeometry())->text();
QString txt = ( label->getPartId() == -1 ? text : QString( text[label->getPartId()] ) );

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

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

if (lyr.bufferSize != 0)
drawLabelBuffer(painter, text, lyr.bufferSize, lyr.bufferColor);

painter->setPen( lyr.textColor );
if (label->getPartId() == -1)
painter->drawText(0,0, text);
if (drawBuffer)
{
// we're drawing buffer
drawLabelBuffer(painter, txt, lyr.textFont, lyr.bufferSize, lyr.bufferColor);
}
else
painter->drawText(0,0, QString( text[label->getPartId()] ) );
{
// we're drawing real label
painter->setFont( lyr.textFont );
painter->setPen( lyr.textColor );
painter->drawText(0,0, txt);
}
painter->restore();

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


void PalLabeling::drawLabelBuffer(QPainter* p, QString text, int size, QColor color)
void PalLabeling::drawLabelBuffer(QPainter* p, QString text, const QFont& font, int size, QColor color)
{
p->save();
p->setPen(color);
/*
p->setFont( font );
p->setPen( color );
for (int x = -size; x <= size; x++)
for (int y = -size; y <= size; y++)
p->drawText(x,y, text);
p->restore();
*/

QPainterPath path;
path.addText(0,0, font, text);
p->setPen( QPen(color, size) );
p->setBrush( color );
p->drawPath(path);
}
4 changes: 2 additions & 2 deletions src/plugins/labeling/pallabeling.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class PalLabeling


void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
void drawLabel( pal::LabelPosition* label, QPainter* painter, const QgsMapToPixel* xform);
static void drawLabelBuffer(QPainter* p, QString text, int size, QColor color);
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);

protected:

Expand Down

0 comments on commit 3d04984

Please sign in to comment.