Skip to content

Commit 8cf0ef9

Browse files
committed
Add QgsOptional and QgsOptionalExpression
1 parent 760816b commit 8cf0ef9

10 files changed

+353
-2
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
%Include qgsobjectcustomproperties.sip
106106
%Include qgsofflineediting.sip
107107
%Include qgsogcutils.sip
108+
%Include qgsoptionalexpression.sip
108109
%Include qgsowsconnection.sip
109110
%Include qgspaintenginehack.sip
110111
%Include qgspainting.sip

python/core/qgsexpression.sip

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class QgsExpression
1212
* loop in which this expression is used.
1313
*/
1414
QgsExpression( const QString& expr );
15+
/**
16+
* Create an empty expression.
17+
*
18+
* @note Added in QGIS 3.0
19+
*/
20+
QgsExpression();
21+
1522
~QgsExpression();
1623

1724
/**
@@ -85,6 +92,13 @@ class QgsExpression
8592
*/
8693
static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
8794

95+
/**
96+
* Set the expression string, will reset the whole internal structure.
97+
*
98+
* @note Added in QGIS 3.0
99+
*/
100+
void setExpression( const QString& expression );
101+
88102
//! Return the original, unmodified expression string.
89103
//! If there was none supplied because it was constructed by sole
90104
//! API calls, dump() will be used to create one instead.

python/core/qgsoptionalexpression.sip

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/***************************************************************************
2+
qgsoptionalexpression.sip - QgsOptionalExpression
3+
4+
---------------------
5+
begin : 8.9.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
/**
18+
* \ingroup core
19+
*
20+
* QgsOptionalExpression is a container for an expression with an additional enabled/disabled flag.
21+
*
22+
* @note Added in QGIS 3.0
23+
*/
24+
class QgsOptionalExpression
25+
{
26+
%TypeHeaderCode
27+
#include <qgsoptionalexpression.h>
28+
%End
29+
public:
30+
/**
31+
* A QgsOptionalExpression is disabled by default if default constructed.
32+
*/
33+
QgsOptionalExpression();
34+
35+
/**
36+
* A QgsOptionalExpression is enabled by default if constructed with an expression.
37+
*/
38+
QgsOptionalExpression( const QgsExpression& data );
39+
40+
/**
41+
* A QgsOptionalExptression constructed with enabled status and data
42+
*/
43+
QgsOptionalExpression( const QgsExpression& data, bool enabled );
44+
45+
/**
46+
* Compare this QgsOptionalExptression to another one.
47+
*
48+
* This will compare the enabled flag and call the == operator
49+
* of the contained class.
50+
*
51+
* @note Added in QGIS 3.0
52+
*/
53+
bool operator== ( const QgsOptionalExpression& other ) const;
54+
operator bool () const;
55+
56+
/**
57+
* Check if this optional is enabled
58+
*
59+
* @note Added in QGIS 3.0
60+
*/
61+
bool enabled() const;
62+
63+
/**
64+
* Set if this optional is enabled
65+
*
66+
* @note Added in QGIS 3.0
67+
*/
68+
void setEnabled( bool enabled );
69+
70+
/**
71+
* Access the payload data
72+
*
73+
* @note Added in QGIS 3.0
74+
*/
75+
QgsExpression data() const;
76+
77+
/**
78+
* Set the payload data
79+
*
80+
* @note Added in QGIS 3.0
81+
*/
82+
void setData( const QgsExpression& data );
83+
};

