Aircraft parts history component

This frame component allows to display the current aircraft parts
and the received history for a given callsign.

refs #835
This commit is contained in:
Roland Winklmeier
2016-12-16 20:51:13 +01:00
committed by Mathew Sutcliffe
parent 4994bf12b2
commit 7e1a3d4ef2
4 changed files with 433 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
/* Copyright (C) 2016
* 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 "aircraftpartshistory.h"
#include "ui_aircraftpartshistory.h"
#include "blackmisc/propertyindexlist.h"
#include "blackmisc/htmlutils.h"
#include "blackcore/context/contextnetwork.h"
#include "blackcore/context/contextsimulator.h"
#include "blackgui/guiapplication.h"
#include "blackgui/uppercasevalidator.h"
#include <QCompleter>
#include <QStringListModel>
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
using namespace BlackCore;
using namespace BlackCore::Context;
namespace BlackGui
{
namespace Components
{
CAircraftPartsHistory::CAircraftPartsHistory(QWidget *parent) :
QFrame(parent),
ui(new Ui::CAircraftPartsHistory)
{
ui->setupUi(this);
ui->le_Callsign->setValidator(new CUpperCaseValidator(this));
ui->le_Callsign->setCompleter(new QCompleter(ui->le_Callsign));
this->m_timerCallsignUpdate.setInterval(20 * 1000);
this->m_timerUpdateHistory.setInterval(2 * 1000);
this->initGui();
this->m_text.setDefaultStyleSheet(CStatusMessageList::htmlStyleSheet());
connect(ui->le_Callsign, &QLineEdit::textEdited, this, &CAircraftPartsHistory::callsignModified);
connect(ui->le_Callsign, &QLineEdit::returnPressed, this, &CAircraftPartsHistory::callsignEntered);
connect(ui->cb_PartsHistoryEnabled, &QCheckBox::toggled, this, &CAircraftPartsHistory::toggleHistoryEnabled);
if (this->hasContexts())
{
connect(sGui->getIContextNetwork(), &IContextNetwork::changedLogOrDebugSettings, this, &CAircraftPartsHistory::valuesChanged);
connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this, &CAircraftPartsHistory::connectionStatusChanged);
}
connect(&this->m_timerCallsignUpdate, &QTimer::timeout, this, &CAircraftPartsHistory::updateCallsignCompleter);
connect(&this->m_timerUpdateHistory, &QTimer::timeout, this, &CAircraftPartsHistory::updatePartsHistory);
}
CAircraftPartsHistory::~CAircraftPartsHistory()
{ }
void CAircraftPartsHistory::initGui()
{
const bool needCallsigns = this->partsHistoryEnabled();
if (needCallsigns && !m_timerCallsignUpdate.isActive() && !m_timerUpdateHistory.isActive())
{
this->m_timerCallsignUpdate.start();
this->m_timerUpdateHistory.start();
this->updateCallsignCompleter();
}
else if (!needCallsigns)
{
this->m_timerCallsignUpdate.stop();
this->m_timerUpdateHistory.stop();
}
// avoid signal roundtrip
bool c = sGui->getIContextNetwork()->isAircraftPartsHistoryEnabled();
ui->cb_PartsHistoryEnabled->setChecked(c);
}
bool CAircraftPartsHistory::hasContexts() const
{
return sGui && sGui->getIContextSimulator() && sGui->getIContextNetwork();
}
bool CAircraftPartsHistory::partsHistoryEnabled() const
{
return this->hasContexts();
}
void CAircraftPartsHistory::updateCallsignCompleter()
{
if (!this->hasContexts() || !sGui->getIContextNetwork()->isConnected()) { return; }
const QStringList callsigns = sGui->getIContextNetwork()->getAircraftInRangeCallsigns().toStringList(false);
QCompleter *completer = ui->le_Callsign->completer();
Q_ASSERT_X(completer, Q_FUNC_INFO, "missing completer");
if (!completer->model())
{
completer->setModel(new QStringListModel(callsigns, completer));
}
else
{
qobject_cast<QStringListModel *>(completer->model())->setStringList(callsigns);
}
}
void CAircraftPartsHistory::updatePartsHistory()
{
if (!this->hasContexts()) { return; }
if (isBeingModified) { return; }
static const CPropertyIndexList properties({ CStatusMessage::IndexUtcTimestampFormattedHms, CStatusMessage::IndexMessage });
const CCallsign cs(ui->le_Callsign->text().trimmed().toUpper());
if (cs.isEmpty()) { return; }
const auto currentAircraftParts = sGui->getIContextNetwork()->getRemoteAircraftParts(cs, -1).frontOrDefault();
const auto aircraftPartsHistory = sGui->getIContextNetwork()->getAircraftPartsHistory(cs);
QString html;
if (currentAircraftParts == CAircraftParts() && aircraftPartsHistory.isEmpty())
{
html = cs.toQString() + QString(" does not support aircraft parts or nothing received yet.");
}
else
{
QString s;
s += "lights on:";
s += "<br>";
s += "&nbsp;&nbsp;&nbsp;&nbsp;";
s += currentAircraftParts.getLights().toQString();
s += "<br>";
s += "gear down: ";
s += BlackMisc::boolToYesNo(currentAircraftParts.isGearDown());
s += "<br>";
s += "flaps pct: ";
s += QString::number(currentAircraftParts.getFlapsPercent());
s += "<br>";
s += "spoilers out: ";
s += BlackMisc::boolToYesNo(currentAircraftParts.isSpoilersOut());
s += "<br>";
s += "engines on: ";
s += "<br>";
s += "&nbsp;&nbsp;&nbsp;&nbsp;";
s += currentAircraftParts.getEngines().toQString();
s += "<br>";
s += " on ground: ";
s += BlackMisc::boolToYesNo(currentAircraftParts.isOnGround());
html += s;
if (ui->cb_PartsHistoryEnabled->isChecked())
{
html +="<hr>";
html += aircraftPartsHistory.toHtml(properties);
}
}
this->m_text.setHtml(html);
ui->te_Messages->setDocument(&this->m_text);
if (ui->cb_AutoScrollEnabled->isChecked())
{
QTextCursor c = ui->te_Messages->textCursor();
c.movePosition(QTextCursor::End);
ui->te_Messages->setTextCursor(c);
}
}
void CAircraftPartsHistory::callsignEntered()
{
isBeingModified = false;
updatePartsHistory();
m_timerUpdateHistory.start();
}
void CAircraftPartsHistory::callsignModified()
{
isBeingModified = true;
}
void CAircraftPartsHistory::valuesChanged()
{
this->initGui();
}
void CAircraftPartsHistory::toggleHistoryEnabled(bool enabled)
{
if (!sGui || !sGui->getIContextNetwork() || !sGui->getIContextSimulator()) { return; }
const QObject *sender = QObject::sender();
if (sender == ui->cb_PartsHistoryEnabled)
{
sGui->getIContextNetwork()->enableAircraftPartsHistory(enabled);
}
}
void CAircraftPartsHistory::connectionStatusChanged(INetwork::ConnectionStatus from, INetwork::ConnectionStatus to)
{
Q_UNUSED(from);
if (to == INetwork::Connected || to == INetwork::Disconnected)
{
this->initGui();
}
}
} // ns
} // ns

View File

@@ -0,0 +1,81 @@
/* Copyright (C) 2016
* 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_COMPONENT_AIRCRAFTPARTSHISTORY_H
#define BLACKGUI_COMPONENT_AIRCRAFTPARTSHISTORY_H
#include "blackcore/network.h"
#include <QFrame>
#include <QTabWidget>
#include <QTimer>
#include <QTextDocument>
namespace Ui { class CAircraftPartsHistory; }
namespace BlackGui
{
namespace Components
{
/*!
* History frame for received aircraft parts
*/
class CAircraftPartsHistory : public QFrame
{
Q_OBJECT
public:
//! Constructor
explicit CAircraftPartsHistory(QWidget *parent = nullptr);
//! Destructor
~CAircraftPartsHistory();
private:
QScopedPointer<Ui::CAircraftPartsHistory> ui;
QTimer m_timerCallsignUpdate { this };
QTimer m_timerUpdateHistory { this };
QTextDocument m_text { this };
bool isBeingModified = false;
//! Init
void initGui();
//! Contexts available
bool hasContexts() const;
//! Is parts history enabled?
bool partsHistoryEnabled() const;
private:
//! Update the completer
void updateCallsignCompleter();
//! Update parts history
void updatePartsHistory();
//! Callsign was entered
void callsignEntered();
//! Callsign was modified
void callsignModified();
//! When values changed elsewhere
void valuesChanged();
//! Toggle between enabling and disabling of history
void toggleHistoryEnabled(bool enabled);
//! Connection status changed
void connectionStatusChanged(BlackCore::INetwork::ConnectionStatus from, BlackCore::INetwork::ConnectionStatus to);
};
} // ns
} // ns
#endif // guard

