File indexing completed on 2024-04-14 03:52:07

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2015 Martin Klapetek <mklapetek@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kholidaysdeclarativeplugin.h"
0010 #include "holidayregionsmodel.h"
0011 
0012 #include <KHolidays/LunarPhase>
0013 #include <KHolidays/SunRiseSet>
0014 
0015 #include <QCoreApplication>
0016 #include <QQmlEngine>
0017 #include <QTimeZone>
0018 
0019 // convert to/from QDateTime for JS
0020 class LunarPhaseWrapper
0021 {
0022     Q_GADGET
0023 public:
0024     static Q_INVOKABLE KHolidays::LunarPhase::Phase phaseAtDate(const QDateTime &date)
0025     {
0026         return KHolidays::LunarPhase::phaseAtDate(date.date());
0027     }
0028     static Q_INVOKABLE QString phaseNameAtDate(const QDateTime &date)
0029     {
0030         return KHolidays::LunarPhase::phaseNameAtDate(date.date());
0031     }
0032     static Q_INVOKABLE QString phaseName(KHolidays::LunarPhase::Phase phase)
0033     {
0034         return KHolidays::LunarPhase::phaseName(phase);
0035     }
0036 };
0037 
0038 class SunRiseSetWrapper
0039 {
0040     Q_GADGET
0041 public:
0042     static Q_INVOKABLE QDateTime utcSunrise(const QDateTime &date, double latitude, double longitude)
0043     {
0044         const auto time = KHolidays::SunRiseSet::utcSunrise(date.date(), latitude, longitude);
0045         return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
0046     }
0047     static Q_INVOKABLE QDateTime utcSunset(const QDateTime &date, double latitude, double longitude)
0048     {
0049         const auto time = KHolidays::SunRiseSet::utcSunset(date.date(), latitude, longitude);
0050         return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
0051     }
0052     static Q_INVOKABLE QDateTime utcDawn(const QDateTime &date, double latitude, double longitude)
0053     {
0054         const auto time = KHolidays::SunRiseSet::utcDawn(date.date(), latitude, longitude);
0055         return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
0056     }
0057     static Q_INVOKABLE QDateTime utcDusk(const QDateTime &date, double latitude, double longitude)
0058     {
0059         const auto time = KHolidays::SunRiseSet::utcDusk(date.date(), latitude, longitude);
0060         return time.isValid() ? QDateTime(date.date(), time, QTimeZone::utc()) : QDateTime();
0061     }
0062     static Q_INVOKABLE bool isPolarDay(const QDateTime &date, double latitude)
0063     {
0064         return KHolidays::SunRiseSet::isPolarDay(date.date(), latitude);
0065     }
0066     static Q_INVOKABLE bool isPolarTwilight(const QDateTime &date, double latitude)
0067     {
0068         return KHolidays::SunRiseSet::isPolarTwilight(date.date(), latitude);
0069     }
0070     static Q_INVOKABLE bool isPolarNight(const QDateTime &date, double latitude)
0071     {
0072         return KHolidays::SunRiseSet::isPolarNight(date.date(), latitude);
0073     }
0074 };
0075 
0076 void KHolidaysDeclarativePlugin::registerTypes(const char *uri)
0077 {
0078     qmlRegisterType<HolidayRegionsDeclarativeModel>(uri, 1, 0, "HolidayRegionsModel");
0079     qRegisterMetaType<KHolidays::LunarPhase::Phase>();
0080     qmlRegisterUncreatableMetaObject(KHolidays::LunarPhase::staticMetaObject, uri, 1, 0, "LunarPhase", {});
0081 
0082     // HACK qmlplugindump chokes on gadget singletons, to the point of breaking ecm_find_qmlmodule()
0083     if (QCoreApplication::applicationName() != QLatin1String("qmlplugindump")) {
0084         qmlRegisterSingletonType(uri, 1, 0, "Lunar", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
0085             return engine->toScriptValue(LunarPhaseWrapper());
0086         });
0087         qmlRegisterSingletonType(uri, 1, 0, "SunRiseSet", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
0088             return engine->toScriptValue(SunRiseSetWrapper());
0089         });
0090     }
0091 }
0092 
0093 #include "kholidaysdeclarativeplugin.moc"
0094 #include "moc_kholidaysdeclarativeplugin.cpp"