APMAirframeLoader.cc 2.76 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 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.
 *
 ****************************************************************************/
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


/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "APMAirframeLoader.h"
#include "QGCApplication.h"
#include "QGCLoggingCategory.h"
#include "APMAirframeComponentAirframes.h"

#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QDebug>

QGC_LOGGING_CATEGORY(APMAirframeLoaderLog, "APMAirframeLoaderLog")

bool APMAirframeLoader::_airframeMetaDataLoaded = false;

APMAirframeLoader::APMAirframeLoader(AutoPilotPlugin* autopilot, UASInterface* uas, QObject* parent)
{
    Q_UNUSED(autopilot);
    Q_UNUSED(uas);
    Q_UNUSED(parent);
    Q_ASSERT(uas);
}

/// Load Airframe Fact meta data
void APMAirframeLoader::loadAirframeFactMetaData(void)
{
    if (_airframeMetaDataLoaded) {
        return;
    }

    qCDebug(APMAirframeLoaderLog) << "Loading APM airframe fact meta data";

    Q_ASSERT(APMAirframeComponentAirframes::get().count() == 0);

47
    QString airframeFilename(QStringLiteral(":/AutoPilotPlugins/APM/APMAirframeFactMetaData.xml"));
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    qCDebug(APMAirframeLoaderLog) << "Loading meta data file:" << airframeFilename;

    QFile xmlFile(airframeFilename);
    Q_ASSERT(xmlFile.exists());

    bool success = xmlFile.open(QIODevice::ReadOnly);
    Q_UNUSED(success);
    Q_ASSERT(success);

    QXmlStreamReader xml(xmlFile.readAll());
    xmlFile.close();
    if (xml.hasError()) {
        qCWarning(APMAirframeLoaderLog) << "Badly formed XML" << xml.errorString();
        return;
    }

    QString airframeGroup;
    QString image;
    int groupId = 0;
    while (!xml.atEnd()) {
        if (xml.isStartElement()) {
            QString elementName = xml.name().toString();
            QXmlStreamAttributes attr = xml.attributes();
72 73 74 75
            if (elementName == QLatin1Literal("airframe_group")) {
                airframeGroup = attr.value(QStringLiteral("name")).toString();
                image = attr.value(QStringLiteral("image")).toString();
                groupId = attr.value(QStringLiteral("id")).toInt();
76
                APMAirframeComponentAirframes::insert(airframeGroup, groupId, image);
77 78 79
            } else if (elementName == QLatin1Literal("airframe")) {
                QString name = attr.value(QStringLiteral("name")).toString();
                QString file = attr.value(QStringLiteral("file")).toString();
80 81 82 83 84 85 86 87
                APMAirframeComponentAirframes::insert(airframeGroup, groupId, image, name, file);
            }
        }
        xml.readNext();
    }

    _airframeMetaDataLoaded = true;
}