View File

@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CAircraftPartsHistory</class>
<widget class="QFrame" name="CAircraftPartsHistory">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>276</width>
<height>260</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="QVBoxLayout" name="verticalLayout">
<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>
<widget class="QFrame" name="fr_DataEntry">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gl_MatcherSelection">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="lbl_Callsign">
<property name="text">
<string>Callsign:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="cb_PartsHistoryEnabled">
<property name="toolTip">
<string>enable logging of reverse lookup messages</string>
</property>
<property name="text">
<string>Parts History</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbl_Enable">
<property name="text">
<string>Enable:</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="le_Callsign">
<property name="placeholderText">
<string>callsign</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="cb_AutoScrollEnabled">
<property name="text">
<string>Autoscroll</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTextEdit" name="te_Messages">
<property name="documentTitle">
<string>Messages</string>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="placeholderText">
<string>Result will go here</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -130,6 +130,35 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tb_AircraftPartsHistory">
<attribute name="title">
<string>Aircraft parts log</string>
</attribute>
<layout class="QVBoxLayout" name="vl_AircraftPartsHistory">
<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="BlackGui::Components::CAircraftPartsHistory" name="comp_AircraftPartsHistory">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
@@ -266,6 +295,12 @@
<header>blackgui/components/modelmatcherlogcomponent.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>BlackGui::Components::CAircraftPartsHistory</class>
<extends>QFrame</extends>
<header>blackgui/components/aircraftpartshistory.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>BlackGui::Components::CAircraftModelStringCompleter</class>
<extends>QFrame</extends>