Skip to content

Commit 9c6fe06

Browse files
author
Sandro Santilli
committed
Pre-rotate vectors before applying labels
Revert custom rotation handling in label placement, dropping rotation component from the transformation used to determine placement as rotation was applied up-front. Have manually placed labels follow the map.
1 parent a09d3d6 commit 9c6fe06

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

src/core/qgspallabeling.cpp

+50-6
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,17 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
17801780
clonedGeometry.reset( geom );
17811781
}
17821782

1783+
// Rotate the geometry if needed, before clipping
1784+
const QgsMapToPixel& m2p = context.mapToPixel();
1785+
if ( m2p.mapRotation() )
1786+
{
1787+
if ( geom->rotate( m2p.mapRotation(), context.extent().center() ) )
1788+
{
1789+
QgsDebugMsg( QString("Error rotating geometry").arg( geom->exportToWkt() ) );
1790+
return; // really ?
1791+
}
1792+
}
1793+
17831794
// CLIP the geometry if it is bigger than the extent
17841795
// don't clip if centroid is requested for whole feature
17851796
bool do_clip = false;
@@ -1967,6 +1978,9 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
19671978
if ( ok )
19681979
{
19691980
dataDefinedRotation = true;
1981+
// TODO: add setting to disable having data defined rotation follow
1982+
// map rotation ?
1983+
rotD -= m2p.mapRotation();
19701984
angle = rotD * M_PI / 180.0;
19711985
}
19721986
}
@@ -2070,6 +2084,18 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
20702084
}
20712085
}
20722086

2087+
//rotate position with map if data-defined
2088+
if ( dataDefinedPosition && m2p.mapRotation() )
2089+
{
2090+
const QgsPoint& center = context.extent().center();
2091+
QTransform t = QTransform::fromTranslate( center.x(), center.y() );
2092+
t.rotate( -m2p.mapRotation() );
2093+
t.translate( -center.x(), -center.y() );
2094+
double xPosR, yPosR;
2095+
t.map( xPos, yPos, &xPosR, &yPosR );
2096+
xPos = xPosR; yPos = yPosR;
2097+
}
2098+
20732099
xPos += xdiff;
20742100
yPos += ydiff;
20752101
}
@@ -3876,7 +3902,16 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
38763902
if ( context.renderingStopped() )
38773903
return; // it has been cancelled
38783904

3905+
#if 1 // XXX strk
3906+
// features are pre-rotated but not scaled/translated,
3907+
// so we only disable rotation here. Ideally, they'd be
3908+
// also pre-scaled/translated, as suggested here:
3909+
// http://hub.qgis.org/issues/11856
3910+
QgsMapToPixel xform = mMapSettings->mapToPixel();
3911+
xform.setMapRotation(0,0,0);
3912+
#else
38793913
const QgsMapToPixel& xform = mMapSettings->mapToPixel();
3914+
#endif
38803915

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

41254160
painter->save();
41264161

4127-
#if 1 // TODO: generalize some of this
4162+
#if 0 // TODO: generalize some of this
41284163
double w = lp->getWidth();
41294164
double h = lp->getHeight();
41304165
double cx = lp->getX() + w / 2.0;
@@ -4172,12 +4207,21 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
41724207
{
41734208
// NOTE: this is repeatedly called for multi-part labels
41744209
QPainter* painter = context.painter();
4175-
const QgsMapToPixel* xform = &context.mapToPixel();
4210+
#if 1 // XXX strk
4211+
// features are pre-rotated but not scaled/translated,
4212+
// so we only disable rotation here. Ideally, they'd be
4213+
// also pre-scaled/translated, as suggested here:
4214+
// http://hub.qgis.org/issues/11856
4215+
QgsMapToPixel xform = context.mapToPixel();
4216+
xform.setMapRotation(0,0,0);
4217+
#else
4218+
const QgsMapToPixel& xform = context.mapToPixel();
4219+
#endif
41764220

41774221
QgsLabelComponent component;
41784222
component.setDpiRatio( dpiRatio );
41794223

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

@@ -4188,8 +4232,8 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
41884232
{
41894233
// get rotated label's center point
41904234
QgsPoint centerPt( outPt );
4191-
QgsPoint outPt2 = xform->transform( label->getX() + label->getWidth() / 2,
4192-
label->getY() + label->getHeight() / 2 );
4235+
QgsPoint outPt2 = xform.transform( label->getX() + label->getWidth() / 2,
4236+
label->getY() + label->getHeight() / 2 );
41934237

41944238
double xc = outPt2.x() - outPt.x();
41954239
double yc = outPt2.y() - outPt.y();
@@ -4290,7 +4334,7 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
42904334
for ( int i = 0; i < lines; ++i )
42914335
{
42924336
painter->save();
4293-
#if 1 // TODO: generalize some of this
4337+
#if 0 // TODO: generalize some of this
42944338
LabelPosition* lp = label;
42954339
double w = lp->getWidth();
42964340
double h = lp->getHeight();

0 commit comments

Comments
 (0)