1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/
#include "AirspaceManager.h"
#include "AirspaceFlightPlanProvider.h"
#include <QQmlEngine>
//-----------------------------------------------------------------------------
AirspaceFlightAuthorization::AirspaceFlightAuthorization(QObject *parent)
: QObject(parent)
{
}
//-----------------------------------------------------------------------------
AirspaceFlightInfo::AirspaceFlightInfo(QObject *parent)
: QObject(parent)
{
}
//-----------------------------------------------------------------------------
AirspaceFlightPlanProvider::AirspaceFlightPlanProvider(QObject *parent)
: QObject(parent)
{
}
//-----------------------------------------------------------------------------
AirspaceFlightModel::AirspaceFlightModel(QObject *parent)
: QAbstractListModel(parent)
{
}
//-----------------------------------------------------------------------------
AirspaceFlightInfo*
AirspaceFlightModel::get(int index)
{
if (index < 0 || index >= _flightEntries.count()) {
return nullptr;
}
return _flightEntries[index];
}
//-----------------------------------------------------------------------------
int
AirspaceFlightModel::findFlightID(QString flightID)
{
for(int i = 0; i < _flightEntries.count(); i++) {
if(_flightEntries[i]->flightID() == flightID) {
return i;
}
}
return -1;
}
//-----------------------------------------------------------------------------
int
AirspaceFlightModel::count() const
{
return _flightEntries.count();
}
//-----------------------------------------------------------------------------
void
AirspaceFlightModel::append(AirspaceFlightInfo* object)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
_flightEntries.append(object);
endInsertRows();
emit countChanged();
}
//-----------------------------------------------------------------------------
void
AirspaceFlightModel::remove(const QString& flightID)
{
remove(findFlightID(flightID));
}
//-----------------------------------------------------------------------------
void
AirspaceFlightModel::remove(int index)
{
if (index >= 0 && index < _flightEntries.count()) {
beginRemoveRows(QModelIndex(), index, index);
AirspaceFlightInfo* entry = _flightEntries[index];
if(entry) {
qCDebug(AirspaceManagementLog) << "Deleting flight plan" << entry->flightPlanID();
entry->deleteLater();
}
_flightEntries.removeAt(index);
endRemoveRows();
emit countChanged();
}
}
//-----------------------------------------------------------------------------
void
AirspaceFlightModel::clear(void)
{
if(!_flightEntries.isEmpty()) {
beginResetModel();
while (_flightEntries.count()) {
AirspaceFlightInfo* entry = _flightEntries.last();
if(entry) entry->deleteLater();
_flightEntries.removeLast();
}
endResetModel();
emit countChanged();
}
}
//-----------------------------------------------------------------------------
static bool
flight_sort(QObject* a, QObject* b)
{
AirspaceFlightInfo* aa = qobject_cast<AirspaceFlightInfo*>(a);
AirspaceFlightInfo* bb = qobject_cast<AirspaceFlightInfo*>(b);
if(!aa || !bb) return false;
return aa->qStartTime() > bb->qStartTime();
}
//-----------------------------------------------------------------------------
void
AirspaceFlightModel::sortStartFlight()
{
beginResetModel();
std::sort(_flightEntries.begin(), _flightEntries.end(), flight_sort);
endResetModel();
}
//-----------------------------------------------------------------------------
AirspaceFlightInfo*
AirspaceFlightModel::operator[](int index)
{
return get(index);
}
//-----------------------------------------------------------------------------
int
AirspaceFlightModel::rowCount(const QModelIndex& /*parent*/) const
{
return _flightEntries.count();
}
//-----------------------------------------------------------------------------
QVariant
AirspaceFlightModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= _flightEntries.count())
return QVariant();
if (role == ObjectRole)
return QVariant::fromValue(_flightEntries[index.row()]);
return QVariant();
}
//-----------------------------------------------------------------------------
QHash<int, QByteArray>
AirspaceFlightModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[ObjectRole] = "flightEntry";
return roles;
}