Skip to content

Commit babfb6b

Browse files
committed
Allow optional labels for composer extent decorations
1 parent 09110ea commit babfb6b

5 files changed

Lines changed: 101 additions & 1 deletion

‎src/app/qgsdecorationlayoutextent.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ void QgsDecorationLayoutExtent::projectRead()
6868
QgsSimpleLineSymbolLayer *layer = new QgsSimpleLineSymbolLayer( QColor( 0, 0, 0, 100 ), 0, Qt::DashLine );
6969
mSymbol->changeSymbolLayer( 0, layer );
7070
}
71+
72+
QString textXml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Font" ) );
73+
if ( !textXml.isEmpty() )
74+
{
75+
doc.setContent( textXml );
76+
elem = doc.documentElement();
77+
mTextFormat.readXml( elem, rwContext );
78+
}
79+
mLabelExtents = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/Labels" ), true );
7180
}
7281

7382
void QgsDecorationLayoutExtent::saveToProject()
@@ -85,6 +94,12 @@ void QgsDecorationLayoutExtent::saveToProject()
8594
// FIXME this works, but XML will not be valid as < is replaced by &lt;
8695
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Symbol" ), doc.toString() );
8796
}
97+
98+
QDomDocument textDoc;
99+
QDomElement textElem = mTextFormat.writeXml( textDoc, rwContext );
100+
textDoc.appendChild( textElem );
101+
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Font" ), textDoc.toString() );
102+
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Labels" ), mLabelExtents );
88103
}
89104

90105
void QgsDecorationLayoutExtent::run()
@@ -116,6 +131,7 @@ void QgsDecorationLayoutExtent::render( const QgsMapSettings &mapSettings, QgsRe
116131
Q_FOREACH ( const QgsComposerMap *map, composition->composerMapItems() )
117132
{
118133
QPolygonF extent = map->visibleExtentPolygon();
134+
QPointF labelPoint = extent.at( 1 );
119135
QgsGeometry g = QgsGeometry::fromQPolygonF( extent );
120136

121137
if ( map->crs() !=
@@ -128,21 +144,39 @@ void QgsDecorationLayoutExtent::render( const QgsMapSettings &mapSettings, QgsRe
128144
try
129145
{
130146
g.transform( ct );
147+
labelPoint = ct.transform( labelPoint.x(), labelPoint.y() ).toQPointF();
131148
}
132149
catch ( QgsCsException & )
133150
{
134151
}
135152
}
136153

137154
g.transform( transform );
155+
labelPoint = transform.map( labelPoint );
138156
extent = g.asQPolygonF();
139157
mSymbol->renderPolygon( extent, nullptr, nullptr, context );
158+
159+
if ( mLabelExtents )
160+
{
161+
QgsTextRenderer::drawText( labelPoint, ( map->mapRotation() - mapSettings.rotation() ) * M_PI / 180.0, QgsTextRenderer::AlignRight, QStringList() << tr( "%1: %2" ).arg( composition->name(), map->displayName() ),
162+
context, mTextFormat );
163+
}
140164
}
141165
}
142166
mSymbol->stopRender( context );
143167
context.painter()->restore();
144168
}
145169

170+
bool QgsDecorationLayoutExtent::labelExtents() const
171+
{
172+
return mLabelExtents;
173+
}
174+
175+
void QgsDecorationLayoutExtent::setLabelExtents( bool labelExtents )
176+
{
177+
mLabelExtents = labelExtents;
178+
}
179+
146180
QgsFillSymbol *QgsDecorationLayoutExtent::symbol() const
147181
{
148182
return mSymbol.get();

‎src/app/qgsdecorationlayoutextent.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QObject>
2626
#include "qgis_app.h"
2727
#include "qgssymbol.h"
28+
#include "qgstextrenderer.h"
2829
#include <memory>
2930

3031
class QgsDecorationLayoutExtentDialog;
@@ -52,6 +53,34 @@ class APP_EXPORT QgsDecorationLayoutExtent : public QgsDecorationItem
5253
*/
5354
void setSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
5455

56+
/**
57+
* Returns the text format for extent labels.
58+
* \see setTextFormat()
59+
* \see labelExtents()
60+
*/
61+
QgsTextFormat textFormat() const { return mTextFormat; }
62+
63+
/**
64+
* Sets the text \a format for extent labels.
65+
* \see textFormat()
66+
* \see setLabelExtents()
67+
*/
68+
void setTextFormat( const QgsTextFormat &format ) { mTextFormat = format; }
69+
70+
/**
71+
* Returns true if layout extents should be labeled with the name of the associated layout & map.
72+
* \see setLabelExtents()
73+
* \see textFormat()
74+
*/
75+
bool labelExtents() const;
76+
77+
/**
78+
* Sets whether layout extents should be labeled with the name of the associated layout & map.
79+
* \see labelExtents()
80+
* \see setTextFormat()
81+
*/
82+
void setLabelExtents( bool labelExtents );
83+
5584
public slots:
5685
void projectRead() override;
5786
void saveToProject() override;
@@ -60,6 +89,8 @@ class APP_EXPORT QgsDecorationLayoutExtent : public QgsDecorationItem
6089

6190
private:
6291
std::unique_ptr< QgsFillSymbol > mSymbol;
92+
QgsTextFormat mTextFormat;
93+
bool mLabelExtents = true;
6394

6495
friend class QgsDecorationLayoutExtentDialog;
6596
};

