File indexing completed on 2024-04-14 14:21:03

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 
0018 // convert to/from QDateTime for JS
0019 class LunarPhaseWrapper
0020 {
0021     Q_GADGET
0022 public:
0023     static Q_INVOKABLE KHolidays::LunarPhase::Phase phaseAtDate(const QDateTime &date)
0024     {
0025         return KHolidays::LunarPhase::phaseAtDate(date.date());
0026     }
0027     static Q_INVOKABLE QString phaseNameAtDate(const QDateTime &date)
0028     {
0029         return KHolidays::LunarPhase::phaseNameAtDate(date.date());
0030     }
0031     static Q_INVOKABLE QString phaseName(KHolidays::LunarPhase::Phase phase)
0032     {
0033         return KHolidays::LunarPhase::phaseName(phase);
0034     }
0035 };
0036 
0037 class SunRiseSetWrapper
0038 {
0039     Q_GADGET
0040 public:
0041     static Q_INVOKABLE QDateTime utcSunrise(const QDateTime &date, double latitude, double longitude)
0042     {
0043         const auto time = KHolidays::SunRiseSet::utcSunrise(date.date(), latitude, longitude);
0044         return time.isValid() ? QDateTime(date.date(), time, Qt::UTC) : QDateTime();
0045     }
0046     static Q_INVOKABLE QDateTime utcSunset(const QDateTime &date, double latitude, double longitude)
0047     {
0048         const auto time = KHolidays::SunRiseSet::utcSunset(date.date(), latitude, longitude);
0049         return time.isValid() ? QDateTime(date.date(), time, Qt::UTC) : QDateTime();
0050     }
0051     static Q_INVOKABLE QDateTime utcDawn(const QDateTime &date, double latitude, double longitude)
0052     {
0053         const auto time = KHolidays::SunRiseSet::utcDawn(date.date(), latitude, longitude);
0054         return time.isValid() ? QDateTime(date.date(), time, Qt::UTC) : QDateTime();
0055     }
0056     static Q_INVOKABLE QDateTime utcDusk(const QDateTime &date, double latitude, double longitude)
0057     {
0058         const auto time = KHolidays::SunRiseSet::utcDusk(date.date(), latitude, longitude);
0059         return time.isValid() ? QDateTime(date.date(), time, Qt::UTC) : QDateTime();
0060     }
0061     static Q_INVOKABLE bool isPolarDay(const QDateTime &date, double latitude)
0062     {
0063         return KHolidays::SunRiseSet::isPolarDay(date.date(), latitude);
0064     }
0065     static Q_INVOKABLE bool isPolarTwilight(const QDateTime &date, double latitude)
0066     {
0067         return KHolidays::SunRiseSet::isPolarTwilight(date.date(), latitude);
0068     }
0069     static Q_INVOKABLE bool isPolarNight(const QDateTime &date, double latitude)
0070     {
0071         return KHolidays::SunRiseSet::isPolarNight(date.date(), latitude);
0072     }
0073 };
0074 
0075 void KHolidaysDeclarativePlugin::registerTypes(const char *uri)
0076 {
0077     qmlRegisterType<HolidayRegionsDeclarativeModel>(uri, 1, 0, "HolidayRegionsModel");
0078     qRegisterMetaType<KHolidays::LunarPhase::Phase>();
0079     qmlRegisterUncreatableType<KHolidays::LunarPhase>(uri, 1, 0, "LunarPhase", {});
0080 
0081     // HACK qmlplugindump chokes on gadget singletons, to the point of breaking ecm_find_qmlmodule()
0082     if (QCoreApplication::applicationName() != QLatin1String("qmlplugindump")) {
0083         qmlRegisterSingletonType(uri, 1, 0, "Lunar", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
0084             return engine->toScriptValue(LunarPhaseWrapper());
0085         });
0086         qmlRegisterSingletonType(uri, 1, 0, "SunRiseSet", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
0087             return engine->toScriptValue(SunRiseSetWrapper());
0088         });
0089     }
0090 }
0091 
0092 #include "kholidaysdeclarativeplugin.moc"
0093 #include "moc_kholidaysdeclarativeplugin.cpp"