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 qRegisterMetaType<QGeoCoordinate>();
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";
58 connect(settings, SIGNAL(favoriteStationsChanged()),
59 this, SLOT(updateFavoriteStations()));
60 updateFavoriteStations();
61 connect(settings, SIGNAL(recentStationsChanged()),
62 this, SLOT(updateRecentStations()));
63 updateRecentStations();
66 Qt::ItemFlags StationListProxyModel::flags(const QModelIndex &index) const
68 return QSortFilterProxyModel::flags(index);
71 bool StationListProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
73 return QSortFilterProxyModel::setData(index, value, role);
76 bool StationListProxyModel::lessThan(const QModelIndex &left,
77 const QModelIndex &right) const
79 int role = sortRole();
81 if (role == StationListModel::PositionRole) {
82 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
83 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
84 return first.distanceTo(m_here) < second.distanceTo(m_here);
86 bool leftIsFavorite = left.data(StationListModel::FavoriteIndicatorRole).toBool();
87 bool rightIsFavorite = right.data(StationListModel::FavoriteIndicatorRole).toBool();
88 if (leftIsFavorite && !rightIsFavorite) {
90 } else if (rightIsFavorite && !leftIsFavorite) {
93 return QString::compare(left.data(role).toString(),
94 right.data(role).toString(),
95 sortCaseSensitivity()) < 0;
100 void StationListProxyModel::setUserPosition(const QGeoCoordinate &pos)
102 qDebug() << "Position is now" << pos;
104 if (sortingMode() == StationListProxyModel::DistanceSorting) {
109 void StationListProxyModel::setRecentStations(const QStringList &stations)
111 qDebug() << "Recent stations are now" << stations;
112 m_stations = stations;
113 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
118 void StationListProxyModel::updateRecentStations(void)
120 Settings *settings = Settings::instance();
121 setRecentStations(settings->recentStations());
124 void StationListProxyModel::setFavoriteStations(const QStringList &stations)
126 qDebug() << "Favorite stations are now" << stations;
127 if (sortingMode() == StationListProxyModel::AlphaSorting) {
132 void StationListProxyModel::updateFavoriteStations(void)
134 Settings *settings = Settings::instance();
135 setFavoriteStations(settings->favoriteStations());
138 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
139 const QModelIndex &sourceParent) const
142 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
143 QString stationName = sourceModel()->data(i).toString();
144 if (m_filterRecentOnly) {
145 acceptable = m_stations.contains(stationName);
149 return acceptable && stationName.contains(filterRegExp());
152 void StationListProxyModel::setRecentOnlyFilter(bool activation)
154 m_filterRecentOnly = activation;
157 QString StationListProxyModel::searchPattern() const
159 return m_searchPattern;
162 void StationListProxyModel::setSearchPattern(const QString &pattern)
164 m_searchPattern = pattern;
165 setFilterFixedString(m_searchPattern);
166 qDebug() << "set Search pattern to" << pattern;
169 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
171 return m_sortingMode;
174 void StationListProxyModel::setSortingMode(SortingMode mode)
176 if (mode != m_sortingMode) {
178 forceSortingMode(mode);
181 Settings *settings = Settings::instance();
182 settings->setStationListSortingMode(m_sortingMode);
184 emit sortingModeChanged(mode);
187 void StationListProxyModel::forceSortingMode(SortingMode mode)
189 m_sortingMode = mode;
190 setRecentOnlyFilter(false);
193 case StationListProxyModel::AlphaSorting:
194 setSortRole(Qt::DisplayRole);
196 case StationListProxyModel::DistanceSorting:
197 setSortRole(StationListModel::PositionRole);
199 case StationListProxyModel::RecentUsageSorting:
200 setRecentOnlyFilter(true);
205 if (mode == StationListProxyModel::DistanceSorting) {
206 if (positionInfoSource) {
207 positionInfoSource->startUpdates();
210 if (positionInfoSource) {
211 positionInfoSource->stopUpdates();
218 void StationListProxyModel::updatePosition(const QGeoPositionInfo &update)
220 qDebug() << "Position update received" << update;
221 if (update.isValid()) {
222 QGeoCoordinate newPosition = update.coordinate();
223 if (newPosition.distanceTo(m_here) > 50.0) {
224 setUserPosition(update.coordinate());