1 #include "stationlistmodel.h"
5 #include <QStandardItem>
6 #include <QGeoCoordinate>
9 Q_DECLARE_METATYPE(QGeoCoordinate)
11 StationListModel::StationListModel(QObject *parent) :
12 QStandardItemModel(parent)
18 bool StationListModel::load(const QString &filename)
22 qDebug() << "loading filename:" << filename;
24 if (!file.open(QFile::ReadOnly | QFile::Text)) {
25 qDebug() << "cannot open file:" << filename;
28 reader.setDevice(&file);
30 while (!reader.atEnd()) {
31 if (reader.isStartElement()) {
32 if(reader.name() == "stations") {
33 readStationsElement();
35 reader.raiseError(tr("Not a qpl file"));
42 if (reader.hasError()) {
43 qDebug() << "parser error for:" << filename;
45 } else if (file.error() != QFile::NoError) {
46 qDebug() << "file error for:" << filename;
52 void StationListModel::readStationsElement()
54 qDebug() << "reading stations element";
57 while (!reader.atEnd()) {
58 if (reader.isEndElement()) {
61 } else if (reader.isStartElement()) {
62 if (reader.name() == "station") {
73 void StationListModel::readStationElement()
75 qDebug() << "reading station element";
77 QStandardItem *item = new QStandardItem;
79 while (!reader.atEnd()) {
80 if (reader.isEndElement()) {
81 this->appendRow(item);
84 } else if (reader.isStartElement()) {
85 if (reader.name() == "pos") {
87 } else if (reader.name() == "name") {
88 readNameElement(item);
98 void StationListModel::readPosElement(QStandardItem *item)
100 qDebug() << "reading pos element";
102 QStringList coordinates = reader.readElementText().split(",");
103 QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
104 item->setData(QVariant::fromValue(pos));
105 qDebug() << "pos:" << pos;
106 reader.readElementText();
107 if (reader.isEndElement()) {
112 void StationListModel::readNameElement(QStandardItem *item)
114 qDebug() << "reading name element";
116 item->setText(reader.readElementText());
117 qDebug() << "name:" << item->text();
118 if (reader.isEndElement()) {
123 void StationListModel::skipUnknownElement()
125 qDebug() << "skipping unknown element";
128 while (!reader.atEnd()) {
129 if (reader.isEndElement()) {
132 } else if (!reader.isStartElement()) {
133 skipUnknownElement();