Skip to content

Commit

Permalink
[FEATURE] allow data defined rotation without data defined position
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Sep 15, 2012
1 parent eb2f7cf commit 8a347eb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
5 changes: 0 additions & 5 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -1043,15 +1043,10 @@ void QgsLabelingGui::disableDataDefinedAlignment()
mHorizontalAlignmentComboBox->setEnabled( false );
mVerticalAlignmentComboBox->setCurrentIndex( mVerticalAlignmentComboBox->findText( "" ) );
mVerticalAlignmentComboBox->setEnabled( false );
mRotationComboBox->setCurrentIndex( mRotationComboBox->findText( "" ) );
mRotationComboBox->setEnabled( false );
chkPreserveRotation->setEnabled( false );
}

void QgsLabelingGui::enableDataDefinedAlignment()
{
mHorizontalAlignmentComboBox->setEnabled( true );
mVerticalAlignmentComboBox->setEnabled( true );
mRotationComboBox->setEnabled( true );
chkPreserveRotation->setEnabled( true );
}
27 changes: 11 additions & 16 deletions src/core/pal/feature.cpp
Expand Up @@ -254,7 +254,7 @@ namespace pal
return f->uid;
}

int FeaturePart::setPositionOverPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width )
int FeaturePart::setPositionOverPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle )
{
Q_UNUSED( scale );
Q_UNUSED( delta_width );
Expand All @@ -269,20 +269,19 @@ namespace pal
double ly = y - label_y / 2;
double cost = 0.0001;
int id = 0;
double alpha = 0;

double offset = label_x / 4;

// at the center
( *lPos )[0] = new LabelPosition( id, lx, ly, label_x, label_y, alpha, cost, this );
( *lPos )[0] = new LabelPosition( id, lx, ly, label_x, label_y, angle, cost, this );
// shifted to the sides - with higher cost
cost = 0.0021;
( *lPos )[1] = new LabelPosition( id, lx + offset, ly, label_x, label_y, alpha, cost, this );
( *lPos )[2] = new LabelPosition( id, lx - offset, ly, label_x, label_y, alpha, cost, this );
( *lPos )[1] = new LabelPosition( id, lx + offset, ly, label_x, label_y, angle, cost, this );
( *lPos )[2] = new LabelPosition( id, lx - offset, ly, label_x, label_y, angle, cost, this );
return nbp;
}

int FeaturePart::setPositionForPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width )
int FeaturePart::setPositionForPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle )
{

#ifdef _DEBUG_
Expand Down Expand Up @@ -426,7 +425,7 @@ namespace pal
else
cost = 0.0001 + 0.0020 * double( icost ) / double( nbp - 1 );

( *lPos )[i] = new LabelPosition( i, lx, ly, xrm, yrm, 0, cost, this );
( *lPos )[i] = new LabelPosition( i, lx, ly, xrm, yrm, angle, cost, this );

icost += inc;

Expand Down Expand Up @@ -1247,16 +1246,12 @@ namespace pal
bbox[3] = bbox_max[1];

double delta = bbox_max[0] - bbox_min[0];
double angle = f->fixedRotation ? f->fixedAngle : 0.0;

if ( f->fixedPosition() )
{
nbp = 1;
*lPos = new LabelPosition *[nbp];
double angle = 0.0;
if ( f->fixedRotation )
{
angle = f->fixedAngle;
}
( *lPos )[0] = new LabelPosition( 0, f->fixedPosX, f->fixedPosY, f->label_x, f->label_y, angle, 0.0, this );
}
else
Expand All @@ -1265,9 +1260,9 @@ namespace pal
{
case GEOS_POINT:
if ( f->layer->getArrangement() == P_POINT_OVER )
nbp = setPositionOverPoint( x[0], y[0], scale, lPos, delta );
nbp = setPositionOverPoint( x[0], y[0], scale, lPos, delta, angle );
else
nbp = setPositionForPoint( x[0], y[0], scale, lPos, delta );
nbp = setPositionForPoint( x[0], y[0], scale, lPos, delta, angle );
break;
case GEOS_LINESTRING:
if ( f->layer->getArrangement() == P_CURVED )
Expand All @@ -1284,9 +1279,9 @@ namespace pal
double cx, cy;
mapShape->getCentroid( cx, cy );
if ( f->layer->getArrangement() == P_POINT_OVER )
nbp = setPositionOverPoint( cx, cy, scale, lPos, delta );
nbp = setPositionOverPoint( cx, cy, scale, lPos, delta, angle );
else
nbp = setPositionForPoint( cx, cy, scale, lPos, delta );
nbp = setPositionForPoint( cx, cy, scale, lPos, delta, angle );
break;
case P_LINE:
nbp = setPositionForLine( scale, lPos, mapShape, delta );
Expand Down
5 changes: 3 additions & 2 deletions src/core/pal/feature.h
Expand Up @@ -158,14 +158,15 @@ namespace pal
* \param y y coordinates of the point
* \param scale map scale is 1:scale
* \param lPos pointer to an array of candidates, will be filled by generated candidates
* \param angle orientation of the label
* \return the number of generated cadidates
*/
int setPositionForPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width );
int setPositionForPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle );

