refs #513 Make plugin selection persistent

* Saved listeners are started on application startup
* CVariant has support for QStringList
This commit is contained in:
Michał Garapich
2016-01-03 13:42:10 +01:00
parent f4eadddee7
commit 1a3e610cbf
9 changed files with 92 additions and 18 deletions

View File

@@ -425,6 +425,8 @@ namespace BlackCore
bool s = QMetaObject::invokeMethod(listener, "start", Qt::QueuedConnection); bool s = QMetaObject::invokeMethod(listener, "start", Qt::QueuedConnection);
Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke method"); Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke method");
Q_UNUSED(s); Q_UNUSED(s);
CLogMessage(this).info("Listening for simulator %1") << simulatorInfo.getIdentifier();
} }
void CContextSimulator::listenForAllSimulators() void CContextSimulator::listenForAllSimulators()
@@ -560,6 +562,21 @@ namespace BlackCore
m_simulatorPlugin.second->updateOwnSimulatorCockpit(ownAircraft, originator); 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 CPixmap CContextSimulator::iconForModel(const QString &modelString) const
{ {
if (m_simulatorPlugin.first.isUnspecified()) { return CPixmap(); } if (m_simulatorPlugin.first.isUnspecified()) { return CPixmap(); }

View File

@@ -16,6 +16,7 @@
#include "blackcore/contextsimulator.h" #include "blackcore/contextsimulator.h"
#include "blackcore/simulator.h" #include "blackcore/simulator.h"
#include "blackcore/weathermanager.h" #include "blackcore/weathermanager.h"
#include "blackcore/settings/application.h"
#include "blackmisc/simulation/simulatorplugininfolist.h" #include "blackmisc/simulation/simulatorplugininfolist.h"
#include "blackmisc/simulation/simulatedaircraftlist.h" #include "blackmisc/simulation/simulatedaircraftlist.h"
#include "blackmisc/network/textmessagelist.h" #include "blackmisc/network/textmessagelist.h"
@@ -161,6 +162,9 @@ namespace BlackCore
//! \remarks set by runtime, only to be used locally (not via DBus) //! \remarks set by runtime, only to be used locally (not via DBus)
void ps_updateSimulatorCockpitFromContext(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft, const BlackMisc::CIdentifier &originator); void ps_updateSimulatorCockpitFromContext(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft, const BlackMisc::CIdentifier &originator);
//! Reads list of enabled simulators, starts listeners
void restoreSimulatorPlugins();
private: private:
//! Load plugin, if required start listeners before //! Load plugin, if required start listeners before
bool loadSimulatorPlugin(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorInfo, bool withListeners); bool loadSimulatorPlugin(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorInfo, bool withListeners);
@@ -181,6 +185,7 @@ namespace BlackCore
CPluginManagerSimulator *m_plugins = nullptr; CPluginManagerSimulator *m_plugins = nullptr;
BlackMisc::CRegularThread m_listenersThread; BlackMisc::CRegularThread m_listenersThread;
BlackCore::CWeatherManager m_weatherManager { this }; BlackCore::CWeatherManager m_weatherManager { this };
BlackMisc::CSetting<BlackCore::Settings::Application::EnabledSimulators> m_enabledSimulators { this, &CContextSimulator::restoreSimulatorPlugins };
}; };
} // namespace } // namespace

View File

@@ -14,6 +14,7 @@
#include "blackmisc/settingscache.h" #include "blackmisc/settingscache.h"
#include "blackmisc/input/actionhotkeylist.h" #include "blackmisc/input/actionhotkeylist.h"
#include <QStringList>
namespace BlackCore namespace BlackCore
{ {
@@ -39,6 +40,27 @@ namespace BlackCore
return true; return true;
} }
}; };
//! Selected simulator plugins
struct EnabledSimulators : public BlackMisc::CSettingTrait<QStringList>
{
//! \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 } // ns
} // ns } // ns

View File

@@ -1,6 +1,6 @@
#include "settingssimulatorcomponent.h" #include "settingssimulatorcomponent.h"
#include "ui_settingssimulatorcomponent.h" #include "ui_settingssimulatorcomponent.h"
#include "blackcore/contextapplication.h"
#include "blackgui/guiapplication.h" #include "blackgui/guiapplication.h"
#include "blackgui/pluginconfigwindow.h" #include "blackgui/pluginconfigwindow.h"
#include "blackgui/plugindetailswindow.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_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); 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 // connects
connect(sGui->getIContextSimulator(), &IContextSimulator::simulatorPluginChanged, this, &CSettingsSimulatorComponent::ps_simulatorPluginChanged); connect(sGui->getIContextSimulator(), &IContextSimulator::simulatorPluginChanged, this, &CSettingsSimulatorComponent::ps_simulatorPluginChanged);
connect(this->ui->ps_EnabledSimulators, &CPluginSelector::pluginStateChanged, this, &CSettingsSimulatorComponent::ps_pluginStateChanged); 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_MaxAircraft, &QSpinBox::editingFinished, this, &CSettingsSimulatorComponent::ps_onApplyMaxRenderedAircraft);
connect(this->ui->sb_MaxDistance, &QSpinBox::editingFinished, this, &CSettingsSimulatorComponent::ps_onApplyMaxRenderedDistance); connect(this->ui->sb_MaxDistance, &QSpinBox::editingFinished, this, &CSettingsSimulatorComponent::ps_onApplyMaxRenderedDistance);
// values // list all available simulators
this->ps_simulatorPluginChanged(sGui->getIContextSimulator()->getSimulatorPluginInfo()); 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() CSettingsSimulatorComponent::~CSettingsSimulatorComponent()
{ } {
if (!m_enabledSimulators.isSaved())
sGui->getIContextApplication()->saveSettings(m_enabledSimulators.getKey());
}
void CSettingsSimulatorComponent::setGuiValues() void CSettingsSimulatorComponent::setGuiValues()
{ {
@@ -127,17 +129,18 @@ namespace BlackGui
return; return;
} }
if (enabled) auto e = m_enabledSimulators.get();
if (enabled && !e.contains(selected->getIdentifier()))
{ {
sGui->getIContextSimulator()->startSimulatorPlugin(*selected); e << selected->getIdentifier();
CLogMessage(this).info("Started listening for %1") << selected->getName();
} }
else else if (!enabled)
{ {
sGui->getIContextSimulator()->stopSimulatorPlugin(*selected); e.removeAll(selected->getIdentifier());
CLogMessage(this).info("Stopped listening for %1") << selected->getName();
} }
m_enabledSimulators.set(e);
// changing of GUI state will be done via received signal // changing of GUI state will be done via received signal
} }
@@ -273,6 +276,16 @@ namespace BlackGui
window->setAttribute(Qt::WA_DeleteOnClose); window->setAttribute(Qt::WA_DeleteOnClose);
window->show(); 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 } // namespace

