Skip to content
Permalink
Browse files
[labeling] test for rotation unit
  • Loading branch information
domi4484 committed Sep 1, 2021
1 parent 44fb7d6 commit b19b39637e6cde27fd8b0cdce44658de52b22b29
@@ -11,7 +11,6 @@




class QgsPalLayerSettings
{
%Docstring(signature="appended")
@@ -395,7 +394,15 @@ Sets the polygon placement ``flags``, which dictate how polygon labels can be pl

bool preserveRotation;

QgsUnitTypes::AngleUnit rotationUnit;
QgsUnitTypes::AngleUnit rotationUnit() const;
%Docstring
Unit for rotation of labels.
%End

void setRotationUnit( QgsUnitTypes::AngleUnit angleUnit );
%Docstring
Set unit for rotation of labels.
%End

double maxCurvedCharAngleIn;

@@ -332,7 +332,7 @@ QgsPalLayerSettings &QgsPalLayerSettings::operator=( const QgsPalLayerSettings &
distMapUnitScale = s.distMapUnitScale;
angleOffset = s.angleOffset;
preserveRotation = s.preserveRotation;
rotationUnit = s.rotationUnit;
mRotationUnit = s.mRotationUnit;
maxCurvedCharAngleIn = s.maxCurvedCharAngleIn;
maxCurvedCharAngleOut = s.maxCurvedCharAngleOut;
priority = s.priority;
@@ -607,6 +607,16 @@ QgsExpression *QgsPalLayerSettings::getLabelExpression()
return expression;
}

QgsUnitTypes::AngleUnit QgsPalLayerSettings::rotationUnit() const
{
return mRotationUnit;
}

void QgsPalLayerSettings::setRotationUnit( QgsUnitTypes::AngleUnit angleUnit )
{
mRotationUnit = angleUnit;
}

QString updateDataDefinedString( const QString &value )
{
// TODO: update or remove this when project settings for labeling are migrated to better XML layout
@@ -798,7 +808,7 @@ void QgsPalLayerSettings::readFromLayerCustomProperties( QgsVectorLayer *layer )
}

preserveRotation = layer->customProperty( QStringLiteral( "labeling/preserveRotation" ), QVariant( true ) ).toBool();
rotationUnit = static_cast<QgsUnitTypes::AngleUnit>( layer->customProperty( QStringLiteral( "labeling/rotationUnit" ), QVariant( static_cast< unsigned int >( QgsUnitTypes::AngleDegrees ) ) ).toInt() );
mRotationUnit = layer->customEnumProperty( QStringLiteral( "labeling/rotationUnit" ), QgsUnitTypes::AngleDegrees );
maxCurvedCharAngleIn = layer->customProperty( QStringLiteral( "labeling/maxCurvedCharAngleIn" ), QVariant( 25.0 ) ).toDouble();
maxCurvedCharAngleOut = layer->customProperty( QStringLiteral( "labeling/maxCurvedCharAngleOut" ), QVariant( -25.0 ) ).toDouble();
priority = layer->customProperty( QStringLiteral( "labeling/priority" ) ).toInt();
@@ -1026,7 +1036,7 @@ void QgsPalLayerSettings::readXml( const QDomElement &elem, const QgsReadWriteCo
}

preserveRotation = placementElem.attribute( QStringLiteral( "preserveRotation" ), QStringLiteral( "1" ) ).toInt();
rotationUnit = static_cast<QgsUnitTypes::AngleUnit>( placementElem.attribute( QStringLiteral( "rotationUnit" ), 0 ).toInt() );
mRotationUnit = qgsEnumKeyToValue( placementElem.attribute( QStringLiteral( "rotationUnit" ), qgsEnumValueToKey( QgsUnitTypes::AngleDegrees ) ), QgsUnitTypes::AngleDegrees );
maxCurvedCharAngleIn = placementElem.attribute( QStringLiteral( "maxCurvedCharAngleIn" ), QStringLiteral( "25" ) ).toDouble();
maxCurvedCharAngleOut = placementElem.attribute( QStringLiteral( "maxCurvedCharAngleOut" ), QStringLiteral( "-25" ) ).toDouble();
priority = placementElem.attribute( QStringLiteral( "priority" ) ).toInt();
@@ -1216,7 +1226,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc, const QgsReadWrite
placementElem.setAttribute( QStringLiteral( "labelOffsetMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( labelOffsetMapUnitScale ) );
placementElem.setAttribute( QStringLiteral( "rotationAngle" ), angleOffset );
placementElem.setAttribute( QStringLiteral( "preserveRotation" ), preserveRotation );
placementElem.setAttribute( QStringLiteral( "rotationUnit" ), static_cast< unsigned int >( rotationUnit ) );
placementElem.setAttribute( QStringLiteral( "rotationUnit" ), qgsEnumValueToKey( mRotationUnit ) );
placementElem.setAttribute( QStringLiteral( "maxCurvedCharAngleIn" ), maxCurvedCharAngleIn );
placementElem.setAttribute( QStringLiteral( "maxCurvedCharAngleOut" ), maxCurvedCharAngleOut );
placementElem.setAttribute( QStringLiteral( "priority" ), priority );
@@ -2342,7 +2352,7 @@ std::unique_ptr<QgsLabelFeature> QgsPalLayerSettings::registerFeatureWithDetails
{
dataDefinedRotation = true;

double rotationDegrees = rotation * QgsUnitTypes::fromUnitToUnitFactor( rotationUnit,
double rotationDegrees = rotation * QgsUnitTypes::fromUnitToUnitFactor( mRotationUnit,
QgsUnitTypes::AngleDegrees );

// TODO: add setting to disable having data defined rotation follow
@@ -15,9 +15,6 @@
* *
***************************************************************************/

//Note: although this file is in the core library, it is not part of the stable API
//and might change at any time!

#ifndef QGSPALLABELING_H
#define QGSPALLABELING_H

@@ -637,7 +634,10 @@ class CORE_EXPORT QgsPalLayerSettings
bool preserveRotation = true;

//! Unit for rotation of labels.
QgsUnitTypes::AngleUnit rotationUnit = QgsUnitTypes::AngleDegrees;
QgsUnitTypes::AngleUnit rotationUnit() const;

//! Set unit for rotation of labels.
void setRotationUnit( QgsUnitTypes::AngleUnit angleUnit );

/**
* Maximum angle between inside curved label characters (valid range 20.0 to 60.0).
@@ -1137,6 +1137,9 @@ class CORE_EXPORT QgsPalLayerSettings

Qgis::UnplacedLabelVisibility mUnplacedVisibility = Qgis::UnplacedLabelVisibility::FollowEngineSetting;

//! Unit for rotation of labels.
QgsUnitTypes::AngleUnit mRotationUnit = QgsUnitTypes::AngleDegrees;

static void initPropertyDefinitions();
};

@@ -435,8 +435,8 @@ void QgsLabelingGui::setLayer( QgsMapLayer *mapLayer )
chkPreserveRotation->setChecked( mSettings.preserveRotation );

mCoordRotationUnitComboBox->setCurrentIndex( 0 );
if ( mCoordRotationUnitComboBox->findData( static_cast< unsigned int >( mSettings.rotationUnit ) ) >= 0 )
mCoordRotationUnitComboBox->setCurrentIndex( mCoordRotationUnitComboBox->findData( static_cast< unsigned int >( mSettings.rotationUnit ) ) );
if ( mCoordRotationUnitComboBox->findData( static_cast< unsigned int >( mSettings.rotationUnit() ) ) >= 0 )
mCoordRotationUnitComboBox->setCurrentIndex( mCoordRotationUnitComboBox->findData( static_cast< unsigned int >( mSettings.rotationUnit() ) ) );

mScaleBasedVisibilityChkBx->setChecked( mSettings.scaleVisibility );
mMinScaleWidget->setScale( mSettings.minimumScale );
@@ -620,7 +620,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.useMaxLineLengthForAutoWrap = mAutoWrapTypeComboBox->currentIndex() == 0;
lyr.multilineAlign = static_cast< QgsPalLayerSettings::MultiLineAlign >( mFontMultiLineAlignComboBox->currentData().toInt() );
lyr.preserveRotation = chkPreserveRotation->isChecked();
lyr.rotationUnit = static_cast< QgsUnitTypes::AngleUnit >( mCoordRotationUnitComboBox->currentData().toInt() );
lyr.setRotationUnit( static_cast< QgsUnitTypes::AngleUnit >( mCoordRotationUnitComboBox->currentData().toInt() ) );
lyr.geometryGenerator = mGeometryGenerator->text();
lyr.geometryGeneratorType = mGeometryGeneratorType->currentData().value<QgsWkbTypes::GeometryType>();
lyr.geometryGeneratorEnabled = mGeometryGeneratorGroupBox->isChecked();
@@ -77,6 +77,7 @@ class TestQgsLabelingEngine : public QObject
void testLabelBoundary();
void testLabelBlockingRegion();
void testLabelRotationWithReprojection();
void testLabelRotationUnit();
void drawUnplaced();
void labelingResults();
void labelingResultsWithCallouts();
@@ -1854,6 +1855,49 @@ void TestQgsLabelingEngine::testLabelRotationWithReprojection()
QVERIFY( imageCheck( QStringLiteral( "label_rotate_with_reproject" ), img, 20 ) );
}

void TestQgsLabelingEngine::testLabelRotationUnit()
{
QSize size( 640, 480 );
QgsMapSettings mapSettings;
mapSettings.setLabelingEngineSettings( createLabelEngineSettings() );
mapSettings.setOutputSize( size );
mapSettings.setExtent( vl->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vl );
mapSettings.setOutputDpi( 96 );

// first render the map and labeling separately

QgsMapRendererSequentialJob job( mapSettings );
job.start();
job.waitForFinished();

QImage img = job.renderedImage();

QPainter p( &img );
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
context.setPainter( &p );

QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Class" );
setDefaultLabelParams( settings );

settings.dataDefinedProperties().setProperty( QgsPalLayerSettings::LabelRotation, QgsProperty::fromExpression( QString::number( 3.14 / 2.0 ) ) );
settings.setRotationUnit( QgsUnitTypes::AngleRadians );

vl->setLabelsEnabled( true );

QgsDefaultLabelingEngine engine;
engine.setMapSettings( mapSettings );
engine.addProvider( new QgsVectorLayerLabelProvider( vl, QString(), true, &settings ) );
engine.run( context );

p.end();

QVERIFY( imageCheck( "label_rotate_unit", img, 20 ) );

vl->setLabeling( nullptr );
}

void TestQgsLabelingEngine::drawUnplaced()
{
// test drawing unplaced labels
Binary file not shown.

0 comments on commit b19b396

Please sign in to comment.