Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 235 lines (194 sloc) 6.312 kb
418ce8a @gnuvince initial commit
authored
1 /*
2 * stationmodel.cpp
3 * Copyright (C) 2011 Nicolas Martin, Université de Montréal
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 3 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
13 * GNU 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. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "stationmodel.h"
20 #include <QFile>
21 #include <QXmlStreamReader>
bf29915 @gnuvince added data
authored
22 #include "geoposition.h"
23 #include "station.h"
24 #include <QDebug>
ba35c54 @gnuvince Added interrogation icon to get station information
authored
25 #include <QPixmap>
418ce8a @gnuvince initial commit
authored
26
27 StationModel::StationModel(QObject *parent) :
28 QAbstractTableModel(parent)
29 {
9e588f1 @Gabriano User selection
Gabriano authored
30 currentPosition = new GeoPosition;
418ce8a @gnuvince initial commit
authored
31 }
32
33 QVariant StationModel::fieldAt(const QModelIndex &index, int role) const
34 {
35 uint row = index.row();
36 uint column = index.column();
37
38 qint64 id = stations.keys()[row];
39 Station *s = stations[id];
8040e84 @gnuvince Fixed some parts of station selection
authored
40
41 if (column == Station::COL_DIST && role == Qt::DisplayRole) {
bf29915 @gnuvince added data
authored
42 qreal distance = s->getPosition().distanceFrom(currentPosition);
ba35c54 @gnuvince Added interrogation icon to get station information
authored
43 return QString("%1 km").arg(distance, 0, 'f', 2);
44 }
45 if (column == Station::COL_INFO && role == Qt::DecorationRole) {
46 QPixmap pix(":/icones/data/icons/help.png");
47 return pix;
bf29915 @gnuvince added data
authored
48 }
8040e84 @gnuvince Fixed some parts of station selection
authored
49
418ce8a @gnuvince initial commit
authored
50 return s->field(column, role);
51 }
52
53 QVariant StationModel::data(const QModelIndex &index, int role) const
54 {
55 if (!index.isValid())
56 return QVariant();
8040e84 @gnuvince Fixed some parts of station selection
authored
57
418ce8a @gnuvince initial commit
authored
58 return fieldAt(index, role);
59 }
60
61 int StationModel::rowCount(const QModelIndex &parent) const
62 {
63 if (parent.isValid()) return 0;
64 return stations.count();
65 }
66
67 int StationModel::columnCount(const QModelIndex &parent) const
68 {
69 if (parent.isValid()) return 0;
70 return Station::headerCount();
71 }
72
73 QVariant StationModel::headerData(int section, Qt::Orientation orientation, int role) const
74 {
75 if (role != Qt::DisplayRole)
76 return QVariant();
77 if (orientation == Qt::Horizontal) {
78 return Station::header(section);
79 } else if (orientation == Qt::Vertical) {
80 if (section < 0 || section >= stations.count())
81 return QVariant();
82 return section;
83 }
84 return QVariant();
85 }
86
87 /*
88 bool StationModel::setData(const QModelIndex &index, const QVariant &value, int role)
89 {
90 if (!index.isValid())
91 return false;
92 if (role != Qt::EditRole)
93 return false;
94
95 uint row = index.row();
96 uint column = index.column();
97
98 qint64 id = stations.keys().value(row,-1);
99 if (id < 0) return false;
100
101 Station *s = stations[id];
102
103 if (s->changeField(column, value)) {
104 emit dataChanged(index,index);
105 return true;
106 }
107
108 return false;
109 }
110
111 Qt::ItemFlags StationModel::flags(const QModelIndex &index) const
112 {
113 if (!index.isValid())
114 return Qt::ItemFlags();
115 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
116 }
117 */
118
119 #include <QDebug>
120
121 bool StationModel::setStations(QString xml_path, QString photo_path)
122 {
123 QFile f(xml_path);
124 if (!f.open(QIODevice::ReadOnly|QIODevice::Text))
125 return false;
126
127 QXmlStreamReader rd;
128 rd.setDevice(&f);
129
130 beginResetModel();
131
132 stations.clear();
133 while (rd.readNextStartElement()) {
134 if (rd.name() == "station") {
135 uint id;
136 QString nom,desc,coffret;
137 qreal lat, lon;
138 while (rd.readNextStartElement()) {
139 if (rd.isStartElement()) {
140 if (rd.name() == "num") {
141 id = rd.readElementText().toUInt();
142 } else if (rd.name() == "nom") {
143 nom = rd.readElementText();
144 } else if (rd.name() == "description") {
145 desc = rd.readElementText();
146 } else if (rd.name() == "coffret") {
147 coffret = rd.readElementText();
148 } else if (rd.name() == "lat") {
149 lat = rd.readElementText().toDouble();
150 } else if (rd.name() == "lng") {
151 lon = rd.readElementText().toDouble();
152 } else
153 rd.skipCurrentElement();
154 }
155 }
156 QPixmap photo, map;
157 QString path;
158 path = QString("%2photo%1.jpg").arg(QString::number(id).rightJustified(3,QChar('0'))).arg(photo_path);
159 if (!photo.load(path))
160 return false;
161 path = QString("%2map%1.gif").arg(QString::number(id).rightJustified(3,QChar('0'))).arg(photo_path);
162 if (!map.load(path))
163 return false;
164
165 stations[id] = new Station(id, nom, desc,coffret, GeoPosition(lat,lon), photo, map, this);
166 }
167 }
168
169 endResetModel();
170
171 return true;
172 }
173
174 /*
175 bool StationModel::addStation(Station *station)
176 {
177 //tester si elle est valide
178
179 int pos = stations.count();
180 beginInsertRows(QModelIndex(), pos, pos+1);
181 stations[station->getId()] = station;
182 endInsertRows();
183
184 return true;
185 }
186
187 bool StationModel::removeStation(qint64 id)
188 {
189 if (stations.count(id) == 0) return false;
190
191 QList<qint64>::const_iterator it = qFind(stations.keys(),id);
192 int pos = it-stations.keys().begin();
193 beginRemoveRows(QModelIndex(), pos, pos+1);
194 stations.remove(id);
195 endRemoveRows();
196
197 return true;
198 }
199 */
200
201 QDataStream& operator<<(QDataStream &ds, const StationModel &model)
202 {
203 int count = model.stations.count();
204
205 ds << count;
206 foreach (Station *s, model.stations) {
207 ds << (*s);
208 }
209 return ds;
210 }
211
212 QDataStream& operator>>(QDataStream &ds, StationModel &model)
213 {
214 Station s;
215 int count;
216
217 model.beginResetModel();
218 model.stations.clear();
219 ds >> count;
220 for (int i=0; i<count; ++i) {
221 ds >> s;
222 model.stations[s.id] = new Station(s);
223 }
224 model.endResetModel();
225 return ds;
226 }
e64a8e4 @gnuvince Selecting a position on the map affects the selection of the station …
authored
227
228
229
230
231 void StationModel::updateCurrentPosition(GeoPosition pos) {
232 currentPosition->setLat(pos.getLat());
233 currentPosition->setLon(pos.getLon());
234 }
Something went wrong with that request. Please try again.