diff --git a/src/app/SettingsPage.qml b/src/app/SettingsPage.qml index 8cd261f..6d90152 100644 --- a/src/app/SettingsPage.qml +++ b/src/app/SettingsPage.qml @@ -1,71 +1,73 @@ /* Copyright (C) 2018 Volker Krause This program 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 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 Library 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 . */ import QtQuick 2.5 import QtQuick.Layouts 1.1 import QtQuick.Controls 2.1 as QQC2 import org.kde.kirigami 2.4 as Kirigami import org.kde.itinerary 1.0 import "." as App Kirigami.ScrollablePage { id: root title: qsTr("Settings") CountryModel { id: countryModel } GridLayout { columns: 2 width: root.width QQC2.Label { text: qsTr("Home Country") } QQC2.ComboBox { model: countryModel textRole: "display" + currentIndex: countryModel.isoCodeToIndex(_settings.homeCountryIsoCode) + onActivated: _settings.homeCountryIsoCode = countryModel.isoCodeFromIndex(currentIndex) } QQC2.Label { text: qsTr("Weather Forecast") } QQC2.Switch { id: weatherSwitch checked: _settings.weatherForecastEnabled onToggled: _settings.weatherForecastEnabled = checked } QQC2.Label { Layout.columnSpan: 2 Layout.fillWidth: true text: qsTr("Showing weather forecasts will query online services.") visible: !weatherSwitch.checked } // ATTENTION do not remove this note, see https://api.met.no/license_data.html QQC2.Label { Layout.columnSpan: 2 Layout.fillWidth: true text: qsTr("Using data from The Norwegian Meteorological Institute under Creative Commons 4.0 BY International license.") visible: weatherSwitch.checked wrapMode: Text.WordWrap onLinkActivated: Qt.openUrlExternally(link) } } onBackRequested: pageStack.pop() } diff --git a/src/app/countrymodel.cpp b/src/app/countrymodel.cpp index 630b3b4..bb51beb 100644 --- a/src/app/countrymodel.cpp +++ b/src/app/countrymodel.cpp @@ -1,57 +1,74 @@ /* Copyright (C) 2018 Volker Krause This program 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 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 Library 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 . */ #include "countrymodel.h" #include #include using namespace KItinerary; CountryModel::CountryModel(QObject *parent) : QAbstractListModel(parent) { } CountryModel::~CountryModel() = default; +int CountryModel::isoCodeToIndex(const QString &isoCode) const +{ + const auto id = KnowledgeDb::CountryId{isoCode}; + const auto it = std::lower_bound(KnowledgeDb::countriesBegin(), KnowledgeDb::countriesEnd(), id, [](const KnowledgeDb::Country &lhs, KnowledgeDb::CountryId rhs) { + return lhs.id < rhs; + }); + if (it == KnowledgeDb::countriesEnd() || (*it).id != id) { + return -1; + } + return std::distance(KnowledgeDb::countriesBegin(), it); +} + +QString CountryModel::isoCodeFromIndex(int index) const +{ + return (KnowledgeDb::countriesBegin() + index)->id.toString(); +} + int CountryModel::rowCount(const QModelIndex& parent) const { if (parent.isValid()) { return 0; } return std::distance(KnowledgeDb::countriesBegin(), KnowledgeDb::countriesEnd()); } QVariant CountryModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) { return {}; } const auto it = KnowledgeDb::countriesBegin() + index.row(); switch (role) { case Qt::DisplayRole: return KContacts::Address::ISOtoCountry((*it).id.toString()); case Qt::EditRole: return (*it).id.toString(); } return {}; } diff --git a/src/app/countrymodel.h b/src/app/countrymodel.h index 766b394..864db06 100644 --- a/src/app/countrymodel.h +++ b/src/app/countrymodel.h @@ -1,35 +1,38 @@ /* Copyright (C) 2018 Volker Krause This program 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 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 Library 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 . */ #ifndef COUNTRYMODEL_H #define COUNTRYMODEL_H #include /** Country model for selecting the home country. */ class CountryModel : public QAbstractListModel { Q_OBJECT public: explicit CountryModel(QObject *parent = nullptr); ~CountryModel(); + Q_INVOKABLE int isoCodeToIndex(const QString &isoCode) const; + Q_INVOKABLE QString isoCodeFromIndex(int index) const; + int rowCount(const QModelIndex& parent) const override; QVariant data(const QModelIndex& index, int role) const override; }; #endif // COUNTRYMODEL_H diff --git a/src/app/settings.cpp b/src/app/settings.cpp index 26c451f..62ad366 100644 --- a/src/app/settings.cpp +++ b/src/app/settings.cpp @@ -1,56 +1,70 @@ /* Copyright (C) 2018 Volker Krause This program 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 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 Library 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 . */ #include "settings.h" #include Settings::Settings(QObject *parent) : QObject(parent) { QSettings s; s.beginGroup(QLatin1String("Settings")); m_weatherEnabled = s.value(QLatin1String("WeatherForecastEnabled"), false).toBool(); // TODO configurable home country m_homeCountry = s.value(QLatin1String("HomeCountry"), QStringLiteral("DE")).toString(); } Settings::~Settings() = default; bool Settings::weatherForecastEnabled() const { return m_weatherEnabled; } void Settings::setWeatherForecastEnabled(bool enabled) { if (m_weatherEnabled == enabled) { return; } m_weatherEnabled = enabled; QSettings s; s.beginGroup(QLatin1String("Settings")); s.setValue(QLatin1String("WeatherForecastEnabled"), enabled); emit weatherForecastEnabledChanged(enabled); } QString Settings::homeCountryIsoCode() const { return m_homeCountry; } + +void Settings::setHomeCountryIsoCode(const QString& isoCode) +{ + if (m_homeCountry == isoCode) { + return; + } + + m_homeCountry = isoCode; + QSettings s; + s.beginGroup(QLatin1String("Settings")); + s.setValue(QLatin1String("HomeCountry"), isoCode); + + emit homeCountryIsoCodeChanged(isoCode); +} diff --git a/src/app/settings.h b/src/app/settings.h index e418dd4..2f84c9b 100644 --- a/src/app/settings.h +++ b/src/app/settings.h @@ -1,46 +1,48 @@ /* Copyright (C) 2018 Volker Krause This program 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 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 Library 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 . */ #ifndef SETTINGS_H #define SETTINGS_H #include /** Application settings accessible by QML. */ class Settings : public QObject { Q_OBJECT Q_PROPERTY(bool weatherForecastEnabled READ weatherForecastEnabled WRITE setWeatherForecastEnabled NOTIFY weatherForecastEnabledChanged) + Q_PROPERTY(QString homeCountryIsoCode READ homeCountryIsoCode WRITE setHomeCountryIsoCode NOTIFY homeCountryIsoCodeChanged) public: explicit Settings(QObject *parent = nullptr); ~Settings(); bool weatherForecastEnabled() const; void setWeatherForecastEnabled(bool enabled); QString homeCountryIsoCode() const; + void setHomeCountryIsoCode(const QString &isoCode); signals: void weatherForecastEnabledChanged(bool enabled); void homeCountryIsoCodeChanged(const QString &isoCode); private: QString m_homeCountry; bool m_weatherEnabled = false; }; #endif // SETTINGS_H