X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=application%2Fstationlistproxymodel.cpp;h=fe613c2b5b23d78954bf7bac50cc9f42df12c6fc;hb=34fab52a2172a66c8bc5bc5f379dbeb57c865934;hp=9b38b16d0689da97a9c81bb51a72d1547ef2f554;hpb=61d204d6b9510ee35840f7bd638ebd2a1c00d42d;p=quandoparte diff --git a/application/stationlistproxymodel.cpp b/application/stationlistproxymodel.cpp index 9b38b16..fe613c2 100644 --- a/application/stationlistproxymodel.cpp +++ b/application/stationlistproxymodel.cpp @@ -1,37 +1,202 @@ +/* + +Copyright (C) 2011 Luciano Montanaro + +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 +#ifndef TARGET_PLATFORM_SAILFISH #include QTM_USE_NAMESPACE Q_DECLARE_METATYPE(QGeoCoordinate) +#endif StationListProxyModel::StationListProxyModel(QObject *parent) : QSortFilterProxyModel(parent), - m_here(44.5, 9.0) +#ifndef TARGET_PLATFORM_SAILFISH + positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)), + m_here(44.5, 9.0), +#endif + m_filterRecentOnly(false) { + 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, const QModelIndex &right) const +bool StationListProxyModel::lessThan(const QModelIndex &left, + const QModelIndex &right) const { int role = sortRole(); if (role == StationListModel::PositionRole) { +#ifdef TARGET_PLATFORM_SAILFISH + return 0; +#else QGeoCoordinate first = left.data(role).value(); QGeoCoordinate second = right.data(role).value(); - //qDebug() << "PositionRole" << left.data(Qt::DisplayRole) << first << right.data(Qt::DisplayRole) << second << "Here" << m_here; return first.distanceTo(m_here) < second.distanceTo(m_here); +#endif } else { - //qDebug() << "OtherRole"; return QString::compare(left.data(role).toString(), right.data(role).toString(), sortCaseSensitivity()) < 0; } } + +#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, + const QModelIndex &sourceParent) const +{ + bool acceptable; + QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent); + QString stationName = sourceModel()->data(i).toString(); + if (m_filterRecentOnly) { + acceptable = m_stations.contains(stationName); + } else { + acceptable = true; + } + return acceptable && stationName.contains(filterRegExp()); +} + +void StationListProxyModel::setRecentOnlyFilter(bool activation) +{ + m_filterRecentOnly = activation; +} + +QString StationListProxyModel::searchPattern() const +{ + return m_searchPattern; +} + +void StationListProxyModel::setSearchPattern(const QString &pattern) +{ + m_searchPattern = pattern; + setFilterFixedString(m_searchPattern); + qDebug() << "set Search pattern to" << pattern; +} + +StationListProxyModel::SortingMode StationListProxyModel::sortingMode() +{ + return m_sortingMode; +} + +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