Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label pre-rotation #1757

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/core/qgspalgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ class QgsPalGeometry : public PalGeometry
maxoutangle = -95.0;

// create label info!
QgsPoint ptZero = xform->toMapCoordinates( 0, 0 );
QgsPoint ptSize = xform->toMapCoordinatesF( 0.0, -fm->height() / fontScale );
double mapScale = xform->mapUnitsPerPixel();
double labelHeight = mapScale * fm->height() / fontScale;

// mLetterSpacing/mWordSpacing = 0.0 is default for non-curved labels
// (non-curved spacings handled by Qt in QgsPalLayerSettings/QgsPalLabeling)
qreal charWidth;
qreal wordSpaceFix;
mInfo = new pal::LabelInfo( mText.count(), ptSize.y() - ptZero.y(), maxinangle, maxoutangle );
mInfo = new pal::LabelInfo( mText.count(), labelHeight, maxinangle, maxoutangle );
for ( int i = 0; i < mText.count(); i++ )
{
mInfo->char_info[i].chr = mText[i].unicode();
Expand All @@ -99,8 +99,8 @@ class QgsPalGeometry : public PalGeometry
charWidth = fm->width( QString( mText[i] ) ) + wordSpaceFix;
}

ptSize = xform->toMapCoordinatesF((( double ) charWidth ) / fontScale, 0.0 );
mInfo->char_info[i].width = ptSize.x() - ptZero.x();
double labelWidth = mapScale * charWidth / fontScale;
mInfo->char_info[i].width = labelWidth;
}
return mInfo;
}
Expand Down
52 changes: 46 additions & 6 deletions src/core/qgspallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,17 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
clonedGeometry.reset( geom );
}

// Rotate the geometry if needed, before clipping
const QgsMapToPixel& m2p = context.mapToPixel();
if ( m2p.mapRotation() )
{
if ( geom->rotate( m2p.mapRotation(), context.extent().center() ) )
{
QgsDebugMsg( QString("Error rotating geometry").arg( geom->exportToWkt() ) );
return; // really ?
}
}

// CLIP the geometry if it is bigger than the extent
// don't clip if centroid is requested for whole feature
bool do_clip = false;
Expand Down Expand Up @@ -1967,6 +1978,9 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
if ( ok )
{
dataDefinedRotation = true;
// TODO: add setting to disable having data defined rotation follow
// map rotation ?
rotD -= m2p.mapRotation();
angle = rotD * M_PI / 180.0;
}
}
Expand Down Expand Up @@ -2070,6 +2084,18 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
}
}

//rotate position with map if data-defined
if ( dataDefinedPosition && m2p.mapRotation() )
{
const QgsPoint& center = context.extent().center();
QTransform t = QTransform::fromTranslate( center.x(), center.y() );
t.rotate( -m2p.mapRotation() );
t.translate( -center.x(), -center.y() );
double xPosR, yPosR;
t.map( xPos, yPos, &xPosR, &yPosR );
xPos = xPosR; yPos = yPosR;
}

xPos += xdiff;
yPos += ydiff;
}
Expand Down Expand Up @@ -3876,7 +3902,12 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
if ( context.renderingStopped() )
return; // it has been cancelled

#if 1 // XXX strk testing
QgsMapToPixel xform = mMapSettings->mapToPixel();
xform.setMapRotation(0,0,0);
#else
const QgsMapToPixel& xform = mMapSettings->mapToPixel();
#endif

// draw rectangles with all candidates
// this is done before actual solution of the problem
Expand Down Expand Up @@ -4124,7 +4155,7 @@ void QgsPalLabeling::drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* p

painter->save();

#if 1 // TODO: generalize some of this
#if 0 // TODO: generalize some of this
double w = lp->getWidth();
double h = lp->getHeight();
double cx = lp->getX() + w / 2.0;
Expand Down Expand Up @@ -4172,12 +4203,21 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
{
// NOTE: this is repeatedly called for multi-part labels
QPainter* painter = context.painter();
const QgsMapToPixel* xform = &context.mapToPixel();
#if 1
// features are pre-rotated but not scaled/translated,
// so we only disable rotation here. Ideally, they'd be
// also pre-scaled/translated, as suggested here:
// http://hub.qgis.org/issues/11856
QgsMapToPixel xform = context.mapToPixel();
xform.setMapRotation(0,0,0);
#else
const QgsMapToPixel& xform = context.mapToPixel();
#endif

QgsLabelComponent component;
component.setDpiRatio( dpiRatio );

QgsPoint outPt = xform->transform( label->getX(), label->getY() );
QgsPoint outPt = xform.transform( label->getX(), label->getY() );
// QgsPoint outPt2 = xform->transform( label->getX() + label->getWidth(), label->getY() + label->getHeight() );
// QRectF labelRect( 0, 0, outPt2.x() - outPt.x(), outPt2.y() - outPt.y() );

Expand All @@ -4188,8 +4228,8 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
{
// get rotated label's center point
QgsPoint centerPt( outPt );
QgsPoint outPt2 = xform->transform( label->getX() + label->getWidth() / 2,
label->getY() + label->getHeight() / 2 );
QgsPoint outPt2 = xform.transform( label->getX() + label->getWidth() / 2,
label->getY() + label->getHeight() / 2 );

double xc = outPt2.x() - outPt.x();
double yc = outPt2.y() - outPt.y();
Expand Down Expand Up @@ -4290,7 +4330,7 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
for ( int i = 0; i < lines; ++i )
{
painter->save();
#if 1 // TODO: generalize some of this
#if 0 // TODO: generalize some of this
LabelPosition* lp = label;
double w = lp->getWidth();
double h = lp->getHeight();
Expand Down