QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsstoredexpressionmanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsstoredexpressionmanager.cpp
3 -------------------
4 begin : August 2019
5 copyright : (C) 2019 David Signer
6 email : david at opengis dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgis.h"
21
22#include <QDomElement>
23
24#include "moc_qgsstoredexpressionmanager.cpp"
25
26QString QgsStoredExpressionManager::addStoredExpression( const QString &name, const QString &expression, const QgsStoredExpression::Category &tag )
27{
28 QgsStoredExpression storedExpression( name, expression, tag );
29
30 mStoredExpressions.append( storedExpression );
31
32 return storedExpression.id;
33}
34
36{
37 int i = 0;
38 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
39 {
40 if ( storedExpression.id == id )
41 {
42 mStoredExpressions.removeAt( i );
43 //leave the loop after editing source
44 break;
45 }
46 ++i;
47 }
48}
49
50void QgsStoredExpressionManager::updateStoredExpression( const QString &id, const QString &name, const QString &expression, const QgsStoredExpression::Category &tag )
51{
52 int i = 0;
53 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
54 {
55 if ( storedExpression.id == id )
56 {
57 QgsStoredExpression newStoredExpression = storedExpression;
58 newStoredExpression.name = name;
59 newStoredExpression.expression = expression;
60 newStoredExpression.tag = tag;
61 mStoredExpressions.replace( i, newStoredExpression );
62 //leave the loop after editing source
63 break;
64 }
65 ++i;
66 }
67}
68
70{
71 mStoredExpressions.append( storedExpressions );
72}
73
75{
76 QList< QgsStoredExpression > storedExpressions;
77
78 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
79 {
80 if ( storedExpression.tag & tag )
81 {
83 }
84 }
85 return storedExpressions;
86}
87
89{
90 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
91 {
92 if ( storedExpression.id == id )
93 {
94 return storedExpression;
95 }
96 }
97 return QgsStoredExpression();
98}
99
101{
102 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
103 {
104 if ( storedExpression.expression == expression && storedExpression.tag & tag )
105 {
106 return storedExpression;
107 }
108 }
109 return QgsStoredExpression();
110}
111
113{
114 mStoredExpressions.clear();
115}
116
117bool QgsStoredExpressionManager::writeXml( QDomNode &layerNode ) const
118{
119 QDomElement aStoredExpressions = layerNode.ownerDocument().createElement( QStringLiteral( "storedexpressions" ) );
120
121 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
122 {
123 QDomElement aStoredExpression = layerNode.ownerDocument().createElement( QStringLiteral( "storedexpression" ) );
124 aStoredExpression.setAttribute( QStringLiteral( "name" ), storedExpression.name );
125 aStoredExpression.setAttribute( QStringLiteral( "expression" ), storedExpression.expression );
126 aStoredExpression.setAttribute( QStringLiteral( "tag" ), storedExpression.tag );
127 aStoredExpressions.appendChild( aStoredExpression );
128 }
129 layerNode.appendChild( aStoredExpressions );
130
131 return true;
132}
133
134bool QgsStoredExpressionManager::readXml( const QDomNode &layerNode )
135{
137
138 QDomNode aaNode = layerNode.namedItem( QStringLiteral( "storedexpressions" ) );
139
140 if ( !aaNode.isNull() )
141 {
142 QDomNodeList aStoredExpressions = aaNode.toElement().elementsByTagName( QStringLiteral( "storedexpression" ) );
143 for ( int i = 0; i < aStoredExpressions.size(); ++i )
144 {
145 QDomElement aStoredExpression = aStoredExpressions.at( i ).toElement();
146 addStoredExpression( aStoredExpression.attribute( QStringLiteral( "name" ) ), aStoredExpression.attribute( QStringLiteral( "expression" ) ), QgsStoredExpression::Category( aStoredExpression.attribute( QStringLiteral( "tag" ) ).toInt() ) );
147 }
148 }
149 return true;
150}
void removeStoredExpression(const QString &id)
Removes an expression to the list.
QgsStoredExpression storedExpression(const QString &id) const
Returns an expression according to the id.
bool writeXml(QDomNode &layerNode) const
Writes the stored expressions out in XML format.
void clearStoredExpressions()
Clears list of stored expressions.
QgsStoredExpression findStoredExpressionByExpression(const QString &expression, const QgsStoredExpression::Category &tag=QgsStoredExpression::Category::All) const
Returns an expression according to the expression text.
QList< QgsStoredExpression > storedExpressions(const QgsStoredExpression::Category &tag=QgsStoredExpression::Category::All)
Returns the list of named expressions.
void addStoredExpressions(const QList< QgsStoredExpression > &storedExpressions)
Appends a list of expressions to the existing list.
void updateStoredExpression(const QString &id, const QString &name, const QString &expression, const QgsStoredExpression::Category &tag)
Updates an expression by id.
bool readXml(const QDomNode &layerNode)
Reads the stored expressions in in XML format.
QString addStoredExpression(const QString &name, const QString &expression, const QgsStoredExpression::Category &tag=QgsStoredExpression::Category::FilterExpression)
Adds an expression to the list.
Stored expression containing name, content (expression text) and a category tag.
QString expression
expression text
Category
Categories of use cases FilterExpression for stored expressions to filter attribute table DefaultValu...
QString name
descriptive name of the expression
QgsStoredExpression::Category tag
category of the expression use case