Skip to content
Permalink
Browse files

Allow optional labels for composer extent decorations

  • Loading branch information
nyalldawson committed Jun 1, 2017
1 parent 09110ea commit babfb6bef961bceca358e44085b2ed12d1b4c66b
@@ -68,6 +68,15 @@ void QgsDecorationLayoutExtent::projectRead()
QgsSimpleLineSymbolLayer *layer = new QgsSimpleLineSymbolLayer( QColor( 0, 0, 0, 100 ), 0, Qt::DashLine );
mSymbol->changeSymbolLayer( 0, layer );
}

QString textXml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Font" ) );
if ( !textXml.isEmpty() )
{
doc.setContent( textXml );
elem = doc.documentElement();
mTextFormat.readXml( elem, rwContext );
}
mLabelExtents = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/Labels" ), true );
}

void QgsDecorationLayoutExtent::saveToProject()
@@ -85,6 +94,12 @@ void QgsDecorationLayoutExtent::saveToProject()
// FIXME this works, but XML will not be valid as < is replaced by &lt;
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Symbol" ), doc.toString() );
}

QDomDocument textDoc;
QDomElement textElem = mTextFormat.writeXml( textDoc, rwContext );
textDoc.appendChild( textElem );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Font" ), textDoc.toString() );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Labels" ), mLabelExtents );
}

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

if ( map->crs() !=
@@ -128,21 +144,39 @@ void QgsDecorationLayoutExtent::render( const QgsMapSettings &mapSettings, QgsRe
try
{
g.transform( ct );
labelPoint = ct.transform( labelPoint.x(), labelPoint.y() ).toQPointF();
}
catch ( QgsCsException & )
{
}
}

g.transform( transform );
labelPoint = transform.map( labelPoint );
extent = g.asQPolygonF();
mSymbol->renderPolygon( extent, nullptr, nullptr, context );

if ( mLabelExtents )
{
QgsTextRenderer::drawText( labelPoint, ( map->mapRotation() - mapSettings.rotation() ) * M_PI / 180.0, QgsTextRenderer::AlignRight, QStringList() << tr( "%1: %2" ).arg( composition->name(), map->displayName() ),
context, mTextFormat );
}
}
}
mSymbol->stopRender( context );
context.painter()->restore();
}

bool QgsDecorationLayoutExtent::labelExtents() const
{
return mLabelExtents;
}

void QgsDecorationLayoutExtent::setLabelExtents( bool labelExtents )
{
mLabelExtents = labelExtents;
}

QgsFillSymbol *QgsDecorationLayoutExtent::symbol() const
{
return mSymbol.get();
@@ -25,6 +25,7 @@
#include <QObject>
#include "qgis_app.h"
#include "qgssymbol.h"
#include "qgstextrenderer.h"
#include <memory>

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

/**
* Returns the text format for extent labels.
* \see setTextFormat()
* \see labelExtents()
*/
QgsTextFormat textFormat() const { return mTextFormat; }

/**
* Sets the text \a format for extent labels.
* \see textFormat()
* \see setLabelExtents()
*/
void setTextFormat( const QgsTextFormat &format ) { mTextFormat = format; }

/**
* Returns true if layout extents should be labeled with the name of the associated layout & map.
* \see setLabelExtents()
* \see textFormat()
*/
bool labelExtents() const;

/**
* Sets whether layout extents should be labeled with the name of the associated layout & map.
* \see labelExtents()
* \see setTextFormat()
*/
void setLabelExtents( bool labelExtents );

public slots:
void projectRead() override;
void saveToProject() override;
@@ -60,6 +89,8 @@ class APP_EXPORT QgsDecorationLayoutExtent : public QgsDecorationItem

private:
std::unique_ptr< QgsFillSymbol > mSymbol;
QgsTextFormat mTextFormat;
bool mLabelExtents = true;

friend class QgsDecorationLayoutExtentDialog;
};
@@ -27,6 +27,7 @@
#include "qgisapp.h"
#include "qgsguiutils.h"
#include "qgssettings.h"
#include "qgstextformatwidget.h"

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

connect( mCheckBoxLabelExtents, &QCheckBox::toggled, mButtonFontStyle, &QPushButton::setEnabled );
mCheckBoxLabelExtents->setChecked( false );
mButtonFontStyle->setEnabled( false );

updateGuiElements();
connect( buttonBox->button( QDialogButtonBox::Apply ), &QAbstractButton::clicked, this, &QgsDecorationLayoutExtentDialog::apply );
connect( mSymbolButton, &QPushButton::clicked, this, &QgsDecorationLayoutExtentDialog::changeSymbol );
connect( mButtonFontStyle, &QPushButton::clicked, this, &QgsDecorationLayoutExtentDialog::changeFont );

}

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

void QgsDecorationLayoutExtentDialog::updateDecoFromGui()
@@ -62,6 +71,8 @@ void QgsDecorationLayoutExtentDialog::updateDecoFromGui()
{
mDeco.setSymbol( mSymbol->clone() );
}
mDeco.setTextFormat( mTextFormat );
mDeco.setLabelExtents( mCheckBoxLabelExtents->isChecked() );
}

QgsDecorationLayoutExtentDialog::~QgsDecorationLayoutExtentDialog()
@@ -108,3 +119,10 @@ void QgsDecorationLayoutExtentDialog::changeSymbol()
}
}
}

void QgsDecorationLayoutExtentDialog::changeFont()
{
QgsTextFormatDialog dlg( mTextFormat, QgisApp::instance()->mapCanvas(), this );
if ( dlg.exec() )
mTextFormat = dlg.format();
}
@@ -20,6 +20,7 @@
#include "ui_qgsdecorationlayoutextentdialog.h"
#include <QDialog>
#include "qgis_app.h"
#include "qgstextrenderer.h"
#include <memory>

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

void changeSymbol();
void changeFont();

private:
QgsDecorationLayoutExtent &mDeco;
std::unique_ptr< QgsFillSymbol > mSymbol;
QgsTextFormat mTextFormat;

void updateGuiElements();
void updateDecoFromGui();
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>293</width>
<height>124</height>
<height>143</height>
</rect>
</property>
<property name="windowTitle">
@@ -30,6 +30,20 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QPushButton" name="mButtonFontStyle">
<property name="text">
<string>Font...</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="mCheckBoxLabelExtents">
<property name="text">
<string>Label extents</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>

0 comments on commit babfb6b

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