Skip to content

Commit 9f3aae1

Browse files
committed
LP-419 config: made SerialPlugin a configurable plugin
inspired from notification plugin
1 parent 2c14920 commit 9f3aae1

7 files changed

Lines changed: 113 additions & 109 deletions

File tree

ground/gcs/src/plugins/serialconnection/serialplugin.cpp

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file serialplugin.cpp
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
67
* @addtogroup GCSPlugins GCS Plugins
78
* @{
89
* @addtogroup SerialPlugin Serial Connection Plugin
@@ -65,16 +66,14 @@ void SerialEnumerationThread::stop()
6566
}
6667
}
6768

68-
SerialConnection::SerialConnection() :
69+
SerialConnection::SerialConnection(SerialPluginConfiguration *config) :
6970
serialHandle(NULL),
7071
enablePolling(true),
7172
m_enumerateThread(this),
72-
m_deviceOpened(false)
73+
m_deviceOpened(false),
74+
m_config(config)
7375
{
74-
m_config = new SerialPluginConfiguration("Serial Telemetry", this);
75-
m_config->restoreSettings();
76-
77-
m_optionspage = new SerialPluginOptionsPage(m_config, this);
76+
m_optionsPage = new SerialPluginOptionsPage(m_config, this);
7877

7978

8079
// Experimental: enable polling on all OS'es since there
@@ -90,8 +89,8 @@ SerialConnection::SerialConnection() :
9089
// this, SLOT(onEnumerationChanged()));
9190
// #else
9291
// Other OSes do not send such signals:
93-
QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()),
94-
this, SLOT(onEnumerationChanged()));
92+
QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()), this, SLOT(onEnumerationChanged()));
93+
QObject::connect(m_optionsPage, SIGNAL(availableDevChanged()), this, SLOT(onEnumerationChanged()));
9594
m_enumerateThread.start();
9695
// #endif
9796
}
@@ -224,27 +223,54 @@ void SerialConnection::resumePolling()
224223
enablePolling = true;
225224
}
226225

227-
SerialPlugin::SerialPlugin() : m_connection(0)
226+
SerialPlugin::SerialPlugin() : m_connection(0), m_config(0)
228227
{}
229228

230229
SerialPlugin::~SerialPlugin()
231230
{
232-
removeObject(m_connection->Optionspage());
233-
}
234-
235-
void SerialPlugin::extensionsInitialized()
236-
{
237-
addAutoReleasedObject(m_connection);
231+
removeObject(m_connection->optionsPage());
238232
}
239233

240234
bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString)
241235
{
242236
Q_UNUSED(arguments);
243237
Q_UNUSED(errorString);
244-
m_connection = new SerialConnection();
238+
239+
Core::ICore::instance()->readSettings(this);
240+
241+
m_connection = new SerialConnection(m_config);
242+
245243
// must manage this registration of child object ourselves
246244
// if we use an autorelease here it causes the GCS to crash
247245
// as it is deleting objects as the app closes...
248-
addObject(m_connection->Optionspage());
246+
addObject(m_connection->optionsPage());
247+
248+
// FIXME this is really a contrived way to save the settings...
249+
// needs to be done centrally from
250+
QObject::connect(m_connection, &SerialConnection::availableDevChanged,
251+
[this]() { Core::ICore::instance()->saveSettings(this); }
252+
);
253+
249254
return true;
250255
}
256+
257+
void SerialPlugin::extensionsInitialized()
258+
{
259+
addAutoReleasedObject(m_connection);
260+
}
261+
262+
void SerialPlugin::readConfig(QSettings &settings, Core::UAVConfigInfo *configInfo)
263+
{
264+
Q_UNUSED(configInfo);
265+
266+
m_config = new SerialPluginConfiguration("SerialConnection", settings, this);
267+
}
268+
269+
void SerialPlugin::saveConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) const
270+
{
271+
Q_UNUSED(configInfo);
272+
273+
if (m_config) {
274+
m_config->saveConfig(settings);
275+
}
276+
}

