QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include <QHash>
25 #include <memory>
26 
30 class QgsSymbol;
31 
32 typedef QList<QgsConditionalStyle> QgsConditionalStyles;
33 
34 
40 class CORE_EXPORT QgsConditionalLayerStyles : public QObject
41 {
42  Q_OBJECT
43 
44  public:
45 
49  QgsConditionalLayerStyles( QObject *parent = nullptr );
50 
56  QgsConditionalStyles rowStyles() const;
57 
65  void setRowStyles( const QgsConditionalStyles &styles );
66 
72  void setFieldStyles( const QString &fieldName, const QList<QgsConditionalStyle> &styles );
73 
79  QList<QgsConditionalStyle> fieldStyles( const QString &fieldName ) const;
80 
86  bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
87 
93  bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
94 
99  bool rulesNeedGeometry() const;
100 
101  signals:
102 
108  void changed();
109 
110  private:
111  QHash<QString, QgsConditionalStyles> mFieldStyles;
112  QgsConditionalStyles mRowStyles;
113 };
114 
120 class CORE_EXPORT QgsConditionalStyle
121 {
122  public:
125  QgsConditionalStyle( const QString &rule );
127 
128  QgsConditionalStyle &operator=( const QgsConditionalStyle &other );
129 
136  bool matches( const QVariant &value, QgsExpressionContext &context ) const;
137 
145  QPixmap renderPreview( const QSize &size = QSize() ) const;
146 
151  void setName( const QString &value ) { mName = value; mValid = true; }
152 
158  void setRule( const QString &value ) { mRule = value; mValid = true; }
159 
164  void setBackgroundColor( const QColor &value ) { mBackColor = value; mValid = true; }
165 
170  void setTextColor( const QColor &value ) { mTextColor = value; mValid = true; }
171 
176  void setFont( const QFont &value ) { mFont = value; mValid = true; }
177 
182  void setSymbol( QgsSymbol *value );
183 
188  QString displayText() const;
189 
194  QString name() const { return mName; }
195 
200  QPixmap icon() const { return mIcon; }
201 
206  QgsSymbol *symbol() const { return mSymbol.get(); }
207 
212  QColor textColor() const { return mTextColor; }
213 
219  bool validTextColor() const;
220 
225  QColor backgroundColor() const { return mBackColor; }
226 
232  bool validBackgroundColor() const;
233 
238  QFont font() const { return mFont; }
239 
245  QString rule() const { return mRule; }
246 
252  bool isValid() const { return mValid; }
253 
261  static QList<QgsConditionalStyle> matchingConditionalStyles( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
262 
270  static QgsConditionalStyle matchingConditionalStyle( const QList<QgsConditionalStyle> &styles, const QVariant &value, QgsExpressionContext &context );
271 
278  static QgsConditionalStyle compressStyles( const QList<QgsConditionalStyle> &styles );
279 
283  bool readXml( const QDomNode &node, const QgsReadWriteContext &context );
284 
288  bool writeXml( QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context ) const;
289 
290  bool operator==( const QgsConditionalStyle &other ) const;
291  bool operator!=( const QgsConditionalStyle &other ) const;
292 
293 #ifdef SIP_RUN
294  SIP_PYOBJECT __repr__();
295  % MethodCode
296  QString str;
297  if ( !sipCpp->name().isEmpty() )
298  str = QStringLiteral( "<QgsConditionalStyle: '%1' (%2)>" ).arg( sipCpp->name(), sipCpp->rule() );
299  else
300  str = QStringLiteral( "<QgsConditionalStyle: %2>" ).arg( sipCpp->rule() );
301  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
302  % End
303 #endif
304 
305  private:
306 
307  bool mValid = false;
308  QString mName;
309  QString mRule;
310  std::unique_ptr<QgsSymbol> mSymbol;
311  QFont mFont;
312  QColor mBackColor;
313  QColor mTextColor;
314  QPixmap mIcon;
315 };
316 
317 #endif // QGSCONDITIONALSTYLE_H
QgsConditionalStyles
QList< QgsConditionalStyle > QgsConditionalStyles
Definition: qgsconditionalstyle.h:30
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsConditionalStyle::symbol
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
Definition: qgsconditionalstyle.h:206
QgsConditionalStyle::isValid
bool isValid() const
isValid Check if this rule is valid.
Definition: qgsconditionalstyle.h:252
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
QgsConditionalStyle::setBackgroundColor
void setBackgroundColor(const QColor &value)
Set the background color for the style.
Definition: qgsconditionalstyle.h:164
QgsConditionalStyle::setTextColor
void setTextColor(const QColor &value)
Set the text color for the style.
Definition: qgsconditionalstyle.h:170
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsConditionalLayerStyles
The QgsConditionalLayerStyles class holds conditional style information for a layer....
Definition: qgsconditionalstyle.h:40
QgsConditionalStyle
Conditional styling for a rule.
Definition: qgsconditionalstyle.h:120
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:430
QgsConditionalStyle::setRule
void setRule(const QString &value)
Set the rule for the style.
Definition: qgsconditionalstyle.h:158
QgsConditionalStyle::icon
QPixmap icon() const
The icon set for style generated from the set symbol.
Definition: qgsconditionalstyle.h:200
QgsConditionalStyle::rule
QString rule() const
The condition rule set for the style.
Definition: qgsconditionalstyle.h:245
QgsConditionalStyle::backgroundColor
QColor backgroundColor() const
The background color for style.
Definition: qgsconditionalstyle.h:225
QgsConditionalStyle::setName
void setName(const QString &value)
Set the name of the style.
Definition: qgsconditionalstyle.h:151
QgsConditionalStyle::name
QString name() const
The name of the style.
Definition: qgsconditionalstyle.h:194
QgsConditionalStyle::font
QFont font() const
The font for the style.
Definition: qgsconditionalstyle.h:238
QgsConditionalStyle::textColor
QColor textColor() const
The text color set for style.
Definition: qgsconditionalstyle.h:212
str
#define str(x)
Definition: qgis.cpp:37
QgsConditionalStyle::setFont
void setFont(const QFont &value)
Set the font for the style.
Definition: qgsconditionalstyle.h:176