Skip to content
Permalink
Browse files

[FEATURE] Top/bottom centering placement for decoration items

The centered placement is implemented for:
- Title decoration
- Copyright decoration
  • Loading branch information
nirvn committed Nov 14, 2018
1 parent 95f69c5 commit bba29e448625baa8095cc8410d2c970a0de6133f
@@ -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>
@@ -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 );

@@ -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
@@ -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 );
@@ -41,6 +41,8 @@ class APP_EXPORT QgsDecorationItem : public QObject, public QgsMapDecoration
TopLeft,
TopRight,
BottomRight,
TopCenter,
BottomCenter
};

//! Constructor
@@ -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
@@ -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();
@@ -40,7 +40,7 @@
QgsDecorationTitle::QgsDecorationTitle( QObject *parent )
: QgsDecorationItem( parent )
{
mPlacement = TopLeft;
mPlacement = TopCenter;
mMarginUnit = QgsUnitTypes::RenderMillimeters;

setName( "Title Label" );
@@ -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 );
@@ -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
@@ -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 );

0 comments on commit bba29e4

Please sign in to comment.
You can’t perform that action at this time.