KWeatherCore

sunrise.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Han Young <[email protected]>
3  * SPDX-FileCopyrightText: 2020 Devin Lin <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 #include "sunrise.h"
8 
9 #include <KLocalizedString>
10 
11 namespace KWeatherCore
12 {
13 class Sunrise::SunrisePrivate : public QSharedData
14 {
15 public:
16  QPair<QDateTime, double> highMoon;
17  QPair<QDateTime, double> solarMidnight;
18  QPair<QDateTime, double> solarNoon;
24  double moonPhase;
25 };
26 Sunrise::Sunrise()
27  : d(new SunrisePrivate)
28 {
29 }
30 Sunrise::Sunrise(const Sunrise &other)
31  : d(other.d)
32 {
33 }
34 Sunrise::~Sunrise() = default;
35 Sunrise &Sunrise::operator=(const Sunrise &other)
36 {
37  if (this != &other) {
38  d = other.d;
39  }
40 
41  return *this;
42 }
43 
45 {
46  Sunrise s;
47  s.setSunSet(QDateTime::fromString(obj[QStringLiteral("sunset")].toString(), Qt::ISODate));
48  s.setSunRise(QDateTime::fromString(obj[QStringLiteral("sunrise")].toString(), Qt::ISODate));
49  s.setLowMoon(
50  QPair<QDateTime, double>(QDateTime::fromString(obj[QStringLiteral("lowmoon")].toString(), Qt::ISODate), obj[QStringLiteral("lowmoonEle")].toDouble()));
51  s.setHighMoon(QPair<QDateTime, double>(QDateTime::fromString(obj[QStringLiteral("highmoon")].toString(), Qt::ISODate),
52  obj[QStringLiteral("highmoonEle")].toDouble()));
53  s.setSolarMidnight(QPair<QDateTime, double>(QDateTime::fromString(obj[QStringLiteral("solarmidnight")].toString(), Qt::ISODate),
54  obj[QStringLiteral("solarmidnightEle")].toDouble()));
55  s.setSolarNoon(QPair<QDateTime, double>(QDateTime::fromString(obj[QStringLiteral("solarnoon")].toString(), Qt::ISODate),
56  obj[QStringLiteral("solarnoonEle")].toDouble()));
57  s.setMoonPhase(obj[QStringLiteral("moonphase")].toDouble());
58  s.setMoonSet(QDateTime::fromString(obj[QStringLiteral("moonset")].toString(), Qt::ISODate));
59  s.setMoonRise(QDateTime::fromString(obj[QStringLiteral("moonrise")].toString(), Qt::ISODate));
60  return s;
61 }
63 {
64  QJsonObject obj;
65  obj[QStringLiteral("sunrise")] = sunRise().toString(Qt::ISODate);
66  obj[QStringLiteral("sunset")] = sunSet().toString(Qt::ISODate);
67  obj[QStringLiteral("moonrise")] = moonRise().toString(Qt::ISODate);
68  obj[QStringLiteral("moonset")] = moonSet().toString(Qt::ISODate);
69  obj[QStringLiteral("lowmoon")] = lowMoonTime().toString(Qt::ISODate);
70  obj[QStringLiteral("solarmidnight")] = solarMidnightTime().toString(Qt::ISODate);
71  obj[QStringLiteral("solarnoon")] = solarNoonTime().toString(Qt::ISODate);
72  obj[QStringLiteral("highmoon")] = highMoonTime().toString(Qt::ISODate);
73  obj[QStringLiteral("lowmoonEle")] = lowMoon();
74  obj[QStringLiteral("highmoonEle")] = highMoon();
75  obj[QStringLiteral("solarmidnightEle")] = solarMidnight();
76  obj[QStringLiteral("solarNoonEle")] = solarNoon();
77  obj[QStringLiteral("moonphase")] = moonPhase();
78  return obj;
79 }
80 
81 QDateTime Sunrise::highMoonTime() const
82 {
83  return d->highMoon.first;
84 }
85 double Sunrise::highMoon() const
86 {
87  return d->highMoon.second;
88 }
89 QDateTime Sunrise::lowMoonTime() const
90 {
91  return d->lowMoon.first;
92 }
93 double Sunrise::lowMoon() const
94 {
95  return d->lowMoon.second;
96 }
97 QDateTime Sunrise::solarMidnightTime() const
98 {
99  return d->solarMidnight.first;
100 }
101 QDateTime Sunrise::solarNoonTime() const
102 {
103  return d->solarNoon.first;
104 }
105 double Sunrise::solarMidnight() const
106 {
107  return d->solarMidnight.second;
108 }
109 double Sunrise::solarNoon() const
110 {
111  return d->solarNoon.second;
112 }
113 const QDateTime &Sunrise::sunRise() const
114 {
115  return d->sunRise;
116 }
117 const QDateTime &Sunrise::sunSet() const
118 {
119  return d->sunSet;
120 }
121 const QDateTime &Sunrise::moonRise() const
122 {
123  return d->moonRise;
124 }
125 const QDateTime &Sunrise::moonSet() const
126 {
127  return d->moonSet;
128 }
129 double Sunrise::moonPhase() const
130 {
131  return d->moonPhase;
132 }
134 {
135  d->highMoon = highMoon;
136 }
138 {
139  d->solarMidnight = solarMidnight;
140 }
142 {
143  d->solarNoon = solarNoon;
144 }
146 {
147  d->lowMoon = lowMoon;
148 }
149 void Sunrise::setSunRise(const QDateTime &sunRise)
150 {
151  d->sunRise = sunRise;
152 }
153 void Sunrise::setSunSet(const QDateTime &sunSet)
154 {
155  d->sunSet = sunSet;
156 }
157 void Sunrise::setMoonRise(const QDateTime &moonRise)
158 {
159  d->moonRise = moonRise;
160 }
161 void Sunrise::setMoonSet(const QDateTime &moonSet)
162 {
163  d->moonSet = moonSet;
164 }
165 void Sunrise::setMoonPhase(double moonPhase)
166 {
167  d->moonPhase = moonPhase;
168 }
169 QString Sunrise::moonPhaseString() const
170 {
171  if (d->moonPhase <= 5) {
172  return i18n("New Moon");
173  } else if (d->moonPhase <= 25) {
174  return i18n("Waxing Crescent");
175  } else if (d->moonPhase <= 45) {
176  return i18n("Waxing Gibbous");
177  } else if (d->moonPhase <= 55) {
178  return i18n("Full Moon");
179  } else if (d->moonPhase <= 75) {
180  return i18n("Waning Gibbous");
181  } else if (d->moonPhase <= 95) {
182  return i18n("Waning Crescent");
183  } else {
184  return i18n("New Moon");
185  }
186 }
187 
188 }
189 
190 Q_DECLARE_METATYPE(KWeatherCore::Sunrise)
The Sunrise class contains the information of sunrise/set on a day and more.
Definition: sunrise.h:24
void setMoonRise(const QDateTime &moonRise)
set moon rise time
Definition: sunrise.cpp:157
void setMoonSet(const QDateTime &moonSet)
set moon set time
Definition: sunrise.cpp:161
void setSolarNoon(const QPair< QDateTime, double > &solarNoon)
set solar noon time and elevation
Definition: sunrise.cpp:141
QJsonObject toJson() const
convert to json
Definition: sunrise.cpp:62
void setHighMoon(const QPair< QDateTime, double > &highMoon)
set high moon time and elevation
Definition: sunrise.cpp:133
void setSunRise(const QDateTime &sunRise)
sun rise time
Definition: sunrise.cpp:149
QDateTime fromString(const QString &string, Qt::DateFormat format)
void setSolarMidnight(const QPair< QDateTime, double > &solarMidnight)
set solar midnight time and elevation
Definition: sunrise.cpp:137
QString i18n(const char *text, const TYPE &arg...)
void setMoonPhase(double moonPhase)
set moon phase
Definition: sunrise.cpp:165
QDateTime currentDateTime()
void setSunSet(const QDateTime &sunSet)
set sun set time
Definition: sunrise.cpp:153
static Sunrise fromJson(QJsonObject obj)
construct from json
Definition: sunrise.cpp:44
void setLowMoon(const QPair< QDateTime, double > &lowMoon)
set low moon time time and elevation
Definition: sunrise.cpp:145
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:05:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.