src/core/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ SET(QGIS_CORE_SRCS
164164
qgsofflineediting.cpp
165165
qgsogcutils.cpp
166166
qgsogrutils.cpp
167+
qgsoptional.cpp
167168
qgsowsconnection.cpp
168169
qgspaintenginehack.cpp
169170
qgspainting.cpp
@@ -672,6 +673,8 @@ SET(QGIS_CORE_HDRS
672673
qgsmultirenderchecker.h
673674
qgsobjectcustomproperties.h
674675
qgsogcutils.h
676+
qgsoptional.h
677+
qgsoptionalexpression.h
675678
qgsowsconnection.h
676679
qgspaintenginehack.h
677680
qgspainting.h

src/core/qgsexpression.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class CORE_EXPORT QgsExpression
123123
* it does not need to be re-parsed.
124124
*/
125125
QgsExpression( const QgsExpression& other );
126+
126127
/**
127128
* Create a copy of this expression. This is preferred
128129
* over recreating an expression from a string since
@@ -131,7 +132,9 @@ class CORE_EXPORT QgsExpression
131132
QgsExpression& operator=( const QgsExpression& other );
132133

133134
/**
134-
* Create an empty expression
135+
* Create an empty expression.
136+
*
137+
* @note Added in QGIS 3.0
135138
*/
136139
QgsExpression();
137140

@@ -171,7 +174,8 @@ class CORE_EXPORT QgsExpression
171174

172175
/**
173176
* Get list of columns referenced by the expression.
174-
* @note if the returned list contains the QgsFeatureRequest::AllAttributes constant then
177+
*
178+
* @note If the returned list contains the QgsFeatureRequest::AllAttributes constant then
175179
* all attributes from the layer are required for evaluation of the expression.
176180
* QgsFeatureRequest::setSubsetOfAttributes automatically handles this case.
177181
*

src/core/qgsoptional.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/***************************************************************************
2+
qgsoptional.cpp - QgsOptional
3+
4+
---------------------
5+
begin : 7.9.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgsoptional.h"

src/core/qgsoptional.h

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/***************************************************************************
2+
qgsoptional.h - QgsOptional
3+
4+
---------------------
5+
begin : 7.9.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSOPTIONAL_H
17+
#define QGSOPTIONAL_H
18+
19+
/**
20+
* \ingroup core
21+
*
22+
* \brief
23+
* QgsOptional is a container for other classes and adds an additional enabled/disabled flag.
24+
*
25+
* Often it is used for configuration options which can be enabled or disabled but also have
26+
* more internal configuration information that should not be lost when disabling and re-enabling.
27+
*
28+
* @note Added in QGIS 3.0
29+
* @note For python you need to use implementations for specific template classes
30+
*/
31+
template<class T>
32+
class CORE_EXPORT QgsOptional
33+
{
34+
public:
35+
/**
36+
* A QgsOptional is disabled by default if default constructed.
37+
*/
38+
QgsOptional()
39+
: mEnabled( false )
40+
{
41+
}
42+
43+
/**
44+
* A QgsOptional is enabled by default if constructed with payload.
45+
*/
46+
QgsOptional( const T& data )
47+
: mEnabled( true )
48+
, mData( data )
49+
{
50+
}
51+
52+
/**
53+
* A QgsOptional constructed with enabled status and data
54+
*/
55+
QgsOptional( const T& data, bool enabled )
56+
: mEnabled( enabled )
57+
, mData( data )
58+
{
59+
}
60+
61+
/**
62+
* Compare this QgsOptional to another one.
63+
*
64+
* This will compare the enabled flag and call the == operator
65+
* of the contained class.
66+
*
67+
* @note Added in QGIS 3.0
68+
*/
69+
bool operator== ( const QgsOptional<T>& other ) const
70+
{
71+
return mEnabled == other.mEnabled && mData == other.mData;
72+
}
73+
74+
/**
75+
* Boolean operator. Will return true if this optional is enabled.
76+
*/
77+
operator bool() const
78+
{
79+
return mEnabled;
80+
}
81+
82+
/**
83+
* Check if this optional is enabled
84+
*
85+
* @note Added in QGIS 3.0
86+
*/
87+
bool enabled() const
88+
{
89+
return mEnabled;
90+
}
91+
92+
/**
93+
* Set if this optional is enabled
94+
*
95+
* @note Added in QGIS 3.0
96+
*/
97+
void setEnabled( bool enabled )
98+
{
99+
mEnabled = enabled;
100+
}
101+
102+
/**
103+
* Access the payload data
104+
*
105+
* @note Added in QGIS 3.0
106+
*/
107+
const T* operator->() const
108+
{
109+
return &mData;
110+
}
111+
112+
/**
113+
* Access the payload data
114+
*
115+
* @note Added in QGIS 3.0
116+
*/
117+
T data() const
118+
{
119+
return mData;
120+
}
121+
122+
/**
123+
* Set the payload data
124+
*
125+
* @note Added in QGIS 3.0
126+
*/
127+
void setData( const T& data )
128+
{
129+
mData = data;
130+
}
131+
132+
private:
133+
T mData;
134+
bool mEnabled;
135+
};
136+
137+
#endif // QGSOPTIONAL_H

src/core/qgsoptionalexpression.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/***************************************************************************
2+
qgsoptionalexpression - %{Cpp:License:ClassName}
3+
4+
---------------------
5+
begin : 8.9.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSOPTIONALEXPRESSION_H
17+
#define QGSOPTIONALEXPRESSION_H
18+
19+
#include "qgsoptional.h"
20+
#include "qgsexpression.h"
21+
22+
/**
23+
* An expression with an additional enabled flag.
24+
*
25+
* This can be used for configuration options where an expression can be enabled
26+
* or diabled but when disabled it shouldn't lose it's information for the case
27+
* it gets re-enabled later on and a user shoulnd't be force to redo the configuration.
28+
*
29+
* Added in QGIS 3.0
30+
*/
31+
typedef QgsOptional<QgsExpression> QgsOptionalExpression;
32+
33+
#endif // QGSOPTIONALEXPRESSION_H

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ ADD_PYTHON_TEST(PyQgsNullSymbolRenderer test_qgsnullsymbolrenderer.py)
6262
ADD_PYTHON_TEST(PyQgsNewGeoPackageLayerDialog test_qgsnewgeopackagelayerdialog.py)
6363
ADD_PYTHON_TEST(PyQgsOGRProviderGpkg test_provider_ogr_gpkg.py)
6464
ADD_PYTHON_TEST(PyQgsOGRProviderSqlite test_provider_ogr_sqlite.py)
65+
ADD_PYTHON_TEST(PyQgsOptional test_qgsoptional.py)
6566
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
6667
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
6768
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)

0 commit comments

Comments
 (0)