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 <QtLocation/QGeoCoordinate>
30 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
32 Q_DECLARE_METATYPE(QGeoCoordinate)
35 static QHash<int, QByteArray> roles;
37 StationListModel::StationListModel(QObject *parent) :
38 QStandardItemModel(parent)
41 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
42 QHash<int, QByteArray> roles;
44 roles[Qt::DisplayRole] = "name";
45 roles[StationListModel::PositionRole] = "position";
46 roles[StationListModel::StationCodeRole] = "code";
47 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
52 bool StationListModel::load(const QString &filename)
57 qDebug() << "loading file:" << fi.absoluteFilePath();
59 emit layoutAboutToBeChanged();
61 if (!file.open(QFile::ReadOnly | QFile::Text)) {
62 qDebug() << "cannot open file:" << filename;
65 m_reader.setDevice(&file);
67 while (!m_reader.atEnd()) {
68 if (m_reader.isStartElement()) {
69 if(m_reader.name() == "stations") {
70 readStationsElement();
72 m_reader.raiseError(tr("Not a qpl file"));
79 qDebug() << rowCount() << "stations loaded";
80 if (m_reader.hasError()) {
81 qDebug() << "parser error for:" << filename;
83 } else if (file.error() != QFile::NoError) {
84 qDebug() << "file error for:" << filename;
92 QHash<int, QByteArray> StationListModel::roleNames() const
97 void StationListModel::readStationsElement()
100 while (!m_reader.atEnd()) {
101 if (m_reader.isEndElement()) {
104 } else if (m_reader.isStartElement()) {
105 if (m_reader.name() == "station") {
106 readStationElement();
108 skipUnknownElement();
116 void StationListModel::readStationElement()
118 QStandardItem *item = new QStandardItem;
120 while (!m_reader.atEnd()) {
121 if (m_reader.isEndElement()) {
122 this->appendRow(item);
125 } else if (m_reader.isStartElement()) {
126 if (m_reader.name() == "pos") {
127 readPosElement(item);
128 } else if (m_reader.name() == "name") {
129 readNameElement(item);
130 } else if (m_reader.name() == "code") {
131 readCodeElement(item);
133 skipUnknownElement();
141 void StationListModel::readPosElement(QStandardItem *item)
143 QStringList coordinates = m_reader.readElementText().split(",");
144 QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
145 item->setData(QVariant::fromValue(pos), PositionRole);
146 m_reader.readElementText();
147 if (m_reader.isEndElement()) {
152 void StationListModel::readNameElement(QStandardItem *item)
154 item->setText(m_reader.readElementText());
155 if (m_reader.isEndElement()) {
160 void StationListModel::readCodeElement(QStandardItem *item)
162 const QString code = m_reader.readElementText();
163 qDebug() << "reading code element" << code;
165 item->setData(QVariant::fromValue(code), StationCodeRole);
166 if (m_reader.isEndElement()) {
171 void StationListModel::skipUnknownElement()
173 qDebug() << "skipping unknown element";
176 while (!m_reader.atEnd()) {
177 if (m_reader.isEndElement()) {
180 } else if (!m_reader.isStartElement()) {
181 skipUnknownElement();