746610fe1fd6f519b2fe61e0e7812df0c8f2eaca
[quandoparte] / application / stationlistproxymodel.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 "stationlistproxymodel.h"
23 #include "stationlistmodel.h"
24
25 #include <QDebug>
26 #include <QGeoCoordinate>
27
28 QTM_USE_NAMESPACE
29
30 Q_DECLARE_METATYPE(QGeoCoordinate)
31
32 StationListProxyModel::StationListProxyModel(QObject *parent) :
33     QSortFilterProxyModel(parent),
34     positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
35     m_here(44.5, 9.0),
36     m_sortingMode(AlphaSorting),
37     m_filterRecentOnly(false)
38 {
39     QHash<int, QByteArray> roles;
40     roles[StationListModel::PositionRole] = "position";
41     setRoleNames(roles);
42
43     setFilterCaseSensitivity(Qt::CaseInsensitive);
44     setSortCaseSensitivity(Qt::CaseInsensitive);
45     if (positionInfoSource) {
46         qDebug() << "position info source available";
47         connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
48                 SLOT(updatePosition(QGeoPositionInfo)));
49     } else {
50         qDebug() << "No position info source available";
51     }
52 }
53
54 bool StationListProxyModel::lessThan(const QModelIndex &left,
55                                      const QModelIndex &right) const
56 {
57     int role = sortRole();
58
59     if (role == StationListModel::PositionRole) {
60         QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
61         QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
62        return first.distanceTo(m_here) < second.distanceTo(m_here);
63     } else {
64         return QString::compare(left.data(role).toString(),
65                                 right.data(role).toString(),
66                                 sortCaseSensitivity()) < 0;
67     }
68 }
69
70 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
71 {
72     m_here = pos;
73 }
74
75 void StationListProxyModel::setRecentStations(const QStringList &stations)
76 {
77     m_stations = stations;
78 }
79
80 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
81                                              const QModelIndex &sourceParent) const
82 {
83     bool acceptable;
84     QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
85     QString stationName = sourceModel()->data(i).toString();
86     if (m_filterRecentOnly) {
87         acceptable =  m_stations.contains(stationName);
88     } else {
89         acceptable = true;
90     }
91     return acceptable && stationName.contains(filterRegExp());
92 }
93
94 void StationListProxyModel::setRecentOnlyFilter(bool activation)
95 {
96     m_filterRecentOnly = activation;
97 }
98
99 QString StationListProxyModel::searchPattern() const
100 {
101     return m_searchPattern;
102 }
103
104 void StationListProxyModel::setSearchPattern(const QString &pattern)
105 {
106     m_searchPattern = pattern;
107     setFilterFixedString(m_searchPattern);
108     qDebug() << "set Search pattern to" << pattern;
109 }
110
111 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
112 {
113     return m_sortingMode;
114 }
115
116 void StationListProxyModel::setSortingMode(SortingMode mode)
117 {
118     if (mode != m_sortingMode) {
119         qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
120         m_sortingMode = mode;
121         setRecentOnlyFilter(false);
122
123         switch (mode) {
124         case StationListProxyModel::AlphaSorting:
125             setSortRole(Qt::DisplayRole);
126             break;
127         case StationListProxyModel::DistanceSorting:
128             setSortRole(StationListModel::PositionRole);
129             break;
130         case StationListProxyModel::RecentUsageSorting:
131             setRecentOnlyFilter(true);
132             break;
133         default:
134             break;
135         }
136         if (mode == StationListProxyModel::DistanceSorting) {
137             positionInfoSource->startUpdates();
138         } else {
139             positionInfoSource->stopUpdates();
140         }
141         invalidate();
142         sort(0);
143         emit sortingModeChanged(mode);
144     }
145 }
146
147 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
148 {
149     qDebug() << "Position update received" << update;
150     setUserPosition(update.coordinate());
151     invalidate();
152     sort(0);
153 }