Skip to content

Commit

Permalink
[FEATURE][layout] Expose temporal UI for map items
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed May 10, 2020
1 parent ec32e32 commit f105add
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/layout/qgslayoutobject.sip.in
Expand Up @@ -164,6 +164,8 @@ A base class for objects which belong to a layout.
//table item
AttributeTableSourceLayer,
MapCrs,
StartDateTime,
EndDateTime,
};

enum PropertyValueType
Expand Down
34 changes: 34 additions & 0 deletions python/core/auto_generated/qgsproperty.sip.in
Expand Up @@ -55,6 +55,7 @@ commonly used property types, such as colors and blend modes.
VerticalAnchor,
SvgPath,
Offset,
DateTime,
Custom,
};

Expand Down Expand Up @@ -346,6 +347,29 @@ Calculates the current value of the property, including any transforms which are

.. seealso:: :py:func:`valueAsInt`

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

QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok /Out/ = 0 ) const;
%Docstring
Calculates the current value of the property and interprets it as a datetime.

:param context: QgsExpressionContext to evaluate the property for.
:param defaultDateTime: default datetime to return if the property cannot be calculated as a datetime

:return: - value parsed to datetime
- ok: will be set to ``True`` if conversion was successful

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`

.. seealso:: :py:func:`valueAsDouble`

.. seealso:: :py:func:`valueAsInt`

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

Expand All @@ -361,6 +385,8 @@ Calculates the current value of the property and interprets it as a string.

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsColor`

.. seealso:: :py:func:`valueAsDouble`
Expand All @@ -382,6 +408,8 @@ Calculates the current value of the property and interprets it as a color.

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsDouble`
Expand All @@ -403,6 +431,8 @@ Calculates the current value of the property and interprets it as a double.

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`
Expand All @@ -424,6 +454,8 @@ Calculates the current value of the property and interprets it as an integer.

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`
Expand All @@ -445,6 +477,8 @@ Calculates the current value of the property and interprets it as an boolean.

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`
Expand Down
36 changes: 36 additions & 0 deletions python/core/auto_generated/qgspropertycollection.sip.in
Expand Up @@ -8,6 +8,7 @@




typedef QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition;


Expand Down Expand Up @@ -108,6 +109,31 @@ valueAs* variants) refer to the property() and :py:func:`QgsProperty.value()`

.. seealso:: :py:func:`valueAsInt`

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

QDateTime valueAsDateTime( int key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok /Out/ = 0 ) const;
%Docstring
Calculates the current value of the property with the specified key and interprets it as a string.

:param key: integer key for property to return. The intended use case is that a context specific enum is cast to
int and used for the key value.
:param context: QgsExpressionContext to evaluate the property for.
:param defaultString: default string to return if the property cannot be calculated as a string

:return: - value parsed to string
- ok: will be set to ``True`` if conversion was successful

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`

.. seealso:: :py:func:`valueAsDouble`

.. seealso:: :py:func:`valueAsInt`

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

Expand All @@ -125,6 +151,8 @@ Calculates the current value of the property with the specified key and interpre

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsColor`

