Files
pilotclient/src/blackmisc/aviation/aircraftenginelist.cpp
Klaus Basan 94c9886c35 Based on discussion around T107 and slack
* Use CJsonScope scope("MetaData") for stacktrace
* added Q_UNUSED(scope) to avoid QtC warnings
2017-09-24 19:50:37 +01:00

80 lines
2.4 KiB
C++

/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackmisc/aviation/aircraftenginelist.h"
#include "blackmisc/range.h"
#include <QJsonValue>
#include <QString>
#include <QtGlobal>
namespace BlackMisc
{
namespace Aviation
{
CAircraftEngineList::CAircraftEngineList(std::initializer_list<bool> enginesOnOff)
{
int no = 1; // engines 1 based
for (auto it = enginesOnOff.begin(); it != enginesOnOff.end(); ++it)
{
CAircraftEngine engine(no++, *it);
this->push_back(engine);
}
}
CAircraftEngineList::CAircraftEngineList(const CSequence<CAircraftEngine> &other) :
CSequence<CAircraftEngine>(other)
{ }
CAircraftEngine CAircraftEngineList::getEngine(int engineNumber) const
{
Q_ASSERT(engineNumber >= 0);
return this->findBy(&CAircraftEngine::getNumber, engineNumber).frontOrDefault();
}
bool CAircraftEngineList::isEngineOn(int engineNumber) const
{
return this->getEngine(engineNumber).isOn();
}
bool CAircraftEngineList::isAnyEngineOn() const
{
return this->contains(&CAircraftEngine::isOn, true);
}
QJsonObject CAircraftEngineList::toJson() const
{
QJsonObject map;
for (const auto &e : *this)
{
QJsonObject value = e.toJson();
map.insert(QString::number(e.getNumber()), value);
}
return map;
}
void CAircraftEngineList::convertFromJson(const QJsonObject &json)
{
clear();
for (const auto &e : json.keys())
{
CAircraftEngine engine;
int number = e.toInt();
CJsonScope scope(e);
Q_UNUSED(scope);
engine.convertFromJson(json.value(e).toObject());
engine.setNumber(number);
push_back(engine);
}
}
} // namespace
} // namespace