Warning, /libraries/kpublictransport/tests/locationpicker.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 import QtQuick
0008 import QtQuick.Layouts
0009 import QtQuick.Controls as QQC2
0010 import org.kde.kirigami as Kirigami
0011 import org.kde.kirigami.delegates as Kirigami
0012 import org.kde.kitemmodels
0013 import org.kde.i18n.localeData
0014 import org.kde.kpublictransport
0015 
0016 Kirigami.ApplicationWindow {
0017     title: "Location Picker Example"
0018 
0019     width: 540
0020     height: 720
0021 
0022     pageStack.initialPage: locationPickerPage
0023 
0024     Manager {
0025         id: ptMgr
0026     }
0027 
0028     Component {
0029         id: locationPickerPage
0030         Kirigami.ScrollablePage {
0031             actions: [
0032                 Kirigami.Action {
0033                     text: "Remove All"
0034                     onTriggered: locationHistoryModel.clear()
0035                 }
0036             ]
0037             header: ColumnLayout {
0038                 QQC2.ComboBox {
0039                     id: countryCombo
0040                     model: {
0041                         var countries = new Array();
0042                         for (const b of ptMgr.backends) {
0043                             for (const t of [CoverageArea.Realtime, CoverageArea.Regular, CoverageArea.Any]) {
0044                                 for (const c of b.coverageArea(t).regions) {
0045                                     if (c != 'UN' && c != 'EU') {
0046                                         countries.push(c.substr(0, 2));
0047                                     }
0048                                 }
0049                             }
0050                         }
0051                         return [...new Set(countries)].sort();
0052                     }
0053                     Layout.fillWidth: true
0054                     readonly property var currentCountry: Country.fromAlpha2(currentValue)
0055                     displayText: currentCountry.emojiFlag + ' ' + currentCountry.name
0056                     delegate: QQC2.ItemDelegate {
0057                         text: {
0058                             const c = Country.fromAlpha2(modelData);
0059                             return c.emojiFlag + ' ' + c.name;
0060                         }
0061                         width: parent ? parent.width : undefined
0062                     }
0063                     Component.onCompleted: {
0064                         countryCombo.currentIndex = countryCombo.indexOfValue(Qt.locale().name.match(/_([A-Z]{2})/)[1])
0065                     }
0066                 }
0067                 Kirigami.SearchField {
0068                     id: queryTextField
0069                     Layout.fillWidth: true
0070                     onAccepted: {
0071                         if (text !== "") {
0072                             var loc = locationQueryModel.request.location;
0073                             loc.name = text;
0074                             loc.country = countryCombo.currentValue;
0075                             locationQueryModel.request.location = loc;
0076                             locationQueryModel.request.type = Location.Stop
0077                         }
0078                     }
0079                 }
0080                 QQC2.ButtonGroup { buttons: sortGroup.children }
0081                 RowLayout {
0082                     id: sortGroup
0083                     QQC2.RadioButton {
0084                         text: "Name"
0085                         onCheckedChanged: {
0086                             historySortModel.sortRoleName = "locationName";
0087                             historySortModel.sortOrder = Qt.AscendingOrder;
0088                         }
0089                     }
0090                     QQC2.RadioButton {
0091                         checked: true
0092                         text: "Most Recent"
0093                         onCheckedChanged: {
0094                             historySortModel.sortRoleName = "lastUsed";
0095                             historySortModel.sortOrder = Qt.DescendingOrder;
0096                         }
0097                     }
0098                     QQC2.RadioButton {
0099                         text: "Most Often"
0100                         onCheckedChanged: {
0101                             historySortModel.sortRoleName = "useCount";
0102                             historySortModel.sortOrder = Qt.DescendingOrder;
0103                         }
0104                     }
0105                 }
0106             }
0107 
0108             LocationQueryModel {
0109                 id: locationQueryModel
0110                 manager: ptMgr
0111                 queryDelay: 500
0112             }
0113             LocationHistoryModel {
0114                 id: locationHistoryModel
0115             }
0116             KSortFilterProxyModel {
0117                 id: historySortModel
0118                 sourceModel: locationHistoryModel
0119                 sortRoleName: "lastUsed"
0120                 sortOrder: Qt.DescendingOrder
0121             }
0122 
0123             Component {
0124                 id: historyDelegate
0125                 Kirigami.SwipeListItem {
0126                     readonly property var sourceModel: ListView.view.model
0127                     contentItem: QQC2.Label {
0128                         text: model.location.name
0129                     }
0130                     actions: [
0131                         Kirigami.Action {
0132                             icon.name: "edit-delete"
0133                             text: "Remove history entry"
0134                             onTriggered: {
0135                                 sourceModel.removeRows(model.index, 1)
0136                             }
0137                         }
0138                     ]
0139                     onClicked: {
0140                         locationHistoryModel.addLocation(model.location);
0141                     }
0142                 }
0143             }
0144 
0145             Component {
0146                 id: queryResultDelegate
0147                 QQC2.ItemDelegate {
0148                     width: ListView.view.width
0149                     contentItem: Kirigami.TitleSubtitle {
0150                         title: model.location.name
0151                     }
0152                     onClicked: {
0153                         locationHistoryModel.addLocation(model.location);
0154                         queryTextField.text = "";
0155                     }
0156                 }
0157             }
0158 
0159             ListView {
0160                 id: historyView
0161                 model: queryTextField.text === "" ? historySortModel : locationQueryModel
0162                 delegate: queryTextField.text === "" ? historyDelegate : queryResultDelegate
0163 
0164                 QQC2.BusyIndicator {
0165                     anchors.centerIn: parent
0166                     running: locationQueryModel.loading
0167                 }
0168 
0169                 QQC2.Label {
0170                     anchors.centerIn: parent
0171                     width: parent.width
0172                     text: locationQueryModel.errorMessage
0173                     color: Kirigami.Theme.negativeTextColor
0174                     wrapMode: Text.Wrap
0175                 }
0176             }
0177         }
0178     }
0179 }