Commit 296907c8 authored by Nicolas Fella's avatar Nicolas Fella

Remove unneeded QML plugin

parent 5da13090
set(dateandtime_SRCS
lib/timezonemodel.cpp
lib/timezonesi18n.cpp
lib/plugin.cpp)
add_definitions(-DTRANSLATION_DOMAIN=\"kirigami_dateandtime\")
add_library(dateandtimeplugin SHARED ${dateandtime_SRCS})
target_link_libraries(dateandtimeplugin
Qt5::Quick
Qt5::Qml
KF5::I18n
)
install(TARGETS dateandtimeplugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kirigamiaddons/dateandtime)
install(FILES
qmldir
ClockElement.qml
......
/*
* Copyright (C) 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QQmlEngine>
#include <QQmlExtensionPlugin>
#include "timezonemodel.h"
class KirigamiAddonsDataAndTimePlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
KirigamiAddonsDataAndTimePlugin() = default;
~KirigamiAddonsDataAndTimePlugin() = default;
void initializeEngine(QQmlEngine *engine, const char *uri) override {};
void registerTypes(const char *uri) override;
};
void KirigamiAddonsDataAndTimePlugin::registerTypes(const char *uri)
{
qmlRegisterType<TimeZoneModel>(uri, 0, 1, "TimeZoneModel");
qmlRegisterType<TimeZoneFilterProxy>(uri, 0, 1, "TimeZoneFilterModel");
}
#include "plugin.moc"
/***************************************************************************
* Copyright (C) 2014 Kai Uwe Broulik <kde@privat.broulik.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#ifndef TIMEZONEDATA_H
#define TIMEZONEDATA_H
#include <QString>
class TimeZoneData
{
public:
QString id;
QString region;
QString city;
QString comment;
bool checked;
int offsetFromUtc;
};
#endif // TIMEZONEDATA_H
/***************************************************************************
* Copyright (C) 2014 Kai Uwe Broulik <kde@privat.broulik.de> *
* Copyright (C) 2014 Martin Klapetek <mklapetek@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "timezonemodel.h"
#include "timezonesi18n.h"
#include <KLocalizedString>
#include <QStringMatcher>
#include <QTimeZone>
TimeZoneFilterProxy::TimeZoneFilterProxy(QObject *parent)
: QSortFilterProxyModel(parent)
{
m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
}
bool TimeZoneFilterProxy::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (!sourceModel() || m_filterString.isEmpty()) {
return true;
}
const QString city = sourceModel()->index(source_row, 0, source_parent).data(TimeZoneModel::CityRole).toString();
const QString region = sourceModel()->index(source_row, 0, source_parent).data(TimeZoneModel::RegionRole).toString();
const QString comment = sourceModel()->index(source_row, 0, source_parent).data(TimeZoneModel::CommentRole).toString();
if (m_stringMatcher.indexIn(city) != -1 || m_stringMatcher.indexIn(region) != -1 || m_stringMatcher.indexIn(comment) != -1) {
return true;
}
return false;
}
void TimeZoneFilterProxy::setFilterString(const QString &filterString)
{
m_filterString = filterString;
m_stringMatcher.setPattern(filterString);
emit filterStringChanged();
invalidateFilter();
}
//=============================================================================
TimeZoneModel::TimeZoneModel(QObject *parent)
: QAbstractListModel(parent)
, m_timezonesI18n(new TimezonesI18n(this))
{
update();
}
TimeZoneModel::~TimeZoneModel()
{
}
int TimeZoneModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_data.count();
}
QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
TimeZoneData currentData = m_data.at(index.row());
switch (role) {
case TimeZoneIdRole:
return currentData.id;
case RegionRole:
return currentData.region;
case CityRole:
return currentData.city;
case CommentRole:
return currentData.comment;
case CheckedRole:
return currentData.checked;
}
}
return QVariant();
}
bool TimeZoneModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || value.isNull()) {
return false;
}
if (role == CheckedRole) {
m_data[index.row()].checked = value.toBool();
emit dataChanged(index, index);
if (m_data[index.row()].checked) {
m_selectedTimeZones.append(m_data[index.row()].id);
m_offsetData.insert(m_data[index.row()].id, m_data[index.row()].offsetFromUtc);
} else {
m_selectedTimeZones.removeAll(m_data[index.row()].id);
m_offsetData.remove(m_data[index.row()].id);
}
sortTimeZones();
emit selectedTimeZonesChanged();
return true;
}
return false;
}
void TimeZoneModel::update()
{
beginResetModel();
m_data.clear();
QTimeZone localZone = QTimeZone(QTimeZone::systemTimeZoneId());
const QStringList data = QString::fromUtf8(localZone.id()).split(QLatin1Char('/'));
TimeZoneData local;
local.id = QStringLiteral("Local");
local.region = i18nc("This means \"Local Timezone\"", "Local");
local.city = m_timezonesI18n->i18nCity(data.last());
local.comment = i18n("Your system time zone");
local.checked = false;
m_data.append(local);
QStringList cities;
QHash<QString, QTimeZone> zonesByCity;
const QList<QByteArray> systemTimeZones = QTimeZone::availableTimeZoneIds();
for (auto it = systemTimeZones.constBegin(); it != systemTimeZones.constEnd(); ++it) {
const QTimeZone zone(*it);
const QStringList splitted = QString::fromUtf8(zone.id()).split(QStringLiteral("/"));
// CITY | COUNTRY | CONTINENT
const QString key = QStringLiteral("%1|%2|%3").arg(splitted.last(), QLocale::countryToString(zone.country()), splitted.first());
cities.append(key);
zonesByCity.insert(key, zone);
}
cities.sort(Qt::CaseInsensitive);
Q_FOREACH (const QString &key, cities) {
const QTimeZone timeZone = zonesByCity.value(key);
QString comment = timeZone.comment();
// FIXME - this was in the old code but makes no sense
// if (!comment.isEmpty()) {
// comment = i18n(comment.toUtf8());
// }
const QStringList cityCountryContinent = key.split(QLatin1Char('|'));
TimeZoneData newData;
newData.id = QString::fromUtf8(timeZone.id());
newData.region = timeZone.country() == QLocale::AnyCountry ? QString() : m_timezonesI18n->i18nContinents(cityCountryContinent.at(2)) + QLatin1Char('/') + m_timezonesI18n->i18nCountry(timeZone.country());
newData.city = m_timezonesI18n->i18nCity(cityCountryContinent.at(0));
newData.comment = comment;
newData.checked = false;
newData.offsetFromUtc = timeZone.offsetFromUtc(QDateTime::currentDateTimeUtc());
m_data.append(newData);
}
endResetModel();
}
void TimeZoneModel::setSelectedTimeZones(const QStringList &selectedTimeZones)
{
m_selectedTimeZones = selectedTimeZones;
for (int i = 0; i < m_data.size(); i++) {
if (m_selectedTimeZones.contains(m_data.at(i).id)) {
m_data[i].checked = true;
m_offsetData.insert(m_data[i].id, m_data[i].offsetFromUtc);
QModelIndex index = createIndex(i, 0);
emit dataChanged(index, index);
}
}
sortTimeZones();
}
void TimeZoneModel::selectLocalTimeZone()
{
m_data[0].checked = true;
QModelIndex index = createIndex(0, 0);
emit dataChanged(index, index);
m_selectedTimeZones << m_data[0].id;
emit selectedTimeZonesChanged();
}
QHash<int, QByteArray> TimeZoneModel::roleNames() const
{
return QHash<int, QByteArray>({{TimeZoneIdRole, "timeZoneId"}, {RegionRole, "region"}, {CityRole, "city"}, {CommentRole, "comment"}, {CheckedRole, "checked"}});
}
void TimeZoneModel::sortTimeZones()
{
std::sort(m_selectedTimeZones.begin(), m_selectedTimeZones.end(), [this](const QString &a, const QString &b) { return m_offsetData.value(a) < m_offsetData.value(b); });
}
/***************************************************************************
* Copyright (C) 2014 Kai Uwe Broulik <kde@privat.broulik.de> *
* Copyright (C) 2014 Martin Klapetek <mklapetek@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#ifndef TIMEZONEMODEL_H
#define TIMEZONEMODEL_H
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include "timezonedata.h"
class TimezonesI18n;
class TimeZoneFilterProxy : public QSortFilterProxyModel
{
Q_OBJECT
Q_PROPERTY(QString filterString WRITE setFilterString MEMBER m_filterString NOTIFY filterStringChanged)
public:
explicit TimeZoneFilterProxy(QObject *parent = nullptr);
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
void setFilterString(const QString &filterString);
Q_SIGNALS:
void filterStringChanged();
private:
QString m_filterString;
QStringMatcher m_stringMatcher;
};
//=============================================================================
class TimeZoneModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QStringList selectedTimeZones WRITE setSelectedTimeZones MEMBER m_selectedTimeZones NOTIFY selectedTimeZonesChanged)
public:
explicit TimeZoneModel(QObject *parent = nullptr);
~TimeZoneModel() override;
enum Roles { TimeZoneIdRole = Qt::UserRole + 1, RegionRole, CityRole, CommentRole, CheckedRole };
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
void update();
void setSelectedTimeZones(const QStringList &selectedTimeZones);
Q_INVOKABLE void selectLocalTimeZone();
Q_SIGNALS:
void selectedTimeZonesChanged();
protected:
QHash<int, QByteArray> roleNames() const override;
private:
void sortTimeZones();
QList<TimeZoneData> m_data;
QHash<QString, int> m_offsetData; // used for sorting
QStringList m_selectedTimeZones;
TimezonesI18n *m_timezonesI18n;
};
#endif // TIMEZONEMODEL_H
This diff is collapsed.
/*
* Copyright 2014 Martin Klapetek <mklapetek@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef TIMEZONESI18N_H
#define TIMEZONESI18N_H
#include <QHash>
#include <QLocale>
#include <QObject>
class TimezonesI18n : public QObject
{
Q_OBJECT
public:
explicit TimezonesI18n(QObject *parent = nullptr);
Q_INVOKABLE QString i18nCity(const QString &city);
Q_INVOKABLE QString i18nContinents(const QString &continent);
Q_INVOKABLE QString i18nCountry(QLocale::Country country);
private:
void init();
QHash<QString, QString> m_i18nCities;
QHash<QString, QString> m_i18nContinents;
QHash<QLocale::Country, QString> m_i18nCountries;
bool m_isInitialized;
};
#endif // TIMEZONESI18N_H
Markdown is supported
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