2 ******************************************************************************
4 * @file uavgadgetdecorator.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup CorePlugin Core Plugin
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
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"
31 #include <QtCore/QByteArray>
32 #include <QtCore/QDebug>
36 UAVGadgetDecorator::UAVGadgetDecorator(IUAVGadget
*gadget
, QList
<IUAVGadgetConfiguration
*> *configurations
) :
37 IUAVGadget(gadget
->classId(), gadget
->parent()),
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)));
51 UAVGadgetDecorator::~UAVGadgetDecorator()
53 delete m_configurations
;
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
) {
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()) {
91 m_configurations
->append(config
);
92 m_toolbar
->addItem(config
->name());
96 void UAVGadgetDecorator::configurationToBeDeleted(IUAVGadgetConfiguration
*config
)
98 if (config
->classId() != classId()) {
101 int index
= m_configurations
->indexOf(config
);
103 m_toolbar
->removeItem(index
);
104 m_configurations
->removeAt(index
);
109 void UAVGadgetDecorator::configurationNameChanged(IUAVGadgetConfiguration
*config
, QString oldName
, QString newName
)
111 if (config
->classId() != classId()) {
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
);
147 // restore gadget individual state
148 qSettings
->beginGroup("state");
149 m_gadget
->restoreState(qSettings
);
150 qSettings
->endGroup();