Ref T111, created aircraft parts form to be used in internals and dummy driver

This commit is contained in:
Klaus Basan
2017-08-05 00:46:50 +02:00
committed by Mathew Sutcliffe
parent 7939fd0184
commit eea6039d58
5 changed files with 559 additions and 1 deletions

View File

@@ -0,0 +1,196 @@
/* Copyright (C) 2017
* 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 "aircraftpartsform.h"
#include "ui_aircraftpartsform.h"
#include "blackgui/guiutility.h"
#include "blackmisc/aviation/aircraftenginelist.h"
#include "blackmisc/aviation/aircraftlights.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/statusmessage.h"
#include <QPushButton>
#include <QJsonDocument>
#include <QJsonParseError>
using namespace BlackGui;
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
namespace BlackGui
{
namespace Editors
{
CAircraftPartsForm::CAircraftPartsForm(QWidget *parent) :
CForm(parent),
ui(new Ui::CAircraftPartsForm)
{
ui->setupUi(this);
connect(ui->pb_AircraftPartsLightsOn, &QPushButton::pressed, this, &CAircraftPartsForm::setAllLights);
connect(ui->pb_AircraftPartsLightsOff, &QPushButton::pressed, this, &CAircraftPartsForm::setAllLights);
connect(ui->pb_AircraftPartsEnginesOn, &QPushButton::pressed, this, &CAircraftPartsForm::setAllEngines);
connect(ui->pb_AircraftPartsEnginesOff, &QPushButton::pressed, this, &CAircraftPartsForm::setAllEngines);
connect(ui->pb_AircraftPartsUiToJson, &QPushButton::pressed, this, &CAircraftPartsForm::guiToJson);
}
CAircraftPartsForm::~CAircraftPartsForm()
{ }
void CAircraftPartsForm::setReadOnly(bool readonly)
{
CGuiUtility::childrenSetEnabled<QPushButton>(this, !readonly);
CGuiUtility::checkBoxesReadOnly(this, readonly);
}
void CAircraftPartsForm::setSelectOnly()
{
this->setReadOnly(true);
}
CStatusMessageList CAircraftPartsForm::validate(bool nested) const
{
Q_UNUSED(nested);
CStatusMessageList msgs;
return msgs;
}
CAircraftParts CAircraftPartsForm::getAircraftPartsFromGui() const
{
return this->guiToAircraftParts();
}
CAircraftParts CAircraftPartsForm::getAircraftPartsFromJson() const
{
const QString jsonParts = ui->te_AircraftPartsJson->toPlainText().trimmed();
CAircraftParts parts;
if (jsonParts.isEmpty())
{
CLogMessage(this).validationError("No JSON content");
return parts;
}
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonParts.toUtf8(), &jsonError));
if (jsonError.error != QJsonParseError::NoError)
{
CLogMessage(this).validationError("Parse error: %1") << jsonError.errorString();
return parts;
}
try
{
parts.convertFromJson(jsonDoc.object());
}
catch (const CJsonException &ex)
{
ex.toLogMessage(this, "Parse error");
return parts;
}
return parts;
}
void CAircraftPartsForm::setAircraftParts(const CAircraftParts &parts)
{
this->partsToGui(parts);
ui->te_AircraftPartsJson->setText(parts.toJsonString());
}
void CAircraftPartsForm::showSetButton(bool visible)
{
ui->pb_Set->setVisible(visible);
}
void CAircraftPartsForm::showJson(bool visible)
{
ui->te_AircraftPartsJson->setVisible(visible);
}
void CAircraftPartsForm::setAllLights()
{
const bool on = (QObject::sender() == ui->pb_AircraftPartsLightsOn);
ui->cb_AircraftPartsLightsStrobe->setChecked(on);
ui->cb_AircraftPartsLightsLanding->setChecked(on);
ui->cb_AircraftPartsLightsTaxi->setChecked(on);
ui->cb_AircraftPartsLightsBeacon->setChecked(on);
ui->cb_AircraftPartsLightsNav->setChecked(on);
ui->cb_AircraftPartsLightsLogo->setChecked(on);
}
void CAircraftPartsForm::setAllEngines()
{
const bool on = (QObject::sender() == ui->pb_AircraftPartsEnginesOn);
ui->cb_AircraftPartsEngine1->setChecked(on);
ui->cb_AircraftPartsEngine2->setChecked(on);
ui->cb_AircraftPartsEngine3->setChecked(on);
ui->cb_AircraftPartsEngine4->setChecked(on);
ui->cb_AircraftPartsEngine5->setChecked(on);
ui->cb_AircraftPartsEngine6->setChecked(on);
}
Aviation::CAircraftParts CAircraftPartsForm::guiToAircraftParts() const
{
const CAircraftLights lights(
ui->cb_AircraftPartsLightsStrobe->isChecked(),
ui->cb_AircraftPartsLightsLanding->isChecked(),
ui->cb_AircraftPartsLightsTaxi->isChecked(),
ui->cb_AircraftPartsLightsBeacon->isChecked(),
ui->cb_AircraftPartsLightsNav->isChecked(),
ui->cb_AircraftPartsLightsLogo->isChecked()
);
const CAircraftEngineList engines(
{
ui->cb_AircraftPartsEngine1->isChecked(),
ui->cb_AircraftPartsEngine2->isChecked(),
ui->cb_AircraftPartsEngine3->isChecked(),
ui->cb_AircraftPartsEngine4->isChecked(),
ui->cb_AircraftPartsEngine5->isChecked(),
ui->cb_AircraftPartsEngine6->isChecked()
}
);
const CAircraftParts parts(lights,
ui->cb_AircraftPartsGearDown->isChecked(),
ui->sb_AircraftPartsFlapsPercentage->value(),
ui->cb_AircraftPartsSpoilers->isChecked(),
engines,
ui->cb_AircraftPartsIsOnGround->isChecked()
);
return parts;
}
void CAircraftPartsForm::partsToGui(const Aviation::CAircraftParts &parts)
{
ui->cb_AircraftPartsGearDown->setChecked(parts.isGearDown());
ui->cb_AircraftPartsIsOnGround->setChecked(parts.isOnGround());
ui->cb_AircraftPartsSpoilers->setChecked(parts.isSpoilersOut());
ui->sb_AircraftPartsFlapsPercentage->setValue(parts.getFlapsPercent());
const CAircraftLights lights = parts.getLights();
ui->cb_AircraftPartsLightsBeacon->setChecked(lights.isBeaconOn());
ui->cb_AircraftPartsLightsLanding->setChecked(lights.isLandingOn());
ui->cb_AircraftPartsLightsLogo->setChecked(lights.isLogoOn());
ui->cb_AircraftPartsLightsNav->setChecked(lights.isNavOn());
ui->cb_AircraftPartsLightsStrobe->setChecked(lights.isStrobeOn());
ui->cb_AircraftPartsLightsTaxi->setChecked(lights.isTaxiOn());
const CAircraftEngineList engines = parts.getEngines();
ui->cb_AircraftPartsEngine1->setChecked(engines.isEngineOn(1));
ui->cb_AircraftPartsEngine2->setChecked(engines.isEngineOn(2));
ui->cb_AircraftPartsEngine3->setChecked(engines.isEngineOn(3));
ui->cb_AircraftPartsEngine4->setChecked(engines.isEngineOn(4));
ui->cb_AircraftPartsEngine5->setChecked(engines.isEngineOn(5));
ui->cb_AircraftPartsEngine6->setChecked(engines.isEngineOn(6));
}
void CAircraftPartsForm::guiToJson()
{
const QJsonDocument json(guiToAircraftParts().toJson());
const QString j(json.toJson(QJsonDocument::Indented));
ui->te_AircraftPartsJson->setText(j);
}
} // ns
} // ns

View File

@@ -0,0 +1,87 @@
/* Copyright (C) 2017
* 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.
*/
//! \file
#ifndef BLACKGUI_EDITORS_AIRCRAFTPARTSFORM_H
#define BLACKGUI_EDITORS_AIRCRAFTPARTSFORM_H
#include "blackgui/editors/form.h"
#include "blackgui/blackguiexport.h"
#include "blackmisc/aviation/aircraftparts.h"
#include "blackmisc/statusmessagelist.h"
class QWidget;
namespace Ui { class CAircraftPartsForm; }
namespace BlackGui
{
namespace Editors
{
/**
* Edit and set aircraft parts
*/
class BLACKGUI_EXPORT CAircraftPartsForm : public CForm
{
Q_OBJECT
public:
//! Constructor
explicit CAircraftPartsForm(QWidget *parent = nullptr);
//! Destructor
virtual ~CAircraftPartsForm();
//! \name Form class implementations
//! @{
virtual void setReadOnly(bool readonly) override;
virtual void setSelectOnly() override;
virtual BlackMisc::CStatusMessageList validate(bool nested = false) const override;
//! @}
//! Get the parts
BlackMisc::Aviation::CAircraftParts getAircraftPartsFromGui() const;
//! Get the parts from JSON
BlackMisc::Aviation::CAircraftParts getAircraftPartsFromJson() const;
//! Set the parts
void setAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts);
//! Set button visible
void showSetButton(bool visible);
//! Set JSON textarea visible
void showJson(bool visible);
signals:
//! Change coordinate
void changeAircraftParts();
private:
QScopedPointer<Ui::CAircraftPartsForm> ui;
//! Get parts object
BlackMisc::Aviation::CAircraftParts guiToAircraftParts() const;
//! GUI set by parts
void partsToGui(const BlackMisc::Aviation::CAircraftParts &parts);
//! GUI values to JSON
void guiToJson();
//! All lights on
void setAllLights();
//! All engines on
void setAllEngines();
};
} // ns
} // ns
#endif // guard

