Commit 9b0c25db authored by Volker Krause's avatar Volker Krause
Browse files

Unify the three different country combo boxes

parent ed272e3f
Pipeline #119080 passed with stage
in 3 minutes
......@@ -80,7 +80,6 @@ endif()
add_executable(itinerary-app ${itinerary_app_srcs})
target_sources(itinerary-app PRIVATE
main.cpp
countrymodel.cpp
developmentmodecontroller.cpp
documentsmodel.cpp
locationinformationdelegatecontroller.cpp
......
/*
SPDX-FileCopyrightText: 2021-2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
import QtQuick 2.15
import QtQuick.Controls 2.15 as QQC2
import org.kde.i18n.localeData 1.0
/** Combo box for showing a list of countries.
* The model is expected to be an array of ISO 3166-1 alpha 2 codes.
*/
QQC2.ComboBox {
/** The currently selected country, as a KCountry object. */
readonly property var currentCountry: Country.fromAlpha2(currentValue)
/** Initially selected country. */
property string initialCountry
onModelChanged: {
model = model.sort((lhs, rhs) => { return Country.fromAlpha2(lhs).name.localeCompare(Country.fromAlpha2(rhs).name); });
}
displayText: currentCountry ? (currentCountry.emojiFlag + ' ' + currentCountry.name) : ""
delegate: QQC2.ItemDelegate {
text: {
const c = Country.fromAlpha2(modelData);
return c.emojiFlag + ' ' + c.name;
}
width: parent ? parent.width : undefined
}
Component.onCompleted: {
if (initialCountry) {
currentIndex = indexOfValue(initialCountry);
}
}
}
......@@ -10,6 +10,7 @@ import QtQuick.Controls 2.1 as QQC2
import QtLocation 5.15 as QtLocation
import QtPositioning 5.15
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.kitinerary 1.0
import org.kde.itinerary 1.0
import "." as App
......@@ -29,7 +30,7 @@ Kirigami.FormLayout {
addr.postalCode = postalCode.text;
addr.addressLocality = addressLocality.text;
addr.addressRegion = addressRegion.text;
addr.addressCountry = countryModel.isoCodeFromIndex(addressCountry.currentIndex)
addr.addressCountry = addressCountry.currentValue;
var geo = place.geo;
geo.latitude = latitude;
geo.longitude = longitude;
......@@ -80,15 +81,11 @@ Kirigami.FormLayout {
text: place.address.addressRegion
}
CountryModel {
id: countryModel
}
QQC2.ComboBox {
App.CountryComboBox {
id: addressCountry
Kirigami.FormData.label: i18n("Country:")
model: countryModel
textRole: "display"
currentIndex: countryModel.isoCodeToIndex(place.address.addressCountry)
model: Country.allCountries.map(c => c.alpha2)
initialCountry: place.address.addressCountry
}
QtLocation.Plugin {
......@@ -132,7 +129,7 @@ Kirigami.FormLayout {
geocodeAddr.postalCode = postalCode.text;
geocodeAddr.city = addressLocality.text;
geocodeAddr.state = addressRegion.text;
geocodeAddr.countryCode = countryModel.isoCodeFromIndex(addressCountry.currentIndex);
geocodeAddr.countryCode = addressCountry.currentValue;
geocodeModel.update();
}
}
......
......@@ -9,6 +9,7 @@ import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import QtPositioning 5.11
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.itinerary 1.0
import "." as App
......@@ -28,10 +29,6 @@ Kirigami.ScrollablePage {
FavoriteLocationPage {}
}
CountryModel {
id: countryModel
}
Kirigami.FormLayout {
width: root.width
......@@ -41,13 +38,12 @@ Kirigami.ScrollablePage {
Kirigami.FormData.label: i18n("Home")
}
QQC2.ComboBox {
App.CountryComboBox {
Kirigami.FormData.label: i18n("Home Country")
Layout.fillWidth: true
model: countryModel
textRole: "display"
currentIndex: countryModel.isoCodeToIndex(Settings.homeCountryIsoCode)
onActivated: Settings.homeCountryIsoCode = countryModel.isoCodeFromIndex(currentIndex)
model: Country.allCountries.map(c => c.alpha2)
initialCountry: Settings.homeCountryIsoCode
onActivated: Settings.homeCountryIsoCode = currentValue
}
QQC2.Button {
......
......@@ -10,6 +10,7 @@ import org.kde.kirigami 2.17 as Kirigami
import org.kde.kitemmodels 1.0
import org.kde.i18n.localeData 1.0
import org.kde.kpublictransport 1.0
import "." as App
Kirigami.ScrollablePage {
id: root
......@@ -62,7 +63,7 @@ Kirigami.ScrollablePage {
]
header: ColumnLayout {
spacing: Kirigami.Units.smallSpacing
QQC2.ComboBox {
App.CountryComboBox {
id: countryCombo
Layout.topMargin: Kirigami.Units.smallSpacing
Layout.leftMargin: Kirigami.Units.smallSpacing
......@@ -82,20 +83,9 @@ Kirigami.ScrollablePage {
}
}
}
return [...new Set(countries)].sort((lhs, rhs) => { return Country.fromAlpha2(lhs).name.localeCompare(Country.fromAlpha2(rhs).name); });
}
readonly property var currentCountry: Country.fromAlpha2(currentValue)
displayText: currentCountry.emojiFlag + ' ' + currentCountry.name
delegate: QQC2.ItemDelegate {
text: {
const c = Country.fromAlpha2(modelData);
return c.emojiFlag + ' ' + c.name;
}
width: parent ? parent.width : undefined
}
Component.onCompleted: {
countryCombo.currentIndex = countryCombo.indexOfValue(root.initialCountry)
return [...new Set(countries)];
}
initialCountry: root.initialCountry
}
Kirigami.SearchField {
id: queryTextField
......
/*
SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "countrymodel.h"
#include <KCountry>
#include <QCollator>
CountryModel::CountryModel(QObject *parent)
: QAbstractListModel(parent)
{
m_countries = KCountry::allCountries();
QCollator collator;
std::sort(m_countries.begin(), m_countries.end(), [&collator](const auto &lhs, const auto &rhs) { return collator.compare(lhs.name(), rhs.name()) < 0; });
}
CountryModel::~CountryModel() = default;
int CountryModel::isoCodeToIndex(const QString &isoCode) const
{
const auto id = KCountry::fromAlpha2(isoCode);
const auto it = std::find(m_countries.constBegin(), m_countries.constEnd(), id);
if (it == m_countries.constEnd()) {
return -1;
}
return std::distance(m_countries.constBegin(), it);
}
QString CountryModel::isoCodeFromIndex(int index) const
{
if (index < 0 || index >= m_countries.size()) {
return {};
}
return m_countries.at(index).alpha2();
}
int CountryModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
return 0;
}
return m_countries.count();
}
QVariant CountryModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return {};
}
const auto country = m_countries.at(index.row());
switch (role) {
case Qt::DisplayRole:
return country.name();
case Qt::EditRole:
return country.alpha2();
}
return {};
}
/*
SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef COUNTRYMODEL_H
#define COUNTRYMODEL_H
#include <QAbstractListModel>
class KCountry;
/** Country model for selecting the home country. */
class CountryModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit CountryModel(QObject *parent = nullptr);
~CountryModel() override;
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;
private:
QList<KCountry> m_countries;
};
#endif // COUNTRYMODEL_H
......@@ -9,7 +9,6 @@
#include "logging.h"
#include "applicationcontroller.h"
#include "countrymodel.h"
#include "developmentmodecontroller.h"
#include "documentmanager.h"
#include "documentsmodel.h"
......@@ -111,7 +110,6 @@ void registerApplicationTypes()
qmlRegisterUncreatableType<TimelineModel>("org.kde.itinerary", 1, 0, "TimelineModel", {});
qmlRegisterUncreatableType<Transfer>("org.kde.itinerary", 1, 0, "Transfer", {});
qmlRegisterType<CountryModel>("org.kde.itinerary", 1, 0, "CountryModel");
qmlRegisterType<DocumentsModel>("org.kde.itinerary", 1, 0, "DocumentsModel");
qmlRegisterType<LocationInformationDelegateController>("org.kde.itinerary", 1, 0, "LocationInformationDelegateController");
qmlRegisterType<QSortFilterProxyModel>("org.kde.itinerary", 1, 0, "SortFilterProxyModel"); // TODO use this from kitemmodels?
......
......@@ -15,6 +15,7 @@
<file>BusPage.qml</file>
<file>CarRentalDelegate.qml</file>
<file>CarRentalPage.qml</file>
<file>CountryComboBox.qml</file>
<file>DateInput.qml</file>
<file>DateTimeEdit.qml</file>
<file>DepartureQueryPage.qml</file>
......
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