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