Add and delete works now!

This commit is contained in:
2022-04-01 16:08:02 -07:00
parent 58b32a6cb3
commit a5ef038823
2 changed files with 106 additions and 26 deletions

View File

@@ -37,23 +37,53 @@ Kirigami.ApplicationWindow {
contentItem: Item { contentItem: Item {
implicitWidth: delegateLayout.implicitWidth implicitWidth: delegateLayout.implicitWidth
implicitHeight: delegateLayout.implicitHeight implicitHeight: delegateLayout.implicitHeight
RowLayout { ColumnLayout {
id: delegateLayout id: delegateLayout
anchors { anchors {
left: parent.left left: parent.left
top: parent.top top: parent.top
right: parent.right right: parent.right
} }
Kirigami.Heading { RowLayout {
text: flightCategory Layout.fillWidth: true
} Kirigami.Heading {
Controls.Label { text: stationId
text: rawMetar }
Kirigami.Heading {
text: flightCategory
// Dynamically set the color based on the flight category
Component.onCompleted: {
color = Qt.binding(function() {
if (flightCategory == "VFR") {
return "green";
} else if (flightCategory == "MVFR") {
return "blue";
} else if (flightCategory == "IFR") {
return "red";
} else if (flightCategory == "LIFR") {
return "fuchsia";
} else {
return "white";
}
});
}
}
Controls.Button {
Layout.alignment: Qt.AlignRight
icon.name: "edit-delete"
onClicked: logic.deleteAirport(stationId)
}
}
Controls.Label {
text: rawMetar
Layout.fillWidth: true
wrapMode: Text.WordWrap
}
} }
} }
} }
} }
}
Kirigami.OverlaySheet { Kirigami.OverlaySheet {
id: addAirport id: addAirport

View File

@@ -1,4 +1,4 @@
import std/[httpclient, times, xmlparser, xmltree] import std/[httpclient, os, sequtils, sugar, strutils, xmlparser, xmltree]
import Tables import Tables
import strformat import strformat
import NimQml import NimQml
@@ -58,6 +58,38 @@ proc newMetarData*(xmlData: XMLNode): MetarData =
type AirportRoles {.pure.} = enum type AirportRoles {.pure.} = enum
RawMetar = UserRole + 1 RawMetar = UserRole + 1
FlightCategory = UserRole + 2 FlightCategory = UserRole + 2
StationId = UserRole + 3
proc readConfig(): seq[string] =
let metarConfigDir = getConfigDir() & "/metarweather"
let metarConf = metarConfigDir & "/metarweather.conf"
if not dirExists(metarConfigDir):
createDir(metarConfigDir)
if not fileExists(metarConf):
return @[]
let config = readFile(metarConf)
if config.isEmptyOrWhiteSpace:
return @[]
return config.strip().split(",")
proc writeConfig(airports: seq[string]) =
let config = airports.join(",")
let metarConfigDir = getConfigDir() & "/metarweather"
let metarConf = metarConfigDir & "/metarweather.conf"
writeFile(metarConf, config)
proc getMetars(airports: seq[string]): seq[MetarData] =
let client = newHttpClient()
airports.map(airportCode => client.getMetar(airportCode)).map(metarXml => newMetarData(metarXml))
QtObject: QtObject:
type MetarList* = ref object of QAbstractListModel type MetarList* = ref object of QAbstractListModel
@@ -87,10 +119,12 @@ QtObject:
case airportRole: case airportRole:
of AirportRoles.RawMetar: result = newQVariant(airport.rawText) of AirportRoles.RawMetar: result = newQVariant(airport.rawText)
of AirportRoles.FlightCategory: result = newQVariant(airport.flightCategory) of AirportRoles.FlightCategory: result = newQVariant(airport.flightCategory)
of AirportRoles.StationId: result = newQVariant(airport.stationId)
method roleNames(self: MetarList): Table[int, string] = method roleNames(self: MetarList): Table[int, string] =
{ AirportRoles.RawMetar.int:"rawMetar", { AirportRoles.RawMetar.int:"rawMetar",
AirportRoles.FlightCategory.int:"flightCategory"}.toTable AirportRoles.FlightCategory.int:"flightCategory",
AirportRoles.StationId.int:"stationId"}.toTable
QtObject: QtObject:
type ApplicationLogic* = ref object of QObject type ApplicationLogic* = ref object of QObject
@@ -104,6 +138,11 @@ QtObject:
proc setup(self: ApplicationLogic) = proc setup(self: ApplicationLogic) =
self.QObject.setup self.QObject.setup
proc getMetarList(self: ApplicationLogic): QVariant {.slot.} =
return newQVariant(self.metarList)
proc metarListChanged(self: ApplicationLogic, metarList: QVariant) {.signal.}
proc newApplicationLogic*(app: QApplication, airports: seq[MetarData]): ApplicationLogic = proc newApplicationLogic*(app: QApplication, airports: seq[MetarData]): ApplicationLogic =
new(result, delete) new(result, delete)
result.app = app result.app = app
@@ -111,19 +150,34 @@ QtObject:
result.setup() result.setup()
proc refresh(self: ApplicationLogic) {.slot.} = proc refresh(self: ApplicationLogic) {.slot.} =
echo "Refresh called" let airports = self.metarList.airports.map(metar => metar.stationId)
self.metarList.delete
self.metarList = newMetarList(airports.getMetars())
self.metarListChanged(newQVariant(self.metarList))
proc addAirport(self: ApplicationLogic, code: string) {.slot.} = proc addAirport(self: ApplicationLogic, code: string) {.slot.} =
echo &"Add airport called {code}" let airports = self.metarList.airports.map(metar => metar.stationId) & code
writeConfig(airports)
self.metarList.delete
self.metarList = newMetarList(airports.getMetars())
self.metarListChanged(newQVariant(self.metarList))
proc deleteAirport(self: ApplicationLogic, code: string) {.slot.} = proc deleteAirport(self: ApplicationLogic, code: string) {.slot.} =
echo &"Delete {code}" let airports = self.metarList.airports.map(metar => metar.stationId)
.filter(stationId => stationId != code)
proc getMetarList(self: ApplicationLogic): QVariant {.slot.} = writeConfig(airports)
return newQVariant(self.metarList)
self.metarList.delete
self.metarList = newMetarList(airports.getMetars())
self.metarListChanged(newQVariant(self.metarList))
QtProperty[QVariant] metarList: QtProperty[QVariant] metarList:
read = getMetarList read = getMetarList
notify = metarListChanged
proc mainProc() = proc mainProc() =
let app = newQApplication() let app = newQApplication()
@@ -132,12 +186,8 @@ proc mainProc() =
let engine = newQQmlApplicationEngine() let engine = newQQmlApplicationEngine()
defer: engine.delete() defer: engine.delete()
let client = newHttpClient() let airports = readConfig()
let metarData = getMetars(airports)
let metarData = @[
newMetarData(client.getMetar("KOAK")),
newMetarData(client.getMetar("KHWD")),
]
let logic = newApplicationLogic(app, metarData) let logic = newApplicationLogic(app, metarData)
let logicVariant = newQVariant(logic) let logicVariant = newQVariant(logic)