34 if ( styles == mRowStyles )
43 if ( mFieldStyles.value( fieldName ) == styles )
46 mFieldStyles.insert( fieldName, styles );
52 return mFieldStyles.value( fieldName );
57 QDomElement stylesel = doc.createElement( QStringLiteral(
"conditionalstyles" ) );
58 QDomElement rowel = doc.createElement( QStringLiteral(
"rowstyles" ) );
59 const auto constMRowStyles = mRowStyles;
62 style.writeXml( rowel, doc, context );
65 stylesel.appendChild( rowel );
67 QDomElement fieldsel = doc.createElement( QStringLiteral(
"fieldstyles" ) );
68 QHash<QString, QgsConditionalStyles>::const_iterator it = mFieldStyles.constBegin();
69 for ( ; it != mFieldStyles.constEnd(); ++it )
71 QDomElement fieldel = doc.createElement( QStringLiteral(
"fieldstyle" ) );
72 fieldel.setAttribute( QStringLiteral(
"fieldname" ), it.key() );
74 const auto constStyles = styles;
77 style.writeXml( fieldel, doc, context );
79 fieldsel.appendChild( fieldel );
82 stylesel.appendChild( fieldsel );
84 node.appendChild( stylesel );
90 QDomElement condel = node.firstChildElement( QStringLiteral(
"conditionalstyles" ) );
93 QDomElement rowstylesel = condel.firstChildElement( QStringLiteral(
"rowstyles" ) );
94 QDomNodeList nodelist = rowstylesel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
95 for (
int i = 0; i < nodelist.count(); i++ )
97 QDomElement styleElm = nodelist.at( i ).toElement();
99 style.
readXml( styleElm, context );
100 mRowStyles.append( style );
103 QDomElement fieldstylesel = condel.firstChildElement( QStringLiteral(
"fieldstyles" ) );
104 nodelist = fieldstylesel.toElement().elementsByTagName( QStringLiteral(
"fieldstyle" ) );
105 QList<QgsConditionalStyle> styles;
106 for (
int i = 0; i < nodelist.count(); i++ )
109 QDomElement fieldel = nodelist.at( i ).toElement();
110 QString fieldName = fieldel.attribute( QStringLiteral(
"fieldname" ) );
111 QDomNodeList stylenodelist = fieldel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
112 styles.reserve( stylenodelist.count() );
113 for (
int i = 0; i < stylenodelist.count(); i++ )
115 QDomElement styleElm = stylenodelist.at( i ).toElement();
117 style.
readXml( styleElm, context );
118 styles.append( style );
120 mFieldStyles.insert( fieldName, styles );
135 : mValid( other.mValid )
136 , mName( other.mName )
137 , mRule( other.mRule )
138 , mFont( other.mFont )
139 , mBackColor( other.mBackColor )
140 , mTextColor( other.mTextColor )
141 , mIcon( other.mIcon )
144 mSymbol.reset( other.mSymbol->clone() );
149 mValid = other.mValid;
152 mBackColor = other.mBackColor;
153 mTextColor = other.mTextColor;
158 mSymbol.reset( other.mSymbol->clone() );
169 if (
name().isEmpty() )
172 return QStringLiteral(
"%1 \n%2" ).arg(
name(),
rule() );
180 mSymbol.reset( value->
clone() );
193 return exp.
evaluate( &context ).toBool();
198 QPixmap pixmap( size.isValid() ? size.width() : 64, size.isValid() ? size.height() : 32 );
199 pixmap.fill( Qt::transparent );
201 QPainter painter( &pixmap );
204 painter.setBrush( mBackColor );
206 QRect rect = QRect( 0, 0, pixmap.width(), pixmap.height() );
207 painter.setPen( Qt::NoPen );
208 painter.drawRect( rect );
209 const QPixmap symbolIcon =
icon();
210 if ( !symbolIcon.isNull() )
212 painter.drawPixmap( ( pixmap.width() / 3 - symbolIcon.width() ) / 2, ( pixmap.height() - symbolIcon.height() ) / 2, symbolIcon );
216 painter.setPen( mTextColor );
218 painter.setPen( Qt::black );
220 painter.setRenderHint( QPainter::Antialiasing );
221 painter.setRenderHint( QPainter::HighQualityAntialiasing );
222 painter.setFont(
font() );
223 rect = QRect( pixmap.width() / 3, 0, 2 * pixmap.width() / 3, pixmap.height() );
224 painter.drawText( rect, Qt::AlignCenter, QStringLiteral(
"abc\n123" ) );
241 QList<QgsConditionalStyle> matchingstyles;
242 const auto constStyles = styles;
245 if ( style.matches( value, context ) )
246 matchingstyles.append( style );
248 return matchingstyles;
253 const auto constStyles = styles;
256 if ( style.matches( value, context ) )
271 if ( s.backgroundColor().isValid() && s.backgroundColor().alpha() != 0 )
273 if ( s.textColor().isValid() && s.textColor().alpha() != 0 )
283 QDomElement stylesel = doc.createElement( QStringLiteral(
"style" ) );
284 stylesel.setAttribute( QStringLiteral(
"rule" ), mRule );
285 stylesel.setAttribute( QStringLiteral(
"name" ), mName );
286 if ( mBackColor.isValid() )
288 stylesel.setAttribute( QStringLiteral(
"background_color" ), mBackColor.name() );
289 stylesel.setAttribute( QStringLiteral(
"background_color_alpha" ), mBackColor.alpha() );
291 if ( mTextColor.isValid() )
293 stylesel.setAttribute( QStringLiteral(
"text_color" ), mTextColor.name() );
294 stylesel.setAttribute( QStringLiteral(
"text_color_alpha" ), mTextColor.alpha() );
297 stylesel.appendChild( labelFontElem );
301 stylesel.appendChild( symbolElm );
303 node.appendChild( stylesel );
309 return mValid == other.mValid
310 && mName == other.mName
311 && mRule == other.mRule
312 && mFont == other.mFont
313 && mBackColor == other.mBackColor
314 && mTextColor == other.mTextColor
315 &&
static_cast< bool >( mSymbol ) == static_cast< bool >( other.mSymbol )
321 return !( *
this == other );
326 QDomElement styleElm = node.toElement();
327 setRule( styleElm.attribute( QStringLiteral(
"rule" ) ) );
328 setName( styleElm.attribute( QStringLiteral(
"name" ) ) );
329 if ( styleElm.hasAttribute( QStringLiteral(
"background_color" ) ) )
331 QColor bColor = QColor( styleElm.attribute( QStringLiteral(
"background_color" ) ) );
332 if ( styleElm.hasAttribute( QStringLiteral(
"background_color_alpha" ) ) )
334 bColor.setAlpha( styleElm.attribute( QStringLiteral(
"background_color_alpha" ) ).toInt() );
336 if ( bColor.alpha() == 0 )
345 if ( styleElm.hasAttribute( QStringLiteral(
"text_color" ) ) )
347 QColor tColor = QColor( styleElm.attribute( QStringLiteral(
"text_color" ) ) );
348 if ( styleElm.hasAttribute( QStringLiteral(
"text_color_alpha" ) ) )
350 tColor.setAlpha( styleElm.attribute( QStringLiteral(
"text_color_alpha" ) ).toInt() );
352 if ( tColor.alpha() == 0 )
362 QDomElement symbolElm = styleElm.firstChildElement( QStringLiteral(
"symbol" ) );
363 if ( !symbolElm.isNull() )
365 QgsSymbol *
symbol = QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElm, context );
Class for parsing and evaluation of expressions (formerly called "search strings").
The class is used as a container of context for various read/write operations on other objects...
QString name() const
The name of the style.
void setName(const QString &value)
Set the name of the style.
Single variable definition for use within a QgsExpressionContextScope.
QPixmap renderPreview(const QSize &size=QSize()) const
Render a preview icon of the rule, at the specified size.
Abstract base class for all rendered symbols.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes the condition styles state to a DOM node.
void setRule(const QString &value)
Set the rule for the style.
bool matches(const QVariant &value, QgsExpressionContext &context) const
Check if the rule matches using the given value and feature.
QVariant evaluate()
Evaluate the feature and return the result.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Write vector conditional style specific state from layer Dom node.
bool validBackgroundColor() const
Check if the background color is valid for render.
QPixmap icon() const
The icon set for style generated from the set symbol.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
bool operator==(const QgsConditionalStyle &other) const
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
QgsConditionalStyle & operator=(const QgsConditionalStyle &other)
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
Conditional styling for a rule.
bool operator!=(const QgsConditionalStyle &other) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool isValid() const
isValid Check if this rule is valid.
static QList< QgsConditionalStyle > matchingConditionalStyles(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching styles for the value and feature.
QColor backgroundColor() const
The background color for style.
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
static QgsConditionalStyle compressStyles(const QList< QgsConditionalStyle > &styles)
Compress a list of styles into a single style.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads the condition styles state from a DOM node.
void setBackgroundColor(const QColor &value)
Set the background color for the style.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName) const
Returns the conditional styles set for the field with matching fieldName.
static QString symbolProperties(QgsSymbol *symbol)
Returns a string representing the symbol.
void setFont(const QFont &value)
Set the font for the style.
QColor textColor() const
The text color set for style.
static QgsConditionalStyle matchingConditionalStyle(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching style for the value and feature.
void setSymbol(QgsSymbol *value)
Set the icon for the style.
void changed()
Emitted when the conditional styles are changed.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr)
Returns a pixmap preview for a color ramp.
bool validTextColor() const
Check if the text color is valid for render.
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
QString displayText() const
The name of the style.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
QFont font() const
The font for the style.
void setRowStyles(const QgsConditionalStyles &styles)
Sets the conditional styles that apply to full rows of data in the attribute table.
void setFieldStyles(const QString &fieldName, const QList< QgsConditionalStyle > &styles)
Set the conditional styles for a field, with the specified fieldName.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads vector conditional style specific state from layer Dom node.
QString rule() const
The condition rule set for the style.
QgsConditionalLayerStyles(QObject *parent=nullptr)
Constructor for QgsConditionalLayerStyles, with the specified parent object.
QList< QgsConditionalStyle > QgsConditionalStyles
QgsConditionalStyles rowStyles() const
Returns a list of row styles associated with the layer.
void setTextColor(const QColor &value)
Set the text color for the style.