View File

@@ -11,6 +11,7 @@
#define BLACKGUI_SETTINGSSIMULATORCOMPONENT_H #define BLACKGUI_SETTINGSSIMULATORCOMPONENT_H
#include "blackcore/pluginmanagersimulator.h" #include "blackcore/pluginmanagersimulator.h"
#include "blackcore/settings/application.h"
#include "blackgui/blackguiexport.h" #include "blackgui/blackguiexport.h"
#include "blackmisc/simulation/simulatorplugininfolist.h" #include "blackmisc/simulation/simulatorplugininfolist.h"
#include <QFrame> #include <QFrame>
@@ -62,6 +63,9 @@ namespace BlackGui
//! Show plugin config //! Show plugin config
void ps_showPluginConfig(const QString &identifier); void ps_showPluginConfig(const QString &identifier);
//! Select/deselect enabled/disabled plugins
void ps_reloadPluginConfig();
private: private:
//! Set the GUI values //! Set the GUI values
void setGuiValues(); void setGuiValues();
@@ -72,6 +76,7 @@ namespace BlackGui
QScopedPointer<Ui::CSettingsSimulatorComponent> ui; //!< UI QScopedPointer<Ui::CSettingsSimulatorComponent> ui; //!< UI
bool m_pluginLoaded = false; //!< plugin loaded bool m_pluginLoaded = false; //!< plugin loaded
BlackCore::CPluginManagerSimulator* m_plugins = nullptr; BlackCore::CPluginManagerSimulator* m_plugins = nullptr;
BlackMisc::CSetting<BlackCore::Settings::Application::EnabledSimulators> m_enabledSimulators { this, &CSettingsSimulatorComponent::ps_reloadPluginConfig };
}; };
} }

View File

@@ -39,6 +39,7 @@ namespace BlackGui
pw->setLayout(layout); pw->setLayout(layout);
QCheckBox *cb = new QCheckBox(name); QCheckBox *cb = new QCheckBox(name);
cb->setObjectName(identifier);
cb->setProperty("pluginIdentifier", identifier); cb->setProperty("pluginIdentifier", identifier);
connect(cb, &QCheckBox::stateChanged, this, &CPluginSelector::ps_handlePluginStateChange); connect(cb, &QCheckBox::stateChanged, this, &CPluginSelector::ps_handlePluginStateChange);
if (enabled) if (enabled)
@@ -71,6 +72,13 @@ namespace BlackGui
this->layout()->addWidget(pw); this->layout()->addWidget(pw);
} }
void CPluginSelector::setEnabled(const QString &identifier, bool enabled)
{
QCheckBox* cb = findChild<QCheckBox *>(identifier);
Q_ASSERT(cb);
cb->setChecked(enabled);
}
void CPluginSelector::ps_handlePluginStateChange() void CPluginSelector::ps_handlePluginStateChange()
{ {
QCheckBox *cb = qobject_cast<QCheckBox*>(sender()); QCheckBox *cb = qobject_cast<QCheckBox*>(sender());

View File

@@ -49,6 +49,9 @@ namespace BlackGui
//! \param enabled Defines whether the plugin is initially enabled or not //! \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); 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: private slots:
void ps_handlePluginStateChange(); void ps_handlePluginStateChange();

View File

@@ -112,6 +112,7 @@ namespace BlackMisc
case QVariant::DateTime: json.insert("value", m_v.toDateTime().toString(Qt::ISODate)); break; 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::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::Time: json.insert("value", m_v.toTime().toString(Qt::ISODate)); break;
case QVariant::StringList: json.insert("value", QJsonArray::fromStringList(m_v.toStringList())); break;
default: default:
try try
{ {
@@ -160,6 +161,7 @@ namespace BlackMisc
case QVariant::DateTime: m_v.setValue(QDateTime::fromString(json.value("value").toString(), Qt::ISODate)); break; 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::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::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: default:
try try
{ {

View File

@@ -185,7 +185,6 @@ namespace BlackSimPlugin
QDBusConnection m_conn { "default" }; QDBusConnection m_conn { "default" };
QDBusServiceWatcher *m_watcher { nullptr }; QDBusServiceWatcher *m_watcher { nullptr };
BlackMisc::CSetting<XBusServer> m_xbusServerSetting { this, &CSimulatorXPlaneListener::ps_xbusServerSettingChanged }; BlackMisc::CSetting<XBusServer> m_xbusServerSetting { this, &CSimulatorXPlaneListener::ps_xbusServerSettingChanged };
}; };
//! Factory for creating CSimulatorXPlane instance //! Factory for creating CSimulatorXPlane instance