diff --git a/ground/gcs/src/plugins/serialconnection/serialplugin.cpp b/ground/gcs/src/plugins/serialconnection/serialplugin.cpp index 829de4145..d4155e906 100644 --- a/ground/gcs/src/plugins/serialconnection/serialplugin.cpp +++ b/ground/gcs/src/plugins/serialconnection/serialplugin.cpp @@ -2,7 +2,8 @@ ****************************************************************************** * * @file serialplugin.cpp - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @addtogroup GCSPlugins GCS Plugins * @{ * @addtogroup SerialPlugin Serial Connection Plugin @@ -65,16 +66,14 @@ void SerialEnumerationThread::stop() } } -SerialConnection::SerialConnection() : +SerialConnection::SerialConnection(SerialPluginConfiguration *config) : serialHandle(NULL), enablePolling(true), m_enumerateThread(this), - m_deviceOpened(false) + m_deviceOpened(false), + m_config(config) { - m_config = new SerialPluginConfiguration("Serial Telemetry", this); - m_config->restoreSettings(); - - m_optionspage = new SerialPluginOptionsPage(m_config, this); + m_optionsPage = new SerialPluginOptionsPage(m_config, this); // Experimental: enable polling on all OS'es since there @@ -90,8 +89,8 @@ SerialConnection::SerialConnection() : // this, SLOT(onEnumerationChanged())); // #else // Other OSes do not send such signals: - QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()), - this, SLOT(onEnumerationChanged())); + QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()), this, SLOT(onEnumerationChanged())); + QObject::connect(m_optionsPage, SIGNAL(availableDevChanged()), this, SLOT(onEnumerationChanged())); m_enumerateThread.start(); // #endif } @@ -224,12 +223,35 @@ void SerialConnection::resumePolling() enablePolling = true; } -SerialPlugin::SerialPlugin() : m_connection(0) +SerialPlugin::SerialPlugin() : m_connection(0), m_config(0) {} SerialPlugin::~SerialPlugin() { - removeObject(m_connection->Optionspage()); + removeObject(m_connection->optionsPage()); +} + +bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString) +{ + Q_UNUSED(arguments); + Q_UNUSED(errorString); + + Core::ICore::instance()->readSettings(this); + + m_connection = new SerialConnection(m_config); + + // must manage this registration of child object ourselves + // if we use an autorelease here it causes the GCS to crash + // as it is deleting objects as the app closes... + addObject(m_connection->optionsPage()); + + // FIXME this is really a contrived way to save the settings... + // needs to be done centrally from + QObject::connect(m_connection, &SerialConnection::availableDevChanged, + [this]() { Core::ICore::instance()->saveSettings(this); } + ); + + return true; } void SerialPlugin::extensionsInitialized() @@ -237,14 +259,18 @@ void SerialPlugin::extensionsInitialized() addAutoReleasedObject(m_connection); } -bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString) +void SerialPlugin::readConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) { - Q_UNUSED(arguments); - Q_UNUSED(errorString); - m_connection = new SerialConnection(); - // must manage this registration of child object ourselves - // if we use an autorelease here it causes the GCS to crash - // as it is deleting objects as the app closes... - addObject(m_connection->Optionspage()); - return true; + Q_UNUSED(configInfo); + + m_config = new SerialPluginConfiguration("SerialConnection", settings, this); +} + +void SerialPlugin::saveConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) const +{ + Q_UNUSED(configInfo); + + if (m_config) { + m_config->saveConfig(settings); + } } diff --git a/ground/gcs/src/plugins/serialconnection/serialplugin.h b/ground/gcs/src/plugins/serialconnection/serialplugin.h index 3a78a03ee..eecb73bf5 100644 --- a/ground/gcs/src/plugins/serialconnection/serialplugin.h +++ b/ground/gcs/src/plugins/serialconnection/serialplugin.h @@ -2,7 +2,8 @@ ****************************************************************************** * * @file serialplugin.h - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @addtogroup GCSPlugins GCS Plugins * @{ * @addtogroup SerialPlugin Serial Connection Plugin @@ -29,12 +30,16 @@ #define SERIALPLUGIN_H // #include "serial_global.h" -#include -#include -#include "coreplugin/iconnection.h" #include +#include +#include "coreplugin/iconnection.h" + #include "serialpluginconfiguration.h" #include "serialpluginoptionspage.h" + +#include +#include + #include class IConnection; @@ -70,12 +75,10 @@ protected: * Plugin will add a instance of this class to the pool, * so the connection manager can use it. */ -// class SERIAL_EXPORT SerialConnection -class SerialConnection - : public Core::IConnection { +class SerialConnection : public Core::IConnection { Q_OBJECT public: - SerialConnection(); + SerialConnection(SerialPluginConfiguration *config); virtual ~SerialConnection(); virtual QList availableDevices(); @@ -91,37 +94,34 @@ public: { return m_deviceOpened; } - SerialPluginConfiguration *Config() const + + SerialPluginOptionsPage *optionsPage() const { - return m_config; - } - SerialPluginOptionsPage *Optionspage() const - { - return m_optionspage; + return m_optionsPage; } +protected slots: + void onEnumerationChanged(); + private: QSerialPort *serialHandle; bool enablePolling; - SerialPluginConfiguration *m_config; - SerialPluginOptionsPage *m_optionspage; - QList availablePorts(); - -protected slots: - void onEnumerationChanged(); - -protected: SerialEnumerationThread m_enumerateThread; bool m_deviceOpened; + + // FIXME m_config and m_optionsPage belong in IPConnectionPlugin + SerialPluginConfiguration *m_config; + SerialPluginOptionsPage *m_optionsPage; + + QList availablePorts(); }; -// class SERIAL_EXPORT SerialPlugin -class SerialPlugin : public ExtensionSystem::IPlugin { +class SerialPlugin : public Core::IConfigurablePlugin { Q_OBJECT - Q_PLUGIN_METADATA(IID "OpenPilot.Serial") + Q_PLUGIN_METADATA(IID "OpenPilot.Serial") public: SerialPlugin(); @@ -129,8 +129,13 @@ public: virtual bool initialize(const QStringList &arguments, QString *error_message); virtual void extensionsInitialized(); + + void readConfig(QSettings &settings, Core::UAVConfigInfo *configInfo); + void saveConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) const; + private: SerialConnection *m_connection; + SerialPluginConfiguration *m_config; }; #endif // SERIALPLUGIN_H diff --git a/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.cpp b/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.cpp index b106a15d4..979acb47d 100644 --- a/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.cpp +++ b/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.cpp @@ -2,7 +2,8 @@ ****************************************************************************** * * @file serialpluginconfiguration.cpp - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @see The GNU Public License (GPL) Version 3 * @addtogroup GCSPlugins GCS Plugins * @{ @@ -37,10 +38,14 @@ * Loads a saved configuration or defaults if non exist. * */ -SerialPluginConfiguration::SerialPluginConfiguration(QString classId, QObject *parent) : +SerialPluginConfiguration::SerialPluginConfiguration(QString classId, QSettings &settings, QObject *parent) : IUAVGadgetConfiguration(classId, parent), m_speed("57600") { + m_speed = settings.value("speed", "57600").toString(); + if (m_speed.isEmpty()) { + m_speed = "57600"; + } } SerialPluginConfiguration::SerialPluginConfiguration(const SerialPluginConfiguration &obj) : @@ -69,30 +74,3 @@ void SerialPluginConfiguration::saveConfig(QSettings &settings) const { settings.setValue("speed", m_speed); } - -void SerialPluginConfiguration::restoreSettings() -{ - QSettings settings; - - settings.beginGroup("SerialConnection"); - - QString str = settings.value("speed", "").toString(); - if (str.isEmpty()) { - m_speed = "57600"; - } else { - m_speed = str; - } - - settings.endGroup(); -} - -void SerialPluginConfiguration::saveSettings() const -{ - QSettings settings; - - settings.beginGroup("SerialConnection"); - - settings.setValue("speed", m_speed); - - settings.endGroup(); -} diff --git a/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.h b/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.h index 27ed4aea2..0594261b8 100644 --- a/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.h +++ b/ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.h @@ -2,7 +2,8 @@ ****************************************************************************** * * @file serialpluginconfiguration.h - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @see The GNU Public License (GPL) Version 3 * @addtogroup GCSPlugins GCS Plugins * @{ @@ -39,7 +40,7 @@ using namespace Core; class SerialPluginConfiguration : public IUAVGadgetConfiguration { Q_OBJECT public: - explicit SerialPluginConfiguration(QString classId, QObject *parent = 0); + explicit SerialPluginConfiguration(QString classId, QSettings &setting, QObject *parent = 0); explicit SerialPluginConfiguration(const SerialPluginConfiguration &obj); virtual ~SerialPluginConfiguration(); @@ -47,9 +48,6 @@ public: IUAVGadgetConfiguration *clone() const; void saveConfig(QSettings &settings) const; - void saveSettings() const; - void restoreSettings(); - QString speed() { return m_speed; diff --git a/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.cpp b/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.cpp index 8517d6602..a4a556e9c 100644 --- a/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.cpp +++ b/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.cpp @@ -2,7 +2,8 @@ ****************************************************************************** * * @file serialpluginoptionspage.cpp - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @see The GNU Public License (GPL) Version 3 * @addtogroup GCSPlugins GCS Plugins * @{ @@ -27,24 +28,23 @@ */ #include "serialpluginoptionspage.h" -#include "serialpluginconfiguration.h" + #include "ui_serialpluginoptions.h" + +#include "serialpluginconfiguration.h" #include "extensionsystem/pluginmanager.h" SerialPluginOptionsPage::SerialPluginOptionsPage(SerialPluginConfiguration *config, QObject *parent) : - IOptionsPage(parent), - m_config(config) + IOptionsPage(parent), m_page(0), m_config(config) {} // creates options page widget (uses the UI file) QWidget *SerialPluginOptionsPage::createPage(QWidget *parent) { - Q_UNUSED(parent); - options_page = new Ui::SerialPluginOptionsPage(); - // main widget - QWidget *optionsPageWidget = new QWidget; - // main layout - options_page->setupUi(optionsPageWidget); + m_page = new Ui::SerialPluginOptionsPage(); + QWidget *w = new QWidget(parent); + m_page->setupUi(w); + QStringList allowedSpeeds; allowedSpeeds << "1200" #ifdef Q_OS_UNIX @@ -75,10 +75,9 @@ QWidget *SerialPluginOptionsPage::createPage(QWidget *parent) #endif ; - - options_page->cb_speed->addItems(allowedSpeeds); - options_page->cb_speed->setCurrentIndex(options_page->cb_speed->findText(m_config->speed())); - return optionsPageWidget; + m_page->cb_speed->addItems(allowedSpeeds); + m_page->cb_speed->setCurrentIndex(m_page->cb_speed->findText(m_config->speed())); + return w; } /** @@ -89,12 +88,15 @@ QWidget *SerialPluginOptionsPage::createPage(QWidget *parent) */ void SerialPluginOptionsPage::apply() { - m_config->setSpeed(options_page->cb_speed->currentText()); - m_config->saveSettings(); + m_config->setSpeed(m_page->cb_speed->currentText()); + + // FIXME this signal is too low level (and duplicated all over the place) + // FIXME this signal will trigger (amongst other things) the saving of the configuration ! + emit availableDevChanged(); } void SerialPluginOptionsPage::finish() { - delete options_page; + delete m_page; } diff --git a/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.h b/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.h index 28e6b94b3..4760bbfa5 100644 --- a/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.h +++ b/ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.h @@ -2,7 +2,8 @@ ****************************************************************************** * * @file serialpluginoptionspage.h - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @see The GNU Public License (GPL) Version 3 * @addtogroup GCSPlugins GCS Plugins * @{ @@ -26,18 +27,12 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef SERIALpluginOPTIONSPAGE_H -#define SERIALpluginOPTIONSPAGE_H +#ifndef SERIALPLUGINOPTIONSPAGE_H +#define SERIALPLUGINOPTIONSPAGE_H #include "coreplugin/dialogs/ioptionspage.h" -#include "QString" -#include -#include -#include -namespace Core { -class IUAVpluginConfiguration; -} +#include "QString" class SerialPluginConfiguration; @@ -72,9 +67,12 @@ public: void apply(); void finish(); +signals: + void availableDevChanged(); + private: - Ui::SerialPluginOptionsPage *options_page; + Ui::SerialPluginOptionsPage *m_page; SerialPluginConfiguration *m_config; }; -#endif // SERIALpluginOPTIONSPAGE_H +#endif // SERIALPLUGINOPTIONSPAGE_H diff --git a/ground/gcs/src/share/configurations/default.xml b/ground/gcs/src/share/configurations/default.xml index fb57c12c6..e4e84b442 100644 --- a/ground/gcs/src/share/configurations/default.xml +++ b/ground/gcs/src/share/configurations/default.xml @@ -82,9 +82,6 @@ - - 57600 -