time-kcm: Port to FormCard

This commit is contained in:
Carl Schwan 2023-09-21 20:05:48 +02:00
parent 8030c73c67
commit 37b9fad3b1
No known key found for this signature in database
GPG key ID: 02325448204E452A
4 changed files with 130 additions and 145 deletions

View file

@ -12,7 +12,7 @@
class TimeZoneData class TimeZoneData
{ {
public: public:
QString id; QByteArray id;
QString region; QString region;
QString city; QString city;
QString comment; QString comment;

View file

@ -58,30 +58,32 @@ TimeZoneModel::~TimeZoneModel()
int TimeZoneModel::rowCount(const QModelIndex &parent) const int TimeZoneModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); return parent.isValid() ? 0 : m_data.count();
return m_data.count();
} }
QVariant TimeZoneModel::data(const QModelIndex &index, int role) const QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
{ {
if (index.isValid()) { Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
TimeZoneData currentData = m_data.at(index.row());
switch (role) { const TimeZoneData &currentData = m_data.at(index.row());
case TimeZoneIdRole:
switch (role) {
case TimeZoneIdRole:
return currentData.id;
case RegionRole: {
return currentData.region;
case CityRole:
if (currentData.city.isEmpty())
return currentData.id; return currentData.id;
case RegionRole:
return currentData.region;
case CityRole:
return currentData.city;
case CommentRole:
return currentData.comment;
case CheckedRole:
return currentData.checked;
} }
return currentData.city;
case CommentRole:
return currentData.comment;
case CheckedRole:
return currentData.checked;
} }
return QVariant(); return {};
} }
bool TimeZoneModel::setData(const QModelIndex &index, const QVariant &value, int role) bool TimeZoneModel::setData(const QModelIndex &index, const QVariant &value, int role)
@ -120,7 +122,7 @@ void TimeZoneModel::update()
const QStringList data = QString::fromUtf8(localZone.id()).split(QLatin1Char('/')); const QStringList data = QString::fromUtf8(localZone.id()).split(QLatin1Char('/'));
TimeZoneData local; TimeZoneData local;
local.id = QStringLiteral("Local"); local.id = "Local";
local.region = i18nc("This means \"Local Timezone\"", "Local"); local.region = i18nc("This means \"Local Timezone\"", "Local");
local.city = m_timezonesI18n->i18nCity(data.last()); local.city = m_timezonesI18n->i18nCity(data.last());
local.comment = i18n("Your system time zone"); local.comment = i18n("Your system time zone");
@ -131,21 +133,22 @@ void TimeZoneModel::update()
QStringList cities; QStringList cities;
QHash<QString, QTimeZone> zonesByCity; QHash<QString, QTimeZone> zonesByCity;
const QList<QByteArray> systemTimeZones = QTimeZone::availableTimeZoneIds(); const auto systemTimeZones = QTimeZone::availableTimeZoneIds();
for (auto it = systemTimeZones.constBegin(); it != systemTimeZones.constEnd(); ++it) { for (const auto &id : systemTimeZones) {
const QTimeZone zone(*it); const QTimeZone zone(id);
const QStringList splitted = QString::fromUtf8(zone.id()).split(QStringLiteral("/"));
const QStringList splitted = QString::fromUtf8(id).split(QStringLiteral("/"));
// CITY | COUNTRY | CONTINENT // CITY | COUNTRY | CONTINENT
const QString key = QStringLiteral("%1|%2|%3").arg(splitted.last(), QLocale::countryToString(zone.country()), splitted.first()); const QString key = QStringLiteral("%1|%2|%3").arg(splitted.last(), QLocale::territoryToString(zone.territory()), splitted.first());
cities.append(key); cities.append(key);
zonesByCity.insert(key, zone); zonesByCity.insert(key, zone);
} }
cities.sort(Qt::CaseInsensitive); cities.sort(Qt::CaseInsensitive);
Q_FOREACH (const QString &key, cities) { for (const QString &key : std::as_const(cities)) {
const QTimeZone timeZone = zonesByCity.value(key); const QTimeZone timeZone = zonesByCity.value(key);
QString comment = timeZone.comment(); QString comment = timeZone.comment();
@ -157,10 +160,10 @@ void TimeZoneModel::update()
const QStringList cityCountryContinent = key.split(QLatin1Char('|')); const QStringList cityCountryContinent = key.split(QLatin1Char('|'));
TimeZoneData newData; TimeZoneData newData;
newData.id = QString::fromUtf8(timeZone.id()); newData.id = timeZone.id();
newData.region = timeZone.country() == QLocale::AnyCountry newData.region = timeZone.territory() == QLocale::AnyCountry
? QString() ? QString()
: m_timezonesI18n->i18nContinents(cityCountryContinent.at(2)) + QLatin1Char('/') + m_timezonesI18n->i18nCountry(timeZone.country()); : m_timezonesI18n->i18nContinents(cityCountryContinent.at(2)) + QLatin1Char('/') + m_timezonesI18n->i18nCountry(timeZone.territory());
newData.city = m_timezonesI18n->i18nCity(cityCountryContinent.at(0)); newData.city = m_timezonesI18n->i18nCity(cityCountryContinent.at(0));
newData.comment = comment; newData.comment = comment;
newData.checked = false; newData.checked = false;

View file

@ -5,11 +5,11 @@
SPDX-License-Identifier: GPL-2.0-or-later SPDX-License-Identifier: GPL-2.0-or-later
*/ */
#ifndef TIMEZONEMODEL_H #pragma once
#define TIMEZONEMODEL_H
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QTimeZone>
#include "timezonedata.h" #include "timezonedata.h"
@ -76,5 +76,3 @@ private:
QStringList m_selectedTimeZones; QStringList m_selectedTimeZones;
TimezonesI18n *m_timezonesI18n; TimezonesI18n *m_timezonesI18n;
}; };
#endif // TIMEZONEMODEL_H

