dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
uavgadgetdecorator.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "uavgadgetdecorator.h"
30 #include <QComboBox>
31 #include <QtCore/QByteArray>
32 #include <QtCore/QDebug>
33 
34 using namespace Core;
35 
37  QList<IUAVGadgetConfiguration *> *configurations,
38  bool loadDefault)
39  : IUAVGadget(gadget->classId(), gadget->parent())
40  , m_gadget(gadget)
41  , m_toolbar(new QComboBox)
42  , m_activeConfiguration(nullptr)
43  , m_configurations(configurations)
44 {
45  m_gadget->setParent(this);
46  m_toolbar->setMinimumContentsLength(15);
47  foreach (IUAVGadgetConfiguration *config, *m_configurations)
48  m_toolbar->addItem(config->name());
49  connect(m_toolbar, SIGNAL(activated(int)), this, SLOT(loadConfiguration(int)));
50 
51  // If a gadget configuration exists, use the first configuration when
52  // creating the gadget.
53  if ((m_configurations->count() > 0) && loadDefault) {
54  // Must call loadConfiguration(), or else GCS crashes when
55  // changing widgets using Edit Gadgets mode.
57  }
58 
59  updateToolbar();
60 }
61 
63 {
64  delete m_configurations;
65  delete m_toolbar;
66 }
67 
69 {
70  IUAVGadgetConfiguration *config = m_configurations->at(index);
71  loadConfiguration(config);
72 }
73 
75 {
76  m_activeConfiguration = config;
77  int index = m_toolbar->findText(config->name());
78  m_toolbar->setCurrentIndex(index);
79  m_gadget->loadConfiguration(config);
80 }
81 
83 {
84  if (config == m_activeConfiguration)
85  loadConfiguration(config);
86 }
87 
89 {
90  if (config->classId() != classId())
91  return;
92  m_configurations->append(config);
93  m_toolbar->addItem(config->name());
94  updateToolbar();
95 }
96 
98 {
99  if (config->classId() != classId())
100  return;
101  int index = m_configurations->indexOf(config);
102  if (index >= 0) {
103  m_toolbar->removeItem(index);
104  m_configurations->removeAt(index);
105  }
106  updateToolbar();
107 }
108 
110  QString newName)
111 {
112  if (config->classId() != classId())
113  return;
114  for (int i = 0; i < m_toolbar->count(); ++i) {
115  if (m_toolbar->itemText(i) == oldName)
116  m_toolbar->setItemText(i, newName);
117  }
118 }
119 
120 void UAVGadgetDecorator::updateToolbar()
121 {
122  m_toolbar->setEnabled(m_toolbar->count() > 1);
123 }
124 
125 void UAVGadgetDecorator::saveState(QSettings *qSettings)
126 {
127  if (m_activeConfiguration) {
128  qSettings->setValue("activeConfiguration", m_activeConfiguration->name());
129  }
130 }
131 
132 void UAVGadgetDecorator::restoreState(QSettings *qSetting)
133 {
134  QString configName = qSetting->value("activeConfiguration").toString();
135  foreach (IUAVGadgetConfiguration *config, *m_configurations) {
136  if (config->name() == configName) {
137  m_activeConfiguration = config;
138  loadConfiguration(config);
139  }
140  }
141 }
void restoreState(QSettings *qSettings)
void loadConfiguration(IUAVGadgetConfiguration *config)
QString classId() const
Definition: iuavgadget.h:62
virtual void loadConfiguration(IUAVGadgetConfiguration *)
Definition: iuavgadget.h:65
void configurationAdded(IUAVGadgetConfiguration *config)
for i
Definition: OPPlots.m:140
void configurationChanged(IUAVGadgetConfiguration *config)
void configurationToBeDeleted(IUAVGadgetConfiguration *config)
void configurationNameChanged(IUAVGadgetConfiguration *config, QString oldName, QString newName)
UAVGadgetDecorator(IUAVGadget *gadget, QList< IUAVGadgetConfiguration * > *configurations, bool loadDefault=false)
void saveState(QSettings *qSettings)
Definition: icore.h:39