33using namespace Qt::StringLiterals;
37 auto lineSymbolLayer = std::make_unique< QgsSimpleLineSymbolLayer >( QColor( 0, 0, 0 ), 0.2 );
38 mLineSymbol = std::make_unique< QgsLineSymbol >(
QgsSymbolLayerList() << lineSymbolLayer.release() );
44 : mType( other.mType )
45 , mTitleLabel( other.mTitleLabel )
46 , mSizeClasses( other.mSizeClasses )
47 , mSymbol( other.mSymbol.get() ? other.mSymbol->clone() : nullptr )
48 , mLineSymbol( other.mLineSymbol.get() ? other.mLineSymbol->clone() : nullptr )
49 , mSizeScaleTransformer( other.mSizeScaleTransformer.get() ? new
QgsSizeScaleTransformer( *other.mSizeScaleTransformer ) : nullptr )
50 , mVAlign( other.mVAlign )
51 , mFont( other.mFont )
52 , mTextColor( other.mTextColor )
53 , mTextAlignment( other.mTextAlignment )
61 mTitleLabel = other.mTitleLabel;
62 mSizeClasses = other.mSizeClasses;
63 mSymbol.reset( other.mSymbol.get() ? other.mSymbol->clone() :
nullptr );
64 mLineSymbol.reset( other.mLineSymbol.get() ? other.mLineSymbol->clone() :
nullptr );
65 mSizeScaleTransformer.reset( other.mSizeScaleTransformer.get() ?
new QgsSizeScaleTransformer( *other.mSizeScaleTransformer ) :
nullptr );
66 mVAlign = other.mVAlign;
68 mTextColor = other.mTextColor;
69 mTextAlignment = other.mTextAlignment;
86 mLineSymbol.reset(
symbol );
91 return mLineSymbol.get();
96 mSizeScaleTransformer.reset( transformer );
101 return mSizeScaleTransformer.get();
107 mSymbol.reset(
symbol->clone() );
111 mSizeScaleTransformer.reset( sizeTransformer ? sizeTransformer->
clone() :
nullptr );
113 if ( mTitleLabel.isEmpty() )
117 if ( sizeTransformer && mSizeClasses.isEmpty() )
119 mSizeClasses.clear();
121 for (
double v : prettyBreaks )
123 mSizeClasses <<
SizeClass( v, QString::number( v ) );
131 QVariant isDataDefinedSize(
true );
132 if ( !mTitleLabel.isEmpty() )
152 lst.reserve( mSizeClasses.size() );
153 for (
const SizeClass &cl : mSizeClasses )
158 double size = cl.
size;
159 if ( mSizeScaleTransformer )
161 size = mSizeScaleTransformer->size( size );
181 *outputSize = QSizeF();
188 double hLengthLineMM = 2;
189 double hSpaceLineTextMM = 1;
191 std::unique_ptr<QgsMarkerSymbol> s( mSymbol->clone() );
193 QList<SizeClass>
classes = mSizeClasses;
196 if ( mSizeScaleTransformer )
199 cls.size = mSizeScaleTransformer->size( cls.size );
207 int dpm = std::round( context.
scaleFactor() * 1000 );
210 QImage tmpImg( QSize( 1, 1 ), QImage::Format_ARGB32_Premultiplied );
211 tmpImg.setDotsPerMeterX( dpm );
212 tmpImg.setDotsPerMeterY( dpm );
213 QFontMetricsF fm( mFont, &tmpImg );
214 double textHeight = fm.height();
215 double leading = fm.leading();
216 double minTextDistY = textHeight + leading;
223 double maxTextWidth = 0;
226 maxTextWidth = std::max( maxTextWidth, fm.boundingRect(
c.label ).width() );
232 double largestSize =
classes.at( 0 ).size;
233 double outputLargestSize = context.
convertToPainterUnits( largestSize, s->sizeUnit(), s->sizeMapUnitScale() );
236 QList<double> symbolTopY;
243 symbolTopY << outputLargestSize / 2 - outputSymbolSize / 2;
246 symbolTopY << outputLargestSize - outputSymbolSize;
253 double middleIndex = 0;
254 QList<double> textCenterY;
255 double lastY = middleIndex < symbolTopY.size() ? symbolTopY[middleIndex] : 0;
256 textCenterY << lastY;
257 for (
int i = middleIndex + 1; i <
classes.count(); ++i )
259 double symbolY = symbolTopY[i];
260 if ( symbolY - lastY < minTextDistY )
261 symbolY = lastY + minTextDistY;
262 textCenterY << symbolY;
266 double textTopY = textCenterY.first() - textHeight / 2;
267 double textBottomY = textCenterY.last() + textHeight / 2;
268 double totalTextHeight = textBottomY - textTopY;
270 double fullWidth = outputLargestSize + hLengthLine + hSpaceLineText + maxTextWidth;
271 double fullHeight = std::max( outputLargestSize - textTopY, totalTextHeight );
274 *outputSize = QSizeF( fullWidth, fullHeight );
276 *labelXOffset = outputLargestSize + hLengthLine + hSpaceLineText;
285 QPainter *p = context.
painter();
287 p->translate( 0, -textTopY );
292 s->setSize(
c.size );
295 double tx = ( outputLargestSize - outputSymbolSize ) / 2;
301 p->translate( tx, ( outputLargestSize - outputSymbolSize ) / 2 );
304 p->translate( tx, outputLargestSize - outputSymbolSize );
307 s->drawPreviewIcon(
nullptr, QSize( outputSymbolSize, outputSymbolSize ), &context );
315 mLineSymbol->startRender( context );
324 mLineSymbol->renderPolyline( QPolygonF() << QPointF( outputLargestSize / 2, symbolTopY[i] ) << QPointF( outputLargestSize + hLengthLine, textCenterY[i] ),
nullptr, context );
328 QRect rect( outputLargestSize + hLengthLine + hSpaceLineText, textCenterY[i] - textHeight / 2, maxTextWidth, textHeight );
335 mLineSymbol->stopRender( context );
349 int dpm = std::round( context.
scaleFactor() * 1000 );
351 QImage img( contentSize.width() + padding * 2, contentSize.height() + padding * 2, QImage::Format_ARGB32_Premultiplied );
352 img.setDotsPerMeterX( dpm );
353 img.setDotsPerMeterY( dpm );
354 img.fill( backgroundColor );
356 QPainter painter( &img );
357 painter.setRenderHint( QPainter::Antialiasing,
true );
359 painter.translate( padding, padding );
362 QPainter *oldPainter = context.
painter();
378 ddsLegend->
setTitle( elem.attribute( u
"title"_s ) );
380 QDomElement elemSymbol = elem.firstChildElement( u
"symbol"_s );
381 if ( !elemSymbol.isNull() )
386 const QDomElement lineSymbolElem = elem.firstChildElement( u
"lineSymbol"_s );
387 if ( !lineSymbolElem.isNull() )
393 QDomElement elemTransformer = elem.firstChildElement( u
"transformer"_s );
394 if ( !elemTransformer.isNull() )
401 QDomElement elemTextStyle = elem.firstChildElement( u
"text-style"_s );
402 if ( !elemTextStyle.isNull() )
404 QDomElement elemFont = elemTextStyle.firstChildElement( u
"font"_s );
405 if ( !elemFont.isNull() )
408 QgsFontUtils::createFont( elemFont.attribute( u
"family"_s ), elemFont.attribute( u
"size"_s ).toInt(), elemFont.attribute( u
"weight"_s ).toInt(), elemFont.attribute( u
"italic"_s ).toInt() )
412 ddsLegend->
setTextAlignment(
static_cast<Qt::AlignmentFlag
>( elemTextStyle.attribute( u
"align"_s ).toInt() ) );
415 QDomElement elemClasses = elem.firstChildElement( u
"classes"_s );
416 if ( !elemClasses.isNull() )
419 QDomElement elemClass = elemClasses.firstChildElement( u
"class"_s );
420 while ( !elemClass.isNull() )
422 classes <<
SizeClass( elemClass.attribute( u
"size"_s ).toDouble(), elemClass.attribute( u
"label"_s ) );
423 elemClass = elemClass.nextSiblingElement();
433 QDomDocument doc = elem.ownerDocument();
435 elem.setAttribute( u
"type"_s, mType ==
LegendCollapsed ?
"collapsed" :
"separated" );
436 elem.setAttribute( u
"valign"_s, mVAlign ==
AlignCenter ?
"center" :
"bottom" );
437 elem.setAttribute( u
"title"_s, mTitleLabel );
442 elem.appendChild( elemSymbol );
447 QDomElement lineSymbolElem = doc.createElement( u
"lineSymbol"_s );
449 elem.appendChild( lineSymbolElem );
452 if ( mSizeScaleTransformer )
455 elemTransformer.setTagName( u
"transformer"_s );
456 elem.appendChild( elemTransformer );
459 QDomElement elemFont = doc.createElement( u
"font"_s );
460 elemFont.setAttribute( u
"family"_s, mFont.family() );
461 elemFont.setAttribute( u
"size"_s, mFont.pointSize() );
462 elemFont.setAttribute( u
"weight"_s, mFont.weight() );
463 elemFont.setAttribute( u
"italic"_s, mFont.italic() );
465 QDomElement elemTextStyle = doc.createElement( u
"text-style"_s );
467 elemTextStyle.setAttribute( u
"align"_s,
static_cast<int>( mTextAlignment ) );
468 elemTextStyle.appendChild( elemFont );
469 elem.appendChild( elemTextStyle );
471 if ( !mSizeClasses.isEmpty() )
473 QDomElement elemClasses = doc.createElement( u
"classes"_s );
474 for (
const SizeClass &sc : std::as_const( mSizeClasses ) )
476 QDomElement elemClass = doc.createElement( u
"class"_s );
477 elemClass.setAttribute( u
"size"_s, sc.size );
478 elemClass.setAttribute( u
"label"_s, sc.label );
479 elemClasses.appendChild( elemClass );
481 elem.appendChild( elemClasses );
@ Expression
Expression based property.
@ Millimeters
Millimeters.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
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...
static QFont createFont(const QString &family, int pointSize=-1, int weight=-1, bool italic=false)
Creates a font with the specified family.
@ IsDataDefinedSize
Set when a node is related to data defined size (title or separated legend items)....
Stores information about one class/rule of a vector layer renderer in a unified way that can be used ...
void setUserData(int key, QVariant &value)
Adds a key - value pair to the item's user data.
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) const
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...
A store for object properties.
QString expressionString() const
Returns the expression used for the property value.
Qgis::PropertyType propertyType() const
Returns the property type.
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,...
A container for the context for various read/write operations on 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.
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 setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Scoped object for saving and restoring a QPainter object's state.
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 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.
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 void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static Qgis::TextHorizontalAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a Qgis::TextHorizontalAlignment value.
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.