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"
23 #include "stationlistmodel.h"
26 #include <QGeoCoordinate>
30 Q_DECLARE_METATYPE(QGeoCoordinate)
32 StationListProxyModel::StationListProxyModel(QObject *parent) :
33 QSortFilterProxyModel(parent),
34 positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
36 m_sortingMode(AlphaSorting),
37 m_filterRecentOnly(false)
39 QHash<int, QByteArray> roles;
40 roles[StationListModel::PositionRole] = "position";
43 setFilterCaseSensitivity(Qt::CaseInsensitive);
44 setSortCaseSensitivity(Qt::CaseInsensitive);
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";
54 bool StationListProxyModel::lessThan(const QModelIndex &left,
55 const QModelIndex &right) const
57 int role = sortRole();
59 if (role == StationListModel::PositionRole) {
60 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
61 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
62 return first.distanceTo(m_here) < second.distanceTo(m_here);
64 return QString::compare(left.data(role).toString(),
65 right.data(role).toString(),
66 sortCaseSensitivity()) < 0;
70 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
75 void StationListProxyModel::setRecentStations(const QStringList &stations)
77 m_stations = stations;
80 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
81 const QModelIndex &sourceParent) const
84 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
85 QString stationName = sourceModel()->data(i).toString();
86 if (m_filterRecentOnly) {
87 acceptable = m_stations.contains(stationName);
91 return acceptable && stationName.contains(filterRegExp());
94 void StationListProxyModel::setRecentOnlyFilter(bool activation)
96 m_filterRecentOnly = activation;
99 QString StationListProxyModel::searchPattern() const
101 return m_searchPattern;
104 void StationListProxyModel::setSearchPattern(const QString &pattern)
106 m_searchPattern = pattern;
107 setFilterFixedString(m_searchPattern);
108 qDebug() << "set Search pattern to" << pattern;
111 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
113 return m_sortingMode;
116 void StationListProxyModel::setSortingMode(SortingMode mode)
118 if (mode != m_sortingMode) {
119 qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
120 m_sortingMode = mode;
121 setRecentOnlyFilter(false);
124 case StationListProxyModel::AlphaSorting:
125 setSortRole(Qt::DisplayRole);
127 case StationListProxyModel::DistanceSorting:
128 setSortRole(StationListModel::PositionRole);
130 case StationListProxyModel::RecentUsageSorting:
131 setRecentOnlyFilter(true);
136 if (mode == StationListProxyModel::DistanceSorting) {
137 positionInfoSource->startUpdates();
139 positionInfoSource->stopUpdates();
143 emit sortingModeChanged(mode);
147 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
149 qDebug() << "Position update received" << update;
150 setUserPosition(update.coordinate());