Warning, /pim/itinerary/src/app/StopPickerPage.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 import QtQuick
0007 import QtQuick.Layouts
0008 import QtQuick.Controls as QQC2
0009 import org.kde.kirigami as Kirigami
0010 import org.kde.kitemmodels
0011 import org.kde.i18n.localeData
0012 import org.kde.kpublictransport
0013 import org.kde.itinerary
0014 
0015 Kirigami.ScrollablePage {
0016     id: root
0017     property var publicTransportManager
0018     /**
0019      * Initially selected country.
0020      * If not specified the country from the current locale is used.
0021      */
0022     property string initialCountry: Qt.locale().name.match(/_([A-Z]{2})/)[1]
0023     property var location
0024 
0025     QQC2.ActionGroup { id: sortActionGroup }
0026     actions: [
0027         Kirigami.Action {
0028             text: i18n("Clear history")
0029             icon.name: "edit-clear-history"
0030             onTriggered: locationHistoryModel.clear()
0031         },
0032         Kirigami.Action { separator: true },
0033 
0034         Kirigami.Action {
0035             QQC2.ActionGroup.group: sortActionGroup
0036             checkable: true
0037             checked: historySortModel.sortRoleName == "locationName"
0038             text: i18n("Sort by name")
0039             onTriggered: historySortModel.sortRoleName = "locationName"
0040         },
0041         Kirigami.Action {
0042             QQC2.ActionGroup.group: sortActionGroup
0043             checkable: true
0044             checked: historySortModel.sortRoleName == "lastUsed"
0045             text: i18n("Most recently used")
0046             onTriggered: historySortModel.sortRoleName = "lastUsed"
0047         },
0048         Kirigami.Action {
0049             QQC2.ActionGroup.group: sortActionGroup
0050             checkable: true
0051             checked: historySortModel.sortRoleName == "useCount"
0052             text: i18n("Most often used")
0053             onTriggered: historySortModel.sortRoleName = "useCount"
0054         }
0055     ]
0056     header: ColumnLayout {
0057         spacing: Kirigami.Units.smallSpacing
0058         CountryComboBox {
0059             id: countryCombo
0060             Layout.topMargin: Kirigami.Units.smallSpacing
0061             Layout.leftMargin: Kirigami.Units.smallSpacing
0062             Layout.rightMargin: Kirigami.Units.smallSpacing
0063             Layout.fillWidth: true
0064             model: {
0065                 var countries = new Array();
0066                 for (const b of publicTransportManager.backends) {
0067                     if (!publicTransportManager.isBackendEnabled(b.identifier)) {
0068                         continue;
0069                     }
0070                     for (const t of [CoverageArea.Realtime, CoverageArea.Regular, CoverageArea.Any]) {
0071                         for (const c of b.coverageArea(t).regions) {
0072                             if (c != 'UN' && c != 'EU') {
0073                                 countries.push(c.substr(0, 2));
0074                             }
0075                         }
0076                     }
0077                 }
0078                 return [...new Set(countries)];
0079             }
0080             initialCountry: root.initialCountry
0081             onCurrentValueChanged: {
0082                 var loc = locationQueryModel.request.location;
0083                 loc.country = countryCombo.currentValue;
0084                 locationQueryModel.request.location = loc;
0085                 locationQueryModel.request.type = Location.Stop
0086             }
0087         }
0088         Kirigami.SearchField {
0089             id: queryTextField
0090             Layout.leftMargin: Kirigami.Units.smallSpacing
0091             Layout.rightMargin: Kirigami.Units.smallSpacing
0092             Layout.bottomMargin: Kirigami.Units.smallSpacing
0093             Layout.fillWidth: true
0094             onAccepted: {
0095                 if (text !== "" && countryCombo.currentValue !== "") {
0096                     var loc = locationQueryModel.request.location;
0097                     loc.name = text;
0098                     loc.country = countryCombo.currentValue;
0099                     locationQueryModel.request.location = loc;
0100                     locationQueryModel.request.type = Location.Stop
0101                 }
0102             }
0103         }
0104     }
0105 
0106     LocationQueryModel {
0107         id: locationQueryModel
0108         manager: publicTransportManager
0109         queryDelay: 500
0110     }
0111     LocationHistoryModel {
0112         id: locationHistoryModel
0113     }
0114     KSortFilterProxyModel {
0115         id: historySortModel
0116         sourceModel: locationHistoryModel
0117         sortRoleName: Settings.read("StopPicker/historySortMode", "lastUsed")
0118         onSortRoleChanged: Settings.write("StopPicker/historySortMode", sortRoleName)
0119         sortOrder: sortRoleName == "locationName" ? Qt.AscendingOrder : Qt.DescendingOrder
0120     }
0121 
0122     Component {
0123         id: historyDelegate
0124         Kirigami.SwipeListItem {
0125             id: delegate
0126             readonly property var sourceModel: ListView.view.model
0127             text: model.location.name
0128             contentItem: QQC2.Label {
0129                 text: model.location.name
0130                 elide: Text.ElideRight
0131                 Accessible.ignored: true
0132             }
0133             actions: [
0134                 Kirigami.Action {
0135                     icon.name: "edit-delete"
0136                     text: i18n("Remove history entry")
0137                     onTriggered: {
0138                         sourceModel.removeRows(model.index, 1)
0139                     }
0140                 }
0141             ]
0142             onClicked: {
0143                 root.location = model.location;
0144                 locationHistoryModel.addLocation(model.location);
0145                 applicationWindow().pageStack.goBack();
0146             }
0147             Accessible.onPressAction: delegate.clicked()
0148         }
0149     }
0150 
0151     Component {
0152         id: queryResultDelegate
0153         QQC2.ItemDelegate {
0154             id: delegate
0155             text: model.location.name
0156             width: ListView.view.width
0157             contentItem: Kirigami.TitleSubtitle {
0158                 title: model.location.name
0159             }
0160             onClicked: {
0161                 root.location = model.location
0162                 locationHistoryModel.addLocation(model.location);
0163                 applicationWindow().pageStack.goBack();
0164                 queryTextField.clear();
0165             }
0166             Accessible.onPressAction: delegate.clicked()
0167         }
0168     }
0169 
0170     ListView {
0171         id: locationView
0172         model: queryTextField.text === "" ? historySortModel : locationQueryModel
0173         delegate: queryTextField.text === "" ? historyDelegate : queryResultDelegate
0174 
0175         QQC2.BusyIndicator {
0176             anchors.centerIn: parent
0177             running: locationQueryModel.loading
0178         }
0179 
0180         QQC2.Label {
0181             anchors.centerIn: parent
0182             width: parent.width
0183             text: locationQueryModel.errorMessage
0184             color: Kirigami.Theme.negativeTextColor
0185             wrapMode: Text.Wrap
0186         }
0187 
0188         Kirigami.PlaceholderMessage {
0189             text: i18n("No locations found")
0190             visible: locationView.count === 0 && !locationQueryModel.loading && queryTextField !== ""
0191             anchors.centerIn: parent
0192         }
0193     }
0194 }