QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgslayoutitemshape.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemshape.cpp
3 -----------------------
4 begin : July 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgslayoutitemshape.h"
18
19#include "qgsfillsymbol.h"
20#include "qgslayout.h"
21#include "qgslayoutmodel.h"
23#include "qgslayoututils.h"
25#include "qgssymbollayerutils.h"
26
27#include <QPainter>
28#include <QString>
29
30#include "moc_qgslayoutitemshape.cpp"
31
32using namespace Qt::StringLiterals;
33
36 , mCornerRadius( 0 )
37{
38 setBackgroundEnabled( false );
39 setFrameEnabled( false );
40 QVariantMap properties;
41 properties.insert( u"color"_s, u"white"_s );
42 properties.insert( u"style"_s, u"solid"_s );
43 properties.insert( u"style_border"_s, u"solid"_s );
44 properties.insert( u"color_border"_s, u"black"_s );
45 properties.insert( u"width_border"_s, u"0.3"_s );
46 properties.insert( u"joinstyle"_s, u"miter"_s );
47 mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
48 refreshSymbol( false );
49
50 connect( this, &QgsLayoutItemShape::sizePositionChanged, this, [this] {
51 updateBoundingRect();
52 update();
53 emit clipPathChanged();
54 } );
55}
56
58
63
68
70{
71 switch ( mShape )
72 {
73 case Ellipse:
74 return QgsApplication::getThemeIcon( u"/mLayoutItemShapeEllipse.svg"_s );
75 case Rectangle:
76 return QgsApplication::getThemeIcon( u"/mLayoutItemShapeRectangle.svg"_s );
77 case Triangle:
78 return QgsApplication::getThemeIcon( u"/mLayoutItemShapeTriangle.svg"_s );
79 }
80
81 return QIcon();
82}
83
85{
86 if ( !id().isEmpty() )
87 {
88 return id();
89 }
90
91 switch ( mShape )
92 {
93 case Ellipse:
94 return tr( "<Ellipse>" );
95 case Rectangle:
96 return tr( "<Rectangle>" );
97 case Triangle:
98 return tr( "<Triangle>" );
99 }
100
101 return tr( "<Shape>" );
102}
103
110
112{
113 if ( type == mShape )
114 {
115 return;
116 }
117
118 mShape = type;
119
120 if ( mLayout && id().isEmpty() )
121 {
122 //notify the model that the display name has changed
123 mLayout->itemsModel()->updateItemDisplayName( this );
124 }
125
126 emit clipPathChanged();
127}
128
129void QgsLayoutItemShape::refreshSymbol( bool redraw )
130{
131 if ( auto *lLayout = layout() )
132 {
133 const QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( lLayout, nullptr, lLayout->renderContext().dpi() );
134 mMaxSymbolBleed = ( 25.4 / lLayout->renderContext().dpi() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mShapeStyleSymbol.get(), rc );
135 }
136
137 updateBoundingRect();
138
139 if ( redraw )
140 update();
141
142 emit frameChanged();
143}
144
145void QgsLayoutItemShape::updateBoundingRect()
146{
147 QRectF rectangle = rect();
148 rectangle.adjust( -mMaxSymbolBleed, -mMaxSymbolBleed, mMaxSymbolBleed, mMaxSymbolBleed );
149 if ( rectangle != mCurrentRectangle )
150 {
151 prepareGeometryChange();
152 mCurrentRectangle = rectangle;
153 }
154}
155
157{
158 if ( !symbol )
159 return;
160
161 mShapeStyleSymbol.reset( symbol->clone() );
162 refreshSymbol( true );
163}
164
166{
167 mCornerRadius = radius;
168 emit clipPathChanged();
169}
170
172{
173 QPolygonF shapePolygon = mapToScene( calculatePolygon( 1.0 ) );
174 // ensure polygon is closed
175 if ( shapePolygon.at( 0 ) != shapePolygon.constLast() )
176 shapePolygon << shapePolygon.at( 0 );
177 return QgsGeometry::fromQPolygonF( shapePolygon );
178}
179
181{
182 return mCurrentRectangle;
183}
184
186{
187 return mMaxSymbolBleed;
188}
189
191{
192 if ( mShapeStyleSymbol )
193 {
194 QgsStyleSymbolEntity entity( mShapeStyleSymbol.get() );
195 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
196 return false;
197 }
198
199 return true;
200}
201
203{
204 QgsRenderContext renderContext = context.renderContext();
205 // symbol clipping messes with geometry generators used in the symbol for this item, and has no
206 // valid use here. See https://github.com/qgis/QGIS/issues/58909
208
209 QPainter *painter = renderContext.painter();
210 painter->setPen( Qt::NoPen );
211 painter->setBrush( Qt::NoBrush );
212
213 const double scale = renderContext.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
214
215 const QVector<QPolygonF> rings; //empty list
216
217 symbol()->startRender( renderContext );
218 symbol()->renderPolygon( calculatePolygon( scale ), &rings, nullptr, renderContext );
219 symbol()->stopRender( renderContext );
220}
221
222QPolygonF QgsLayoutItemShape::calculatePolygon( double scale ) const
223{
224 QPolygonF shapePolygon;
225
226 //shapes with curves must be enlarged before conversion to QPolygonF, or
227 //the curves are approximated too much and appear jaggy
228 const QTransform t = QTransform::fromScale( 100, 100 );
229 //inverse transform used to scale created polygons back to expected size
230 const QTransform ti = t.inverted();
231
232 switch ( mShape )
233 {
234 case Ellipse:
235 {
236 //create an ellipse
237 QPainterPath ellipsePath;
238 ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
239 const QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
240 shapePolygon = ti.map( ellipsePoly );
241 break;
242 }
243 case Rectangle:
244 {
245 //if corner radius set, then draw a rounded rectangle
246 if ( mCornerRadius.length() > 0 )
247 {
248 QPainterPath roundedRectPath;
249 const double radius = mLayout->convertToLayoutUnits( mCornerRadius ) * scale;
250 roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ), radius, radius );
251 const QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
252 shapePolygon = ti.map( roundedPoly );
253 }
254 else
255 {
256 shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
257 }
258 break;
259 }
260 case Triangle:
261 {
262 shapePolygon << QPointF( 0, rect().height() * scale );
263 shapePolygon << QPointF( rect().width() * scale, rect().height() * scale );
264 shapePolygon << QPointF( rect().width() / 2.0 * scale, 0 );
265 shapePolygon << QPointF( 0, rect().height() * scale );
266 break;
267 }
268 }
269 return shapePolygon;
270}
271
272bool QgsLayoutItemShape::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
273{
274 element.setAttribute( u"shapeType"_s, mShape );
275 element.setAttribute( u"cornerRadiusMeasure"_s, mCornerRadius.encodeMeasurement() );
276
277 const QDomElement shapeStyleElem = QgsSymbolLayerUtils::saveSymbol( QString(), mShapeStyleSymbol.get(), document, context );
278 element.appendChild( shapeStyleElem );
279
280 return true;
281}
282
283bool QgsLayoutItemShape::readPropertiesFromElement( const QDomElement &element, const QDomDocument &, const QgsReadWriteContext &context )
284{
285 mShape = static_cast< Shape >( element.attribute( u"shapeType"_s, u"0"_s ).toInt() );
286 if ( element.hasAttribute( u"cornerRadiusMeasure"_s ) )
287 mCornerRadius = QgsLayoutMeasurement::decodeMeasurement( element.attribute( u"cornerRadiusMeasure"_s, u"0"_s ) );
288 else
289 mCornerRadius = QgsLayoutMeasurement( element.attribute( u"cornerRadius"_s, u"0"_s ).toDouble() );
290
291 const QDomElement shapeStyleSymbolElem = element.firstChildElement( u"symbol"_s );
292 if ( !shapeStyleSymbolElem.isNull() )
293 {
294 mShapeStyleSymbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( shapeStyleSymbolElem, context );
295 refreshSymbol( false );
296 }
297
298 return true;
299}
@ Millimeters
Millimeters.
Definition qgis.h:5341
@ DisableSymbolClippingToExtent
Force symbol clipping to map extent to be disabled in all situations. This will result in slower rend...
Definition qgis.h:2867
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
void renderPolygon(const QPolygonF &points, const QVector< QPolygonF > *rings, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Renders the symbol using the given render context.
A geometry is the spatial representation of a feature.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
QRectF boundingRect() const override
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
static QgsLayoutItemShape * create(QgsLayout *layout)
Returns a new shape item for the specified layout.
double estimatedFrameBleed() const override
Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.
QgsFillSymbol * symbol()
Returns the fill symbol used to draw the shape.
~QgsLayoutItemShape() override
void setShapeType(QgsLayoutItemShape::Shape type)
Sets the type of shape (e.g.
QString displayName() const override
Gets item display name.
QgsLayoutItemShape(QgsLayout *layout)
Constructor for QgsLayoutItemShape, with the specified parent layout.
QgsGeometry clipPath() const override
Returns the clipping path generated by this item, in layout coordinates.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
void setSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to draw the shape.
QIcon icon() const override
Returns the item's icon.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
int type() const override
@ Ellipse
Ellipse shape.
@ Rectangle
Rectangle shape.
@ Triangle
Triangle shape.
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
void setCornerRadius(QgsLayoutMeasurement radius)
Sets the corner radius for rounded rectangle corners.
friend class QgsLayout
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
virtual void redraw()
Triggers a redraw (update) of the item.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
@ FlagProvidesClipPath
Item can act as a clipping path provider (see clipPath()).
void sizePositionChanged()
Emitted when the item's size or position changes.
virtual QString uuid() const
Returns the item identification string.
QString id() const
Returns the item's ID name.
void frameChanged()
Emitted if the item's frame style changes.
virtual Flags itemFlags() const
Returns the item's flags, which indicate how the item behaves.
void clipPathChanged()
Emitted when the item's clipping path has changed.
QFlags< Flag > Flags
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
static QgsLayoutMeasurement decodeMeasurement(const QString &string)
Decodes a measurement from a string.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
Definition qgsstyle.h:1393
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Contains information relating to the style entity currently being visited.