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),
35 m_sortingMode(NoSorting),
36 m_filterRecentOnly(false)
38 QHash<int, QByteArray> roles;
39 roles[StationListModel::PositionRole] = "position";
42 setFilterCaseSensitivity(Qt::CaseInsensitive);
43 setSortCaseSensitivity(Qt::CaseInsensitive);
46 bool StationListProxyModel::lessThan(const QModelIndex &left,
47 const QModelIndex &right) const
49 int role = sortRole();
51 if (role == StationListModel::PositionRole) {
52 QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
53 QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
54 return first.distanceTo(m_here) < second.distanceTo(m_here);
56 return QString::compare(left.data(role).toString(),
57 right.data(role).toString(),
58 sortCaseSensitivity()) < 0;
62 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
67 void StationListProxyModel::setRecentStations(const QStringList &stations)
69 m_stations = stations;
72 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
73 const QModelIndex &sourceParent) const
76 QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
77 QString stationName = sourceModel()->data(i).toString();
78 if (m_filterRecentOnly) {
79 acceptable = m_stations.contains(stationName);
83 return acceptable && stationName.contains(filterRegExp());
86 void StationListProxyModel::setRecentOnlyFilter(bool activation)
88 m_filterRecentOnly = activation;
91 QString StationListProxyModel::searchPattern() const
93 return m_searchPattern;
96 void StationListProxyModel::setSearchPattern(const QString &pattern)
98 m_searchPattern = pattern;
99 setFilterFixedString(m_searchPattern);
100 qDebug() << "set Search pattern to" << pattern;
103 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
105 return m_sortingMode;
108 void StationListProxyModel::setSortingMode(SortingMode mode)
110 if (mode != m_sortingMode) {
111 qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
112 m_sortingMode = mode;
113 setRecentOnlyFilter(false);
116 case StationListProxyModel::AlphaSorting:
117 setSortRole(Qt::DisplayRole);
119 case StationListProxyModel::DistanceSorting:
120 setSortRole(StationListModel::PositionRole);
122 case StationListProxyModel::RecentUsageSorting:
123 setRecentOnlyFilter(true);
125 case StationListProxyModel::NoSorting:
131 emit sortingModeChanged(mode);