REfactored DataProvider method names
[quandoparte] / application / stationlistproxymodel.cpp
index 13cc18e..4adc370 100644 (file)
@@ -1,3 +1,24 @@
+/*
+
+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 "stationlistmodel.h"
 
@@ -11,8 +32,15 @@ Q_DECLARE_METATYPE(QGeoCoordinate)
 StationListProxyModel::StationListProxyModel(QObject *parent) :
     QSortFilterProxyModel(parent),
     m_here(44.5, 9.0),
+    m_sortingMode(NoSorting),
     m_filterRecentOnly(false)
 {
+    QHash<int, QByteArray> roles;
+    roles[StationListModel::PositionRole] = "position";
+    setRoleNames(roles);
+
+    setFilterCaseSensitivity(Qt::CaseInsensitive);
+    setSortCaseSensitivity(Qt::CaseInsensitive);
 }
 
 bool StationListProxyModel::lessThan(const QModelIndex &left,
@@ -44,14 +72,62 @@ void StationListProxyModel::setRecentStations(const QStringList &stations)
 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) {
-        QModelIndex i = sourceModel()->index(sourceRow, 0, sourceParent);
-        return m_stations.contains(sourceModel()->data(i).toString());
+        acceptable =  m_stations.contains(stationName);
+    } else {
+        acceptable = true;
     }
-    return true;
+    return acceptable && stationName.contains(filterRegExp());
+}
+
+void StationListProxyModel::setRecentOnlyFilter(bool activation)
+{
+    m_filterRecentOnly = activation;
 }
 
-void StationListProxyModel::setRecentOnlyFilter(bool)
+QString StationListProxyModel::searchPattern() const
 {
-    m_filterRecentOnly = true;
+    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) {
+        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;
+        }
+        invalidate();
+        sort(0);
+        emit sortingModeChanged(mode);
+    }
 }