QGIS API Documentation  3.19.0-Master (c9e5875a2b)
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 "qgis_core.h"
19 #include <QFont>
20 #include <QColor>
21 #include <QPixmap>
22 #include <QDomNode>
23 #include <QDomDocument>
24 
25 #include "qgssymbol.h"
26 
29 
30 typedef QList<QgsConditionalStyle> QgsConditionalStyles;
31 
32 
38 class CORE_EXPORT QgsConditionalLayerStyles : public QObject
39 {
40  Q_OBJECT
41 
42  public:
43 
47  QgsConditionalLayerStyles( QObject *parent = nullptr );
48 
54  QgsConditionalStyles rowStyles() const;
55 
63  void setRowStyles( const QgsConditionalStyles &styles );
64 
70  void setFieldStyles( const QString &fieldName, const QList<QgsConditionalStyle> &styles );
71 
77  QList<QgsConditionalStyle> fieldStyles( const QString &fieldName ) const;
78 
84  bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
85 
91  bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
92 
93  signals:
94 
100  void changed();
101 
102  private:
103  QHash<QString, QgsConditionalStyles> mFieldStyles;
104  QgsConditionalStyles mRowStyles;
105 };
106 
112 class CORE_EXPORT QgsConditionalStyle
113 {
114  public:
117  QgsConditionalStyle( const QString &rule );
118 
119  QgsConditionalStyle &operator=( const QgsConditionalStyle &other );
120 
127  bool matches( const QVariant &value, QgsExpressionContext &context ) const;
128 
136  QPixmap renderPreview( const QSize &size = QSize() ) const;
137 
142  void setName( const QString &value ) { mName = value; mValid = true; }
143 
149  void setRule( const QString &value ) { mRule = value; mValid = true; }
150 
155  void setBackgroundColor( const QColor &value ) { mBackColor = value; mValid = true; }
156 
161  void setTextColor( const QColor &value ) { mTextColor = value; mValid = true; }
162 
167  void setFont( const QFont &value ) { mFont = value; mValid = true; }
168 
173  void setSymbol( QgsSymbol *value );
174 
179  QString displayText() const;
180 
185  QString name() const { return mName; }
186 
191  QPixmap icon() const { return mIcon; }
192 
197  QgsSymbol *symbol() const { return mSymbol.get(); }
198 
203  QColor textColor() const { return mTextColor; }
204 
210  bool validTextColor() const;
211 
216  QColor backgroundColor() const { return mBackColor; }
217 
223  bool validBackgroundColor() const;
224 
229  QFont font() const { return mFont; }
230 
236  QString rule() const { return mRule; }
237 
243  bool isValid() const { return mValid; }
244 
252  static QList<QgsConditionalStyle> matchingConditionalStyles( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
253 
261  static QgsConditionalStyle matchingConditionalStyle( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
262 
269  static QgsConditionalStyle compressStyles( const QList<QgsConditionalStyle> &styles );
270 
274  bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
275 
279  bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
280 
281  bool operator==( const QgsConditionalStyle &other ) const;
282  bool operator!=( const QgsConditionalStyle &other ) const;
283 
284 #ifdef SIP_RUN
285  SIP_PYOBJECT __repr__();
286  % MethodCode
287  QString str;
288  if ( !sipCpp->name().isEmpty() )
289  str = QStringLiteral( "<QgsConditionalStyle: '%1' (%2)>" ).arg( sipCpp->name(), sipCpp->rule() );
290  else
291  str = QStringLiteral( "<QgsConditionalStyle: %2>" ).arg( sipCpp->rule() );
292  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
293  % End
294 #endif
295 
296  private:
297 
298  bool mValid = false;
299  QString mName;
300  QString mRule;
301  std::unique_ptr<QgsSymbol> mSymbol;
302  QFont mFont;
303  QColor mBackColor;
304  QColor mTextColor;
305  QPixmap mIcon;
306 };
307 
308 #endif // QGSCONDITIONALSTYLE_H
The QgsConditionalLayerStyles class holds conditional style information for a layer.
void changed()
Emitted when the conditional styles are changed.
Conditional styling for a rule.
QString name() const
The name of the style.
void setName(const QString &value)
Set the name of the style.
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
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.
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...
The class is used as a container of context for various read/write operations on other objects.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:66
QList< QgsConditionalStyle > QgsConditionalStyles
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)