QGIS API Documentation  3.9.0-Master (224899f119)
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 "qgis.h"
20 
21 
22 #include <QDomElement>
23 
24 QString QgsStoredExpressionManager::addStoredExpression( const QString &name, const QString &expression, const QgsStoredExpression::Category &tag )
25 {
26  QgsStoredExpression storedExpression( name, expression, tag );
27 
28  mStoredExpressions.append( storedExpression );
29 
30  return storedExpression.id;
31 }
32 
34 {
35  int i = 0;
36  for ( const QgsStoredExpression &storedExpression : qgis::as_const( mStoredExpressions ) )
37  {
38  if ( storedExpression.id == id )
39  {
40  mStoredExpressions.removeAt( i );
41  //leave the loop after editing source
42  break;
43  }
44  ++i;
45  }
46 }
47 
48 void QgsStoredExpressionManager::updateStoredExpression( const QString &id, const QString &name, const QString &expression, const QgsStoredExpression::Category &tag )
49 {
50  int i = 0;
51  for ( const QgsStoredExpression &storedExpression : qgis::as_const( mStoredExpressions ) )
52  {
53  if ( storedExpression.id == id )
54  {
55  QgsStoredExpression newStoredExpression = storedExpression;
56  newStoredExpression.name = name;
57  newStoredExpression.expression = expression;
58  newStoredExpression.tag = tag;
59  mStoredExpressions.replace( i, newStoredExpression );
60  //leave the loop after editing source
61  break;
62  }
63  ++i;
64  }
65 }
66 
67 void QgsStoredExpressionManager::addStoredExpressions( const QList< QgsStoredExpression > &storedExpressions )
68 {
69  mStoredExpressions.append( storedExpressions );
70 }
71 
73 {
74  QList< QgsStoredExpression > storedExpressions;
75 
76  for ( const QgsStoredExpression &storedExpression : qgis::as_const( mStoredExpressions ) )
77  {
78  if ( storedExpression.tag & tag )
79  {
80  storedExpressions.append( storedExpression );
81  }
82  }
83  return storedExpressions;
84 }
85 
87 {
88  for ( const QgsStoredExpression &storedExpression : qgis::as_const( mStoredExpressions ) )
89  {
90  if ( storedExpression.id == id )
91  {
92  return storedExpression;
93  }
94  }
95  return QgsStoredExpression();
96 }
97 
99 {
100  for ( const QgsStoredExpression &storedExpression : qgis::as_const( mStoredExpressions ) )
101  {
102  if ( storedExpression.expression == expression && storedExpression.tag & tag )
103  {
104  return storedExpression;
105  }
106  }
107  return QgsStoredExpression();
108 }
109 
111 {
112  mStoredExpressions.clear();
113 }
114 
115 bool QgsStoredExpressionManager::writeXml( QDomNode &layerNode ) const
116 {
117  QDomElement aStoredExpressions = layerNode.ownerDocument().createElement( QStringLiteral( "storedexpressions" ) );
118 
119  for ( const QgsStoredExpression &storedExpression : qgis::as_const( mStoredExpressions ) )
120  {
121  QDomElement aStoredExpression = layerNode.ownerDocument().createElement( QStringLiteral( "storedexpression" ) );
122  aStoredExpression.setAttribute( QStringLiteral( "name" ), storedExpression.name );
123  aStoredExpression.setAttribute( QStringLiteral( "expression" ), storedExpression.expression );
124  aStoredExpression.setAttribute( QStringLiteral( "tag" ), storedExpression.tag );
125  aStoredExpressions.appendChild( aStoredExpression );
126  }
127  layerNode.appendChild( aStoredExpressions );
128 
129  return true;
130 }
131 
132 bool QgsStoredExpressionManager::readXml( const QDomNode &layerNode )
133 {
135 
136  QDomNode aaNode = layerNode.namedItem( QStringLiteral( "storedexpressions" ) );
137 
138  if ( !aaNode.isNull() )
139  {
140  QDomNodeList aStoredExpressions = aaNode.toElement().elementsByTagName( QStringLiteral( "storedexpression" ) );
141  for ( int i = 0; i < aStoredExpressions.size(); ++i )
142  {
143  QDomElement aStoredExpression = aStoredExpressions.at( i ).toElement();
144  addStoredExpression( aStoredExpression.attribute( QStringLiteral( "name" ) ), aStoredExpression.attribute( QStringLiteral( "expression" ) ), QgsStoredExpression::Category( aStoredExpression.attribute( QStringLiteral( "tag" ) ).toInt() ) );
145  }
146  }
147  return true;
148 }
QString addStoredExpression(const QString &name, const QString &expression, const QgsStoredExpression::Category &tag=QgsStoredExpression::Category::FilterExpression)
Adds an expression to the list.
Category
Categories of use cases FilterExpression for stored expressions to filter attribute table DefaultValu...
QgsStoredExpression storedExpression(const QString &id) const
Returns an expression according to the id.
void updateStoredExpression(const QString &id, const QString &name, const QString &expression, const QgsStoredExpression::Category &tag)
Updates an expression by id.
Category tag
category of the expression use case
bool writeXml(QDomNode &layerNode) const
Writes the stored expressions out in XML format.
void addStoredExpressions(const QList< QgsStoredExpression > &storedExpressions)
Appends a list of expressions to the existing list.
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.
QString id
generated uuid used for identification
QString expression
expression text
bool readXml(const QDomNode &layerNode)
Reads the stored expressions in in XML format.
void removeStoredExpression(const QString &id)
Removes an expression to the list.
QString name
descriptive name of the expression
void clearStoredExpressions()
Clears list of stored expressions.
Stored expression containing name, content (expression text) and a category tag.