merge github
[apps/navigation.git] / app / markermodel.h
1 #ifndef MARKERMODEL_H
2 #define MARKERMODEL_H
3
4 #include <QAbstractListModel>
5 #include <QGeoCoordinate>
6
7 class MarkerModel : public QAbstractListModel
8 {
9     Q_OBJECT
10
11 public:
12     using QAbstractListModel::QAbstractListModel;
13     enum MarkerRoles{positionRole = Qt::UserRole + 1};
14
15     Q_INVOKABLE void addMarker(const QGeoCoordinate &coordinate){
16         beginInsertRows(QModelIndex(), rowCount(), rowCount());
17         m_coordinates.append(coordinate);
18         endInsertRows();
19     }
20
21     Q_INVOKABLE void removeMarker(){
22         beginResetModel();
23         m_coordinates.clear();
24         endResetModel();
25     }
26
27     int rowCount(const QModelIndex &parent = QModelIndex()) const override{
28         Q_UNUSED(parent)
29         return m_coordinates.count();
30     }
31
32     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override{
33         if (index.row() < 0 || index.row() >= m_coordinates.count())
34             return QVariant();
35         if(role== MarkerModel::positionRole)
36             return QVariant::fromValue(m_coordinates[index.row()]);
37         return QVariant();
38     }
39
40     QHash<int, QByteArray> roleNames() const{
41         QHash<int, QByteArray> roles;
42         roles[positionRole] = "position";
43         return roles;
44     }
45
46 private:
47     QList<QGeoCoordinate> m_coordinates;
48 };
49
50 #endif // MARKERMODEL_H