QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
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
135 void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
136
137#ifdef SIP_RUN
138 SIP_PYOBJECT __repr__();
139 % MethodCode
140 const QString str = !sipCpp->value().isValid()
141 ? QStringLiteral( "<QgsRendererCategory>" )
142 : sipCpp->label().isEmpty()
143 ? QStringLiteral( "<QgsRendererCategory: %1>" ).arg( sipCpp->value().toString() )
144 : QStringLiteral( "<QgsRendererCategory: %1 (%2)>" ).arg( sipCpp->value().toString(), sipCpp->label() );
145 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
146 % End
147#endif
148
149 protected:
150 QVariant mValue;
151 std::unique_ptr<QgsSymbol> mSymbol;
152 QString mLabel;
153 bool mRender = true;
154 QString mUuid;
155
156 void swap( QgsRendererCategory &other );
157};
158
159typedef QList<QgsRendererCategory> QgsCategoryList;
160
166{
167 public:
168
177 QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
179
180 Qgis::FeatureRendererFlags flags() const override;
181 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
182 QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
183 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
184 void stopRender( QgsRenderContext &context ) override;
185 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
186 bool filterNeedsGeometry() const override;
187 QString dump() const override;
188 QgsCategorizedSymbolRenderer *clone() const override SIP_FACTORY;
189 void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
190 QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
191 QString filter( const QgsFields &fields = QgsFields() ) override;
192 QgsSymbolList symbols( QgsRenderContext &context ) const override;
193 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
194
201 void updateSymbols( QgsSymbol *sym );
202
206 const QgsCategoryList &categories() const { return mCategories; }
207
211 int categoryIndexForValue( const QVariant &val );
212
216 int categoryIndexForLabel( const QString &val );
217
227 bool updateCategoryValue( int catIndex, const QVariant &value );
228
238 bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
239
250 bool updateCategoryLabel( int catIndex, const QString &label );
251
263 bool updateCategoryRenderState( int catIndex, bool render );
264
270 void addCategory( const QgsRendererCategory &category );
271
277 bool deleteCategory( int catIndex );
278
284 void deleteAllCategories();
285
289 void moveCategory( int from, int to );
290
296 void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
297
303 void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
304
312 QString classAttribute() const { return mAttrName; }
313
321 void setClassAttribute( const QString &attr ) { mAttrName = attr; }
322
326 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
327
328 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
329 QgsLegendSymbolList legendSymbolItems() const override;
330 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
331 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
332
339 QgsSymbol *sourceSymbol();
340
348 const QgsSymbol *sourceSymbol() const SIP_SKIP;
349
357 void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
358
364 QgsColorRamp *sourceColorRamp();
365
372 const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
373
380 void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
381
386 void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
387
388 bool legendSymbolItemsCheckable() const override;
389 bool legendSymbolItemChecked( const QString &key ) override;
390 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
391 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
392 QString legendClassificationAttribute() const override { return classAttribute(); }
393
401 static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
402
412 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
413
418 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
419
437 int matchToSymbols( QgsStyle *style, Qgis::SymbolType type,
438 QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
439
440
451 static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
452
462 static QString displayString( const QVariant &value, int precision = -1 );
463
464
465 protected:
466 QString mAttrName;
467 QgsCategoryList mCategories;
468 std::unique_ptr<QgsSymbol> mSourceSymbol;
469 std::unique_ptr<QgsColorRamp> mSourceColorRamp;
470 std::unique_ptr<QgsExpression> mExpression;
471
472 std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
473
475 int mAttrNum = -1;
476
478 QHash<QString, QgsSymbol *> mSymbolHash;
479 bool mCounting = false;
480
481 void rebuildHash();
482
486 Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
487
492 Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
493
494 // TODO QGIS 4.0 - rename Python method to symbolForValue
495
509 QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
510
511 private:
512#ifdef SIP_RUN
515#endif
516
518 QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
519
521 QgsLegendSymbolList baseLegendSymbolItems() const;
522
523};
524
525#endif // QGSCATEGORIZEDSYMBOLRENDERER_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:772
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.
Class for 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
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.
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
std::unique_ptr< QgsSymbol > mSymbol
QgsRendererCategory()=default
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Represents a vector layer which manages a vector based data sets.
#define str(x)
Definition qgis.cpp:38
#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:47
int precision