QGIS API Documentation 4.1.0-Master (31622b25bb0)
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:
186
195 QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
197
198 Qgis::FeatureRendererFlags flags() const override;
199 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
200 QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
201 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
202 void stopRender( QgsRenderContext &context ) override;
203 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
204 bool filterNeedsGeometry() const override;
205 QString dump() const override;
207 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
208 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
210 QString filter( const QgsFields &fields = QgsFields() ) override;
211 QgsSymbolList symbols( QgsRenderContext &context ) const override;
212 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
213
220 void updateSymbols( QgsSymbol *sym );
221
225 const QgsCategoryList &categories() const { return mCategories; }
226
230 int categoryIndexForValue( const QVariant &val );
231
235 int categoryIndexForLabel( const QString &val );
236
246 bool updateCategoryValue( int catIndex, const QVariant &value );
247
257 bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
258
269 bool updateCategoryLabel( int catIndex, const QString &label );
270
282 bool updateCategoryRenderState( int catIndex, bool render );
283
289 void addCategory( const QgsRendererCategory &category );
290
296 bool deleteCategory( int catIndex );
297
303 void deleteAllCategories();
304
308 void moveCategory( int from, int to );
309
315 void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
316
322 void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
323
331 QString classAttribute() const { return mAttrName; }
332
340 void setClassAttribute( const QString &attr ) { mAttrName = attr; }
341
345 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
346
347 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
348 QgsLegendSymbolList legendSymbolItems() const override;
349 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
350 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
351
358 QgsSymbol *sourceSymbol();
359
367 const QgsSymbol *sourceSymbol() const SIP_SKIP;
368
376 void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
377
383 QgsColorRamp *sourceColorRamp();
384
391 const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
392
399 void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
400
405 void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
406
407 bool legendSymbolItemsCheckable() const override;
408 bool legendSymbolItemChecked( const QString &key ) override;
409 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
410 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
411 QString legendClassificationAttribute() const override { return classAttribute(); }
412
420 static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
421
431 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
432
437 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
438
456 int matchToSymbols( QgsStyle *style, Qgis::SymbolType type, QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
457
458
469 static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
470
481 Q_DECL_DEPRECATED static QString displayString( const QVariant &value, int precision = -1 ) SIP_DEPRECATED;
482
483
484 protected:
485 QString mAttrName;
487 std::unique_ptr<QgsSymbol> mSourceSymbol;
489 std::unique_ptr<QgsExpression> mExpression;
490
492
494 int mAttrNum = -1;
495
497 bool mAttrIsNumeric = false;
498
500 QHash<QString, QgsSymbol *> mSymbolHash;
501 bool mCounting = false;
502
503 void rebuildHash();
504
508 Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
509
514 Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
515
516 // TODO QGIS 5.0 - rename Python method to symbolForValue
517
531 QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
532
533 private:
534#ifdef SIP_RUN
537#endif
538
540 QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
541
543 QgsLegendSymbolList baseLegendSymbolItems() const;
544};
545
546#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:886
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.
bool mAttrIsNumeric
whether the attribute is numeric (derived from attribute name in startRender)
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:91
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