libosmscout 1.1.1
Loading...
Searching...
No Matches
SunriseSunset.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_CLIENT_QT_SUNRISESUNSET_H
2#define OSMSCOUT_CLIENT_QT_SUNRISESUNSET_H
3
4/*
5 OSMScout - a Qt backend for libosmscout and libosmscout-map
6 Copyright (C) 2022 Lukas Karas
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
24
25#include <osmscout/GeoCoord.h>
27
28#include <QObject>
29#include <QDateTime>
30#include <QTimer>
31
32#include <cmath>
33
34namespace osmscout {
35
41class OSMSCOUT_CLIENT_QT_API SunriseSunset : public QObject {
42 Q_OBJECT
43 Q_PROPERTY(double latitude READ getLatitude WRITE setLatitude)
44 Q_PROPERTY(double longitude READ getLongitude WRITE setLongitude)
45 Q_PROPERTY(bool ready READ isReady NOTIFY updated)
46 Q_PROPERTY(bool day READ isDay NOTIFY updated)
47 Q_PROPERTY(QDateTime sunrise READ getSunrise NOTIFY updated)
48 Q_PROPERTY(QDateTime sunset READ getSunset NOTIFY updated)
49
50signals:
51 void updated();
52
53public slots:
54 void tick();
55
56public:
58 SunriseSunset(const SunriseSunset &) = delete;
60
61 ~SunriseSunset() override = default;
62
63 SunriseSunset& operator=(const SunriseSunset&) = delete;
64 SunriseSunset& operator=(SunriseSunset&&) = delete;
65
66 double getLatitude() const
67 {
68 return position.GetLat();
69 }
70
71 void setLatitude(double lat);
72
73 double getLongitude() const
74 {
75 return position.GetLon();
76 }
77
78 void setLongitude(double lon);
79
80 bool isReady() const
81 {
82 return !std::isnan(updatePosition.GetLat()) && !std::isnan(updatePosition.GetLon());
83 }
84
85 QDateTime getSunrise() const
86 {
87 return getValue<0>();
88 }
89
90 QDateTime getSunset() const
91 {
92 return getValue<1>();
93 }
94
95 bool isDay() const;
96
97private:
98 void update();
99 void update(const GeoCoord &newPosition);
100
101 template <size_t i>
102 QDateTime getValue() const
103 {
104 if (!sunriseSunset.has_value()) {
105 return QDateTime();
106 }
107 using namespace std::chrono;
108 Timestamp ts=std::get<i>(sunriseSunset.value());
109 milliseconds millisSinceEpoch = duration_cast<milliseconds>(ts.time_since_epoch());
110 return QDateTime::fromMSecsSinceEpoch(millisSinceEpoch.count());
111 }
112
113private:
114 GeoCoord position{NAN, NAN};
115 GeoCoord updatePosition{NAN, NAN};
116 QDate updateDate;
117 SunriseSunsetRes sunriseSunset=std::nullopt;
118 QTimer updateTimer;
119};
120
121}
122
123#endif //OSMSCOUT_CLIENT_QT_SUNRISESUNSET_H
#define OSMSCOUT_CLIENT_QT_API
Definition ClientQtImportExport.h:45
QDateTime sunset
Definition SunriseSunset.h:48
double getLongitude() const
Definition SunriseSunset.h:73
QDateTime getSunrise() const
Definition SunriseSunset.h:85
double getLatitude() const
Definition SunriseSunset.h:66
QDateTime sunrise
Definition SunriseSunset.h:47
double latitude
Definition SunriseSunset.h:43
double longitude
Definition SunriseSunset.h:44
void setLongitude(double lon)
bool isReady() const
Definition SunriseSunset.h:80
bool day
Definition SunriseSunset.h:46
QDateTime getSunset() const
Definition SunriseSunset.h:90
bool ready
Definition SunriseSunset.h:45
void setLatitude(double lat)
Definition Area.h:39
std::chrono::system_clock::time_point Timestamp
Definition Time.h:27
std::optional< std::tuple< Timestamp, Timestamp > > SunriseSunsetRes
Definition SunriseSunset.h:41