Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / gcscontrol / gcscontrolgadgetoptionspage.cpp
blob45316ce0511755570e09d2071e47f4d1da079c08
1 /**
2 ******************************************************************************
4 * @file gcscontrolgadgetoptionspage.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup GCSControlGadgetPlugin GCSControl Gadget Plugin
9 * @{
10 * @brief A place holder gadget 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 "gcscontrolgadgetoptionspage.h"
29 #include "gcscontrolgadgetconfiguration.h"
30 #include "ui_gcscontrolgadgetoptionspage.h"
32 #include <QFileDialog>
33 #include <QtAlgorithms>
34 #include <QStringList>
36 GCSControlGadgetOptionsPage::GCSControlGadgetOptionsPage(GCSControlGadgetConfiguration *config, QObject *parent) :
37 IOptionsPage(parent),
38 m_config(config)
40 options_page = NULL;
42 sdlGamepad = dynamic_cast<GCSControlPlugin *>(parent)->sdlGamepad;
45 GCSControlGadgetOptionsPage::~GCSControlGadgetOptionsPage()
49 void GCSControlGadgetOptionsPage::buttonState(ButtonNumber number, bool pressed)
51 if (options_page) {
52 QList<QCheckBox *> rbList;
53 rbList << options_page->buttonInput0 <<
54 options_page->buttonInput1 << options_page->buttonInput2 <<
55 options_page->buttonInput3 << options_page->buttonInput4 <<
56 options_page->buttonInput5 << options_page->buttonInput6 <<
57 options_page->buttonInput7;
59 if (number < 8) { // We only support 8 buttons
60 rbList.at(number)->setChecked(pressed);
65 void GCSControlGadgetOptionsPage::gamepads(quint8 count)
67 Q_UNUSED(count);
69 /*options_page->AvailableControllerList->clear();
70 for (int i=0;i<count;i++)
72 options_page->AvailableControllerList->addItem(QString().sprintf("%d",i));//SDL_JoystickName(i));
73 }*/
76 void GCSControlGadgetOptionsPage::axesValues(QListInt16 values)
78 if (options_page) {
79 QList<QProgressBar *> pbList;
80 pbList << options_page->joyCh0 <<
81 options_page->joyCh1 << options_page->joyCh2 <<
82 options_page->joyCh3 << options_page->joyCh4 <<
83 options_page->joyCh5 << options_page->joyCh6 <<
84 options_page->joyCh7;
85 int i = 0;
86 foreach(qint16 value, values) {
87 if (i > 7) {
88 break; // We only support 7 channels
90 if (chRevList.at(i)->isChecked() == 1) {
91 value = 65535 - value;
93 if (pbList.at(i)->minimum() > value) {
94 pbList.at(i)->setMinimum(value);
96 if (pbList.at(i)->maximum() < value) {
97 pbList.at(i)->setMaximum(value);
99 pbList.at(i++)->setValue(value);
105 // creates options page widget (uses the UI file)
106 QWidget *GCSControlGadgetOptionsPage::createPage(QWidget *parent)
108 Q_UNUSED(parent);
110 int i;
111 options_page = new Ui::GCSControlGadgetOptionsPage();
112 QWidget *optionsPageWidget = new QWidget;
113 options_page->setupUi(optionsPageWidget);
116 // QList<QComboBox*> chList;
117 chList.clear();
118 chList << options_page->channel0 << options_page->channel1 <<
119 options_page->channel2 << options_page->channel3 <<
120 options_page->channel4 << options_page->channel5 <<
121 options_page->channel6 << options_page->channel7;
122 QStringList chOptions;
123 chOptions << "None" << "Roll" << "Pitch" << "Yaw" << "Throttle";
124 foreach(QComboBox * qb, chList) {
125 qb->addItems(chOptions);
127 // QList<QCheckBox*> chRevList;
128 chRevList.clear();
129 chRevList << options_page->revCheckBox_1 << options_page->revCheckBox_2 <<
130 options_page->revCheckBox_3 << options_page->revCheckBox_4 <<
131 options_page->revCheckBox_5 << options_page->revCheckBox_6 <<
132 options_page->revCheckBox_7 << options_page->revCheckBox_8;
134 // QList<QComboBox*> buttonFunctionList;
135 buttonFunctionList.clear();
136 buttonFunctionList << options_page->buttonFunction0 << options_page->buttonFunction1 <<
137 options_page->buttonFunction2 << options_page->buttonFunction3 <<
138 options_page->buttonFunction4 << options_page->buttonFunction5 <<
139 options_page->buttonFunction6 << options_page->buttonFunction7;
140 QStringList buttonOptions;
141 buttonOptions << "-" << "Roll" << "Pitch" << "Yaw" << "Throttle" << "Armed" << "GCS Control"; // added UDP control to action list
142 foreach(QComboBox * qb, buttonFunctionList) {
143 qb->addItems(buttonOptions);
145 // QList<QComboBox*> buttonActionList;
146 buttonActionList.clear();
147 buttonActionList << options_page->buttonAction0 << options_page->buttonAction1 <<
148 options_page->buttonAction2 << options_page->buttonAction3 <<
149 options_page->buttonAction4 << options_page->buttonAction5 <<
150 options_page->buttonAction6 << options_page->buttonAction7;
151 QStringList buttonActionOptions;
152 buttonActionOptions << "Does nothing" << "Increases" << "Decreases" << "Toggles";
153 foreach(QComboBox * qb, buttonActionList) {
154 qb->addItems(buttonActionOptions);
156 // QList<QDoubleSpinBox*> buttonValueList;
157 buttonValueList.clear();
158 buttonValueList << options_page->buttonAmount0 << options_page->buttonAmount1 <<
159 options_page->buttonAmount2 << options_page->buttonAmount3 <<
160 options_page->buttonAmount4 << options_page->buttonAmount5 <<
161 options_page->buttonAmount6 << options_page->buttonAmount7;
162 // QList<QLabel*> buttonLabelList;
163 buttonLabelList.clear();
164 buttonLabelList << options_page->buttonLabel0 << options_page->buttonLabel1 <<
165 options_page->buttonLabel2 << options_page->buttonLabel3 <<
166 options_page->buttonLabel4 << options_page->buttonLabel5 <<
167 options_page->buttonLabel6 << options_page->buttonLabel7;
169 for (i = 0; i < 8; i++) {
170 buttonActionList.at(i)->setCurrentIndex(m_config->getbuttonSettings(i).ActionID);
171 buttonFunctionList.at(i)->setCurrentIndex(m_config->getbuttonSettings(i).FunctionID);
172 buttonValueList.at(i)->setValue(m_config->getbuttonSettings(i).Amount);
174 connect(buttonFunctionList.at(i), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonFunction()));
175 // connect(buttonActionList.at(i),SIGNAL(currentIndexChanged(int)),this,SLOT(updateButtonActions[i]()));
176 updateButtonAction(i);
177 buttonFunctionList.at(i)->setCurrentIndex(m_config->getbuttonSettings(i).FunctionID);
179 connect(buttonActionList.at(0), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_0()));
180 connect(buttonActionList.at(1), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_1()));
181 connect(buttonActionList.at(2), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_2()));
182 connect(buttonActionList.at(3), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_3()));
183 connect(buttonActionList.at(4), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_4()));
184 connect(buttonActionList.at(5), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_5()));
185 connect(buttonActionList.at(6), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_6()));
186 connect(buttonActionList.at(7), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonAction_7()));
188 // updateButtonFunction();
190 options_page->udp_host->setText(m_config->getUDPControlHost().toString());
191 options_page->udp_port->setText(QString::number(m_config->getUDPControlPort()));
194 // Controls mode are from 1 to 4.
195 if (m_config->getControlsMode() > 0 && m_config->getControlsMode() < 5) {
196 options_page->controlsMode->setCurrentIndex(m_config->getControlsMode() - 1);
197 } else {
198 qDebug() << "GCSControl: Invalid control modes setting! Did you edit by hand?";
201 QList<int> ql = m_config->getChannelsMapping();
202 for (int i = 0; i < 4; i++) {
203 if (ql.at(i) > -1) {
204 chList.at(ql.at(i))->setCurrentIndex(i + 1);
207 QList<bool> qlChRev = m_config->getChannelsReverse();
208 for (i = 0; i < 8; i++) {
209 chRevList.at(i)->setChecked(qlChRev.at(i));;
212 connect(sdlGamepad, SIGNAL(axesValues(QListInt16)), this, SLOT(axesValues(QListInt16)));
213 connect(sdlGamepad, SIGNAL(buttonState(ButtonNumber, bool)), this, SLOT(buttonState(ButtonNumber, bool)));
214 connect(sdlGamepad, SIGNAL(gamepads(quint8)), this, SLOT(gamepads(quint8)));
216 return optionsPageWidget;
220 * Called when the user presses apply or OK.
222 * Saves the current values
225 void GCSControlGadgetOptionsPage::apply()
227 m_config->setControlsMode(options_page->controlsMode->currentIndex() + 1);
228 /*QList<QComboBox*> chList;
229 chList << options_page->channel0 << options_page->channel1 <<
230 options_page->channel2 << options_page->channel3 <<
231 options_page->channel4 << options_page->channel5 <<
232 options_page->channel6 << options_page->channel7;
233 QList<QComboBox*> buttonFunctionList;
234 buttonFunctionList << options_page->buttonFunction0 << options_page->buttonFunction1 <<
235 options_page->buttonFunction2 << options_page->buttonFunction3 <<
236 options_page->buttonFunction4 << options_page->buttonFunction5 <<
237 options_page->buttonFunction6 << options_page->buttonFunction7;
238 QList<QComboBox*> buttonActionList;
239 buttonActionList << options_page->buttonAction0 << options_page->buttonAction1 <<
240 options_page->buttonAction2 << options_page->buttonAction3 <<
241 options_page->buttonAction4 << options_page->buttonAction5 <<
242 options_page->buttonAction6 << options_page->buttonAction7;
243 QList<QDoubleSpinBox*> buttonValueList;
244 buttonValueList << options_page->buttonAmount0 << options_page->buttonAmount1 <<
245 options_page->buttonAmount2 << options_page->buttonAmount3 <<
246 options_page->buttonAmount4 << options_page->buttonAmount5 <<
247 options_page->buttonAmount6 << options_page->buttonAmount7;
250 int roll = -1, pitch = -1, yaw = -1, throttle = -1;
251 for (int i = 0; i < chList.length(); i++) {
252 switch (chList.at(i)->currentIndex()) {
253 case 1:
254 roll = i;
255 break;
256 case 2:
257 pitch = i;
258 break;
259 case 3:
260 yaw = i;
261 break;
262 case 4:
263 throttle = i;
264 break;
267 m_config->setRPYTchannels(roll, pitch, yaw, throttle);
269 m_config->setUDPControlSettings(options_page->udp_port->text().toInt(), options_page->udp_host->text());
272 int j;
273 for (j = 0; j < 8; j++) {
274 m_config->setbuttonSettingsAction(j, buttonActionList.at(j)->currentIndex());
275 m_config->setbuttonSettingsFunction(j, buttonFunctionList.at(j)->currentIndex());
276 m_config->setbuttonSettingsAmount(j, buttonValueList.at(j)->value());
277 m_config->setChannelReverse(j, chRevList.at(j)->isChecked());
281 void GCSControlGadgetOptionsPage::finish()
283 disconnect(sdlGamepad, 0, this, 0);
284 delete options_page;
285 options_page = NULL;
289 void GCSControlGadgetOptionsPage::updateButtonFunction()
291 int i;
293 /*QList<QComboBox*> buttonFunctionList;
294 buttonFunctionList << options_page->buttonFunction0 << options_page->buttonFunction1 <<
295 options_page->buttonFunction2 << options_page->buttonFunction3 <<
296 options_page->buttonFunction4 << options_page->buttonFunction5 <<
297 options_page->buttonFunction6 << options_page->buttonFunction7;
298 QList<QComboBox*> buttonActionList;
299 buttonActionList << options_page->buttonAction0 << options_page->buttonAction1 <<
300 options_page->buttonAction2 << options_page->buttonAction3 <<
301 options_page->buttonAction4 << options_page->buttonAction5 <<
302 options_page->buttonAction6 << options_page->buttonAction7;
303 QList<QDoubleSpinBox*> buttonValueList;
304 buttonValueList << options_page->buttonAmount0 << options_page->buttonAmount1 <<
305 options_page->buttonAmount2 << options_page->buttonAmount3 <<
306 options_page->buttonAmount4 << options_page->buttonAmount5 <<
307 options_page->buttonAmount6 << options_page->buttonAmount7;
308 QList<QLabel*> buttonLabelList;
309 buttonLabelList << options_page->buttonLabel0 << options_page->buttonLabel1 <<
310 options_page->buttonLabel2 << options_page->buttonLabel3 <<
311 options_page->buttonLabel4 << options_page->buttonLabel5 <<
312 options_page->buttonLabel6 << options_page->buttonLabel7;
314 for (i = 0; i < 8; i++) {
315 if (buttonActionList.at(i)->currentText().compare("Does nothing") == 0) {
316 buttonFunctionList.at(i)->setVisible(0);
317 buttonLabelList.at(i)->setVisible(0);
318 buttonValueList.at(i)->setVisible(0);
319 } else if (buttonActionList.at(i)->currentText().compare("Toggles") == 0) {
320 buttonFunctionList.at(i)->setVisible(1);
321 buttonLabelList.at(i)->setVisible(0);
322 buttonValueList.at(i)->setVisible(0);
323 } else {
324 buttonFunctionList.at(i)->setVisible(1);
325 buttonLabelList.at(i)->setVisible(1);
326 buttonValueList.at(i)->setVisible(1);
331 void GCSControlGadgetOptionsPage::updateButtonAction(int controlID)
333 int i;
334 QStringList buttonOptions;
336 /*QList<QComboBox*> buttonFunctionList;
337 buttonFunctionList << options_page->buttonFunction0 << options_page->buttonFunction1 <<
338 options_page->buttonFunction2 << options_page->buttonFunction3 <<
339 options_page->buttonFunction4 << options_page->buttonFunction5 <<
340 options_page->buttonFunction6 << options_page->buttonFunction7;
341 QList<QComboBox*> buttonActionList;
342 buttonActionList << options_page->buttonAction0 << options_page->buttonAction1 <<
343 options_page->buttonAction2 << options_page->buttonAction3 <<
344 options_page->buttonAction4 << options_page->buttonAction5 <<
345 options_page->buttonAction6 << options_page->buttonAction7;
346 QList<QDoubleSpinBox*> buttonValueList;
347 buttonValueList << options_page->buttonAmount0 << options_page->buttonAmount1 <<
348 options_page->buttonAmount2 << options_page->buttonAmount3 <<
349 options_page->buttonAmount4 << options_page->buttonAmount5 <<
350 options_page->buttonAmount6 << options_page->buttonAmount7;
351 QList<QLabel*> buttonLabelList;
352 buttonLabelList << options_page->buttonLabel0 << options_page->buttonLabel1 <<
353 options_page->buttonLabel2 << options_page->buttonLabel3 <<
354 options_page->buttonLabel4 << options_page->buttonLabel5 <<
355 options_page->buttonLabel6 << options_page->buttonLabel7;
357 // for (i=0;i<8;i++)
358 i = controlID;
360 if (buttonActionList.at(i)->currentText().compare("Does nothing") == 0) {
361 buttonFunctionList.at(i)->setVisible(0);
362 buttonLabelList.at(i)->setVisible(0);
363 buttonValueList.at(i)->setVisible(0);
364 } else if (buttonActionList.at(i)->currentText().compare("Toggles") == 0) {
365 disconnect(buttonFunctionList.at(i), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonFunction()));
366 buttonOptions << "-" << "Armed" << "GCS Control" << "UDP Control";
367 buttonFunctionList.at(i)->clear();
368 buttonFunctionList.at(i)->insertItems(-1, buttonOptions);
370 buttonFunctionList.at(i)->setVisible(1);
371 buttonLabelList.at(i)->setVisible(0);
372 buttonValueList.at(i)->setVisible(0);
373 connect(buttonFunctionList.at(i), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonFunction()));
374 } else {
375 disconnect(buttonFunctionList.at(i), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonFunction()));
376 buttonOptions << "-" << "Roll" << "Pitch" << "Yaw" << "Throttle";
377 buttonFunctionList.at(i)->clear();
378 buttonFunctionList.at(i)->addItems(buttonOptions);
380 buttonFunctionList.at(i)->setVisible(1);
381 buttonLabelList.at(i)->setVisible(1);
382 buttonValueList.at(i)->setVisible(1);
383 connect(buttonFunctionList.at(i), SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtonFunction()));