Skip to content

Commit

Permalink
[FEATURE][layouts] New item type for marker symbols
Browse files Browse the repository at this point in the history
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
nyalldawson committed Apr 5, 2020
1 parent b48a576 commit 632448c
Show file tree
Hide file tree
Showing 19 changed files with 846 additions and 0 deletions.
2 changes: 2 additions & 0 deletions images/images.qrc
Expand Up @@ -838,6 +838,8 @@
<file>themes/default/temporal_navigation/pause.svg</file>
<file>themes/default/mIconIterate.svg</file>
<file>themes/default/mIconNetworkLogger.svg</file>
<file>themes/default/mActionAddMarker.svg</file>
<file>themes/default/mLayoutItemMarker.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
1 change: 1 addition & 0 deletions images/themes/default/mActionAddMarker.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/themes/default/mLayoutItemMarker.svg
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 python/core/auto_generated/layout/qgslayoutitemmarker.sip.in
@@ -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 *
************************************************************************/
Expand Up @@ -192,6 +192,7 @@ of layout items.
Layout3DMap,

LayoutManualTable,
LayoutMarker,

// item types provided by plugins
PluginItem,
Expand Down
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -364,6 +364,7 @@
%Include auto_generated/layout/qgslayoutitemmapgrid.sip
%Include auto_generated/layout/qgslayoutitemmapitem.sip
%Include auto_generated/layout/qgslayoutitemmapoverview.sip
%Include auto_generated/layout/qgslayoutitemmarker.sip
%Include auto_generated/layout/qgslayoutitemnodeitem.sip
%Include auto_generated/layout/qgslayoutitempage.sip
%Include auto_generated/layout/qgslayoutitempicture.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -467,6 +467,7 @@ SET(QGIS_CORE_SRCS
layout/qgslayoutitemmapgrid.cpp
layout/qgslayoutitemmapitem.cpp
layout/qgslayoutitemmapoverview.cpp
layout/qgslayoutitemmarker.cpp
layout/qgslayoutitemnodeitem.cpp
layout/qgslayoutitempage.cpp
layout/qgslayoutitempicture.cpp
Expand Down Expand Up @@ -1135,6 +1136,7 @@ SET(QGIS_CORE_HDRS
layout/qgslayoutitemmapgrid.h
layout/qgslayoutitemmapitem.h
layout/qgslayoutitemmapoverview.h
layout/qgslayoutitemmarker.h
layout/qgslayoutitemnodeitem.h
layout/qgslayoutitempage.h
layout/qgslayoutitempicture.h
Expand Down
168 changes: 168 additions & 0 deletions src/core/layout/qgslayoutitemmarker.cpp
@@ -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;
}

0 comments on commit 632448c

Please sign in to comment.