QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsgraduatedsymbolrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgraduatedsymbolrenderer.cpp
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <QDomDocument>
17#include <QDomElement>
18
19#include <ctime>
20#include <cmath>
21
23
24#include "qgsattributes.h"
26#include "qgscolorramp.h"
27#include "qgscolorrampimpl.h"
29#include "qgsexpression.h"
30#include "qgsfeature.h"
32#include "qgslogger.h"
33#include "qgspainteffect.h"
36#include "qgsproperty.h"
37#include "qgssymbol.h"
38#include "qgssymbollayer.h"
39#include "qgssymbollayerutils.h"
41#include "qgsvectorlayer.h"
42#include "qgsvectorlayerutils.h"
47#include "qgsapplication.h"
50#include "qgsmarkersymbol.h"
51#include "qgslinesymbol.h"
52
54 : QgsFeatureRenderer( QStringLiteral( "graduatedSymbol" ) )
55 , mAttrName( attrName )
56{
57 // TODO: check ranges for sanity (NULL symbols, invalid ranges)
58
59 //important - we need a deep copy of the ranges list, not a shared copy. This is required because
60 //QgsRendererRange::symbol() is marked const, and so retrieving the symbol via this method does not
61 //trigger a detachment and copy of mRanges BUT that same method CAN be used to modify a symbol in place
62 const auto constRanges = ranges;
63 for ( const QgsRendererRange &range : constRanges )
64 {
65 mRanges << range;
66 }
67
69}
70
72{
73 mRanges.clear(); // should delete all the symbols
74}
75
76
78{
79 for ( const QgsRendererRange &range : mRanges )
80 {
81 if ( range.lowerValue() <= value && range.upperValue() >= value )
82 {
83 if ( range.renderState() || mCounting )
84 return &range;
85 else
86 return nullptr;
87 }
88 }
89
90 // second chance -- use a bit of double tolerance to avoid floating point equality fuzziness
91 // if a value falls just outside of a range, but within acceptable double precision tolerance
92 // then we accept it anyway
93 for ( const QgsRendererRange &range : mRanges )
94 {
95 if ( qgsDoubleNear( range.lowerValue(), value ) || qgsDoubleNear( range.upperValue(), value ) )
96 {
97 if ( range.renderState() || mCounting )
98 return &range;
99 else
100 return nullptr;
101 }
102 }
103 // the value is out of the range: return NULL instead of symbol
104 return nullptr;
105}
106
108{
109 if ( const QgsRendererRange *range = rangeForValue( value ) )
110 return range->symbol();
111 return nullptr;
112}
113
115{
116 if ( const QgsRendererRange *matchingRange = rangeForValue( value ) )
117 {
118 int i = 0;
119 for ( const QgsRendererRange &range : mRanges )
120 {
121 if ( matchingRange == &range )
122 return QString::number( i );
123 i++;
124 }
125 }
126 return QString();
127}
128
130{
131 return originalSymbolForFeature( feature, context );
132}
133
134QVariant QgsGraduatedSymbolRenderer::valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const
135{
136 QgsAttributes attrs = feature.attributes();
137 QVariant value;
138 if ( mExpression )
139 {
140 value = mExpression->evaluate( &context.expressionContext() );
141 }
142 else
143 {
144 value = attrs.value( mAttrNum );
145 }
146
147 return value;
148}
149
151{
152 QVariant value = valueForFeature( feature, context );
153
154 // Null values should not be categorized
155 if ( QgsVariantUtils::isNull( value ) )
156 return nullptr;
157
158 // find the right category
159 return symbolForValue( value.toDouble() );
160}
161
163{
164 QgsFeatureRenderer::startRender( context, fields );
165
166 mCounting = context.rendererScale() == 0.0;
167
168 // find out classification attribute index from name
169 mAttrNum = fields.lookupField( mAttrName );
170
171 if ( mAttrNum == -1 )
172 {
173 mExpression.reset( new QgsExpression( mAttrName ) );
174 mExpression->prepare( &context.expressionContext() );
175 }
176
177 for ( const QgsRendererRange &range : std::as_const( mRanges ) )
178 {
179 if ( !range.symbol() )
180 continue;
181
182 range.symbol()->startRender( context, fields );
183 }
184}
185
187{
189
190 for ( const QgsRendererRange &range : std::as_const( mRanges ) )
191 {
192 if ( !range.symbol() )
193 continue;
194
195 range.symbol()->stopRender( context );
196 }
197}
198
200{
201 QSet<QString> attributes;
202
203 // mAttrName can contain either attribute name or an expression.
204 // Sometimes it is not possible to distinguish between those two,
205 // e.g. "a - b" can be both a valid attribute name or expression.
206 // Since we do not have access to fields here, try both options.
207 attributes << mAttrName;
208
209 QgsExpression testExpr( mAttrName );
210 if ( !testExpr.hasParserError() )
211 attributes.unite( testExpr.referencedColumns() );
212
213 QgsRangeList::const_iterator range_it = mRanges.constBegin();
214 for ( ; range_it != mRanges.constEnd(); ++range_it )
215 {
216 QgsSymbol *symbol = range_it->symbol();
217 if ( symbol )
218 {
219 attributes.unite( symbol->usedAttributes( context ) );
220 }
221 }
222 return attributes;
223}
224
226{
227 QgsExpression testExpr( mAttrName );
228 if ( !testExpr.hasParserError() )
229 {
230 QgsExpressionContext context;
231 context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) ); // unfortunately no layer access available!
232 testExpr.prepare( &context );
233 return testExpr.needsGeometry();
234 }
235 return false;
236}
237
239{
240 if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
241 return false;
242 mRanges[rangeIndex].setSymbol( symbol );
243 return true;
244}
245
246bool QgsGraduatedSymbolRenderer::updateRangeLabel( int rangeIndex, const QString &label )
247{
248 if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
249 return false;
250 mRanges[rangeIndex].setLabel( label );
251 return true;
252}
253
254bool QgsGraduatedSymbolRenderer::updateRangeUpperValue( int rangeIndex, double value )
255{
256 if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
257 return false;
258 QgsRendererRange &range = mRanges[rangeIndex];
260 if ( rangeIndex == 0 )
262 else if ( rangeIndex == mRanges.count() )
264
265 bool isDefaultLabel = mClassificationMethod->labelForRange( range, pos ) == range.label();
266 range.setUpperValue( value );
267 if ( isDefaultLabel )
268 range.setLabel( mClassificationMethod->labelForRange( range, pos ) );
269
270 return true;
271}
272
273bool QgsGraduatedSymbolRenderer::updateRangeLowerValue( int rangeIndex, double value )
274{
275 if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
276 return false;
277
278 QgsRendererRange &range = mRanges[rangeIndex];
280 if ( rangeIndex == 0 )
282 else if ( rangeIndex == mRanges.count() )
284
285 bool isDefaultLabel = mClassificationMethod->labelForRange( range, pos ) == range.label();
286 range.setLowerValue( value );
287 if ( isDefaultLabel )
288 range.setLabel( mClassificationMethod->labelForRange( range, pos ) );
289
290 return true;
291}
292
294{
295 if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
296 return false;
297 mRanges[rangeIndex].setRenderState( value );
298 return true;
299}
300
302{
303 QString s = QStringLiteral( "GRADUATED: attr %1\n" ).arg( mAttrName );
304 for ( int i = 0; i < mRanges.count(); i++ )
305 s += mRanges[i].dump();
306 return s;
307}
308
310{
312
314
315 if ( mSourceSymbol )
316 r->setSourceSymbol( mSourceSymbol->clone() );
317 if ( mSourceColorRamp )
318 {
320 }
323 copyRendererData( r );
324 return r;
325}
326
327void QgsGraduatedSymbolRenderer::toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
328{
329 QVariantMap newProps = props;
330 newProps[ QStringLiteral( "attribute" )] = mAttrName;
331 newProps[ QStringLiteral( "method" )] = graduatedMethodStr( mGraduatedMethod );
332
333 // create a Rule for each range
334 bool first = true;
335 for ( QgsRangeList::const_iterator it = mRanges.constBegin(); it != mRanges.constEnd(); ++it )
336 {
337 it->toSld( doc, element, newProps, first );
338 first = false;
339 }
340}
341
343{
344 Q_UNUSED( context )
345 QgsSymbolList lst;
346 lst.reserve( mRanges.count() );
347 for ( const QgsRendererRange &range : std::as_const( mRanges ) )
348 {
349 lst.append( range.symbol() );
350 }
351 return lst;
352}
353
355{
356 for ( const QgsRendererRange &range : std::as_const( mRanges ) )
357 {
358 QgsStyleSymbolEntity entity( range.symbol() );
359 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, QStringLiteral( "%1 - %2" ).arg( range.lowerValue() ).arg( range.upperValue() ), range.label() ) ) )
360 return false;
361 }
362
363 if ( mSourceColorRamp )
364 {
366 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity ) ) )
367 return false;
368 }
369
370 return true;
371}
372
373void QgsGraduatedSymbolRenderer::makeBreaksSymmetric( QList<double> &breaks, double symmetryPoint, bool astride )
374{
376}
377
378QList<double> QgsGraduatedSymbolRenderer::calcEqualIntervalBreaks( double minimum, double maximum, int classes, bool useSymmetricMode, double symmetryPoint, bool astride )
379{
382 QList<QgsClassificationRange> _classes = method.classes( minimum, maximum, classes );
384}
385
388 QgsVectorLayer *vlayer,
389 const QString &attrName,
390 int classes,
391 Mode mode,
392 QgsSymbol *symbol,
393 QgsColorRamp *ramp,
394 const QgsRendererRangeLabelFormat &labelFormat,
395 bool useSymmetricMode,
396 double symmetryPoint,
397 const QStringList &listForCboPrettyBreaks,
398 bool astride
399)
400{
401 Q_UNUSED( listForCboPrettyBreaks )
402
405 r->setSourceSymbol( symbol->clone() );
406 r->setSourceColorRamp( ramp->clone() );
407
408 QString methodId = methodIdFromMode( mode );
410
411 if ( method )
412 {
414 method->setLabelFormat( labelFormat.format() );
417 }
418 r->setClassificationMethod( method );
419
420 r->updateClasses( vlayer, classes );
421 return r;
422}
424
426 bool useSymmetricMode, double symmetryPoint, bool astride )
427{
428 if ( mAttrName.isEmpty() )
429 return;
430
431 QString methodId = methodIdFromMode( mode );
434 setClassificationMethod( method );
435
436 updateClasses( vlayer, nclasses );
437}
438
440{
442 return;
443
444 QList<QgsClassificationRange> classes = mClassificationMethod->classes( vl, mAttrName, nclasses );
445
447
448 for ( QList<QgsClassificationRange>::iterator it = classes.begin(); it != classes.end(); ++it )
449 {
451 addClass( QgsRendererRange( *it, newSymbol ) );
452 }
453 updateColorRamp( nullptr );
454}
455
458{
459 return QgsRendererRangeLabelFormat( mClassificationMethod->labelFormat(), mClassificationMethod->labelPrecision(), mClassificationMethod->labelTrimTrailingZeroes() );
460}
462
464{
465 QDomElement symbolsElem = element.firstChildElement( QStringLiteral( "symbols" ) );
466 if ( symbolsElem.isNull() )
467 return nullptr;
468
469 QDomElement rangesElem = element.firstChildElement( QStringLiteral( "ranges" ) );
470 if ( rangesElem.isNull() )
471 return nullptr;
472
473 QgsSymbolMap symbolMap = QgsSymbolLayerUtils::loadSymbols( symbolsElem, context );
475
476 QDomElement rangeElem = rangesElem.firstChildElement();
477 while ( !rangeElem.isNull() )
478 {
479 if ( rangeElem.tagName() == QLatin1String( "range" ) )
480 {
481 double lowerValue = rangeElem.attribute( QStringLiteral( "lower" ) ).toDouble();
482 double upperValue = rangeElem.attribute( QStringLiteral( "upper" ) ).toDouble();
483 QString symbolName = rangeElem.attribute( QStringLiteral( "symbol" ) );
484 QString label = rangeElem.attribute( QStringLiteral( "label" ) );
485 bool render = rangeElem.attribute( QStringLiteral( "render" ), QStringLiteral( "true" ) ) != QLatin1String( "false" );
486 if ( symbolMap.contains( symbolName ) )
487 {
488 QgsSymbol *symbol = symbolMap.take( symbolName );
489 ranges.append( QgsRendererRange( lowerValue, upperValue, symbol, label, render ) );
490 }
491 }
492 rangeElem = rangeElem.nextSiblingElement();
493 }
494
495 QString attrName = element.attribute( QStringLiteral( "attr" ) );
496
498
499 QString attrMethod = element.attribute( QStringLiteral( "graduatedMethod" ) );
500 if ( !attrMethod.isEmpty() )
501 {
502 if ( attrMethod == graduatedMethodStr( Qgis::GraduatedMethod::Color ) )
503 r->setGraduatedMethod( Qgis::GraduatedMethod::Color );
504 else if ( attrMethod == graduatedMethodStr( Qgis::GraduatedMethod::Size ) )
505 r->setGraduatedMethod( Qgis::GraduatedMethod::Size );
506 }
507
508
509 // delete symbols if there are any more
511
512 // try to load source symbol (optional)
513 QDomElement sourceSymbolElem = element.firstChildElement( QStringLiteral( "source-symbol" ) );
514 if ( !sourceSymbolElem.isNull() )
515 {
516 QgsSymbolMap sourceSymbolMap = QgsSymbolLayerUtils::loadSymbols( sourceSymbolElem, context );
517 if ( sourceSymbolMap.contains( QStringLiteral( "0" ) ) )
518 {
519 r->setSourceSymbol( sourceSymbolMap.take( QStringLiteral( "0" ) ) );
520 }
521 QgsSymbolLayerUtils::clearSymbolMap( sourceSymbolMap );
522 }
523
524 // try to load color ramp (optional)
525 QDomElement sourceColorRampElem = element.firstChildElement( QStringLiteral( "colorramp" ) );
526 if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
527 {
528 r->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
529 }
530
531 // try to load mode
532
533 QDomElement modeElem = element.firstChildElement( QStringLiteral( "mode" ) ); // old format, backward compatibility
534 QDomElement methodElem = element.firstChildElement( QStringLiteral( "classificationMethod" ) );
535 QgsClassificationMethod *method = nullptr;
536
537 // TODO QGIS 4 Remove
538 // backward compatibility for QGIS project < 3.10
539 if ( !modeElem.isNull() )
540 {
541 QString modeString = modeElem.attribute( QStringLiteral( "name" ) );
542 QString methodId;
543 // the strings saved in the project does not match with the old Mode enum
544 if ( modeString == QLatin1String( "equal" ) )
545 methodId = QStringLiteral( "EqualInterval" );
546 else if ( modeString == QLatin1String( "quantile" ) )
547 methodId = QStringLiteral( "Quantile" );
548 else if ( modeString == QLatin1String( "jenks" ) )
549 methodId = QStringLiteral( "Jenks" );
550 else if ( modeString == QLatin1String( "stddev" ) )
551 methodId = QStringLiteral( "StdDev" );
552 else if ( modeString == QLatin1String( "pretty" ) )
553 methodId = QStringLiteral( "Pretty" );
554
556
557 // symmetric mode
558 QDomElement symmetricModeElem = element.firstChildElement( QStringLiteral( "symmetricMode" ) );
559 if ( !symmetricModeElem.isNull() )
560 {
561 // symmetry
562 QString symmetricEnabled = symmetricModeElem.attribute( QStringLiteral( "enabled" ) );
563 QString symmetricPointString = symmetricModeElem.attribute( QStringLiteral( "symmetryPoint" ) );
564 QString astrideEnabled = symmetricModeElem.attribute( QStringLiteral( "astride" ) );
565 method->setSymmetricMode( symmetricEnabled == QLatin1String( "true" ), symmetricPointString.toDouble(), astrideEnabled == QLatin1String( "true" ) );
566 }
567 QDomElement labelFormatElem = element.firstChildElement( QStringLiteral( "labelformat" ) );
568 if ( !labelFormatElem.isNull() )
569 {
570 // label format
571 QString format = labelFormatElem.attribute( QStringLiteral( "format" ), "%1" + QStringLiteral( " - " ) + "%2" );
572 int precision = labelFormatElem.attribute( QStringLiteral( "decimalplaces" ), QStringLiteral( "4" ) ).toInt();
573 bool trimTrailingZeroes = labelFormatElem.attribute( QStringLiteral( "trimtrailingzeroes" ), QStringLiteral( "false" ) ) == QLatin1String( "true" );
574 method->setLabelFormat( format );
575 method->setLabelPrecision( precision );
576 method->setLabelTrimTrailingZeroes( trimTrailingZeroes );
577 }
578 // End of backward compatibility
579 }
580 else
581 {
582 // QGIS project 3.10+
583 method = QgsClassificationMethod::create( methodElem, context );
584 }
585
586 // apply the method
587 r->setClassificationMethod( method );
588
589 QDomElement rotationElem = element.firstChildElement( QStringLiteral( "rotation" ) );
590 if ( !rotationElem.isNull() && !rotationElem.attribute( QStringLiteral( "field" ) ).isEmpty() )
591 {
592 for ( const QgsRendererRange &range : std::as_const( r->mRanges ) )
593 {
594 convertSymbolRotation( range.symbol(), rotationElem.attribute( QStringLiteral( "field" ) ) );
595 }
596 if ( r->mSourceSymbol )
597 {
598 convertSymbolRotation( r->mSourceSymbol.get(), rotationElem.attribute( QStringLiteral( "field" ) ) );
599 }
600 }
601 QDomElement sizeScaleElem = element.firstChildElement( QStringLiteral( "sizescale" ) );
602 if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( QStringLiteral( "field" ) ).isEmpty() )
603 {
604 for ( const QgsRendererRange &range : std::as_const( r->mRanges ) )
605 {
606 convertSymbolSizeScale( range.symbol(),
607 QgsSymbolLayerUtils::decodeScaleMethod( sizeScaleElem.attribute( QStringLiteral( "scalemethod" ) ) ),
608 sizeScaleElem.attribute( QStringLiteral( "field" ) ) );
609 }
610 if ( r->mSourceSymbol && r->mSourceSymbol->type() == Qgis::SymbolType::Marker )
611 {
613 QgsSymbolLayerUtils::decodeScaleMethod( sizeScaleElem.attribute( QStringLiteral( "scalemethod" ) ) ),
614 sizeScaleElem.attribute( QStringLiteral( "field" ) ) );
615 }
616 }
617
618 QDomElement ddsLegendSizeElem = element.firstChildElement( QStringLiteral( "data-defined-size-legend" ) );
619 if ( !ddsLegendSizeElem.isNull() )
620 {
621 r->mDataDefinedSizeLegend.reset( QgsDataDefinedSizeLegend::readXml( ddsLegendSizeElem, context ) );
622 }
623// TODO: symbol levels
624 return r;
625}
626
627QDomElement QgsGraduatedSymbolRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
628{
629 QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
630 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "graduatedSymbol" ) );
631 rendererElem.setAttribute( QStringLiteral( "attr" ), mAttrName );
632 rendererElem.setAttribute( QStringLiteral( "graduatedMethod" ), graduatedMethodStr( mGraduatedMethod ) );
633
634 // ranges
635 int i = 0;
637 QDomElement rangesElem = doc.createElement( QStringLiteral( "ranges" ) );
638 QgsRangeList::const_iterator it = mRanges.constBegin();
639 for ( ; it != mRanges.constEnd(); ++it )
640 {
641 const QgsRendererRange &range = *it;
642 QString symbolName = QString::number( i );
643 symbols.insert( symbolName, range.symbol() );
644
645 QDomElement rangeElem = doc.createElement( QStringLiteral( "range" ) );
646 rangeElem.setAttribute( QStringLiteral( "lower" ), QString::number( range.lowerValue(), 'f', 15 ) );
647 rangeElem.setAttribute( QStringLiteral( "upper" ), QString::number( range.upperValue(), 'f', 15 ) );
648 rangeElem.setAttribute( QStringLiteral( "symbol" ), symbolName );
649 rangeElem.setAttribute( QStringLiteral( "label" ), range.label() );
650 rangeElem.setAttribute( QStringLiteral( "render" ), range.renderState() ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
651 rangesElem.appendChild( rangeElem );
652 i++;
653 }
654
655 rendererElem.appendChild( rangesElem );
656
657 // save symbols
658 QDomElement symbolsElem = QgsSymbolLayerUtils::saveSymbols( symbols, QStringLiteral( "symbols" ), doc, context );
659 rendererElem.appendChild( symbolsElem );
660
661 // save source symbol
662 if ( mSourceSymbol )
663 {
664 QgsSymbolMap sourceSymbols;
665 sourceSymbols.insert( QStringLiteral( "0" ), mSourceSymbol.get() );
666 QDomElement sourceSymbolElem = QgsSymbolLayerUtils::saveSymbols( sourceSymbols, QStringLiteral( "source-symbol" ), doc, context );
667 rendererElem.appendChild( sourceSymbolElem );
668 }
669
670 // save source color ramp
671 if ( mSourceColorRamp )
672 {
673 QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), mSourceColorRamp.get(), doc );
674 rendererElem.appendChild( colorRampElem );
675 }
676
677 // save classification method
678 QDomElement classificationMethodElem = mClassificationMethod->save( doc, context );
679 rendererElem.appendChild( classificationMethodElem );
680
681 QDomElement rotationElem = doc.createElement( QStringLiteral( "rotation" ) );
682 rendererElem.appendChild( rotationElem );
683
684 QDomElement sizeScaleElem = doc.createElement( QStringLiteral( "sizescale" ) );
685 rendererElem.appendChild( sizeScaleElem );
686
688 {
689 QDomElement ddsLegendElem = doc.createElement( QStringLiteral( "data-defined-size-legend" ) );
690 mDataDefinedSizeLegend->writeXml( ddsLegendElem, context );
691 rendererElem.appendChild( ddsLegendElem );
692 }
693
694 saveRendererData( doc, rendererElem, context );
695
696 return rendererElem;
697}
698
699QgsLegendSymbolList QgsGraduatedSymbolRenderer::baseLegendSymbolItems() const
700{
702 int i = 0;
703 lst.reserve( mRanges.size() );
704 for ( const QgsRendererRange &range : mRanges )
705 {
706 lst << QgsLegendSymbolItem( range.symbol(), range.label(), QString::number( i++ ), true );
707 }
708 return lst;
709}
710
712QString QgsGraduatedSymbolRenderer::methodIdFromMode( QgsGraduatedSymbolRenderer::Mode mode )
713{
714 switch ( mode )
715 {
716 case EqualInterval:
717 return QStringLiteral( "EqualInterval" );
718 case Quantile:
719 return QStringLiteral( "Quantile" );
720 case Jenks:
721 return QStringLiteral( "Jenks" );
722 case StdDev:
723 return QStringLiteral( "StdDev" );
724 case Pretty:
725 return QStringLiteral( "Pretty" );
726 case Custom:
727 return QString();
728 }
729 return QString();
730}
731
732QgsGraduatedSymbolRenderer::Mode QgsGraduatedSymbolRenderer::modeFromMethodId( const QString &methodId )
733{
734 if ( methodId == QLatin1String( "EqualInterval" ) )
735 return EqualInterval;
736 if ( methodId == QLatin1String( "Quantile" ) )
737 return Quantile;
738 if ( methodId == QLatin1String( "Jenks" ) )
739 return Jenks;
740 if ( methodId == QLatin1String( "StdDev" ) )
741 return StdDev;
742 if ( methodId == QLatin1String( "Pretty" ) )
743 return Pretty;
744 else
745 return Custom;
746}
748
750{
752 {
753 // check that all symbols that have the same size expression
754 QgsProperty ddSize;
755 for ( const QgsRendererRange &range : mRanges )
756 {
757 const QgsMarkerSymbol *symbol = static_cast<const QgsMarkerSymbol *>( range.symbol() );
758 if ( ddSize )
759 {
760 QgsProperty sSize( symbol->dataDefinedSize() );
761 if ( sSize && sSize != ddSize )
762 {
763 // no common size expression
764 return baseLegendSymbolItems();
765 }
766 }
767 else
768 {
769 ddSize = symbol->dataDefinedSize();
770 }
771 }
772
773 if ( ddSize && ddSize.isActive() )
774 {
776
778 ddSizeLegend.updateFromSymbolAndProperty( static_cast<const QgsMarkerSymbol *>( mSourceSymbol.get() ), ddSize );
779 lst += ddSizeLegend.legendSymbolList();
780
781 lst += baseLegendSymbolItems();
782 return lst;
783 }
784 }
785
786 return baseLegendSymbolItems();
787}
788
789QSet< QString > QgsGraduatedSymbolRenderer::legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const
790{
791 QVariant value = valueForFeature( feature, context );
792
793 // Null values should not be categorized
794 if ( QgsVariantUtils::isNull( value ) )
795 return QSet< QString >();
796
797 // find the right category
798 QString key = legendKeyForValue( value.toDouble() );
799 if ( !key.isNull() )
800 return QSet< QString >() << key;
801 else
802 return QSet< QString >();
803}
804
805QString QgsGraduatedSymbolRenderer::legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const
806{
807 ok = false;
808 int ruleIndex = key.toInt( &ok );
809 if ( !ok || ruleIndex < 0 || ruleIndex >= mRanges.size() )
810 {
811 ok = false;
812 return QString();
813 }
814
815 const QString attributeComponent = QgsExpression::quoteFieldExpression( mAttrName, layer );
816
817 ok = true;
818 const QgsRendererRange &range = mRanges[ ruleIndex ];
819
820 return QStringLiteral( "(%1 >= %2) AND (%1 <= %3)" ).arg( attributeComponent, QgsExpression::quotedValue( range.lowerValue(), QVariant::Double ),
821 QgsExpression::quotedValue( range.upperValue(), QVariant::Double ) );
822}
823
825{
826 return mSourceSymbol.get();
827}
828
830{
831 return mSourceSymbol.get();
832}
833
835{
836 mSourceSymbol.reset( sym );
837}
838
840{
841 return mSourceColorRamp.get();
842}
843
845{
846 return mSourceColorRamp.get();
847}
848
850{
851 if ( ramp == mSourceColorRamp.get() )
852 return;
853
854 mSourceColorRamp.reset( ramp );
855}
856
858{
859 double min = std::numeric_limits<double>::max();
860 for ( int i = 0; i < mRanges.count(); i++ )
861 {
862 double sz = 0;
863 if ( mRanges[i].symbol()->type() == Qgis::SymbolType::Marker )
864 sz = static_cast< QgsMarkerSymbol * >( mRanges[i].symbol() )->size();
865 else if ( mRanges[i].symbol()->type() == Qgis::SymbolType::Line )
866 sz = static_cast< QgsLineSymbol * >( mRanges[i].symbol() )->width();
867 min = std::min( sz, min );
868 }
869 return min;
870}
871
873{
874 double max = std::numeric_limits<double>::min();
875 for ( int i = 0; i < mRanges.count(); i++ )
876 {
877 double sz = 0;
878 if ( mRanges[i].symbol()->type() == Qgis::SymbolType::Marker )
879 sz = static_cast< QgsMarkerSymbol * >( mRanges[i].symbol() )->size();
880 else if ( mRanges[i].symbol()->type() == Qgis::SymbolType::Line )
881 sz = static_cast< QgsLineSymbol * >( mRanges[i].symbol() )->width();
882 max = std::max( sz, max );
883 }
884 return max;
885}
886
887void QgsGraduatedSymbolRenderer::setSymbolSizes( double minSize, double maxSize )
888{
889 for ( int i = 0; i < mRanges.count(); i++ )
890 {
891 std::unique_ptr<QgsSymbol> symbol( mRanges.at( i ).symbol() ? mRanges.at( i ).symbol()->clone() : nullptr );
892 const double size = mRanges.count() > 1
893 ? minSize + i * ( maxSize - minSize ) / ( mRanges.count() - 1 )
894 : .5 * ( maxSize + minSize );
895 if ( symbol->type() == Qgis::SymbolType::Marker )
896 static_cast< QgsMarkerSymbol * >( symbol.get() )->setSize( size );
897 if ( symbol->type() == Qgis::SymbolType::Line )
898 static_cast< QgsLineSymbol * >( symbol.get() )->setWidth( size );
899 updateRangeSymbol( i, symbol.release() );
900 }
901}
902
904{
905 int i = 0;
906 if ( ramp )
907 {
908 setSourceColorRamp( ramp );
909 }
910
911 if ( mSourceColorRamp )
912 {
913 for ( const QgsRendererRange &range : std::as_const( mRanges ) )
914 {
915 QgsSymbol *symbol = range.symbol() ? range.symbol()->clone() : nullptr;
916 if ( symbol )
917 {
918 double colorValue;
919 colorValue = ( mRanges.count() > 1 ? static_cast< double >( i ) / ( mRanges.count() - 1 ) : 0 );
920 symbol->setColor( mSourceColorRamp->color( colorValue ) );
921 }
922 updateRangeSymbol( i, symbol );
923 ++i;
924 }
925 }
926
927}
928
930{
931 if ( !sym )
932 return;
933
934 int i = 0;
935 for ( const QgsRendererRange &range : std::as_const( mRanges ) )
936 {
937 std::unique_ptr<QgsSymbol> symbol( sym->clone() );
938 switch ( mGraduatedMethod )
939 {
940 case Qgis::GraduatedMethod::Color:
941 {
942 symbol->setColor( range.symbol()->color() );
943 break;
944 }
945 case Qgis::GraduatedMethod::Size:
946 {
947 if ( symbol->type() == Qgis::SymbolType::Marker )
948 static_cast<QgsMarkerSymbol *>( symbol.get() )->setSize(
949 static_cast<QgsMarkerSymbol *>( range.symbol() )->size() );
950 else if ( symbol->type() == Qgis::SymbolType::Line )
951 static_cast<QgsLineSymbol *>( symbol.get() )->setWidth(
952 static_cast<QgsLineSymbol *>( range.symbol() )->width() );
953 break;
954 }
955 }
956 updateRangeSymbol( i, symbol.release() );
957 ++i;
958 }
959 setSourceSymbol( sym->clone() );
960}
961
963{
964 return true;
965}
966
968{
969 bool ok;
970 int index = key.toInt( &ok );
971 if ( ok && index >= 0 && index < mRanges.size() )
972 return mRanges.at( index ).renderState();
973 else
974 return true;
975}
976
977void QgsGraduatedSymbolRenderer::checkLegendSymbolItem( const QString &key, bool state )
978{
979 bool ok;
980 int index = key.toInt( &ok );
981 if ( ok )
982 updateRangeRenderState( index, state );
983}
984
986{
987 bool ok;
988 int index = key.toInt( &ok );
989 if ( ok )
990 updateRangeSymbol( index, symbol );
991 else
992 delete symbol;
993}
994
996{
997 QgsSymbol *newSymbol = symbol->clone();
998 QString label = QStringLiteral( "0.0 - 0.0" );
999 mRanges.insert( 0, QgsRendererRange( 0.0, 0.0, newSymbol, label ) );
1000}
1001
1003{
1004 QgsSymbol *newSymbol = mSourceSymbol->clone();
1005 QString label = mClassificationMethod->labelForRange( lower, upper );
1006 mRanges.append( QgsRendererRange( lower, upper, newSymbol, label ) );
1007}
1008
1009void QgsGraduatedSymbolRenderer::addBreak( double breakValue, bool updateSymbols )
1010{
1011 QMutableListIterator< QgsRendererRange > it( mRanges );
1012 while ( it.hasNext() )
1013 {
1014 QgsRendererRange range = it.next();
1015 if ( range.lowerValue() < breakValue && range.upperValue() > breakValue )
1016 {
1018 newRange.setLowerValue( breakValue );
1019 newRange.setUpperValue( range.upperValue() );
1020 newRange.setLabel( mClassificationMethod->labelForRange( newRange ) );
1021 newRange.setSymbol( mSourceSymbol->clone() );
1022
1023 //update old range
1024 bool isDefaultLabel = range.label() == mClassificationMethod->labelForRange( range );
1025 range.setUpperValue( breakValue );
1026 if ( isDefaultLabel )
1027 range.setLabel( mClassificationMethod->labelForRange( range.lowerValue(), breakValue ) );
1028 it.setValue( range );
1029
1030 it.insert( newRange );
1031 break;
1032 }
1033 }
1034
1035 if ( updateSymbols )
1036 {
1037 switch ( mGraduatedMethod )
1038 {
1039 case Qgis::GraduatedMethod::Color:
1041 break;
1042 case Qgis::GraduatedMethod::Size:
1044 break;
1045 }
1046 }
1047}
1048
1050{
1051 mRanges.append( range );
1052}
1053
1055{
1056 mRanges.removeAt( idx );
1057}
1058
1060{
1061 mRanges.clear();
1062}
1063
1066{
1067 mClassificationMethod->setLabelFormat( labelFormat.format() );
1068 mClassificationMethod->setLabelPrecision( labelFormat.precision() );
1069 mClassificationMethod->setLabelTrimTrailingZeroes( labelFormat.trimTrailingZeroes() );
1070
1071 if ( updateRanges )
1072 {
1074 }
1075}
1077
1079{
1080 for ( int i = 0; i < mRanges.count(); i++ )
1081 {
1083 if ( i == 0 )
1085 else if ( i == mRanges.count() - 1 )
1087 mRanges[i].setLabel( mClassificationMethod->labelForRange( mRanges[i], pos ) );
1088 }
1089}
1090
1092{
1093 // Find the minimum size of a class
1094 double minClassRange = 0.0;
1095 for ( const QgsRendererRange &rendererRange : std::as_const( mRanges ) )
1096 {
1097 double range = rendererRange.upperValue() - rendererRange.lowerValue();
1098 if ( range <= 0.0 )
1099 continue;
1100 if ( minClassRange == 0.0 || range < minClassRange )
1101 minClassRange = range;
1102 }
1103 if ( minClassRange <= 0.0 )
1104 return;
1105
1106 // Now set the number of decimal places to ensure no more than 20% error in
1107 // representing this range (up to 10% at upper and lower end)
1108
1109 int ndp = 10;
1110 double nextDpMinRange = 0.0000000099;
1111 while ( ndp > 0 && nextDpMinRange < minClassRange )
1112 {
1113 ndp--;
1114 nextDpMinRange *= 10.0;
1115 }
1116 mClassificationMethod->setLabelPrecision( ndp );
1117 if ( updateRanges )
1119}
1120
1122{
1123 if ( from < 0 || from >= mRanges.size() || to < 0 || to >= mRanges.size() )
1124 return;
1125 mRanges.move( from, to );
1126}
1127
1129{
1130 return r1 < r2;
1131}
1132
1134{
1135 return !valueLessThan( r1, r2 );
1136}
1137
1139{
1140 if ( order == Qt::AscendingOrder )
1141 {
1142 std::sort( mRanges.begin(), mRanges.end(), valueLessThan );
1143 }
1144 else
1145 {
1146 std::sort( mRanges.begin(), mRanges.end(), valueGreaterThan );
1147 }
1148}
1149
1151{
1152 QgsRangeList sortedRanges = mRanges;
1153 std::sort( sortedRanges.begin(), sortedRanges.end(), valueLessThan );
1154
1155 QgsRangeList::const_iterator it = sortedRanges.constBegin();
1156 if ( it == sortedRanges.constEnd() )
1157 return false;
1158
1159 if ( ( *it ).upperValue() < ( *it ).lowerValue() )
1160 return true;
1161
1162 double prevMax = ( *it ).upperValue();
1163 ++it;
1164
1165 for ( ; it != sortedRanges.constEnd(); ++it )
1166 {
1167 if ( ( *it ).upperValue() < ( *it ).lowerValue() )
1168 return true;
1169
1170 if ( ( *it ).lowerValue() < prevMax )
1171 return true;
1172
1173 prevMax = ( *it ).upperValue();
1174 }
1175 return false;
1176}
1177
1179{
1180 QgsRangeList sortedRanges = mRanges;
1181 std::sort( sortedRanges.begin(), sortedRanges.end(), valueLessThan );
1182
1183 QgsRangeList::const_iterator it = sortedRanges.constBegin();
1184 if ( it == sortedRanges.constEnd() )
1185 return false;
1186
1187 double prevMax = ( *it ).upperValue();
1188 ++it;
1189
1190 for ( ; it != sortedRanges.constEnd(); ++it )
1191 {
1192 if ( !qgsDoubleNear( ( *it ).lowerValue(), prevMax ) )
1193 return true;
1194
1195 prevMax = ( *it ).upperValue();
1196 }
1197 return false;
1198}
1199
1201{
1202 return QString::localeAwareCompare( r1.label(), r2.label() ) < 0;
1203}
1204
1206{
1207 return !labelLessThan( r1, r2 );
1208}
1209
1211{
1212 if ( order == Qt::AscendingOrder )
1213 {
1214 std::sort( mRanges.begin(), mRanges.end(), labelLessThan );
1215 }
1216 else
1217 {
1218 std::sort( mRanges.begin(), mRanges.end(), labelGreaterThan );
1219 }
1220}
1221
1223{
1224 return mClassificationMethod.get();
1225}
1226
1228{
1229 mClassificationMethod.reset( method );
1230}
1231
1233{
1234 QString methodId = methodIdFromMode( mode );
1236 setClassificationMethod( method );
1237}
1238
1240{
1241 mClassificationMethod->setSymmetricMode( useSymmetricMode, mClassificationMethod->symmetryPoint(), mClassificationMethod->symmetryAstride() );
1242}
1243
1245{
1246 mClassificationMethod->setSymmetricMode( mClassificationMethod->symmetricModeEnabled(), symmetryPoint, mClassificationMethod->symmetryAstride() );
1247}
1248
1250{
1251 mClassificationMethod->setSymmetricMode( mClassificationMethod->symmetricModeEnabled(), mClassificationMethod->symmetryPoint(), astride );
1252}
1253
1255{
1256 std::unique_ptr< QgsGraduatedSymbolRenderer > r;
1257 if ( renderer->type() == QLatin1String( "graduatedSymbol" ) )
1258 {
1259 r.reset( static_cast<QgsGraduatedSymbolRenderer *>( renderer->clone() ) );
1260 }
1261 else if ( renderer->type() == QLatin1String( "categorizedSymbol" ) )
1262 {
1263 const QgsCategorizedSymbolRenderer *categorizedSymbolRenderer = dynamic_cast<const QgsCategorizedSymbolRenderer *>( renderer );
1264 if ( categorizedSymbolRenderer )
1265 {
1266 r = std::make_unique< QgsGraduatedSymbolRenderer >( QString(), QgsRangeList() );
1267 if ( categorizedSymbolRenderer->sourceSymbol() )
1268 r->setSourceSymbol( categorizedSymbolRenderer->sourceSymbol()->clone() );
1269 if ( categorizedSymbolRenderer->sourceColorRamp() )
1270 {
1271 bool isRandom = dynamic_cast<const QgsRandomColorRamp *>( categorizedSymbolRenderer->sourceColorRamp() ) ||
1272 dynamic_cast<const QgsLimitedRandomColorRamp *>( categorizedSymbolRenderer->sourceColorRamp() );
1273 if ( !isRandom )
1274 r->setSourceColorRamp( categorizedSymbolRenderer->sourceColorRamp()->clone() );
1275 }
1276 r->setClassAttribute( categorizedSymbolRenderer->classAttribute() );
1277 }
1278 }
1279 else if ( renderer->type() == QLatin1String( "pointDisplacement" ) || renderer->type() == QLatin1String( "pointCluster" ) )
1280 {
1281 const QgsPointDistanceRenderer *pointDistanceRenderer = dynamic_cast<const QgsPointDistanceRenderer *>( renderer );
1282 if ( pointDistanceRenderer )
1283 r.reset( convertFromRenderer( pointDistanceRenderer->embeddedRenderer() ) );
1284 }
1285 else if ( renderer->type() == QLatin1String( "invertedPolygonRenderer" ) )
1286 {
1287 const QgsInvertedPolygonRenderer *invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer *>( renderer );
1288 if ( invertedPolygonRenderer )
1289 r.reset( convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() ) );
1290 }
1291
1292 // If not one of the specifically handled renderers, then just grab the symbol from the renderer
1293 // Could have applied this to specific renderer types (singleSymbol, graduatedSymbol)
1294
1295 if ( !r )
1296 {
1297 r = std::make_unique< QgsGraduatedSymbolRenderer >( QString(), QgsRangeList() );
1298 QgsRenderContext context;
1299 QgsSymbolList symbols = const_cast<QgsFeatureRenderer *>( renderer )->symbols( context );
1300 if ( !symbols.isEmpty() )
1301 {
1302 r->setSourceSymbol( symbols.at( 0 )->clone() );
1303 }
1304 }
1305
1306 renderer->copyRendererData( r.get() );
1307
1308 return r.release();
1309}
1310
1312{
1313 mDataDefinedSizeLegend.reset( settings );
1314}
1315
1317{
1318 return mDataDefinedSizeLegend.get();
1319}
1320
1322{
1323 switch ( method )
1324 {
1325 case Qgis::GraduatedMethod::Color:
1326 return QStringLiteral( "GraduatedColor" );
1327 case Qgis::GraduatedMethod::Size:
1328 return QStringLiteral( "GraduatedSize" );
1329 }
1330 return QString();
1331}
1332
1333
GraduatedMethod
Methods for modifying symbols by range in a graduated symbol renderer.
Definition: qgis.h:2266
@ Marker
Marker symbol.
@ Line
Line symbol.
static QgsClassificationMethodRegistry * classificationMethodRegistry()
Returns the application's classification methods registry, used in graduated renderer.
A vector of attributes.
Definition: qgsattributes.h:59
QgsSymbol * sourceSymbol()
Returns the renderer's source symbol, which is the base symbol used for the each categories' symbol b...
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
QgsColorRamp * sourceColorRamp()
Returns the source color ramp, from which each categories' color is derived.
QgsClassificationCustom is a dummy implementation of QgsClassification which does not compute any bre...
static const QString METHOD_ID
QgsClassificationEqualInterval is an implementation of QgsClassificationMethod for equal intervals.
QgsClassificationMethod * method(const QString &id)
Returns a new instance of the method for the given id.
QgsClassificationMethod is an abstract class for implementations of classification methods.
static void makeBreaksSymmetric(QList< double > &breaks, double symmetryPoint, bool astride)
Remove the breaks that are above the existing opposite sign classes to keep colors symmetrically bala...
QList< QgsClassificationRange > classes(const QgsVectorLayer *layer, const QString &expression, int nclasses)
This will calculate the classes for a given layer to define the classes.
void setLabelTrimTrailingZeroes(bool trimTrailingZeroes)
Defines if the trailing 0 are trimmed in the label.
void setSymmetricMode(bool enabled, double symmetryPoint=0, bool symmetryAstride=false)
Defines if the symmetric mode is enables and configures its parameters.
ClassPosition
Defines the class position.
@ LowerBound
The class is at the lower bound.
@ UpperBound
The class is at the upper bound.
@ Inner
The class is not at a bound.
static QList< double > rangesToBreaks(const QList< QgsClassificationRange > &classes)
Transforms a list of classes to a list of breaks.
void setLabelFormat(const QString &format)
Defines the format of the labels for the classes, using %1 and %2 for the bounds.
static QgsClassificationMethod * create(const QDomElement &element, const QgsReadWriteContext &context)
Reads the DOM element and return a new classification method from it.
void setLabelPrecision(int labelPrecision)
Defines the precision for the formatting of the labels.
Abstract base class for color ramps.
Definition: qgscolorramp.h:30
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
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 updateFromSymbolAndProperty(const QgsMarkerSymbol *symbol, const QgsProperty &ddSize)
Updates the list of classes, source symbol and title label from given symbol and property.
QgsLegendSymbolList legendSymbolList() const
Generates legend symbol items according to the configuration.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
static QString quoteFieldExpression(const QString &expression, const QgsVectorLayer *layer)
Validate if the expression is a field in the layer and ensure it is quoted.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QString type() const
Definition: qgsrenderer.h:142
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:46
static void convertSymbolRotation(QgsSymbol *symbol, const QString &field)
void saveRendererData(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context)
Saves generic renderer data into the specified element.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:90
static void convertSymbolSizeScale(QgsSymbol *symbol, Qgis::ScaleMethod method, const QString &field)
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
Container of fields for a vector layer.
Definition: qgsfields.h:45
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:359
QgsLegendSymbolList legendSymbolItems() const override
Returns a list of symbology items for the legend.
QgsSymbol * sourceSymbol()
Returns the renderer's source symbol, which is the base symbol used for the each classes' symbol befo...
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
Stores renderer properties to an XML element.
void updateSymbols(QgsSymbol *sym)
Update all the symbols but leave breaks and colors.
const QgsRendererRange * rangeForValue(double value) const
Returns the renderer range matching the provided value, or nullptr if no range matches the value.
QgsColorRamp * sourceColorRamp()
Returns the source color ramp, from which each classes' color is derived.
bool updateRangeSymbol(int rangeIndex, QgsSymbol *symbol)
void addBreak(double breakValue, bool updateSymbols=true)
Add a breakpoint by splitting existing classes so that the specified value becomes a break between tw...
Q_DECL_DEPRECATED void setAstride(bool astride)
Set if we want a central class astride the pivot value.
void setClassificationMethod(QgsClassificationMethod *method)
Defines the classification method This will take ownership of the method.
static Q_DECL_DEPRECATED QList< double > calcEqualIntervalBreaks(double minimum, double maximum, int classes, bool useSymmetricMode, double symmetryPoint, bool astride)
Compute the equal interval classification.
QgsGraduatedSymbolRenderer * clone() const override
Create a deep copy of this renderer.
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
static Q_DECL_DEPRECATED void makeBreaksSymmetric(QList< double > &breaks, double symmetryPoint, bool astride)
Remove the breaks that are above the existing opposite sign classes to keep colors symmetrically bala...
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
double maxSymbolSize() const
Returns the max symbol size when graduated by size.
Q_DECL_DEPRECATED bool useSymmetricMode() const
Returns if we want to classify symmetric around a given value.
bool updateRangeLabel(int rangeIndex, const QString &label)
QgsClassificationMethod * classificationMethod() const
Returns the classification method.
QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns symbol for feature.
void sortByValue(Qt::SortOrder order=Qt::AscendingOrder)
void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
void checkLegendSymbolItem(const QString &key, bool state=true) override
Sets whether the legend symbology item with the specified ley should be checked.
std::unique_ptr< QgsSymbol > mSourceSymbol
void setLegendSymbolItem(const QString &key, QgsSymbol *symbol) override
Sets the symbol to be used for a legend symbol item.
void setGraduatedMethod(Qgis::GraduatedMethod method)
Set the method used for graduation (either size or color).
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
create renderer from XML element
Q_DECL_DEPRECATED void setMode(Mode mode)
QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns legend keys matching a specified feature.
bool rangesOverlap() const
Tests whether classes assigned to the renderer have ranges which overlap.
std::shared_ptr< QgsClassificationMethod > mClassificationMethod
Q_DECL_DEPRECATED bool astride() const
Returns if we want to have a central class astride the pivot value.
Q_DECL_DEPRECATED void setLabelFormat(const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges=false)
Set the label format used to generate default classification labels.
Q_DECL_DEPRECATED void updateClasses(QgsVectorLayer *vlayer, Mode mode, int nclasses, bool useSymmetricMode=false, double symmetryPoint=0.0, bool astride=false)
Recalculate classes for a layer.
QString dump() const override
Returns debug information about this renderer.
bool updateRangeUpperValue(int rangeIndex, double value)
QString legendKeyToExpression(const QString &key, QgsVectorLayer *layer, bool &ok) const override
Attempts to convert the specified legend rule key to a QGIS expression matching the features displaye...
QString legendKeyForValue(double value) const
Returns the matching legend key for a value.
void setSourceColorRamp(QgsColorRamp *ramp)
Sets the source color ramp.
void setSourceSymbol(QgsSymbol *sym)
Sets the source symbol for the renderer, which is the base symbol used for the each classes' symbol b...
bool legendSymbolItemChecked(const QString &key) override
Returns true if the legend symbology item with the specified key is checked.
bool updateRangeLowerValue(int rangeIndex, double value)
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
static QgsGraduatedSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
creates a QgsGraduatedSymbolRenderer from an existing renderer.
bool legendSymbolItemsCheckable() const override
Returns true if symbology items in legend are checkable.
void moveClass(int from, int to)
Moves the category at index position from to index position to.
void sortByLabel(Qt::SortOrder order=Qt::AscendingOrder)
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
void updateRangeLabels()
Updates the labels of the ranges.
bool updateRangeRenderState(int rangeIndex, bool render)
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
To be overridden.
std::unique_ptr< QgsColorRamp > mSourceColorRamp
QgsSymbolList symbols(QgsRenderContext &context) const override
Returns list of symbols used by the renderer.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration of appearance of legend when using data-defined size for marker symbols.
void updateColorRamp(QgsColorRamp *ramp=nullptr)
Update the color ramp used.
Q_DECL_DEPRECATED Mode mode() const
QgsGraduatedSymbolRenderer(const QString &attrName=QString(), const QgsRangeList &ranges=QgsRangeList())
double minSymbolSize() const
Returns the min symbol size when graduated by size.
Qgis::GraduatedMethod graduatedMethod() const
Returns the method used for graduation (either size or color).
bool filterNeedsGeometry() const override
Returns true if this renderer requires the geometry to apply the filter.
int mAttrNum
attribute index (derived from attribute name in startRender)
Q_DECL_DEPRECATED void setSymmetryPoint(double symmetryPoint)
Set the pivot point.
std::unique_ptr< QgsExpression > mExpression
static QString graduatedMethodStr(Qgis::GraduatedMethod method)
void setSymbolSizes(double minSize, double maxSize)
set varying symbol size for classes
void calculateLabelPrecision(bool updateRanges=true)
Reset the label decimal places to a numberbased on the minimum class interval.
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
const QgsRangeList & ranges() const
Returns a list of all ranges used in the classification.
Q_DECL_DEPRECATED QgsRendererRangeLabelFormat labelFormat() const
Returns the label format used to generate default classification labels.
static Q_DECL_DEPRECATED QgsGraduatedSymbolRenderer * createRenderer(QgsVectorLayer *vlayer, const QString &attrName, int classes, Mode mode, QgsSymbol *symbol, QgsColorRamp *ramp, const QgsRendererRangeLabelFormat &legendFormat=QgsRendererRangeLabelFormat(), bool useSymmetricMode=false, double symmetryPoint=0.0, const QStringList &listForCboPrettyBreaks=QStringList(), bool astride=false)
Creates a new graduated renderer.
Q_DECL_DEPRECATED double symmetryPoint() const
Returns the pivot value for symmetric classification.
Q_DECL_DEPRECATED void setUseSymmetricMode(bool useSymmetricMode)
Set if we want to classify symmetric around a given value.
QgsSymbol * symbolForValue(double value) const
Gets the symbol which is used to represent value.
bool rangesHaveGaps() const
Tests whether classes assigned to the renderer have gaps between the ranges.
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted,...
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Constrained random color ramp, which returns random colors based on preset parameters.
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:30
double width() const
Returns the estimated width for the whole symbol, which is the maximum width of all marker symbol lay...
A marker symbol type, for rendering Point and MultiPoint geometries.
double size() const
Returns the estimated size for the whole symbol, which is the maximum size of all marker symbol layer...
QgsProperty dataDefinedSize() const
Returns data defined size for whole symbol (including all symbol layers).
const QgsFeatureRenderer * embeddedRenderer() const override
Returns the current embedded renderer (subrenderer) for this feature renderer.
An abstract base class for distance based point renderers (e.g., clusterer and displacement renderers...
const QgsFeatureRenderer * embeddedRenderer() const override
Returns the current embedded renderer (subrenderer) for this feature renderer.
A store for object properties.
Definition: qgsproperty.h:230
bool isActive() const
Returns whether the property is currently active.
Totally random color ramp.
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 rendererScale() const
Returns the renderer map scale.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setUpperValue(double upperValue)
Sets the upper bound of the range.
QString label() const
Returns the label used for the range.
void setSymbol(QgsSymbol *s)
Sets the symbol used for the range.
QgsSymbol * symbol() const
Returns the symbol used for the range.
void setLabel(const QString &label)
Sets the label used for the range.
bool renderState() const
Returns true if the range should be rendered.
double upperValue() const
Returns the upper bound of the range.
void setLowerValue(double lowerValue)
Sets the lower bound of the range.
double lowerValue() const
Returns the lower bound of the range.
A color ramp entity for QgsStyle databases.
Definition: qgsstyle.h:1373
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:1341
static QgsColorRamp * loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
static Qgis::ScaleMethod decodeScaleMethod(const QString &str)
Decodes a symbol scale method from a string.
static QDomElement saveColorRamp(const QString &name, QgsColorRamp *ramp, QDomDocument &doc)
Encodes a color ramp's settings to an XML element.
static void clearSymbolMap(QgsSymbolMap &symbols)
static QgsSymbolMap loadSymbols(QDomElement &element, const QgsReadWriteContext &context)
Reads a collection of symbols from XML and returns them in a map. Caller is responsible for deleting ...
static QDomElement saveSymbols(QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a collection of symbols to XML with specified tagName for the top-level element.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:94
void setColor(const QColor &color) const
Sets the color for the symbol.
Definition: qgssymbol.cpp:901
QSet< QString > usedAttributes(const QgsRenderContext &context) const
Returns a list of attributes required to render this feature.
Definition: qgssymbol.cpp:1199
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:704
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
Definition: MathUtils.cpp:407
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:4572
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:4571
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3988
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
bool valueLessThan(const QgsRendererRange &r1, const QgsRendererRange &r2)
bool labelGreaterThan(const QgsRendererRange &r1, const QgsRendererRange &r2)
bool labelLessThan(const QgsRendererRange &r1, const QgsRendererRange &r2)
bool valueGreaterThan(const QgsRendererRange &r1, const QgsRendererRange &r2)
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define RENDERER_TAG_NAME
Definition: qgsrenderer.h:50
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:45
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QList< QgsRendererRange > QgsRangeList
int precision
Contains information relating to the style entity currently being visited.