Verified Commit 1123ebec authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

alternatecalendar: Implement config backend

This provides a config backend and a model contaning all the available
calendar systems that can be used in a combobox.

CCBUG: 429892
parent 145155e0
......@@ -27,3 +27,5 @@ configure_file(config-ICU.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-ICU.h)
kcoreaddons_add_plugin(alternatecalendar SOURCES ${alternatecalendar_LIB_SRCS} INSTALL_NAMESPACE "plasmacalendarplugins")
target_link_libraries(alternatecalendar ${alternatecalendar_LINK_LIBRARIES})
add_subdirectory(config)
......@@ -6,6 +6,9 @@
#include "alternatecalendarplugin.h"
#include <KConfigGroup>
#include <KSharedConfig>
#include "provider/qtcalendar.h"
class AlternateCalendarPluginPrivate
......@@ -23,12 +26,16 @@ public:
private:
std::unique_ptr<AbstractCalendarProvider> m_calendarProvider;
KConfigGroup m_generalConfigGroup;
AlternateCalendarPlugin *q;
};
AlternateCalendarPluginPrivate::AlternateCalendarPluginPrivate(AlternateCalendarPlugin *parent)
: q(parent)
{
auto config = KSharedConfig::openConfig(QStringLiteral("plasma_calendar_alternatecalendar"));
m_generalConfigGroup = config->group("General");
init();
}
AlternateCalendarPluginPrivate::~AlternateCalendarPluginPrivate()
......@@ -37,6 +44,9 @@ 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);
// Load/Reload the calendar provider
switch (m_calendarSystem) {
#ifndef QT_BOOTSTRAPPED
......
......@@ -14,6 +14,9 @@ class CalendarSystem
Q_GADGET
public:
/**
* @note When adding a new calendar system, CalendarSystemModel should also be updated.
*/
enum System {
Gregorian = static_cast<int>(QCalendar::System::Gregorian),
#ifndef QT_BOOTSTRAPPED
......
# SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
# SPDX-License-Identifier: GPL-2.0-or-later
add_subdirectory(plugin)
# SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
# SPDX-License-Identifier: GPL-2.0-or-later
add_library(plasmacalendaralternatecalendarconfig SHARED configplugin.cpp configstorage.cpp ../../calendarsystem.h)
target_link_libraries(plasmacalendaralternatecalendarconfig
Qt::Qml
Qt::Core
KF5::ConfigCore
KF5::I18n
)
install(TARGETS plasmacalendaralternatecalendarconfig DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasmacalendar/alternatecalendarconfig)
install(FILES qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasmacalendar/alternatecalendarconfig)
/*
SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "configplugin.h"
#include <QQmlEngine>
#include "../../calendarsystem.h"
#include "configstorage.h"
void AlternateCalendarConfigPlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QByteArray("org.kde.plasmacalendar.alternatecalendarconfig"));
qmlRegisterType<ConfigStorage>(uri, 1, 0, "ConfigStorage");
qmlRegisterUncreatableType<CalendarSystem>(uri, 1, 0, "CalendarSystem", QStringLiteral("Error: only enums"));
}
/*
SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef CONFIGPLUGIN_H
#define CONFIGPLUGIN_H
#include <QQmlExtensionPlugin>
class AlternateCalendarConfigPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri) override;
};
#endif
/*
SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config-ICU.h"
#include "configstorage.h"
#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
};
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);
}
}
endInsertRows();
}
QVariant CalendarSystemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
const auto &item = m_items.at(index.row());
switch (role) {
case Qt::DisplayRole:
return item.text;
case ValueRole:
return item.value;
default:
return QVariant();
}
}
int CalendarSystemModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_items.size();
}
QHash<int, QByteArray> CalendarSystemModel::roleNames() const
{
return {
{Qt::DisplayRole, QByteArrayLiteral("display")},
{ValueRole, QByteArrayLiteral("value")},
};
}
int CalendarSystemModel::indexOf(CalendarSystem::System value) const
{
const auto it = std::find_if(m_items.cbegin(), m_items.cend(), [value](const CalendarSystemItem &item) {
return item.value == value;
});
if (it != m_items.cend()) {
return std::distance(m_items.cbegin(), it);
}
return 0;
}
ConfigStorage::ConfigStorage(QObject *parent)
: QObject(parent)
, m_calendarSystemModel(new CalendarSystemModel(this))
{
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_dateOffset = m_generalConfigGroup.readEntry("dateOffset", 0);
}
QAbstractListModel *ConfigStorage::calendarSystemModel() const
{
return m_calendarSystemModel;
}
int ConfigStorage::currentIndex() const
{
return m_calendarSystemModel->indexOf(m_calendarSystem);
}
void ConfigStorage::save()
{
m_generalConfigGroup.writeEntry("calendarSystem", static_cast<int>(m_calendarSystem), KConfigBase::Notify);
m_generalConfigGroup.writeEntry("dateOffset", m_dateOffset, KConfigBase::Notify);
m_generalConfigGroup.sync();
}
/*
SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef CONFIGSTORAGE_H
#define CONFIGSTORAGE_H
#include <QAbstractListModel>
#include <KConfigGroup>
#include "../../calendarsystem.h"
class CalendarSystemModel : public QAbstractListModel
{
Q_OBJECT
public:
enum Role {
ValueRole = Qt::UserRole,
};
explicit CalendarSystemModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
int indexOf(CalendarSystem::System value) const;
private:
struct CalendarSystemItem {
QString text;
CalendarSystem::System value;
};
std::vector<CalendarSystemItem> m_items;
};
class ConfigStorage : public QObject
{
Q_OBJECT
/**
* The current choosen calendar system
*/
Q_PROPERTY(CalendarSystem::System calendarSystem MEMBER m_calendarSystem NOTIFY calendarSystemChanged)
/**
* The available calendar system list
*/
Q_PROPERTY(QAbstractListModel *calendarSystemModel READ calendarSystemModel CONSTANT)
/**
* The index of current choosen calendar system
*/
Q_PROPERTY(int currentIndex READ currentIndex CONSTANT)
/**
* The date offset in days
*/
Q_PROPERTY(int dateOffset MEMBER m_dateOffset NOTIFY dateOffsetChanged)
public:
explicit ConfigStorage(QObject *parent = nullptr);
QAbstractListModel *calendarSystemModel() const;
int currentIndex() const;
/**
* Saves the modifed configuration.
*
* @see calendarSystem
* @see dateOffset
*/
Q_INVOKABLE void save();
Q_SIGNALS:
void calendarSystemChanged();
void dateOffsetChanged();
private:
KConfigGroup m_generalConfigGroup;
CalendarSystem::System m_calendarSystem;
CalendarSystemModel *const m_calendarSystemModel;
int m_dateOffset; // For the (tabular) Islamic Civil calendar
};
#endif
# SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
# SPDX-License-Identifier: GPL-2.0-or-later
module org.kde.plasmacalendar.alternatecalendarconfig
plugin plasmacalendaralternatecalendarconfig
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