diff --git a/src/blackcore/contextsimulatorimpl.cpp b/src/blackcore/contextsimulatorimpl.cpp index 3fd234609..a7c77d658 100644 --- a/src/blackcore/contextsimulatorimpl.cpp +++ b/src/blackcore/contextsimulatorimpl.cpp @@ -425,6 +425,8 @@ namespace BlackCore bool s = QMetaObject::invokeMethod(listener, "start", Qt::QueuedConnection); Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke method"); Q_UNUSED(s); + + CLogMessage(this).info("Listening for simulator %1") << simulatorInfo.getIdentifier(); } void CContextSimulator::listenForAllSimulators() @@ -560,6 +562,21 @@ namespace BlackCore m_simulatorPlugin.second->updateOwnSimulatorCockpit(ownAircraft, originator); } + void CContextSimulator::restoreSimulatorPlugins() + { + stopSimulatorListeners(); + + auto enabledSimulators = m_enabledSimulators.get(); + auto allSimulators = m_plugins->getAvailableSimulatorPlugins(); + for (const CSimulatorPluginInfo& s: allSimulators) + { + if (enabledSimulators.contains(s.getIdentifier())) + { + startSimulatorPlugin(s); + } + } + } + CPixmap CContextSimulator::iconForModel(const QString &modelString) const { if (m_simulatorPlugin.first.isUnspecified()) { return CPixmap(); } diff --git a/src/blackcore/contextsimulatorimpl.h b/src/blackcore/contextsimulatorimpl.h index 559c224a3..93ac4d9c3 100644 --- a/src/blackcore/contextsimulatorimpl.h +++ b/src/blackcore/contextsimulatorimpl.h @@ -16,6 +16,7 @@ #include "blackcore/contextsimulator.h" #include "blackcore/simulator.h" #include "blackcore/weathermanager.h" +#include "blackcore/settings/application.h" #include "blackmisc/simulation/simulatorplugininfolist.h" #include "blackmisc/simulation/simulatedaircraftlist.h" #include "blackmisc/network/textmessagelist.h" @@ -161,6 +162,9 @@ namespace BlackCore //! \remarks set by runtime, only to be used locally (not via DBus) void ps_updateSimulatorCockpitFromContext(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft, const BlackMisc::CIdentifier &originator); + //! Reads list of enabled simulators, starts listeners + void restoreSimulatorPlugins(); + private: //! Load plugin, if required start listeners before bool loadSimulatorPlugin(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorInfo, bool withListeners); @@ -181,6 +185,7 @@ namespace BlackCore CPluginManagerSimulator *m_plugins = nullptr; BlackMisc::CRegularThread m_listenersThread; BlackCore::CWeatherManager m_weatherManager { this }; + BlackMisc::CSetting m_enabledSimulators { this, &CContextSimulator::restoreSimulatorPlugins }; }; } // namespace diff --git a/src/blackcore/settings/application.h b/src/blackcore/settings/application.h index 4f21a46f2..b5f5d4477 100644 --- a/src/blackcore/settings/application.h +++ b/src/blackcore/settings/application.h @@ -14,6 +14,7 @@ #include "blackmisc/settingscache.h" #include "blackmisc/input/actionhotkeylist.h" +#include namespace BlackCore { @@ -39,6 +40,27 @@ namespace BlackCore return true; } }; + + //! Selected simulator plugins + struct EnabledSimulators : public BlackMisc::CSettingTrait + { + //! \copydoc BlackCore::CSettingTrait::key + static const char *key() { return "application/enabledsimulators"; } + + //! \copydoc BlackCore::CSettingTrait::defaultValue + static const QStringList &defaultValue() + { + // All default simulators + static const QStringList enabledSimulators + { + QStringLiteral("org.swift-project.plugins.simulator.fs9"), + QStringLiteral("org.swift-project.plugins.simulator.fsx"), + QStringLiteral("org.swift-project.plugins.simulator.xplane") + }; + + return enabledSimulators; + } + }; } // ns } // ns } // ns diff --git a/src/blackgui/components/settingssimulatorcomponent.cpp b/src/blackgui/components/settingssimulatorcomponent.cpp index 6ffe57965..587175d8c 100644 --- a/src/blackgui/components/settingssimulatorcomponent.cpp +++ b/src/blackgui/components/settingssimulatorcomponent.cpp @@ -1,6 +1,6 @@ #include "settingssimulatorcomponent.h" #include "ui_settingssimulatorcomponent.h" - +#include "blackcore/contextapplication.h" #include "blackgui/guiapplication.h" #include "blackgui/pluginconfigwindow.h" #include "blackgui/plugindetailswindow.h" @@ -36,13 +36,6 @@ namespace BlackGui this->ui->led_RestrictedRendering->setValues(CLedWidget::Yellow, CLedWidget::Black, shape, "Limited", "Unlimited", 14); this->ui->led_RenderingEnabled->setValues(CLedWidget::Yellow, CLedWidget::Black, shape, "Rendering enabled", "No aircraft will be rendered", 14); - // set values - for (const auto &p : getAvailablePlugins()) - { - QString config = m_plugins->getPluginConfigId(p.getIdentifier()); - ui->ps_EnabledSimulators->addPlugin(p.getIdentifier(), p.getName(), !config.isEmpty(), false); - } - // connects connect(sGui->getIContextSimulator(), &IContextSimulator::simulatorPluginChanged, this, &CSettingsSimulatorComponent::ps_simulatorPluginChanged); connect(this->ui->ps_EnabledSimulators, &CPluginSelector::pluginStateChanged, this, &CSettingsSimulatorComponent::ps_pluginStateChanged); @@ -56,12 +49,21 @@ namespace BlackGui connect(this->ui->sb_MaxAircraft, &QSpinBox::editingFinished, this, &CSettingsSimulatorComponent::ps_onApplyMaxRenderedAircraft); connect(this->ui->sb_MaxDistance, &QSpinBox::editingFinished, this, &CSettingsSimulatorComponent::ps_onApplyMaxRenderedDistance); - // values - this->ps_simulatorPluginChanged(sGui->getIContextSimulator()->getSimulatorPluginInfo()); + // list all available simulators + for (const auto &p : getAvailablePlugins()) + { + QString config = m_plugins->getPluginConfigId(p.getIdentifier()); + ui->ps_EnabledSimulators->addPlugin(p.getIdentifier(), p.getName(), !config.isEmpty(), false); + } + + ps_reloadPluginConfig(); } CSettingsSimulatorComponent::~CSettingsSimulatorComponent() - { } + { + if (!m_enabledSimulators.isSaved()) + sGui->getIContextApplication()->saveSettings(m_enabledSimulators.getKey()); + } void CSettingsSimulatorComponent::setGuiValues() { @@ -127,17 +129,18 @@ namespace BlackGui return; } - if (enabled) + auto e = m_enabledSimulators.get(); + if (enabled && !e.contains(selected->getIdentifier())) { - sGui->getIContextSimulator()->startSimulatorPlugin(*selected); - CLogMessage(this).info("Started listening for %1") << selected->getName(); + e << selected->getIdentifier(); } - else + else if (!enabled) { - sGui->getIContextSimulator()->stopSimulatorPlugin(*selected); - CLogMessage(this).info("Stopped listening for %1") << selected->getName(); + e.removeAll(selected->getIdentifier()); } + m_enabledSimulators.set(e); + // changing of GUI state will be done via received signal } @@ -273,6 +276,16 @@ namespace BlackGui window->setAttribute(Qt::WA_DeleteOnClose); window->show(); } + + void CSettingsSimulatorComponent::ps_reloadPluginConfig() + { + // list all available simulators + auto enabledSimulators = m_enabledSimulators.get(); + for (const auto &p : getAvailablePlugins()) + { + ui->ps_EnabledSimulators->setEnabled(p.getIdentifier(), enabledSimulators.contains(p.getIdentifier())); + } + } } } // namespace diff --git a/src/blackgui/components/settingssimulatorcomponent.h b/src/blackgui/components/settingssimulatorcomponent.h index e1a233795..cc007f35a 100644 --- a/src/blackgui/components/settingssimulatorcomponent.h +++ b/src/blackgui/components/settingssimulatorcomponent.h @@ -11,6 +11,7 @@ #define BLACKGUI_SETTINGSSIMULATORCOMPONENT_H #include "blackcore/pluginmanagersimulator.h" +#include "blackcore/settings/application.h" #include "blackgui/blackguiexport.h" #include "blackmisc/simulation/simulatorplugininfolist.h" #include @@ -62,6 +63,9 @@ namespace BlackGui //! Show plugin config void ps_showPluginConfig(const QString &identifier); + //! Select/deselect enabled/disabled plugins + void ps_reloadPluginConfig(); + private: //! Set the GUI values void setGuiValues(); @@ -72,6 +76,7 @@ namespace BlackGui QScopedPointer ui; //!< UI bool m_pluginLoaded = false; //!< plugin loaded BlackCore::CPluginManagerSimulator* m_plugins = nullptr; + BlackMisc::CSetting m_enabledSimulators { this, &CSettingsSimulatorComponent::ps_reloadPluginConfig }; }; } diff --git a/src/blackgui/pluginselector.cpp b/src/blackgui/pluginselector.cpp index 733ab51ed..8978c0881 100644 --- a/src/blackgui/pluginselector.cpp +++ b/src/blackgui/pluginselector.cpp @@ -39,6 +39,7 @@ namespace BlackGui pw->setLayout(layout); QCheckBox *cb = new QCheckBox(name); + cb->setObjectName(identifier); cb->setProperty("pluginIdentifier", identifier); connect(cb, &QCheckBox::stateChanged, this, &CPluginSelector::ps_handlePluginStateChange); if (enabled) @@ -71,6 +72,13 @@ namespace BlackGui this->layout()->addWidget(pw); } + void CPluginSelector::setEnabled(const QString &identifier, bool enabled) + { + QCheckBox* cb = findChild(identifier); + Q_ASSERT(cb); + cb->setChecked(enabled); + } + void CPluginSelector::ps_handlePluginStateChange() { QCheckBox *cb = qobject_cast(sender()); diff --git a/src/blackgui/pluginselector.h b/src/blackgui/pluginselector.h index 5214d0a4e..c53a35a9c 100644 --- a/src/blackgui/pluginselector.h +++ b/src/blackgui/pluginselector.h @@ -49,6 +49,9 @@ namespace BlackGui //! \param enabled Defines whether the plugin is initially enabled or not void addPlugin(const QString &identifier, const QString &name, bool hasConfig = false, bool enabled = true); + //! Enables/disabled the given plugin. + void setEnabled(const QString &identifier, bool enabled); + private slots: void ps_handlePluginStateChange(); diff --git a/src/blackmisc/variant.cpp b/src/blackmisc/variant.cpp index dc223544b..fbeb1fbe7 100644 --- a/src/blackmisc/variant.cpp +++ b/src/blackmisc/variant.cpp @@ -112,6 +112,7 @@ namespace BlackMisc case QVariant::DateTime: json.insert("value", m_v.toDateTime().toString(Qt::ISODate)); break; case QVariant::Date: json.insert("value", m_v.toDate().toString(Qt::ISODate)); break; case QVariant::Time: json.insert("value", m_v.toTime().toString(Qt::ISODate)); break; + case QVariant::StringList: json.insert("value", QJsonArray::fromStringList(m_v.toStringList())); break; default: try { @@ -160,6 +161,7 @@ namespace BlackMisc case QVariant::DateTime: m_v.setValue(QDateTime::fromString(json.value("value").toString(), Qt::ISODate)); break; case QVariant::Date: m_v.setValue(QDate::fromString(json.value("value").toString(), Qt::ISODate)); break; case QVariant::Time: m_v.setValue(QTime::fromString(json.value("value").toString(), Qt::ISODate)); break; + case QVariant::StringList: m_v.setValue(QVariant(json.value("value").toArray().toVariantList()).toStringList()); break; default: try { diff --git a/src/plugins/simulator/xplane/simulatorxplane.h b/src/plugins/simulator/xplane/simulatorxplane.h index ecf24b174..a230a5533 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.h +++ b/src/plugins/simulator/xplane/simulatorxplane.h @@ -185,7 +185,6 @@ namespace BlackSimPlugin QDBusConnection m_conn { "default" }; QDBusServiceWatcher *m_watcher { nullptr }; BlackMisc::CSetting m_xbusServerSetting { this, &CSimulatorXPlaneListener::ps_xbusServerSettingChanged }; - }; //! Factory for creating CSimulatorXPlane instance