Commit 1634d5ed authored by Friedrich W. H. Kossebau's avatar Friedrich W. H. Kossebau
Browse files

[weather applet] Config: Use separate dialog for searching a new station

Prepares for having a set of stations which then could be switched
between to select the currently shown one

Having a separate dialog with completely own code also allows to
handle the Enter key as shortcut for triggering the search button.

BUG: 361968
FIXED-IN: 5.13.0
parent ddf9cbf8
......@@ -15,9 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.2
import QtQuick 2.9
import QtQuick.Controls 1.4 as QtControls
import QtQuick.Layouts 1.0
import QtQuick.Layouts 1.3
import org.kde.plasma.components 2.0 as PlasmaComponents
......@@ -27,89 +28,39 @@ import org.kde.plasma.private.weather 1.0
ColumnLayout {
id: weatherStationConfigPage
property alias selectedServices : serviceListModel.selectedServices
property string source
signal configurationChanged
function saveConfig() {
var config = {};
config.services = selectedServices;
// only pick a new source if there is one selected in the locationListView
if (locationListView.rowCount && locationListView.currentRow !== -1) {
config.source = locationListModel.valueForListIndex(locationListView.currentRow);
}
config.updateInterval = updateIntervalSpin.value;
var config = {
services: stationPicker.selectedServices,
source: source,
updateInterval: updateIntervalSpin.value
};
plasmoid.nativeInterface.saveConfig(config);
plasmoid.nativeInterface.configChanged();
}
function searchLocation() {
// avoid automatic selection once model is refilled
locationListView.currentRow = -1;
locationListView.selection.clear();
noSearchResultReport.visible = false;
locationListModel.searchLocations(searchStringEdit.text, selectedServices);
}
function handleLocationSearchDone(success, searchString) {
if (!success) {
noSearchResultReport.text = i18nc("@info", "No weather stations found for '%1'", searchString);
noSearchResultReport.visible = true;
}
}
Component.onCompleted: {
var config = plasmoid.nativeInterface.configValues();
selectedServices = config.services;
stationPicker.selectedServices = config.services;
var source;
var sourceDetails = config.source.split('|');
if (sourceDetails.length > 2) {
source = i18nc("A weather station location and the weather service it comes from",
"%1 (%2)", sourceDetails[2], sourceDetails[0]);
} else {
source = "";
}
locationDisplay.setLocation(source);
source = config.source;
updateIntervalSpin.value = config.updateInterval;
}
LocationListModel {
id: locationListModel
onLocationSearchDone: handleLocationSearchDone(success, searchString);
}
WeatherStationPickerDialog {
id: stationPicker
ServiceListModel {
id: serviceListModel
}
QtControls.Menu {
id: serviceSelectionMenu
Instantiator {
model: serviceListModel
delegate: QtControls.MenuItem {
text: model.display
checkable: true
checked: model.checked
onToggled: {
model.checked = checked;
checked = Qt.binding(function() { return model.checked; });
weatherStationConfigPage.configurationChanged();
}
}
onObjectAdded: serviceSelectionMenu.insertItem(index, object)
onObjectRemoved: serviceSelectionMenu.removeItem(object)
onAccepted: {
weatherStationConfigPage.source = source;
weatherStationConfigPage.configurationChanged();
}
}
GridLayout {
......@@ -122,93 +73,44 @@ ColumnLayout {
text: i18nc("@label", "Location:")
}
QtControls.Label {
id: locationDisplay
Layout.row: 0
Layout.column: 1
Layout.fillWidth: true
elide: Text.ElideRight
function setLocation(location) {
locationDisplay.text = location || "-";
}
}
RowLayout {
Layout.row: 1
Layout.row: 0
Layout.column: 1
Layout.fillWidth: true
QtControls.TextField {
id: searchStringEdit
QtControls.Label {
id: locationDisplay
Layout.fillWidth: true
}
QtControls.Button {
id: serviceSelectionButton
iconName: "services"
tooltip: i18nc("@info:tooltip", "Select weather services providers")
menu: serviceSelectionMenu
}
Item {
Layout.preferredHeight: Math.max(searchButton.height, searchStringEdit.height)
Layout.preferredWidth: Layout.preferredHeight
PlasmaComponents.BusyIndicator {
id: busy
anchors.fill: parent
visible: locationListModel.validatingInput
elide: Text.ElideRight
text: {
var sourceDetails = source.split('|');
if (sourceDetails.length > 2) {
return i18nc("A weather station location and the weather service it comes from",
"%1 (%2)", sourceDetails[2], sourceDetails[0]);
}
return i18nc("no weather station", "-");
}
}
QtControls.Button {
id: searchButton
id: selectButton
iconName: "edit-find"
text: i18nc("@action:button", "Search")
enabled: !!searchStringEdit.text && selectedServices.length
onClicked: searchLocation();
text: i18nc("@action:button", "Select")
onClicked: stationPicker.visible = true;
}
}
QtControls.TableView {
id: locationListView
Layout.row: 2
Layout.column: 1
Layout.fillWidth: true
Layout.fillHeight: true
headerVisible: false
model: locationListModel
onActivated: {
if (row !== -1) {
locationDisplay.setLocation(locationListModel.nameForListIndex(row));
weatherStationConfigPage.configurationChanged();
}
}
QtControls.TableViewColumn {
id: locationListViewStationColumn
movable: false
resizable: false
role: "display"
}
QtControls.Label {
id: noSearchResultReport
anchors.centerIn: parent
visible: false
}
}
QtControls.Label {
Layout.row: 3
Layout.row: 1
Layout.column: 0
Layout.alignment: Qt.AlignRight
text: i18nc("@label:spinbox", "Update every:")
}
QtControls.SpinBox {
id: updateIntervalSpin
Layout.row: 3
Layout.row: 1
Layout.column: 1
Layout.minimumWidth: units.gridUnit * 8
suffix: i18nc("@item:valuesuffix spacing to number + unit (minutes)", " min")
......@@ -218,4 +120,8 @@ ColumnLayout {
onValueChanged: weatherStationConfigPage.configurationChanged();
}
}
Item { // tighten layout
Layout.fillHeight: true
}
}
/*
* Copyright 2016,2018 Friedrich W. H. Kossebau <kossebau@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU 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 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 <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.9
import QtQuick.Controls 1.4 as QtControls
import QtQuick.Layouts 1.3
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.private.weather 1.0
ColumnLayout {
id: root
property alias selectedServices: serviceListModel.selectedServices
property string source
readonly property bool canSearch: !!searchStringEdit.text && selectedServices.length
readonly property bool handlesEnterKey: canSearch && searchStringEdit.activeFocus
function searchLocation() {
if (!canSearch) {
return;
}
// avoid automatic selection once model is refilled
locationListView.currentRow = -1;
locationListView.selection.clear();
noSearchResultReport.visible = false;
source = "";
locationListView.forceActiveFocus();
locationListModel.searchLocations(searchStringEdit.text, selectedServices);
}
function handleLocationSearchDone(success, searchString) {
if (!success) {
noSearchResultReport.text = i18nc("@info", "No weather stations found for '%1'", searchString);
noSearchResultReport.visible = true;
}
}
LocationListModel {
id: locationListModel
onLocationSearchDone: handleLocationSearchDone(success, searchString);
}
ServiceListModel {
id: serviceListModel
}
QtControls.Menu {
id: serviceSelectionMenu
Instantiator {
model: serviceListModel
delegate: QtControls.MenuItem {
text: model.display
checkable: true
checked: model.checked
onToggled: {
model.checked = checked;
checked = Qt.binding(function() { return model.checked; });
weatherStationConfigPage.configurationChanged();
}
}
onObjectAdded: serviceSelectionMenu.insertItem(index, object)
onObjectRemoved: serviceSelectionMenu.removeItem(object)
}
}
ColumnLayout {
RowLayout {
Layout.fillWidth: true
QtControls.TextField {
id: searchStringEdit
Layout.fillWidth: true
Layout.minimumWidth: implicitWidth
placeholderText: i18nc("@info:placeholder", "Enter location")
onAccepted: {
searchLocation();
}
}
QtControls.Button {
id: serviceSelectionButton
iconName: "services"
tooltip: i18nc("@info:tooltip", "Select weather services providers")
menu: serviceSelectionMenu
}
Item {
Layout.preferredHeight: Math.max(searchButton.height, searchStringEdit.height)
Layout.preferredWidth: Layout.preferredHeight
PlasmaComponents.BusyIndicator {
id: busy
anchors.fill: parent
visible: locationListModel.validatingInput
}
}
QtControls.Button {
id: searchButton
iconName: "edit-find"
text: i18nc("@action:button", "Search")
enabled: canSearch
onClicked: {
searchLocation();
}
}
}
QtControls.TableView {
id: locationListView
Layout.minimumWidth: implicitWidth
Layout.minimumHeight: implicitHeight
Layout.fillWidth: true
Layout.fillHeight: true
headerVisible: false
model: locationListModel
onActivated: {
if (row !== -1 && rowCount) {
source = locationListModel.valueForListIndex(row);
}
}
QtControls.TableViewColumn {
id: locationListViewStationColumn
movable: false
resizable: false
role: "display"
}
QtControls.Label {
id: noSearchResultReport
anchors.centerIn: parent
visible: false
}
}
}
Component.onCompleted: {
searchStringEdit.forceActiveFocus();
}
}
/*
* Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU 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 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 <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4 as QtControls
import QtQuick.Layouts 1.3
import org.kde.plasma.core 2.0 as PlasmaCore
Window {
id: dialog
LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
LayoutMirroring.childrenInherit: true
flags: Qt.Dialog
modality: Qt.WindowModal
minimumWidth: mainColumn.Layout.minimumWidth
minimumHeight: mainColumn.Layout.minimumHeight
title: i18nc("@title:window", "Select Weather Station")
color: syspal.window
property alias selectedServices : stationPicker.selectedServices
property alias source: stationPicker.source
signal accepted
function close() {
dialog.visible = false;
}
SystemPalette {
id: syspal
}
QtControls.Action {
id: acceptAction
shortcut: "Return"
enabled: !!source && !stationPicker.handlesEnterKey
onTriggered: {
accepted();
dialog.close();
}
}
QtControls.Action {
id: cancelAction
shortcut: "Escape"
onTriggered: {
dialog.close();
}
}
ColumnLayout {
id: mainColumn
anchors {
fill: parent
margins: mainColumn.spacing * units.devicePixelRatio //margins are hardcoded in QStyle we should match that here
}
// TODO: not yet perfect
Layout.minimumWidth: Math.max(stationPicker.Layout.minimumWidth, buttonsRow.implicitWidth) + 2*anchors.margins
Layout.minimumHeight: stationPicker.Layout.minimumHeight + buttonsRow.implicitHeight + 2*anchors.margins
WeatherStationPicker {
id: stationPicker
Layout.fillWidth: true
Layout.fillHeight: true
}
RowLayout {
id: buttonsRow
Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
QtControls.Button {
enabled: !!source
iconName: "dialog-ok"
text: i18nc("@action:button", "Select")
onClicked: {
acceptAction.trigger();
}
}
QtControls.Button {
iconName: "dialog-cancel"
text: i18nc("@action:button", "Cancel")
onClicked: {
cancelAction.trigger();
}
}
}
}
}
Supports Markdown
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