Read and save sorting state from the settings object
[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     m_sortingMode = settings->stationListSortingMode();
46     setFilterCaseSensitivity(Qt::CaseInsensitive);
47     setSortCaseSensitivity(Qt::CaseInsensitive);
48     if (positionInfoSource) {
49         qDebug() << "position info source available";
50         connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
51                 SLOT(updatePosition(QGeoPositionInfo)));
52     } else {
53         qDebug() << "No position info source available";
54     }
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     m_here = pos;
76 }
77
78 void StationListProxyModel::setRecentStations(const QStringList &stations)
79 {
80     m_stations = stations;
81 }
82
83 bool StationListProxyModel::filterAcceptsRow(int sourceRow,
84                                              const QModelIndex &sourceParent) const
85 {
86     bool acceptable;
87     QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
88     QString stationName = sourceModel()->data(i).toString();
89     if (m_filterRecentOnly) {
90         acceptable =  m_stations.contains(stationName);
91     } else {
92         acceptable = true;
93     }
94     return acceptable && stationName.contains(filterRegExp());
95 }
96
97 void StationListProxyModel::setRecentOnlyFilter(bool activation)
98 {
99     m_filterRecentOnly = activation;
100 }
101
102 QString StationListProxyModel::searchPattern() const
103 {
104     return m_searchPattern;
105 }
106
107 void StationListProxyModel::setSearchPattern(const QString &pattern)
108 {
109     m_searchPattern = pattern;
110     setFilterFixedString(m_searchPattern);
111     qDebug() << "set Search pattern to" << pattern;
112 }
113
114 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
115 {
116     return m_sortingMode;
117 }
118
119 void StationListProxyModel::setSortingMode(SortingMode mode)
120 {
121     if (mode != m_sortingMode) {
122         qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
123         m_sortingMode = mode;
124         setRecentOnlyFilter(false);
125
126         switch (mode) {
127         case StationListProxyModel::AlphaSorting:
128             setSortRole(Qt::DisplayRole);
129             break;
130         case StationListProxyModel::DistanceSorting:
131             setSortRole(StationListModel::PositionRole);
132             break;
133         case StationListProxyModel::RecentUsageSorting:
134             setRecentOnlyFilter(true);
135             break;
136         default:
137             break;
138         }
139         if (mode == StationListProxyModel::DistanceSorting) {
140             positionInfoSource->startUpdates();
141         } else {
142             positionInfoSource->stopUpdates();
143         }
144         invalidate();
145         sort(0);
146
147         Settings *settings = Settings::instance();
148         settings->setStationListSortingMode(m_sortingMode);
149
150         emit sortingModeChanged(mode);
151     }
152 }
153
154 void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
155 {
156     qDebug() << "Position update received" << update;
157     setUserPosition(update.coordinate());
158     invalidate();
159     sort(0);
160 }