maplocationmodel: store the coordinate as QGeoCoordinate

Instead of maintaining a seperate latitude/longitude values
in C++ and passing them to QML separatelly, pass them as a QGeoCoordinate.

This reduces the number of model "roles" and also prevents the creations
of extra objects in QML (e.g. via QtPositioning.coordinate(..)).

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
Lubomir I. Ivanov 2017-07-17 22:36:55 +03:00 committed by Dirk Hohndel
parent d783984443
commit f44645b6fe
3 changed files with 11 additions and 16 deletions

View file

@ -37,7 +37,7 @@ Item {
delegate: MapQuickItem { delegate: MapQuickItem {
anchorPoint.x: 0 anchorPoint.x: 0
anchorPoint.y: mapItemImage.height anchorPoint.y: mapItemImage.height
coordinate: QtPositioning.coordinate(latitude, longitude) coordinate: model.coordinate
sourceItem: Image { id: mapItemImage; source: "qrc:///mapwidget-marker-image" } sourceItem: Image { id: mapItemImage; source: "qrc:///mapwidget-marker-image" }
} }
} }

View file

@ -5,18 +5,16 @@ MapLocation::MapLocation()
{ {
} }
MapLocation::MapLocation(qreal latitude, qreal longitude) : MapLocation::MapLocation(QGeoCoordinate coord) :
m_latitude(latitude), m_longitude(longitude) m_coordinate(coord)
{ {
} }
QVariant MapLocation::getRole(int role) const QVariant MapLocation::getRole(int role) const
{ {
switch (role) { switch (role) {
case Roles::RoleLatitude: case Roles::RoleCoordinate:
return m_latitude; return QVariant::fromValue(m_coordinate);
case Roles::RoleLongitude:
return m_longitude;
default: default:
return QVariant(); return QVariant();
} }
@ -24,8 +22,7 @@ QVariant MapLocation::getRole(int role) const
MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent) MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent)
{ {
m_roles[MapLocation::Roles::RoleLatitude] = "latitude"; m_roles[MapLocation::Roles::RoleCoordinate] = "coordinate";
m_roles[MapLocation::Roles::RoleLongitude] = "longitude";
} }
MapLocationModel::~MapLocationModel() MapLocationModel::~MapLocationModel()

View file

@ -7,27 +7,25 @@
#include <QHash> #include <QHash>
#include <QByteArray> #include <QByteArray>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QGeoCoordinate>
class MapLocation : public QObject class MapLocation : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal latitude MEMBER m_latitude) Q_PROPERTY(QGeoCoordinate coordinate MEMBER m_coordinate)
Q_PROPERTY(qreal longitude MEMBER m_longitude)
public: public:
explicit MapLocation(); explicit MapLocation();
explicit MapLocation(qreal lat, qreal lng); explicit MapLocation(QGeoCoordinate);
QVariant getRole(int role) const; QVariant getRole(int role) const;
enum Roles { enum Roles {
RoleLatitude = Qt::UserRole + 1, RoleCoordinate = Qt::UserRole + 1,
RoleLongitude
}; };
private: private:
qreal m_latitude; QGeoCoordinate m_coordinate;
qreal m_longitude;
}; };
class MapLocationModel : public QAbstractListModel class MapLocationModel : public QAbstractListModel