From c5a1a6048b3c4fa829b80cbcc0aa218729a0f4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Tue, 31 Mar 2015 20:39:31 +0200 Subject: [PATCH] Refactor Simulator connection status * Removed obsolete and unused signals (ISimulator, CSimulatorContext) * Removed ISimulator::ConnectionStatus enum --- src/blackcore/blackcorefreefunctions.cpp | 3 --- src/blackcore/context_simulator.h | 5 +--- src/blackcore/context_simulator_impl.cpp | 18 ++++++++++--- src/blackcore/context_simulator_impl.h | 5 +++- src/blackcore/context_simulator_proxy.cpp | 7 ++--- src/blackcore/simulator.cpp | 7 ++++- src/blackcore/simulator.h | 26 +++++-------------- .../components/infobarstatuscomponent.cpp | 16 +++++------- .../components/infobarstatuscomponent.h | 2 +- .../components/simulatorcomponent.cpp | 8 +++--- src/blackgui/components/simulatorcomponent.h | 4 +-- 11 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/blackcore/blackcorefreefunctions.cpp b/src/blackcore/blackcorefreefunctions.cpp index 27b77f5bf..ac3cb068d 100644 --- a/src/blackcore/blackcorefreefunctions.cpp +++ b/src/blackcore/blackcorefreefunctions.cpp @@ -12,9 +12,6 @@ namespace BlackCore { void registerMetadata() { - // for some reasons (ask RW) these are registered twice - qRegisterMetaType(); - qRegisterMetaType("Status"); qRegisterMetaType(); } diff --git a/src/blackcore/context_simulator.h b/src/blackcore/context_simulator.h index 443b0fd47..9e52498f1 100644 --- a/src/blackcore/context_simulator.h +++ b/src/blackcore/context_simulator.h @@ -65,14 +65,11 @@ namespace BlackCore virtual ~IContextSimulator() {} signals: - //! Emitted when the simulator connection changes - void connectionChanged(bool connected); - //! Simulator started or stopped void startedChanged(bool startedChanged); //! Simulator combined status - void simulatorStatusChanged(bool connected, bool running, bool paused); + void simulatorStatusChanged(int status); //! Only a limited number of aircraft displayed void restrictedRenderingChanged(bool restricted); diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index 5ee12ef9b..41e4e0a7a 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -248,6 +248,11 @@ namespace BlackCore if (this->m_simulator && this->m_simulator->getSimulatorInfo() == simulatorInfo) { return true; } // already loaded + + /* TODO Specify behaviour below */ + if (simulatorInfo.isUnspecified()) { + return false; + } // warning if we do not have any plugins if (m_simulatorFactories.isEmpty()) { @@ -283,8 +288,7 @@ namespace BlackCore this->unloadSimulatorPlugin(); // old plugin unloaded m_simulator = newSimulator; - connect(m_simulator, &ISimulator::connectionStatusChanged, this, &CContextSimulator::ps_onConnectionStatusChanged); - connect(m_simulator, &ISimulator::simulatorStatusChanged, this, &CContextSimulator::simulatorStatusChanged); + connect(m_simulator, &ISimulator::simulatorStatusChanged, this, &CContextSimulator::ps_onSimulatorStatusChanged); connect(m_simulator, &ISimulator::ownAircraftModelChanged, this, &IContextSimulator::ownAircraftModelChanged); connect(m_simulator, &ISimulator::modelMatchingCompleted, this, &IContextSimulator::modelMatchingCompleted); connect(m_simulator, &ISimulator::installedAircraftModelsChanged, this, &IContextSimulator::installedAircraftModelsChanged); @@ -438,7 +442,7 @@ namespace BlackCore { connected = false; } - emit connectionChanged(connected); + emit simulatorStatusChanged(status); } void CContextSimulator::ps_textMessagesReceived(const Network::CTextMessageList &textMessages) @@ -547,6 +551,7 @@ namespace BlackCore void CContextSimulator::ps_simulatorStarted(CSimulatorInfo simulatorInfo) { CLogMessage(this).info("Simulator %1 started.") << simulatorInfo.toQString(); + stopSimulatorListeners(); loadSimulatorPlugin(simulatorInfo); } @@ -595,5 +600,12 @@ namespace BlackCore } } } + + void CContextSimulator::stopSimulatorListeners() + { + std::for_each(m_simulatorListeners.begin(), m_simulatorListeners.end(), [](ISimulatorListener* l) { + l->stop(); + }); + } } // namespace diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index b3efb768f..ffc61bdc1 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -166,7 +166,7 @@ namespace BlackCore void ps_removedRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign); //! Handle new connection status of simulator - void ps_onConnectionStatusChanged(ISimulator::ConnectionStatus status); + void ps_onSimulatorStatusChanged(int status); //! Text message received void ps_textMessagesReceived(const BlackMisc::Network::CTextMessageList &textMessages); @@ -191,6 +191,9 @@ namespace BlackCore //! \brief find and catalog all simulator plugins void findSimulatorPlugins(); + //! \brief call stop() on all loaded listeners + void stopSimulatorListeners(); + BlackCore::ISimulator *m_simulator = nullptr; //!< simulator plugin QDir m_pluginsDir; QSet m_simulatorFactories; diff --git a/src/blackcore/context_simulator_proxy.cpp b/src/blackcore/context_simulator_proxy.cpp index f44fb2dae..2e4a9ec22 100644 --- a/src/blackcore/context_simulator_proxy.cpp +++ b/src/blackcore/context_simulator_proxy.cpp @@ -34,13 +34,10 @@ namespace BlackCore void CContextSimulatorProxy::relaySignals(const QString &serviceName, QDBusConnection &connection) { bool s = connection.connect(serviceName, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName(), - "connectionChanged", this, SIGNAL(connectionChanged(bool))); + "simulatorStatusChanged", this, SIGNAL(simulatorStatusChanged(int))); Q_ASSERT(s); s = connection.connect(serviceName, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName(), - "startedChanged", this, SIGNAL(startedChanged(bool))); - Q_ASSERT(s); - s = connection.connect(serviceName, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName(), - "simulatorStatusChanged", this, SIGNAL(simulatorStatusChanged(bool, bool, bool))); + "simulatorStatusChanged", this, SIGNAL(simulatorStatusChanged(int))); Q_ASSERT(s); s = connection.connect(serviceName, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName(), "installedAircraftModelsChanged", this, SIGNAL(installedAircraftModelsChanged())); diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index 7779d6d78..bcabb8355 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -21,7 +21,12 @@ namespace BlackCore { void ISimulator::emitSimulatorCombinedStatus() { - emit simulatorStatusChanged(isConnected(), isSimulating(), isPaused()); + quint8 status = + (isConnected() ? Connected : static_cast(0)) + | (isSimulating() ? Running : static_cast(0)) + | (isPaused() ? Paused : static_cast(0)) + ; + emit simulatorStatusChanged(status); } ISimulatorListener::ISimulatorListener(QObject* parent) : QObject(parent) diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 3e4e3bf7b..9e6949a7f 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -32,15 +32,13 @@ namespace BlackCore class ISimulator : public QObject { Q_OBJECT - Q_ENUMS(ConnectionStatus) - public: - //! ISimulator connection - enum ConnectionStatus + //! ISimulator status + enum SimulatorStatus { - Disconnected, - Connected, - ConnectionFailed + Connected = 1 << 0, //!< Is the plugin connected to the simulator? + Running = 1 << 1, //!< Is the simulator actually simulating? + Paused = 1 << 2, //!< Is the simulator paused? }; //! Render all aircraft @@ -177,24 +175,15 @@ namespace BlackCore virtual bool isRenderingEnabled() const = 0; signals: - //! Emitted when the connection status has changed - void connectionStatusChanged(ISimulator::ConnectionStatus status); + //! Simulator combined status + void simulatorStatusChanged(quint8 status); //! Emitted when own aircraft model has changed void ownAircraftModelChanged(BlackMisc::Simulation::CSimulatedAircraft aircraft); - //! Simulator combined status - void simulatorStatusChanged(bool connected, bool running, bool paused); - //! Only a limited number of aircraft displayed void restrictedRenderingChanged(bool restricted); - //! Simulator started - void simulatorStarted(); - - //! Simulator stopped - void simulatorStopped(); - //! A single model has been matched void modelMatchingCompleted(BlackMisc::Simulation::CSimulatedAircraft aircraft); @@ -363,6 +352,5 @@ namespace BlackCore } // namespace Q_DECLARE_INTERFACE(BlackCore::ISimulatorFactory, "org.swift.pilotclient.BlackCore.SimulatorInterface") -Q_DECLARE_METATYPE(BlackCore::ISimulator::ConnectionStatus) #endif // guard diff --git a/src/blackgui/components/infobarstatuscomponent.cpp b/src/blackgui/components/infobarstatuscomponent.cpp index 905962c56..acb0f0581 100644 --- a/src/blackgui/components/infobarstatuscomponent.cpp +++ b/src/blackgui/components/infobarstatuscomponent.cpp @@ -13,6 +13,7 @@ #include "blackcore/context_network.h" #include "blackcore/context_application.h" #include "blackcore/context_audio.h" +#include "blackcore/simulator.h" #include "blackmisc/project.h" #include "blackmisc/icons.h" @@ -97,21 +98,16 @@ namespace BlackGui } } - void CInfoBarStatusComponent::ps_onSimulatorStatusChanged(bool connected, bool running, bool paused) + void CInfoBarStatusComponent::ps_onSimulatorStatusChanged(quint8 status) { - if (connected && running) - { +// if (connected && running) + if (status & (ISimulator::Connected | ISimulator::Running)) { this->ui->led_Simulator->setOn(true); - } - else if (connected) - { + } else if (status & ISimulator::Connected) { this->ui->led_Simulator->setTriState(); - } - else - { + } else { this->ui->led_Simulator->setOn(false); } - Q_UNUSED(paused); } void CInfoBarStatusComponent::ps_onNetworkConnectionChanged(uint from, uint to) diff --git a/src/blackgui/components/infobarstatuscomponent.h b/src/blackgui/components/infobarstatuscomponent.h index 2030e4412..f7bbea62b 100644 --- a/src/blackgui/components/infobarstatuscomponent.h +++ b/src/blackgui/components/infobarstatuscomponent.h @@ -54,7 +54,7 @@ namespace BlackGui private slots: //! Simulator connection has been changed - void ps_onSimulatorStatusChanged(bool connected, bool running, bool paused); + void ps_onSimulatorStatusChanged(quint8 status); //! Network connection has been changed void ps_onNetworkConnectionChanged(uint from, uint to); diff --git a/src/blackgui/components/simulatorcomponent.cpp b/src/blackgui/components/simulatorcomponent.cpp index 1e3b5fc9a..f269fa274 100644 --- a/src/blackgui/components/simulatorcomponent.cpp +++ b/src/blackgui/components/simulatorcomponent.cpp @@ -12,6 +12,7 @@ #include "blackcore/context_settings.h" #include "blackcore/context_simulator.h" #include "blackcore/context_ownaircraft.h" +#include "blackcore/simulator.h" #include "blackmisc/iconlist.h" #include "blackmisc/avaircraft.h" @@ -101,7 +102,7 @@ namespace BlackGui { Q_ASSERT(this->getIContextSimulator()); if (!this->getIContextSimulator()) return; - QObject::connect(this->getIContextSimulator(), &IContextSimulator::connectionChanged, this, &CSimulatorComponent::ps_onSimulatorConnectionChanged); + QObject::connect(this->getIContextSimulator(), &IContextSimulator::simulatorStatusChanged, this, &CSimulatorComponent::ps_onSimulatorStatusChanged); this->setUpdateInterval(getUpdateIntervalMs()); if (!getIContextSimulator()->isConnected()) @@ -110,10 +111,9 @@ namespace BlackGui } } - void CSimulatorComponent::ps_onSimulatorConnectionChanged(bool isAvailable) + void CSimulatorComponent::ps_onSimulatorStatusChanged(int status) { - if (isAvailable) - { + if (status & ISimulator::Connected) { int intervalMs = getUpdateIntervalMs(); this->m_updateTimer->startTimer(intervalMs); } else { diff --git a/src/blackgui/components/simulatorcomponent.h b/src/blackgui/components/simulatorcomponent.h index c9eb877ba..7b442c78f 100644 --- a/src/blackgui/components/simulatorcomponent.h +++ b/src/blackgui/components/simulatorcomponent.h @@ -72,8 +72,8 @@ namespace BlackGui void runtimeHasBeenSet() override; private slots: - //! \copydoc ISimulator:: - void ps_onSimulatorConnectionChanged(bool isAvailable); + //! \copydoc ISimulator::simulatorStatusChanged + void ps_onSimulatorStatusChanged(int status); private: //! Update interval