LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / coreplugin / uavgadgetdecorator.cpp
blob816dc41c3167c87e0632aa916b247c300d3a55a2
1 /**
2 ******************************************************************************
4 * @file uavgadgetdecorator.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup CorePlugin Core Plugin
9 * @{
10 * @brief The Core GCS plugin
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "uavgadgetdecorator.h"
29 #include "iuavgadgetconfiguration.h"
30 #include <QComboBox>
31 #include <QtCore/QByteArray>
32 #include <QtCore/QDebug>
34 using namespace Core;
36 UAVGadgetDecorator::UAVGadgetDecorator(IUAVGadget *gadget, QList<IUAVGadgetConfiguration *> *configurations) :
37 IUAVGadget(gadget->classId(), gadget->parent()),
38 m_gadget(gadget),
39 m_toolbar(new QComboBox),
40 m_activeConfiguration(0),
41 m_configurations(configurations)
43 m_gadget->setParent(this);
44 m_toolbar->setMinimumContentsLength(15);
45 foreach(IUAVGadgetConfiguration * config, *m_configurations)
46 m_toolbar->addItem(config->name());
47 connect(m_toolbar, SIGNAL(activated(int)), this, SLOT(loadConfiguration(int)));
48 updateToolbar();
51 UAVGadgetDecorator::~UAVGadgetDecorator()
53 delete m_configurations;
54 delete m_toolbar;
57 void UAVGadgetDecorator::loadConfiguration(int index)
59 IUAVGadgetConfiguration *config = m_configurations->at(index);
61 loadConfiguration(config);
64 void UAVGadgetDecorator::loadConfiguration(IUAVGadgetConfiguration *config)
66 if (m_activeConfiguration == config) {
67 return;
69 m_activeConfiguration = config;
70 int index = m_toolbar->findText(config->name());
71 if (m_toolbar->currentIndex() != index) {
72 m_toolbar->setCurrentIndex(index);
74 m_gadget->loadConfiguration(config);
77 void UAVGadgetDecorator::configurationChanged(IUAVGadgetConfiguration *config)
79 if (config == m_activeConfiguration) {
80 // force a configuration reload
81 m_activeConfiguration = NULL;
82 loadConfiguration(config);
86 void UAVGadgetDecorator::configurationAdded(IUAVGadgetConfiguration *config)
88 if (config->classId() != classId()) {
89 return;
91 m_configurations->append(config);
92 m_toolbar->addItem(config->name());
93 updateToolbar();
96 void UAVGadgetDecorator::configurationToBeDeleted(IUAVGadgetConfiguration *config)
98 if (config->classId() != classId()) {
99 return;
101 int index = m_configurations->indexOf(config);
102 if (index >= 0) {
103 m_toolbar->removeItem(index);
104 m_configurations->removeAt(index);
106 updateToolbar();
109 void UAVGadgetDecorator::configurationNameChanged(IUAVGadgetConfiguration *config, QString oldName, QString newName)
111 if (config->classId() != classId()) {
112 return;
114 for (int i = 0; i < m_toolbar->count(); ++i) {
115 if (m_toolbar->itemText(i) == oldName) {
116 m_toolbar->setItemText(i, newName);
121 void UAVGadgetDecorator::updateToolbar()
123 m_toolbar->setEnabled(m_toolbar->count() > 1);
126 void UAVGadgetDecorator::saveState(QSettings *qSettings)
128 if (m_activeConfiguration) {
129 qSettings->setValue("activeConfiguration", m_activeConfiguration->name());
131 // save gadget individual state
132 qSettings->beginGroup("state");
133 m_gadget->saveState(qSettings);
134 qSettings->endGroup();
137 void UAVGadgetDecorator::restoreState(QSettings *qSettings)
139 QString configName = qSettings->value("activeConfiguration").toString();
141 foreach(IUAVGadgetConfiguration * config, *m_configurations) {
142 if (config->name() == configName) {
143 loadConfiguration(config);
144 break;
147 // restore gadget individual state
148 qSettings->beginGroup("state");
149 m_gadget->restoreState(qSettings);
150 qSettings->endGroup();