Print number of stations loaded on loading.
[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 }
38
39 bool StationListModel::load(const QString &filename)
40 {
41     QFile file(filename);
42     QFileInfo fi(file);
43
44     qDebug() << "loading file:" << fi.absoluteFilePath();
45
46     emit layoutAboutToBeChanged();
47     beginResetModel();
48     if (!file.open(QFile::ReadOnly | QFile::Text)) {
49         qDebug() << "cannot open file:" << filename;
50         return false;
51     }
52     m_reader.setDevice(&file);
53     m_reader.readNext();
54     while (!m_reader.atEnd()) {
55         if (m_reader.isStartElement()) {
56             if(m_reader.name() == "stations") {
57                 readStationsElement();
58             } else {
59                 m_reader.raiseError(tr("Not a qpl file"));
60             }
61         } else {
62             m_reader.readNext();
63         }
64     }
65     file.close();
66     qDebug() << rowCount() << "stations loaded";
67     if (m_reader.hasError()) {
68         qDebug() << "parser error for:" << filename;
69         return false;
70     } else if (file.error() != QFile::NoError) {
71         qDebug() << "file error for:" << filename;
72         return false;
73     }
74     endResetModel();
75     emit layoutChanged();
76     return true;
77 }
78
79 void StationListModel::readStationsElement()
80 {
81     m_reader.readNext();
82     while (!m_reader.atEnd()) {
83         if (m_reader.isEndElement()) {
84             m_reader.readNext();
85             break;
86         } else if (m_reader.isStartElement()) {
87             if (m_reader.name() == "station") {
88                 readStationElement();
89             } else {
90                 skipUnknownElement();
91             }
92         } else {
93             m_reader.readNext();
94         }
95     }
96 }
97
98 void StationListModel::readStationElement()
99 {
100     QStandardItem *item = new QStandardItem;
101     m_reader.readNext();
102     while (!m_reader.atEnd()) {
103         if (m_reader.isEndElement()) {
104             this->appendRow(item);
105             m_reader.readNext();
106             break;
107         } else if (m_reader.isStartElement()) {
108             if (m_reader.name() == "pos") {
109                 readPosElement(item);
110             } else  if (m_reader.name() == "name") {
111                 readNameElement(item);
112             } else {
113                 skipUnknownElement();
114             }
115         } else {
116             m_reader.readNext();
117         }
118     }
119 }
120
121 void StationListModel::readPosElement(QStandardItem *item)
122 {
123     QStringList coordinates = m_reader.readElementText().split(",");
124     QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
125     item->setData(QVariant::fromValue(pos), PositionRole);
126     m_reader.readElementText();
127     if (m_reader.isEndElement()) {
128         m_reader.readNext();
129     }
130 }
131
132 void StationListModel::readNameElement(QStandardItem *item)
133 {
134     item->setText(m_reader.readElementText());
135     if (m_reader.isEndElement()) {
136         m_reader.readNext();
137     }
138 }
139
140 void StationListModel::skipUnknownElement()
141 {
142     qDebug() << "skipping unknown element";
143
144     m_reader.readNext();
145     while (!m_reader.atEnd()) {
146         if (m_reader.isEndElement()) {
147             m_reader.readNext();
148             break;
149         } else if (!m_reader.isStartElement()) {
150             skipUnknownElement();
151         } else {
152             m_reader.readNext();
153         }
154     }
155 }