‎src/app/qgsdecorationlayoutextentdialog.cpp‎

100644100755
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgisapp.h"
2828
#include "qgsguiutils.h"
2929
#include "qgssettings.h"
30+
#include "qgstextformatwidget.h"
3031

3132
QgsDecorationLayoutExtentDialog::QgsDecorationLayoutExtentDialog( QgsDecorationLayoutExtent &deco, QWidget *parent )
3233
: QDialog( parent )
@@ -37,9 +38,15 @@ QgsDecorationLayoutExtentDialog::QgsDecorationLayoutExtentDialog( QgsDecorationL
3738
QgsSettings settings;
3839
restoreGeometry( settings.value( "/Windows/DecorationLayoutExtent/geometry" ).toByteArray() );
3940

41+
connect( mCheckBoxLabelExtents, &QCheckBox::toggled, mButtonFontStyle, &QPushButton::setEnabled );
42+
mCheckBoxLabelExtents->setChecked( false );
43+
mButtonFontStyle->setEnabled( false );
44+
4045
updateGuiElements();
4146
connect( buttonBox->button( QDialogButtonBox::Apply ), &QAbstractButton::clicked, this, &QgsDecorationLayoutExtentDialog::apply );
4247
connect( mSymbolButton, &QPushButton::clicked, this, &QgsDecorationLayoutExtentDialog::changeSymbol );
48+
connect( mButtonFontStyle, &QPushButton::clicked, this, &QgsDecorationLayoutExtentDialog::changeFont );
49+
4350
}
4451

4552
void QgsDecorationLayoutExtentDialog::updateGuiElements()
@@ -52,6 +59,8 @@ void QgsDecorationLayoutExtentDialog::updateGuiElements()
5259
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mSymbol.get(), mSymbolButton->iconSize() );
5360
mSymbolButton->setIcon( icon );
5461
}
62+
mTextFormat = mDeco.textFormat();
63+
mCheckBoxLabelExtents->setChecked( mDeco.labelExtents() );
5564
}
5665

5766
void QgsDecorationLayoutExtentDialog::updateDecoFromGui()
@@ -62,6 +71,8 @@ void QgsDecorationLayoutExtentDialog::updateDecoFromGui()
6271
{
6372
mDeco.setSymbol( mSymbol->clone() );
6473
}
74+
mDeco.setTextFormat( mTextFormat );
75+
mDeco.setLabelExtents( mCheckBoxLabelExtents->isChecked() );
6576
}
6677

6778
QgsDecorationLayoutExtentDialog::~QgsDecorationLayoutExtentDialog()
@@ -108,3 +119,10 @@ void QgsDecorationLayoutExtentDialog::changeSymbol()
108119
}
109120
}
110121
}
122+
123+
void QgsDecorationLayoutExtentDialog::changeFont()
124+
{
125+
QgsTextFormatDialog dlg( mTextFormat, QgisApp::instance()->mapCanvas(), this );
126+
if ( dlg.exec() )
127+
mTextFormat = dlg.format();
128+
}

‎src/app/qgsdecorationlayoutextentdialog.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ui_qgsdecorationlayoutextentdialog.h"
2121
#include <QDialog>
2222
#include "qgis_app.h"
23+
#include "qgstextrenderer.h"
2324
#include <memory>
2425

2526
class QgsDecorationLayoutExtent;
@@ -39,10 +40,12 @@ class APP_EXPORT QgsDecorationLayoutExtentDialog : public QDialog, private Ui::Q
3940
void on_buttonBox_rejected();
4041

4142
void changeSymbol();
43+
void changeFont();
4244

4345
private:
4446
QgsDecorationLayoutExtent &mDeco;
4547
std::unique_ptr< QgsFillSymbol > mSymbol;
48+
QgsTextFormat mTextFormat;
4649

4750
void updateGuiElements();
4851
void updateDecoFromGui();

‎src/ui/qgsdecorationlayoutextentdialog.ui‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>293</width>
10-
<height>124</height>
10+
<height>143</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -30,6 +30,20 @@
3030
</property>
3131
<layout class="QGridLayout" name="gridLayout">
3232
<item row="1" column="1">
33+
<widget class="QPushButton" name="mButtonFontStyle">
34+
<property name="text">
35+
<string>Font...</string>
36+
</property>
37+
</widget>
38+
</item>
39+
<item row="1" column="0">
40+
<widget class="QCheckBox" name="mCheckBoxLabelExtents">
41+
<property name="text">
42+
<string>Label extents</string>
43+
</property>
44+
</widget>
45+
</item>
46+
<item row="2" column="1">
3347
<spacer name="verticalSpacer">
3448
<property name="orientation">
3549
<enum>Qt::Vertical</enum>

0 commit comments

Comments
 (0)