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"
29 #include <QGeoCoordinate>
31 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
34 Q_DECLARE_METATYPE(QGeoCoordinate)
37 StationListProxyModel::StationListProxyModel(QObject *parent) :
38 QSortFilterProxyModel(parent),
39 positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
41 m_filterRecentOnly(false)
43 Settings *settings = Settings::instance();
44 forceSortingMode(settings->stationListSortingMode());
45 setFilterCaseSensitivity(Qt::CaseInsensitive);
46 setSortCaseSensitivity(Qt::CaseInsensitive);
47 setDynamicSortFilter(true);
48 if (positionInfoSource) {
49 qDebug() << "position info source available";
50 connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
51 SLOT(updatePosition(QGeoPositionInfo)));
52 positionInfoSource->setUpdateInterval(5000);
54 qDebug() << "No position info source available";
56 connect(settings, SIGNAL(recentStationsChanged()),
57 this, SLOT(updateRecentStations()));
58 updateRecentStations();
61 bool StationListProxyModel::lessThan(const QModelIndex &left,
62 const QModelIndex &right) const
64 int role = sortRole();
66 if (role == StationListModel::PositionRole) {
67 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
68 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
69 return first.distanceTo(m_here) < second.distanceTo(m_here);
71 return QString::compare(left.data(role).toString(),
72 right.data(role).toString(),
73 sortCaseSensitivity()) < 0;
78 void StationListProxyModel::setUserPosition(const QGeoCoordinate &pos)
80 qDebug() << "Position is now" << pos;
82 if (sortingMode() == StationListProxyModel::DistanceSorting) {
87 void StationListProxyModel::setRecentStations(const QStringList &stations)
89 qDebug() << "Recent stations are now" << stations;
90 m_stations = stations;
91 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
96 void StationListProxyModel::updateRecentStations(void)
98 Settings *settings = Settings::instance();
99 setRecentStations(settings->recentStations());
102 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
103 const QModelIndex &sourceParent) const
106 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
107 QString stationName = sourceModel()->data(i).toString();
108 if (m_filterRecentOnly) {
109 acceptable = m_stations.contains(stationName);
113 return acceptable && stationName.contains(filterRegExp());
116 void StationListProxyModel::setRecentOnlyFilter(bool activation)
118 m_filterRecentOnly = activation;
121 QString StationListProxyModel::searchPattern() const
123 return m_searchPattern;
126 void StationListProxyModel::setSearchPattern(const QString &pattern)
128 m_searchPattern = pattern;
129 setFilterFixedString(m_searchPattern);
130 qDebug() << "set Search pattern to" << pattern;
133 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
135 return m_sortingMode;
138 void StationListProxyModel::setSortingMode(SortingMode mode)
140 if (mode != m_sortingMode) {
142 forceSortingMode(mode);
145 Settings *settings = Settings::instance();
146 settings->setStationListSortingMode(m_sortingMode);
148 emit sortingModeChanged(mode);
151 void StationListProxyModel::forceSortingMode(SortingMode mode)
153 m_sortingMode = mode;
154 setRecentOnlyFilter(false);
157 case StationListProxyModel::AlphaSorting:
158 setSortRole(Qt::DisplayRole);
160 case StationListProxyModel::DistanceSorting:
161 setSortRole(StationListModel::PositionRole);
163 case StationListProxyModel::RecentUsageSorting:
164 setRecentOnlyFilter(true);
169 if (mode == StationListProxyModel::DistanceSorting) {
170 if (positionInfoSource) {
171 positionInfoSource->startUpdates();
174 if (positionInfoSource) {
175 positionInfoSource->stopUpdates();
182 void StationListProxyModel::updatePosition(const QGeoPositionInfo &update)
184 qDebug() << "Position update received" << update;
185 if (update.isValid()) {
186 QGeoCoordinate newPosition = update.coordinate();
187 if (newPosition.distanceTo(m_here) > 50.0) {
188 setUserPosition(update.coordinate());