QGIS API Documentation 3.99.0-Master (d270888f95f)
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.h"
19#include "qgis_core.h"
20#include "qgsrenderer.h"
21
22#include <QHash>
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27class QgsVectorLayer;
28class QgsStyle;
30class QgsSymbol;
31class QgsExpression;
32class QgsColorRamp;
33
38class CORE_EXPORT QgsRendererCategory
39{
40 public:
41
43
57 QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true, const QString &uuid = QString() );
58
62
67 QString uuid() const;
68
76 QVariant value() const;
77
82 QgsSymbol *symbol() const;
83
89 QString label() const;
90
98 void setValue( const QVariant &value );
99
108
114 void setLabel( const QString &label );
115
120 bool renderState() const;
121
126 void setRenderState( bool render );
127
128 // debugging
129
133 QString dump() const;
134
140 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const SIP_DEPRECATED;
141
147 bool toSld( QDomDocument &doc, QDomElement &element, const QString &classAttribute, QgsSldExportContext &context ) const;
148
149#ifdef SIP_RUN
150 SIP_PYOBJECT __repr__();
151 % MethodCode
152 const QString str = !sipCpp->value().isValid()
153 ? u"<QgsRendererCategory>"_s
154 : sipCpp->label().isEmpty()
155 ? u"<QgsRendererCategory: %1>"_s.arg( sipCpp->value().toString() )
156 : u"<QgsRendererCategory: %1 (%2)>"_s.arg( sipCpp->value().toString(), sipCpp->label() );
157 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
158 % End
159#endif
160
161 protected:
163
164 QVariant mValue;
165 std::unique_ptr<QgsSymbol> mSymbol;
166 QString mLabel;
167 bool mRender = true;
168 QString mUuid;
169};
170
171typedef QList<QgsRendererCategory> QgsCategoryList;
172
179{
180 public:
181
190 QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
192
193 Qgis::FeatureRendererFlags flags() const override;
194 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
195 QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
196 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
197 void stopRender( QgsRenderContext &context ) override;
198 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
199 bool filterNeedsGeometry() const override;
200 QString dump() const override;
202 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
203 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
205 QString filter( const QgsFields &fields = QgsFields() ) override;
206 QgsSymbolList symbols( QgsRenderContext &context ) const override;
207 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
208
215 void updateSymbols( QgsSymbol *sym );
216
220 const QgsCategoryList &categories() const { return mCategories; }
221
225 int categoryIndexForValue( const QVariant &val );
226
230 int categoryIndexForLabel( const QString &val );
231
241 bool updateCategoryValue( int catIndex, const QVariant &value );
242
252 bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
253
264 bool updateCategoryLabel( int catIndex, const QString &label );
265
277 bool updateCategoryRenderState( int catIndex, bool render );
278
284 void addCategory( const QgsRendererCategory &category );
285
291 bool deleteCategory( int catIndex );
292
298 void deleteAllCategories();
299
303 void moveCategory( int from, int to );
304
310 void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
311
317 void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
318
326 QString classAttribute() const { return mAttrName; }
327
335 void setClassAttribute( const QString &attr ) { mAttrName = attr; }
336
340 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
341
342 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
343 QgsLegendSymbolList legendSymbolItems() const override;
344 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
345 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
346
353 QgsSymbol *sourceSymbol();
354
362 const QgsSymbol *sourceSymbol() const SIP_SKIP;
363
371 void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
372
378 QgsColorRamp *sourceColorRamp();
379
386 const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
387
394 void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
395
400 void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
401
402 bool legendSymbolItemsCheckable() const override;
403 bool legendSymbolItemChecked( const QString &key ) override;
404 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
405 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
406 QString legendClassificationAttribute() const override { return classAttribute(); }
407
415 static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
416
426 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
427
432 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
433
451 int matchToSymbols( QgsStyle *style, Qgis::SymbolType type,
452 QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
453
454
465 static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
466
477 Q_DECL_DEPRECATED static QString displayString( const QVariant &value, int precision = -1 ) SIP_DEPRECATED;
478
479
480 protected:
481 QString mAttrName;
483 std::unique_ptr<QgsSymbol> mSourceSymbol;
485 std::unique_ptr<QgsExpression> mExpression;
486
488
490 int mAttrNum = -1;
491
493 QHash<QString, QgsSymbol *> mSymbolHash;
494 bool mCounting = false;
495
496 void rebuildHash();
497
501 Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
502
507 Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
508
509 // TODO QGIS 5.0 - rename Python method to symbolForValue
510
524 QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
525
526 private:
527#ifdef SIP_RUN
530#endif
531
533 QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
534
536 QgsLegendSymbolList baseLegendSymbolItems() const;
537
538};
539
540#endif // QGSCATEGORIZEDSYMBOLRENDERER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:857
A feature renderer which represents features using a list of renderer categories.
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
To be overridden.
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Q_DECL_DEPRECATED QgsSymbol * symbolForValue(const QVariant &value) const
Returns the matching symbol corresponding to an attribute value.
std::unique_ptr< QgsSymbol > mSourceSymbol
QHash< QString, QgsSymbol * > mSymbolHash
hashtable for faster access to symbols
bool filterNeedsGeometry() const override
Returns true if this renderer requires the geometry to apply the filter.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
std::unique_ptr< QgsExpression > mExpression
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
QgsCategorizedSymbolRenderer(const QString &attrName=QString(), const QgsCategoryList &categories=QgsCategoryList())
Constructor for QgsCategorizedSymbolRenderer.
QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns symbol for feature.
int mAttrNum
attribute index (derived from attribute name in startRender)
Qgis::FeatureRendererFlags flags() const override
Returns flags associated with the renderer.
Q_DECL_DEPRECATED QgsSymbol * skipRender()
QString dump() const override
Returns debug information about this renderer.
Q_DECL_DEPRECATED 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.
QgsCategorizedSymbolRenderer * clone() const override
Create a deep copy of this renderer.
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
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
QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
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.
QgsFeatureRenderer(const QString &type)
QFlags< Capability > Capabilities
@ SymbolLevels
Rendering with symbol levels (i.e. implements symbols(), symbolForFeature()).
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
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.
void setRenderState(bool render)
Sets whether the category is currently enabled and should be rendered.
std::unique_ptr< QgsSymbol > mSymbol
QgsSymbol * symbol() const
Returns the symbol which will be used to render this category.
void setSymbol(QgsSymbol *s)
Sets the symbol which will be used to render this category.
QString uuid() const
Returns the unique identifier for this category.
QgsRendererCategory()=default
bool renderState() const
Returns true if the category is currently enabled and should be rendered.
QString dump() const
Returns a string representing the categories settings, used for debugging purposes only.
void setLabel(const QString &label)
Sets the label for this category, which is used to represent the category within legends and the laye...
Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, QVariantMap props) const
Converts the category to a matching SLD rule, within the specified DOM document and element.
void setValue(const QVariant &value)
Sets the value corresponding to this category.
QVariant value() const
Returns the value corresponding to this category.
QString label() const
Returns the label for this category, which is used to represent the category within legends and the l...
QgsRendererCategory & operator=(QgsRendererCategory cat)
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:89
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:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYNAME(name)
Definition qgis_sip.h:89
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:84
QList< QgsRendererCategory > QgsCategoryList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:51