Skip to content

Commit 3fc7d17

Browse files
committedSep 9, 2016
Add QgsOptional and QgsOptionalExpression
1 parent d06c11f commit 3fc7d17

File tree

10 files changed

+360
-2
lines changed

10 files changed

+360
-2
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
%Include qgsobjectcustomproperties.sip
102102
%Include qgsofflineediting.sip
103103
%Include qgsogcutils.sip
104+
%Include qgsoptionalexpression.sip
104105
%Include qgsowsconnection.sip
105106
%Include qgspaintenginehack.sip
106107
%Include qgspallabeling.sip

‎python/core/qgsexpression.sip

Lines changed: 21 additions & 0 deletions
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
/**
@@ -143,6 +150,20 @@ class QgsExpression
143150

144151
double scale();
145152

153+
/**
154+
* Set the expression string, will reset the whole internal structure.
155+
*
156+
* @note Added in QGIS 3.0
157+
*/
158+
void setExpression( const QString& expression );
159+
160+
/**
161+
* Set the expression string, will reset the whole internal structure.
162+
*
163+
* @note Added in QGIS 3.0
164+
*/
165+
void setExpression( const QString& expression );
166+
146167
//! Return the original, unmodified expression string.
147168
//! If there was none supplied because it was constructed by sole
148169
//! API calls, dump() will be used to create one instead.

‎python/core/qgsoptionalexpression.sip

Lines changed: 83 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ SET(QGIS_CORE_SRCS
163163
qgsofflineediting.cpp
164164
qgsogcutils.cpp
165165
qgsogrutils.cpp
166+
qgsoptional.cpp
166167
qgsowsconnection.cpp
167168
qgspaintenginehack.cpp
168169
qgspallabeling.cpp
@@ -677,6 +678,8 @@ SET(QGIS_CORE_HDRS
677678
qgsmultirenderchecker.h
678679
qgsobjectcustomproperties.h
679680
qgsogcutils.h
681+
qgsoptional.h
682+
qgsoptionalexpression.h
680683
qgsowsconnection.h
681684
qgspaintenginehack.h
682685
qgspallabeling.h

‎src/core/qgsexpression.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class CORE_EXPORT QgsExpression
124124
* it does not need to be re-parsed.
125125
*/
126126
QgsExpression( const QgsExpression& other );
127+
127128
/**
128129
* Create a copy of this expression. This is preferred
129130
* over recreating an expression from a string since
@@ -132,7 +133,9 @@ class CORE_EXPORT QgsExpression
132133
QgsExpression& operator=( const QgsExpression& other );
133134

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

@@ -176,7 +179,8 @@ class CORE_EXPORT QgsExpression
176179

177180
/**
178181
* Get list of columns referenced by the expression.
179-
* @note if the returned list contains the QgsFeatureRequest::AllAttributes constant then
182+
*
183+
* @note If the returned list contains the QgsFeatureRequest::AllAttributes constant then
180184
* all attributes from the layer are required for evaluation of the expression.
181185
* QgsFeatureRequest::setSubsetOfAttributes automatically handles this case.
182186
*

‎src/core/qgsoptional.cpp

Lines changed: 16 additions & 0 deletions
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

Lines changed: 137 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ADD_PYTHON_TEST(PyQgsNullSymbolRenderer test_qgsnullsymbolrenderer.py)
6363
ADD_PYTHON_TEST(PyQgsNewGeoPackageLayerDialog test_qgsnewgeopackagelayerdialog.py)
6464
ADD_PYTHON_TEST(PyQgsOGRProviderGpkg test_provider_ogr_gpkg.py)
6565
ADD_PYTHON_TEST(PyQgsOGRProviderSqlite test_provider_ogr_sqlite.py)
66+
ADD_PYTHON_TEST(PyQgsOptional test_qgsoptional.py)
6667
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
6768
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
6869
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)

‎tests/src/python/test_qgsoptional.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
test_qgsoptional.py
4+
--------------------------------------
5+
Date : September 2016
6+
Copyright : (C) 2016 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+
import qgis # NOQA
19+
20+
from qgis.testing import unittest
21+
from qgis.core import QgsOptionalExpression, QgsExpression
22+
23+
24+
class TestQgsOptional(unittest.TestCase):
25+
26+
def setUp(self):
27+
"""Run before each test."""
28+
pass
29+
30+
def tearDown(self):
31+
"""Run after each test."""
32+
pass
33+
34+
def testQgsOptionalExpression(self):
35+
opt = QgsOptionalExpression()
36+
self.assertFalse(opt.enabled())
37+
38+
opt = QgsOptionalExpression(QgsExpression('true'))
39+
self.assertTrue(opt.enabled())
40+
self.assertEquals(opt.data().expression(), 'true')
41+
opt.setEnabled(False)
42+
self.assertFalse(opt.enabled())
43+
# boolean operator not yet working in python
44+
# self.assertFalse(opt)
45+
self.assertEquals(opt.data().expression(), 'true')
46+
opt.setEnabled(True)
47+
self.assertTrue(opt.enabled())
48+
# self.assertTrue(opt)
49+
self.assertEquals(opt.data().expression(), 'true')
50+
opt.setData(QgsExpression('xyz'))
51+
self.assertTrue(opt.enabled())
52+
self.assertEquals(opt.data().expression(), 'xyz')
53+
54+
opt = QgsOptionalExpression(QgsExpression('true'), False)
55+
self.assertFalse(opt.enabled())
56+
57+
58+
if __name__ == '__main__':
59+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.