fe613c2b5b23d78954bf7bac50cc9f42df12c6fc
[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
24 #include "settings.h"
25 #include "stationlistmodel.h"
26
27 #include <QDebug>
28 #ifndef TARGET_PLATFORM_SAILFISH
29 #include <QGeoCoordinate>
30
31 QTM_USE_NAMESPACE
32
33 Q_DECLARE_METATYPE(QGeoCoordinate)
34 #endif
35
36 StationListProxyModel::StationListProxyModel(QObject *parent) :
37     QSortFilterProxyModel(parent),
38 #ifndef TARGET_PLATFORM_SAILFISH
39     positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
40     m_here(44.5, 9.0),
41 #endif
42     m_filterRecentOnly(false)
43 {
44     Settings *settings = Settings::instance();
45     forceSortingMode(settings->stationListSortingMode());
46     setFilterCaseSensitivity(Qt::CaseInsensitive);
47     setSortCaseSensitivity(Qt::CaseInsensitive);
48     setDynamicSortFilter(true);
49 #ifndef TARGET_PLATFORM_SAILFISH
50     if (positionInfoSource) {
51         qDebug() << "position info source available";
52         connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
53                 SLOT(updatePosition(QGeoPositionInfo)));
54         positionInfoSource->setUpdateInterval(5000);
55     } else {
56         qDebug() << "No position info source available";
57     }
58 #endif
59     connect(settings, SIGNAL(recentStationsChanged()),
60             this, SLOT(updateRecentStations()));
61     updateRecentStations();
62 }
63
64 bool StationListProxyModel::lessThan(const QModelIndex &left,
65                                      const QModelIndex &right) const
66 {
67     int role = sortRole();
68
69     if (role == StationListModel::PositionRole) {
70 #ifdef TARGET_PLATFORM_SAILFISH
71         return 0;
72 #else
73         QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
74         QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
75        return first.distanceTo(m_here) < second.distanceTo(m_here);
76 #endif
77     } else {
78         return QString::compare(left.data(role).toString(),
79                                 right.data(role).toString(),
80                                 sortCaseSensitivity()) < 0;
81     }
82 }
83
84
85 #ifndef TARGET_PLATFORM_SAILFISH
86 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
87 {
88     qDebug() << "Position is now" << pos;
89     m_here = pos;
90     if (sortingMode() == StationListProxyModel::DistanceSorting) {
91         invalidate();
92     }
93 }
94 #endif
95
96 void StationListProxyModel::setRecentStations(const QStringList &stations)
97 {
98     qDebug() << "Recent stations are now" << stations;
99     m_stations = stations;
100     if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
101         invalidate();
102     }
103 }
104
105 void StationListProxyModel::updateRecentStations(void)
106 {
107     Settings *settings = Settings::instance();
108     setRecentStations(settings->recentStations());
109 }
110
111 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
112                                              const QModelIndex &sourceParent) const
113 {
114     bool acceptable;
115     QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
116     QString stationName = sourceModel()->data(i).toString();
117     if (m_filterRecentOnly) {
118         acceptable =  m_stations.contains(stationName);
119     } else {
120         acceptable = true;
121     }
122     return acceptable && stationName.contains(filterRegExp());
123 }
124
125 void StationListProxyModel::setRecentOnlyFilter(bool activation)
126 {
127     m_filterRecentOnly = activation;
128 }
129
130 QString StationListProxyModel::searchPattern() const
131 {
132     return m_searchPattern;
133 }
134
135 void StationListProxyModel::setSearchPattern(const QString &pattern)
136 {
137     m_searchPattern = pattern;
138     setFilterFixedString(m_searchPattern);
139     qDebug() << "set Search pattern to" << pattern;
140 }
141
142 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
143 {
144     return m_sortingMode;
145 }
146
147 void StationListProxyModel::setSortingMode(SortingMode mode)
148 {
149     if (mode != m_sortingMode) {
150         beginResetModel();
151         forceSortingMode(mode);
152         endResetModel();
153     }
154     Settings *settings = Settings::instance();
155     settings->setStationListSortingMode(m_sortingMode);
156
157     emit sortingModeChanged(mode);
158 }
159
160 void StationListProxyModel::forceSortingMode(SortingMode mode)
161 {
162     m_sortingMode = mode;
163     setRecentOnlyFilter(false);
164
165     switch (mode) {
166     case StationListProxyModel::AlphaSorting:
167         setSortRole(Qt::DisplayRole);
168         break;
169     case StationListProxyModel::DistanceSorting:
170         setSortRole(StationListModel::PositionRole);
171         break;
172     case StationListProxyModel::RecentUsageSorting:
173         setRecentOnlyFilter(true);
174         break;
175     default:
176         break;
177     }
178 #ifndef TARGET_PLATFORM_SAILFISH
179     if (mode == StationListProxyModel::DistanceSorting) {
180         positionInfoSource->startUpdates();
181     } else {
182         positionInfoSource->stopUpdates();
183     }
184 #endif
185     invalidate();
186     sort(0);
187 }
188
189 #ifndef TARGET_PLATFORM_SAILFISH
190 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
191 {
192     qDebug() << "Position update received" << update;
193     if (update.isValid()) {
194         QGeoCoordinate newPosition = update.coordinate();
195         if (newPosition.distanceTo(m_here) > 50.0) {
196             setUserPosition(update.coordinate());
197             invalidate();
198             sort(0);
199         }
200     }
201 }
202 #endif