List rendering finally works!
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
metar
|
||||||
31
main.qml
31
main.qml
@@ -23,7 +23,38 @@ Kirigami.ApplicationWindow {
|
|||||||
onTriggered: addAirport.open()
|
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 {
|
Kirigami.OverlaySheet {
|
||||||
id: addAirport
|
id: addAirport
|
||||||
|
|
||||||
|
|||||||
72
metar.nim
72
metar.nim
@@ -1,4 +1,5 @@
|
|||||||
import std/[httpclient, times, xmlparser, xmltree]
|
import std/[httpclient, times, xmlparser, xmltree]
|
||||||
|
import Tables
|
||||||
import strformat
|
import strformat
|
||||||
import NimQml
|
import NimQml
|
||||||
import macros
|
import macros
|
||||||
@@ -10,7 +11,11 @@ proc getMetar(client: HttpClient, code: string): XMLNode =
|
|||||||
parseXml(metarData)
|
parseXml(metarData)
|
||||||
|
|
||||||
proc childString(node: XMLnode, name: string): string =
|
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
|
type SkyCondition = object
|
||||||
skyCover: string
|
skyCover: string
|
||||||
@@ -29,7 +34,7 @@ type MetarData = object
|
|||||||
flightCategory: string
|
flightCategory: string
|
||||||
skyConditions: seq[SkyCondition]
|
skyConditions: seq[SkyCondition]
|
||||||
|
|
||||||
proc fromXml*(xmlData: XMLNode): MetarData =
|
proc newMetarData*(xmlData: XMLNode): MetarData =
|
||||||
let metar = xmlData.child("data").child("METAR")
|
let metar = xmlData.child("data").child("METAR")
|
||||||
|
|
||||||
result = MetarData(
|
result = MetarData(
|
||||||
@@ -39,7 +44,7 @@ proc fromXml*(xmlData: XMLNode): MetarData =
|
|||||||
temperature: metar.childString("temp_c"),
|
temperature: metar.childString("temp_c"),
|
||||||
dewPoint: metar.childString("dewpoint_c"),
|
dewPoint: metar.childString("dewpoint_c"),
|
||||||
windHeading: metar.childString("wind_dir_degrees"),
|
windHeading: metar.childString("wind_dir_degrees"),
|
||||||
visibility: metar.childString("wisibility_statute_mi"),
|
visibility: metar.childString("visibility_statute_mi"),
|
||||||
altimiter: metar.childString("altim_in_hg"),
|
altimiter: metar.childString("altim_in_hg"),
|
||||||
flightCategory: metar.childString("flight_category")
|
flightCategory: metar.childString("flight_category")
|
||||||
)
|
)
|
||||||
@@ -50,23 +55,61 @@ proc fromXml*(xmlData: XMLNode): MetarData =
|
|||||||
skyCover: skyConditionXml.attr("sky_cover"),
|
skyCover: skyConditionXml.attr("sky_cover"),
|
||||||
cloudBaseAgl: skyconditionXml.attr("cloud_base_ft_agl")))
|
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:
|
QtObject:
|
||||||
type ApplicationLogic* = ref object of QObject
|
type ApplicationLogic* = ref object of QObject
|
||||||
app: QApplication
|
app: QApplication
|
||||||
|
metarList: MetarList
|
||||||
|
|
||||||
proc delete*(self: ApplicationLogic) =
|
proc delete*(self: ApplicationLogic) =
|
||||||
self.QObject.delete
|
self.QObject.delete
|
||||||
|
self.metarList.delete
|
||||||
|
|
||||||
proc setup(self: ApplicationLogic) =
|
proc setup(self: ApplicationLogic) =
|
||||||
self.QObject.setup
|
self.QObject.setup
|
||||||
|
|
||||||
proc newApplicationLogic*(app: QApplication): ApplicationLogic =
|
proc newApplicationLogic*(app: QApplication, airports: seq[MetarData]): ApplicationLogic =
|
||||||
new(result)
|
new(result, delete)
|
||||||
result.app = app
|
result.app = app
|
||||||
|
result.metarList = newMetarList(airports)
|
||||||
result.setup()
|
result.setup()
|
||||||
|
|
||||||
## Real methods below here
|
|
||||||
|
|
||||||
proc refresh(self: ApplicationLogic) {.slot.} =
|
proc refresh(self: ApplicationLogic) {.slot.} =
|
||||||
echo "Refresh called"
|
echo "Refresh called"
|
||||||
|
|
||||||
@@ -76,6 +119,12 @@ QtObject:
|
|||||||
proc deleteAirport(self: ApplicationLogic, code: string) {.slot.} =
|
proc deleteAirport(self: ApplicationLogic, code: string) {.slot.} =
|
||||||
echo &"Delete {code}"
|
echo &"Delete {code}"
|
||||||
|
|
||||||
|
proc getMetarList(self: ApplicationLogic): QVariant {.slot.} =
|
||||||
|
return newQVariant(self.metarList)
|
||||||
|
|
||||||
|
QtProperty[QVariant] metarList:
|
||||||
|
read = getMetarList
|
||||||
|
|
||||||
proc mainProc() =
|
proc mainProc() =
|
||||||
let app = newQApplication()
|
let app = newQApplication()
|
||||||
defer: app.delete()
|
defer: app.delete()
|
||||||
@@ -83,7 +132,14 @@ proc mainProc() =
|
|||||||
let engine = newQQmlApplicationEngine()
|
let engine = newQQmlApplicationEngine()
|
||||||
defer: engine.delete()
|
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)
|
let logicVariant = newQVariant(logic)
|
||||||
|
|
||||||
engine.setRootContextProperty("logic", logicVariant)
|
engine.setRootContextProperty("logic", logicVariant)
|
||||||
|
|||||||
Reference in New Issue
Block a user