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 "stationlistproxymodel.h"
25 #include "stationlistmodel.h"
28 #ifndef TARGET_PLATFORM_SAILFISH
29 #include <QGeoCoordinate>
33 Q_DECLARE_METATYPE(QGeoCoordinate)
36 StationListProxyModel::StationListProxyModel(QObject *parent) :
37 QSortFilterProxyModel(parent),
38 #ifndef TARGET_PLATFORM_SAILFISH
39 positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
42 m_filterRecentOnly(false)
44 Settings *settings = Settings::instance();
45 forceSortingMode(settings->stationListSortingMode());
46 setFilterCaseSensitivity(Qt::CaseInsensitive);
47 setSortCaseSensitivity(Qt::CaseInsensitive);
48 setDynamicSortFilter(true);
49 #ifndef TARGET_PLATFORM_SAILFISH
50 if (positionInfoSource) {
51 qDebug() << "position info source available";
52 connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
53 SLOT(updatePosition(QGeoPositionInfo)));
54 positionInfoSource->setUpdateInterval(5000);
56 qDebug() << "No position info source available";
59 connect(settings, SIGNAL(recentStationsChanged()),
60 this, SLOT(updateRecentStations()));
61 updateRecentStations();
64 bool StationListProxyModel::lessThan(const QModelIndex &left,
65 const QModelIndex &right) const
67 int role = sortRole();
69 if (role == StationListModel::PositionRole) {
70 #ifdef TARGET_PLATFORM_SAILFISH
73 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
74 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
75 return first.distanceTo(m_here) < second.distanceTo(m_here);
78 return QString::compare(left.data(role).toString(),
79 right.data(role).toString(),
80 sortCaseSensitivity()) < 0;
85 #ifndef TARGET_PLATFORM_SAILFISH
86 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
88 qDebug() << "Position is now" << pos;
90 if (sortingMode() == StationListProxyModel::DistanceSorting) {
96 void StationListProxyModel::setRecentStations(const QStringList &stations)
98 qDebug() << "Recent stations are now" << stations;
99 m_stations = stations;
100 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
105 void StationListProxyModel::updateRecentStations(void)
107 Settings *settings = Settings::instance();
108 setRecentStations(settings->recentStations());
111 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
112 const QModelIndex &sourceParent) const
115 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
116 QString stationName = sourceModel()->data(i).toString();
117 if (m_filterRecentOnly) {
118 acceptable = m_stations.contains(stationName);
122 return acceptable && stationName.contains(filterRegExp());
125 void StationListProxyModel::setRecentOnlyFilter(bool activation)
127 m_filterRecentOnly = activation;
130 QString StationListProxyModel::searchPattern() const
132 return m_searchPattern;
135 void StationListProxyModel::setSearchPattern(const QString &pattern)
137 m_searchPattern = pattern;
138 setFilterFixedString(m_searchPattern);
139 qDebug() << "set Search pattern to" << pattern;
142 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
144 return m_sortingMode;
147 void StationListProxyModel::setSortingMode(SortingMode mode)
149 if (mode != m_sortingMode) {
151 forceSortingMode(mode);
154 Settings *settings = Settings::instance();
155 settings->setStationListSortingMode(m_sortingMode);
157 emit sortingModeChanged(mode);
160 void StationListProxyModel::forceSortingMode(SortingMode mode)
162 m_sortingMode = mode;
163 setRecentOnlyFilter(false);
166 case StationListProxyModel::AlphaSorting:
167 setSortRole(Qt::DisplayRole);
169 case StationListProxyModel::DistanceSorting:
170 setSortRole(StationListModel::PositionRole);
172 case StationListProxyModel::RecentUsageSorting:
173 setRecentOnlyFilter(true);
178 #ifndef TARGET_PLATFORM_SAILFISH
179 if (mode == StationListProxyModel::DistanceSorting) {
180 positionInfoSource->startUpdates();
182 positionInfoSource->stopUpdates();
189 #ifndef TARGET_PLATFORM_SAILFISH
190 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
192 qDebug() << "Position update received" << update;
193 if (update.isValid()) {
194 QGeoCoordinate newPosition = update.coordinate();
195 if (newPosition.distanceTo(m_here) > 50.0) {
196 setUserPosition(update.coordinate());