Skip to content

Commit bba29e4

Browse files
committed
[FEATURE] Top/bottom centering placement for decoration items
The centered placement is implemented for: - Title decoration - Copyright decoration
1 parent 95f69c5 commit bba29e4

8 files changed

+53
-8
lines changed

images/images.qrc

+1
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@
584584
<file>themes/default/symbologyRemove.svg</file>
585585
<file>themes/default/sync_views.svg</file>
586586
<file>themes/default/text.svg</file>
587+
<file>themes/default/copyright_label.svg</file>
587588
<file>themes/default/tracking.svg</file>
588589
<file>themes/default/transformed.svg</file>
589590
<file>themes/default/transp-background_8x8.png</file>

src/app/decorations/qgsdecorationcopyright.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende
121121
QStringList displayStringList = displayString.split( QStringLiteral( "\n" ) );
122122

123123
QFontMetricsF fm( mTextFormat.scaledFont( context ) );
124+
QFontMetricsF textMetrics = QgsTextRenderer::fontMetrics( context, mTextFormat );
125+
double textDescent = textMetrics.descent();
124126
double textWidth = QgsTextRenderer::textWidth( context, mTextFormat, displayStringList, &fm );
125127
double textHeight = QgsTextRenderer::textHeight( context, mTextFormat, displayStringList, QgsTextRenderer::Point, &fm );
126128

@@ -166,23 +168,33 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende
166168
switch ( mPlacement )
167169
{
168170
case BottomLeft: // Bottom Left, xOffset is set above
169-
yOffset = deviceHeight - yOffset;
171+
yOffset = deviceHeight - yOffset - textDescent;
170172
break;
171173
case TopLeft: // Top left, xOffset is set above
172-
yOffset = yOffset + textHeight;
174+
yOffset = yOffset + textHeight - textDescent;
173175
break;
174176
case TopRight: // Top Right
175-
yOffset = yOffset + textHeight;
177+
yOffset = yOffset + textHeight - textDescent;
176178
xOffset = deviceWidth - xOffset;
177179
horizontalAlignment = QgsTextRenderer::AlignRight;
178180
break;
179181
case BottomRight: // Bottom Right
180-
yOffset = deviceHeight - yOffset;
182+
yOffset = deviceHeight - yOffset - textDescent;
181183
xOffset = deviceWidth - xOffset;
182184
horizontalAlignment = QgsTextRenderer::AlignRight;
183185
break;
186+
case TopCenter: // Top Center
187+
yOffset = yOffset + textHeight - textDescent;
188+
xOffset = deviceWidth / 2;
189+
horizontalAlignment = QgsTextRenderer::AlignCenter;
190+
break;
191+
case BottomCenter: // Bottom Center
192+
yOffset = deviceHeight - yOffset - textDescent;
193+
xOffset = deviceWidth / 2;
194+
horizontalAlignment = QgsTextRenderer::AlignCenter;
195+
break;
184196
default:
185-
QgsDebugMsg( QStringLiteral( "Unknown placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
197+
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
186198
}
187199

188200
//Paint label to canvas

src/app/decorations/qgsdecorationcopyrightdialog.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig
6161

6262
// placement
6363
cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft );
64+
cboPlacement->addItem( tr( "Top center" ), QgsDecorationItem::TopCenter );
6465
cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight );
6566
cboPlacement->addItem( tr( "Bottom left" ), QgsDecorationItem::BottomLeft );
67+
cboPlacement->addItem( tr( "Bottom center" ), QgsDecorationItem::BottomCenter );
6668
cboPlacement->addItem( tr( "Bottom right" ), QgsDecorationItem::BottomRight );
6769
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
6870
spnHorizontal->setValue( mDeco.mMarginHorizontal );

src/app/decorations/qgsdecorationitem.h

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class APP_EXPORT QgsDecorationItem : public QObject, public QgsMapDecoration
4141
TopLeft,
4242
TopRight,
4343
BottomRight,
44+
TopCenter,
45+
BottomCenter
4446
};
4547

