diff --git a/kcms/time/timezonedata.h b/kcms/time/timezonedata.h index 98d1338b..e6af11cb 100644 --- a/kcms/time/timezonedata.h +++ b/kcms/time/timezonedata.h @@ -12,7 +12,7 @@ class TimeZoneData { public: - QString id; + QByteArray id; QString region; QString city; QString comment; diff --git a/kcms/time/timezonemodel.cpp b/kcms/time/timezonemodel.cpp index 4960eb82..33504094 100644 --- a/kcms/time/timezonemodel.cpp +++ b/kcms/time/timezonemodel.cpp @@ -58,30 +58,32 @@ TimeZoneModel::~TimeZoneModel() int TimeZoneModel::rowCount(const QModelIndex &parent) const { - Q_UNUSED(parent); - return m_data.count(); + return parent.isValid() ? 0 : m_data.count(); } QVariant TimeZoneModel::data(const QModelIndex &index, int role) const { - if (index.isValid()) { - TimeZoneData currentData = m_data.at(index.row()); + Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)); - switch (role) { - case TimeZoneIdRole: + const TimeZoneData ¤tData = m_data.at(index.row()); + + switch (role) { + case TimeZoneIdRole: + return currentData.id; + case RegionRole: { + return currentData.region; + case CityRole: + if (currentData.city.isEmpty()) 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) @@ -120,7 +122,7 @@ void TimeZoneModel::update() const QStringList data = QString::fromUtf8(localZone.id()).split(QLatin1Char('/')); TimeZoneData local; - local.id = QStringLiteral("Local"); + local.id = "Local"; local.region = i18nc("This means \"Local Timezone\"", "Local"); local.city = m_timezonesI18n->i18nCity(data.last()); local.comment = i18n("Your system time zone"); @@ -131,21 +133,22 @@ void TimeZoneModel::update() QStringList cities; QHash zonesByCity; - const QList systemTimeZones = QTimeZone::availableTimeZoneIds(); + const auto 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("/")); + for (const auto &id : systemTimeZones) { + const QTimeZone zone(id); + + const QStringList splitted = QString::fromUtf8(id).split(QStringLiteral("/")); // 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); zonesByCity.insert(key, zone); } cities.sort(Qt::CaseInsensitive); - Q_FOREACH (const QString &key, cities) { + for (const QString &key : std::as_const(cities)) { const QTimeZone timeZone = zonesByCity.value(key); QString comment = timeZone.comment(); @@ -157,10 +160,10 @@ void TimeZoneModel::update() const QStringList cityCountryContinent = key.split(QLatin1Char('|')); TimeZoneData newData; - newData.id = QString::fromUtf8(timeZone.id()); - newData.region = timeZone.country() == QLocale::AnyCountry + newData.id = timeZone.id(); + newData.region = timeZone.territory() == QLocale::AnyCountry ? 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.comment = comment; newData.checked = false; diff --git a/kcms/time/timezonemodel.h b/kcms/time/timezonemodel.h index 1bc71078..0b64aa1e 100644 --- a/kcms/time/timezonemodel.h +++ b/kcms/time/timezonemodel.h @@ -5,11 +5,11 @@ SPDX-License-Identifier: GPL-2.0-or-later */ -#ifndef TIMEZONEMODEL_H -#define TIMEZONEMODEL_H +#pragma once #include #include +#include #include "timezonedata.h" @@ -76,5 +76,3 @@ private: QStringList m_selectedTimeZones; TimezonesI18n *m_timezonesI18n; }; - -#endif // TIMEZONEMODEL_H diff --git a/kcms/time/ui/main.qml b/kcms/time/ui/main.qml index 361d7ffb..d1aed363 100644 --- a/kcms/time/ui/main.qml +++ b/kcms/time/ui/main.qml @@ -5,141 +5,107 @@ * SPDX-License-Identifier: LGPL-2.0-or-later */ -import QtQuick 2.7 -import QtQuick.Layouts 1.3 -import QtQuick.Controls 2.3 as Controls +import QtQuick +import QtQuick.Layouts +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.timesettings 1.0 -import org.kde.kirigamiaddons.labs.mobileform 0.1 as MobileForm +import org.kde.timesettings +import org.kde.kirigamiaddons.formcard 1 as FormCard +import org.kde.kirigamiaddons.delegates 1 as Delegates SimpleKCM { id: timeModule leftPadding: 0 rightPadding: 0 - topPadding: Kirigami.Units.gridUnit - bottomPadding: Kirigami.Units.gridUnit - - 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) - } - } - } + topPadding: 0 + bottomPadding: 0 ColumnLayout { spacing: 0 - width: parent.width - - MobileForm.FormCard { - Layout.fillWidth: true - - contentItem: ColumnLayout { - spacing: 0 - - MobileForm.FormCardHeader { - title: i18n("Display") - } - MobileForm.FormSwitchDelegate { - 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 } - - MobileForm.FormButtonDelegate { - id: timeZoneSelect - text: i18n("Timezone") - description: kcm.timeZone - onClicked: timeZonePickerSheet.open() + FormCard.FormHeader { + title: i18n("Display") + } + + FormCard.FormCard { + FormCard.FormSwitchDelegate { + 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.FormCard { - Layout.topMargin: Kirigami.Units.gridUnit - Layout.fillWidth: true - - contentItem: ColumnLayout { - spacing: 0 - - MobileForm.FormCardHeader { - title: i18n("Time and Date") - } - MobileForm.FormSwitchDelegate { - id: ntpCheckBox - text: i18n("Automatic Time Synchronization") - description: i18n("Whether to set the time automatically.") - checked: kcm.useNtp - onCheckedChanged: { - kcm.useNtp = checked - if (!checked) { - kcm.ntpServer = "" - kcm.saveTime() - } + FormCard.FormDelegateSeparator { above: hourFormatSwitch; below: timeZoneSelect } + + FormCard.FormButtonDelegate { + id: timeZoneSelect + text: i18n("Timezone") + description: kcm.timeZone + onClicked: timeZonePickerSheet.open() + } + } + + FormCard.FormHeader { + title: i18n("Time and Date") + } + + FormCard.FormCard { + FormCard.FormSwitchDelegate { + id: ntpCheckBox + text: i18n("Automatic Time Synchronization") + description: i18n("Whether to set the time automatically.") + checked: kcm.useNtp + onCheckedChanged: { + kcm.useNtp = checked + if (!checked) { + kcm.ntpServer = "" + kcm.saveTime() } } - - MobileForm.FormDelegateSeparator { above: ntpCheckBox; below: timeSelect } - - MobileForm.FormButtonDelegate { - id: timeSelect - enabled: !ntpCheckBox.checked - icon.name: "clock" - text: i18n("Current Time") - description: Qt.formatTime(kcm.currentTime, Locale.LongFormat) - onClicked: timePickerSheet.open() - } - - MobileForm.FormDelegateSeparator { above: timeSelect; below: dateSelect } - - MobileForm.FormButtonDelegate { - id: dateSelect - enabled: !ntpCheckBox.checked - icon.name: "view-calendar" - text: i18n("Date") - description: Qt.formatDate(kcm.currentDate, Locale.LongFormat) - onClicked: datePickerSheet.open() - } + } + + FormCard.FormDelegateSeparator { above: ntpCheckBox; below: timeSelect } + + FormCard.FormButtonDelegate { + id: timeSelect + enabled: !ntpCheckBox.checked + icon.name: "clock" + text: i18n("Current Time") + description: Qt.formatTime(kcm.currentTime, Locale.LongFormat) + onClicked: timePickerSheet.open() + } + + FormCard.FormDelegateSeparator { above: timeSelect; below: dateSelect } + + FormCard.FormButtonDelegate { + id: dateSelect + enabled: !ntpCheckBox.checked + icon.name: "view-calendar" + text: i18n("Date") + description: Qt.formatDate(kcm.currentDate, Locale.LongFormat) + onClicked: datePickerSheet.open() } } } - Kirigami.OverlaySheet { + data: Kirigami.OverlaySheet { id: timeZonePickerSheet + header: ColumnLayout { Kirigami.Heading { text: i18nc("@title:window", "Pick Timezone") + Layout.fillWidth: true } + Kirigami.SearchField { Layout.fillWidth: true - width: parent.width - onTextChanged: { - kcm.timeZonesModel.filterString = text - } + onTextChanged: kcm.timeZonesModel.filterString = text } } @@ -152,17 +118,35 @@ SimpleKCM { onClicked: timeZonePickerSheet.close() } } + ListView { id: listView clip: true - anchors.fill: parent 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 twentyFour: hourFormatSwitch.checked - implicitWidth: width > Kirigami.Units.gridUnit * 15 ? width : Kirigami.Units.gridUnit * 15 + implicitWidth: Kirigami.Units.gridUnit * 15 Component.onCompleted: { var date = new Date(kcm.currentTime); @@ -185,7 +169,7 @@ SimpleKCM { } Connections { target: kcm - onCurrentTimeChanged: { + function onCurrentTimeChanged() { if (timePicker.userConfiguring) { return; } @@ -229,7 +213,7 @@ SimpleKCM { } Connections { target: kcm - onCurrentDateChanged: { + function onCurrentDateChanged() { if (datePicker.userConfiguring) { return }