From 32245b0a24047750585d4b20ed4fb198857e31e2 Mon Sep 17 00:00:00 2001 From: Luciano Montanaro Date: Sun, 5 Jun 2011 11:53:00 +0200 Subject: [PATCH] Added support for Geographical sorting At the moment the list is always geographically sorted, (and resorted periodically). Not for release. --- application/application.pro | 6 +++-- application/stationlistmodel.cpp | 2 +- application/stationlistmodel.h | 7 ++++-- application/stationlistproxymodel.cpp | 39 +++++++++++++++++++++++++++++++++ application/stationlistproxymodel.h | 25 +++++++++++++++++++++ application/stationlistview.cpp | 10 ++++++++- application/stationlistview.h | 3 ++- 7 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 application/stationlistproxymodel.cpp create mode 100644 application/stationlistproxymodel.h diff --git a/application/application.pro b/application/application.pro index 2e4ac46..34d6f55 100644 --- a/application/application.pro +++ b/application/application.pro @@ -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 \ diff --git a/application/stationlistmodel.cpp b/application/stationlistmodel.cpp index 05b9434..4c3bc54 100644 --- a/application/stationlistmodel.cpp +++ b/application/stationlistmodel.cpp @@ -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()) { diff --git a/application/stationlistmodel.h b/application/stationlistmodel.h index bc575cf..ea51f1b 100644 --- a/application/stationlistmodel.h +++ b/application/stationlistmodel.h @@ -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 // +#include + +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 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); + } 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 index 0000000..5aa4436 --- /dev/null +++ b/application/stationlistproxymodel.h @@ -0,0 +1,25 @@ +#ifndef STATIONLISTPROXYMODEL_H +#define STATIONLISTPROXYMODEL_H + +#include +#include + +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 diff --git a/application/stationlistview.cpp b/application/stationlistview.cpp index 3bbb9eb..76beb59 100644 --- a/application/stationlistview.cpp +++ b/application/stationlistview.cpp @@ -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); } diff --git a/application/stationlistview.h b/application/stationlistview.h index 88c6e27..95ab332 100644 --- a/application/stationlistview.h +++ b/application/stationlistview.h @@ -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; }; -- 1.7.9.5