.. seealso:: :py:func:`valueAsDouble`
Expand All @@ -148,6 +176,8 @@ Calculates the current value of the property with the specified key and interpre

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsDouble`
Expand All @@ -171,6 +201,8 @@ Calculates the current value of the property with the specified key and interpre

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`
Expand All @@ -194,6 +226,8 @@ Calculates the current value of the property with the specified key and interpre

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`
Expand All @@ -217,6 +251,8 @@ Calculates the current value of the property with the specified key and interpre

.. seealso:: :py:func:`value`

.. seealso:: :py:func:`valueAsDateTime`

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsColor`
Expand Down
15 changes: 14 additions & 1 deletion src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -818,7 +818,7 @@ bool QgsLayoutItemMap::readPropertiesFromElement( const QDomElement &itemElem, c
if ( isTemporal() )
{
QDateTime begin = QDateTime::fromString( itemElem.attribute( QStringLiteral( "temporalRangeBegin" ) ), Qt::ISODate );
QDateTime end = QDateTime::fromString( itemElem.attribute( QStringLiteral( "temporalRangeBegin" ) ), Qt::ISODate );
QDateTime end = QDateTime::fromString( itemElem.attribute( QStringLiteral( "temporalRangeEnd" ) ), Qt::ISODate );
setTemporalRange( QgsDateTimeRange( begin, end ) );
}

Expand Down Expand Up @@ -1766,6 +1766,19 @@ void QgsLayoutItemMap::refreshDataDefinedProperty( const QgsLayoutObject::DataDe
emit themeChanged( mLastEvaluatedThemeName );
}

if ( isTemporal() && ( property == QgsLayoutObject::StartDateTime || property == QgsLayoutObject::EndDateTime || property == QgsLayoutObject::AllProperties ) )
{
QDateTime begin = temporalRange().begin();
QDateTime end = temporalRange().end();

if ( property == QgsLayoutObject::StartDateTime || property == QgsLayoutObject::AllProperties )
begin = mDataDefinedProperties.valueAsDateTime( QgsLayoutObject::StartDateTime, context, temporalRange().begin() );
if ( property == QgsLayoutObject::EndDateTime || property == QgsLayoutObject::AllProperties )
end = mDataDefinedProperties.valueAsDateTime( QgsLayoutObject::EndDateTime, context, temporalRange().end() );

setTemporalRange( QgsDateTimeRange( begin, end ) );
}

//force redraw
mCacheInvalidated = true;

Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutobject.cpp
Expand Up @@ -91,6 +91,8 @@ void QgsLayoutObject::initPropertyDefinitions()
{ QgsLayoutObject::ScalebarLineWidth, QgsPropertyDefinition( "dataDefinedScalebarLineWidth", QObject::tr( "Line width" ), QgsPropertyDefinition::StrokeWidth ) },
{ QgsLayoutObject::AttributeTableSourceLayer, QgsPropertyDefinition( "dataDefinedAttributeTableSourceLayer", QObject::tr( "Table source layer" ), QgsPropertyDefinition::String ) },
{ QgsLayoutObject::MapCrs, QgsPropertyDefinition( "dataDefinedCrs", QgsPropertyDefinition::DataTypeString, QObject::tr( "Map CRS" ), QObject::tr( "string representing a CRS, either an authority/id pair (e.g. \"EPSG:4326\"), a proj string prefixes by \"PROJ:\" (e.g. \"PROJ: +proj=...\") or a WKT string prefixed by \"WKT:\" (e.g. \"WKT:GEOGCS[\"WGS 84\"...)" ) ) },
{ QgsLayoutObject::StartDateTime, QgsPropertyDefinition( "dataDefinedStartDateTime", QObject::tr( "Temporal range start date / time" ), QgsPropertyDefinition::DateTime ) },
{ QgsLayoutObject::EndDateTime, QgsPropertyDefinition( "dataDefinedEndDateTime", QObject::tr( "Temporal range end date / time" ), QgsPropertyDefinition::DateTime ) },
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutobject.h
Expand Up @@ -192,6 +192,8 @@ class CORE_EXPORT QgsLayoutObject: public QObject, public QgsExpressionContextGe
//table item
AttributeTableSourceLayer, //!< Attribute table source layer
MapCrs, //!< Map CRS
StartDateTime, //!< Temporal range's start DateTime
EndDateTime, //!< Temporal range's end DateTime
};