4648
//! Constructor

src/app/decorations/qgsdecorationnortharrow.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
218218
context.painter()->translate( deviceWidth - xOffset - maxLength + ( maxLength - size.width() ) / 2,
219219
deviceHeight - yOffset - maxLength + ( maxLength - size.height() ) / 2 );
220220
break;
221+
case TopCenter:
222+
case BottomCenter:
223+
default:
224+
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
221225
}
222226

223227
//rotate the canvas by the north arrow rotation amount

src/app/decorations/qgsdecorationscalebar.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ void QgsDecorationScaleBar::render( const QgsMapSettings &mapSettings, QgsRender
369369
originX = deviceWidth - originX - size.width();
370370
originY = deviceHeight - originY - size.height();
371371
break;
372+
case TopCenter:
373+
case BottomCenter:
374+
default:
375+
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
372376
}
373377

374378
context.painter()->save();

src/app/decorations/qgsdecorationtitle.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
QgsDecorationTitle::QgsDecorationTitle( QObject *parent )
4141
: QgsDecorationItem( parent )
4242
{
43-
mPlacement = TopLeft;
43+
mPlacement = TopCenter;
4444
mMarginUnit = QgsUnitTypes::RenderMillimeters;
4545

4646
setName( "Title Label" );
@@ -53,7 +53,7 @@ void QgsDecorationTitle::projectRead()
5353
QgsDecorationItem::projectRead();
5454

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

5858
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
5959
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 );
@@ -190,8 +190,26 @@ void QgsDecorationTitle::render( const QgsMapSettings &mapSettings, QgsRenderCon
190190
xOffset = deviceWidth - xOffset;
191191
horizontalAlignment = QgsTextRenderer::AlignRight;
192192
break;
193+
case TopCenter: // Top Center
194+
backgroundBar << QPointF( 0, 0 )
195+
<< QPointF( deviceWidth, 0 )
196+
<< QPointF( deviceWidth, yOffset * 2 + textHeight )
197+
<< QPointF( 0, yOffset * 2 + textHeight );
198+
yOffset = yOffset + textHeight - textDescent;
199+
xOffset = deviceWidth / 2;
200+
horizontalAlignment = QgsTextRenderer::AlignCenter;
201+
break;
202+
case BottomCenter: // Bottom Center
203+
backgroundBar << QPointF( 0, deviceHeight )
204+
<< QPointF( deviceWidth, deviceHeight )
205+
<< QPointF( deviceWidth, deviceHeight - ( yOffset * 2 + textHeight ) )
206+
<< QPointF( 0, deviceHeight - ( yOffset * 2 + textHeight ) );
207+
yOffset = deviceHeight - yOffset - textDescent;
208+
xOffset = deviceWidth / 2;
209+
horizontalAlignment = QgsTextRenderer::AlignCenter;
210+
break;
193211
default:
194-
QgsDebugMsg( QStringLiteral( "Unknown placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
212+
QgsDebugMsg( QStringLiteral( "Unsupported placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
195213
}
196214

197215
// Draw background bar

src/app/decorations/qgsdecorationtitledialog.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ QgsDecorationTitleDialog::QgsDecorationTitleDialog( QgsDecorationTitle &deco, QW
6767

6868
// placement
6969
cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft );
70+
cboPlacement->addItem( tr( "Top center" ), QgsDecorationItem::TopCenter );
7071
cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight );
7172
cboPlacement->addItem( tr( "Bottom left" ), QgsDecorationItem::BottomLeft );
73+
cboPlacement->addItem( tr( "Bottom center" ), QgsDecorationItem::BottomCenter );
7274
cboPlacement->addItem( tr( "Bottom right" ), QgsDecorationItem::BottomRight );
7375
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
7476
spnHorizontal->setValue( mDeco.mMarginHorizontal );

0 commit comments

Comments
 (0)