Verified Commit 250860cc authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

plasmacalendarplugins/alternatecalendar: use calendar id string in the config

Enum is not stable, use id string instead.
parent 3b1b79af
Pipeline #182539 passed with stage
in 1 minute and 24 seconds
......@@ -4,6 +4,7 @@
set(alternatecalendar_LIB_SRCS
alternatecalendarplugin.cpp
calendarsystem.h
provider/abstractcalendarprovider.cpp
provider/qtcalendar.cpp
)
......@@ -11,6 +12,7 @@ set(alternatecalendar_LIB_SRCS
set(alternatecalendar_LINK_LIBRARIES
KF5::ConfigCore
KF5::CalendarEvents
KF5::I18n
)
if(${HAVE_ICU})
......
......@@ -4,5 +4,5 @@
#!/usr/bin/env bash
$EXTRACTRC `find . -name \*.rc -o -name \*.ui -o -name \*.kcfg` >> rc.cpp
$XGETTEXT `find . -name \*.js -o -name \*.qml -o -name \*.cpp` -o $podir/plasma_calendar_alternatecalendar.pot
$XGETTEXT `find . -name \*.js -o -name \*.qml -o -name \*.cpp -o -name \*.h` -o $podir/plasma_calendar_alternatecalendar.pot
rm -f rc.cpp
......@@ -65,9 +65,19 @@ AlternateCalendarPluginPrivate::~AlternateCalendarPluginPrivate()
void AlternateCalendarPluginPrivate::init()
{
m_calendarSystem = static_cast<CalendarSystem::System>(m_generalConfigGroup.readEntry("calendarSystem", static_cast<int>(CalendarSystem::Gregorian)));
m_dateOffset = m_generalConfigGroup.readEntry("dateOffset", 0);
// Find the matched calendar system
const QString system = m_generalConfigGroup.readEntry("calendarSystem", "Julian");
const auto systemIt = s_calendarMap.find(system);
if (systemIt == s_calendarMap.end()) {
// Invalid config, fall back to Gregorian
m_calendarSystem = CalendarSystem::Gregorian;
} else {
m_calendarSystem = systemIt->second.system;
}
// Load/Reload the calendar provider
switch (m_calendarSystem) {
#ifdef HAVE_ICU
......
......@@ -6,16 +6,20 @@
#pragma once
#include <map>
#include <QCalendar>
#include <QMetaType>
#include <KLocalizedString>
class CalendarSystem
{
Q_GADGET
public:
/**
* @note When adding a new calendar system, CalendarSystemModel should also be updated.
* @note When adding a new calendar system, \s_calendarMap should also be updated.
*/
enum System {
Gregorian = static_cast<int>(QCalendar::System::Gregorian),
......@@ -35,3 +39,38 @@ public:
Q_ENUM(System)
};
Q_DECLARE_METATYPE(CalendarSystem)
struct CalendarSystemItem {
CalendarSystem::System system;
QString id;
QString text;
};
// clang-format off
static const std::map<QString /* id */, CalendarSystemItem> s_calendarMap{
{
QStringLiteral("Julian"),
{CalendarSystem::Julian, QStringLiteral("Julian"), i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Julian")}
},
{
QStringLiteral("Milankovic"),
{CalendarSystem::Milankovic, QStringLiteral("Milankovic"), i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Milankovic")}
},
{
QStringLiteral("Jalali"),
{CalendarSystem::Jalali, QStringLiteral("Jalali"), i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "The Solar Hijri Calendar (Persian)")}
},
{
QStringLiteral("IslamicCivil"),
{CalendarSystem::IslamicCivil, QStringLiteral("IslamicCivil"), i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "The Islamic Civil Calendar")}
},
{
QStringLiteral("Chinese"),
{CalendarSystem::Chinese, QStringLiteral("Chinese"), i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Chinese Lunar Calendar")}
},
{
QStringLiteral("Indian"),
{CalendarSystem::Indian, QStringLiteral("Indian"), i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Indian National Calendar")}
},
};
// clang-format on
......@@ -10,39 +10,25 @@
#include <QMetaEnum>
#include <KLocalizedString>
#include <KSharedConfig>
CalendarSystemModel::CalendarSystemModel(QObject *parent)
: QAbstractListModel(parent)
{
const std::map<CalendarSystem::System, QString> textMap{
#ifndef QT_BOOTSTRAPPED
{CalendarSystem::Julian, i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Julian")},
{CalendarSystem::Milankovic, i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Milankovic")},
#endif
#if QT_CONFIG(jalalicalendar)
{CalendarSystem::Jalali, i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "The Solar Hijri Calendar (Persian)")},
#endif
#if QT_CONFIG(islamiccivilcalendar)
{CalendarSystem::IslamicCivil, i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "The Islamic Civil Calendar")},
#endif
#ifdef HAVE_ICU
{CalendarSystem::Chinese, i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Chinese Lunar Calendar")},
{CalendarSystem::Indian, i18ndc("plasma_calendar_alternatecalendar", "@item:inlist", "Indian National Calendar")},
#endif
};
const QMetaEnum e = QMetaEnum::fromType<CalendarSystem::System>();
beginInsertRows(QModelIndex(), 0, e.keyCount() - 2 /* Gregorian */);
m_items.reserve(e.keyCount() - 1);
for (int k = 0; k < e.keyCount(); k++) {
CalendarSystemItem item;
item.value = static_cast<CalendarSystem::System>(e.value(k));
if (textMap.count(item.value) > 0) {
item.text = textMap.at(item.value);
m_items.emplace_back(item);
const auto system = static_cast<CalendarSystem::System>(e.value(k));
auto it = std::find_if(s_calendarMap.cbegin(), s_calendarMap.cend(), [system](const std::pair<QString, CalendarSystemItem> &pr) {
return pr.second.system == system;
});
if (it != s_calendarMap.cend()) {
m_items.emplace_back(it->second);
}
}
......@@ -60,8 +46,8 @@ QVariant CalendarSystemModel::data(const QModelIndex &index, int role) const
switch (role) {
case Qt::DisplayRole:
return item.text;
case ValueRole:
return item.value;
case IdRole:
return item.id;
default:
return QVariant();
}
......@@ -76,14 +62,14 @@ QHash<int, QByteArray> CalendarSystemModel::roleNames() const
{
return {
{Qt::DisplayRole, QByteArrayLiteral("display")},
{ValueRole, QByteArrayLiteral("value")},
{IdRole, QByteArrayLiteral("id")},
};
}
int CalendarSystemModel::indexOf(CalendarSystem::System value) const
int CalendarSystemModel::indexOf(const QString &id) const
{
const auto it = std::find_if(m_items.cbegin(), m_items.cend(), [value](const CalendarSystemItem &item) {
return item.value == value;
const auto it = std::find_if(m_items.cbegin(), m_items.cend(), [&id](const CalendarSystemItem &item) {
return item.id == id;
});
if (it != m_items.cend()) {
......@@ -100,7 +86,7 @@ ConfigStorage::ConfigStorage(QObject *parent)
auto config = KSharedConfig::openConfig(QStringLiteral("plasma_calendar_alternatecalendar"));
m_generalConfigGroup = config->group("General");
m_calendarSystem = static_cast<CalendarSystem::System>(m_generalConfigGroup.readEntry("calendarSystem", static_cast<int>(CalendarSystem::Julian)));
m_calendarSystem = m_generalConfigGroup.readEntry("calendarSystem", "Gregorian");
m_dateOffset = m_generalConfigGroup.readEntry("dateOffset", 0);
}
......@@ -116,7 +102,7 @@ int ConfigStorage::currentIndex() const
void ConfigStorage::save()
{
m_generalConfigGroup.writeEntry("calendarSystem", static_cast<int>(m_calendarSystem), KConfigBase::Notify);
m_generalConfigGroup.writeEntry("calendarSystem", m_calendarSystem, KConfigBase::Notify);
m_generalConfigGroup.writeEntry("dateOffset", m_dateOffset, KConfigBase::Notify);
m_generalConfigGroup.sync();
......
......@@ -20,7 +20,7 @@ class CalendarSystemModel : public QAbstractListModel
public:
enum Role {
ValueRole = Qt::UserRole,
IdRole = Qt::UserRole,
};
explicit CalendarSystemModel(QObject *parent = nullptr);
......@@ -29,14 +29,9 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
int indexOf(CalendarSystem::System value) const;
int indexOf(const QString &id) const;
private:
struct CalendarSystemItem {
QString text;
CalendarSystem::System value;
};
std::vector<CalendarSystemItem> m_items;
};
......@@ -47,7 +42,7 @@ class ConfigStorage : public QObject
/**
* The current choosen calendar system
*/
Q_PROPERTY(CalendarSystem::System calendarSystem MEMBER m_calendarSystem NOTIFY calendarSystemChanged)
Q_PROPERTY(QString calendarSystem MEMBER m_calendarSystem NOTIFY calendarSystemChanged)
/**
* The available calendar system list
......@@ -86,7 +81,7 @@ Q_SIGNALS:
private:
KConfigGroup m_generalConfigGroup;
CalendarSystem::System m_calendarSystem;
QString m_calendarSystem;
CalendarSystemModel *const m_calendarSystemModel;
int m_dateOffset; // For the (tabular) Islamic Civil calendar
......
......@@ -38,14 +38,14 @@ Kirigami.FormLayout {
Kirigami.FormData.label: i18ndc("plasma_calendar_alternatecalendar", "@label:listbox", "Calendar system:")
model: configStorage.calendarSystemModel
textRole: "display"
valueRole: "value"
valueRole: "id"
currentIndex: configStorage.currentIndex
onActivated: configPage.configurationChanged();
}
Loader {
id: dateOffsetSpinBoxLoader
active: calendarSystemComboBox.currentValue === CalendarSystem.IslamicCivil
active: calendarSystemComboBox.currentValue === "IslamicCivil"
visible: active
Kirigami.FormData.label: i18ndc("plasma_calendar_alternatecalendar", "@label:spinbox", "Date offset:")
......
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