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 QHash<int, QByteArray> roles;
41 roles[StationListModel::PositionRole] = "position";
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)));
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;
77 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
79 qDebug() << "Position is now" << pos;
81 if (sortingMode() == StationListProxyModel::DistanceSorting) {
86 void StationListProxyModel::setRecentStations(const QStringList &stations)
88 qDebug() << "Recent stations are now" << stations;
89 m_stations = stations;
90 if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
95 void StationListProxyModel::updateRecentStations(void)
97 Settings *settings = Settings::instance();
98 setRecentStations(settings->recentStations());
101 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
102 const QModelIndex &sourceParent) const
105 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
106 QString stationName = sourceModel()->data(i).toString();
107 if (m_filterRecentOnly) {
108 acceptable = m_stations.contains(stationName);
112 return acceptable && stationName.contains(filterRegExp());
115 void StationListProxyModel::setRecentOnlyFilter(bool activation)
117 m_filterRecentOnly = activation;
120 QString StationListProxyModel::searchPattern() const
122 return m_searchPattern;
125 void StationListProxyModel::setSearchPattern(const QString &pattern)
127 m_searchPattern = pattern;
128 setFilterFixedString(m_searchPattern);
129 qDebug() << "set Search pattern to" << pattern;
132 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
134 return m_sortingMode;
137 void StationListProxyModel::setSortingMode(SortingMode mode)
139 if (mode != m_sortingMode) {
141 forceSortingMode(mode);
144 Settings *settings = Settings::instance();
145 settings->setStationListSortingMode(m_sortingMode);
147 emit sortingModeChanged(mode);
150 void StationListProxyModel::forceSortingMode(SortingMode mode)
152 m_sortingMode = mode;
153 setRecentOnlyFilter(false);
156 case StationListProxyModel::AlphaSorting:
157 setSortRole(Qt::DisplayRole);
159 case StationListProxyModel::DistanceSorting:
160 setSortRole(StationListModel::PositionRole);
162 case StationListProxyModel::RecentUsageSorting:
163 setRecentOnlyFilter(true);
168 if (mode == StationListProxyModel::DistanceSorting) {
169 positionInfoSource->startUpdates();
171 positionInfoSource->stopUpdates();
177 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
179 qDebug() << "Position update received" << update;
180 setUserPosition(update.coordinate());