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