Verified Commit 37335d80 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

alternatecalendar: Add QCalendar provider

This implements calendar system supports from QCalendar.

CCBUG: 429892
parent 3d5b448d
......@@ -5,6 +5,7 @@
set(alternatecalendar_LIB_SRCS
alternatecalendarplugin.cpp
provider/abstractcalendarprovider.cpp
provider/qtcalendar.cpp
)
set(alternatecalendar_LINK_LIBRARIES
......
......@@ -6,6 +6,8 @@
#include "alternatecalendarplugin.h"
#include "provider/qtcalendar.h"
class AlternateCalendarPluginPrivate
{
public:
......
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qtcalendar.h"
class QtCalendarProviderPrivate
{
public:
explicit QtCalendarProviderPrivate(QCalendar::System system);
~QtCalendarProviderPrivate();
QDate fromGregorian(const QDate &date) const;
CalendarEvents::CalendarEventsPlugin::SubLabel subLabels(const QDate &date) const;
private:
const QCalendar m_calendar;
};
QtCalendarProviderPrivate::QtCalendarProviderPrivate(QCalendar::System system)
: m_calendar(QCalendar(system))
{
}
QtCalendarProviderPrivate::~QtCalendarProviderPrivate()
{
}
QDate QtCalendarProviderPrivate::fromGregorian(const QDate &date) const
{
if (!date.isValid()) {
return QDate();
}
const QCalendar::YearMonthDay ymd = m_calendar.partsFromDate(date);
return QDate(ymd.year, ymd.month, ymd.day);
}
CalendarEvents::CalendarEventsPlugin::SubLabel QtCalendarProviderPrivate::subLabels(const QDate &date) const
{
auto sublabel = CalendarEvents::CalendarEventsPlugin::SubLabel{};
if (!date.isValid()) {
return sublabel;
}
const QDate altDate = fromGregorian(date);
sublabel.label = QLocale::system().toString(altDate);
return sublabel;
}
QtCalendarProvider::QtCalendarProvider(QObject *parent, CalendarSystem::System calendarSystem)
: AbstractCalendarProvider(parent, calendarSystem)
, d(new QtCalendarProviderPrivate(static_cast<QCalendar::System>(calendarSystem)))
{
Q_ASSERT(m_calendarSystem <= static_cast<CalendarSystem::System>(QCalendar::System::Last));
}
QtCalendarProvider::~QtCalendarProvider()
{
}
QDate QtCalendarProvider::fromGregorian(const QDate &date) const
{
return d->fromGregorian(date);
}
CalendarEvents::CalendarEventsPlugin::SubLabel QtCalendarProvider::subLabels(const QDate &date) const
{
return d->subLabels(date);
}
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "abstractcalendarprovider.h"
/**
* @short An alternate calendar provider for the calendar system available in QCalendar.
*
* This class presents an alternate calendar provider for the calendar system available
* in QCalendar.
* The available calendar systems are listed in QCalendar::availableCalendars().
*/
class QtCalendarProvider : public AbstractCalendarProvider
{
Q_OBJECT
public:
explicit QtCalendarProvider(QObject *parent, CalendarSystem::System calendarSystem);
~QtCalendarProvider() override;
QDate fromGregorian(const QDate &date) const override;
CalendarEvents::CalendarEventsPlugin::SubLabel subLabels(const QDate &date) const override;
private:
const std::unique_ptr<class QtCalendarProviderPrivate> d;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment