4adc370262c58ccbe2a1f55f0f801b5d5b42571f
[quandoparte] / application / stationlistproxymodel.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
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.
9
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.
14
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.
19
20 */
21
22 #include "stationlistproxymodel.h"
23 #include "stationlistmodel.h"
24
25 #include <QDebug>
26 #include <QGeoCoordinate>
27
28 QTM_USE_NAMESPACE
29
30 Q_DECLARE_METATYPE(QGeoCoordinate)
31
32 StationListProxyModel::StationListProxyModel(QObject *parent) :
33     QSortFilterProxyModel(parent),
34     m_here(44.5, 9.0),
35     m_sortingMode(NoSorting),
36     m_filterRecentOnly(false)
37 {
38     QHash<int, QByteArray> roles;
39     roles[StationListModel::PositionRole] = "position";
40     setRoleNames(roles);
41
42     setFilterCaseSensitivity(Qt::CaseInsensitive);
43     setSortCaseSensitivity(Qt::CaseInsensitive);
44 }
45
46 bool StationListProxyModel::lessThan(const QModelIndex &left,
47                                      const QModelIndex &right) const
48 {
49     int role = sortRole();
50
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);
55     } else {
56         return QString::compare(left.data(role).toString(),
57                                 right.data(role).toString(),
58                                 sortCaseSensitivity()) < 0;
59     }
60 }
61
62 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
63 {
64     m_here = pos;
65 }
66
67 void StationListProxyModel::setRecentStations(const QStringList &stations)
68 {
69     m_stations = stations;
70 }
71
72 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
73                                              const QModelIndex &sourceParent) const
74 {
75     bool acceptable;
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);
80     } else {
81         acceptable = true;
82     }
83     return acceptable && stationName.contains(filterRegExp());
84 }
85
86 void StationListProxyModel::setRecentOnlyFilter(bool activation)
87 {
88     m_filterRecentOnly = activation;
89 }
90
91 QString StationListProxyModel::searchPattern() const
92 {
93     return m_searchPattern;
94 }
95
96 void StationListProxyModel::setSearchPattern(const QString &pattern)
97 {
98     m_searchPattern = pattern;
99     setFilterFixedString(m_searchPattern);
100     qDebug() << "set Search pattern to" << pattern;
101 }
102
103 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
104 {
105     return m_sortingMode;
106 }
107
108 void StationListProxyModel::setSortingMode(SortingMode mode)
109 {
110     if (mode != m_sortingMode) {
111         qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
112         m_sortingMode = mode;
113         setRecentOnlyFilter(false);
114
115         switch (mode) {
116         case StationListProxyModel::AlphaSorting:
117             setSortRole(Qt::DisplayRole);
118             break;
119         case StationListProxyModel::DistanceSorting:
120             setSortRole(StationListModel::PositionRole);
121             break;
122         case StationListProxyModel::RecentUsageSorting:
123             setRecentOnlyFilter(true);
124             break;
125         case StationListProxyModel::NoSorting:
126         default:
127             break;
128         }
129         invalidate();
130         sort(0);
131         emit sortingModeChanged(mode);
132     }
133 }