QGIS API Documentation 3.43.0-Master (7a94ac641b4)
qgscategorizedsymbolrenderer.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscategorizedsymbolrenderer.h
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#ifndef QGSCATEGORIZEDSYMBOLRENDERER_H
16#define QGSCATEGORIZEDSYMBOLRENDERER_H
17
18#include "qgis_core.h"
19#include "qgis.h"
20#include "qgsrenderer.h"
21
22#include <QHash>
23
24class QgsVectorLayer;
25class QgsStyle;
27class QgsSymbol;
28class QgsExpression;
29class QgsColorRamp;
30
35class CORE_EXPORT QgsRendererCategory
36{
37 public:
38
40
54 QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true, const QString &uuid = QString() );
55
59
64 QString uuid() const;
65
73 QVariant value() const;
74
79 QgsSymbol *symbol() const;
80
86 QString label() const;
87
95 void setValue( const QVariant &value );
96
104 void setSymbol( QgsSymbol *s SIP_TRANSFER );
105
111 void setLabel( const QString &label );
112
117 bool renderState() const;
118
123 void setRenderState( bool render );
124
125 // debugging
126
130 QString dump() const;
131
137 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const SIP_DEPRECATED;
138
144 bool toSld( QDomDocument &doc, QDomElement &element, const QString &classAttribute, QgsSldExportContext &context ) const;
145
146#ifdef SIP_RUN
147 SIP_PYOBJECT __repr__();
148 % MethodCode
149 const QString str = !sipCpp->value().isValid()
150 ? QStringLiteral( "<QgsRendererCategory>" )
151 : sipCpp->label().isEmpty()
152 ? QStringLiteral( "<QgsRendererCategory: %1>" ).arg( sipCpp->value().toString() )
153 : QStringLiteral( "<QgsRendererCategory: %1 (%2)>" ).arg( sipCpp->value().toString(), sipCpp->label() );
154 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
155 % End
156#endif
157
158 protected:
160
161 QVariant mValue;
162 std::unique_ptr<QgsSymbol> mSymbol;
163 QString mLabel;
164 bool mRender = true;
165 QString mUuid;
166};
167
168typedef QList<QgsRendererCategory> QgsCategoryList;
169
176{
177 public:
178
187 QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
189
190 Qgis::FeatureRendererFlags flags() const override;
191 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
192 QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
193 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
194 void stopRender( QgsRenderContext &context ) override;
195 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
196 bool filterNeedsGeometry() const override;
197 QString dump() const override;
198 QgsCategorizedSymbolRenderer *clone() const override SIP_FACTORY;
199 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
200 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
201 QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
202 QString filter( const QgsFields &fields = QgsFields() ) override;
203 QgsSymbolList symbols( QgsRenderContext &context ) const override;
204 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
205
212 void updateSymbols( QgsSymbol *sym );
213
217 const QgsCategoryList &categories() const { return mCategories; }
218
222 int categoryIndexForValue( const QVariant &val );
223
227 int categoryIndexForLabel( const QString &val );
228
238 bool updateCategoryValue( int catIndex, const QVariant &value );
239
249 bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
250
261 bool updateCategoryLabel( int catIndex, const QString &label );
262
274 bool updateCategoryRenderState( int catIndex, bool render );
275
281 void addCategory( const QgsRendererCategory &category );
282
288 bool deleteCategory( int catIndex );
289
295 void deleteAllCategories();
296
300 void moveCategory( int from, int to );
301
307 void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
308
314 void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
315
323 QString classAttribute() const { return mAttrName; }
324
332 void setClassAttribute( const QString &attr ) { mAttrName = attr; }
333
337 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
338
339 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
340 QgsLegendSymbolList legendSymbolItems() const override;
341 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
342 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
343
350 QgsSymbol *sourceSymbol();
351
359 const QgsSymbol *sourceSymbol() const SIP_SKIP;
360
368 void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
369
375 QgsColorRamp *sourceColorRamp();
376
383 const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
384
391 void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
392
397 void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
398
399 bool legendSymbolItemsCheckable() const override;
400 bool legendSymbolItemChecked( const QString &key ) override;
401 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
402 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
403 QString legendClassificationAttribute() const override { return classAttribute(); }
404
412 static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
413
423 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
424
429 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
430
448 int matchToSymbols( QgsStyle *style, Qgis::SymbolType type,
449 QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
450
451
462 static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
463
473 static QString displayString( const QVariant &value, int precision = -1 );
474
475
476 protected:
477 QString mAttrName;
478 QgsCategoryList mCategories;
479 std::unique_ptr<QgsSymbol> mSourceSymbol;
480 std::unique_ptr<QgsColorRamp> mSourceColorRamp;
481 std::unique_ptr<QgsExpression> mExpression;
482
483 std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
484
486 int mAttrNum = -1;
487
489 QHash<QString, QgsSymbol *> mSymbolHash;
490 bool mCounting = false;
491
492 void rebuildHash();
493
497 Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
498
503 Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
504
505 // TODO QGIS 4.0 - rename Python method to symbolForValue
506
520 QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
521
522 private:
523#ifdef SIP_RUN
526#endif
527
529 QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
530
532 QgsLegendSymbolList baseLegendSymbolItems() const;
533
534};
535
536#endif // QGSCATEGORIZEDSYMBOLRENDERER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:54
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:802
A widget for configuring a QgsCategorizedSymbolRenderer.
A feature renderer which represents features using a list of renderer categories.
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
void setClassAttribute(const QString &attr)
Sets the class attribute for the renderer, which is the field name or expression string from the laye...
~QgsCategorizedSymbolRenderer() override
Abstract base class for color ramps.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Handles parsing and evaluation of expressions (formerly called "search strings").
Abstract base class for all 2D vector feature renderers.
QFlags< Capability > Capabilities
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Container of fields for a vector layer.
Definition qgsfields.h:46
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
std::unique_ptr< QgsSymbol > mSymbol
QgsRendererCategory()=default
Holds SLD export options and other information related to SLD export of a QGIS layer style.
An interface for classes which can visit style entity (e.g.
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:88
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:76
QList< QgsRendererCategory > QgsCategoryList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:48
int precision