View File

@@ -0,0 +1,250 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CAircraftPartsForm</class>
<widget class="QFrame" name="CAircraftPartsForm">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>276</width>
<height>403</height>
</rect>
</property>
<property name="windowTitle">
<string>Frame</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gl_AircraftPartsForm">
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item row="4" column="1">
<widget class="QCheckBox" name="cb_AircraftPartsLightsStrobe">
<property name="text">
<string>Strobe</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QPushButton" name="pb_AircraftPartsEnginesOff">
<property name="text">
<string>all off</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lbl_AircraftPartsFlapsPercentage">
<property name="text">
<string>Flaps %:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="sb_AircraftPartsFlapsPercentage">
<property name="maximum">
<number>100</number>
</property>
<property name="singleStep">
<number>5</number>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QCheckBox" name="cb_AircraftPartsLightsLanding">
<property name="text">
<string>Landing</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="cb_AircraftPartsLightsNav">
<property name="text">
<string>Nav</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pb_AircraftPartsUiToJson">
<property name="text">
<string>to JSON</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="cb_AircraftPartsGearDown">
<property name="text">
<string>Gear down</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="cb_AircraftPartsLightsBeacon">
<property name="text">
<string>Beacon</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="cb_AircraftPartsIsOnGround">
<property name="text">
<string>on ground</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lbl_AircraftPartsLights">
<property name="text">
<string>Lights:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="cb_AircraftPartsLightsTaxi">
<property name="text">
<string>Taxi</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="cb_AircraftPartsSpoilers">
<property name="text">
<string>Spoilers</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="pb_AircraftPartsLightsOff">
<property name="text">
<string>all off</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="pb_AircraftPartsLightsOn">
<property name="text">
<string>all on</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QCheckBox" name="cb_AircraftPartsLightsLogo">
<property name="text">
<string>Logo</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QPushButton" name="pb_AircraftPartsEnginesOn">
<property name="text">
<string>all on</string>
</property>
</widget>
</item>
<item row="7" column="0" colspan="3">
<widget class="QTextEdit" name="te_AircraftPartsJson"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="lbl_AircraftPartsEngines">
<property name="text">
<string>Engines:</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="3">
<widget class="QFrame" name="fr_Engines">
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="hl_Engines">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="cb_AircraftPartsEngine1">
<property name="text">
<string>1</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_AircraftPartsEngine2">
<property name="text">
<string>2</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_AircraftPartsEngine3">
<property name="text">
<string>3</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_AircraftPartsEngine4">
<property name="text">
<string>4</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_AircraftPartsEngine5">
<property name="text">
<string>5</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_AircraftPartsEngine6">
<property name="text">
<string>6</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="8" column="2">
<widget class="QPushButton" name="pb_Set">
<property name="text">
<string>set</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -211,6 +211,16 @@ namespace BlackGui
}
}
void CGuiUtility::checkBoxesReadOnly(QWidget *parent, bool readOnly)
{
if (!parent) { return; }
QList<QCheckBox *> allCheckBoxes = parent->findChildren<QCheckBox *>();
for (QCheckBox *cb : allCheckBoxes)
{
CGuiUtility::checkBoxReadOnly(cb, readOnly);
}
}
QWidgetList CGuiUtility::topLevelApplicationWidgetsWithName()
{
QWidgetList tlw = QApplication::topLevelWidgets();

View File

@@ -18,6 +18,7 @@
#include <QByteArray>
#include <QPoint>
#include <QString>
#include <QWidget>
#include <QWidgetList>
#include <QModelIndexList>
@@ -26,7 +27,6 @@ class QComboBox;
class QLayout;
class QMimeData;
class QTabWidget;
class QWidget;
class QGraphicsOpacityEffect;
namespace BlackGui
@@ -86,6 +86,21 @@ namespace BlackGui
//! Pseudo readonly state for checkbox
static void checkBoxReadOnly(QCheckBox *checkBox, bool readOnly);
//! Pseudo readonly state for checkboxes of widget
static void checkBoxesReadOnly(QWidget *parent, bool readOnly);
//! Enable/disable all child widgets
template <class WIDGET>
static void childrenSetEnabled(QWidget *parent, bool enabled)
{
if (!parent) { return; }
QList<WIDGET *> children = parent->findChildren<WIDGET *>();
for (WIDGET *w : children)
{
w->setEnabled(enabled);
}
}
//! Toogle window flags / stay on top
static bool toggleStayOnTop(QWidget *widget);