28 std::unique_ptr< QgsSimpleLineSymbolLayer > lineSymbolLayer = qgis::make_unique< QgsSimpleLineSymbolLayer >( QColor( 0, 0, 0 ), 0.2 );
29 mLineSymbol = qgis::make_unique< QgsLineSymbol >(
QgsSymbolLayerList() << lineSymbolLayer.release() );
35 : mType( other.mType )
36 , mTitleLabel( other.mTitleLabel )
37 , mSizeClasses( other.mSizeClasses )
38 , mSymbol( other.mSymbol.get() ? other.mSymbol->clone() : nullptr )
39 , mLineSymbol( other.mLineSymbol.get() ? other.mLineSymbol->clone() : nullptr )
40 , mSizeScaleTransformer( other.mSizeScaleTransformer.get() ? new
QgsSizeScaleTransformer( *other.mSizeScaleTransformer ) : nullptr )
41 , mVAlign( other.mVAlign )
42 , mFont( other.mFont )
43 , mTextColor( other.mTextColor )
44 , mTextAlignment( other.mTextAlignment )
53 mTitleLabel = other.mTitleLabel;
54 mSizeClasses = other.mSizeClasses;
55 mSymbol.reset( other.mSymbol.get() ? other.mSymbol->clone() :
nullptr );
56 mLineSymbol.reset( other.mLineSymbol.get() ? other.mLineSymbol->clone() :
nullptr );
57 mSizeScaleTransformer.reset( other.mSizeScaleTransformer.get() ?
new QgsSizeScaleTransformer( *other.mSizeScaleTransformer ) :
nullptr );
58 mVAlign = other.mVAlign;
60 mTextColor = other.mTextColor;
61 mTextAlignment = other.mTextAlignment;
78 mLineSymbol.reset(
symbol );
83 return mLineSymbol.get();
88 mSizeScaleTransformer.reset( transformer );
93 return mSizeScaleTransformer.get();
103 mSizeScaleTransformer.reset( sizeTransformer ? sizeTransformer->
clone() : nullptr );
105 if ( mTitleLabel.isEmpty() )
109 if ( sizeTransformer && mSizeClasses.isEmpty() )
111 mSizeClasses.clear();
113 for (
double v : prettyBreaks )
115 mSizeClasses <<
SizeClass( v, QString::number( v ) );
123 if ( !mTitleLabel.isEmpty() )
141 lst.reserve( mSizeClasses.size() );
142 for (
const SizeClass &cl : mSizeClasses )
146 double size = cl.
size;
147 if ( mSizeScaleTransformer )
149 size = mSizeScaleTransformer->size( size );
169 *outputSize = QSizeF();
176 double hLengthLineMM = 2;
177 double hSpaceLineTextMM = 1;
179 std::unique_ptr<QgsMarkerSymbol> s( mSymbol->clone() );
181 QList<SizeClass>
classes = mSizeClasses;
184 if ( mSizeScaleTransformer )
187 cls.size = mSizeScaleTransformer->size( cls.size );
195 int dpm = std::round( context.
scaleFactor() * 1000 );
198 QImage tmpImg( QSize( 1, 1 ), QImage::Format_ARGB32_Premultiplied );
199 tmpImg.setDotsPerMeterX( dpm );
200 tmpImg.setDotsPerMeterY( dpm );
201 QFontMetricsF fm( mFont, &tmpImg );
202 double textHeight = fm.height();
203 double leading = fm.leading();
204 double minTextDistY = textHeight + leading;
211 double maxTextWidth = 0;
214 maxTextWidth = std::max( maxTextWidth, fm.boundingRect(
c.label ).width() );
220 double largestSize =
classes.at( 0 ).size;
221 double outputLargestSize = context.
convertToPainterUnits( largestSize, s->sizeUnit(), s->sizeMapUnitScale() );
224 QList<double> symbolTopY;
231 symbolTopY << outputLargestSize / 2 - outputSymbolSize / 2;
234 symbolTopY << outputLargestSize - outputSymbolSize;
241 double middleIndex = 0;
242 QList<double> textCenterY;
243 double lastY = middleIndex < symbolTopY.size() ? symbolTopY[middleIndex] : 0;
244 textCenterY << lastY;
245 for (
int i = middleIndex + 1; i <
classes.count(); ++i )
247 double symbolY = symbolTopY[i];
248 if ( symbolY - lastY < minTextDistY )
249 symbolY = lastY + minTextDistY;
250 textCenterY << symbolY;
254 double textTopY = textCenterY.first() - textHeight / 2;
255 double textBottomY = textCenterY.last() + textHeight / 2;
256 double totalTextHeight = textBottomY - textTopY;
258 double fullWidth = outputLargestSize + hLengthLine + hSpaceLineText + maxTextWidth;
259 double fullHeight = std::max( outputLargestSize - textTopY, totalTextHeight );
262 *outputSize = QSizeF( fullWidth, fullHeight );
264 *labelXOffset = outputLargestSize + hLengthLine + hSpaceLineText;
273 QPainter *p = context.
painter();
275 p->translate( 0, -textTopY );
280 s->setSize(
c.size );
283 double tx = ( outputLargestSize - outputSymbolSize ) / 2;
289 p->translate( tx, ( outputLargestSize - outputSymbolSize ) / 2 );
292 p->translate( tx, outputLargestSize - outputSymbolSize );
295 s->drawPreviewIcon(
nullptr, QSize( outputSymbolSize, outputSymbolSize ), &context );
303 mLineSymbol->startRender( context );
312 mLineSymbol->renderPolyline( QPolygonF() << QPointF( outputLargestSize / 2, symbolTopY[i] )
313 << QPointF( outputLargestSize + hLengthLine, textCenterY[i] ),
nullptr, context );
317 QRect rect( outputLargestSize + hLengthLine + hSpaceLineText, textCenterY[i] - textHeight / 2,
318 maxTextWidth, textHeight );
321 QStringList() <<
c.label, context, format );
326 mLineSymbol->stopRender( context );
340 int dpm = std::round( context.
scaleFactor() * 1000 );
342 QImage img( contentSize.width() + padding * 2, contentSize.height() + padding * 2, QImage::Format_ARGB32_Premultiplied );
343 img.setDotsPerMeterX( dpm );
344 img.setDotsPerMeterY( dpm );
345 img.fill( backgroundColor );
347 QPainter painter( &img );
348 painter.setRenderHint( QPainter::Antialiasing,
true );
350 painter.translate( padding, padding );
353 QPainter *oldPainter = context.
painter();
369 ddsLegend->
setTitle( elem.attribute( QStringLiteral(
"title" ) ) );
371 QDomElement elemSymbol = elem.firstChildElement( QStringLiteral(
"symbol" ) );
372 if ( !elemSymbol.isNull() )
374 ddsLegend->
setSymbol( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( elemSymbol, context ) );
377 const QDomElement lineSymbolElem = elem.firstChildElement( QStringLiteral(
"lineSymbol" ) );
378 if ( !lineSymbolElem.isNull() )
380 ddsLegend->
setLineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( lineSymbolElem.firstChildElement(), context ) );
384 QDomElement elemTransformer = elem.firstChildElement( QStringLiteral(
"transformer" ) );
385 if ( !elemTransformer.isNull() )
392 QDomElement elemTextStyle = elem.firstChildElement( QStringLiteral(
"text-style" ) );
393 if ( !elemTextStyle.isNull() )
395 QDomElement elemFont = elemTextStyle.firstChildElement( QStringLiteral(
"font" ) );
396 if ( !elemFont.isNull() )
398 ddsLegend->
setFont( QFont( elemFont.attribute( QStringLiteral(
"family" ) ), elemFont.attribute( QStringLiteral(
"size" ) ).toInt(),
399 elemFont.attribute( QStringLiteral(
"weight" ) ).toInt(), elemFont.attribute( QStringLiteral(
"italic" ) ).toInt() ) );
402 ddsLegend->
setTextAlignment(
static_cast<Qt::AlignmentFlag
>( elemTextStyle.attribute( QStringLiteral(
"align" ) ).toInt() ) );
405 QDomElement elemClasses = elem.firstChildElement( QStringLiteral(
"classes" ) );
406 if ( !elemClasses.isNull() )
409 QDomElement elemClass = elemClasses.firstChildElement( QStringLiteral(
"class" ) );
410 while ( !elemClass.isNull() )
412 classes <<
SizeClass( elemClass.attribute( QStringLiteral(
"size" ) ).toDouble(), elemClass.attribute( QStringLiteral(
"label" ) ) );
413 elemClass = elemClass.nextSiblingElement();
423 QDomDocument doc = elem.ownerDocument();
425 elem.setAttribute( QStringLiteral(
"type" ), mType ==
LegendCollapsed ?
"collapsed" :
"separated" );
426 elem.setAttribute( QStringLiteral(
"valign" ), mVAlign ==
AlignCenter ?
"center" :
"bottom" );
427 elem.setAttribute( QStringLiteral(
"title" ), mTitleLabel );
432 elem.appendChild( elemSymbol );
437 QDomElement lineSymbolElem = doc.createElement( QStringLiteral(
"lineSymbol" ) );
439 elem.appendChild( lineSymbolElem );
442 if ( mSizeScaleTransformer )
445 elemTransformer.setTagName( QStringLiteral(
"transformer" ) );
446 elem.appendChild( elemTransformer );
449 QDomElement elemFont = doc.createElement( QStringLiteral(
"font" ) );
450 elemFont.setAttribute( QStringLiteral(
"family" ), mFont.family() );
451 elemFont.setAttribute( QStringLiteral(
"size" ), mFont.pointSize() );
452 elemFont.setAttribute( QStringLiteral(
"weight" ), mFont.weight() );
453 elemFont.setAttribute( QStringLiteral(
"italic" ), mFont.italic() );
455 QDomElement elemTextStyle = doc.createElement( QStringLiteral(
"text-style" ) );
457 elemTextStyle.setAttribute( QStringLiteral(
"align" ),
static_cast<int>( mTextAlignment ) );
458 elemTextStyle.appendChild( elemFont );
459 elem.appendChild( elemTextStyle );
461 if ( !mSizeClasses.isEmpty() )
463 QDomElement elemClasses = doc.createElement( QStringLiteral(
"classes" ) );
464 for (
const SizeClass &sc : qgis::as_const( mSizeClasses ) )
466 QDomElement elemClass = doc.createElement( QStringLiteral(
"class" ) );
467 elemClass.setAttribute( QStringLiteral(
"size" ), sc.size );
468 elemClass.setAttribute( QStringLiteral(
"label" ), sc.label );
469 elemClasses.appendChild( elemClass );
471 elem.appendChild( elemClasses );
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
void setTitle(const QString &title)
Sets title label for data-defined size legend.
QList< QgsDataDefinedSizeLegend::SizeClass > classes() const
Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setSymbol(QgsMarkerSymbol *symbol SIP_TRANSFER)
Sets marker symbol that will be used to draw markers in legend.
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
void setLineSymbol(QgsLineSymbol *symbol SIP_TRANSFER)
Sets the line symbol that will be used to draw callout lines in legend.
QgsMarkerSymbol * symbol() const
Returns marker symbol that will be used to draw markers in legend.
@ AlignCenter
Symbols are aligned to the center.
@ AlignBottom
Symbols are aligned to the bottom.
void setTextAlignment(Qt::AlignmentFlag flag)
Sets horizontal text alignment for rendering of labels - only valid for collapsed legend.
QgsDataDefinedSizeLegend & operator=(const QgsDataDefinedSizeLegend &other)
void setClasses(const QList< QgsDataDefinedSizeLegend::SizeClass > &classes)
Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setLegendType(LegendType type)
Sets how the legend should be rendered.
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
QString title() const
Returns title label for data-defined size legend.
void setSizeScaleTransformer(QgsSizeScaleTransformer *transformer SIP_TRANSFER)
Sets transformer for scaling of symbol sizes. Takes ownership of the object. Accepts nullptr to set n...
QgsDataDefinedSizeLegend()
Constructor for QgsDataDefinedSizeLegend.
static QgsDataDefinedSizeLegend * readXml(const QDomElement &elem, const QgsReadWriteContext &context) SIP_FACTORY
Creates instance from given element and returns it (caller takes ownership). Returns nullptr on error...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const
Writes configuration to the given XML element.
@ LegendSeparated
Each class (size value) has a separate legend node.
@ LegendCollapsed
All classes are rendered within one legend node.
~QgsDataDefinedSizeLegend()
QgsLineSymbol * lineSymbol() const
Returns the line symbol that will be used to draw callout lines in legend.
void updateFromSymbolAndProperty(const QgsMarkerSymbol *symbol, const QgsProperty &ddSize)
Updates the list of classes, source symbol and title label from given symbol and property.
QgsSizeScaleTransformer * sizeScaleTransformer() const
Returns transformer for scaling of symbol sizes. Returns nullptr if no transformer is defined.
QImage collapsedLegendImage(QgsRenderContext &context, const QColor &backgroundColor=Qt::transparent, double paddingMM=1) const
Returns output image that would be shown in the legend. Returns invalid image if legend is not config...
QgsLegendSymbolList legendSymbolList() const
Generates legend symbol items according to the configuration.
void drawCollapsedLegend(QgsRenderContext &context, QSizeF *outputSize SIP_OUT=nullptr, double *labelXOffset SIP_OUT=nullptr) const
Draw the legend if using LegendOneNodeForAll and optionally output size of the legend and x offset of...
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
void setDataDefinedSizeLegendSettings(QgsDataDefinedSizeLegend *settings)
Sets extra information about data-defined size.
QgsSymbol * symbol() const
Returns associated symbol. May be nullptr.
A line symbol type, for rendering LineString and MultiLineString geometries.
A marker symbol type, for rendering Point and MultiPoint geometries.
void setSize(double size)
Sets the size for the whole symbol.
double size() const
Returns the estimated size for the whole symbol, which is the maximum size of all marker symbol layer...
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
A store for object properties.
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
QString expressionString() const
Returns the expression used for the property value.
QString field() const
Returns the current field name the property references.
const QgsPropertyTransformer * transformer() const
Returns the existing transformer used for manipulating the calculated values for the property,...
Type propertyType() const
Returns the property type.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Scoped object for saving and restoring a QPainter object's state.
static QColor decodeColor(const QString &str)
static QList< double > prettyBreaks(double minimum, double maximum, int classes)
Computes a sequence of about 'classes' equally spaced round values which cover the range of values fr...
static QString encodeColor(const QColor &color)
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
static HAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a QgsTextRenderer::HAlignment value.
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop)
Draws text within a rectangle using the specified settings.
@ RenderMillimeters
Millimeters.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition of one class for the legend.