39 mFieldStyles.insert( fieldName, styles );
44 if ( mFieldStyles.contains( fieldName ) )
46 return mFieldStyles[fieldName];
48 return QList<QgsConditionalStyle>();
53 QDomElement stylesel = doc.createElement( QStringLiteral(
"conditionalstyles" ) );
54 QDomElement rowel = doc.createElement( QStringLiteral(
"rowstyles" ) );
57 style.
writeXml( rowel, doc, context );
60 stylesel.appendChild( rowel );
62 QDomElement fieldsel = doc.createElement( QStringLiteral(
"fieldstyles" ) );
63 QHash<QString, QgsConditionalStyles>::const_iterator it = mFieldStyles.constBegin();
64 for ( ; it != mFieldStyles.constEnd(); ++it )
66 QDomElement fieldel = doc.createElement( QStringLiteral(
"fieldstyle" ) );
67 fieldel.setAttribute( QStringLiteral(
"fieldname" ), it.key() );
71 style.
writeXml( fieldel, doc, context );
73 fieldsel.appendChild( fieldel );
76 stylesel.appendChild( fieldsel );
78 node.appendChild( stylesel );
84 QDomElement condel = node.firstChildElement( QStringLiteral(
"conditionalstyles" ) );
87 QDomElement rowstylesel = condel.firstChildElement( QStringLiteral(
"rowstyles" ) );
88 QDomNodeList nodelist = rowstylesel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
89 for (
int i = 0; i < nodelist.count(); i++ )
91 QDomElement styleElm = nodelist.at( i ).toElement();
93 style.
readXml( styleElm, context );
94 mRowStyles.append( style );
97 QDomElement fieldstylesel = condel.firstChildElement( QStringLiteral(
"fieldstyles" ) );
98 nodelist = fieldstylesel.toElement().elementsByTagName( QStringLiteral(
"fieldstyle" ) );
99 QList<QgsConditionalStyle> styles;
100 for (
int i = 0; i < nodelist.count(); i++ )
103 QDomElement fieldel = nodelist.at( i ).toElement();
104 QString fieldName = fieldel.attribute( QStringLiteral(
"fieldname" ) );
105 QDomNodeList stylenodelist = fieldel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
106 styles.reserve( stylenodelist.count() );
107 for (
int i = 0; i < stylenodelist.count(); i++ )
109 QDomElement styleElm = stylenodelist.at( i ).toElement();
111 style.
readXml( styleElm, context );
112 styles.append( style );
114 mFieldStyles.insert( fieldName, styles );
121 : mBackColor( QColor( 0, 0, 0, 0 ) )
122 , mTextColor( QColor( 0, 0, 0, 0 ) )
126 : mBackColor( QColor( 0, 0, 0, 0 ) )
127 , mTextColor( QColor( 0, 0, 0, 0 ) )
133 : mValid( other.mValid )
134 , mName( other.mName )
135 , mRule( other.mRule )
136 , mFont( other.mFont )
137 , mBackColor( other.mBackColor )
138 , mTextColor( other.mTextColor )
139 , mIcon( other.mIcon )
142 mSymbol.reset( other.mSymbol->clone() );
147 mValid = other.mValid;
150 mBackColor = other.mBackColor;
151 mTextColor = other.mTextColor;
156 mSymbol.reset( other.mSymbol->clone() );
167 if (
name().isEmpty() )
170 return QStringLiteral(
"%1 \n%2" ).arg(
name(),
rule() );
178 mSymbol.reset( value->
clone() );
191 return exp.
evaluate( &context ).toBool();
196 QPixmap pixmap( 64, 32 );
197 pixmap.fill( Qt::transparent );
199 QPainter painter( &pixmap );
202 painter.setBrush( mBackColor );
204 QRect rect = QRect( 0, 0, 64, 32 );
205 painter.setPen( Qt::NoPen );
206 painter.drawRect( rect );
207 painter.drawPixmap( 8, 8,
icon() );
210 painter.setPen( mTextColor );
212 painter.setPen( Qt::black );
214 painter.setRenderHint( QPainter::Antialiasing );
215 painter.setRenderHint( QPainter::HighQualityAntialiasing );
216 painter.setFont(
font() );
217 rect = QRect( 32, 0, 32, 32 );
218 painter.drawText( rect, Qt::AlignCenter, QStringLiteral(
"abc\n123" ) );
235 QList<QgsConditionalStyle> matchingstyles;
238 if ( style.
matches( value, context ) )
239 matchingstyles.append( style );
241 return matchingstyles;
248 if ( style.
matches( value, context ) )
272 QDomElement stylesel = doc.createElement( QStringLiteral(
"style" ) );
273 stylesel.setAttribute( QStringLiteral(
"rule" ), mRule );
274 stylesel.setAttribute( QStringLiteral(
"name" ), mName );
275 stylesel.setAttribute( QStringLiteral(
"background_color" ), mBackColor.name() );
276 stylesel.setAttribute( QStringLiteral(
"background_color_alpha" ), mBackColor.alpha() );
277 stylesel.setAttribute( QStringLiteral(
"text_color" ), mTextColor.name() );
278 stylesel.setAttribute( QStringLiteral(
"text_color_alpha" ), mTextColor.alpha() );
280 stylesel.appendChild( labelFontElem );
284 stylesel.appendChild( symbolElm );
286 node.appendChild( stylesel );
292 QDomElement styleElm = node.toElement();
293 setRule( styleElm.attribute( QStringLiteral(
"rule" ) ) );
294 setName( styleElm.attribute( QStringLiteral(
"name" ) ) );
295 QColor bColor = QColor( styleElm.attribute( QStringLiteral(
"background_color" ) ) );
296 if ( styleElm.hasAttribute( QStringLiteral(
"background_color_alpha" ) ) )
298 bColor.setAlpha( styleElm.attribute( QStringLiteral(
"background_color_alpha" ) ).toInt() );
301 QColor tColor = QColor( styleElm.attribute( QStringLiteral(
"text_color" ) ) );
302 if ( styleElm.hasAttribute( QStringLiteral(
"text_color_alpha" ) ) )
304 tColor.setAlpha( styleElm.attribute( QStringLiteral(
"text_color_alpha" ) ).toInt() );
308 QDomElement symbolElm = styleElm.firstChildElement( QStringLiteral(
"symbol" ) );
309 if ( !symbolElm.isNull() )
311 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...
void setName(const QString &value)
Set the name of the style.
Single variable definition for use within a QgsExpressionContextScope.
static QPixmap symbolPreviewPixmap(QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr)
Returns a pixmap preview for a color ramp.
bool validTextColor() const
Check if the text color is valid for render.
Abstract base class for all rendered symbols.
QColor textColor() const
The text color set for style.
bool validBackgroundColor() const
Check if the background color is valid for render.
void setRule(const QString &value)
Set the rule for the style.
QFont font() const
The font for the style.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName)
Returns the conditional styles set for the field UI properties.
QVariant evaluate()
Evaluate the feature and return the result.
QgsConditionalLayerStyles()
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
QgsConditionalStyle & operator=(const QgsConditionalStyle &other)
QList< QgsConditionalStyle > rowStyles()
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
QPixmap icon() const
The icon set for style generated from the set symbol.
Conditional styling for a rule.
bool matches(const QVariant &value, QgsExpressionContext &context) const
Check if the rule matches using the given value and feature.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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.
QString name() const
The name of the style.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads field ui properties specific state from Dom node.
void setBackgroundColor(const QColor &value)
Set the background color for the style.
QPixmap renderPreview() const
Render a preview icon of the rule.
void setFont(const QFont &value)
Set the font for the style.
void setRowStyles(const QList< QgsConditionalStyle > &styles)
Set the conditional styles that apply to full rows of data in the attribute table.
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.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
static QDomElement saveSymbol(const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
QString rule() const
The condition rule set for the style.
bool isValid() const
isValid Check if this rule is valid.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Write field ui properties specific state from Dom node.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
void setFieldStyles(const QString &fieldName, const QList< QgsConditionalStyle > &styles)
Set the conditional styles for the field UI properties.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads vector conditional style specific state from layer Dom node.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Write vector conditional style specific state from layer Dom node.
QString displayText() const
The name of the style.
QList< QgsConditionalStyle > QgsConditionalStyles
void setTextColor(const QColor &value)
Set the text color for the style.