Updated station generation script, station list
[quandoparte] / application / stationlistproxymodel.cpp
1 #include "stationlistproxymodel.h"
2 #include "stationlistmodel.h"
3
4 #include <QDebug>
5 #include <QGeoCoordinate>
6
7 QTM_USE_NAMESPACE
8
9 Q_DECLARE_METATYPE(QGeoCoordinate)
10
11 StationListProxyModel::StationListProxyModel(QObject *parent) :
12     QSortFilterProxyModel(parent),
13     m_here(44.5, 9.0),
14     m_filterRecentOnly(false)
15 {
16 }
17
18 bool StationListProxyModel::lessThan(const QModelIndex &left,
19                                      const QModelIndex &right) const
20 {
21     int role = sortRole();
22
23     if (role == StationListModel::PositionRole) {
24         QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
25         QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
26        return first.distanceTo(m_here) < second.distanceTo(m_here);
27     } else {
28         return QString::compare(left.data(role).toString(),
29                                 right.data(role).toString(),
30                                 sortCaseSensitivity()) < 0;
31     }
32 }
33
34 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
35 {
36     m_here = pos;
37 }
38
39 void StationListProxyModel::setRecentStations(const QStringList &stations)
40 {
41     m_stations = stations;
42 }
43
44 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
45                                              const QModelIndex &sourceParent) const
46 {
47     if (m_filterRecentOnly) {
48         QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
49         return m_stations.contains(sourceModel()->data(i).toString());
50     }
51     return true;
52 }
53
54 void StationListProxyModel::setRecentOnlyFilter(bool)
55 {
56     m_filterRecentOnly = true;
57 }