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 bool leftIsFavorite = left.data(StationListModel::FavoriteIndicatorRole).toBool();
72 bool rightIsFavorite = right.data(StationListModel::FavoriteIndicatorRole).toBool();
73 if (leftIsFavorite && !rightIsFavorite) {
75 } else if (rightIsFavorite && !leftIsFavorite) {
78 return QString::compare(left.data(role).toString(),
79 right.data(role).toString(),
80 sortCaseSensitivity()) < 0;
85 void StationListProxyModel::setUserPosition(const QGeoCoordinate &pos)
87 qDebug() << "Position is now" << pos;
89 if (sortingMode() == StationListProxyModel::DistanceSorting) {
94 void StationListProxyModel::setRecentStations(const QStringList &stations)
96 qDebug() << "Recent stations are now" << stations;
97 m_stations = stations;
98 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
103 void StationListProxyModel::updateRecentStations(void)
105 Settings *settings = Settings::instance();
106 setRecentStations(settings->recentStations());
109 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
110 const QModelIndex &sourceParent) const
113 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
114 QString stationName = sourceModel()->data(i).toString();
115 if (m_filterRecentOnly) {
116 acceptable = m_stations.contains(stationName);
120 return acceptable && stationName.contains(filterRegExp());
123 void StationListProxyModel::setRecentOnlyFilter(bool activation)
125 m_filterRecentOnly = activation;
128 QString StationListProxyModel::searchPattern() const
130 return m_searchPattern;
133 void StationListProxyModel::setSearchPattern(const QString &pattern)
135 m_searchPattern = pattern;
136 setFilterFixedString(m_searchPattern);
137 qDebug() << "set Search pattern to" << pattern;
140 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
142 return m_sortingMode;
145 void StationListProxyModel::setSortingMode(SortingMode mode)
147 if (mode != m_sortingMode) {
149 forceSortingMode(mode);
152 Settings *settings = Settings::instance();
153 settings->setStationListSortingMode(m_sortingMode);
155 emit sortingModeChanged(mode);
158 void StationListProxyModel::forceSortingMode(SortingMode mode)
160 m_sortingMode = mode;
161 setRecentOnlyFilter(false);
164 case StationListProxyModel::AlphaSorting:
165 setSortRole(Qt::DisplayRole);
167 case StationListProxyModel::DistanceSorting:
168 setSortRole(StationListModel::PositionRole);
170 case StationListProxyModel::RecentUsageSorting:
171 setRecentOnlyFilter(true);
176 if (mode == StationListProxyModel::DistanceSorting) {
177 if (positionInfoSource) {
178 positionInfoSource->startUpdates();
181 if (positionInfoSource) {
182 positionInfoSource->stopUpdates();
189 void StationListProxyModel::updatePosition(const QGeoPositionInfo &update)
191 qDebug() << "Position update received" << update;
192 if (update.isValid()) {
193 QGeoCoordinate newPosition = update.coordinate();
194 if (newPosition.distanceTo(m_here) > 50.0) {
195 setUserPosition(update.coordinate());