Actually make the code visible to QML
[quandoparte] / application / stationlistmodel.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
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.
9
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.
14
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.
19
20 */
21
22 #include "stationlistmodel.h"
23
24 #include <QFile>
25 #include <QFileInfo>
26 #include <QDebug>
27 #include <QStandardItem>
28 #include <QGeoCoordinate>
29
30 QTM_USE_NAMESPACE
31 Q_DECLARE_METATYPE(QGeoCoordinate)
32
33 StationListModel::StationListModel(QObject *parent) :
34     QStandardItemModel(parent)
35 {
36     setRowCount(0);
37     QHash<int, QByteArray> roles;
38     roles[Qt::DisplayRole] = "name";
39     roles[StationListModel::PositionRole] = "position";
40     roles[StationListModel::StationCodeRole] = "code";
41     setRoleNames(roles);
42 }
43
44 bool StationListModel::load(const QString &filename)
45 {
46     QFile file(filename);
47     QFileInfo fi(file);
48
49     qDebug() << "loading file:" << fi.absoluteFilePath();
50
51     emit layoutAboutToBeChanged();
52     beginResetModel();
53     if (!file.open(QFile::ReadOnly | QFile::Text)) {
54         qDebug() << "cannot open file:" << filename;
55         return false;
56     }
57     m_reader.setDevice(&file);
58     m_reader.readNext();
59     while (!m_reader.atEnd()) {
60         if (m_reader.isStartElement()) {
61             if(m_reader.name() == "stations") {
62                 readStationsElement();
63             } else {
64                 m_reader.raiseError(tr("Not a qpl file"));
65             }
66         } else {
67             m_reader.readNext();
68         }
69     }
70     file.close();
71     qDebug() << rowCount() << "stations loaded";
72     if (m_reader.hasError()) {
73         qDebug() << "parser error for:" << filename;
74         return false;
75     } else if (file.error() != QFile::NoError) {
76         qDebug() << "file error for:" << filename;
77         return false;
78     }
79     endResetModel();
80     emit layoutChanged();
81     return true;
82 }
83
84 void StationListModel::readStationsElement()
85 {
86     m_reader.readNext();
87     while (!m_reader.atEnd()) {
88         if (m_reader.isEndElement()) {
89             m_reader.readNext();
90             break;
91         } else if (m_reader.isStartElement()) {
92             if (m_reader.name() == "station") {
93                 readStationElement();
94             } else {
95                 skipUnknownElement();
96             }
97         } else {
98             m_reader.readNext();
99         }
100     }
101 }
102
103 void StationListModel::readStationElement()
104 {
105     QStandardItem *item = new QStandardItem;
106     m_reader.readNext();
107     while (!m_reader.atEnd()) {
108         if (m_reader.isEndElement()) {
109             this->appendRow(item);
110             m_reader.readNext();
111             break;
112         } else if (m_reader.isStartElement()) {
113             if (m_reader.name() == "pos") {
114                 readPosElement(item);
115             } else  if (m_reader.name() == "name") {
116                 readNameElement(item);
117             } else  if (m_reader.name() == "code") {
118                 readCodeElement(item);
119             } else {
120                 skipUnknownElement();
121             }
122         } else {
123             m_reader.readNext();
124         }
125     }
126 }
127
128 void StationListModel::readPosElement(QStandardItem *item)
129 {
130     QStringList coordinates = m_reader.readElementText().split(",");
131     QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
132     item->setData(QVariant::fromValue(pos), PositionRole);
133     m_reader.readElementText();
134     if (m_reader.isEndElement()) {
135         m_reader.readNext();
136     }
137 }
138
139 void StationListModel::readNameElement(QStandardItem *item)
140 {
141     item->setText(m_reader.readElementText());
142     if (m_reader.isEndElement()) {
143         m_reader.readNext();
144     }
145 }
146
147 void StationListModel::readCodeElement(QStandardItem *item)
148 {
149     const QString code = m_reader.readElementText();
150     qDebug() << "reading code element" << code;
151
152     item->setData(QVariant::fromValue(code), StationCodeRole);
153     if (m_reader.isEndElement()) {
154         m_reader.readNext();
155     }
156 }
157
158 void StationListModel::skipUnknownElement()
159 {
160     qDebug() << "skipping unknown element";
161
162     m_reader.readNext();
163     while (!m_reader.atEnd()) {
164         if (m_reader.isEndElement()) {
165             m_reader.readNext();
166             break;
167         } else if (!m_reader.isStartElement()) {
168             skipUnknownElement();
169         } else {
170             m_reader.readNext();
171         }
172     }
173 }