Warning, /utilities/kweather/src/qml/locationslist/AddLocationListView.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2020-2022 Devin Lin <espidev@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 import QtQuick
0009 import QtQuick.Controls
0010 import QtQuick.Layouts
0011 import org.kde.kirigami as Kirigami
0012 
0013 import org.kde.kweather
0014 
0015 import org.kde.kweather.components
0016 
0017 ListView {
0018     id: root
0019     
0020     property string searchQuery: ""
0021     
0022     model: LocationQueryModel {}
0023     
0024     signal closeRequested()
0025     signal focusRequested()
0026     
0027     header: Control {
0028         width: root.width
0029         leftPadding: Kirigami.Units.largeSpacing
0030         rightPadding: Kirigami.Units.largeSpacing
0031         topPadding: Kirigami.Units.largeSpacing
0032         bottomPadding: Kirigami.Units.largeSpacing 
0033         
0034         contentItem: RowLayout {
0035             spacing: Kirigami.Units.smallSpacing
0036             
0037             Kirigami.SearchField {
0038                 id: search
0039                 Layout.fillWidth: true
0040                 
0041                 Connections {
0042                     target: root
0043                     function onFocusRequested() {
0044                         search.forceActiveFocus();
0045                     }
0046                 }
0047 
0048                 placeholderText: i18n("Search for a location…")
0049                 onTextChanged: {
0050                     root.searchQuery = text;
0051                     root.model.textChanged(text);
0052                     root.currentIndex = 0;
0053                 }
0054                 onEditingFinished: {
0055                     if (root.searchQuery === text) {
0056                         // no change
0057                         return;
0058                     }
0059                     root.searchQuery = text;
0060                     root.model.textChanged(text, 0); // when return is pressed, query immediately
0061                     root.currentIndex = 0;
0062                 }
0063                 KeyNavigation.down: root
0064             }
0065             
0066             Button {
0067                 id: searchButton
0068                 icon.name: "search"
0069                 implicitHeight: search.implicitHeight
0070                 implicitWidth: height
0071                 enabled: search.text !== ""
0072                 width: height
0073                 height: search.height
0074                 onClicked: root.model.textChanged(root.searchQuery, 0)
0075             }
0076         }
0077     }
0078 
0079     // unable to connect message
0080     Kirigami.PlaceholderMessage {
0081         anchors.centerIn: parent
0082         anchors.left: parent.left
0083         anchors.right: parent.right
0084         anchors.margins: Kirigami.Units.largeSpacing
0085 
0086         icon.name: "network-disconnect"
0087         text: i18n("Unable to connect")
0088         visible: root.model.networkError
0089     }
0090 
0091     // default message (has not searched yet)
0092     Kirigami.PlaceholderMessage {
0093         anchors.centerIn: parent
0094         anchors.left: parent.left
0095         anchors.right: parent.right
0096         anchors.margins: Kirigami.Units.largeSpacing
0097 
0098         icon.name: "search"
0099         text: i18n("Search for a location")
0100         visible: !root.model.networkError && !root.model.loading && root.count == 0 && root.searchQuery == ""
0101 
0102         helpfulAction: Kirigami.Action {
0103             icon.name: "mark-location"
0104             text: i18n("Add current location")
0105             onTriggered: {
0106                 WeatherLocationListModel.requestCurrentLocation()
0107                 
0108                 root.closeRequested();
0109                 
0110                 let page = getPage("Forecast");
0111                 switchToPage(page, 0);
0112                 if (page.loading !== undefined) {
0113                     getPage("Forecast").loading = true;
0114                 }
0115             }
0116         }
0117     }
0118 
0119     // no results
0120     Kirigami.PlaceholderMessage {
0121         anchors.centerIn: parent
0122         anchors.left: parent.left
0123         anchors.right: parent.right
0124         anchors.margins: Kirigami.Units.largeSpacing
0125 
0126         icon.name: "search"
0127         text: i18n("No results")
0128         visible: !root.model.networkError && !root.model.loading && root.count == 0 && root.searchQuery != ""
0129     }
0130 
0131     // loading results indicator
0132     BusyIndicator {
0133         anchors.centerIn: parent
0134         running: root.model.loading && root.count === 0
0135         Layout.minimumWidth: Kirigami.Units.iconSizes.huge
0136         Layout.minimumHeight: width
0137     }
0138 
0139     delegate: ListDelegate {
0140         width: root.width
0141         showSeparator: model.index != root.count - 1
0142         
0143         leftPadding: Kirigami.Units.largeSpacing
0144         rightPadding: Kirigami.Units.largeSpacing
0145         topPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing * 2
0146         bottomPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing * 2
0147         
0148         function apply() {
0149             root.model.addLocation(index);
0150             applicationWindow().getPage("Forecast").switchPageIndex(WeatherLocationListModel.count - 1);
0151             switchToPage(appwindow.getPage("Forecast"), 0);
0152             root.closeRequested();
0153         }
0154         
0155         onClicked: apply()
0156         Keys.onPressed: event => {
0157             if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) {
0158                 apply();
0159                 event.accepted = true;
0160             } else {
0161                 event.accepted = false;
0162             }
0163         }
0164         
0165         contentItem: RowLayout {
0166             spacing: Kirigami.Units.largeSpacing
0167             
0168             Label {
0169                 Layout.fillWidth: true
0170                 Layout.alignment: Qt.AlignVCenter
0171                 text: model.name
0172             }
0173         }
0174     }
0175 }