KHolidays

kholidaysdeclarativeplugin.cpp
1/*
2 This file is part of the kholidays library.
3
4 SPDX-FileCopyrightText: 2015 Martin Klapetek <mklapetek@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kholidaysdeclarativeplugin.h"
10#include "holidayregionsmodel.h"
11
12#include <KHolidays/LunarPhase>
13#include <KHolidays/SunRiseSet>
14
15#include <QCoreApplication>
16#include <QQmlEngine>
17#include <QTimeZone>
18
19// convert to/from QDateTime for JS
20class LunarPhaseWrapper
21{
22 Q_GADGET
23public:
24 static Q_INVOKABLE KHolidays::LunarPhase::Phase phaseAtDate(const QDateTime &date)
25 {
27 }
28 static Q_INVOKABLE QString phaseNameAtDate(const QDateTime &date)
29 {
31 }
32 static Q_INVOKABLE QString phaseName(KHolidays::LunarPhase::Phase phase)
33 {
35 }
36};
37
38class SunRiseSetWrapper
39{
40 Q_GADGET
41public:
42 static Q_INVOKABLE QDateTime utcSunrise(const QDateTime &date, double latitude, double longitude)
43 {
44 const auto time = KHolidays::SunRiseSet::utcSunrise(date.date(), latitude, longitude);
45 return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
46 }
47 static Q_INVOKABLE QDateTime utcSunset(const QDateTime &date, double latitude, double longitude)
48 {
49 const auto time = KHolidays::SunRiseSet::utcSunset(date.date(), latitude, longitude);
50 return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
51 }
52 static Q_INVOKABLE QDateTime utcDawn(const QDateTime &date, double latitude, double longitude)
53 {
54 const auto time = KHolidays::SunRiseSet::utcDawn(date.date(), latitude, longitude);
55 return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
56 }
57 static Q_INVOKABLE QDateTime utcDusk(const QDateTime &date, double latitude, double longitude)
58 {
59 const auto time = KHolidays::SunRiseSet::utcDusk(date.date(), latitude, longitude);
60 return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
61 }
62 static Q_INVOKABLE bool isPolarDay(const QDateTime &date, double latitude)
63 {
64 return KHolidays::SunRiseSet::isPolarDay(date.date(), latitude);
65 }
66 static Q_INVOKABLE bool isPolarTwilight(const QDateTime &date, double latitude)
67 {
68 return KHolidays::SunRiseSet::isPolarTwilight(date.date(), latitude);
69 }
70 static Q_INVOKABLE bool isPolarNight(const QDateTime &date, double latitude)
71 {
72 return KHolidays::SunRiseSet::isPolarNight(date.date(), latitude);
73 }
74};
75
76void KHolidaysDeclarativePlugin::registerTypes(const char *uri)
77{
78 qmlRegisterType<HolidayRegionsDeclarativeModel>(uri, 1, 0, "HolidayRegionsModel");
79 qRegisterMetaType<KHolidays::LunarPhase::Phase>();
80 qmlRegisterUncreatableMetaObject(KHolidays::LunarPhase::staticMetaObject, uri, 1, 0, "LunarPhase", {});
81
82 // HACK qmlplugindump chokes on gadget singletons, to the point of breaking ecm_find_qmlmodule()
83 if (QCoreApplication::applicationName() != QLatin1String("qmlplugindump")) {
84 qmlRegisterSingletonType(uri, 1, 0, "Lunar", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
85 return engine->toScriptValue(LunarPhaseWrapper());
86 });
87 qmlRegisterSingletonType(uri, 1, 0, "SunRiseSet", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
88 return engine->toScriptValue(SunRiseSetWrapper());
89 });
90 }
91}
92
93#include "kholidaysdeclarativeplugin.moc"
94#include "moc_kholidaysdeclarativeplugin.cpp"
static QString phaseNameAtDate(const QDate &date)
Return the lunar phase as a text string for the specified date.
static Phase phaseAtDate(const QDate &date)
Return the lunar phase for the specified Gregorian date.
Phase
Phases of the moon, in traditional English notation.
Definition lunarphase.h:54
static QString phaseName(Phase phase)
Return the string representation of phase.
KHOLIDAYS_EXPORT bool isPolarTwilight(const QDate &date, double latitude)
Checks whether it is polar twilight on day date at latitude.
KHOLIDAYS_EXPORT QTime utcSunset(const QDate &date, double latitude, double longitude)
Compute the sunset time (UTC) for a date and Earth location.
KHOLIDAYS_EXPORT QTime utcSunrise(const QDate &date, double latitude, double longitude)
Compute the sunrise time (UTC) for a date and Earth location.
KHOLIDAYS_EXPORT bool isPolarDay(const QDate &date, double latitude)
Checks whether it is polar day on day date at latitude.
KHOLIDAYS_EXPORT bool isPolarNight(const QDate &date, double latitude)
Checks whether it is polar night on day date at latitude.
KHOLIDAYS_EXPORT QTime utcDawn(const QDate &date, double latitude, double longitude)
Compute the civil dawn time (UTC) for a date and Earth location.
KHOLIDAYS_EXPORT QTime utcDusk(const QDate &date, double latitude, double longitude)
Compute the civil dawn time (UTC) for a date and Earth location.
QDate date() const const
QJSValue toScriptValue(const T &value)
QTimeZone utc()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:22 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.