Skip to content

Commit

Permalink
[labeling] test for rotation unit
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Sep 1, 2021
1 parent 44fb7d6 commit b19b396
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
11 changes: 9 additions & 2 deletions python/core/auto_generated/labeling/qgspallabeling.sip.in
Expand Up @@ -11,7 +11,6 @@




class QgsPalLayerSettings
{
%Docstring(signature="appended")
Expand Down Expand Up @@ -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;

Expand Down
20 changes: 15 additions & 5 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/core/labeling/qgspallabeling.h
Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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();
};

Expand Down
6 changes: 3 additions & 3 deletions src/gui/labeling/qgslabelinggui.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand Down
44 changes: 44 additions & 0 deletions tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -77,6 +77,7 @@ class TestQgsLabelingEngine : public QObject
void testLabelBoundary();
void testLabelBlockingRegion();
void testLabelRotationWithReprojection();
void testLabelRotationUnit();
void drawUnplaced();
void labelingResults();
void labelingResultsWithCallouts();
Expand Down Expand Up @@ -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
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b19b396

Please sign in to comment.