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 #include <QGeoCoordinate>
32 Q_DECLARE_METATYPE(QGeoCoordinate)
34 StationListProxyModel::StationListProxyModel(QObject *parent) :
35 QSortFilterProxyModel(parent),
36 positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
38 m_filterRecentOnly(false)
40 Settings *settings = Settings::instance();
41 forceSortingMode(settings->stationListSortingMode());
42 setFilterCaseSensitivity(Qt::CaseInsensitive);
43 setSortCaseSensitivity(Qt::CaseInsensitive);
44 setDynamicSortFilter(true);
45 if (positionInfoSource) {
46 qDebug() << "position info source available";
47 connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
48 SLOT(updatePosition(QGeoPositionInfo)));
49 positionInfoSource->setUpdateInterval(5000);
51 qDebug() << "No position info source available";
53 connect(settings, SIGNAL(recentStationsChanged()),
54 this, SLOT(updateRecentStations()));
55 updateRecentStations();
58 bool StationListProxyModel::lessThan(const QModelIndex &left,
59 const QModelIndex &right) const
61 int role = sortRole();
63 if (role == StationListModel::PositionRole) {
64 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
65 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
66 return first.distanceTo(m_here) < second.distanceTo(m_here);
68 return QString::compare(left.data(role).toString(),
69 right.data(role).toString(),
70 sortCaseSensitivity()) < 0;
74 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
76 qDebug() << "Position is now" << pos;
78 if (sortingMode() == StationListProxyModel::DistanceSorting) {
83 void StationListProxyModel::setRecentStations(const QStringList &stations)
85 qDebug() << "Recent stations are now" << stations;
86 m_stations = stations;
87 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
92 void StationListProxyModel::updateRecentStations(void)
94 Settings *settings = Settings::instance();
95 setRecentStations(settings->recentStations());
98 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
99 const QModelIndex &sourceParent) const
102 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
103 QString stationName = sourceModel()->data(i).toString();
104 if (m_filterRecentOnly) {
105 acceptable = m_stations.contains(stationName);
109 return acceptable && stationName.contains(filterRegExp());
112 void StationListProxyModel::setRecentOnlyFilter(bool activation)
114 m_filterRecentOnly = activation;
117 QString StationListProxyModel::searchPattern() const
119 return m_searchPattern;
122 void StationListProxyModel::setSearchPattern(const QString &pattern)
124 m_searchPattern = pattern;
125 setFilterFixedString(m_searchPattern);
126 qDebug() << "set Search pattern to" << pattern;
129 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
131 return m_sortingMode;
134 void StationListProxyModel::setSortingMode(SortingMode mode)
136 if (mode != m_sortingMode) {
138 forceSortingMode(mode);
141 Settings *settings = Settings::instance();
142 settings->setStationListSortingMode(m_sortingMode);
144 emit sortingModeChanged(mode);
147 void StationListProxyModel::forceSortingMode(SortingMode mode)
149 m_sortingMode = mode;
150 setRecentOnlyFilter(false);
153 case StationListProxyModel::AlphaSorting:
154 setSortRole(Qt::DisplayRole);
156 case StationListProxyModel::DistanceSorting:
157 setSortRole(StationListModel::PositionRole);
159 case StationListProxyModel::RecentUsageSorting:
160 setRecentOnlyFilter(true);
165 if (mode == StationListProxyModel::DistanceSorting) {
166 positionInfoSource->startUpdates();
168 positionInfoSource->stopUpdates();
174 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
176 qDebug() << "Position update received" << update;
177 if (update.isValid()) {
178 QGeoCoordinate newPosition = update.coordinate();
179 if (newPosition.distanceTo(m_here) > 50.0) {
180 setUserPosition(update.coordinate());