app.cpp \
stationlistview.cpp \
keypressforwarder.cpp \
- stationlistmodel.cpp
+ stationlistmodel.cpp \
+ stationlistproxymodel.cpp
HEADERS += \
settingsdialog.h \
app.h \
stationlistview.h \
keypressforwarder.h \
- stationlistmodel.h
+ stationlistmodel.h \
+ stationlistproxymodel.h
FORMS += \
settingsdialog.ui \
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()) {
{
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);
--- /dev/null
+#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;
+}
--- /dev/null
+#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
#include "stationlistview.h"
#include "ui_stationlistview.h"
#include "stationlistmodel.h"
+#include "stationlistproxymodel.h"
#include "keypressforwarder.h"
#include "settingsdialog.h"
ui(new Ui::StationListView),
viewSelectionGroup(new QActionGroup(0)),
stationListModel(model),
- filterModel(new QSortFilterProxyModel(this)),
+ filterModel(new StationListProxyModel(this)),
keyPressForwarder(new KeyPressForwarder(this))
{
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;
void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
{
qDebug() << "Position update received" << update;
+ filterModel->setUserPosition(update.coordinate());
+ filterModel->invalidate();
+ filterModel->sort(0);
}
class StationView;
class StationListModel;
+class StationListProxyModel;
QTM_USE_NAMESPACE
Ui::StationListView *ui;
QActionGroup *viewSelectionGroup;
StationListModel *stationListModel;
- QSortFilterProxyModel *filterModel;
+ StationListProxyModel *filterModel;
KeyPressForwarder *keyPressForwarder;
};