+/*
+
+Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; see the file COPYING. If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.
+
+*/
+
#include "stationlistproxymodel.h"
+
+#include "settings.h"
#include "stationlistmodel.h"
#include <QDebug>
+#ifndef TARGET_PLATFORM_SAILFISH
#include <QGeoCoordinate>
QTM_USE_NAMESPACE
Q_DECLARE_METATYPE(QGeoCoordinate)
+#endif
StationListProxyModel::StationListProxyModel(QObject *parent) :
QSortFilterProxyModel(parent),
+#ifndef TARGET_PLATFORM_SAILFISH
+ positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
m_here(44.5, 9.0),
- m_sortingMode(NoSorting),
+#endif
m_filterRecentOnly(false)
{
- QHash<int, QByteArray> roles;
- roles[StationListModel::PositionRole] = "position";
- setRoleNames(roles);
-
+ Settings *settings = Settings::instance();
+ forceSortingMode(settings->stationListSortingMode());
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
+ setDynamicSortFilter(true);
+#ifndef TARGET_PLATFORM_SAILFISH
+ if (positionInfoSource) {
+ qDebug() << "position info source available";
+ connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
+ SLOT(updatePosition(QGeoPositionInfo)));
+ positionInfoSource->setUpdateInterval(5000);
+ } else {
+ qDebug() << "No position info source available";
+ }
+#endif
+ connect(settings, SIGNAL(recentStationsChanged()),
+ this, SLOT(updateRecentStations()));
+ updateRecentStations();
}
bool StationListProxyModel::lessThan(const QModelIndex &left,
int role = sortRole();
if (role == StationListModel::PositionRole) {
+#ifdef TARGET_PLATFORM_SAILFISH
+ return 0;
+#else
QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
return first.distanceTo(m_here) < second.distanceTo(m_here);
+#endif
} else {
return QString::compare(left.data(role).toString(),
right.data(role).toString(),
}
}
+
+#ifndef TARGET_PLATFORM_SAILFISH
void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
{
+ qDebug() << "Position is now" << pos;
m_here = pos;
+ if (sortingMode() == StationListProxyModel::DistanceSorting) {
+ invalidate();
+ }
}
+#endif
void StationListProxyModel::setRecentStations(const QStringList &stations)
{
+ qDebug() << "Recent stations are now" << stations;
m_stations = stations;
+ if (sortingMode() == StationListProxyModel::RecentUsageSorting) {
+ invalidate();
+ }
+}
+
+void StationListProxyModel::updateRecentStations(void)
+{
+ Settings *settings = Settings::instance();
+ setRecentStations(settings->recentStations());
}
bool StationListProxyModel::filterAcceptsRow(int sourceRow,
void StationListProxyModel::setSortingMode(SortingMode mode)
{
if (mode != m_sortingMode) {
- qDebug() << "setSorting Mode" << mode << m_sortingMode << "called";
- m_sortingMode = mode;
- setRecentOnlyFilter(false);
-
- switch (mode) {
- case StationListProxyModel::AlphaSorting:
- setSortRole(Qt::DisplayRole);
- break;
- case StationListProxyModel::DistanceSorting:
- setSortRole(StationListModel::PositionRole);
- break;
- case StationListProxyModel::RecentUsageSorting:
- setRecentOnlyFilter(true);
- break;
- case StationListProxyModel::NoSorting:
- default:
- break;
+ beginResetModel();
+ forceSortingMode(mode);
+ endResetModel();
+ }
+ Settings *settings = Settings::instance();
+ settings->setStationListSortingMode(m_sortingMode);
+
+ emit sortingModeChanged(mode);
+}
+
+void StationListProxyModel::forceSortingMode(SortingMode mode)
+{
+ m_sortingMode = mode;
+ setRecentOnlyFilter(false);
+
+ switch (mode) {
+ case StationListProxyModel::AlphaSorting:
+ setSortRole(Qt::DisplayRole);
+ break;
+ case StationListProxyModel::DistanceSorting:
+ setSortRole(StationListModel::PositionRole);
+ break;
+ case StationListProxyModel::RecentUsageSorting:
+ setRecentOnlyFilter(true);
+ break;
+ default:
+ break;
+ }
+#ifndef TARGET_PLATFORM_SAILFISH
+ if (mode == StationListProxyModel::DistanceSorting) {
+ positionInfoSource->startUpdates();
+ } else {
+ positionInfoSource->stopUpdates();
+ }
+#endif
+ invalidate();
+ sort(0);
+}
+
+#ifndef TARGET_PLATFORM_SAILFISH
+void StationListProxyModel::updatePosition(const QtMobility::QGeoPositionInfo &update)
+{
+ qDebug() << "Position update received" << update;
+ if (update.isValid()) {
+ QGeoCoordinate newPosition = update.coordinate();
+ if (newPosition.distanceTo(m_here) > 50.0) {
+ setUserPosition(update.coordinate());
+ invalidate();
+ sort(0);
}
- invalidate();
- sort(0);
- emit sortingModeChanged(mode);
}
}
+#endif