Marble

KmlWhenTagHandler.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2010 Harshit Jain <[email protected]>
4 //
5 
6 #include "KmlWhenTagHandler.h"
7 
8 #include "MarbleDebug.h"
9 #include <QDateTime>
10 
11 #include "KmlElementDictionary.h"
12 #include "GeoDataTimeStamp.h"
13 #include "GeoDataTrack.h"
14 #include "GeoParser.h"
15 
16 namespace Marble
17 {
18 namespace kml
19 {
20 KML_DEFINE_TAG_HANDLER( when )
21 
22 GeoNode* KmlwhenTagHandler::parse( GeoParser& parser ) const
23 {
24  Q_ASSERT(parser.isStartElement() && parser.isValidElement(QLatin1String(kmlTag_when)));
25 
26  GeoStackItem parentItem = parser.parentElement();
27 
28  QString whenString = parser.readElementText().trimmed();
29  GeoDataTimeStamp::TimeResolution resolution = modify( whenString );
30  QDateTime when = QDateTime::fromString( whenString, Qt::ISODate );
31  if( parentItem.represents( kmlTag_TimeStamp ) ) {
32  parentItem.nodeAs<GeoDataTimeStamp>()->setWhen( when );
33  parentItem.nodeAs<GeoDataTimeStamp>()->setResolution( resolution );
34  } else if ( parentItem.represents( kmlTag_Track ) ) {
35  parentItem.nodeAs<GeoDataTrack>()->appendWhen( when );
36  }
37 
38  return nullptr;
39 }
40 
41 QDateTime KmlwhenTagHandler::parse( const QString &dateTime )
42 {
43  QString iso = dateTime;
44  modify( iso );
45  return QDateTime::fromString( iso, Qt::ISODate );
46 }
47 
48 GeoDataTimeStamp KmlwhenTagHandler::parseTimestamp( const QString &dateTime )
49 {
50  GeoDataTimeStamp result;
51  QString input = dateTime;
52  result.setResolution( modify( input ) );
53  result.setWhen( parse( input) );
54  return result;
55 }
56 
57 GeoDataTimeStamp::TimeResolution KmlwhenTagHandler::modify( QString& whenString )
58 {
59  switch( whenString.length() )
60  {
61  case 4:
62  whenString += QLatin1String("-01-01");
63  return GeoDataTimeStamp::YearResolution;
64  case 7:
65  whenString += QLatin1String("-01");
66  return GeoDataTimeStamp::MonthResolution;
67  case 10:
68  return GeoDataTimeStamp::DayResolution;
69  default:
70  return GeoDataTimeStamp::SecondResolution;
71  }
72 
73  return GeoDataTimeStamp::SecondResolution;
74 }
75 
76 }
77 
78 }
QString trimmed() const const
int length() const const
Binds a QML item to a specific geodetic location in screen coordinates.
QDateTime fromString(const QString &string, Qt::DateFormat format)
QList< QVariant > parse(const QString &message, const QDateTime &externalIssueDateTime=QDateTime())
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Sep 25 2023 03:50:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.