View file

@ -5,141 +5,107 @@
* SPDX-License-Identifier: LGPL-2.0-or-later * SPDX-License-Identifier: LGPL-2.0-or-later
*/ */
import QtQuick 2.7 import QtQuick
import QtQuick.Layouts 1.3 import QtQuick.Layouts
import QtQuick.Controls 2.3 as Controls import QtQuick.Controls as Controls
import org.kde.kirigami 2.10 as Kirigami import org.kde.kirigami as Kirigami
import org.kde.kcmutils import org.kde.kcmutils
import org.kde.timesettings 1.0 import org.kde.timesettings
import org.kde.kirigamiaddons.labs.mobileform 0.1 as MobileForm import org.kde.kirigamiaddons.formcard 1 as FormCard
import org.kde.kirigamiaddons.delegates 1 as Delegates
SimpleKCM { SimpleKCM {
id: timeModule id: timeModule
leftPadding: 0 leftPadding: 0
rightPadding: 0 rightPadding: 0
topPadding: Kirigami.Units.gridUnit topPadding: 0
bottomPadding: Kirigami.Units.gridUnit bottomPadding: 0
Component {
id: listDelegateComponent
Kirigami.BasicListItem {
text: {
if (model) {
if (model.region) {
return "%1 / %2".arg(model.region).arg(model.city)
} else {
return model.city
}
}
return ""
}
onClicked: {
timeZonePickerSheet.close()
kcm.saveTimeZone(model.timeZoneId)
}
}
}
ColumnLayout { ColumnLayout {
spacing: 0 spacing: 0
width: parent.width
MobileForm.FormCard { FormCard.FormHeader {
Layout.fillWidth: true title: i18n("Display")
}
contentItem: ColumnLayout { FormCard.FormCard {
spacing: 0 FormCard.FormSwitchDelegate {
id: hourFormatSwitch
MobileForm.FormCardHeader { text: i18n("24-Hour Format")
title: i18n("Display") description: i18n("Whether to use a 24-hour format for clocks.")
checked: kcm.twentyFour
onCheckedChanged: {
kcm.twentyFour = checked
} }
}
MobileForm.FormSwitchDelegate { FormCard.FormDelegateSeparator { above: hourFormatSwitch; below: timeZoneSelect }
id: hourFormatSwitch
text: i18n("24-Hour Format")
description: i18n("Whether to use a 24-hour format for clocks.")
checked: kcm.twentyFour
onCheckedChanged: {
kcm.twentyFour = checked
}
}
MobileForm.FormDelegateSeparator { above: hourFormatSwitch; below: timeZoneSelect } FormCard.FormButtonDelegate {
id: timeZoneSelect
MobileForm.FormButtonDelegate { text: i18n("Timezone")
id: timeZoneSelect description: kcm.timeZone
text: i18n("Timezone") onClicked: timeZonePickerSheet.open()
description: kcm.timeZone
onClicked: timeZonePickerSheet.open()
}
} }
} }
MobileForm.FormCard { FormCard.FormHeader {
Layout.topMargin: Kirigami.Units.gridUnit title: i18n("Time and Date")
Layout.fillWidth: true }
contentItem: ColumnLayout { FormCard.FormCard {
spacing: 0 FormCard.FormSwitchDelegate {
id: ntpCheckBox
MobileForm.FormCardHeader { text: i18n("Automatic Time Synchronization")
title: i18n("Time and Date") description: i18n("Whether to set the time automatically.")
} checked: kcm.useNtp
onCheckedChanged: {
MobileForm.FormSwitchDelegate { kcm.useNtp = checked
id: ntpCheckBox if (!checked) {
text: i18n("Automatic Time Synchronization") kcm.ntpServer = ""
description: i18n("Whether to set the time automatically.") kcm.saveTime()
checked: kcm.useNtp
onCheckedChanged: {
kcm.useNtp = checked
if (!checked) {
kcm.ntpServer = ""
kcm.saveTime()
}
} }
} }
}
MobileForm.FormDelegateSeparator { above: ntpCheckBox; below: timeSelect } FormCard.FormDelegateSeparator { above: ntpCheckBox; below: timeSelect }
MobileForm.FormButtonDelegate { FormCard.FormButtonDelegate {
id: timeSelect id: timeSelect
enabled: !ntpCheckBox.checked enabled: !ntpCheckBox.checked
icon.name: "clock" icon.name: "clock"
text: i18n("Current Time") text: i18n("Current Time")
description: Qt.formatTime(kcm.currentTime, Locale.LongFormat) description: Qt.formatTime(kcm.currentTime, Locale.LongFormat)
onClicked: timePickerSheet.open() onClicked: timePickerSheet.open()
} }
MobileForm.FormDelegateSeparator { above: timeSelect; below: dateSelect } FormCard.FormDelegateSeparator { above: timeSelect; below: dateSelect }
MobileForm.FormButtonDelegate { FormCard.FormButtonDelegate {
id: dateSelect id: dateSelect
enabled: !ntpCheckBox.checked enabled: !ntpCheckBox.checked
icon.name: "view-calendar" icon.name: "view-calendar"
text: i18n("Date") text: i18n("Date")
description: Qt.formatDate(kcm.currentDate, Locale.LongFormat) description: Qt.formatDate(kcm.currentDate, Locale.LongFormat)
onClicked: datePickerSheet.open() onClicked: datePickerSheet.open()
}
} }
} }
} }
Kirigami.OverlaySheet { data: Kirigami.OverlaySheet {
id: timeZonePickerSheet id: timeZonePickerSheet
header: ColumnLayout { header: ColumnLayout {
Kirigami.Heading { Kirigami.Heading {
text: i18nc("@title:window", "Pick Timezone") text: i18nc("@title:window", "Pick Timezone")
Layout.fillWidth: true
} }
Kirigami.SearchField { Kirigami.SearchField {
Layout.fillWidth: true Layout.fillWidth: true
width: parent.width onTextChanged: kcm.timeZonesModel.filterString = text
onTextChanged: {
kcm.timeZonesModel.filterString = text
}
} }
} }
@ -152,17 +118,35 @@ SimpleKCM {
onClicked: timeZonePickerSheet.close() onClicked: timeZonePickerSheet.close()
} }
} }
ListView { ListView {
id: listView id: listView
clip: true clip: true
anchors.fill: parent
implicitWidth: 18 * Kirigami.Units.gridUnit implicitWidth: 18 * Kirigami.Units.gridUnit
model: kcm.timeZonesModel
delegate: Kirigami.DelegateRecycler {
width: listView.width
sourceComponent: listDelegateComponent topMargin: Math.round(Kirigami.Units.smallSpacing / 2)
bottomMargin: Math.round(Kirigami.Units.smallSpacing / 2)
model: kcm.timeZonesModel
delegate: Delegates.RoundedItemDelegate {
required property string region
required property string city
width: ListView.view.width
text: {
if (region) {
return "%1 / %2".arg(region).arg(city)
} else {
return city
}
}
onClicked: {
timeZonePickerSheet.close()
kcm.saveTimeZone(model.timeZoneId)
}
} }
} }
} }
@ -175,7 +159,7 @@ SimpleKCM {
enabled: !ntpCheckBox.checked enabled: !ntpCheckBox.checked
twentyFour: hourFormatSwitch.checked twentyFour: hourFormatSwitch.checked
implicitWidth: width > Kirigami.Units.gridUnit * 15 ? width : Kirigami.Units.gridUnit * 15 implicitWidth: Kirigami.Units.gridUnit * 15
Component.onCompleted: { Component.onCompleted: {
var date = new Date(kcm.currentTime); var date = new Date(kcm.currentTime);
@ -185,7 +169,7 @@ SimpleKCM {
} }
Connections { Connections {
target: kcm target: kcm
onCurrentTimeChanged: { function onCurrentTimeChanged() {
if (timePicker.userConfiguring) { if (timePicker.userConfiguring) {
return; return;
} }
@ -229,7 +213,7 @@ SimpleKCM {
} }
Connections { Connections {
target: kcm target: kcm
onCurrentDateChanged: { function onCurrentDateChanged() {
if (datePicker.userConfiguring) { if (datePicker.userConfiguring) {
return return
} }