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)));
50 qDebug() << "No position info source available";
52 connect(settings, SIGNAL(recentStationsChanged()),
53 this, SLOT(updateRecentStations()));
54 updateRecentStations();
57 bool StationListProxyModel::lessThan(const QModelIndex &left,
58 const QModelIndex &right) const
60 int role = sortRole();
62 if (role == StationListModel::PositionRole) {
63 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
64 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
65 return first.distanceTo(m_here) < second.distanceTo(m_here);
67 return QString::compare(left.data(role).toString(),
68 right.data(role).toString(),
69 sortCaseSensitivity()) < 0;
73 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
75 qDebug() << "Position is now" << pos;
77 if (sortingMode() == StationListProxyModel::DistanceSorting) {
82 void StationListProxyModel::setRecentStations(const QStringList &stations)
84 qDebug() << "Recent stations are now" << stations;
85 m_stations = stations;
86 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
91 void StationListProxyModel::updateRecentStations(void)
93 Settings *settings = Settings::instance();
94 setRecentStations(settings->recentStations());
97 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
98 const QModelIndex &sourceParent) const
101 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
102 QString stationName = sourceModel()->data(i).toString();
103 if (m_filterRecentOnly) {
104 acceptable = m_stations.contains(stationName);
108 return acceptable && stationName.contains(filterRegExp());
111 void StationListProxyModel::setRecentOnlyFilter(bool activation)
113 m_filterRecentOnly = activation;
116 QString StationListProxyModel::searchPattern() const
118 return m_searchPattern;
121 void StationListProxyModel::setSearchPattern(const QString &pattern)
123 m_searchPattern = pattern;
124 setFilterFixedString(m_searchPattern);
125 qDebug() << "set Search pattern to" << pattern;
128 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
130 return m_sortingMode;
133 void StationListProxyModel::setSortingMode(SortingMode mode)
135 if (mode != m_sortingMode) {
137 forceSortingMode(mode);
140 Settings *settings = Settings::instance();
141 settings->setStationListSortingMode(m_sortingMode);
143 emit sortingModeChanged(mode);
146 void StationListProxyModel::forceSortingMode(SortingMode mode)
148 m_sortingMode = mode;
149 setRecentOnlyFilter(false);
152 case StationListProxyModel::AlphaSorting:
153 setSortRole(Qt::DisplayRole);
155 case StationListProxyModel::DistanceSorting:
156 setSortRole(StationListModel::PositionRole);
158 case StationListProxyModel::RecentUsageSorting:
159 setRecentOnlyFilter(true);
164 if (mode == StationListProxyModel::DistanceSorting) {
165 positionInfoSource->startUpdates();
167 positionInfoSource->stopUpdates();
173 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
175 qDebug() << "Position update received" << update;
176 setUserPosition(update.coordinate());