QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
43
57 QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true, const QString &uuid = QString() );
58
65
70 QString uuid() const;
71
79 QVariant value() const;
80
85 QgsSymbol *symbol() const;
86
92 QString label() const;
93
101 void setValue( const QVariant &value );
102
110 void setSymbol( QgsSymbol *s SIP_TRANSFER );
111
117 void setLabel( const QString &label );
118
124 bool renderState() const;
125
131 void setRenderState( bool render );
132
133 // debugging
134
138 QString dump() const;
139
143 void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
144
145#ifdef SIP_RUN
146 SIP_PYOBJECT __repr__();
147 % MethodCode
148 const QString str = !sipCpp->value().isValid()
149 ? QStringLiteral( "<QgsRendererCategory>" )
150 : sipCpp->label().isEmpty()
151 ? QStringLiteral( "<QgsRendererCategory: %1>" ).arg( sipCpp->value().toString() )
152 : QStringLiteral( "<QgsRendererCategory: %1 (%2)>" ).arg( sipCpp->value().toString(), sipCpp->label() );
153 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
154 % End
155#endif
156
157 protected:
158 QVariant mValue;
159 std::unique_ptr<QgsSymbol> mSymbol;
160 QString mLabel;
161 bool mRender = true;
162 QString mUuid;
163
164 void swap( QgsRendererCategory &other );
165};
166
167typedef QList<QgsRendererCategory> QgsCategoryList;
168
174{
175 public:
176
185 QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
187
188 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
189 QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
190 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
191 void stopRender( QgsRenderContext &context ) override;
192 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
193 bool filterNeedsGeometry() const override;
194 QString dump() const override;
195 QgsCategorizedSymbolRenderer *clone() const override SIP_FACTORY;
196 void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
197 QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
198 QString filter( const QgsFields &fields = QgsFields() ) override;
199 QgsSymbolList symbols( QgsRenderContext &context ) const override;
200 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
201
208 void updateSymbols( QgsSymbol *sym );
209
213 const QgsCategoryList &categories() const { return mCategories; }
214
218 int categoryIndexForValue( const QVariant &val );
219
224 int categoryIndexForLabel( const QString &val );
225
235 bool updateCategoryValue( int catIndex, const QVariant &value );
236
246 bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
247
258 bool updateCategoryLabel( int catIndex, const QString &label );
259
272 bool updateCategoryRenderState( int catIndex, bool render );
273
279 void addCategory( const QgsRendererCategory &category );
280
286 bool deleteCategory( int catIndex );
287
293 void deleteAllCategories();
294
298 void moveCategory( int from, int to );
299
305 void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
306
312 void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
313
321 QString classAttribute() const { return mAttrName; }
322
330 void setClassAttribute( const QString &attr ) { mAttrName = attr; }
331
335 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
336
337 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
338 QgsLegendSymbolList legendSymbolItems() const override;
339 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
340 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
341
348 QgsSymbol *sourceSymbol();
349
357 const QgsSymbol *sourceSymbol() const SIP_SKIP;
358
366 void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
367
373 QgsColorRamp *sourceColorRamp();
374
381 const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
382
389 void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
390
396 void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
397
398 bool legendSymbolItemsCheckable() const override;
399 bool legendSymbolItemChecked( const QString &key ) override;
400 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
401 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
402 QString legendClassificationAttribute() const override { return classAttribute(); }
403
412 static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
413
424 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
425
431 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
432
450 int matchToSymbols( QgsStyle *style, Qgis::SymbolType type,
451 QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
452
453
464 static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
465
475 static QString displayString( const QVariant &value, int precision = -1 );
476
477
478 protected:
479 QString mAttrName;
480 QgsCategoryList mCategories;
481 std::unique_ptr<QgsSymbol> mSourceSymbol;
482 std::unique_ptr<QgsColorRamp> mSourceColorRamp;
483 std::unique_ptr<QgsExpression> mExpression;
484
485 std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
486
488 int mAttrNum = -1;
489
491 QHash<QString, QgsSymbol *> mSymbolHash;
492 bool mCounting = false;
493
494 void rebuildHash();
495
499 Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
500
505 Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
506
507 // TODO QGIS 4.0 - rename Python method to symbolForValue
508
522 QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
523
524 private:
525#ifdef SIP_RUN
528#endif
529
531 QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
532
534 QgsLegendSymbolList baseLegendSymbolItems() const;
535
536};
537
538#endif // QGSCATEGORIZEDSYMBOLRENDERER_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
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").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
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
Constructor for QgsRendererCategory.
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:94
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:44
int precision