Skip to content
Permalink
Browse files

Add QgsOptional and QgsOptionalExpression

  • Loading branch information
m-kuhn committed Sep 9, 2016
1 parent 760816b commit 8cf0ef93c454a02362377e5bce2d8c8c8b6015a4
@@ -105,6 +105,7 @@
%Include qgsobjectcustomproperties.sip
%Include qgsofflineediting.sip
%Include qgsogcutils.sip
%Include qgsoptionalexpression.sip
%Include qgsowsconnection.sip
%Include qgspaintenginehack.sip
%Include qgspainting.sip
@@ -12,6 +12,13 @@ class QgsExpression
* loop in which this expression is used.
*/
QgsExpression( const QString& expr );
/**
* Create an empty expression.
*
* @note Added in QGIS 3.0
*/
QgsExpression();

~QgsExpression();

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

/**
* Set the expression string, will reset the whole internal structure.
*
* @note Added in QGIS 3.0
*/
void setExpression( const QString& expression );

//! Return the original, unmodified expression string.
//! If there was none supplied because it was constructed by sole
//! API calls, dump() will be used to create one instead.
@@ -0,0 +1,83 @@
/***************************************************************************
qgsoptionalexpression.sip - QgsOptionalExpression

---------------------
begin : 8.9.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

/**
* \ingroup core
*
* QgsOptionalExpression is a container for an expression with an additional enabled/disabled flag.
*
* @note Added in QGIS 3.0
*/
class QgsOptionalExpression
{
%TypeHeaderCode
#include <qgsoptionalexpression.h>
%End
public:
/**
* A QgsOptionalExpression is disabled by default if default constructed.
*/
QgsOptionalExpression();

/**
* A QgsOptionalExpression is enabled by default if constructed with an expression.
*/
QgsOptionalExpression( const QgsExpression& data );

/**
* A QgsOptionalExptression constructed with enabled status and data
*/
QgsOptionalExpression( const QgsExpression& data, bool enabled );

/**
* Compare this QgsOptionalExptression to another one.
*
* This will compare the enabled flag and call the == operator
* of the contained class.
*
* @note Added in QGIS 3.0
*/
bool operator== ( const QgsOptionalExpression& other ) const;
operator bool () const;

/**
* Check if this optional is enabled
*
* @note Added in QGIS 3.0
*/
bool enabled() const;

/**
* Set if this optional is enabled
*
* @note Added in QGIS 3.0
*/
void setEnabled( bool enabled );

/**
* Access the payload data
*
* @note Added in QGIS 3.0
*/
QgsExpression data() const;

/**
* Set the payload data
*
* @note Added in QGIS 3.0
*/
void setData( const QgsExpression& data );
};
@@ -164,6 +164,7 @@ SET(QGIS_CORE_SRCS
qgsofflineediting.cpp
qgsogcutils.cpp
qgsogrutils.cpp
qgsoptional.cpp
qgsowsconnection.cpp
qgspaintenginehack.cpp
qgspainting.cpp
@@ -672,6 +673,8 @@ SET(QGIS_CORE_HDRS
qgsmultirenderchecker.h
qgsobjectcustomproperties.h
qgsogcutils.h
qgsoptional.h
qgsoptionalexpression.h
qgsowsconnection.h
qgspaintenginehack.h
qgspainting.h
@@ -123,6 +123,7 @@ class CORE_EXPORT QgsExpression
* it does not need to be re-parsed.
*/
QgsExpression( const QgsExpression& other );

/**
* Create a copy of this expression. This is preferred
* over recreating an expression from a string since
@@ -131,7 +132,9 @@ class CORE_EXPORT QgsExpression
QgsExpression& operator=( const QgsExpression& other );

/**
* Create an empty expression
* Create an empty expression.
*
* @note Added in QGIS 3.0
*/
QgsExpression();

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

/**
* Get list of columns referenced by the expression.
* @note if the returned list contains the QgsFeatureRequest::AllAttributes constant then
*
* @note If the returned list contains the QgsFeatureRequest::AllAttributes constant then
* all attributes from the layer are required for evaluation of the expression.
* QgsFeatureRequest::setSubsetOfAttributes automatically handles this case.
*
@@ -0,0 +1,16 @@
/***************************************************************************
qgsoptional.cpp - QgsOptional
---------------------
begin : 7.9.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsoptional.h"
@@ -0,0 +1,137 @@
/***************************************************************************
qgsoptional.h - QgsOptional
---------------------
begin : 7.9.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSOPTIONAL_H
#define QGSOPTIONAL_H

/**
* \ingroup core
*
* \brief
* QgsOptional is a container for other classes and adds an additional enabled/disabled flag.
*
* Often it is used for configuration options which can be enabled or disabled but also have
* more internal configuration information that should not be lost when disabling and re-enabling.
*
* @note Added in QGIS 3.0
* @note For python you need to use implementations for specific template classes
*/
template<class T>
class CORE_EXPORT QgsOptional
{
public:
/**
* A QgsOptional is disabled by default if default constructed.
*/
QgsOptional()
: mEnabled( false )
{
}

/**
* A QgsOptional is enabled by default if constructed with payload.
*/
QgsOptional( const T& data )
: mEnabled( true )
, mData( data )
{
}

/**
* A QgsOptional constructed with enabled status and data
*/
QgsOptional( const T& data, bool enabled )
: mEnabled( enabled )
, mData( data )
{
}

/**
* Compare this QgsOptional to another one.
*
* This will compare the enabled flag and call the == operator
* of the contained class.
*
* @note Added in QGIS 3.0
*/
bool operator== ( const QgsOptional<T>& other ) const
{
return mEnabled == other.mEnabled && mData == other.mData;
}

/**
* Boolean operator. Will return true if this optional is enabled.
*/
operator bool() const
{
return mEnabled;
}

/**
* Check if this optional is enabled
*
* @note Added in QGIS 3.0
*/
bool enabled() const
{
return mEnabled;
}

/**
* Set if this optional is enabled
*
* @note Added in QGIS 3.0
*/
void setEnabled( bool enabled )
{
mEnabled = enabled;
}

/**
* Access the payload data
*
* @note Added in QGIS 3.0
*/
const T* operator->() const
{
return &mData;
}

/**
* Access the payload data
*
* @note Added in QGIS 3.0
*/
T data() const
{
return mData;
}

/**
* Set the payload data
*
* @note Added in QGIS 3.0
*/
void setData( const T& data )
{
mData = data;
}

private:
T mData;
bool mEnabled;
};

#endif // QGSOPTIONAL_H
@@ -0,0 +1,33 @@
/***************************************************************************
qgsoptionalexpression - %{Cpp:License:ClassName}
---------------------
begin : 8.9.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSOPTIONALEXPRESSION_H
#define QGSOPTIONALEXPRESSION_H

#include "qgsoptional.h"
#include "qgsexpression.h"

/**
* An expression with an additional enabled flag.
*
* This can be used for configuration options where an expression can be enabled
* or diabled but when disabled it shouldn't lose it's information for the case
* it gets re-enabled later on and a user shoulnd't be force to redo the configuration.
*
* Added in QGIS 3.0
*/
typedef QgsOptional<QgsExpression> QgsOptionalExpression;

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 13, 2016

Contributor

Hi @m-kuhn, I can't build QGIS in vs2010 and vs2015 from this commit. I get these unresolve external symbols.

image

What can I do to resolve it? I should create a new bug entry in the issues repo?
Thanks in advance

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn via email Sep 13, 2016

Author Member

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 13, 2016

Contributor

I think so, but I remove all output dirs.

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 13, 2016

Contributor

Same results, but I build succesfully with this baffling workaround...

image

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Sep 13, 2016

Author Member

Maybe by implementing at least one method the compiler is forced to actually build the object?
Let's give it a try to add writeXml / readXml methods in QgsOptionalExpression.cpp.

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 13, 2016

Contributor

Maybe by implementing at least one method the compiler is forced to actually build the object?

I think so

Let's give it a try to add writeXml / readXml methods in QgsOptionalExpression.cpp.

Thanks, I do all tests that you ask me

This comment has been minimized.

Copy link
@m-kuhn

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 14, 2016

Contributor

Perfect! I build successfully! thanks @m-kuhn !

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Sep 14, 2016

Author Member

Just to be sure, did you try the latest one with writeXml method or the one with the template CORE_EXPORT code?

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 14, 2016

Contributor

I used the commit with the template CORE_EXPORT code

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Sep 14, 2016

Author Member

That was producing tons of warnings here. They could be sorted out but I think the later approach should also work, so if you have the chance to test it, that will be much appreciated :)

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Sep 14, 2016

Contributor

It builds successfully too!


#endif // QGSOPTIONALEXPRESSION_H
@@ -62,6 +62,7 @@ ADD_PYTHON_TEST(PyQgsNullSymbolRenderer test_qgsnullsymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsNewGeoPackageLayerDialog test_qgsnewgeopackagelayerdialog.py)
ADD_PYTHON_TEST(PyQgsOGRProviderGpkg test_provider_ogr_gpkg.py)
ADD_PYTHON_TEST(PyQgsOGRProviderSqlite test_provider_ogr_sqlite.py)
ADD_PYTHON_TEST(PyQgsOptional test_qgsoptional.py)
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)

0 comments on commit 8cf0ef9

Please sign in to comment.
You can’t perform that action at this time.