QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgsconditionalstyle.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsconditionalstyle.h
3 ---------------------
4 begin : August 2015
5 copyright : (C) 2015 by Nathan Woodrow
6 email : woodrow dot nathan 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 QGSCONDITIONALSTYLE_H
16#define QGSCONDITIONALSTYLE_H
17
18#include <memory>
19
20#include "qgis_core.h"
21#include "qgsfield.h"
22
23#include <QColor>
24#include <QDomDocument>
25#include <QDomNode>
26#include <QFont>
27#include <QHash>
28#include <QObject>
29#include <QPixmap>
30#include <QString>
31
32using namespace Qt::StringLiterals;
33
37class QgsSymbol;
38
39typedef QList<QgsConditionalStyle> QgsConditionalStyles;
40
48class CORE_EXPORT QgsConditionalLayerStyles : public QObject
49{
50 Q_OBJECT
51
52 public:
53
57 QgsConditionalLayerStyles( QObject *parent = nullptr );
58
65
72 void setRowStyles( const QgsConditionalStyles &styles );
73
80
89 void setFieldStyles( const QString &fieldName, const QList<QgsConditionalStyle> &styles );
90
96 QList<QgsConditionalStyle> fieldStyles( const QString &fieldName ) const;
97
103 bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
104
110 bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
111
116 bool rulesNeedGeometry() const;
117
118 signals:
119
125 void changed();
126
127 private:
128 QHash<QString, QgsConditionalStyles> mFieldStyles;
129 QgsConditionalStyles mRowStyles;
130};
131
137class CORE_EXPORT QgsConditionalStyle
138{
139 public:
143 QgsConditionalStyle( const QString &rule );
145
148
155 bool matches( const QVariant &value, QgsExpressionContext &context ) const;
156
164 QPixmap renderPreview( const QSize &size = QSize() ) const;
165
170 void setName( const QString &value ) { mName = value; mValid = true; }
171
177 void setRule( const QString &value ) { mRule = value; mValid = true; }
178
183 void setBackgroundColor( const QColor &value ) { mBackColor = value; mValid = true; }
184
189 void setTextColor( const QColor &value ) { mTextColor = value; mValid = true; }
190
195 void setFont( const QFont &value ) { mFont = value; mValid = true; }
196
201 void setSymbol( QgsSymbol *value );
202
207 QString displayText() const;
208
213 QString name() const { return mName; }
214
219 QPixmap icon() const { return mIcon; }
220
225 QgsSymbol *symbol() const { return mSymbol.get(); }
226
231 QColor textColor() const { return mTextColor; }
232
238 bool validTextColor() const;
239
244 QColor backgroundColor() const { return mBackColor; }
245
251 bool validBackgroundColor() const;
252
257 QFont font() const { return mFont; }
258
264 QString rule() const { return mRule; }
265
271 bool isValid() const { return mValid; }
272
280 static QList<QgsConditionalStyle> matchingConditionalStyles( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
281
289 static QgsConditionalStyle matchingConditionalStyle( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
290
297 static QgsConditionalStyle compressStyles( const QList<QgsConditionalStyle> &styles );
298
302 bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
303
307 bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
308
309 bool operator==( const QgsConditionalStyle &other ) const;
310 bool operator!=( const QgsConditionalStyle &other ) const;
311
312#ifdef SIP_RUN
313 SIP_PYOBJECT __repr__();
314 % MethodCode
315 QString str;
316 if ( !sipCpp->name().isEmpty() )
317 str = u"<QgsConditionalStyle: '%1' (%2)>"_s.arg( sipCpp->name(), sipCpp->rule() );
318 else
319 str = u"<QgsConditionalStyle: %2>"_s.arg( sipCpp->rule() );
320 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
321 % End
322#endif
323
324 private:
325
326 bool mValid = false;
327 QString mName;
328 QString mRule;
329 std::unique_ptr<QgsSymbol> mSymbol;
330 QFont mFont;
331 QColor mBackColor;
332 QColor mTextColor;
333 QPixmap mIcon;
334};
335
336#endif // QGSCONDITIONALSTYLE_H
void changed()
Emitted when the conditional styles are changed.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads the condition styles state from a DOM node.
QgsConditionalStyles rowStyles() const
Returns a list of row styles associated with the layer.
QgsConditionalLayerStyles(QObject *parent=nullptr)
Constructor for QgsConditionalLayerStyles, with the specified parent object.
QgsConditionalStyle constraintFailureStyles(QgsFieldConstraints::ConstraintStrength strength)
Returns a style associated to a constraint failure.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes the condition styles state to a DOM node.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName) const
Returns the conditional styles set for the field with matching fieldName.
bool rulesNeedGeometry() const
Returns true if at least one rule needs geometry.
void setFieldStyles(const QString &fieldName, const QList< QgsConditionalStyle > &styles)
Set the conditional styles for a field, with the specified fieldName.
void setRowStyles(const QgsConditionalStyles &styles)
Sets the conditional styles that apply to full rows of data in the attribute table.
Conditional styling for a rule.
QString name() const
The name of the style.
void setName(const QString &value)
Set the name of the style.
QPixmap renderPreview(const QSize &size=QSize()) const
Render a preview icon of the rule, at the specified size.
bool matches(const QVariant &value, QgsExpressionContext &context) const
Check if the rule matches using the given value and feature.
QgsConditionalStyle & operator=(const QgsConditionalStyle &other)
void setTextColor(const QColor &value)
Set the text color for the style.
void setRule(const QString &value)
Set the rule for the style.
void setBackgroundColor(const QColor &value)
Set the background color for the style.
void setFont(const QFont &value)
Set the font for the style.
QColor backgroundColor() const
The background color for style.
QColor textColor() const
The text color set for style.
QString rule() const
The condition rule set for the style.
QFont font() const
The font for the style.
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
bool isValid() const
isValid Check if this rule is valid.
QPixmap icon() const
The icon set for style generated from the set symbol.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
ConstraintStrength
Strength of constraints.
A container for the context for various read/write operations on objects.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
#define SIP_SKIP
Definition qgis_sip.h:134
QList< QgsConditionalStyle > QgsConditionalStyles
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)