ground/gcs/src/plugins/serialconnection/serialplugin.h

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file serialplugin.h
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
67
* @addtogroup GCSPlugins GCS Plugins
78
* @{
89
* @addtogroup SerialPlugin Serial Connection Plugin
@@ -29,12 +30,16 @@
2930
#define SERIALPLUGIN_H
3031

3132
// #include "serial_global.h"
32-
#include <QtSerialPort/QSerialPort>
33-
#include <QtSerialPort/QSerialPortInfo>
34-
#include "coreplugin/iconnection.h"
3533
#include <extensionsystem/iplugin.h>
34+
#include <coreplugin/iconfigurableplugin.h>
35+
#include "coreplugin/iconnection.h"
36+
3637
#include "serialpluginconfiguration.h"
3738
#include "serialpluginoptionspage.h"
39+
40+
#include <QtSerialPort/QSerialPort>
41+
#include <QtSerialPort/QSerialPortInfo>
42+
3843
#include <QThread>
3944

4045
class IConnection;
@@ -70,12 +75,10 @@ class SerialEnumerationThread : public QThread {
7075
* Plugin will add a instance of this class to the pool,
7176
* so the connection manager can use it.
7277
*/
73-
// class SERIAL_EXPORT SerialConnection
74-
class SerialConnection
75-
: public Core::IConnection {
78+
class SerialConnection : public Core::IConnection {
7679
Q_OBJECT
7780
public:
78-
SerialConnection();
81+
SerialConnection(SerialPluginConfiguration *config);
7982
virtual ~SerialConnection();
8083

8184
virtual QList <Core::IConnection::device> availableDevices();
@@ -91,46 +94,48 @@ class SerialConnection
9194
{
9295
return m_deviceOpened;
9396
}
94-
SerialPluginConfiguration *Config() const
95-
{
96-
return m_config;
97-
}
98-
SerialPluginOptionsPage *Optionspage() const
97+
98+
SerialPluginOptionsPage *optionsPage() const
9999
{
100-
return m_optionspage;
100+
return m_optionsPage;
101101
}
102102

103+
protected slots:
104+
void onEnumerationChanged();
105+
103106

104107
private:
105108
QSerialPort *serialHandle;
106109
bool enablePolling;
107-
SerialPluginConfiguration *m_config;
108-
SerialPluginOptionsPage *m_optionspage;
109-
110-
QList<QSerialPortInfo> availablePorts();
111110

112-
protected slots:
113-
void onEnumerationChanged();
114-
115-
protected:
116111
SerialEnumerationThread m_enumerateThread;
117112
bool m_deviceOpened;
113+
114+
// FIXME m_config and m_optionsPage belong in IPConnectionPlugin
115+
SerialPluginConfiguration *m_config;
116+
SerialPluginOptionsPage *m_optionsPage;
117+
118+
QList<QSerialPortInfo> availablePorts();
118119
};
119120

120121

121-
// class SERIAL_EXPORT SerialPlugin
122-
class SerialPlugin : public ExtensionSystem::IPlugin {
122+
class SerialPlugin : public Core::IConfigurablePlugin {
123123
Q_OBJECT
124-
Q_PLUGIN_METADATA(IID "OpenPilot.Serial")
124+
Q_PLUGIN_METADATA(IID "OpenPilot.Serial")
125125

126126
public:
127127
SerialPlugin();
128128
~SerialPlugin();
129129

130130
virtual bool initialize(const QStringList &arguments, QString *error_message);
131131
virtual void extensionsInitialized();
132+
133+
void readConfig(QSettings &settings, Core::UAVConfigInfo *configInfo);
134+
void saveConfig(QSettings &settings, Core::UAVConfigInfo *configInfo) const;
135+
132136
private:
133137
SerialConnection *m_connection;
138+
SerialPluginConfiguration *m_config;
134139
};
135140

136141
#endif // SERIALPLUGIN_H

ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.cpp

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file serialpluginconfiguration.cpp
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
67
* @see The GNU Public License (GPL) Version 3
78
* @addtogroup GCSPlugins GCS Plugins
89
* @{
@@ -37,10 +38,14 @@
3738
* Loads a saved configuration or defaults if non exist.
3839
*
3940
*/
40-
SerialPluginConfiguration::SerialPluginConfiguration(QString classId, QObject *parent) :
41+
SerialPluginConfiguration::SerialPluginConfiguration(QString classId, QSettings &settings, QObject *parent) :
4142
IUAVGadgetConfiguration(classId, parent),
4243
m_speed("57600")
4344
{
45+
m_speed = settings.value("speed", "57600").toString();
46+
if (m_speed.isEmpty()) {
47+
m_speed = "57600";
48+
}
4449
}
4550

4651
SerialPluginConfiguration::SerialPluginConfiguration(const SerialPluginConfiguration &obj) :
@@ -69,30 +74,3 @@ void SerialPluginConfiguration::saveConfig(QSettings &settings) const
6974
{
7075
settings.setValue("speed", m_speed);
7176
}
72-
73-
void SerialPluginConfiguration::restoreSettings()
74-
{
75-
QSettings settings;
76-
77-
settings.beginGroup("SerialConnection");
78-
79-
QString str = settings.value("speed", "").toString();
80-
if (str.isEmpty()) {
81-
m_speed = "57600";
82-
} else {
83-
m_speed = str;
84-
}
85-
86-
settings.endGroup();
87-
}
88-
89-
void SerialPluginConfiguration::saveSettings() const
90-
{
91-
QSettings settings;
92-
93-
settings.beginGroup("SerialConnection");
94-
95-
settings.setValue("speed", m_speed);
96-
97-
settings.endGroup();
98-
}

ground/gcs/src/plugins/serialconnection/serialpluginconfiguration.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file serialpluginconfiguration.h
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
67
* @see The GNU Public License (GPL) Version 3
78
* @addtogroup GCSPlugins GCS Plugins
89
* @{
@@ -39,17 +40,14 @@ using namespace Core;
3940
class SerialPluginConfiguration : public IUAVGadgetConfiguration {
4041
Q_OBJECT
4142
public:
42-
explicit SerialPluginConfiguration(QString classId, QObject *parent = 0);
43+
explicit SerialPluginConfiguration(QString classId, QSettings &setting, QObject *parent = 0);
4344
explicit SerialPluginConfiguration(const SerialPluginConfiguration &obj);
4445

4546
virtual ~SerialPluginConfiguration();
4647

4748
IUAVGadgetConfiguration *clone() const;
4849
void saveConfig(QSettings &settings) const;
4950

50-
void saveSettings() const;
51-
void restoreSettings();
52-
5351
QString speed()
5452
{
5553
return m_speed;

ground/gcs/src/plugins/serialconnection/serialpluginoptionspage.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file serialpluginoptionspage.cpp
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
67
* @see The GNU Public License (GPL) Version 3
78
* @addtogroup GCSPlugins GCS Plugins
89
* @{
@@ -27,24 +28,23 @@
2728
*/
2829

2930
#include "serialpluginoptionspage.h"
30-
#include "serialpluginconfiguration.h"
31+
3132
#include "ui_serialpluginoptions.h"
33+
34+
#include "serialpluginconfiguration.h"
3235
#include "extensionsystem/pluginmanager.h"
3336

3437
SerialPluginOptionsPage::SerialPluginOptionsPage(SerialPluginConfiguration *config, QObject *parent) :
35-
IOptionsPage(parent),
36-
m_config(config)
38+
IOptionsPage(parent), m_page(0), m_config(config)
3739
{}
3840

3941
// creates options page widget (uses the UI file)
4042
QWidget *SerialPluginOptionsPage::createPage(QWidget *parent)
4143
{
42-
Q_UNUSED(parent);
43-
options_page = new Ui::SerialPluginOptionsPage();
44-
// main widget
45-
QWidget *optionsPageWidget = new QWidget;
46-
// main layout
47-
options_page->setupUi(optionsPageWidget);
44+
m_page = new Ui::SerialPluginOptionsPage();
45+
QWidget *w = new QWidget(parent);
46+
m_page->setupUi(w);
47+
4848
QStringList allowedSpeeds;
4949
allowedSpeeds << "1200"
5050
#ifdef Q_OS_UNIX
@@ -75,10 +75,9 @@ QWidget *SerialPluginOptionsPage::createPage(QWidget *parent)
7575
#endif
7676
;
7777

78-
79-
options_page->cb_speed->addItems(allowedSpeeds);
80-
options_page->cb_speed->setCurrentIndex(options_page->cb_speed->findText(m_config->speed()));
81-
return optionsPageWidget;
78+
m_page->cb_speed->addItems(allowedSpeeds);
79+
m_page->cb_speed->setCurrentIndex(m_page->cb_speed->findText(m_config->speed()));
80+
return w;
8281
}
8382

8483
/**
@@ -89,12 +88,15 @@ QWidget *SerialPluginOptionsPage::createPage(QWidget *parent)
8988
*/
9089
void SerialPluginOptionsPage::apply()
9190
{
92-
m_config->setSpeed(options_page->cb_speed->currentText());
93-
m_config->saveSettings();
91+
m_config->setSpeed(m_page->cb_speed->currentText());
92+
93+
// FIXME this signal is too low level (and duplicated all over the place)
94+
// FIXME this signal will trigger (amongst other things) the saving of the configuration !
95+
emit availableDevChanged();
9496
}
9597

9698

9799
void SerialPluginOptionsPage::finish()
98100
{
99-
delete options_page;
101+
delete m_page;
100102
}

0 commit comments

Comments
 (0)