/**
* generate one candidate over specified point
*/
int setPositionOverPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width );
int setPositionOverPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle );

/**
* \brief generate candidates for line feature
Expand Down
33 changes: 19 additions & 14 deletions src/core/qgspallabeling.cpp
Expand Up @@ -276,7 +276,7 @@ static void _writeDataDefinedPropertyMap( QgsVectorLayer* layer, const QMap< Qgs
{
return;
}
for ( int i = 0; i < 20; ++i )
for ( int i = 0; i < QgsPalLayerSettings::PropertyCount; ++i )
{
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator it = propertyMap.find(( QgsPalLayerSettings::DataDefinedProperties )i );
QVariant propertyValue;
Expand Down Expand Up @@ -724,15 +724,24 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f

if ( geos_geom == NULL )
return; // invalid geometry
GEOSGeometry* geos_geom_clone = GEOSGeom_clone( geos_geom );

GEOSGeometry* geos_geom_clone = GEOSGeom_clone( geos_geom );

//data defined position / alignment / rotation?
bool dataDefinedPosition = false;
bool labelIsPinned = false;
bool dataDefinedRotation = false;
double xPos = 0.0, yPos = 0.0, angle = 0.0;
bool ddXPos, ddYPos;
bool ddXPos = false, ddYPos = false;

//data defined rotation?
QMap< DataDefinedProperties, int >::const_iterator rotIt = dataDefinedProperties.find( QgsPalLayerSettings::Rotation );
if ( rotIt != dataDefinedProperties.constEnd() )
{
dataDefinedRotation = true;
angle = f.attributeMap().value( *rotIt ).toDouble() * M_PI / 180.0;

}

QMap< DataDefinedProperties, int >::const_iterator dPosXIt = dataDefinedProperties.find( QgsPalLayerSettings::PositionX );
if ( dPosXIt != dataDefinedProperties.constEnd() )
Expand All @@ -749,8 +758,8 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
dataDefinedPosition = true;
labelIsPinned = true;
//x/y shift in case of alignment
double xdiff = 0;
double ydiff = 0;
double xdiff = 0.0;
double ydiff = 0.0;

//horizontal alignment
QMap< DataDefinedProperties, int >::const_iterator haliIt = dataDefinedProperties.find( QgsPalLayerSettings::Hali );
Expand Down Expand Up @@ -796,12 +805,8 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
}
}

//data defined rotation?
QMap< DataDefinedProperties, int >::const_iterator rotIt = dataDefinedProperties.find( QgsPalLayerSettings::Rotation );
if ( rotIt != dataDefinedProperties.constEnd() )
if( dataDefinedRotation )
{
dataDefinedRotation = true;
angle = f.attributeMap().value( *rotIt ).toDouble() * M_PI / 180;
//adjust xdiff and ydiff because the hali/vali point needs to be the rotation center
double xd = xdiff * cos( angle ) - ydiff * sin( angle );
double yd = xdiff * sin( angle ) + ydiff * cos( angle );
Expand All @@ -816,15 +821,14 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
ct->transformInPlace( xPos, yPos, z );
}

yPos += ydiff;
xPos += xdiff;

yPos += ydiff;
}
}
}

// treat rotated labels of PAL layer point/centroid features as data defined
// does not flag label as pinned or rotatble
// does not flag label as pinned or rotateable
// always set rotation center as if Center/Half were set for data defined
bool overPointCentroid = false;
if ( !dataDefinedPosition
Expand Down Expand Up @@ -1722,7 +1726,8 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, co
painter->scale( 1.0 / lyr.rasterCompressFactor, 1.0 / lyr.rasterCompressFactor );

double yMultiLineOffset = ( multiLineList.size() - 1 - i ) * lyr.fontMetrics->height();
painter->translate( QPointF( 0, - lyr.fontMetrics->descent() - yMultiLineOffset ) );
// yMultiLineOffset += lyr.fontMetrics->descent();
painter->translate( QPointF( 0, -yMultiLineOffset ) );

if ( drawBuffer )
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgspallabeling.h
Expand Up @@ -103,7 +103,8 @@ class CORE_EXPORT QgsPalLayerSettings
MinScale,
MaxScale,
FontTransp,
BufferTransp
BufferTransp,
PropertyCount, // keep last entry
};

QString fieldName;
Expand Down

0 comments on commit 8a347eb

Please sign in to comment.