Skip to content

Commit

Permalink
[FEATURE][processing] New parameter type for datetime (or date, or ti…
Browse files Browse the repository at this point in the history
…me) values

Adds a new parameter type QgsProcessingParameterDateTime for handling
date/datetime/time values
  • Loading branch information
nyalldawson committed Feb 24, 2020
1 parent e7380c7 commit 0efc6f5
Show file tree
Hide file tree
Showing 8 changed files with 1,109 additions and 0 deletions.
183 changes: 183 additions & 0 deletions python/core/auto_generated/processing/qgsprocessingparameters.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,72 @@ Evaluates the parameter with matching ``definition`` to a list of integer values
Evaluates the parameter with matching ``definition`` and ``value`` to a list of integer values.

.. versionadded:: 3.4
%End

static QDateTime parameterAsDateTime( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` to a static datetime value.

.. seealso:: :py:func:`parameterAsDate`

.. seealso:: :py:func:`parameterAsTime`

.. versionadded:: 3.14
%End

static QDateTime parameterAsDateTime( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a static datetime value.

.. seealso:: :py:func:`parameterAsDate`

.. seealso:: :py:func:`parameterAsTime`

.. versionadded:: 3.14
%End

static QDate parameterAsDate( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` to a static date value.

.. seealso:: :py:func:`parameterAsDateTime`

.. seealso:: :py:func:`parameterAsTime`

.. versionadded:: 3.14
%End

static QDate parameterAsDate( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a static date value.

.. seealso:: :py:func:`parameterAsDateTime`

.. seealso:: :py:func:`parameterAsTime`

.. versionadded:: 3.14
%End

static QTime parameterAsTime( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` to a static time value.

.. seealso:: :py:func:`parameterAsDateTime`

.. seealso:: :py:func:`parameterAsDate`

.. versionadded:: 3.14
%End

static QTime parameterAsTime( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a static time value.

.. seealso:: :py:func:`parameterAsDateTime`

.. seealso:: :py:func:`parameterAsDate`

.. versionadded:: 3.14
%End

static int parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
Expand Down Expand Up @@ -3411,6 +3477,123 @@ Creates a new parameter using the definition from a script code.
};


class QgsProcessingParameterDateTime : QgsProcessingParameterDefinition
{
%Docstring
A datetime (or pure date or time) parameter for processing algorithms.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

enum Type
{
DateTime,
Date,
Time,
};

explicit QgsProcessingParameterDateTime( const QString &name, const QString &description = QString(),
Type type = DateTime,
const QVariant &defaultValue = QVariant(),
bool optional = false,
const QDateTime &minValue = QDateTime(),
const QDateTime &maxValue = QDateTime()
);
%Docstring
Constructor for QgsProcessingParameterDateTime.
%End

static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
virtual QgsProcessingParameterDefinition *clone() const /Factory/;

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString toolTip() const;

virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;


QDateTime minimum() const;
%Docstring
Returns the minimum value acceptable by the parameter.

An invalid QDateTime value indicates no minimum value.

.. seealso:: :py:func:`setMinimum`
%End

void setMinimum( const QDateTime &minimum );
%Docstring
Sets the ``minimum`` value acceptable by the parameter.

An invalid QDateTime value indicates no minimum value.

If the dataType() is QgsProcessingParameterDateTime.Time, then the date component of ``minimum``
must be set to any valid date (but this date will not actually be considered when comparing parameter
values to the specified minimum value, only the time component will be considered).

.. seealso:: :py:func:`minimum`
%End

QDateTime maximum() const;
%Docstring
Returns the maximum value acceptable by the parameter.

An invalid QDateTime value indicates no maximum value.

.. seealso:: :py:func:`setMaximum`
%End

void setMaximum( const QDateTime &maximum );
%Docstring
Sets the ``maximum`` value acceptable by the parameter.

An invalid QDateTime value indicates no maximum value.

If the dataType() is QgsProcessingParameterDateTime.Time, then the date component of ``maximum``
must be set to any valid date (but this date will not actually be considered when comparing parameter
values to the specified maximum value, only the time component will be considered).

.. seealso:: :py:func:`maximum`
%End

Type dataType() const;
%Docstring
Returns the acceptable data type for the parameter.

.. seealso:: :py:func:`setDataType`
%End

void setDataType( Type type );
%Docstring
Sets the acceptable data ``type`` for the parameter.

.. seealso:: :py:func:`dataType`
%End

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


static QgsProcessingParameterDateTime *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
%Docstring
Creates a new parameter using the definition from a script code.
%End

};




Expand Down
4 changes: 4 additions & 0 deletions python/processing/algfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
QgsProcessingParameterScale,
QgsProcessingParameterLayout,
QgsProcessingParameterLayoutItem,
QgsProcessingParameterDateTime,
QgsProcessingOutputString,
QgsProcessingOutputBoolean,
QgsProcessingOutputFile,
Expand Down Expand Up @@ -326,6 +327,7 @@ class ProcessingAlgFactory():
COLOR = "COLOR"
LAYOUT = "LAYOUT"
LAYOUT_ITEM = "LAYOUT_ITEM"
DATETIME = "DATETIME"

def __init__(self):
self._current = None
Expand Down Expand Up @@ -458,6 +460,7 @@ def input(self, type, *args, **kwargs):
alg.LAYOUT: QgsProcessingParameterLayout
alg.LAYOUT_ITEM: QgsProcessingParameterLayoutItem
alg.COLOR: QgsProcessingParameterColor
alg.DATETIME: QgsProcessingParameterDateTime
:param type: The type of the input. This should be a type define on `alg` like alg.STRING, alg.DISTANCE
:keyword label: The label of the output. Translates into `description` arg.
Expand Down Expand Up @@ -509,6 +512,7 @@ def dec(f):
ProcessingAlgFactory.LAYOUT: QgsProcessingParameterLayout,
ProcessingAlgFactory.LAYOUT_ITEM: QgsProcessingParameterLayoutItem,
ProcessingAlgFactory.COLOR: QgsProcessingParameterColor,
ProcessingAlgFactory.DATETIME: QgsProcessingParameterDateTime
}

output_type_mapping = {
Expand Down
3 changes: 3 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
{ QStringLiteral( "QgsGeometry" ), QStringLiteral( "from qgis.core import QgsGeometry" ) },
{ QStringLiteral( "QgsProcessingOutputLayerDefinition" ), QStringLiteral( "from qgis.core import QgsProcessingOutputLayerDefinition" ) },
{ QStringLiteral( "QColor" ), QStringLiteral( "from qgis.PyQt.QtGui import QColor" ) },
{ QStringLiteral( "QDateTime" ), QStringLiteral( "from qgis.PyQt.QtCore import QDateTime" ) },
{ QStringLiteral( "QDate" ), QStringLiteral( "from qgis.PyQt.QtCore import QDate" ) },
{ QStringLiteral( "QTime" ), QStringLiteral( "from qgis.PyQt.QtCore import QTime" ) },
};

for ( auto it = sAdditionalImports.constBegin(); it != sAdditionalImports.constEnd(); ++it )
Expand Down
Loading

0 comments on commit 0efc6f5

Please sign in to comment.