AirspaceFlightPlanProvider.cc 4.86 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2017 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.
 *
 ****************************************************************************/

Gus Grubba's avatar
Gus Grubba committed
10
#include "AirspaceManager.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirspaceFlightPlanProvider.h"
Gus Grubba's avatar
Gus Grubba committed
12
#include <QQmlEngine>
Gus Grubba's avatar
Gus Grubba committed
13

Gus Grubba's avatar
Gus Grubba committed
14 15 16 17 18 19
//-----------------------------------------------------------------------------
AirspaceFlightAuthorization::AirspaceFlightAuthorization(QObject *parent)
    : QObject(parent)
{
}

Gus Grubba's avatar
Gus Grubba committed
20
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
21 22 23 24 25
AirspaceFlightInfo::AirspaceFlightInfo(QObject *parent)
    : QObject(parent)
{
}

Gus Grubba's avatar
Gus Grubba committed
26
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
27 28 29 30
AirspaceFlightPlanProvider::AirspaceFlightPlanProvider(QObject *parent)
    : QObject(parent)
{
}
Gus Grubba's avatar
Gus Grubba committed
31 32 33 34 35 36 37 38 39 40 41 42 43

//-----------------------------------------------------------------------------
AirspaceFlightModel::AirspaceFlightModel(QObject *parent)
    : QAbstractListModel(parent)
{

}

//-----------------------------------------------------------------------------
AirspaceFlightInfo*
AirspaceFlightModel::get(int index)
{
    if (index < 0 || index >= _flightEntries.count()) {
44
        return nullptr;
Gus Grubba's avatar
Gus Grubba committed
45 46 47 48
    }
    return _flightEntries[index];
}

Gus Grubba's avatar
Gus Grubba committed
49 50
//-----------------------------------------------------------------------------
int
51
AirspaceFlightModel::findFlightID(QString flightID)
Gus Grubba's avatar
Gus Grubba committed
52 53
{
    for(int i = 0; i < _flightEntries.count(); i++) {
54
        if(_flightEntries[i]->flightID() == flightID) {
Gus Grubba's avatar
Gus Grubba committed
55 56 57 58 59 60
            return i;
        }
    }
    return -1;
}

Gus Grubba's avatar
Gus Grubba committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//-----------------------------------------------------------------------------
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();
}

Gus Grubba's avatar
Gus Grubba committed
79 80
//-----------------------------------------------------------------------------
void
81
AirspaceFlightModel::remove(const QString& flightID)
Gus Grubba's avatar
Gus Grubba committed
82
{
83
    remove(findFlightID(flightID));
Gus Grubba's avatar
Gus Grubba committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

//-----------------------------------------------------------------------------
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();
    }
}

Gus Grubba's avatar
Gus Grubba committed
103 104 105 106 107
//-----------------------------------------------------------------------------
void
AirspaceFlightModel::clear(void)
{
    if(!_flightEntries.isEmpty()) {
108
        beginResetModel();
Gus Grubba's avatar
Gus Grubba committed
109 110 111 112 113
        while (_flightEntries.count()) {
            AirspaceFlightInfo* entry = _flightEntries.last();
            if(entry) entry->deleteLater();
            _flightEntries.removeLast();
        }
114
        endResetModel();
Gus Grubba's avatar
Gus Grubba committed
115 116 117 118
        emit countChanged();
    }
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
//-----------------------------------------------------------------------------
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();
}


Gus Grubba's avatar
Gus Grubba committed
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
//-----------------------------------------------------------------------------
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;
}