3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
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.
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.
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.
22 #include "stationlistmodel.h"
28 #include <QStandardItem>
29 #include <QGeoCoordinate>
31 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
33 Q_DECLARE_METATYPE(QGeoCoordinate)
36 StationListModel::StationListModel(QObject *parent) :
37 QAbstractListModel(parent)
39 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
40 setRoleNames(roleNames());
42 Settings *settings = Settings::instance();
43 m_favorites = settings->favoriteStations().toSet();
44 qDebug() << "favorites:" << m_favorites;
47 bool StationListModel::load(const QString &filename)
52 qDebug() << "loading file:" << fi.absoluteFilePath();
54 emit layoutAboutToBeChanged();
56 if (!file.open(QFile::ReadOnly | QFile::Text)) {
57 qDebug() << "cannot open file:" << filename;
60 m_reader.setDevice(&file);
62 while (!m_reader.atEnd()) {
63 if (m_reader.isStartElement()) {
64 if(m_reader.name() == "stations") {
65 readStationsElement();
67 m_reader.raiseError(tr("Not a qpl file"));
74 qDebug() << rowCount() << "stations loaded";
75 if (m_reader.hasError()) {
76 qDebug() << "parser error for:" << filename;
78 } else if (file.error() != QFile::NoError) {
79 qDebug() << "file error for:" << filename;
87 QHash<int, QByteArray> StationListModel::roleNames() const
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";
101 int StationListModel::rowCount(const QModelIndex &) const
103 return m_stations.count();
106 QVariant StationListModel::data(const QModelIndex &index, int role) const
108 if (!index.isValid()) return QVariant();
109 if (index.row() < 0 || index.row() >= m_stations.count()) {
112 StationItem item = m_stations[index.row()];
113 Settings *settings = Settings::instance();
115 case Qt::DisplayRole:
116 return QVariant::fromValue(item.name());
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());
126 return QVariant::fromValue(item.position().latitude());
128 return QVariant::fromValue(item.position().longitude());
130 if (m_favorites.contains(item.name()))
131 return QVariant::fromValue(tr("Favorites"));
133 return QVariant(item.name()[0]);
135 return QVariant::fromValue(QString("Unknown role requested"));
139 bool StationListModel::setData(const QModelIndex &index, QVariant &value, int role)
141 if (!index.isValid())
143 if (role == FavoriteIndicatorRole) {
144 bool favorite = value.toBool();
145 if (data(index, FavoriteIndicatorRole) != favorite) {
146 QString name = m_stations[index.row()].name();
148 qDebug() << "adding" << name << "to favorites";
149 m_favorites.insert(name);
151 qDebug() << "removing" << name << "from favorites";
152 m_favorites.remove(name);
154 Settings *settings = Settings::instance();
155 settings->setFavoriteStations(QStringList::fromSet(m_favorites));
156 QVector<int> changedRoles;
157 changedRoles << FavoriteIndicatorRole;
158 //emit dataChanged(index, index, changedRoles);
159 emit dataChanged(index, index);
166 Qt::ItemFlags StationListModel::flags(const QModelIndex &index) const
168 if (!index.isValid())
170 return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
173 void StationListModel::readStationsElement()
176 while (!m_reader.atEnd()) {
177 if (m_reader.isEndElement()) {
180 } else if (m_reader.isStartElement()) {
181 if (m_reader.name() == "station") {
182 readStationElement();
184 skipUnknownElement(m_reader.name().toString());
192 void StationListModel::readStationElement()
196 while (!m_reader.atEnd()) {
197 if (m_reader.isEndElement()) {
198 m_stations.append(item);
201 } else if (m_reader.isStartElement()) {
202 if (m_reader.name() == "pos") {
203 readPosElement(item);
204 } else if (m_reader.name() == "name") {
205 readNameElement(item);
206 } else if (m_reader.name() == "code") {
207 readCodeElement(item);
209 skipUnknownElement(m_reader.name().toString());
217 void StationListModel::readPosElement(StationItem &item)
219 QStringList coordinates = m_reader.readElementText().split(",");
220 QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
221 item.setPosition(pos);
222 m_reader.readElementText();
223 if (m_reader.isEndElement()) {
228 void StationListModel::readNameElement(StationItem &item)
230 item.setName(m_reader.readElementText());
231 if (m_reader.isEndElement()) {
236 void StationListModel::readCodeElement(StationItem &item)
238 const QString code = m_reader.readElementText();
239 qDebug() << "reading code element" << code;
242 if (m_reader.isEndElement()) {
247 void StationListModel::skipUnknownElement(const QString &name)
249 qDebug() << "skipping unknown element" << name << "at line" << m_reader.lineNumber();
252 while (!m_reader.atEnd()) {
253 if (m_reader.isEndElement()) {
256 } else if (!m_reader.isStartElement()) {
257 skipUnknownElement(m_reader.name().toString());