QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
33 QVariantMap properties;
34 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"white" ) );
35 properties.insert( QStringLiteral(
"style" ), QStringLiteral(
"solid" ) );
36 properties.insert( QStringLiteral(
"style_border" ), QStringLiteral(
"solid" ) );
37 properties.insert( QStringLiteral(
"color_border" ), QStringLiteral(
"black" ) );
38 properties.insert( QStringLiteral(
"width_border" ), QStringLiteral(
"0.3" ) );
39 properties.insert( QStringLiteral(
"joinstyle" ), QStringLiteral(
"miter" ) );
41 refreshSymbol(
false );
80 if ( !
id().isEmpty() )
88 return tr(
"<Ellipse>" );
90 return tr(
"<Rectangle>" );
92 return tr(
"<Triangle>" );
95 return tr(
"<Shape>" );
107 if (
type == mShape )
114 if (
mLayout &&
id().isEmpty() )
117 mLayout->itemsModel()->updateItemDisplayName(
this );
123 void QgsLayoutItemShape::refreshSymbol(
bool redraw )
125 if (
auto *lLayout =
layout() )
131 updateBoundingRect();
139 void QgsLayoutItemShape::updateBoundingRect()
141 QRectF rectangle = rect();
142 rectangle.adjust( -mMaxSymbolBleed, -mMaxSymbolBleed, mMaxSymbolBleed, mMaxSymbolBleed );
143 if ( rectangle != mCurrentRectangle )
145 prepareGeometryChange();
146 mCurrentRectangle = rectangle;
156 refreshSymbol(
true );
161 mCornerRadius = radius;
167 QPolygonF shapePolygon = mapToScene( calculatePolygon( 1.0 ) );
169 if ( shapePolygon.at( 0 ) != shapePolygon.constLast() )
170 shapePolygon << shapePolygon.at( 0 );
176 return mCurrentRectangle;
181 return mMaxSymbolBleed;
186 if ( mShapeStyleSymbol )
199 painter->setPen( Qt::NoPen );
200 painter->setBrush( Qt::NoBrush );
204 const QVector<QPolygonF> rings;
211 QPolygonF QgsLayoutItemShape::calculatePolygon(
double scale )
const
213 QPolygonF shapePolygon;
217 const QTransform t = QTransform::fromScale( 100, 100 );
219 const QTransform ti = t.inverted();
226 QPainterPath ellipsePath;
227 ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
228 const QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
229 shapePolygon = ti.map( ellipsePoly );
235 if ( mCornerRadius.
length() > 0 )
237 QPainterPath roundedRectPath;
238 const double radius =
mLayout->convertToLayoutUnits( mCornerRadius ) * scale;
239 roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ), radius, radius );
240 const QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
241 shapePolygon = ti.map( roundedPoly );
245 shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
251 shapePolygon << QPointF( 0, rect().height() * scale );
252 shapePolygon << QPointF( rect().width() * scale, rect().height() * scale );
253 shapePolygon << QPointF( rect().width() / 2.0 * scale, 0 );
254 shapePolygon << QPointF( 0, rect().height() * scale );
263 element.setAttribute( QStringLiteral(
"shapeType" ), mShape );
264 element.setAttribute( QStringLiteral(
"cornerRadiusMeasure" ), mCornerRadius.
encodeMeasurement() );
267 element.appendChild( shapeStyleElem );
274 mShape =
static_cast< Shape >( element.attribute( QStringLiteral(
"shapeType" ), QStringLiteral(
"0" ) ).toInt() );
275 if ( element.hasAttribute( QStringLiteral(
"cornerRadiusMeasure" ) ) )
278 mCornerRadius =
QgsLayoutMeasurement( element.attribute( QStringLiteral(
"cornerRadius" ), QStringLiteral(
"0" ) ).toDouble() );
280 const QDomElement shapeStyleSymbolElem = element.firstChildElement( QStringLiteral(
"symbol" ) );
281 if ( !shapeStyleSymbolElem.isNull() )
283 mShapeStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( shapeStyleSymbolElem, context ) );
284 refreshSymbol(
false );
QString id() const
Returns the item's ID name.
const QgsLayout * layout() const
Returns the layout the object is attached to.
Layout item for basic filled shapes (e.g. rectangles, ellipses).
void sizePositionChanged()
Emitted when the item's size or position changes.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
The class is used as a container of context for various read/write operations on other objects.
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
A symbol entity for QgsStyle databases.
double length() const
Returns the length of the measurement.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Contains settings and helpers relating to a render of a QgsLayoutItem.
Contains information about the context of a rendering operation.
QIcon icon() const override
Returns the item's icon.
void setCornerRadius(QgsLayoutMeasurement radius)
Sets the corner radius for rounded rectangle corners.
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
@ RenderMillimeters
Millimeters.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
QgsLayoutItemShape(QgsLayout *layout)
Constructor for QgsLayoutItemShape, with the specified parent layout.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
Contains information relating to the style entity currently being visited.
@ FlagProvidesClipPath
Item can act as a clipping path provider (see clipPath())
QString encodeMeasurement() const
Encodes the layout measurement to a string.
virtual void redraw()
Triggers a redraw (update) of the item.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
QString displayName() const override
Gets item display name.
static QgsLayoutItemShape * create(QgsLayout *layout)
Returns a new shape item for the specified layout.
void frameChanged()
Emitted if the item's frame style changes.
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Base class for graphical items within a QgsLayout.
double estimatedFrameBleed() const override
Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.
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.
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
@ Rectangle
Rectangle shape.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
int type() const override
virtual Flags itemFlags() const
Returns the item's flags, which indicate how the item behaves.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
virtual QString uuid() const
Returns the item identification string.
QPointer< QgsLayout > mLayout
A geometry is the spatial representation of a feature.
QRectF boundingRect() const override
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
~QgsLayoutItemShape() override
void setShapeType(QgsLayoutItemShape::Shape type)
Sets the type of shape (e.g.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
QgsFillSymbol * symbol()
Returns the fill symbol used to draw the shape.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
@ Triangle
Triangle shape.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsGeometry clipPath() const override
Returns the clipping path generated by this item, in layout coordinates.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
void setSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to draw the shape.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
static QgsLayoutMeasurement decodeMeasurement(const QString &string)
Decodes a measurement from a string.
void clipPathChanged()
Emitted when the item's clipping path has changed.