diff --git a/ground/src/plugins/coreplugin/coreplugin.pro b/ground/src/plugins/coreplugin/coreplugin.pro index 8993f0276..1cbbee3ed 100644 --- a/ground/src/plugins/coreplugin/coreplugin.pro +++ b/ground/src/plugins/coreplugin/coreplugin.pro @@ -58,7 +58,8 @@ SOURCES += mainwindow.cpp \ iconnection.cpp \ iuavgadgetconfiguration.cpp \ uavgadgetinstancemanager.cpp \ - uavgadgetoptionspagedecorator.cpp + uavgadgetoptionspagedecorator.cpp \ + uavgadgetdecorator.cpp HEADERS += mainwindow.h \ tabpositionindicator.h \ fancyactionbar.h \ @@ -113,7 +114,8 @@ HEADERS += mainwindow.h \ iconnection.h \ iuavgadgetconfiguration.h \ uavgadgetinstancemanager.h \ - uavgadgetoptionspagedecorator.h + uavgadgetoptionspagedecorator.h \ + uavgadgetdecorator.h FORMS += dialogs/settingsdialog.ui \ dialogs/shortcutsettings.ui \ generalsettings.ui \ diff --git a/ground/src/plugins/coreplugin/iuavgadget.cpp b/ground/src/plugins/coreplugin/iuavgadget.cpp index dbc8b8039..b6fbf961c 100644 --- a/ground/src/plugins/coreplugin/iuavgadget.cpp +++ b/ground/src/plugins/coreplugin/iuavgadget.cpp @@ -26,95 +26,3 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "iuavgadget.h" -#include "iuavgadgetconfiguration.h" - -using namespace Core; - -IUAVGadget::IUAVGadget(QString classId, QList *configurations, QObject *parent) : - IContext(parent), - m_toolbar(new QComboBox), - m_classId(classId), - m_activeConfiguration(0), - m_configurations(configurations) -{ - m_toolbar->setMinimumContentsLength(15); - foreach (IUAVGadgetConfiguration *config, *m_configurations) - m_toolbar->addItem(config->name()); - connect(m_toolbar, SIGNAL(activated(int)), this, SLOT(loadConfiguration(int))); - if (m_configurations->count() > 0) - loadConfiguration(0); - updateToolbar(); -} - -void IUAVGadget::loadConfiguration(int index) { - IUAVGadgetConfiguration* config = m_configurations->at(index); - m_activeConfiguration = config; - loadConfiguration(config); -} - -void IUAVGadget::setActiveConfiguration(IUAVGadgetConfiguration *config) -{ - m_activeConfiguration = config; - int index = m_toolbar->findText(config->name()); - m_toolbar->setCurrentIndex(index); - -} - -void IUAVGadget::configurationChanged(IUAVGadgetConfiguration* config) -{ - if (config == m_activeConfiguration) - loadConfiguration(config); -} - -void IUAVGadget::configurationAdded(IUAVGadgetConfiguration* config) -{ - m_configurations->append(config); - m_toolbar->addItem(config->name()); - updateToolbar(); -} - -void IUAVGadget::configurationToBeDeleted(IUAVGadgetConfiguration* config) -{ - int index = m_configurations->indexOf(config); - if (index >= 0) { - m_toolbar->removeItem(index); - m_configurations->removeAt(index); - } - updateToolbar(); -} - -void IUAVGadget::configurationNameChanged(QString oldName, QString newName) -{ - for (int i = 0; i < m_toolbar->count(); ++i) { - if (m_toolbar->itemText(i) == oldName) - m_toolbar->setItemText(i, newName); - } -} - -void IUAVGadget::updateToolbar() -{ - m_toolbar->setEnabled(m_toolbar->count() > 1); -} - -QByteArray IUAVGadget::saveState() -{ - QByteArray bytes; - QDataStream stream(&bytes, QIODevice::WriteOnly); - if (m_activeConfiguration) - stream << m_activeConfiguration->name().toLatin1(); - return bytes; -} - -void IUAVGadget::restoreState(QByteArray state) -{ - QDataStream stream(state); - QByteArray configName; - stream >> configName; - foreach (IUAVGadgetConfiguration *config, *m_configurations) { - if (config->name() == configName) { - m_activeConfiguration = config; - loadConfiguration(config); - } - } -} diff --git a/ground/src/plugins/coreplugin/iuavgadget.h b/ground/src/plugins/coreplugin/iuavgadget.h index 6307d2cda..9efd289ce 100644 --- a/ground/src/plugins/coreplugin/iuavgadget.h +++ b/ground/src/plugins/coreplugin/iuavgadget.h @@ -46,34 +46,32 @@ class CORE_EXPORT IUAVGadget : public IContext { Q_OBJECT public: - IUAVGadget(QString classId, QList *configurations, QObject *parent = 0); + IUAVGadget(QString classId, QObject *parent = 0) : + IContext(parent), + m_classId(classId) { } + virtual ~IUAVGadget() {} - virtual QList context() const = 0; + QList context() const { return m_context; } + void setContext(QList context) { m_context = context; } virtual QWidget *widget() = 0; + virtual QComboBox *toolBar() { return 0; } virtual QString contextHelpId() const { return QString(); } QString classId() const { return m_classId; } - virtual void loadConfiguration(IUAVGadgetConfiguration* /*config*/) { } - IUAVGadgetConfiguration *activeConfiguration() { return m_activeConfiguration; } - void setActiveConfiguration(IUAVGadgetConfiguration *config); - QComboBox *toolBar() { return m_toolbar; } - virtual QByteArray saveState(); - virtual void restoreState(QByteArray state); + virtual IUAVGadgetConfiguration *activeConfiguration() { return 0; } + virtual void loadConfiguration(IUAVGadgetConfiguration*) { } + virtual QByteArray saveState() { return QByteArray(); } + virtual void restoreState(QByteArray) { } public slots: - void configurationChanged(IUAVGadgetConfiguration* config); - void configurationAdded(IUAVGadgetConfiguration* config); - void configurationToBeDeleted(IUAVGadgetConfiguration* config); - void configurationNameChanged(QString oldName, QString newName); + virtual void configurationChanged(IUAVGadgetConfiguration* ) { } + virtual void configurationAdded(IUAVGadgetConfiguration*) { } + virtual void configurationToBeDeleted(IUAVGadgetConfiguration*) { } + virtual void configurationNameChanged(QString, QString) { } private slots: - void loadConfiguration(int index); -protected: - QComboBox *m_toolbar; private: - void updateToolbar(); QString m_classId; - IUAVGadgetConfiguration *m_activeConfiguration; - QList *m_configurations; + QList m_context; }; } // namespace Core diff --git a/ground/src/plugins/coreplugin/iuavgadgetfactory.h b/ground/src/plugins/coreplugin/iuavgadgetfactory.h index f50bf7269..e5e755a11 100644 --- a/ground/src/plugins/coreplugin/iuavgadgetfactory.h +++ b/ground/src/plugins/coreplugin/iuavgadgetfactory.h @@ -52,10 +52,10 @@ public: m_name(name) {} virtual ~IUAVGadgetFactory() {} - virtual IUAVGadget *createGadget(QList *configurations, QWidget *parent) = 0; + virtual IUAVGadget *createGadget(QWidget *parent) = 0; virtual IUAVGadgetConfiguration *createConfiguration(bool /*locked*/, - const QString /*configName*/, - const QByteArray &/*state*/) { return 0; } + const QString /*configName*/, + const QByteArray &/*state*/) { return 0; } virtual IOptionsPage *createOptionsPage(IUAVGadgetConfiguration */*config*/) { return 0; } QString classId() const { return m_classId; } QString name() const { return m_name; } diff --git a/ground/src/plugins/coreplugin/uavgadgetdecorator.cpp b/ground/src/plugins/coreplugin/uavgadgetdecorator.cpp new file mode 100644 index 000000000..b36c17392 --- /dev/null +++ b/ground/src/plugins/coreplugin/uavgadgetdecorator.cpp @@ -0,0 +1,121 @@ +/** + ****************************************************************************** + * + * @file uavgadgetdecorator.cpp + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup coreplugin + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "uavgadgetdecorator.h" +#include "iuavgadgetconfiguration.h" +#include +#include + +using namespace Core; + +UAVGadgetDecorator::UAVGadgetDecorator(IUAVGadget *gadget, QList *configurations) : + IUAVGadget(gadget->classId(), gadget->parent()), + m_gadget(gadget), + m_toolbar(new QComboBox), + m_activeConfiguration(0), + m_configurations(configurations) +{ + m_toolbar->setMinimumContentsLength(15); + foreach (IUAVGadgetConfiguration *config, *m_configurations) + m_toolbar->addItem(config->name()); + connect(m_toolbar, SIGNAL(activated(int)), this, SLOT(loadConfiguration(int))); + if (m_configurations->count() > 0) + loadConfiguration(0); + updateToolbar(); +} + +void UAVGadgetDecorator::loadConfiguration(int index) { + IUAVGadgetConfiguration* config = m_configurations->at(index); + loadConfiguration(config); +} + +void UAVGadgetDecorator::loadConfiguration(IUAVGadgetConfiguration *config) +{ + m_activeConfiguration = config; + int index = m_toolbar->findText(config->name()); + m_toolbar->setCurrentIndex(index); + m_gadget->loadConfiguration(config); + +} + +void UAVGadgetDecorator::configurationChanged(IUAVGadgetConfiguration* config) +{ + if (config == m_activeConfiguration) + loadConfiguration(config); +} + +void UAVGadgetDecorator::configurationAdded(IUAVGadgetConfiguration* config) +{ + m_configurations->append(config); + m_toolbar->addItem(config->name()); + updateToolbar(); +} + +void UAVGadgetDecorator::configurationToBeDeleted(IUAVGadgetConfiguration* config) +{ + int index = m_configurations->indexOf(config); + if (index >= 0) { + m_toolbar->removeItem(index); + m_configurations->removeAt(index); + } + updateToolbar(); +} + +void UAVGadgetDecorator::configurationNameChanged(QString oldName, QString newName) +{ + for (int i = 0; i < m_toolbar->count(); ++i) { + if (m_toolbar->itemText(i) == oldName) + m_toolbar->setItemText(i, newName); + } +} + +void UAVGadgetDecorator::updateToolbar() +{ + m_toolbar->setEnabled(m_toolbar->count() > 1); +} + +QByteArray UAVGadgetDecorator::saveState() +{ + QByteArray bytes; + QDataStream stream(&bytes, QIODevice::WriteOnly); + if (m_activeConfiguration) + stream << m_activeConfiguration->name().toLatin1(); + return bytes; +} + +void UAVGadgetDecorator::restoreState(QByteArray state) +{ + QDataStream stream(state); + QByteArray configName; + stream >> configName; + foreach (IUAVGadgetConfiguration *config, *m_configurations) { + if (config->name() == configName) { + m_activeConfiguration = config; + loadConfiguration(config); + } + } +} diff --git a/ground/src/plugins/coreplugin/uavgadgetdecorator.h b/ground/src/plugins/coreplugin/uavgadgetdecorator.h new file mode 100644 index 000000000..dfdb46ffd --- /dev/null +++ b/ground/src/plugins/coreplugin/uavgadgetdecorator.h @@ -0,0 +1,66 @@ +/** + ****************************************************************************** + * + * @file uavgadgetdecorator.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup coreplugin + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef UAVGADGETDECORATOR_H +#define UAVGADGETDECORATOR_H +#include + +namespace Core { + +class IUAVGadgetConfiguration; + +class UAVGadgetDecorator : public IUAVGadget +{ +Q_OBJECT +public: + explicit UAVGadgetDecorator(IUAVGadget *gadget, QList *configurations); + + QWidget *widget() { return m_gadget->widget(); } + QComboBox *toolBar() { return m_toolbar; } + IUAVGadgetConfiguration *activeConfiguration() { return m_activeConfiguration; } + void loadConfiguration(IUAVGadgetConfiguration *config); + QByteArray saveState(); + void restoreState(QByteArray state); +public slots: + void configurationChanged(IUAVGadgetConfiguration* config); + void configurationAdded(IUAVGadgetConfiguration* config); + void configurationToBeDeleted(IUAVGadgetConfiguration* config); + void configurationNameChanged(QString oldName, QString newName); +private slots: + void loadConfiguration(int index); +private: + void updateToolbar(); + IUAVGadget *m_gadget; + QComboBox *m_toolbar; + IUAVGadgetConfiguration *m_activeConfiguration; + QList *m_configurations; + +}; + +} // namespace Core + +#endif // UAVGADGETDECORATOR_H diff --git a/ground/src/plugins/coreplugin/uavgadgetinstancemanager.cpp b/ground/src/plugins/coreplugin/uavgadgetinstancemanager.cpp index 6c2c0a855..7c72dd5c9 100644 --- a/ground/src/plugins/coreplugin/uavgadgetinstancemanager.cpp +++ b/ground/src/plugins/coreplugin/uavgadgetinstancemanager.cpp @@ -27,8 +27,10 @@ #include "uavgadgetinstancemanager.h" #include "iuavgadget.h" +#include "uavgadgetdecorator.h" #include "iuavgadgetfactory.h" #include "iuavgadgetconfiguration.h" +#include "uavgadgetoptionspagedecorator.h" #include "coreplugin/dialogs/ioptionspage.h" #include "coreplugin/dialogs/settingsdialog.h" #include "icore.h" @@ -122,7 +124,8 @@ void UAVGadgetInstanceManager::createOptionsPages() foreach (IUAVGadgetConfiguration *config, m_configurations) { IUAVGadgetFactory *f = factory(config->classId()); - IOptionsPage *page = f->createOptionsPage(config); + IOptionsPage *p = f->createOptionsPage(config); + IOptionsPage *page = new UAVGadgetOptionsPageDecorator(p, config); m_optionsPages.append(page); m_pm->addObject(page); } @@ -134,7 +137,8 @@ IUAVGadget *UAVGadgetInstanceManager::createGadget(QString classId, QWidget *par IUAVGadgetFactory *f = factory(classId); if (f) { QList *configs = configurations(classId); - IUAVGadget *gadget = f->createGadget(configs, parent); + IUAVGadget *g = f->createGadget(parent); + IUAVGadget *gadget = new UAVGadgetDecorator(g, configs); m_gadgetInstances.append(gadget); connect(this, SIGNAL(configurationAdded(IUAVGadgetConfiguration*)), gadget, SLOT(configurationAdded(IUAVGadgetConfiguration*))); connect(this, SIGNAL(configurationChanged(IUAVGadgetConfiguration*)), gadget, SLOT(configurationChanged(IUAVGadgetConfiguration*))); @@ -183,7 +187,8 @@ void UAVGadgetInstanceManager::cloneConfiguration(IUAVGadgetConfiguration *conf IUAVGadgetConfiguration *config = configToClone->clone(name); IUAVGadgetFactory *f = factory(config->classId()); - IOptionsPage *page = f->createOptionsPage(config); + IOptionsPage *p = f->createOptionsPage(config); + IOptionsPage *page = new UAVGadgetOptionsPageDecorator(p, config); m_provisionalConfigs.append(config); m_provisionalOptionsPages.append(page); m_settingsDialog->insertPage(page); diff --git a/ground/src/plugins/coreplugin/uavgadgetoptionspagedecorator.cpp b/ground/src/plugins/coreplugin/uavgadgetoptionspagedecorator.cpp index 1e815c2a6..db36108a0 100644 --- a/ground/src/plugins/coreplugin/uavgadgetoptionspagedecorator.cpp +++ b/ground/src/plugins/coreplugin/uavgadgetoptionspagedecorator.cpp @@ -74,8 +74,8 @@ QWidget *UAVGadgetOptionsPageDecorator::createPage(QWidget *parent) void UAVGadgetOptionsPageDecorator::apply() { m_id = m_config->provisionalName(); - m_instanceManager->applyChanges(m_config); m_optionsPage->apply(); + m_instanceManager->applyChanges(m_config); } void UAVGadgetOptionsPageDecorator::finish()