37 hardConstraintFailureStyle.
setTextColor( QColor( 0, 0, 0 ) );
38 return hardConstraintFailureStyle;
45 softConstraintFailureStyle.
setTextColor( QColor( 0, 0, 0 ) );
46 return softConstraintFailureStyle;
63 if ( styles == mRowStyles )
72 if ( mFieldStyles.value( fieldName ) == styles )
75 mFieldStyles.insert( fieldName, styles );
81 return mFieldStyles.value( fieldName );
86 QDomElement stylesel = doc.createElement( QStringLiteral(
"conditionalstyles" ) );
88 QDomElement rowel = doc.createElement( QStringLiteral(
"rowstyles" ) );
89 const auto constMRowStyles = mRowStyles;
92 style.writeXml( rowel, doc, context );
94 stylesel.appendChild( rowel );
96 QDomElement fieldsel = doc.createElement( QStringLiteral(
"fieldstyles" ) );
97 QHash<QString, QgsConditionalStyles>::const_iterator it = mFieldStyles.constBegin();
98 for ( ; it != mFieldStyles.constEnd(); ++it )
100 QDomElement fieldel = doc.createElement( QStringLiteral(
"fieldstyle" ) );
101 fieldel.setAttribute( QStringLiteral(
"fieldname" ), it.key() );
103 const auto constStyles = styles;
106 style.writeXml( fieldel, doc, context );
108 fieldsel.appendChild( fieldel );
110 stylesel.appendChild( fieldsel );
112 node.appendChild( stylesel );
131 mFieldStyles.clear();
133 const QDomElement condel = node.firstChildElement( QStringLiteral(
"conditionalstyles" ) );
135 const QDomElement rowstylesel = condel.firstChildElement( QStringLiteral(
"rowstyles" ) );
136 QDomNodeList nodelist = rowstylesel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
137 for (
int i = 0; i < nodelist.count(); i++ )
139 const QDomElement styleElm = nodelist.at( i ).toElement();
141 style.
readXml( styleElm, context );
142 mRowStyles.append( style );
145 const QDomElement fieldstylesel = condel.firstChildElement( QStringLiteral(
"fieldstyles" ) );
146 nodelist = fieldstylesel.toElement().elementsByTagName( QStringLiteral(
"fieldstyle" ) );
147 QList<QgsConditionalStyle> styles;
148 for (
int i = 0; i < nodelist.count(); i++ )
151 const QDomElement fieldel = nodelist.at( i ).toElement();
152 const QString fieldName = fieldel.attribute( QStringLiteral(
"fieldname" ) );
153 const QDomNodeList stylenodelist = fieldel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
154 styles.reserve( stylenodelist.count() );
155 for (
int j = 0; j < stylenodelist.count(); j++ )
157 const QDomElement styleElm = stylenodelist.at( j ).toElement();
159 style.
readXml( styleElm, context );
160 styles.append( style );
162 mFieldStyles.insert( fieldName, styles );
179 : mValid( other.mValid )
180 , mName( other.mName )
181 , mRule( other.mRule )
182 , mFont( other.mFont )
183 , mBackColor( other.mBackColor )
184 , mTextColor( other.mTextColor )
185 , mIcon( other.mIcon )
188 mSymbol.reset( other.mSymbol->clone() );
193 mValid = other.mValid;
196 mBackColor = other.mBackColor;
197 mTextColor = other.mTextColor;
202 mSymbol.reset( other.mSymbol->clone() );
213 if (
name().isEmpty() )
216 return QStringLiteral(
"%1 \n%2" ).arg(
name(),
rule() );
224 mSymbol.reset( value->
clone() );
237 return exp.
evaluate( &context ).toBool();
242 QPixmap pixmap( size.isValid() ? size.width() : 64, size.isValid() ? size.height() : 32 );
243 pixmap.fill( Qt::transparent );
245 QPainter painter( &pixmap );
248 painter.setBrush( mBackColor );
250 QRect rect = QRect( 0, 0, pixmap.width(), pixmap.height() );
251 painter.setPen( Qt::NoPen );
252 painter.drawRect( rect );
253 const QPixmap symbolIcon =
icon();
254 if ( !symbolIcon.isNull() )
256 painter.drawPixmap( ( pixmap.width() / 3 - symbolIcon.width() ) / 2, ( pixmap.height() - symbolIcon.height() ) / 2, symbolIcon );
260 painter.setPen( mTextColor );
262 painter.setPen( Qt::black );
264 painter.setRenderHint( QPainter::Antialiasing );
265 painter.setFont(
font() );
266 rect = QRect( pixmap.width() / 3, 0, 2 * pixmap.width() / 3, pixmap.height() );
267 painter.drawText( rect, Qt::AlignCenter, QStringLiteral(
"abc\n123" ) );
284 QList<QgsConditionalStyle> matchingstyles;
285 const auto constStyles = styles;
288 if ( style.matches( value, context ) )
289 matchingstyles.append( style );
291 return matchingstyles;
296 const auto constStyles = styles;
299 if ( style.matches( value, context ) )
314 if ( s.backgroundColor().isValid() && s.backgroundColor().alpha() != 0 )
316 if ( s.textColor().isValid() && s.textColor().alpha() != 0 )
318 if (
auto *lSymbol = s.symbol() )
326 QDomElement stylesel = doc.createElement( QStringLiteral(
"style" ) );
327 stylesel.setAttribute( QStringLiteral(
"rule" ), mRule );
328 stylesel.setAttribute( QStringLiteral(
"name" ), mName );
329 if ( mBackColor.isValid() )
331 stylesel.setAttribute( QStringLiteral(
"background_color" ), mBackColor.name() );
332 stylesel.setAttribute( QStringLiteral(
"background_color_alpha" ), mBackColor.alpha() );
334 if ( mTextColor.isValid() )
336 stylesel.setAttribute( QStringLiteral(
"text_color" ), mTextColor.name() );
337 stylesel.setAttribute( QStringLiteral(
"text_color_alpha" ), mTextColor.alpha() );
340 stylesel.appendChild( labelFontElem );
344 stylesel.appendChild( symbolElm );
346 node.appendChild( stylesel );
352 return mValid == other.mValid
353 && mName == other.mName
354 && mRule == other.mRule
355 && mFont == other.mFont
356 && mBackColor == other.mBackColor
357 && mTextColor == other.mTextColor
358 &&
static_cast< bool >( mSymbol ) ==
static_cast< bool >( other.mSymbol )
364 return !( *
this == other );
369 const QDomElement styleElm = node.toElement();
370 setRule( styleElm.attribute( QStringLiteral(
"rule" ) ) );
371 setName( styleElm.attribute( QStringLiteral(
"name" ) ) );
372 if ( styleElm.hasAttribute( QStringLiteral(
"background_color" ) ) )
374 QColor bColor = QColor( styleElm.attribute( QStringLiteral(
"background_color" ) ) );
375 if ( styleElm.hasAttribute( QStringLiteral(
"background_color_alpha" ) ) )
377 bColor.setAlpha( styleElm.attribute( QStringLiteral(
"background_color_alpha" ) ).toInt() );
379 if ( bColor.alpha() == 0 )
388 if ( styleElm.hasAttribute( QStringLiteral(
"text_color" ) ) )
390 QColor tColor = QColor( styleElm.attribute( QStringLiteral(
"text_color" ) ) );
391 if ( styleElm.hasAttribute( QStringLiteral(
"text_color_alpha" ) ) )
393 tColor.setAlpha( styleElm.attribute( QStringLiteral(
"text_color_alpha" ) ).toInt() );
395 if ( tColor.alpha() == 0 )
405 const QDomElement symbolElm = styleElm.firstChildElement( QStringLiteral(
"symbol" ) );
406 if ( !symbolElm.isNull() )
408 QgsSymbol *
symbol = QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElm, context );
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.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads vector conditional style specific state from layer Dom node.
bool operator==(const QgsConditionalStyle &other) const
QString displayText() const
The name of the style.
QString name() const
The name of the style.
static QgsConditionalStyle compressStyles(const QList< QgsConditionalStyle > &styles)
Compress a list of styles into a single style.
bool operator!=(const QgsConditionalStyle &other) const
void setSymbol(QgsSymbol *value)
Set the icon for 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.
static QList< QgsConditionalStyle > matchingConditionalStyles(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching styles for the value and feature.
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 writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Write vector conditional style specific state from layer Dom node.
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.
bool isValid() const
isValid Check if this rule is valid.
static QgsConditionalStyle matchingConditionalStyle(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching style for the value and feature.
QPixmap icon() const
The icon set for style generated from the set symbol.
bool validBackgroundColor() const
Check if the background color is valid for render.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
QVariant evaluate()
Evaluate the feature and return the result.
ConstraintStrength
Strength of constraints.
@ ConstraintStrengthNotSet
Constraint is not set.
@ ConstraintStrengthSoft
User is warned if constraint is violated but feature can still be accepted.
@ ConstraintStrengthHard
Constraint must be honored before feature can be accepted.
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 QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
The class is used as a container of context for various read/write operations on other objects.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *shape=nullptr)
Returns a pixmap preview for a color ramp.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static QString symbolProperties(QgsSymbol *symbol)
Returns a string representing the symbol.
Abstract base class for all rendered symbols.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
QList< QgsConditionalStyle > QgsConditionalStyles
Single variable definition for use within a QgsExpressionContextScope.