29 : mType( other.mType )
30 , mTitleLabel( other.mTitleLabel )
31 , mSizeClasses( other.mSizeClasses )
32 , mSymbol( other.mSymbol.get() ? other.mSymbol->clone() : nullptr )
33 , mSizeScaleTransformer( other.mSizeScaleTransformer.get() ? new
QgsSizeScaleTransformer( *other.mSizeScaleTransformer ) : nullptr )
34 , mVAlign( other.mVAlign )
35 , mFont( other.mFont )
36 , mTextColor( other.mTextColor )
37 , mTextAlignment( other.mTextAlignment )
46 mTitleLabel = other.mTitleLabel;
47 mSizeClasses = other.mSizeClasses;
48 mSymbol.reset( other.mSymbol.get() ? other.mSymbol->clone() : nullptr );
49 mSizeScaleTransformer.reset( other.mSizeScaleTransformer.get() ?
new QgsSizeScaleTransformer( *other.mSizeScaleTransformer ) : nullptr );
50 mVAlign = other.mVAlign;
52 mTextColor = other.mTextColor;
53 mTextAlignment = other.mTextAlignment;
60 mSymbol.reset( symbol );
70 mSizeScaleTransformer.reset( transformer );
75 return mSizeScaleTransformer.get();
81 mSymbol.reset( symbol->
clone() );
85 mSizeScaleTransformer.reset( sizeTransformer ? sizeTransformer->
clone() : nullptr );
87 if ( mTitleLabel.isEmpty() )
91 if ( sizeTransformer && mSizeClasses.isEmpty() )
95 for (
double v : prettyBreaks )
97 mSizeClasses <<
SizeClass( v, QString::number( v ) );
105 if ( !mTitleLabel.isEmpty() )
123 lst.reserve( mSizeClasses.size() );
124 for (
const SizeClass &cl : mSizeClasses )
128 double size = cl.
size;
129 if ( mSizeScaleTransformer )
131 size = mSizeScaleTransformer->size( size );
149 *outputSize = QSizeF();
156 double hLengthLineMM = 2;
157 double hSpaceLineTextMM = 1;
159 std::unique_ptr<QgsMarkerSymbol> s( mSymbol->clone() );
161 QList<SizeClass>
classes = mSizeClasses;
164 if ( mSizeScaleTransformer )
167 cls.size = mSizeScaleTransformer->size( cls.size );
171 std::sort( classes.begin(), classes.end(), [](
const SizeClass & a,
const SizeClass & b ) {
return a.
size > b.size; } );
175 int dpm = std::round( context.
scaleFactor() * 1000 );
178 QImage tmpImg( QSize( 1, 1 ), QImage::Format_ARGB32_Premultiplied );
179 tmpImg.setDotsPerMeterX( dpm );
180 tmpImg.setDotsPerMeterY( dpm );
181 QFontMetricsF fm( mFont, &tmpImg );
182 double textHeight = fm.height();
183 double leading = fm.leading();
184 double minTextDistY = textHeight + leading;
191 double maxTextWidth = 0;
192 for (
const SizeClass &
c : qgis::as_const( classes ) )
194 maxTextWidth = std::max( maxTextWidth, fm.boundingRect(
c.label ).width() );
200 double largestSize = classes.at( 0 ).size;
201 double outputLargestSize = context.
convertToPainterUnits( largestSize, s->sizeUnit(), s->sizeMapUnitScale() );
204 QList<double> symbolTopY;
205 for (
const SizeClass &
c : qgis::as_const( classes ) )
211 symbolTopY << outputLargestSize / 2 - outputSymbolSize / 2;
214 symbolTopY << outputLargestSize - outputSymbolSize;
221 double middleIndex = 0;
222 QList<double> textCenterY;
223 double lastY = symbolTopY[middleIndex];
224 textCenterY << lastY;
225 for (
int i = middleIndex + 1; i < classes.count(); ++i )
227 double symbolY = symbolTopY[i];
228 if ( symbolY - lastY < minTextDistY )
229 symbolY = lastY + minTextDistY;
230 textCenterY << symbolY;
234 double textTopY = textCenterY.first() - textHeight / 2;
235 double textBottomY = textCenterY.last() + textHeight / 2;
236 double totalTextHeight = textBottomY - textTopY;
238 double fullWidth = outputLargestSize + hLengthLine + hSpaceLineText + maxTextWidth;
239 double fullHeight = std::max( outputLargestSize - textTopY, totalTextHeight );
242 *outputSize = QSizeF( fullWidth, fullHeight );
244 *labelXOffset = outputLargestSize + hLengthLine + hSpaceLineText;
253 QPainter *p = context.
painter();
256 p->translate( 0, -textTopY );
259 for (
const SizeClass &
c : qgis::as_const( classes ) )
261 s->setSize(
c.size );
264 double tx = ( outputLargestSize - outputSymbolSize ) / 2;
270 p->translate( tx, ( outputLargestSize - outputSymbolSize ) / 2 );
273 p->translate( tx, outputLargestSize - outputSymbolSize );
276 s->drawPreviewIcon( p, QSize( outputSymbolSize, outputSymbolSize ) );
280 p->setPen( mTextColor );
284 for (
const SizeClass &
c : qgis::as_const( classes ) )
287 p->drawLine( outputLargestSize / 2, symbolTopY[i], outputLargestSize + hLengthLine, textCenterY[i] );
290 QRect rect( outputLargestSize + hLengthLine + hSpaceLineText, textCenterY[i] - textHeight / 2,
291 maxTextWidth, textHeight );
292 p->drawText( rect, mTextAlignment,
c.label );
310 int dpm = std::round( context.
scaleFactor() * 1000 );
312 QImage img( contentSize.width() + padding * 2, contentSize.height() + padding * 2, QImage::Format_ARGB32_Premultiplied );
313 img.setDotsPerMeterX( dpm );
314 img.setDotsPerMeterY( dpm );
315 img.fill( backgroundColor );
317 QPainter painter( &img );
318 painter.setRenderHint( QPainter::Antialiasing,
true );
320 painter.translate( padding, padding );
323 QPainter *oldPainter = context.
painter();
339 ddsLegend->
setTitle( elem.attribute( QStringLiteral(
"title" ) ) );
341 QDomElement elemSymbol = elem.firstChildElement( QStringLiteral(
"symbol" ) );
342 if ( !elemSymbol.isNull() )
344 ddsLegend->
setSymbol( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( elemSymbol, context ) );
348 QDomElement elemTransformer = elem.firstChildElement( QStringLiteral(
"transformer" ) );
349 if ( !elemTransformer.isNull() )
356 QDomElement elemTextStyle = elem.firstChildElement( QStringLiteral(
"text-style" ) );
357 if ( !elemTextStyle.isNull() )
359 QDomElement elemFont = elemTextStyle.firstChildElement( QStringLiteral(
"font" ) );
360 if ( !elemFont.isNull() )
362 ddsLegend->
setFont( QFont( elemFont.attribute( QStringLiteral(
"family" ) ), elemFont.attribute( QStringLiteral(
"size" ) ).toInt(),
363 elemFont.attribute( QStringLiteral(
"weight" ) ).toInt(), elemFont.attribute( QStringLiteral(
"italic" ) ).toInt() ) );
366 ddsLegend->
setTextAlignment( static_cast<Qt::AlignmentFlag>( elemTextStyle.attribute( QStringLiteral(
"align" ) ).toInt() ) );
369 QDomElement elemClasses = elem.firstChildElement( QStringLiteral(
"classes" ) );
370 if ( !elemClasses.isNull() )
373 QDomElement elemClass = elemClasses.firstChildElement( QStringLiteral(
"class" ) );
374 while ( !elemClass.isNull() )
376 classes <<
SizeClass( elemClass.attribute( QStringLiteral(
"size" ) ).toDouble(), elemClass.attribute( QStringLiteral(
"label" ) ) );
377 elemClass = elemClass.nextSiblingElement();
387 QDomDocument doc = elem.ownerDocument();
389 elem.setAttribute( QStringLiteral(
"type" ), mType ==
LegendCollapsed ?
"collapsed" :
"separated" );
390 elem.setAttribute( QStringLiteral(
"valign" ), mVAlign ==
AlignCenter ?
"center" :
"bottom" );
391 elem.setAttribute( QStringLiteral(
"title" ), mTitleLabel );
396 elem.appendChild( elemSymbol );
399 if ( mSizeScaleTransformer )
402 elemTransformer.setTagName( QStringLiteral(
"transformer" ) );
403 elem.appendChild( elemTransformer );
406 QDomElement elemFont = doc.createElement( QStringLiteral(
"font" ) );
407 elemFont.setAttribute( QStringLiteral(
"family" ), mFont.family() );
408 elemFont.setAttribute( QStringLiteral(
"size" ), mFont.pointSize() );
409 elemFont.setAttribute( QStringLiteral(
"weight" ), mFont.weight() );
410 elemFont.setAttribute( QStringLiteral(
"italic" ), mFont.italic() );
412 QDomElement elemTextStyle = doc.createElement( QStringLiteral(
"text-style" ) );
414 elemTextStyle.setAttribute( QStringLiteral(
"align" ), static_cast<int>( mTextAlignment ) );
415 elemTextStyle.appendChild( elemFont );
416 elem.appendChild( elemTextStyle );
418 if ( !mSizeClasses.isEmpty() )
420 QDomElement elemClasses = doc.createElement( QStringLiteral(
"classes" ) );
421 for (
const SizeClass &sc : qgis::as_const( mSizeClasses ) )
423 QDomElement elemClass = doc.createElement( QStringLiteral(
"class" ) );
424 elemClass.setAttribute( QStringLiteral(
"size" ), sc.size );
425 elemClass.setAttribute( QStringLiteral(
"label" ), sc.label );
426 elemClasses.appendChild( elemClass );
428 elem.appendChild( elemClasses );
The class is used as a container of context for various read/write operations on other objects...
Symbols are aligned to the center.
Each class (size value) has a separate legend node.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
void setTextAlignment(Qt::AlignmentFlag flag)
Sets horizontal text alignment for rendering of labels - only valid for collapsed legend...
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
Definition of one class for the legend.
QgsDataDefinedSizeLegend()
Constructor for QgsDataDefinedSizeLegend.
Expression based property (QgsExpressionBasedProperty)
QgsDataDefinedSizeLegend & operator=(const QgsDataDefinedSizeLegend &other)
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
QgsLegendSymbolList legendSymbolList() const
Generates legend symbol items according to the configuration.
void setTitle(const QString &title)
Sets title label for data-defined size legend.
void setLegendType(LegendType type)
Sets how the legend should be rendered.
QgsMarkerSymbol * symbol() const
Returns marker symbol that will be used to draw markers in legend.
A marker symbol type, for rendering Point and MultiPoint geometries.
const QgsPropertyTransformer * transformer() const
Returns the existing transformer used for manipulating the calculated values for the property...
static QString encodeColor(const QColor &color)
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
QString expressionString() const
Returns the expression used for the property value.
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...
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Type propertyType() const
Returns the property type.
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...
QgsSizeScaleTransformer * sizeScaleTransformer() const
Returns transformer for scaling of symbol sizes. Returns nullptr if no transformer is defined...
double size
Marker size in units used by the symbol (usually millimeters). May be further scaled before rendering...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
void updateFromSymbolAndProperty(const QgsMarkerSymbol *symbol, const QgsProperty &ddSize)
Updates the list of classes, source symbol and title label from given symbol and property.
void setSizeScaleTransformer(QgsSizeScaleTransformer *transformer SIP_TRANSFER)
Sets transformer for scaling of symbol sizes. Takes ownership of the object. Accepts nullptr to set n...
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...
A store for object properties.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const
Writes configuration to the given XML element.
QString field() const
Returns the current field name the property references.
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
Symbols are aligned to the bottom.
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...
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setSymbol(QgsMarkerSymbol *symbol SIP_TRANSFER)
Sets marker symbol that will be used to draw markers in legend.
~QgsDataDefinedSizeLegend()
double size() const
Returns the estimated size for the whole symbol, which is the maximum size of all marker symbol layer...
QString title() const
Returns title label for data-defined size legend.
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend...
All classes are rendered within one legend node.
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 setDataDefinedSizeLegendSettings(QgsDataDefinedSizeLegend *settings)
Sets extra information about data-defined size.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
static QColor decodeColor(const QString &str)
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...