ab1dde39dfd7a2f63d5227515fb3bb9fce3449e3
[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 {
15 }
16
17 bool StationListProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
18 {
19     int role = sortRole();
20
21     if (role == StationListModel::PositionRole) {
22         QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
23         QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
24        return first.distanceTo(m_here) < second.distanceTo(m_here);
25     } else {
26         return QString::compare(left.data(role).toString(),
27                                 right.data(role).toString(),
28                                 sortCaseSensitivity()) < 0;
29     }
30 }
31
32 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
33 {
34     m_here = pos;
35 }