/**
Expand Down
33 changes: 33 additions & 0 deletions src/core/qgsproperty.cpp
Expand Up @@ -167,6 +167,11 @@ QgsPropertyDefinition::QgsPropertyDefinition( const QString &name, const QString
mHelpText = QObject::tr( "string of doubles '<b>x,y</b>' or array of doubles <b>[x, y]</b>" );
break;

case DateTime:
mTypes = DataTypeString;
mHelpText = QObject::tr( "DateTime or string representation of a DateTime" );
break;

case Custom:
mTypes = DataTypeString;
}
Expand Down Expand Up @@ -534,6 +539,34 @@ QVariant QgsProperty::value( const QgsExpressionContext &context, const QVariant
return val;
}

QDateTime QgsProperty::valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime, bool *ok ) const
{
bool valOk = false;
QVariant val = value( context, defaultDateTime, &valOk );

if ( !valOk || !val.isValid() )
return defaultDateTime;

QDateTime dateTime;
if ( val.type() == QVariant::DateTime )
{
dateTime = val.value<QDateTime>();
}
else
{
dateTime = val.toDateTime();
}

if ( !dateTime.isValid() )
return defaultDateTime;
else
{
if ( ok )
*ok = true;
return dateTime;
}
}

QString QgsProperty::valueAsString( const QgsExpressionContext &context, const QString &defaultString, bool *ok ) const
{
bool valOk = false;
Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsproperty.h
Expand Up @@ -28,6 +28,7 @@
#include <QDomElement>
#include <QDomDocument>
#include <QColor>
#include <QDateTime>

class QgsPropertyTransformer;
class QgsPropertyPrivate;
Expand Down Expand Up @@ -76,6 +77,7 @@ class CORE_EXPORT QgsPropertyDefinition
VerticalAnchor, //!< Vertical anchor point
SvgPath, //!< Path to an SVG file
Offset, //!< 2D offset
DateTime, //!< 2D offset
Custom = 3000, //!< Custom property types
};

Expand Down Expand Up @@ -378,13 +380,29 @@ class CORE_EXPORT QgsProperty
*/
QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;

/**
* Calculates the current value of the property and interprets it as a datetime.
* \param context QgsExpressionContext to evaluate the property for.
* \param defaultDateTime default datetime to return if the property cannot be calculated as a datetime
* \param ok if specified, will be set to TRUE if conversion was successful
* \returns value parsed to datetime
* \see value()
* \see valueAsString()
* \see valueAsColor()
* \see valueAsDouble()
* \see valueAsInt()
* \see valueAsBool()
*/
QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;

/**
* Calculates the current value of the property and interprets it as a string.
* \param context QgsExpressionContext to evaluate the property for.
* \param defaultString default string to return if the property cannot be calculated as a string
* \param ok if specified, will be set to TRUE if conversion was successful
* \returns value parsed to string
* \see value()
* \see valueAsDateTime()
* \see valueAsColor()
* \see valueAsDouble()
* \see valueAsInt()
Expand All @@ -399,6 +417,7 @@ class CORE_EXPORT QgsProperty
* \param ok if specified, will be set to TRUE if conversion was successful
* \returns value parsed to color
* \see value()
* \see valueAsDateTime()
* \see valueAsString()
* \see valueAsDouble()
* \see valueAsInt()
Expand All @@ -413,6 +432,7 @@ class CORE_EXPORT QgsProperty
* \param ok if specified, will be set to TRUE if conversion was successful
* \returns value parsed to double
* \see value()
* \see valueAsDateTime()
* \see valueAsString()
* \see valueAsColor()
* \see valueAsInt()
Expand All @@ -427,6 +447,7 @@ class CORE_EXPORT QgsProperty
* \param ok if specified, will be set to TRUE if conversion was successful
* \returns value parsed to integer
* \see value()
* \see valueAsDateTime()
* \see valueAsString()
* \see valueAsColor()
* \see valueAsDouble()
Expand All @@ -441,6 +462,7 @@ class CORE_EXPORT QgsProperty
* \param ok if specified, will be set to TRUE if conversion was successful
* \returns value parsed to boolean
* \see value()
* \see valueAsDateTime()
* \see valueAsString()
* \see valueAsColor()
* \see valueAsDouble()
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgspropertycollection.cpp
Expand Up @@ -27,6 +27,18 @@ QgsAbstractPropertyCollection::QgsAbstractPropertyCollection( const QString &nam

}

QDateTime QgsAbstractPropertyCollection::valueAsDateTime( int key, const QgsExpressionContext &context, const QDateTime &defaultDateTime, bool *ok ) const
{
if ( ok )
*ok = false;

QgsProperty prop = property( key );
if ( !prop || !prop.isActive() )
return defaultDateTime;

return prop.valueAsDateTime( context, defaultDateTime, ok );
}

QString QgsAbstractPropertyCollection::valueAsString( int key, const QgsExpressionContext &context, const QString &defaultString, bool *ok ) const
{
if ( ok )
Expand Down

0 comments on commit f105add

Please sign in to comment.