diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b375771 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +metar diff --git a/main.qml b/main.qml index f40bc03..5f2d2a9 100644 --- a/main.qml +++ b/main.qml @@ -23,7 +23,38 @@ Kirigami.ApplicationWindow { onTriggered: addAirport.open() } } + Kirigami.CardsListView { + anchors.fill: parent + id: cardLayout + model: logic.metarList + delegate: metarListDelegate + } } + + Component { + id: metarListDelegate + Kirigami.AbstractCard { + contentItem: Item { + implicitWidth: delegateLayout.implicitWidth + implicitHeight: delegateLayout.implicitHeight + RowLayout { + id: delegateLayout + anchors { + left: parent.left + top: parent.top + right: parent.right + } + Kirigami.Heading { + text: flightCategory + } + Controls.Label { + text: rawMetar + } + } + } + } + } + Kirigami.OverlaySheet { id: addAirport diff --git a/metar b/metar deleted file mode 100755 index 5f674fa..0000000 Binary files a/metar and /dev/null differ diff --git a/metar.nim b/metar.nim index 91e22ed..9c348c8 100644 --- a/metar.nim +++ b/metar.nim @@ -1,4 +1,5 @@ import std/[httpclient, times, xmlparser, xmltree] +import Tables import strformat import NimQml import macros @@ -10,7 +11,11 @@ proc getMetar(client: HttpClient, code: string): XMLNode = parseXml(metarData) proc childString(node: XMLnode, name: string): string = - node.child(name).innerText + let node = node.child(name) + if node != nil: + result = node.innerText + else: + result = "" type SkyCondition = object skyCover: string @@ -29,7 +34,7 @@ type MetarData = object flightCategory: string skyConditions: seq[SkyCondition] -proc fromXml*(xmlData: XMLNode): MetarData = +proc newMetarData*(xmlData: XMLNode): MetarData = let metar = xmlData.child("data").child("METAR") result = MetarData( @@ -39,7 +44,7 @@ proc fromXml*(xmlData: XMLNode): MetarData = temperature: metar.childString("temp_c"), dewPoint: metar.childString("dewpoint_c"), windHeading: metar.childString("wind_dir_degrees"), - visibility: metar.childString("wisibility_statute_mi"), + visibility: metar.childString("visibility_statute_mi"), altimiter: metar.childString("altim_in_hg"), flightCategory: metar.childString("flight_category") ) @@ -50,23 +55,61 @@ proc fromXml*(xmlData: XMLNode): MetarData = skyCover: skyConditionXml.attr("sky_cover"), cloudBaseAgl: skyconditionXml.attr("cloud_base_ft_agl"))) +type AirportRoles {.pure.} = enum + RawMetar = UserRole + 1 + FlightCategory = UserRole + 2 + +QtObject: + type MetarList* = ref object of QAbstractListModel + airports*: seq[MetarData] + + proc delete(self: MetarList) = + self.QAbstractListModel.delete + + proc setup(self: MetarList) = + self.QAbstractListModel.setup + + proc newMetarList*(airports: seq[MetarData]): MetarList = + new(result, delete) + result.airports = airports + result.setup + + method rowCount(self: MetarList, index: QModelIndex = nil): int = + self.airports.len + + method data(self: MetarList, index: QModelIndex, role: int): QVariant = + if not index.isValid: + return + if index.row < 0 or index.row >= self.airports.len: + return + let airport = self.airports[index.row] + let airportRole = role.AirportRoles + case airportRole: + of AirportRoles.RawMetar: result = newQVariant(airport.rawText) + of AirportRoles.FlightCategory: result = newQVariant(airport.flightCategory) + + method roleNames(self: MetarList): Table[int, string] = + { AirportRoles.RawMetar.int:"rawMetar", + AirportRoles.FlightCategory.int:"flightCategory"}.toTable + QtObject: type ApplicationLogic* = ref object of QObject app: QApplication + metarList: MetarList proc delete*(self: ApplicationLogic) = self.QObject.delete + self.metarList.delete proc setup(self: ApplicationLogic) = self.QObject.setup - proc newApplicationLogic*(app: QApplication): ApplicationLogic = - new(result) + proc newApplicationLogic*(app: QApplication, airports: seq[MetarData]): ApplicationLogic = + new(result, delete) result.app = app + result.metarList = newMetarList(airports) result.setup() - ## Real methods below here - proc refresh(self: ApplicationLogic) {.slot.} = echo "Refresh called" @@ -76,6 +119,12 @@ QtObject: proc deleteAirport(self: ApplicationLogic, code: string) {.slot.} = echo &"Delete {code}" + proc getMetarList(self: ApplicationLogic): QVariant {.slot.} = + return newQVariant(self.metarList) + + QtProperty[QVariant] metarList: + read = getMetarList + proc mainProc() = let app = newQApplication() defer: app.delete() @@ -83,7 +132,14 @@ proc mainProc() = let engine = newQQmlApplicationEngine() defer: engine.delete() - let logic = newApplicationLogic(app) + let client = newHttpClient() + + let metarData = @[ + newMetarData(client.getMetar("KOAK")), + newMetarData(client.getMetar("KHWD")), + ] + + let logic = newApplicationLogic(app, metarData) let logicVariant = newQVariant(logic) engine.setRootContextProperty("logic", logicVariant)