diff --git a/src/blackgui/components/cockpitcomcomponent.ui b/src/blackgui/components/cockpitcomcomponent.ui index 450845a3b..24375c898 100644 --- a/src/blackgui/components/cockpitcomcomponent.ui +++ b/src/blackgui/components/cockpitcomcomponent.ui @@ -471,7 +471,17 @@ - + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + @@ -618,17 +628,17 @@ - - BlackGui::CTransponderModeSelector - QComboBox -
blackgui/transpondermodeselector.h
-
BlackGui::CLedWidget QWidget
blackgui/led.h
1
+ + BlackGui::CTransponderModeSelector + QComboBox +
blackgui/transpondermodeselector.h
+
BlackGui::CTransponderCodeSpinBox QSpinBox @@ -640,6 +650,12 @@
blackgui/selcalcodeselector.h
1
+ + BlackGui::Components::CCockpitTransponderModeLedsComponent + QFrame +
blackgui/components/cockpittranspondermodeledscomponent.h
+ 1 +
diff --git a/src/blackgui/components/cockpittranspondermodeledscomponent.cpp b/src/blackgui/components/cockpittranspondermodeledscomponent.cpp new file mode 100644 index 000000000..5bd7580e6 --- /dev/null +++ b/src/blackgui/components/cockpittranspondermodeledscomponent.cpp @@ -0,0 +1,144 @@ +/* Copyright (C) 2013 + * 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 "cockpittranspondermodeledscomponent.h" +#include "blackcore/context_ownaircraft.h" +#include +#include + +using namespace BlackMisc::Aviation; +using namespace BlackCore; + +namespace BlackGui +{ + namespace Components + { + CCockpitTransponderModeLedsComponent::CCockpitTransponderModeLedsComponent(QWidget *parent) : + QFrame(parent), + m_ledStandby(new CLedWidget(false, CLedWidget::Blue, CLedWidget::Black, CLedWidget::Rounded, "standby", "", LedWidth, this)), + m_ledModes(new CLedWidget(false, CLedWidget::Green, CLedWidget::Black, CLedWidget::Rounded, "mode C", "", LedWidth, this)), + m_ledIdent(new CLedWidget(false, CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Rounded, "ident", "", LedWidth, this)) + { + this->init(true); + } + + void CCockpitTransponderModeLedsComponent::runtimeHasBeenSet() + { + Q_ASSERT(this->getIContextOwnAircraft()); + connect(this->getIContextOwnAircraft(), &IContextOwnAircraft::changedAircraftCockpit, this, &CCockpitTransponderModeLedsComponent::ps_onAircraftCockpitChanged); + this->setMode(getOwnTransponder().getTransponderMode()); + } + + void CCockpitTransponderModeLedsComponent::ps_onAircraftCockpitChanged(const CAircraft &aircraft, const QString &originator) + { + if (ledsOriginator() == originator) { return; } + this->setMode(aircraft.getTransponderMode()); + } + + void CCockpitTransponderModeLedsComponent::ps_onLedClicked() + { + QWidget *w = qobject_cast(QObject::sender()); + if (!w) { return; } + if (!this->getIContextOwnAircraft()) { return; } + CTransponder::TransponderMode mode; + if (this->m_ledStandby.data() == w) + { + mode = CTransponder::StateStandby; + } + else if (this->m_ledIdent.data() == w) + { + mode = CTransponder::StateIdent; + } + else if (this->m_ledModes.data() == w) + { + mode = CTransponder::ModeC; + } + else + { + return; + } + CAircraft ownAircraft = this->getOwnAircraft(); + if (ownAircraft.getTransponderMode() == mode) { return; } + + this->setMode(mode); + CTransponder xpdr = ownAircraft.getTransponder(); + xpdr.setTransponderMode(mode); + this->getIContextOwnAircraft()->updateOwnCockpit(ownAircraft.getCom1System(), ownAircraft.getCom2System(), xpdr, ledsOriginator()); + } + + void CCockpitTransponderModeLedsComponent::init(bool horizontal) + { + QBoxLayout *ledLayout = nullptr; + if (horizontal) { ledLayout = new QHBoxLayout(this); } + else { ledLayout = new QVBoxLayout(this); } + + ledLayout->setMargin(0); + ledLayout->addWidget(m_ledStandby.data()); + ledLayout->addWidget(m_ledModes.data()); + ledLayout->addWidget(m_ledIdent.data()); + connect(this->m_ledIdent.data(), &CLedWidget::clicked, this, &CCockpitTransponderModeLedsComponent::ps_onLedClicked); + connect(this->m_ledModes.data(), &CLedWidget::clicked, this, &CCockpitTransponderModeLedsComponent::ps_onLedClicked); + connect(this->m_ledStandby.data(), &CLedWidget::clicked, this, &CCockpitTransponderModeLedsComponent::ps_onLedClicked); + this->setLayout(ledLayout); + + // if context is already available set mode + if (this->getIContextOwnAircraft()) { this->setMode(getOwnTransponder().getTransponderMode()); } + } + + void CCockpitTransponderModeLedsComponent::setMode(BlackMisc::Aviation::CTransponder::TransponderMode mode) + { + m_ledStandby->setOn(false); + m_ledModes->setOn(false); + m_ledIdent->setOn(false); + + switch (mode) + { + case CTransponder::ModeA: + case CTransponder::ModeC: + case CTransponder::ModeS: + case CTransponder::ModeMil1: + case CTransponder::ModeMil2: + case CTransponder::ModeMil3: + case CTransponder::ModeMil4: + case CTransponder::ModeMil5: + m_ledModes->setOn(true); + break; + case CTransponder::StateIdent: + m_ledModes->setOn(true); + m_ledIdent->setOn(true); + break; + default: + case CTransponder::StateStandby: + m_ledStandby->setOn(true); + break; + } + } + + CTransponder CCockpitTransponderModeLedsComponent::getOwnTransponder() const + { + Q_ASSERT(getIContextOwnAircraft()); + return getIContextOwnAircraft()->getOwnAircraft().getTransponder(); + } + + CAircraft CCockpitTransponderModeLedsComponent::getOwnAircraft() const + { + Q_ASSERT(getIContextOwnAircraft()); + return getIContextOwnAircraft()->getOwnAircraft(); + } + + const QString &CCockpitTransponderModeLedsComponent::ledsOriginator() + { + // string is generated once, the timestamp allows to use multiple + // components (as long as they are not generated at the same ms) + static const QString o = QString("XPDRLEDSCOMCOMPONENT:").append(QString::number(QDateTime::currentMSecsSinceEpoch())); + return o; + } + + } // namespace +} // namespace diff --git a/src/blackgui/components/cockpittranspondermodeledscomponent.h b/src/blackgui/components/cockpittranspondermodeledscomponent.h new file mode 100644 index 000000000..32b82d29a --- /dev/null +++ b/src/blackgui/components/cockpittranspondermodeledscomponent.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2013 + * 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 BLACKMISC_COCKPITTRANSPONDERMODELEDSCOMPONENT_H +#define BLACKMISC_COCKPITTRANSPONDERMODELEDSCOMPONENT_H + +#include "enableforruntime.h" +#include "../led.h" +#include "blackmisc/avaircraft.h" +#include "blackmisc/aviotransponder.h" +#include + +namespace BlackGui +{ + namespace Components + { + + //! LEDs representing transponder mode state + class CCockpitTransponderModeLedsComponent : public QFrame, public CEnableForRuntime + { + Q_OBJECT + + public: + //! Constructor + explicit CCockpitTransponderModeLedsComponent(QWidget *parent = nullptr); + + protected: + //! \copydoc CEnableForRuntime::runtimeHasBeenSet + virtual void runtimeHasBeenSet() override; + + private slots: + //! \copydoc IContextOwnAircraft::changedAircraftCockpit + void ps_onAircraftCockpitChanged(const BlackMisc::Aviation::CAircraft &aircraft, const QString &originator); + + //! LED clicked + void ps_onLedClicked(); + + private: + const int LedWidth = 14; //!< LED width + + //! Init either in horizontal or vertical layout + void init(bool horizontal); + + //! Set the mode + void setMode(BlackMisc::Aviation::CTransponder::TransponderMode mode); + + //! Own Transponder + BlackMisc::Aviation::CTransponder getOwnTransponder() const; + + //! Own Aircraft + BlackMisc::Aviation::CAircraft getOwnAircraft() const; + + //! Identifies sender of cockpit updates + static const QString &ledsOriginator(); + + QScopedPointer m_ledStandby; + QScopedPointer m_ledIdent; + QScopedPointer m_ledModes; + + }; + + } // namespace +} // namespace + +#endif // guard diff --git a/src/blackgui/led.cpp b/src/blackgui/led.cpp index c6f4a0bc1..fea76dee3 100644 --- a/src/blackgui/led.cpp +++ b/src/blackgui/led.cpp @@ -26,9 +26,9 @@ namespace BlackGui this->setLed(); } - CLedWidget::CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName, const QString &offName, QWidget *parent) : + CLedWidget::CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName, const QString &offName, int targetWidth, QWidget *parent) : m_value(on ? On : Off), m_colorOn(onColor), m_colorOff(offColor), - m_shape(shape), m_tooltipOn(onName), m_tooltipOff(offName), + m_shape(shape), m_widthTarget(targetWidth), m_tooltipOn(onName), m_tooltipOff(offName), m_renderer(new QSvgRenderer(parent)) { this->setLed(); @@ -62,7 +62,6 @@ namespace BlackGui ledShapeAndColor.append(CLedWidget::colorString(this->m_colorOff)); break; } - } else { @@ -92,10 +91,7 @@ namespace BlackGui this->m_whRatio = s.width() / s.height(); // size - if (this->m_widthTarget < 0) - { - this->m_widthTarget = widths().at(static_cast(m_shape)); - } + if (this->m_widthTarget < 0) { this->m_widthTarget = widths().at(static_cast(m_shape)); } double h = this->m_widthTarget / this->m_whRatio; this->m_heightCalculated = qRound(h); @@ -243,6 +239,19 @@ namespace BlackGui m_renderer->render(&painter); } + void CLedWidget::mousePressEvent(QMouseEvent *event) + { + if (event->button() == Qt::LeftButton) + { + emit clicked(); + event->accept(); + } + else + { + QWidget::mousePressEvent(event); + } + } + const QStringList &CLedWidget::shapes() { static const QStringList shapes( {":/qled/icons/qled/circle_" , ":/qled/icons/qled/square_" , ":/qled/icons/qled/triang_" , ":/qled/icons/qled/round_"}); diff --git a/src/blackgui/led.h b/src/blackgui/led.h index 71404df55..68bc35094 100644 --- a/src/blackgui/led.h +++ b/src/blackgui/led.h @@ -45,7 +45,7 @@ namespace BlackGui CLedWidget(QWidget *parent = nullptr); //! Constructor - CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName = "on", const QString &offName = "off", QWidget *parent = nullptr); + CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName = "on", const QString &offName = "off", int targetWidth = -1, QWidget *parent = nullptr); //! Destructor virtual ~CLedWidget(); @@ -87,7 +87,7 @@ namespace BlackGui void setShape(LedShape); //! Target width - void setTargetWidth(int width) { this->m_widthTarget = width; } + void setTargetWidth(int width) { this->m_widthTarget = width; this->setLed(); } //! Tool tip QString getOnToolTip() const { return m_tooltipOn; } @@ -119,6 +119,10 @@ namespace BlackGui //! Render as pixmap, so it can be used with TableViews QPixmap asPixmap() const; + signals: + //! LED clicked + void clicked(); + protected: State m_value = Off; //!< current value LedColor m_colorOn = Yellow; //!< On color @@ -138,6 +142,9 @@ namespace BlackGui //! Paint event virtual void paintEvent(QPaintEvent *event) override; + //! Mouse pressed + virtual void mousePressEvent(QMouseEvent *event) override; + //! Set / init LED void setLed(LedColor ledColor = NoColor); @@ -156,10 +163,6 @@ namespace BlackGui //! Color string static const QString &colorString(LedColor color); //!