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

LP-419 config: made IPConnectionPlugin a configurable plugin

inspired from notification plugin
next is the serialconnection plugin
also made the code more conformant
This commit is contained in:
Philippe Renon 2016-10-06 00:25:35 +02:00
parent 068988741d
commit bba67f27d6
9 changed files with 165 additions and 190 deletions

View File

@ -12,10 +12,9 @@ namespace Core {
class CORE_EXPORT IConfigurablePlugin : public ExtensionSystem::IPlugin {
Q_OBJECT
public:
// IConfigurablePlugin(QObject *parent = 0){}
virtual ~IConfigurablePlugin() {}
virtual void readConfig(QSettings &settings, UAVConfigInfo *configInfo) = 0;
virtual void saveConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) const = 0;
virtual void saveConfig(QSettings &settings, UAVConfigInfo *configInfo) const = 0;
};
} // namespace Core

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnection_global.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnection_global.h
* @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 IPConnPlugin IP Telemetry Plugin
@ -25,8 +26,8 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef IPconnection_GLOBAL_H
#define IPconnection_GLOBAL_H
#ifndef IPCONNECTION_GLOBAL_H
#define IPCONNECTION_GLOBAL_H
#include <QtCore/qglobal.h>
@ -36,4 +37,4 @@
# define IPconnection_EXPORT Q_DECL_IMPORT
#endif
#endif // IPconnection_GLOBAL_H
#endif // IPCONNECTION_GLOBAL_H

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnectionconfiguration.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnectionconfiguration.cpp
* @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 IPConnPlugin IP Telemetry Plugin
@ -29,20 +30,20 @@
#include <coreplugin/icore.h>
IPconnectionConfiguration::IPconnectionConfiguration(QString classId, QObject *parent) :
IUAVGadgetConfiguration(classId, parent),
m_HostName("127.0.0.1"),
m_Port(1000),
m_UseTCP(1)
IPconnectionConfiguration::IPconnectionConfiguration(QString classId, QSettings &settings, QObject *parent) :
IUAVGadgetConfiguration(classId, parent)
{
m_hostName = settings.value("HostName", "").toString();
m_port = settings.value("Port", 9000).toInt();
m_useTCP = settings.value("UseTCP", true).toInt();
}
IPconnectionConfiguration::IPconnectionConfiguration(const IPconnectionConfiguration &obj) :
IUAVGadgetConfiguration(obj.classId(), obj.parent())
{
m_HostName = obj.m_HostName;
m_Port = obj.m_Port;
m_UseTCP = obj.m_UseTCP;
m_hostName = obj.m_hostName;
m_port = obj.m_port;
m_useTCP = obj.m_useTCP;
}
IPconnectionConfiguration::~IPconnectionConfiguration()
@ -59,40 +60,7 @@ IUAVGadgetConfiguration *IPconnectionConfiguration::clone() const
*/
void IPconnectionConfiguration::saveConfig(QSettings &settings) const
{
settings.setValue("port", m_Port);
settings.setValue("hostName", m_HostName);
settings.setValue("useTCP", m_UseTCP);
}
void IPconnectionConfiguration::saveSettings() const
{
QSettings settings;
settings.beginGroup("IPconnection");
settings.beginWriteArray("Current");
settings.setArrayIndex(0);
settings.setValue("HostName", m_HostName);
settings.setValue("Port", m_Port);
settings.setValue("UseTCP", m_UseTCP);
settings.endArray();
settings.endGroup();
}
void IPconnectionConfiguration::restoreSettings()
{
QSettings settings;
settings.beginGroup("IPconnection");
settings.beginReadArray("Current");
settings.setArrayIndex(0);
m_HostName = settings.value("HostName", "").toString();
m_Port = settings.value("Port", 0).toInt();
m_UseTCP = settings.value("UseTCP", 0).toInt();
settings.endArray();
settings.endGroup();
settings.setValue("HostName", m_hostName);
settings.setValue("Port", m_port);
settings.setValue("UseTCP", m_useTCP);
}

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnectionconfiguration.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnectionconfiguration.h
* @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 IPConnPlugin IP Telemetry Plugin
@ -25,8 +26,8 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef IPconnectionCONFIGURATION_H
#define IPconnectionCONFIGURATION_H
#ifndef IPCONFIGURATIONCONFIGURATION_H
#define IPCONFIGURATIONCONFIGURATION_H
#include <coreplugin/iuavgadgetconfiguration.h>
@ -35,12 +36,12 @@
using namespace Core;
class IPconnectionConfiguration : public IUAVGadgetConfiguration {
Q_OBJECT Q_PROPERTY(QString HostName READ HostName WRITE setHostName)
Q_PROPERTY(int Port READ Port WRITE setPort)
Q_PROPERTY(int UseTCP READ UseTCP WRITE setUseTCP)
Q_OBJECT Q_PROPERTY(QString HostName READ hostName WRITE setHostName)
Q_PROPERTY(int Port READ port WRITE setPort)
Q_PROPERTY(int UseTCP READ useTCP WRITE setUseTCP)
public:
explicit IPconnectionConfiguration(QString classId, QObject *parent = 0);
explicit IPconnectionConfiguration(QString classId, QSettings &settings, QObject *parent = 0);
explicit IPconnectionConfiguration(const IPconnectionConfiguration &obj);
virtual ~IPconnectionConfiguration();
@ -48,40 +49,37 @@ public:
IUAVGadgetConfiguration *clone() const;
void saveConfig(QSettings &settings) const;
void saveSettings() const;
void restoreSettings();
QString HostName() const
QString hostName() const
{
return m_HostName;
return m_hostName;
}
int Port() const
int port() const
{
return m_Port;
return m_port;
}
int UseTCP() const
int useTCP() const
{
return m_UseTCP;
return m_useTCP;
}
public slots:
void setHostName(QString HostName)
void setHostName(QString hostName)
{
m_HostName = HostName;
m_hostName = hostName;
}
void setPort(int Port)
void setPort(int port)
{
m_Port = Port;
m_port = port;
}
void setUseTCP(int UseTCP)
void setUseTCP(int useTCP)
{
m_UseTCP = UseTCP;
m_useTCP = useTCP;
}
private:
QString m_HostName;
int m_Port;
int m_UseTCP;
QString m_hostName;
int m_port;
int m_useTCP;
};
#endif // IPconnectionCONFIGURATION_H
#endif // IPCONFIGURATIONCONFIGURATION_H

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnectionoptionspage.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnectionoptionspage.cpp
* @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 IPConnPlugin IP Telemetry Plugin
@ -26,21 +27,13 @@
*/
#include "ipconnectionoptionspage.h"
#include "ipconnectionconfiguration.h"
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QRadioButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "ui_ipconnectionoptionspage.h"
#include "ipconnectionconfiguration.h"
IPconnectionOptionsPage::IPconnectionOptionsPage(IPconnectionConfiguration *config, QObject *parent) :
IOptionsPage(parent),
m_config(config)
IOptionsPage(parent), m_config(config), m_page(0)
{}
IPconnectionOptionsPage::~IPconnectionOptionsPage()
@ -52,10 +45,10 @@ QWidget *IPconnectionOptionsPage::createPage(QWidget *parent)
QWidget *w = new QWidget(parent);
m_page->setupUi(w);
m_page->Port->setValue(m_config->Port());
m_page->HostName->setText(m_config->HostName());
m_page->UseTCP->setChecked(m_config->UseTCP() ? true : false);
m_page->UseUDP->setChecked(m_config->UseTCP() ? false : true);
m_page->Port->setValue(m_config->port());
m_page->HostName->setText(m_config->hostName());
m_page->UseTCP->setChecked(m_config->useTCP() ? true : false);
m_page->UseUDP->setChecked(m_config->useTCP() ? false : true);
return w;
}
@ -65,8 +58,9 @@ void IPconnectionOptionsPage::apply()
m_config->setPort(m_page->Port->value());
m_config->setHostName(m_page->HostName->text());
m_config->setUseTCP(m_page->UseTCP->isChecked() ? 1 : 0);
m_config->saveSettings();
// 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();
}

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnectionoptionspage.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnectionoptionspage.h
* @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 IPConnPlugin IP Telemetry Plugin
@ -25,17 +26,13 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef IPconnectionOPTIONSPAGE_H
#define IPconnectionOPTIONSPAGE_H
#ifndef IPCONNECTIONOPTIONSPAGE_H
#define IPCONNECTIONOPTIONSPAGE_H
#include "coreplugin/dialogs/ioptionspage.h"
class IPconnectionConfiguration;
namespace Core {
class IUAVGadgetConfiguration;
}
namespace Ui {
class IPconnectionOptionsPage;
}
@ -72,10 +69,9 @@ public:
signals:
void availableDevChanged();
public slots:
private:
IPconnectionConfiguration *m_config;
Ui::IPconnectionOptionsPage *m_page;
};
#endif // IPconnectionOPTIONSPAGE_H
#endif // IPCONNECTIONOPTIONSPAGE_H

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnectionplugin.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnectionplugin.cpp
* @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 IPConnPlugin IP Telemetry Plugin
@ -33,6 +34,7 @@
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/icore.h>
#include "ipconnection_internal.h"
#include <coreplugin/threadmanager.h>
#include <QtCore/QtPlugin>
#include <QMainWindow>
@ -42,7 +44,6 @@
#include <QtNetwork/QUdpSocket>
#include <QWaitCondition>
#include <QMutex>
#include <coreplugin/threadmanager.h>
#include <QDebug>
@ -117,14 +118,11 @@ void IPConnection::onCloseDevice(QAbstractSocket *ipSocket)
IPConnection *connection = 0;
IPconnectionConnection::IPconnectionConnection()
IPconnectionConnection::IPconnectionConnection(IPconnectionConfiguration *config) : m_config(config)
{
ipSocket = NULL;
// create all our objects
m_config = new IPconnectionConfiguration("IP Network Telemetry", this);
m_config->restoreSettings();
m_ipSocket = NULL;
m_optionspage = new IPconnectionOptionsPage(m_config, this);
m_optionsPage = new IPconnectionOptionsPage(m_config, this);
if (!connection) {
connection = new IPConnection(this);
@ -132,17 +130,17 @@ IPconnectionConnection::IPconnectionConnection()
// just signal whenever we have a device event...
QMainWindow *mw = Core::ICore::instance()->mainWindow();
QObject::connect(mw, SIGNAL(deviceChange()),
this, SLOT(onEnumerationChanged()));
QObject::connect(m_optionspage, SIGNAL(availableDevChanged()),
this, SLOT(onEnumerationChanged()));
QObject::connect(mw, SIGNAL(deviceChange()), this, SLOT(onEnumerationChanged()));
QObject::connect(m_optionsPage, SIGNAL(availableDevChanged()), this, SLOT(onEnumerationChanged()));
}
IPconnectionConnection::~IPconnectionConnection()
{ // clean up out resources...
if (ipSocket) {
ipSocket->close();
delete (ipSocket);
{
// clean up out resources...
if (m_ipSocket) {
m_ipSocket->close();
delete m_ipSocket;
m_ipSocket = NULL;
}
if (connection) {
delete connection;
@ -151,21 +149,20 @@ IPconnectionConnection::~IPconnectionConnection()
}
void IPconnectionConnection::onEnumerationChanged()
{ // no change from serial plugin
{
emit availableDevChanged(this);
}
QList <Core::IConnection::device> IPconnectionConnection::availableDevices()
{
QList <Core::IConnection::device> list;
device d;
if (m_config->HostName().length() > 1) {
d.displayName = (const QString)m_config->HostName();
if (m_config->hostName().length() > 1) {
d.displayName = (const QString)m_config->hostName();
} else {
d.displayName = "Unconfigured";
d.displayName = tr("Unconfigured");
}
d.name = (const QString)m_config->HostName();
d.name = (const QString)m_config->hostName();
// we only have one "device" as defined by the configuration m_config
list.append(d);
@ -174,57 +171,57 @@ QList <Core::IConnection::device> IPconnectionConnection::availableDevices()
QIODevice *IPconnectionConnection::openDevice(const QString &)
{
QString HostName;
int Port;
bool UseTCP;
QString hostName;
int port;
bool useTCP;
QMessageBox msgBox;
// get the configuration info
HostName = m_config->HostName();
Port = m_config->Port();
UseTCP = m_config->UseTCP();
hostName = m_config->hostName();
port = m_config->port();
useTCP = m_config->useTCP();
if (ipSocket) {
if (m_ipSocket) {
// Andrew: close any existing socket... this should never occur
ipConMutex.lock();
emit CloseSocket(ipSocket);
emit CloseSocket(m_ipSocket);
closeDeviceWait.wait(&ipConMutex);
ipConMutex.unlock();
ipSocket = NULL;
m_ipSocket = NULL;
}
ipConMutex.lock();
emit CreateSocket(HostName, Port, UseTCP);
emit CreateSocket(hostName, port, useTCP);
openDeviceWait.wait(&ipConMutex);
ipConMutex.unlock();
ipSocket = ret;
if (ipSocket == NULL) {
m_ipSocket = ret;
if (m_ipSocket == NULL) {
msgBox.setText((const QString)errorMsg);
msgBox.exec();
}
return ipSocket;
return m_ipSocket;
}
void IPconnectionConnection::closeDevice(const QString &)
{
if (ipSocket) {
if (m_ipSocket) {
ipConMutex.lock();
emit CloseSocket(ipSocket);
emit CloseSocket(m_ipSocket);
closeDeviceWait.wait(&ipConMutex);
ipConMutex.unlock();
ipSocket = NULL;
m_ipSocket = NULL;
}
}
QString IPconnectionConnection::connectionName()
{ // updated from serial plugin
{
return QString("Network telemetry port");
}
QString IPconnectionConnection::shortName()
{ // updated from serial plugin
if (m_config->UseTCP()) {
{
if (m_config->useTCP()) {
return QString("TCP");
} else {
return QString("UDP");
@ -232,13 +229,37 @@ QString IPconnectionConnection::shortName()
}
IPconnectionPlugin::IPconnectionPlugin()
{ // no change from serial plugin
}
IPconnectionPlugin::IPconnectionPlugin() : m_connection(0), m_config(0)
{}
IPconnectionPlugin::~IPconnectionPlugin()
{ // manually remove the options page object
removeObject(m_connection->Optionspage());
{
// manually remove the options page object
removeObject(m_connection->optionsPage());
}
bool IPconnectionPlugin::initialize(const QStringList &arguments, QString *errorString)
{
Q_UNUSED(arguments);
Q_UNUSED(errorString);
qDebug() << "*** INIT";
Core::ICore::instance()->readSettings(this);
m_connection = new IPconnectionConnection(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, &IPconnectionConnection::availableDevChanged,
[this]() { Core::ICore::instance()->saveSettings(this); }
);
return true;
}
void IPconnectionPlugin::extensionsInitialized()
@ -246,15 +267,18 @@ void IPconnectionPlugin::extensionsInitialized()
addAutoReleasedObject(m_connection);
}
bool IPconnectionPlugin::initialize(const QStringList &arguments, QString *errorString)
void IPconnectionPlugin::readConfig(QSettings &settings, Core::UAVConfigInfo *configInfo)
{
Q_UNUSED(arguments);
Q_UNUSED(errorString);
m_connection = new IPconnectionConnection();
// 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());
Q_UNUSED(configInfo);
return true;
m_config = new IPconnectionConfiguration("IPConnection", settings, this);
}
void IPconnectionPlugin::saveConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) const
{
Q_UNUSED(configInfo);
if (m_config) {
m_config->saveConfig(settings);
}
}

View File

@ -1,8 +1,9 @@
/**
******************************************************************************
*
* @file IPconnectionplugin.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @file ipconnectionplugin.h
* @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 IPConnPlugin IP Telemetry Plugin
@ -25,15 +26,16 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef IPconnectionPLUGIN_H
#define IPconnectionPLUGIN_H
#ifndef IPCONNECTIONPLUGIN_H
#define IPCONNECTIONPLUGIN_H
#include "ipconnection_global.h"
#include "ipconnectionoptionspage.h"
#include "ipconnectionconfiguration.h"
#include "coreplugin/iconnection.h"
#include <extensionsystem/iplugin.h>
// #include <QtCore/QSettings>
#include <coreplugin/iconfigurableplugin.h>
#include <coreplugin/iconnection.h>
class QAbstractSocket;
class QTcpSocket;
@ -50,7 +52,7 @@ class IPconnection_EXPORT IPconnectionConnection : public Core::IConnection {
Q_OBJECT
public:
IPconnectionConnection();
IPconnectionConnection(IPconnectionConfiguration *config);
virtual ~IPconnectionConnection();
virtual QList <Core::IConnection::device> availableDevices();
@ -60,29 +62,28 @@ public:
virtual QString connectionName();
virtual QString shortName();
IPconnectionConfiguration *Config() const
IPconnectionOptionsPage *optionsPage() const
{
return m_config;
}
IPconnectionOptionsPage *Optionspage() const
{
return m_optionspage;
return m_optionsPage;
}
protected slots:
void onEnumerationChanged();
signals: // For the benefit of IPConnection
signals:
// For the benefit of IPConnection
// FIXME change to camel case
void CreateSocket(QString HostName, int Port, bool UseTCP);
void CloseSocket(QAbstractSocket *socket);
private:
QAbstractSocket *ipSocket;
QAbstractSocket *m_ipSocket;
// FIXME m_config and m_optionsPage belong in IPConnectionPlugin
IPconnectionConfiguration *m_config;
IPconnectionOptionsPage *m_optionspage;
IPconnectionOptionsPage *m_optionsPage;
};
class IPconnection_EXPORT IPconnectionPlugin : public ExtensionSystem::IPlugin {
class IPconnection_EXPORT IPconnectionPlugin : public Core::IConfigurablePlugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID "OpenPilot.IPconnection")
@ -92,9 +93,12 @@ 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:
IPconnectionConnection *m_connection;
IPconnectionConfiguration *m_config;
};
#endif // IPconnectionPLUGIN_H
#endif // IPCONNECTIONPLUGIN_H

View File

@ -16,15 +16,6 @@
<SplitterPosition>150</SplitterPosition>
</Settings>
</General>
<IPconnection>
<Current>
<arr_1>
<Port>9000</Port>
<UseTCP>0</UseTCP>
</arr_1>
<size>1</size>
</Current>
</IPconnection>
<KeyBindings>
<size>0</size>
</KeyBindings>