Skip to content

Commit

Permalink
Allow optional labels for composer extent decorations
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 1, 2017
1 parent 09110ea commit babfb6b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/app/qgsdecorationlayoutextent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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() !=
Expand All @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions src/app/qgsdecorationlayoutextent.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QObject>
#include "qgis_app.h"
#include "qgssymbol.h"
#include "qgstextrenderer.h"
#include <memory>

class QgsDecorationLayoutExtentDialog;
Expand Down Expand Up @@ -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;
Expand All @@ -60,6 +89,8 @@ class APP_EXPORT QgsDecorationLayoutExtent : public QgsDecorationItem

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

friend class QgsDecorationLayoutExtentDialog;
};
Expand Down
18 changes: 18 additions & 0 deletions src/app/qgsdecorationlayoutextentdialog.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "qgisapp.h"
#include "qgsguiutils.h"
#include "qgssettings.h"
#include "qgstextformatwidget.h"

QgsDecorationLayoutExtentDialog::QgsDecorationLayoutExtentDialog( QgsDecorationLayoutExtent &deco, QWidget *parent )
: QDialog( parent )
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -62,6 +71,8 @@ void QgsDecorationLayoutExtentDialog::updateDecoFromGui()
{
mDeco.setSymbol( mSymbol->clone() );
}
mDeco.setTextFormat( mTextFormat );
mDeco.setLabelExtents( mCheckBoxLabelExtents->isChecked() );
}

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

void QgsDecorationLayoutExtentDialog::changeFont()
{
QgsTextFormatDialog dlg( mTextFormat, QgisApp::instance()->mapCanvas(), this );
if ( dlg.exec() )
mTextFormat = dlg.format();
}
3 changes: 3 additions & 0 deletions src/app/qgsdecorationlayoutextentdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ui_qgsdecorationlayoutextentdialog.h"
#include <QDialog>
#include "qgis_app.h"
#include "qgstextrenderer.h"
#include <memory>

class QgsDecorationLayoutExtent;
Expand All @@ -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();
Expand Down
16 changes: 15 additions & 1 deletion src/ui/qgsdecorationlayoutextentdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>293</width>
<height>124</height>
<height>143</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -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>
Expand Down

0 comments on commit babfb6b

Please sign in to comment.