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