1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-20 10:54:14 +01:00

LP-419 config: made SerialPlugin a configurable plugin

inspired from notification plugin
This commit is contained in:
Philippe Renon 2016-10-06 01:30:27 +02:00
parent 2c14920ab0
commit 9f3aae1f09
7 changed files with 115 additions and 111 deletions

View File

@ -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);
}
}

View File

@ -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 <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include "coreplugin/iconnection.h"
#include <extensionsystem/iplugin.h>
#include <coreplugin/iconfigurableplugin.h>
#include "coreplugin/iconnection.h"
#include "serialpluginconfiguration.h"
#include "serialpluginoptionspage.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QThread>
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 <Core::IConnection::device> 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<QSerialPortInfo> 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<QSerialPortInfo> 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

View File

@ -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();
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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 <QStringList>
#include <QDebug>
#include <QFont>
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

View File

@ -82,9 +82,6 @@
</data>
</SoundNotifyPlugin>
</Plugins>
<SerialConnection>
<speed>57600</speed>
</SerialConnection>
<UAVGadgetConfigurations>
<ConfigGadget>
<default>