QGIS API Documentation 3.99.0-Master (357b655ed83)
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#include <QString>
24
25#include "moc_qgsstoredexpressionmanager.cpp"
26
27using namespace Qt::StringLiterals;
28
29QString QgsStoredExpressionManager::addStoredExpression( const QString &name, const QString &expression, const QgsStoredExpression::Category &tag )
30{
31 QgsStoredExpression storedExpression( name, expression, tag );
32
33 mStoredExpressions.append( storedExpression );
34
35 return storedExpression.id;
36}
37
39{
40 int i = 0;
41 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
42 {
43 if ( storedExpression.id == id )
44 {
45 mStoredExpressions.removeAt( i );
46 //leave the loop after editing source
47 break;
48 }
49 ++i;
50 }
51}
52
53void QgsStoredExpressionManager::updateStoredExpression( const QString &id, const QString &name, const QString &expression, const QgsStoredExpression::Category &tag )
54{
55 int i = 0;
56 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
57 {
58 if ( storedExpression.id == id )
59 {
60 QgsStoredExpression newStoredExpression = storedExpression;
61 newStoredExpression.name = name;
62 newStoredExpression.expression = expression;
63 newStoredExpression.tag = tag;
64 mStoredExpressions.replace( i, newStoredExpression );
65 //leave the loop after editing source
66 break;
67 }
68 ++i;
69 }
70}
71
73{
74 mStoredExpressions.append( storedExpressions );
75}
76
78{
79 QList< QgsStoredExpression > storedExpressions;
80
81 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
82 {
83 if ( storedExpression.tag & tag )
84 {
86 }
87 }
88 return storedExpressions;
89}
90
92{
93 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
94 {
95 if ( storedExpression.id == id )
96 {
97 return storedExpression;
98 }
99 }
100 return QgsStoredExpression();
101}
102
104{
105 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
106 {
107 if ( storedExpression.expression == expression && storedExpression.tag & tag )
108 {
109 return storedExpression;
110 }
111 }
112 return QgsStoredExpression();
113}
114
116{
117 mStoredExpressions.clear();
118}
119
120bool QgsStoredExpressionManager::writeXml( QDomNode &layerNode ) const
121{
122 QDomElement aStoredExpressions = layerNode.ownerDocument().createElement( u"storedexpressions"_s );
123
124 for ( const QgsStoredExpression &storedExpression : std::as_const( mStoredExpressions ) )
125 {
126 QDomElement aStoredExpression = layerNode.ownerDocument().createElement( u"storedexpression"_s );
127 aStoredExpression.setAttribute( u"name"_s, storedExpression.name );
128 aStoredExpression.setAttribute( u"expression"_s, storedExpression.expression );
129 aStoredExpression.setAttribute( u"tag"_s, storedExpression.tag );
130 aStoredExpressions.appendChild( aStoredExpression );
131 }
132 layerNode.appendChild( aStoredExpressions );
133
134 return true;
135}
136
137bool QgsStoredExpressionManager::readXml( const QDomNode &layerNode )
138{
140
141 QDomNode aaNode = layerNode.namedItem( u"storedexpressions"_s );
142
143 if ( !aaNode.isNull() )
144 {
145 QDomNodeList aStoredExpressions = aaNode.toElement().elementsByTagName( u"storedexpression"_s );
146 for ( int i = 0; i < aStoredExpressions.size(); ++i )
147 {
148 QDomElement aStoredExpression = aStoredExpressions.at( i ).toElement();
149 addStoredExpression( aStoredExpression.attribute( u"name"_s ), aStoredExpression.attribute( u"expression"_s ), QgsStoredExpression::Category( aStoredExpression.attribute( u"tag"_s ).toInt() ) );
150 }
151 }
152 return true;
153}
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