QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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
QgsConditionalStyles
QList< QgsConditionalStyle > QgsConditionalStyles
Definition: qgsconditionalstyle.h:28
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsConditionalStyle::symbol
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
Definition: qgsconditionalstyle.h:197
QgsConditionalStyle::isValid
bool isValid() const
isValid Check if this rule is valid.
Definition: qgsconditionalstyle.h:243
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:399
QgsConditionalStyle::setBackgroundColor
void setBackgroundColor(const QColor &value)
Set the background color for the style.
Definition: qgsconditionalstyle.h:155
QgsConditionalStyle::setTextColor
void setTextColor(const QColor &value)
Set the text color for the style.
Definition: qgsconditionalstyle.h:161
QgsSymbol
Definition: qgssymbol.h:63
QgsConditionalLayerStyles
The QgsConditionalLayerStyles class holds conditional style information for a layer....
Definition: qgsconditionalstyle.h:38
QgsConditionalStyle
Definition: qgsconditionalstyle.h:112
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:404
QgsConditionalStyle::setRule
void setRule(const QString &value)
Set the rule for the style.
Definition: qgsconditionalstyle.h:149
QgsConditionalStyle::icon
QPixmap icon() const
The icon set for style generated from the set symbol.
Definition: qgsconditionalstyle.h:191
QgsConditionalStyle::rule
QString rule() const
The condition rule set for the style.
Definition: qgsconditionalstyle.h:236
QgsConditionalStyle::backgroundColor
QColor backgroundColor() const
The background color for style.
Definition: qgsconditionalstyle.h:216
QgsConditionalStyle::setName
void setName(const QString &value)
Set the name of the style.
Definition: qgsconditionalstyle.h:142
QgsConditionalStyle::name
QString name() const
The name of the style.
Definition: qgsconditionalstyle.h:185
QgsConditionalStyle::font
QFont font() const
The font for the style.
Definition: qgsconditionalstyle.h:229
QgsConditionalStyle::textColor
QColor textColor() const
The text color set for style.
Definition: qgsconditionalstyle.h:203
qgssymbol.h
QgsConditionalStyle::setFont
void setFont(const QFont &value)
Set the font for the style.
Definition: qgsconditionalstyle.h:167