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