QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
42
56 QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true, const QString &uuid = QString() );
57
61
66 QString uuid() const;
67
75 QVariant value() const;
76
81 QgsSymbol *symbol() const;
82
88 QString label() const;
89
97 void setValue( const QVariant &value );
98
107
113 void setLabel( const QString &label );
114
119 bool renderState() const;
120
125 void setRenderState( bool render );
126
127 // debugging
128
132 QString dump() const;
133
139 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const SIP_DEPRECATED;
140
146 bool toSld( QDomDocument &doc, QDomElement &element, const QString &classAttribute, QgsSldExportContext &context ) const;
147
148#ifdef SIP_RUN
149 // clang-format off
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// clang-format on
160#endif
161
162 // clang-format off
163 protected:
164 // clang-format on
166
167 QVariant mValue;
168 std::unique_ptr<QgsSymbol> mSymbol;
169 QString mLabel;
170 bool mRender = true;
171 QString mUuid;
172};
173
174typedef QList<QgsRendererCategory> QgsCategoryList;
175
182{
183 public:
192 QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
194
195 Qgis::FeatureRendererFlags flags() const override;
196 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
197 QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
198 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
199 void stopRender( QgsRenderContext &context ) override;
200 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
201 bool filterNeedsGeometry() const override;
202 QString dump() const override;
204 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
205 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
207 QString filter( const QgsFields &fields = QgsFields() ) override;
208 QgsSymbolList symbols( QgsRenderContext &context ) const override;
209 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
210
217 void updateSymbols( QgsSymbol *sym );
218
222 const QgsCategoryList &categories() const { return mCategories; }
223
227 int categoryIndexForValue( const QVariant &val );
228
232 int categoryIndexForLabel( const QString &val );
233
243 bool updateCategoryValue( int catIndex, const QVariant &value );
244
254 bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
255
266 bool updateCategoryLabel( int catIndex, const QString &label );
267
279 bool updateCategoryRenderState( int catIndex, bool render );
280
286 void addCategory( const QgsRendererCategory &category );
287
293 bool deleteCategory( int catIndex );
294
300 void deleteAllCategories();
301
305 void moveCategory( int from, int to );
306
312 void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
313
319 void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
320
328 QString classAttribute() const { return mAttrName; }
329
337 void setClassAttribute( const QString &attr ) { mAttrName = attr; }
338
342 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
343
344 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
345 QgsLegendSymbolList legendSymbolItems() const override;
346 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
347 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
348
355 QgsSymbol *sourceSymbol();
356
364 const QgsSymbol *sourceSymbol() const SIP_SKIP;
365
373 void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
374
380 QgsColorRamp *sourceColorRamp();
381
388 const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
389
396 void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
397
402 void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
403
404 bool legendSymbolItemsCheckable() const override;
405 bool legendSymbolItemChecked( const QString &key ) override;
406 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
407 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
408 QString legendClassificationAttribute() const override { return classAttribute(); }
409
417 static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
418
428 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
429
434 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
435
453 int matchToSymbols( QgsStyle *style, Qgis::SymbolType type, QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
454
455
466 static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
467
478 Q_DECL_DEPRECATED static QString displayString( const QVariant &value, int precision = -1 ) SIP_DEPRECATED;
479
480
481 protected:
482 QString mAttrName;
484 std::unique_ptr<QgsSymbol> mSourceSymbol;
486 std::unique_ptr<QgsExpression> mExpression;
487
489
491 int mAttrNum = -1;
492
494 QHash<QString, QgsSymbol *> mSymbolHash;
495 bool mCounting = false;
496
497 void rebuildHash();
498
502 Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
503
508 Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
509
510 // TODO QGIS 5.0 - rename Python method to symbolForValue
511
525 QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
526
527 private:
528#ifdef SIP_RUN
531#endif
532
534 QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
535
537 QgsLegendSymbolList baseLegendSymbolItems() const;
538};
539
540#endif // QGSCATEGORIZEDSYMBOLRENDERER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:864
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:227
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83
QList< QgsRendererCategory > QgsCategoryList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:51