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>
30 #include <QGeoPositionInfo>
32 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
35 Q_DECLARE_METATYPE(QGeoCoordinate)
38 StationListProxyModel::StationListProxyModel(QObject *parent) :
39 QSortFilterProxyModel(parent),
40 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 if (positionInfoSource) {
50 qDebug() << "position info source available";
51 connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
52 SLOT(updatePosition(QGeoPositionInfo)));
53 positionInfoSource->setUpdateInterval(5000);
55 qDebug() << "No position info source available";
57 connect(settings, SIGNAL(favoriteStationsChanged()),
58 this, SLOT(updateFavoriteStations()));
59 updateFavoriteStations();
60 connect(settings, SIGNAL(recentStationsChanged()),
61 this, SLOT(updateRecentStations()));
62 updateRecentStations();
65 Qt::ItemFlags StationListProxyModel::flags(const QModelIndex &index) const
67 return QSortFilterProxyModel::flags(index);
70 bool StationListProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
72 return QSortFilterProxyModel::setData(index, value, role);
75 bool StationListProxyModel::lessThan(const QModelIndex &left,
76 const QModelIndex &right) const
78 int role = sortRole();
80 if (role == StationListModel::PositionRole) {
81 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
82 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
83 return first.distanceTo(m_here) < second.distanceTo(m_here);
85 bool leftIsFavorite = left.data(StationListModel::FavoriteIndicatorRole).toBool();
86 bool rightIsFavorite = right.data(StationListModel::FavoriteIndicatorRole).toBool();
87 if (leftIsFavorite && !rightIsFavorite) {
89 } else if (rightIsFavorite && !leftIsFavorite) {
92 return QString::compare(left.data(role).toString(),
93 right.data(role).toString(),
94 sortCaseSensitivity()) < 0;
99 void StationListProxyModel::setUserPosition(const QGeoCoordinate &pos)
101 qDebug() << "Position is now" << pos;
103 if (sortingMode() == StationListProxyModel::DistanceSorting) {
108 void StationListProxyModel::setRecentStations(const QStringList &stations)
110 qDebug() << "Recent stations are now" << stations;
111 m_stations = stations;
112 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
117 void StationListProxyModel::updateRecentStations(void)
119 Settings *settings = Settings::instance();
120 setRecentStations(settings->recentStations());
123 void StationListProxyModel::setFavoriteStations(const QStringList &stations)
125 qDebug() << "Favorite stations are now" << stations;
126 if (sortingMode() == StationListProxyModel::AlphaSorting) {
131 void StationListProxyModel::updateFavoriteStations(void)
133 Settings *settings = Settings::instance();
134 setFavoriteStations(settings->favoriteStations());
137 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
138 const QModelIndex &sourceParent) const
141 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
142 QString stationName = sourceModel()->data(i).toString();
143 if (m_filterRecentOnly) {
144 acceptable = m_stations.contains(stationName);
148 return acceptable && stationName.contains(filterRegExp());
151 void StationListProxyModel::setRecentOnlyFilter(bool activation)
153 m_filterRecentOnly = activation;
156 QString StationListProxyModel::searchPattern() const
158 return m_searchPattern;
161 void StationListProxyModel::setSearchPattern(const QString &pattern)
163 m_searchPattern = pattern;
164 setFilterFixedString(m_searchPattern);
165 qDebug() << "set Search pattern to" << pattern;
168 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
170 return m_sortingMode;
173 void StationListProxyModel::setSortingMode(SortingMode mode)
175 if (mode != m_sortingMode) {
177 forceSortingMode(mode);
180 Settings *settings = Settings::instance();
181 settings->setStationListSortingMode(m_sortingMode);
183 emit sortingModeChanged(mode);
186 void StationListProxyModel::forceSortingMode(SortingMode mode)
188 m_sortingMode = mode;
189 setRecentOnlyFilter(false);
192 case StationListProxyModel::AlphaSorting:
193 setSortRole(Qt::DisplayRole);
195 case StationListProxyModel::DistanceSorting:
196 setSortRole(StationListModel::PositionRole);
198 case StationListProxyModel::RecentUsageSorting:
199 setRecentOnlyFilter(true);
204 if (mode == StationListProxyModel::DistanceSorting) {
205 if (positionInfoSource) {
206 positionInfoSource->startUpdates();
209 if (positionInfoSource) {
210 positionInfoSource->stopUpdates();
217 void StationListProxyModel::updatePosition(const QGeoPositionInfo &update)
219 qDebug() << "Position update received" << update;
220 if (update.isValid()) {
221 QGeoCoordinate newPosition = update.coordinate();
222 if (newPosition.distanceTo(m_here) > 50.0) {
223 setUserPosition(update.coordinate());