Skip to content

Commit

Permalink
[FEATURE] Top/bottom centering placement for decoration items
Browse files Browse the repository at this point in the history
The centered placement is implemented for:
- Title decoration
- Copyright decoration
  • Loading branch information
nirvn committed Nov 15, 2018
1 parent 95f69c5 commit bba29e4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -584,6 +584,7 @@
<file>themes/default/symbologyRemove.svg</file>
<file>themes/default/sync_views.svg</file>
<file>themes/default/text.svg</file>
<file>themes/default/copyright_label.svg</file>
<file>themes/default/tracking.svg</file>
<file>themes/default/transformed.svg</file>
<file>themes/default/transp-background_8x8.png</file>
Expand Down
22 changes: 17 additions & 5 deletions src/app/decorations/qgsdecorationcopyright.cpp
Expand Up @@ -121,6 +121,8 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende
QStringList displayStringList = displayString.split( QStringLiteral( "\n" ) );

QFontMetricsF fm( mTextFormat.scaledFont( context ) );
QFontMetricsF textMetrics = QgsTextRenderer::fontMetrics( context, mTextFormat );
double textDescent = textMetrics.descent();
double textWidth = QgsTextRenderer::textWidth( context, mTextFormat, displayStringList, &fm );
double textHeight = QgsTextRenderer::textHeight( context, mTextFormat, displayStringList, QgsTextRenderer::Point, &fm );

Expand Down Expand Up @@ -166,23 +168,33 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende
switch ( mPlacement )
{
case BottomLeft: // Bottom Left, xOffset is set above
yOffset = deviceHeight - yOffset;
yOffset = deviceHeight - yOffset - textDescent;
break;
case TopLeft: // Top left, xOffset is set above
yOffset = yOffset + textHeight;
yOffset = yOffset + textHeight - textDescent;
break;
case TopRight: // Top Right
yOffset = yOffset + textHeight;
yOffset = yOffset + textHeight - textDescent;
xOffset = deviceWidth - xOffset;
horizontalAlignment = QgsTextRenderer::AlignRight;
break;
case BottomRight: // Bottom Right
yOffset = deviceHeight - yOffset;
yOffset = deviceHeight - yOffset - textDescent;
xOffset = deviceWidth - xOffset;
horizontalAlignment = QgsTextRenderer::AlignRight;
break;
case TopCenter: // Top Center
yOffset = yOffset + textHeight - textDescent;
xOffset = deviceWidth / 2;
horizontalAlignment = QgsTextRenderer::AlignCenter;
break;
case BottomCenter: // Bottom Center
yOffset = deviceHeight - yOffset - textDescent;
xOffset = deviceWidth / 2;
horizontalAlignment = QgsTextRenderer::AlignCenter;
break;
default:
QgsDebugMsg( QStringLiteral( "Unknown placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
}

//Paint label to canvas
Expand Down
2 changes: 2 additions & 0 deletions src/app/decorations/qgsdecorationcopyrightdialog.cpp
Expand Up @@ -61,8 +61,10 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig

// placement
cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft );
cboPlacement->addItem( tr( "Top center" ), QgsDecorationItem::TopCenter );
cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight );
cboPlacement->addItem( tr( "Bottom left" ), QgsDecorationItem::BottomLeft );
cboPlacement->addItem( tr( "Bottom center" ), QgsDecorationItem::BottomCenter );
cboPlacement->addItem( tr( "Bottom right" ), QgsDecorationItem::BottomRight );
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
spnHorizontal->setValue( mDeco.mMarginHorizontal );
Expand Down
2 changes: 2 additions & 0 deletions src/app/decorations/qgsdecorationitem.h
Expand Up @@ -41,6 +41,8 @@ class APP_EXPORT QgsDecorationItem : public QObject, public QgsMapDecoration
TopLeft,
TopRight,
BottomRight,
TopCenter,
BottomCenter
};

