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"
30 #include "iuavgadgetconfiguration.h"
37 UAVGadgetDecorator::UAVGadgetDecorator(IUAVGadget
*gadget
, QList
<IUAVGadgetConfiguration
*> *configurations
) :
38 IUAVGadget(gadget
->classId(), gadget
->parent()),
40 m_toolbar(new QComboBox
),
41 m_activeConfiguration(0),
42 m_configurations(configurations
)
44 m_gadget
->setParent(this);
45 m_toolbar
->setMinimumContentsLength(15);
46 foreach(IUAVGadgetConfiguration
* config
, *m_configurations
)
47 m_toolbar
->addItem(config
->name());
48 connect(m_toolbar
, SIGNAL(activated(int)), this, SLOT(loadConfiguration(int)));
52 UAVGadgetDecorator::~UAVGadgetDecorator()
54 delete m_configurations
;
58 void UAVGadgetDecorator::loadConfiguration(int index
)
60 IUAVGadgetConfiguration
*config
= m_configurations
->at(index
);
62 loadConfiguration(config
);
65 void UAVGadgetDecorator::loadConfiguration(IUAVGadgetConfiguration
*config
)
67 if (m_activeConfiguration
== config
) {
70 m_activeConfiguration
= config
;
71 int index
= m_toolbar
->findText(config
->name());
72 if (m_toolbar
->currentIndex() != index
) {
73 m_toolbar
->setCurrentIndex(index
);
75 m_gadget
->loadConfiguration(config
);
78 void UAVGadgetDecorator::configurationChanged(IUAVGadgetConfiguration
*config
)
80 if (config
== m_activeConfiguration
) {
81 // force a configuration reload
82 m_activeConfiguration
= NULL
;
83 loadConfiguration(config
);
87 void UAVGadgetDecorator::configurationAdded(IUAVGadgetConfiguration
*config
)
89 if (config
->classId() != classId()) {
92 m_configurations
->append(config
);
93 m_toolbar
->addItem(config
->name());
97 void UAVGadgetDecorator::configurationToBeDeleted(IUAVGadgetConfiguration
*config
)
99 if (config
->classId() != classId()) {
102 int index
= m_configurations
->indexOf(config
);
104 m_toolbar
->removeItem(index
);
105 m_configurations
->removeAt(index
);
110 void UAVGadgetDecorator::configurationNameChanged(IUAVGadgetConfiguration
*config
, QString oldName
, QString newName
)
112 if (config
->classId() != classId()) {
115 for (int i
= 0; i
< m_toolbar
->count(); ++i
) {
116 if (m_toolbar
->itemText(i
) == oldName
) {
117 m_toolbar
->setItemText(i
, newName
);
122 void UAVGadgetDecorator::updateToolbar()
124 m_toolbar
->setEnabled(m_toolbar
->count() > 1);
127 void UAVGadgetDecorator::saveState(QSettings
&settings
) const
129 if (m_activeConfiguration
) {
130 settings
.setValue("activeConfiguration", m_activeConfiguration
->name());
132 // save gadget individual state
133 settings
.beginGroup("state");
134 m_gadget
->saveState(settings
);
138 void UAVGadgetDecorator::restoreState(QSettings
&settings
)
140 QString configName
= settings
.value("activeConfiguration").toString();
142 foreach(IUAVGadgetConfiguration
* config
, *m_configurations
) {
143 if (config
->name() == configName
) {
144 loadConfiguration(config
);
148 // restore gadget individual state
149 settings
.beginGroup("state");
150 m_gadget
->restoreState(settings
);