Initial changes for sailfish port
[quandoparte] / application / stationlistproxymodel.cpp
index 77fd467..fe613c2 100644 (file)
@@ -1,24 +1,64 @@
+/*
+
+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),
+#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,
@@ -27,9 +67,13 @@ 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(),
@@ -37,14 +81,31 @@ bool StationListProxyModel::lessThan(const QModelIndex &left,
     }
 }
 
+
+#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,
@@ -75,6 +136,7 @@ void StationListProxyModel::setSearchPattern(const QString &pattern)
 {
     m_searchPattern = pattern;
     setFilterFixedString(m_searchPattern);
+    qDebug() << "set Search pattern to" << pattern;
 }
 
 StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
@@ -82,9 +144,59 @@ StationListProxyModel::SortingMode StationListProxyModel::sortingMode()
     return m_sortingMode;
 }
 
-void StationListProxyModel::setSortingMode(StationListProxyModel::SortingMode mode)
+void StationListProxyModel::setSortingMode(SortingMode mode)
+{
+    if (mode != m_sortingMode) {
+        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);
+        }
+    }
+}
+#endif