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" ) );
55 const auto constMRowStyles = mRowStyles;
58 style.writeXml( rowel, doc, context );
61 stylesel.appendChild( rowel );
63 QDomElement fieldsel = doc.createElement( QStringLiteral(
"fieldstyles" ) );
64 QHash<QString, QgsConditionalStyles>::const_iterator it = mFieldStyles.constBegin();
65 for ( ; it != mFieldStyles.constEnd(); ++it )
67 QDomElement fieldel = doc.createElement( QStringLiteral(
"fieldstyle" ) );
68 fieldel.setAttribute( QStringLiteral(
"fieldname" ), it.key() );
70 const auto constStyles = styles;
73 style.writeXml( fieldel, doc, context );
75 fieldsel.appendChild( fieldel );
78 stylesel.appendChild( fieldsel );
80 node.appendChild( stylesel );
86 QDomElement condel = node.firstChildElement( QStringLiteral(
"conditionalstyles" ) );
89 QDomElement rowstylesel = condel.firstChildElement( QStringLiteral(
"rowstyles" ) );
90 QDomNodeList nodelist = rowstylesel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
91 for (
int i = 0; i < nodelist.count(); i++ )
93 QDomElement styleElm = nodelist.at( i ).toElement();
95 style.
readXml( styleElm, context );
96 mRowStyles.append( style );
99 QDomElement fieldstylesel = condel.firstChildElement( QStringLiteral(
"fieldstyles" ) );
100 nodelist = fieldstylesel.toElement().elementsByTagName( QStringLiteral(
"fieldstyle" ) );
101 QList<QgsConditionalStyle> styles;
102 for (
int i = 0; i < nodelist.count(); i++ )
105 QDomElement fieldel = nodelist.at( i ).toElement();
106 QString fieldName = fieldel.attribute( QStringLiteral(
"fieldname" ) );
107 QDomNodeList stylenodelist = fieldel.toElement().elementsByTagName( QStringLiteral(
"style" ) );
108 styles.reserve( stylenodelist.count() );
109 for (
int i = 0; i < stylenodelist.count(); i++ )
111 QDomElement styleElm = stylenodelist.at( i ).toElement();
113 style.
readXml( styleElm, context );
114 styles.append( style );
116 mFieldStyles.insert( fieldName, styles );
123 : mBackColor( QColor( 0, 0, 0, 0 ) )
124 , mTextColor( QColor( 0, 0, 0, 0 ) )
128 : mBackColor( QColor( 0, 0, 0, 0 ) )
129 , mTextColor( QColor( 0, 0, 0, 0 ) )
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( 64, 32 );
199 pixmap.fill( Qt::transparent );
201 QPainter painter( &pixmap );
204 painter.setBrush( mBackColor );
206 QRect rect = QRect( 0, 0, 64, 32 );
207 painter.setPen( Qt::NoPen );
208 painter.drawRect( rect );
209 painter.drawPixmap( 8, 8,
icon() );
212 painter.setPen( mTextColor );
214 painter.setPen( Qt::black );
216 painter.setRenderHint( QPainter::Antialiasing );
217 painter.setRenderHint( QPainter::HighQualityAntialiasing );
218 painter.setFont(
font() );
219 rect = QRect( 32, 0, 32, 32 );
220 painter.drawText( rect, Qt::AlignCenter, QStringLiteral(
"abc\n123" ) );
237 QList<QgsConditionalStyle> matchingstyles;
238 const auto constStyles = styles;
241 if ( style.matches( value, context ) )
242 matchingstyles.append( style );
244 return matchingstyles;
249 const auto constStyles = styles;
252 if ( style.matches( value, context ) )
261 const auto constStyles = styles;
265 if ( s.backgroundColor().isValid() && s.backgroundColor().alpha() != 0 )
267 if ( s.textColor().isValid() && s.textColor().alpha() != 0 )
277 QDomElement stylesel = doc.createElement( QStringLiteral(
"style" ) );
278 stylesel.setAttribute( QStringLiteral(
"rule" ), mRule );
279 stylesel.setAttribute( QStringLiteral(
"name" ), mName );
280 stylesel.setAttribute( QStringLiteral(
"background_color" ), mBackColor.name() );
281 stylesel.setAttribute( QStringLiteral(
"background_color_alpha" ), mBackColor.alpha() );
282 stylesel.setAttribute( QStringLiteral(
"text_color" ), mTextColor.name() );
283 stylesel.setAttribute( QStringLiteral(
"text_color_alpha" ), mTextColor.alpha() );
285 stylesel.appendChild( labelFontElem );
289 stylesel.appendChild( symbolElm );
291 node.appendChild( stylesel );
297 QDomElement styleElm = node.toElement();
298 setRule( styleElm.attribute( QStringLiteral(
"rule" ) ) );
299 setName( styleElm.attribute( QStringLiteral(
"name" ) ) );
300 QColor bColor = QColor( styleElm.attribute( QStringLiteral(
"background_color" ) ) );
301 if ( styleElm.hasAttribute( QStringLiteral(
"background_color_alpha" ) ) )
303 bColor.setAlpha( styleElm.attribute( QStringLiteral(
"background_color_alpha" ) ).toInt() );
306 QColor tColor = QColor( styleElm.attribute( QStringLiteral(
"text_color" ) ) );
307 if ( styleElm.hasAttribute( QStringLiteral(
"text_color_alpha" ) ) )
309 tColor.setAlpha( styleElm.attribute( QStringLiteral(
"text_color_alpha" ) ).toInt() );
313 QDomElement symbolElm = styleElm.firstChildElement( QStringLiteral(
"symbol" ) );
314 if ( !symbolElm.isNull() )
316 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.
Abstract base class for all rendered symbols.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Write field ui properties specific state from 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.
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.
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.
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.
Conditional styling for a rule.
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 field ui properties specific state from Dom node.
void setBackgroundColor(const QColor &value)
Set the background color for the style.
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.
QColor textColor() const
The text color set for style.
QPixmap renderPreview() const
Render a preview icon of the rule.
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.
bool validTextColor() const
Check if the text color is valid for render.
static QDomElement saveSymbol(const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
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 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.
QString rule() const
The condition rule set for the style.
QList< QgsConditionalStyle > QgsConditionalStyles
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr)
Returns a pixmap preview for a color ramp.
void setTextColor(const QColor &value)
Set the text color for the style.