QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
18 #include "qgsprojecttimesettings.h"
19 #include "qgis.h"
20 #include <QDomElement>
21 
23  : QObject( parent )
24 {
25 
26 }
27 
29 {
30  mRange = QgsDateTimeRange();
31  emit temporalRangeChanged();
32 }
33 
34 QgsDateTimeRange QgsProjectTimeSettings::temporalRange() const
35 {
36  return mRange;
37 }
38 
39 void QgsProjectTimeSettings::setTemporalRange( const QgsDateTimeRange &range )
40 {
41  if ( range == mRange )
42  return;
43  mRange = range;
44 
45  emit temporalRangeChanged();
46 }
47 
48 bool QgsProjectTimeSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
49 {
50  const QDomElement temporalElement = element.firstChildElement( QStringLiteral( "TemporalRange" ) );
51  if ( !temporalElement.isNull() )
52  {
53  const QDomNode begin = temporalElement.namedItem( QStringLiteral( "start" ) );
54  const QDomNode end = temporalElement.namedItem( QStringLiteral( "end" ) );
55 
56  const QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
57  const QDateTime endDate = QDateTime::fromString( end.toElement().text(), Qt::ISODate );
58 
59  setTemporalRange( QgsDateTimeRange( beginDate, endDate ) );
60 
61  }
62 
63  mTimeStepUnit = QgsUnitTypes::decodeTemporalUnit( element.attribute( QStringLiteral( "timeStepUnit" ), QgsUnitTypes::encodeUnit( QgsUnitTypes::TemporalHours ) ) );
64  mTimeStep = element.attribute( QStringLiteral( "timeStep" ), "1" ).toDouble();
65  mFrameRate = element.attribute( QStringLiteral( "frameRate" ), "1" ).toDouble();
66  mCumulativeTemporalRange = element.attribute( QStringLiteral( "cumulativeTemporalRange" ), "0" ).toInt();
67 
68  return true;
69 }
70 
71 QDomElement QgsProjectTimeSettings::writeXml( QDomDocument &document, const QgsReadWriteContext & ) const
72 {
73  QDomElement element = document.createElement( QStringLiteral( "ProjectTimeSettings" ) );
74 
75  if ( mRange.begin().isValid() && mRange.end().isValid() )
76  {
77  QDomElement temporalElement = document.createElement( QStringLiteral( "TemporalRange" ) );
78  QDomElement startElement = document.createElement( QStringLiteral( "start" ) );
79  QDomElement endElement = document.createElement( QStringLiteral( "end" ) );
80 
81  const QDomText startText = document.createTextNode( mRange.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
82  const QDomText endText = document.createTextNode( mRange.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
83 
84  startElement.appendChild( startText );
85  endElement.appendChild( endText );
86 
87  temporalElement.appendChild( startElement );
88  temporalElement.appendChild( endElement );
89 
90  element.appendChild( temporalElement );
91  }
92 
93  element.setAttribute( QStringLiteral( "timeStepUnit" ), QgsUnitTypes::encodeUnit( mTimeStepUnit ) );
94  element.setAttribute( QStringLiteral( "timeStep" ), qgsDoubleToString( mTimeStep ) );
95  element.setAttribute( QStringLiteral( "frameRate" ), qgsDoubleToString( mFrameRate ) );
96  element.setAttribute( QStringLiteral( "cumulativeTemporalRange" ), mCumulativeTemporalRange ? 1 : 0 );
97 
98  return element;
99 }
100 
102 {
103  return mTimeStepUnit;
104 }
105 
107 {
108  mTimeStepUnit = unit;
109 }
110 
112 {
113  return mTimeStep;
114 }
115 
116 void QgsProjectTimeSettings::setTimeStep( double timeStep )
117 {
118  mTimeStep = timeStep;
119 }
120 
122 {
123  mFrameRate = rate;
124 }
125 
127 {
128  return mFrameRate;
129 }
130 
132 {
133  mCumulativeTemporalRange = state;
134 }
136 {
137  return mCumulativeTemporalRange;
138 }
139 
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...
void setTimeStepUnit(QgsUnitTypes::TemporalUnit unit)
Sets the project's time step (length of one animation frame) unit, which is used as the default value...
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.
QgsUnitTypes::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 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 QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:150
@ TemporalHours
Hours.
Definition: qgsunittypes.h:154
static Q_INVOKABLE QgsUnitTypes::TemporalUnit decodeTemporalUnit(const QString &string, bool *ok=nullptr)
Decodes a temporal unit from a string.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:1198