Bump version to 0.9.0
[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 #include "settings.h"
24
25 #include <QFile>
26 #include <QFileInfo>
27 #include <QDebug>
28 #include <QStandardItem>
29 #include <QGeoCoordinate>
30
31 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
32 QTM_USE_NAMESPACE
33 Q_DECLARE_METATYPE(QGeoCoordinate)
34 #endif
35
36 StationListModel::StationListModel(QObject *parent) :
37     QAbstractListModel(parent)
38 {
39 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
40     setRoleNames(roleNames());
41 #endif
42     Settings *settings = Settings::instance();
43     m_favorites = settings->favoriteStations().toSet();
44     qDebug() << "favorites:" << m_favorites;
45 }
46
47 bool StationListModel::load(const QString &filename)
48 {
49     QFile file(filename);
50     QFileInfo fi(file);
51
52     qDebug() << "loading file:" << fi.absoluteFilePath();
53
54     emit layoutAboutToBeChanged();
55     beginResetModel();
56     if (!file.open(QFile::ReadOnly | QFile::Text)) {
57         qDebug() << "cannot open file:" << filename;
58         return false;
59     }
60     m_reader.setDevice(&file);
61     m_reader.readNext();
62     while (!m_reader.atEnd()) {
63         if (m_reader.isStartElement()) {
64             if(m_reader.name() == "stations") {
65                 readStationsElement();
66             } else {
67                 m_reader.raiseError(tr("Not a qpl file"));
68             }
69         } else {
70             m_reader.readNext();
71         }
72     }
73     file.close();
74     qDebug() << rowCount() << "stations loaded";
75     if (m_reader.hasError()) {
76         qDebug() << "parser error for:" << filename;
77         return false;
78     } else if (file.error() != QFile::NoError) {
79         qDebug() << "file error for:" << filename;
80         return false;
81     }
82     endResetModel();
83     emit layoutChanged();
84     return true;
85 }
86
87 QHash<int, QByteArray> StationListModel::roleNames() const
88 {
89     QHash<int, QByteArray> roles;
90     roles[Qt::DisplayRole] = "name";
91     roles[StationListModel::PositionRole] = "position";
92     roles[StationListModel::RecentIndicatorRole] = "recent";
93     roles[StationListModel::FavoriteIndicatorRole] = "favorite";
94     roles[StationListModel::StationCodeRole] = "code";
95     roles[StationListModel::LongitudeRole] = "longitude";
96     roles[StationListModel::LatitudeRole] = "latitude";
97     roles[StationListModel::SectionRole] = "section";
98     return roles;
99 }
100
101 int StationListModel::rowCount(const QModelIndex &) const
102 {
103     return m_stations.count();
104 }
105
106 QVariant StationListModel::data(const QModelIndex &index, int role) const
107 {
108     if (!index.isValid()) return QVariant();
109     if (index.row() < 0 || index.row() >= m_stations.count()) {
110         return QVariant();
111     }
112     StationItem item = m_stations[index.row()];
113     Settings *settings = Settings::instance();
114     switch (role) {
115     case Qt::DisplayRole:
116         return QVariant::fromValue(item.name());
117     case PositionRole:
118         return QVariant::fromValue(item.position());
119     case RecentIndicatorRole:
120         return QVariant(settings->recentStations().contains(item.name()));
121     case FavoriteIndicatorRole:
122         return QVariant(m_favorites.contains(item.name()));
123     case StationCodeRole:
124         return QVariant::fromValue(item.code());
125     case LatitudeRole:
126         return QVariant::fromValue(item.position().latitude());
127     case LongitudeRole:
128         return QVariant::fromValue(item.position().longitude());
129     case SectionRole:
130         if (m_favorites.contains(item.name()))
131             return QVariant::fromValue(tr("Favorites"));
132         else
133             return QVariant(item.name()[0]);
134     default:
135         return QVariant::fromValue(QString("Unknown role requested"));
136     }
137 }
138
139 bool StationListModel::setData(const QModelIndex &index, const QVariant &value, int role)
140 {
141     if (!index.isValid())
142         return false;
143     if (role == FavoriteIndicatorRole) {
144         bool favorite = value.toBool();
145         if (data(index, FavoriteIndicatorRole) != favorite) {
146             QString name =  m_stations[index.row()].name();
147             if (favorite) {
148                 qDebug() << "adding" << name << "to favorites";
149                 m_favorites.insert(name);
150             } else {
151                 qDebug() << "removing" << name << "from favorites";
152                 m_favorites.remove(name);
153             }
154             Settings *settings = Settings::instance();
155             settings->setFavoriteStations(QStringList::fromSet(m_favorites));
156             emit dataChanged(index, index);
157         }
158         return true;
159     }
160    return false;
161 }
162
163 Qt::ItemFlags StationListModel::flags(const QModelIndex &index) const
164 {
165     if (!index.isValid())
166         return 0;
167     return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
168 }
169
170 void StationListModel::readStationsElement()
171 {
172     m_reader.readNext();
173     while (!m_reader.atEnd()) {
174         if (m_reader.isEndElement()) {
175             m_reader.readNext();
176             break;
177         } else if (m_reader.isStartElement()) {
178             if (m_reader.name() == "station") {
179                 readStationElement();
180             } else {
181                 skipUnknownElement(m_reader.name().toString());
182             }
183         } else {
184             m_reader.readNext();
185         }
186     }
187 }
188
189 void StationListModel::readStationElement()
190 {
191     StationItem item;
192     m_reader.readNext();
193     while (!m_reader.atEnd()) {
194         if (m_reader.isEndElement()) {
195             m_stations.append(item);
196             m_reader.readNext();
197             break;
198         } else if (m_reader.isStartElement()) {
199             if (m_reader.name() == "pos") {
200                 readPosElement(item);
201             } else  if (m_reader.name() == "name") {
202                 readNameElement(item);
203             } else  if (m_reader.name() == "code") {
204                 readCodeElement(item);
205             } else {
206                 skipUnknownElement(m_reader.name().toString());
207             }
208         } else {
209             m_reader.readNext();
210         }
211     }
212 }
213
214 void StationListModel::readPosElement(StationItem &item)
215 {
216     QStringList coordinates = m_reader.readElementText().split(",");
217     QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
218     item.setPosition(pos);
219     m_reader.readElementText();
220     if (m_reader.isEndElement()) {
221         m_reader.readNext();
222     }
223 }
224
225 void StationListModel::readNameElement(StationItem &item)
226 {
227     item.setName(m_reader.readElementText());
228     if (m_reader.isEndElement()) {
229         m_reader.readNext();
230     }
231 }
232
233 void StationListModel::readCodeElement(StationItem &item)
234 {
235     const QString code = m_reader.readElementText();
236     qDebug() << "reading code element" << code;
237
238     item.setCode(code);
239     if (m_reader.isEndElement()) {
240         m_reader.readNext();
241     }
242 }
243
244 void StationListModel::skipUnknownElement(const QString &name)
245 {
246     qDebug() << "skipping unknown element" << name << "at line" << m_reader.lineNumber();
247
248     m_reader.readNext();
249     while (!m_reader.atEnd()) {
250         if (m_reader.isEndElement()) {
251             m_reader.readNext();
252             break;
253         } else if (!m_reader.isStartElement()) {
254             skipUnknownElement(m_reader.name().toString());
255         } else {
256             m_reader.readNext();
257         }
258     }
259 }