QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  QDomElement temporalElement = element.firstChildElement( QStringLiteral( "TemporalRange" ) );
51  if ( !temporalElement.isNull() )
52  {
53  QDomNode begin = temporalElement.namedItem( QStringLiteral( "start" ) );
54  QDomNode end = temporalElement.namedItem( QStringLiteral( "end" ) );
55 
56  QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
57  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  QDomText startText = document.createTextNode( mRange.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
82  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 
QgsProjectTimeSettings::setIsTemporalRangeCumulative
void setIsTemporalRangeCumulative(bool state)
Sets the project's temporal range as cumulative in animation settings.
Definition: qgsprojecttimesettings.cpp:131
QgsProjectTimeSettings::isTemporalRangeCumulative
bool isTemporalRangeCumulative() const
Returns the value of cumulative temporal range in animation settings.
Definition: qgsprojecttimesettings.cpp:135
QgsProjectTimeSettings::setFramesPerSecond
void setFramesPerSecond(double rate)
Sets the project's default animation frame rate, in frames per second.
Definition: qgsprojecttimesettings.cpp:121
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsProjectTimeSettings::reset
void reset()
Resets the settings to a default state.
Definition: qgsprojecttimesettings.cpp:28
qgis.h
QgsProjectTimeSettings::setTemporalRange
void setTemporalRange(const QgsDateTimeRange &range)
Sets the project's temporal range, which indicates the earliest and latest datetime ranges associated...
Definition: qgsprojecttimesettings.cpp:39
QgsUnitTypes::TemporalUnit
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:149
QgsProjectTimeSettings::temporalRange
QgsDateTimeRange temporalRange() const
Returns the project's temporal range, which indicates the earliest and latest datetime ranges associa...
Definition: qgsprojecttimesettings.cpp:34
QgsProjectTimeSettings::framesPerSecond
double framesPerSecond() const
Returns the project's default animation frame rate, in frames per second.
Definition: qgsprojecttimesettings.cpp:126
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QgsProjectTimeSettings::timeStepUnit
QgsUnitTypes::TemporalUnit timeStepUnit() const
Returns the project's time step (length of one animation frame) unit, which is used as the default va...
Definition: qgsprojecttimesettings.cpp:101
QgsUnitTypes::decodeTemporalUnit
static Q_INVOKABLE QgsUnitTypes::TemporalUnit decodeTemporalUnit(const QString &string, bool *ok=nullptr)
Decodes a temporal unit from a string.
Definition: qgsunittypes.cpp:1284
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
QgsProjectTimeSettings::QgsProjectTimeSettings
QgsProjectTimeSettings(QObject *parent=nullptr)
Constructor for QgsProjectTimeSettings with the specified parent object.
Definition: qgsprojecttimesettings.cpp:22
QgsUnitTypes::TemporalHours
@ TemporalHours
Hours.
Definition: qgsunittypes.h:154
qgsprojecttimesettings.h
QgsProjectTimeSettings::writeXml
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
Definition: qgsprojecttimesettings.cpp:71
QgsProjectTimeSettings::temporalRangeChanged
void temporalRangeChanged()
Emitted when the temporal range changes.
QgsProjectTimeSettings::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
Definition: qgsprojecttimesettings.cpp:48
QgsProjectTimeSettings::timeStep
double timeStep() const
Returns the project's time step (length of one animation frame), which is used as the default value w...
Definition: qgsprojecttimesettings.cpp:111
QgsProjectTimeSettings::setTimeStep
void setTimeStep(double step)
Sets the project's time step (length of one animation frame), which is used as the default value when...
Definition: qgsprojecttimesettings.cpp:116
QgsProjectTimeSettings::setTimeStepUnit
void setTimeStepUnit(QgsUnitTypes::TemporalUnit unit)
Sets the project's time step (length of one animation frame) unit, which is used as the default value...
Definition: qgsprojecttimesettings.cpp:106