Skip to content

Commit

Permalink
[labeling] specify unit for data defined label rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Sep 1, 2021
1 parent 553bb27 commit 44fb7d6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/labeling/qgspallabeling.sip.in
Expand Up @@ -395,6 +395,8 @@ Sets the polygon placement ``flags``, which dictate how polygon labels can be pl

bool preserveRotation;

QgsUnitTypes::AngleUnit rotationUnit;

double maxCurvedCharAngleIn;

double maxCurvedCharAngleOut;
Expand Down
14 changes: 11 additions & 3 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -332,6 +332,7 @@ QgsPalLayerSettings &QgsPalLayerSettings::operator=( const QgsPalLayerSettings &
distMapUnitScale = s.distMapUnitScale;
angleOffset = s.angleOffset;
preserveRotation = s.preserveRotation;
rotationUnit = s.rotationUnit;
maxCurvedCharAngleIn = s.maxCurvedCharAngleIn;
maxCurvedCharAngleOut = s.maxCurvedCharAngleOut;
priority = s.priority;
Expand Down Expand Up @@ -797,6 +798,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() );
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 @@ -1024,6 +1026,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() );
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 @@ -1213,6 +1216,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( "maxCurvedCharAngleIn" ), maxCurvedCharAngleIn );
placementElem.setAttribute( QStringLiteral( "maxCurvedCharAngleOut" ), maxCurvedCharAngleOut );
placementElem.setAttribute( QStringLiteral( "priority" ), priority );
Expand Down Expand Up @@ -2333,14 +2337,18 @@ std::unique_ptr<QgsLabelFeature> QgsPalLayerSettings::registerFeatureWithDetails
if ( !exprVal.isNull() )
{
bool ok;
double rotD = exprVal.toDouble( &ok );
const double rotation = exprVal.toDouble( &ok );
if ( ok )
{
dataDefinedRotation = true;

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

// TODO: add setting to disable having data defined rotation follow
// map rotation ?
rotD += m2p.mapRotation();
angle = ( 360 - rotD ) * M_PI / 180.0;
rotationDegrees += m2p.mapRotation();
angle = ( 360 - rotationDegrees ) * M_PI / 180.0;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/labeling/qgspallabeling.h
Expand Up @@ -636,6 +636,9 @@ class CORE_EXPORT QgsPalLayerSettings
//! True if label rotation should be preserved during label pin/unpin operations.
bool preserveRotation = true;

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

/**
* Maximum angle between inside curved label characters (valid range 20.0 to 60.0).
* \see maxCurvedCharAngleOut
Expand Down
14 changes: 14 additions & 0 deletions src/gui/labeling/qgslabelinggui.cpp
Expand Up @@ -244,6 +244,15 @@ QgsLabelingGui::QgsLabelingGui( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas,
mFontMultiLineAlignComboBox->addItem( tr( "Right" ), QgsPalLayerSettings::MultiRight );
mFontMultiLineAlignComboBox->addItem( tr( "Justify" ), QgsPalLayerSettings::MultiJustify );

mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleDegrees ), QgsUnitTypes::AngleDegrees );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleRadians ), QgsUnitTypes::AngleRadians );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleGon ), QgsUnitTypes::AngleGon );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleMinutesOfArc ), QgsUnitTypes::AngleMinutesOfArc );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleSecondsOfArc ), QgsUnitTypes::AngleSecondsOfArc );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleTurn ), QgsUnitTypes::AngleTurn );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleMilliradiansSI ), QgsUnitTypes::AngleMilliradiansSI );
mCoordRotationUnitComboBox->addItem( QgsUnitTypes::toString( QgsUnitTypes::AngleMilNATO ), QgsUnitTypes::AngleMilNATO );

// connections for groupboxes with separate activation checkboxes (that need to honor data defined setting)
connect( mBufferDrawChkBx, &QAbstractButton::toggled, this, &QgsLabelingGui::updateUi );
connect( mBufferDrawDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLabelingGui::updateUi );
Expand Down Expand Up @@ -425,6 +434,10 @@ 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 ) ) );

mScaleBasedVisibilityChkBx->setChecked( mSettings.scaleVisibility );
mMinScaleWidget->setScale( mSettings.minimumScale );
mMaxScaleWidget->setScale( mSettings.maximumScale );
Expand Down Expand Up @@ -607,6 +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.geometryGenerator = mGeometryGenerator->text();
lyr.geometryGeneratorType = mGeometryGeneratorType->currentData().value<QgsWkbTypes::GeometryType>();
lyr.geometryGeneratorEnabled = mGeometryGeneratorGroupBox->isChecked();
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgstextformatwidget.cpp
Expand Up @@ -348,6 +348,7 @@ void QgsTextFormatWidget::initWidget()
<< mBufferOpacityWidget
<< mCentroidInsideCheckBox
<< mChkNoObstacle
<< mCoordRotationUnitComboBox
<< mDirectSymbChkBx
<< mDirectSymbLeftLineEdit
<< mDirectSymbRevChkBx
Expand Down
3 changes: 3 additions & 0 deletions src/ui/qgstextformatwidgetbase.ui
Expand Up @@ -5540,6 +5540,9 @@ font-style: italic;</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="mCoordRotationUnitComboBox"/>
</item>
<item>
<widget class="QCheckBox" name="chkPreserveRotation">
<property name="toolTip">
Expand Down

0 comments on commit 44fb7d6

Please sign in to comment.