Skip to content

Commit ff60e71

Browse files
committed
Simplified station add logic and allow for removal
1 parent 69d2e07 commit ff60e71

16 files changed

+306
-613
lines changed

README.md

+6-20
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,20 @@ you input the station ID then view the properties that the station sends up.
88

99
## Running
1010

11-
Configure the widget by finding weather stations (ie. [KGADACUL1](https://www.wunderground.com/dashboard/pws/KGADACUL1)). You can get an overview of stations in your area at [`wunderground.com/wundermap`](https://wunderground.com/wundermap) Weather station IDs can be entered manually or there is a search functionality to search by geographic region.
11+
Configure the widget by finding weather stations (ie. [KGADACUL1](https://www.wunderground.com/dashboard/pws/KGADACUL1)). You can get an overview of stations in your area at [`wunderground.com/wundermap`](https://wunderground.com/wundermap).
1212

13-
### Manual Entry
14-
15-
1. Enter the case-sensitive ID into the text field and press `Test Station`. The settings page will query the station to determine if it is reporting valid data.
16-
- If it is valid, then the status bar will report that the station is active and can be used.
17-
- Otherwise, the status bar will report that the station is not active and you must use a different station or check for a spelling error.
18-
2. Then, press the `Select` button at the bottom right to add this station to the list of available stations.
19-
20-
### Geographic Search
21-
22-
1. Enter your city/locality into the top text field and press `Find Station`.
23-
2. The combo box below will populate with localities matching your search. Choose the closest.
24-
3. Further below, the combox box will populate with weather station IDs in that locality. Choose a desired station.
25-
4. Press `Test Station` to determine if the station is reporting valid data. Like above,
26-
- If it is valid, then the status bar will report that the station is active and can be used.
27-
- Otherwise, the status bar will report that the station is not active and you must use a different station or check for a spelling error.
28-
5. Then, press the `Select` button at the bottom right to add this station to the list of available stations.
29-
30-
### Saved Weather Stations
13+
Navigate to the settings page by clicking `Configure Wunderground` or right-clicking the widget. Then, under `Station`, click `Choose...`. This will open up the saved stations dialog. Enter the case-sensitive ID into the field then press `Add` or hit enter.
3114

3215
Once a station has been added to the list, it is automatically selected and will be highlighted. Otherwise, click on the desired station ID then press `Select` at the bottom right.
3316

3417
Then, the station ID will appear on the config page and the settings can be applied to change the station.
3518

3619
Furthermore, some stations update at different rates so you can set the refresh rate.
3720

21+
### Removing Stations
22+
23+
Navigate back to the saved stations dialog and select the station to be removed. Click the `Remove` button at the bottom left and then confirm the removal. You can then close the window or click `Cancel`. If you have removed all of the stations, you can close the window or click `Cancel` as well.
24+
3825
## Translating
3926

4027
Translations welcome!
@@ -72,7 +59,6 @@ Under Appearance in the widget settings, toggle "Show Background."
7259
- Customizability
7360
- [x] Context menu refresh option
7461
- [ ] Widget size/padding/scaling
75-
- [ ] Allow removal of stations from saved stations
7662
- [x] Specific units choice (eg. from km/h to m/s for metric)
7763
- Graphical
7864
- [ ] Improve text coloring of temp and alerts for dark/transparent themes

kpac

+1-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TC:
4747
FG_Purple='\033[35m'
4848
FG_Cyan='\033[36m'
4949
FG_LightGray='\033[37m'
50-
FG_DarkGray='\033[90m'
50+
FG_DarkGray='\033[1m'
5151
FG_LightRed='\033[91m'
5252
FG_LightGreen='\033[92m'
5353
FG_Yellow='\033[93m'
@@ -56,10 +56,6 @@ class TC:
5656
FG_LightCyan='\033[96m'
5757
FG_White='\033[97m'
5858

59-
@staticmethod
60-
def stripColors(str):
61-
return re.sub('\033' + '\[((\d{1,2});)?(\d{1,2})m', '', str)
62-
6359
def echoTC(terminalColor, *args):
6460
print(terminalColor + ' '.join(args) + TC.Reset)
6561
def echoGray(*args):

plasmoid/contents/code/pws-api.js

+2
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,11 @@ function getCurrentData(callback = function() {}) {
429429
weatherData["winddir"] = obs["winddir"];
430430
weatherData["lat"] = obs["lat"];
431431
weatherData["lon"] = obs["lon"];
432+
weatherData["neighborhood"] = obs["neighborhood"];
432433

433434
plasmoid.configuration.latitude = weatherData["lat"];
434435
plasmoid.configuration.longitude = weatherData["lon"];
436+
plasmoid.configuration.stationName = weatherData["neighborhood"];
435437

436438
printDebug("[pws-api.js] Got new current data");
437439

plasmoid/contents/config/main.xml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<entry name="stationID" type="String">
66
<default/>
77
</entry>
8+
<entry name="stationName" type="String">
9+
<default/>
10+
</entry>
811
<entry name="savedStations" type="StringList">
912
<default/>
1013
</entry>

plasmoid/contents/ui/config/ConfigStation.qml

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ KCM.SimpleKCM {
7272
text: plasmoid.configuration.stationID !== "" ? plasmoid.configuration.stationID : "KGADACUL1"
7373
}
7474

75+
PlasmaComponents.Label {
76+
Kirigami.FormData.label: i18n("Weatherstation Name:")
77+
78+
color: plasmoid.configuration.stationName !== "" ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
79+
80+
text: plasmoid.configuration.stationName !== "" ? plasmoid.configuration.stationName : "Hog Mountain"
81+
}
82+
7583
PlasmaComponents.Label {
7684
Kirigami.FormData.label: i18n("Longitude:")
7785

plasmoid/contents/ui/lib/StationPicker.qml

+8-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ RowLayout {
6868
// Reset error
6969
fixedErrorState = false;
7070
}
71+
72+
if (stationListModel.count === 0) {
73+
// selectedStation may have picked up a value; if user removes all stations,
74+
// clear that value.
75+
selectedStation = "";
76+
}
7177
}
7278
}
7379

@@ -83,7 +89,7 @@ RowLayout {
8389
id: selectBtn
8490
Layout.fillWidth: true
8591
icon.name: "find-location"
86-
text: i18ndc("plasma_applet_org.kde.plasma.weather", "@action:button", "Choose…")
87-
onClicked: stationPicker.visible = true
92+
text: i18n("Choose…")
93+
onClicked: stationPicker.open()
8894
}
8995
}

plasmoid/contents/ui/lib/StationPickerDialog.qml

+112-37
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import QtQuick
2121
import QtQuick.Controls
2222
import QtQuick.Layouts
23-
import org.kde.kcmutils as KCM
23+
import QtQuick.Dialogs
2424
import org.kde.plasma.components as PlasmaComponents
2525
import org.kde.kirigami as Kirigami
2626

@@ -55,32 +55,7 @@ Window {
5555
id: syspal
5656
}
5757

58-
StationSearcher {
59-
id: stationSearcher
60-
61-
onChoosen: {
62-
// User searched for a new station. Check for duplicate, add, and make it the current selection.
63-
var isDuplicate = false;
64-
65-
for (let i = 0; i < stationListModel.count; i++) {
66-
if (stationListModel.get(i).name === newStation) {
67-
isDuplicate = true;
68-
}
69-
}
70-
71-
if (!isDuplicate) {
72-
stationListModel.append({"name": newStation});
73-
}
74-
75-
// The duplicate was not added twice, but make still make it the current selection.
76-
for (let i = 0; i < stationListModel.count; i++) {
77-
if (stationListModel.get(i).name === newStation) {
78-
stationListView.currentIndex = i;
79-
}
80-
}
81-
dialog.source = newStation;
82-
}
83-
}
58+
signal open
8459

8560
signal accepted
8661

@@ -89,7 +64,6 @@ Window {
8964
Action {
9065
id: acceptAction
9166

92-
shortcut: "Return"
9367
enabled: stationListModel.count > 0
9468
onTriggered: {
9569
accepted();
@@ -107,6 +81,36 @@ Window {
10781
}
10882
}
10983

84+
MessageDialog {
85+
id: confirmRemoveDialog
86+
87+
text: i18n("Remove Selected")
88+
informativeText: i18n("Confirm Remove %1", stationListModel.get(stationListView.currentIndex).name)
89+
buttons: MessageDialog.Ok | MessageDialog.Cancel
90+
91+
onAccepted: {
92+
var toRemoveName = stationListModel.get(stationListView.currentIndex).name;
93+
var toRemoveIndex = stationListView.currentIndex;
94+
95+
var wasLast = toRemoveIndex === stationListModel.count - 1;
96+
97+
stationListModel.remove(toRemoveIndex);
98+
99+
if (stationListModel.count > 0) {
100+
if (wasLast) {
101+
// If we removed the last element, set selected to new last.
102+
stationListView.currentIndex = stationListModel.count - 1;
103+
dialog.source = stationListModel.get(stationListView.currentIndex).name;
104+
} else {
105+
// If we removed the n'th element and there are remaining elements, set selected to new n'th.
106+
dialog.source = stationListModel.get(stationListView.currentIndex).name;
107+
}
108+
} else {
109+
dialog.source = "";
110+
}
111+
}
112+
}
113+
110114
Component.onCompleted: {
111115
if (plasmoid.configuration.stationID !== "") {
112116
// Transition state where user has stationID set but no savedStations
@@ -142,7 +146,26 @@ Window {
142146
stationListView.currentIndex = i;
143147
}
144148
}
145-
stationListView.forceActiveFocus();
149+
}
150+
151+
onOpen: {
152+
dialog.visible = true;
153+
154+
stationListModel.clear();
155+
156+
// Populate ListModel with saved stations
157+
for (let i = 0; i < plasmoid.configuration.savedStations.length; i++) {
158+
stationListModel.append({"name": plasmoid.configuration.savedStations[i]});
159+
}
160+
161+
// Set selected station to force highlight
162+
for (let i = 0; i < stationListModel.count; i++) {
163+
if (stationListModel.get(i).name === plasmoid.configuration.stationID) {
164+
dialog.source = plasmoid.configuration.stationID;
165+
166+
stationListView.currentIndex = i;
167+
}
168+
}
146169
}
147170

148171
ColumnLayout {
@@ -155,12 +178,53 @@ Window {
155178
Layout.minimumWidth: Math.max(stationPicker.Layout.minimumWidth, buttonsRow.implicitWidth) + 2*anchors.margins
156179
Layout.minimumHeight: stationPicker.Layout.minimumHeight + buttonsRow.implicitHeight + 2*anchors.margins
157180

158-
Button {
159-
id: addMoreBtn
181+
RowLayout {
160182
Layout.fillWidth: true
161-
icon.name: "list-add"
162-
text: i18n("Add more...")
163-
onClicked: stationSearcher.visible = true
183+
184+
Kirigami.SearchField {
185+
id: addField
186+
187+
Layout.fillWidth: true
188+
focus: true
189+
placeholderText: "KGADACUL1"
190+
191+
Keys.onReturnPressed: function(keyEvent) {
192+
if (text.trim().length > 0) {
193+
addBtn.clicked();
194+
keyEvent.accepted = true;
195+
}
196+
}
197+
}
198+
199+
Button {
200+
id: addBtn
201+
202+
text: i18n("Add")
203+
enabled: addField.text.length > 0
204+
icon.name: "list-add"
205+
206+
onClicked: {
207+
var isNew = true;
208+
209+
// Check for duplicate
210+
for (let i = 0; i < stationListModel.count; i++) {
211+
if (stationListModel.get(i).name === addField.text.trim()) {
212+
isNew = false;
213+
}
214+
}
215+
216+
if (isNew) {
217+
stationListModel.append({"name": addField.text.trim()});
218+
219+
dialog.source = addField.text.trim();
220+
221+
stationListView.currentIndex = stationListModel.count - 1;
222+
stationListView.forceActiveFocus();
223+
224+
addField.text = "";
225+
}
226+
}
227+
}
164228
}
165229

166230
ListView {
@@ -196,19 +260,30 @@ Window {
196260
RowLayout {
197261
id: buttonsRow
198262

199-
Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
263+
Layout.alignment: Qt.AlignVCenter
264+
Layout.fillWidth: true
265+
266+
Button {
267+
icon.name: "list-remove"
268+
text: i18n("Remove")
269+
enabled: stationListModel.count > 0
270+
onClicked: {
271+
confirmRemoveDialog.visible = true;
272+
}
273+
}
200274

201275
Button {
276+
Layout.fillWidth: true
202277
icon.name: "dialog-ok"
203-
text: i18ndc("plasma_applet_org.kde.plasma.weather", "@action:button", "Select")
278+
text: i18n("Select")
204279
enabled: stationListModel.count > 0
205280
onClicked: {
206281
acceptAction.trigger();
207282
}
208283
}
209284
Button {
210285
icon.name: "dialog-cancel"
211-
text: i18ndc("plasma_applet_org.kde.plasma.weather", "@action:button", "Cancel")
286+
text: i18n("Cancel")
212287
onClicked: {
213288
cancelAction.trigger();
214289
}

0 commit comments

Comments
 (0)