3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "stationlistmodel.h"
27 #include <QStandardItem>
28 #include <QGeoCoordinate>
31 Q_DECLARE_METATYPE(QGeoCoordinate)
33 StationListModel::StationListModel(QObject *parent) :
34 QStandardItemModel(parent)
40 bool StationListModel::load(const QString &filename)
45 qDebug() << "loading file:" << fi.absoluteFilePath();
47 if (!file.open(QFile::ReadOnly | QFile::Text)) {
48 qDebug() << "cannot open file:" << filename;
51 m_reader.setDevice(&file);
53 while (!m_reader.atEnd()) {
54 if (m_reader.isStartElement()) {
55 if(m_reader.name() == "stations") {
56 readStationsElement();
58 m_reader.raiseError(tr("Not a qpl file"));
65 if (m_reader.hasError()) {
66 qDebug() << "parser error for:" << filename;
68 } else if (file.error() != QFile::NoError) {
69 qDebug() << "file error for:" << filename;
76 void StationListModel::readStationsElement()
79 while (!m_reader.atEnd()) {
80 if (m_reader.isEndElement()) {
83 } else if (m_reader.isStartElement()) {
84 if (m_reader.name() == "station") {
95 void StationListModel::readStationElement()
97 QStandardItem *item = new QStandardItem;
99 while (!m_reader.atEnd()) {
100 if (m_reader.isEndElement()) {
101 this->appendRow(item);
104 } else if (m_reader.isStartElement()) {
105 if (m_reader.name() == "pos") {
106 readPosElement(item);
107 } else if (m_reader.name() == "name") {
108 readNameElement(item);
110 skipUnknownElement();
118 void StationListModel::readPosElement(QStandardItem *item)
120 QStringList coordinates = m_reader.readElementText().split(",");
121 QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
122 item->setData(QVariant::fromValue(pos), PositionRole);
123 qDebug() << "pos:" << pos;
124 m_reader.readElementText();
125 if (m_reader.isEndElement()) {
130 void StationListModel::readNameElement(QStandardItem *item)
132 item->setText(m_reader.readElementText());
133 qDebug() << "name:" << item->text();
134 if (m_reader.isEndElement()) {
139 void StationListModel::skipUnknownElement()
141 qDebug() << "skipping unknown element";
144 while (!m_reader.atEnd()) {
145 if (m_reader.isEndElement()) {
148 } else if (!m_reader.isStartElement()) {
149 skipUnknownElement();