//! Constructor
Expand Down
4 changes: 4 additions & 0 deletions src/app/decorations/qgsdecorationnortharrow.cpp
Expand Up @@ -218,6 +218,10 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
context.painter()->translate( deviceWidth - xOffset - maxLength + ( maxLength - size.width() ) / 2,
deviceHeight - yOffset - maxLength + ( maxLength - size.height() ) / 2 );
break;
case TopCenter:
case BottomCenter:
default:
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
}

//rotate the canvas by the north arrow rotation amount
Expand Down
4 changes: 4 additions & 0 deletions src/app/decorations/qgsdecorationscalebar.cpp
Expand Up @@ -369,6 +369,10 @@ void QgsDecorationScaleBar::render( const QgsMapSettings &mapSettings, QgsRender
originX = deviceWidth - originX - size.width();
originY = deviceHeight - originY - size.height();
break;
case TopCenter:
case BottomCenter:
default:
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
}

context.painter()->save();
Expand Down
24 changes: 21 additions & 3 deletions src/app/decorations/qgsdecorationtitle.cpp
Expand Up @@ -40,7 +40,7 @@
QgsDecorationTitle::QgsDecorationTitle( QObject *parent )
: QgsDecorationItem( parent )
{
mPlacement = TopLeft;
mPlacement = TopCenter;
mMarginUnit = QgsUnitTypes::RenderMillimeters;

setName( "Title Label" );
Expand All @@ -53,7 +53,7 @@ void QgsDecorationTitle::projectRead()
QgsDecorationItem::projectRead();

mLabelText = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Label" ), QString() );
mBackgroundColor = QgsSymbolLayerUtils::decodeColor( QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/BackgroundColor" ), QStringLiteral( "0,0,0,66" ) ) );
mBackgroundColor = QgsSymbolLayerUtils::decodeColor( QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/BackgroundColor" ), QStringLiteral( "0,0,0,99" ) ) );

mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 );
Expand Down Expand Up @@ -190,8 +190,26 @@ void QgsDecorationTitle::render( const QgsMapSettings &mapSettings, QgsRenderCon
xOffset = deviceWidth - xOffset;
horizontalAlignment = QgsTextRenderer::AlignRight;
break;
case TopCenter: // Top Center
backgroundBar << QPointF( 0, 0 )
<< QPointF( deviceWidth, 0 )
<< QPointF( deviceWidth, yOffset * 2 + textHeight )
<< QPointF( 0, yOffset * 2 + textHeight );
yOffset = yOffset + textHeight - textDescent;
xOffset = deviceWidth / 2;
horizontalAlignment = QgsTextRenderer::AlignCenter;
break;
case BottomCenter: // Bottom Center
backgroundBar << QPointF( 0, deviceHeight )
<< QPointF( deviceWidth, deviceHeight )
<< QPointF( deviceWidth, deviceHeight - ( yOffset * 2 + textHeight ) )
<< QPointF( 0, deviceHeight - ( yOffset * 2 + textHeight ) );
yOffset = deviceHeight - yOffset - textDescent;
xOffset = deviceWidth / 2;
horizontalAlignment = QgsTextRenderer::AlignCenter;
break;
default:
QgsDebugMsg( QStringLiteral( "Unknown placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
}

// Draw background bar
Expand Down
2 changes: 2 additions & 0 deletions src/app/decorations/qgsdecorationtitledialog.cpp
Expand Up @@ -67,8 +67,10 @@ QgsDecorationTitleDialog::QgsDecorationTitleDialog( QgsDecorationTitle &deco, QW

// placement
cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft );
cboPlacement->addItem( tr( "Top center" ), QgsDecorationItem::TopCenter );
cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight );
cboPlacement->addItem( tr( "Bottom left" ), QgsDecorationItem::BottomLeft );
cboPlacement->addItem( tr( "Bottom center" ), QgsDecorationItem::BottomCenter );
cboPlacement->addItem( tr( "Bottom right" ), QgsDecorationItem::BottomRight );
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
spnHorizontal->setValue( mDeco.mMarginHorizontal );
Expand Down

0 comments on commit bba29e4

Please sign in to comment.