Added support for Geographical sorting
authorLuciano Montanaro <mikelima@cirulla.net>
Sun, 5 Jun 2011 09:53:00 +0000 (11:53 +0200)
committerLuciano Montanaro <mikelima@cirulla.net>
Sun, 5 Jun 2011 09:53:00 +0000 (11:53 +0200)
At the moment the list is always geographically sorted, (and resorted
periodically). Not for release.

application/application.pro
application/stationlistmodel.cpp
application/stationlistmodel.h
application/stationlistproxymodel.cpp [new file with mode: 0644]
application/stationlistproxymodel.h [new file with mode: 0644]
application/stationlistview.cpp
application/stationlistview.h

index 2e4ac46..34d6f55 100644 (file)
@@ -21,7 +21,8 @@ SOURCES += main.cpp \
     app.cpp \
     stationlistview.cpp \
     keypressforwarder.cpp \
-    stationlistmodel.cpp
+    stationlistmodel.cpp \
+    stationlistproxymodel.cpp
 
 HEADERS += \
     settingsdialog.h \
@@ -29,7 +30,8 @@ HEADERS += \
     app.h \
     stationlistview.h \
     keypressforwarder.h \
-    stationlistmodel.h
+    stationlistmodel.h \
+    stationlistproxymodel.h
 
 FORMS += \
     settingsdialog.ui \
index 05b9434..4c3bc54 100644 (file)
@@ -101,7 +101,7 @@ void StationListModel::readPosElement(QStandardItem *item)
 
     QStringList coordinates = m_reader.readElementText().split(",");
     QGeoCoordinate pos = QGeoCoordinate(coordinates[0].toDouble(), coordinates[1].toDouble());
-    item->setData(QVariant::fromValue(pos));
+    item->setData(QVariant::fromValue(pos), PositionRole);
     qDebug() << "pos:" << pos;
     m_reader.readElementText();
     if (m_reader.isEndElement()) {
index bc575cf..ea51f1b 100644 (file)
@@ -11,10 +11,13 @@ class StationListModel : public QStandardItemModel
 {
     Q_OBJECT
 
+public:
     enum StationListRole {
-        PositionRole = Qt::UserRole
+        PositionRole = Qt::UserRole + 1, //< QGeoCoordinate - Station coordinate
+        StationIdRole, //< QString - Station Id (Precise name if the Display name is known to fail)
+        RecentIndicatorRole //<bool - If the station has been recently looked up
     };
-public:
+
     explicit StationListModel(QObject *parent = 0);
 
     bool load(const QString &filename);
diff --git a/application/stationlistproxymodel.cpp b/application/stationlistproxymodel.cpp
new file mode 100644 (file)
index 0000000..dd786bb
--- /dev/null
@@ -0,0 +1,39 @@
+#include "stationlistproxymodel.h"
+#include "stationlistmodel.h"
+
+#include <QDebug>
+#include <QGeoCoordinate>
+
+QTM_USE_NAMESPACE
+
+Q_DECLARE_METATYPE(QGeoCoordinate)
+
+StationListProxyModel::StationListProxyModel(QObject *parent) :
+    QSortFilterProxyModel(parent),
+    m_here(44.5, 9.0)
+{
+}
+
+bool StationListProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
+{
+    int role = sortRole();
+
+    qDebug() << "lessThan called";
+
+    if (role == StationListModel::PositionRole) {
+        QGeoCoordinate first = left.data(role).value<QGeoCoordinate>();
+        QGeoCoordinate second = right.data(role).value<QGeoCoordinate>();
+        qDebug() << "PositionRole" << left.data(Qt::DisplayRole) << first << right.data(Qt::DisplayRole) << second << "Here" << m_here;
+       return first.distanceTo(m_here) < second.distanceTo(m_here);
+    } else {
+        qDebug() << "OtherRole";
+        return QString::compare(left.data(role).toString(),
+                                right.data(role).toString(),
+                                sortCaseSensitivity()) < 0;
+    }
+}
+
+void StationListProxyModel::setUserPosition(const QtMobility::QGeoCoordinate &pos)
+{
+    m_here = pos;
+}
diff --git a/application/stationlistproxymodel.h b/application/stationlistproxymodel.h
new file mode 100644 (file)
index 0000000..5aa4436
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef STATIONLISTPROXYMODEL_H
+#define STATIONLISTPROXYMODEL_H
+
+#include <QSortFilterProxyModel>
+#include <QGeoCoordinate>
+
+QTM_USE_NAMESPACE
+
+class StationListProxyModel : public QSortFilterProxyModel
+{
+    Q_OBJECT
+
+public:
+    explicit StationListProxyModel(QObject *parent = 0);
+
+    void setUserPosition(const QGeoCoordinate &pos);
+
+protected:
+    virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
+
+private:
+    QGeoCoordinate m_here;
+};
+
+#endif // STATIONLISTPROXYMODEL_H
index 3bbb9eb..76beb59 100644 (file)
@@ -22,6 +22,7 @@ Boston, MA 02110-1301, USA.
 #include "stationlistview.h"
 #include "ui_stationlistview.h"
 #include "stationlistmodel.h"
+#include "stationlistproxymodel.h"
 #include "keypressforwarder.h"
 #include "settingsdialog.h"
 
@@ -36,7 +37,7 @@ StationListView::StationListView(StationListModel *model, QWidget *parent) :
     ui(new Ui::StationListView),
     viewSelectionGroup(new QActionGroup(0)),
     stationListModel(model),
-    filterModel(new QSortFilterProxyModel(this)),
+    filterModel(new StationListProxyModel(this)),
     keyPressForwarder(new KeyPressForwarder(this))
 
 {
@@ -64,8 +65,12 @@ StationListView::StationListView(StationListModel *model, QWidget *parent) :
             SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
     connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
             SLOT(handleFilterChanges(const QString &)));
+    filterModel->setSortRole(StationListModel::PositionRole);
+    //filterModel->setSortRole(Qt::DisplayRole);
+    filterModel->sort(0);
 }
 
+
 StationListView::~StationListView()
 {
     delete ui;
@@ -100,4 +105,7 @@ void StationListView::handleFilterChanges(const QString &filter)
 void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
 {
     qDebug() << "Position update received" << update;
+    filterModel->setUserPosition(update.coordinate());
+    filterModel->invalidate();
+    filterModel->sort(0);
 }
index 88c6e27..95ab332 100644 (file)
@@ -16,6 +16,7 @@ class KeyPressForwarder;
 
 class StationView;
 class StationListModel;
+class StationListProxyModel;
 
 QTM_USE_NAMESPACE
 
@@ -43,7 +44,7 @@ private:
     Ui::StationListView *ui;
     QActionGroup *viewSelectionGroup;
     StationListModel *stationListModel;
-    QSortFilterProxyModel *filterModel;
+    StationListProxyModel *filterModel;
     KeyPressForwarder *keyPressForwarder;
 };