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 m_sortingMode = settings->stationListSortingMode();
46 setFilterCaseSensitivity(Qt::CaseInsensitive);
47 setSortCaseSensitivity(Qt::CaseInsensitive);
48 if (positionInfoSource) {
49 qDebug() << "position info source available";
50 connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
51 SLOT(updatePosition(QGeoPositionInfo)));
53 qDebug() << "No position info source available";
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)
78 void StationListProxyModel::setRecentStations(const QStringList &stations)
80 m_stations = stations;
83 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
84 const QModelIndex &sourceParent) const
87 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
88 QString stationName = sourceModel()->data(i).toString();
89 if (m_filterRecentOnly) {
90 acceptable = m_stations.contains(stationName);
94 return acceptable && stationName.contains(filterRegExp());
97 void StationListProxyModel::setRecentOnlyFilter(bool activation)
99 m_filterRecentOnly = activation;
102 QString StationListProxyModel::searchPattern() const
104 return m_searchPattern;
107 void StationListProxyModel::setSearchPattern(const QString &pattern)
109 m_searchPattern = pattern;
110 setFilterFixedString(m_searchPattern);
111 qDebug() << "set Search pattern to" << pattern;
114 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
116 return m_sortingMode;
119 void StationListProxyModel::setSortingMode(SortingMode mode)
121 if (mode != m_sortingMode) {
122 qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
123 m_sortingMode = mode;
124 setRecentOnlyFilter(false);
127 case StationListProxyModel::AlphaSorting:
128 setSortRole(Qt::DisplayRole);
130 case StationListProxyModel::DistanceSorting:
131 setSortRole(StationListModel::PositionRole);
133 case StationListProxyModel::RecentUsageSorting:
134 setRecentOnlyFilter(true);
139 if (mode == StationListProxyModel::DistanceSorting) {
140 positionInfoSource->startUpdates();
142 positionInfoSource->stopUpdates();
147 Settings *settings = Settings::instance();
148 settings->setStationListSortingMode(m_sortingMode);
150 emit sortingModeChanged(mode);
154 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
156 qDebug() << "Position update received" << update;
157 setUserPosition(update.coordinate());