Fix mergelist.xq script
[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     QHash<int, QByteArray> roles;
41     roles[StationListModel::PositionRole] = "position";
42     setRoleNames(roles);
43
44     Settings *settings = Settings::instance();
45     forceSortingMode(settings->stationListSortingMode());
46     setFilterCaseSensitivity(Qt::CaseInsensitive);
47     setSortCaseSensitivity(Qt::CaseInsensitive);
48     setDynamicSortFilter(true);
49     if (positionInfoSource) {
50         qDebug() << "position info source available";
51         connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
52                 SLOT(updatePosition(QGeoPositionInfo)));
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 void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
78 {
79     qDebug() << "Position is now" << pos;
80     m_here = pos;
81     if (sortingMode() == StationListProxyModel::DistanceSorting) {
82         invalidate();
83     }
84 }
85
86 void StationListProxyModel::setRecentStations(const QStringList &stations)
87 {
88     qDebug() << "Recent stations are now" << stations;
89     m_stations = stations;
90     if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
91         invalidate();
92     }
93 }
94
95 void StationListProxyModel::updateRecentStations(void)
96 {
97     Settings *settings = Settings::instance();
98     setRecentStations(settings->recentStations());
99 }
100
101 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
102                                              const QModelIndex &sourceParent) const
103 {
104     bool acceptable;
105     QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
106     QString stationName = sourceModel()->data(i).toString();
107     if (m_filterRecentOnly) {
108         acceptable =  m_stations.contains(stationName);
109     } else {
110         acceptable = true;
111     }
112     return acceptable && stationName.contains(filterRegExp());
113 }
114
115 void StationListProxyModel::setRecentOnlyFilter(bool activation)
116 {
117     m_filterRecentOnly = activation;
118 }
119
120 QString StationListProxyModel::searchPattern() const
121 {
122     return m_searchPattern;
123 }
124
125 void StationListProxyModel::setSearchPattern(const QString &pattern)
126 {
127     m_searchPattern = pattern;
128     setFilterFixedString(m_searchPattern);
129     qDebug() << "set Search pattern to" << pattern;
130 }
131
132 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
133 {
134     return m_sortingMode;
135 }
136
137 void StationListProxyModel::setSortingMode(SortingMode mode)
138 {
139     if (mode != m_sortingMode) {
140         beginResetModel();
141         forceSortingMode(mode);
142         endResetModel();
143     }
144     Settings *settings = Settings::instance();
145     settings->setStationListSortingMode(m_sortingMode);
146
147     emit sortingModeChanged(mode);
148 }
149
150 void StationListProxyModel::forceSortingMode(SortingMode mode)
151 {
152     m_sortingMode = mode;
153     setRecentOnlyFilter(false);
154
155     switch (mode) {
156     case StationListProxyModel::AlphaSorting:
157         setSortRole(Qt::DisplayRole);
158         break;
159     case StationListProxyModel::DistanceSorting:
160         setSortRole(StationListModel::PositionRole);
161         break;
162     case StationListProxyModel::RecentUsageSorting:
163         setRecentOnlyFilter(true);
164         break;
165     default:
166         break;
167     }
168     if (mode == StationListProxyModel::DistanceSorting) {
169         positionInfoSource->startUpdates();
170     } else {
171         positionInfoSource->stopUpdates();
172     }
173     invalidate();
174     sort(0);
175 }
176
177 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
178 {
179     qDebug() << "Position update received" << update;
180     setUserPosition(update.coordinate());
181     invalidate();
182     sort(0);
183 }