QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsprojecttimesettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojecttimesettings.cpp
3 ---------------
4 begin : February 2020
5 copyright : (C) 2020 by Samweli Mwakisambwe
6 email : samweli at kartoza dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgis.h"
20#include "qgsunittypes.h"
21#include <QDomElement>
22
24 : QObject( parent )
25{
26
27}
28
30{
31 mRange = QgsDateTimeRange();
33}
34
36{
37 return mRange;
38}
39
40void QgsProjectTimeSettings::setTemporalRange( const QgsDateTimeRange &range )
41{
42 if ( range == mRange )
43 return;
44 mRange = range;
45
47}
48
49bool QgsProjectTimeSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
50{
51 const QDomElement temporalElement = element.firstChildElement( QStringLiteral( "TemporalRange" ) );
52 if ( !temporalElement.isNull() )
53 {
54 const QDomNode begin = temporalElement.namedItem( QStringLiteral( "start" ) );
55 const QDomNode end = temporalElement.namedItem( QStringLiteral( "end" ) );
56
57 const QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
58 const QDateTime endDate = QDateTime::fromString( end.toElement().text(), Qt::ISODate );
59
60 setTemporalRange( QgsDateTimeRange( beginDate, endDate ) );
61
62 }
63
64 mTimeStepUnit = QgsUnitTypes::decodeTemporalUnit( element.attribute( QStringLiteral( "timeStepUnit" ), QgsUnitTypes::encodeUnit( Qgis::TemporalUnit::Hours ) ) );
65 mTimeStep = element.attribute( QStringLiteral( "timeStep" ), "1" ).toDouble();
66 mFrameRate = element.attribute( QStringLiteral( "frameRate" ), "1" ).toDouble();
67 mCumulativeTemporalRange = element.attribute( QStringLiteral( "cumulativeTemporalRange" ), "0" ).toInt();
68
69 return true;
70}
71
72QDomElement QgsProjectTimeSettings::writeXml( QDomDocument &document, const QgsReadWriteContext & ) const
73{
74 QDomElement element = document.createElement( QStringLiteral( "ProjectTimeSettings" ) );
75
76 if ( mRange.begin().isValid() && mRange.end().isValid() )
77 {
78 QDomElement temporalElement = document.createElement( QStringLiteral( "TemporalRange" ) );
79 QDomElement startElement = document.createElement( QStringLiteral( "start" ) );
80 QDomElement endElement = document.createElement( QStringLiteral( "end" ) );
81
82 const QDomText startText = document.createTextNode( mRange.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
83 const QDomText endText = document.createTextNode( mRange.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
84
85 startElement.appendChild( startText );
86 endElement.appendChild( endText );
87
88 temporalElement.appendChild( startElement );
89 temporalElement.appendChild( endElement );
90
91 element.appendChild( temporalElement );
92 }
93
94 element.setAttribute( QStringLiteral( "timeStepUnit" ), QgsUnitTypes::encodeUnit( mTimeStepUnit ) );
95 element.setAttribute( QStringLiteral( "timeStep" ), qgsDoubleToString( mTimeStep ) );
96 element.setAttribute( QStringLiteral( "frameRate" ), qgsDoubleToString( mFrameRate ) );
97 element.setAttribute( QStringLiteral( "cumulativeTemporalRange" ), mCumulativeTemporalRange ? 1 : 0 );
98
99 return element;
100}
101
103{
104 return mTimeStepUnit;
105}
106
108{
109 mTimeStepUnit = unit;
110}
111
113{
114 return mTimeStep;
115}
116
118{
119 mTimeStep = timeStep;
120}
121
123{
124 mFrameRate = rate;
125}
126
128{
129 return mFrameRate;
130}
131
133{
134 mCumulativeTemporalRange = state;
135}
137{
138 return mCumulativeTemporalRange;
139}
140
TemporalUnit
Temporal units.
Definition: qgis.h:3417
QgsDateTimeRange temporalRange() const
Returns the project's temporal range, which indicates the earliest and latest datetime ranges associa...
void setTemporalRange(const QgsDateTimeRange &range)
Sets the project's temporal range, which indicates the earliest and latest datetime ranges associated...
double timeStep() const
Returns the project's time step (length of one animation frame), which is used as the default value w...
void setFramesPerSecond(double rate)
Sets the project's default animation frame rate, in frames per second.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
void temporalRangeChanged()
Emitted when the temporal range changes.
Qgis::TemporalUnit timeStepUnit() const
Returns the project's time step (length of one animation frame) unit, which is used as the default va...
QgsProjectTimeSettings(QObject *parent=nullptr)
Constructor for QgsProjectTimeSettings with the specified parent object.
bool isTemporalRangeCumulative() const
Returns the value of cumulative temporal range in animation settings.
void setTimeStepUnit(Qgis::TemporalUnit unit)
Sets the project's time step (length of one animation frame) unit, which is used as the default value...
void setIsTemporalRangeCumulative(bool state)
Sets the project's temporal range as cumulative in animation settings.
void reset()
Resets the settings to a default state.
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
void setTimeStep(double step)
Sets the project's time step (length of one animation frame), which is used as the default value when...
double framesPerSecond() const
Returns the project's default animation frame rate, in frames per second.
The class is used as a container of context for various read/write operations on other objects.
static Q_INVOKABLE Qgis::TemporalUnit decodeTemporalUnit(const QString &string, bool *ok=nullptr)
Decodes a temporal unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:3927