Use (begin/end)ResetModel() when loading items in the model
[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     if (m_reader.hasError()) {
67         qDebug() << "parser error for:" << filename;
68         return false;
69     } else if (file.error() != QFile::NoError) {
70         qDebug() << "file error for:" << filename;
71         return false;
72     }
73     endResetModel();
74     emit layoutChanged();
75     return true;
76 }
77
78 void StationListModel::readStationsElement()
79 {
80     m_reader.readNext();
81     while (!m_reader.atEnd()) {
82         if (m_reader.isEndElement()) {
83             m_reader.readNext();
84             break;
85         } else if (m_reader.isStartElement()) {
86             if (m_reader.name() == "station") {
87                 readStationElement();
88             } else {
89                 skipUnknownElement();
90             }
91         } else {
92             m_reader.readNext();
93         }
94     }
95 }
96
97 void StationListModel::readStationElement()
98 {
99     QStandardItem *item = new QStandardItem;
100     m_reader.readNext();
101     while (!m_reader.atEnd()) {
102         if (m_reader.isEndElement()) {
103             this->appendRow(item);
104             m_reader.readNext();
105             break;
106         } else if (m_reader.isStartElement()) {
107             if (m_reader.name() == "pos") {
108                 readPosElement(item);
109             } else  if (m_reader.name() == "name") {
110                 readNameElement(item);
111             } else {
112                 skipUnknownElement();
113             }
114         } else {
115             m_reader.readNext();
116         }
117     }
118 }
119
120 void StationListModel::readPosElement(QStandardItem *item)
121 {
122     QStringList coordinates = m_reader.readElementText().split(",");
123     QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
124     item->setData(QVariant::fromValue(pos), PositionRole);
125     m_reader.readElementText();
126     if (m_reader.isEndElement()) {
127         m_reader.readNext();
128     }
129 }
130
131 void StationListModel::readNameElement(QStandardItem *item)
132 {
133     item->setText(m_reader.readElementText());
134     if (m_reader.isEndElement()) {
135         m_reader.readNext();
136     }
137 }
138
139 void StationListModel::skipUnknownElement()
140 {
141     qDebug() << "skipping unknown element";
142
143     m_reader.readNext();
144     while (!m_reader.atEnd()) {
145         if (m_reader.isEndElement()) {
146             m_reader.readNext();
147             break;
148         } else if (!m_reader.isStartElement()) {
149             skipUnknownElement();
150         } else {
151             m_reader.readNext();
152         }
153     }
154 }