QGIS API Documentation 4.1.0-Master (60fea48833c)
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:
56 QgsConditionalLayerStyles( QObject *parent = nullptr );
57
64
71 void setRowStyles( const QgsConditionalStyles &styles );
72
79
88 void setFieldStyles( const QString &fieldName, const QList<QgsConditionalStyle> &styles );
89
95 QList<QgsConditionalStyle> fieldStyles( const QString &fieldName ) const;
96
102 bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
103
109 bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
110
115 bool rulesNeedGeometry() const;
116
117 signals:
118
124 void changed();
125
126 private:
127 QHash<QString, QgsConditionalStyles> mFieldStyles;
128 QgsConditionalStyles mRowStyles;
129};
130
136class CORE_EXPORT QgsConditionalStyle
137{
138 public:
142 QgsConditionalStyle( const QString &rule );
144
147
154 bool matches( const QVariant &value, QgsExpressionContext &context ) const;
155
163 QPixmap renderPreview( const QSize &size = QSize() ) const;
164
169 void setName( const QString &value )
170 {
171 mName = value;
172 mValid = true;
173 }
174
180 void setRule( const QString &value )
181 {
182 mRule = value;
183 mValid = true;
184 }
185
190 void setBackgroundColor( const QColor &value )
191 {
192 mBackColor = value;
193 mValid = true;
194 }
195
200 void setTextColor( const QColor &value )
201 {
202 mTextColor = value;
203 mValid = true;
204 }
205
210 void setFont( const QFont &value )
211 {
212 mFont = value;
213 mValid = true;
214 }
215
220 void setSymbol( QgsSymbol *value );
221
226 QString displayText() const;
227
232 QString name() const { return mName; }
233
238 QPixmap icon() const { return mIcon; }
239
244 QgsSymbol *symbol() const { return mSymbol.get(); }
245
250 QColor textColor() const { return mTextColor; }
251
257 bool validTextColor() const;
258
263 QColor backgroundColor() const { return mBackColor; }
264
270 bool validBackgroundColor() const;
271
276 QFont font() const { return mFont; }
277
283 QString rule() const { return mRule; }
284
290 bool isValid() const { return mValid; }
291
299 static QList<QgsConditionalStyle> matchingConditionalStyles( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
300
308 static QgsConditionalStyle matchingConditionalStyle( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
309
316 static QgsConditionalStyle compressStyles( const QList<QgsConditionalStyle> &styles );
317
321 bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
322
326 bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
327
328 bool operator==( const QgsConditionalStyle &other ) const;
329 bool operator!=( const QgsConditionalStyle &other ) const;
330
331#ifdef SIP_RUN
332 // clang-format off
333 SIP_PYOBJECT __repr__();
334 % MethodCode
335 QString str;
336 if ( !sipCpp->name().isEmpty() )
337 str = u"<QgsConditionalStyle: '%1' (%2)>"_s.arg( sipCpp->name(), sipCpp->rule() );
338 else
339 str = u"<QgsConditionalStyle: %2>"_s.arg( sipCpp->rule() );
340 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
341 % End
342// clang-format on
343#endif
344
345 // clang-format off
346 private:
347 // clang-format on
348
349 bool mValid
350 = false;
351 QString mName;
352 QString mRule;
353 std::unique_ptr<QgsSymbol> mSymbol;
354 QFont mFont;
355 QColor mBackColor;
356 QColor mTextColor;
357 QPixmap mIcon;
358};
359
360#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:227
#define SIP_SKIP
Definition qgis_sip.h:133
QList< QgsConditionalStyle > QgsConditionalStyles
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)