FlightGear next
QmlPositionedModel.cxx
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include <QQmlEngine>
6
9
10#include "QmlPositioned.hxx"
11
17
18QmlPositionedModel::QmlPositionedModel(QObject* parent) : QAbstractListModel(parent),
20{
21}
22
24
26{
27 beginResetModel();
28 d->m_items = items;
29 endResetModel();
30 emit sizeChanged();
31}
32
34{
36 std::transform(runways.begin(), runways.end(), std::back_inserter(p), [](const FGRunwayRef& rwy) {
37 return static_cast<FGPositioned*>(rwy.get());
38 });
39 setValues(p);
40}
41
43{
45 std::transform(parks.begin(), parks.end(), std::back_inserter(p), [](const FGParkingRef& pk) {
46 return static_cast<FGPositioned*>(pk.get());
47 });
48 setValues(p);
49}
50
52{
53 beginResetModel();
54 d->m_items.clear();
55 endResetModel();
56 emit sizeChanged();
57}
58
59int QmlPositionedModel::rowCount(const QModelIndex& parent) const
60{
61 Q_UNUSED(parent)
62 return d->m_items.size();
63}
64
65QHash<int, QByteArray> QmlPositionedModel::roleNames() const
66{
67 auto roles = QAbstractListModel::roleNames();
68 return roles;
69}
70
71QVariant QmlPositionedModel::data(const QModelIndex& m, int role) const
72{
73 auto pos = d->m_items.at(m.row());
74 if (role == Qt::DisplayRole) {
75 return QString::fromStdString(pos->ident());
76 }
77
78 return {};
79}
80
82{
83 auto it = std::find_if(d->m_items.begin(), d->m_items.end(), [qpos](const FGPositionedRef& pos) {
84 return pos == qpos->inner();
85 });
86
87 if (it == d->m_items.end()) {
88 return -1;
89 }
90
91 return static_cast<int>(std::distance(d->m_items.begin(), it));
92}
93
95{
96 if ((index < 0) || (index >= d->m_items.size())) {
97 return nullptr;
98 }
99
100 auto qp = new QmlPositioned(d->m_items.at(index));
101 QQmlEngine::setObjectOwnership(qp, QQmlEngine::JavaScriptOwnership);
102 return qp;
103}
104
106{
107 return d->m_items.empty();
108}
#define p(x)
std::vector< FGRunwayRef > FGRunwayList
std::vector< FGParkingRef > FGParkingList
SGSharedPtr< FGRunway > FGRunwayRef
SGSharedPtr< FGParking > FGParkingRef
SGSharedPtr< FGPositioned > FGPositionedRef
Definition airways.hxx:30
int rowCount(const QModelIndex &parent) const override
Q_INVOKABLE int indexOf(QmlPositioned *pos) const
QmlPositionedModel(QObject *parent=nullptr)
Q_INVOKABLE QmlPositioned * itemAt(int index) const
void setValues(const FGPositionedList &posItems)
QHash< int, QByteArray > roleNames() const override
~QmlPositionedModel() override
QVariant data(const QModelIndex &m, int role) const override
std::vector< FGPositionedRef > FGPositionedList