Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE][layouts] New item type for marker symbols
Allows marker symbols to be placed directly on layouts, eg. for manually placing markers over a map item or for creation of advanced custom legends
- Loading branch information
1 parent
b48a576
commit 632448c
Showing
19 changed files
with
846 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions
86
python/core/auto_generated/layout/qgslayoutitemmarker.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/layout/qgslayoutitemmarker.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsLayoutItemMarker : QgsLayoutItem | ||
{ | ||
%Docstring | ||
A layout item for showing marker symbols. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgslayoutitemmarker.h" | ||
%End | ||
public: | ||
|
||
explicit QgsLayoutItemMarker( QgsLayout *layout ); | ||
%Docstring | ||
Constructor for QgsLayoutItemMarker, with the specified parent ``layout``. | ||
%End | ||
~QgsLayoutItemMarker(); | ||
|
||
static QgsLayoutItemMarker *create( QgsLayout *layout ) /Factory/; | ||
%Docstring | ||
Returns a new marker item for the specified ``layout``. | ||
|
||
The caller takes responsibility for deleting the returned object. | ||
%End | ||
|
||
|
||
virtual int type() const; | ||
|
||
virtual QIcon icon() const; | ||
|
||
|
||
void setSymbol( QgsMarkerSymbol *symbol /Transfer/ ); | ||
%Docstring | ||
Sets the marker ``symbol`` used to draw the shape. Ownership is transferred. | ||
|
||
.. seealso:: :py:func:`symbol` | ||
%End | ||
|
||
QgsMarkerSymbol *symbol(); | ||
%Docstring | ||
Returns the marker symbol used to draw the shape. | ||
|
||
.. seealso:: :py:func:`setSymbol` | ||
%End | ||
|
||
virtual QRectF boundingRect() const; | ||
|
||
|
||
virtual QgsLayoutSize fixedSize() const; | ||
|
||
|
||
virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const; | ||
|
||
|
||
protected: | ||
|
||
virtual void draw( QgsLayoutItemRenderContext &context ); | ||
|
||
|
||
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const; | ||
|
||
virtual bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ); | ||
|
||
|
||
}; | ||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/layout/qgslayoutitemmarker.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/*************************************************************************** | ||
qgslayoutitemmarker.cpp | ||
--------------------- | ||
begin : April 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgslayoutitemmarker.h" | ||
#include "qgslayout.h" | ||
#include "qgslayoututils.h" | ||
#include "qgssymbollayerutils.h" | ||
#include "qgslayoutmodel.h" | ||
#include "qgsstyleentityvisitor.h" | ||
|
||
#include <QPainter> | ||
|
||
QgsLayoutItemMarker::QgsLayoutItemMarker( QgsLayout *layout ) | ||
: QgsLayoutItem( layout ) | ||
{ | ||
setBackgroundEnabled( false ); | ||
setFrameEnabled( false ); | ||
setReferencePoint( QgsLayoutItem::Middle ); | ||
QgsStringMap properties; | ||
properties.insert( QStringLiteral( "size" ), QStringLiteral( "4" ) ); | ||
mShapeStyleSymbol.reset( QgsMarkerSymbol::createSimple( properties ) ); | ||
refreshSymbol(); | ||
|
||
connect( this, &QgsLayoutItemMarker::sizePositionChanged, this, [ = ] | ||
{ | ||
updateBoundingRect(); | ||
update(); | ||
} ); | ||
} | ||
|
||
QgsLayoutItemMarker::~QgsLayoutItemMarker() = default; | ||
|
||
QgsLayoutItemMarker *QgsLayoutItemMarker::create( QgsLayout *layout ) | ||
{ | ||
return new QgsLayoutItemMarker( layout ); | ||
} | ||
|
||
int QgsLayoutItemMarker::type() const | ||
{ | ||
return QgsLayoutItemRegistry::LayoutMarker; | ||
} | ||
|
||
QIcon QgsLayoutItemMarker::icon() const | ||
{ | ||
return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemMarker.svg" ) ); | ||
} | ||
|
||
void QgsLayoutItemMarker::refreshSymbol() | ||
{ | ||
if ( layout() ) | ||
{ | ||
QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( layout(), nullptr, layout()->renderContext().dpi() ); | ||
|
||
mShapeStyleSymbol->startRender( rc ); | ||
QRectF bounds = mShapeStyleSymbol->bounds( QPointF( 0, 0 ), rc ); | ||
mShapeStyleSymbol->stopRender( rc ); | ||
mPoint = QPointF( -bounds.left() * 25.4 / layout()->renderContext().dpi(), | ||
-bounds.top() * 25.4 / layout()->renderContext().dpi() ); | ||
bounds.translate( mPoint ); | ||
|
||
QgsLayoutSize newSizeMm = QgsLayoutSize( bounds.size() * 25.4 / layout()->renderContext().dpi(), QgsUnitTypes::LayoutMillimeters ); | ||
mFixedSize = mLayout->renderContext().measurementConverter().convert( newSizeMm, sizeWithUnits().units() ); | ||
|
||
attemptResize( mFixedSize ); | ||
} | ||
|
||
updateBoundingRect(); | ||
|
||
update(); | ||
emit frameChanged(); | ||
} | ||
|
||
void QgsLayoutItemMarker::updateBoundingRect() | ||
{ | ||
QRectF rectangle = rect(); | ||
|
||
// add a bit, to account for antialiasing on stroke and miter effects on stroke | ||
rectangle.adjust( -5, -5, 5, 5 ); | ||
if ( rectangle != mCurrentRectangle ) | ||
{ | ||
prepareGeometryChange(); | ||
mCurrentRectangle = rectangle; | ||
} | ||
} | ||
|
||
void QgsLayoutItemMarker::setSymbol( QgsMarkerSymbol *symbol ) | ||
{ | ||
if ( !symbol ) | ||
return; | ||
|
||
mShapeStyleSymbol.reset( symbol ); | ||
refreshSymbol(); | ||
} | ||
|
||
QgsMarkerSymbol *QgsLayoutItemMarker::symbol() | ||
{ | ||
return mShapeStyleSymbol.get(); | ||
} | ||
|
||
QRectF QgsLayoutItemMarker::boundingRect() const | ||
{ | ||
return mCurrentRectangle; | ||
} | ||
|
||
QgsLayoutSize QgsLayoutItemMarker::fixedSize() const | ||
{ | ||
return mFixedSize; | ||
} | ||
|
||
bool QgsLayoutItemMarker::accept( QgsStyleEntityVisitorInterface *visitor ) const | ||
{ | ||
if ( mShapeStyleSymbol ) | ||
{ | ||
QgsStyleSymbolEntity entity( mShapeStyleSymbol.get() ); | ||
if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) ) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void QgsLayoutItemMarker::draw( QgsLayoutItemRenderContext &context ) | ||
{ | ||
QPainter *painter = context.renderContext().painter(); | ||
painter->setPen( Qt::NoPen ); | ||
painter->setBrush( Qt::NoBrush ); | ||
|
||
double scale = context.renderContext().convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); | ||
|
||
QPointF shapePoint = mPoint * scale; | ||
|
||
symbol()->startRender( context.renderContext() ); | ||
symbol()->renderPoint( shapePoint, nullptr, context.renderContext() ); | ||
symbol()->stopRender( context.renderContext() ); | ||
} | ||
|
||
bool QgsLayoutItemMarker::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const | ||
{ | ||
QDomElement shapeStyleElem = QgsSymbolLayerUtils::saveSymbol( QString(), mShapeStyleSymbol.get(), document, context ); | ||
element.appendChild( shapeStyleElem ); | ||
|
||
return true; | ||
} | ||
|
||
bool QgsLayoutItemMarker::readPropertiesFromElement( const QDomElement &element, const QDomDocument &, const QgsReadWriteContext &context ) | ||
{ | ||
QDomElement shapeStyleSymbolElem = element.firstChildElement( QStringLiteral( "symbol" ) ); | ||
if ( !shapeStyleSymbolElem.isNull() ) | ||
{ | ||
mShapeStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( shapeStyleSymbolElem, context ) ); | ||
refreshSymbol(); | ||
} | ||
|
||
return true; | ||
} |
Oops, something went wrong.