2 ******************************************************************************
4 * @file lineardialgadgetoptionspage.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup LinearDialPlugin Linear Dial Plugin
10 * @brief Impliments a gadget that displays linear gauges
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 "lineardialgadgetoptionspage.h"
29 #include "lineardialgadgetconfiguration.h"
30 #include "ui_lineardialgadgetoptionspage.h"
31 #include "extensionsystem/pluginmanager.h"
32 #include "uavobjectmanager.h"
33 #include "uavdataobject.h"
35 #include <QFileDialog>
36 #include <QtAlgorithms>
37 #include <QStringList>
38 #include <QFontDialog>
40 LineardialGadgetOptionsPage::LineardialGadgetOptionsPage(LineardialGadgetConfiguration
*config
, QObject
*parent
) :
44 font
= QFont("Arial", 12); // Default in case nothing exists yet.
47 // creates options page widget (uses the UI file)
48 QWidget
*LineardialGadgetOptionsPage::createPage(QWidget
*parent
)
51 options_page
= new Ui::LineardialGadgetOptionsPage();
53 QWidget
*optionsPageWidget
= new QWidget
;
55 options_page
->setupUi(optionsPageWidget
);
58 // Restore the contents from the settings:
59 options_page
->svgSourceFile
->setExpectedKind(Utils::PathChooser::File
);
60 options_page
->svgSourceFile
->setPromptDialogFilter(tr("SVG image (*.svg)"));
61 options_page
->svgSourceFile
->setPromptDialogTitle(tr("Choose SVG image"));
62 options_page
->svgSourceFile
->setPath(m_config
->getDialFile());
63 options_page
->minValue
->setValue(m_config
->getMin());
64 options_page
->maxValue
->setValue(m_config
->getMax());
65 // Do this by hand (in case value is zero, no signal would
67 on_rangeMin_valueChanged(m_config
->getMin());
68 on_rangeMax_valueChanged(m_config
->getMax());
69 options_page
->greenMin
->setValue(m_config
->getGreenMin());
70 options_page
->greenMax
->setValue(m_config
->getGreenMax());
71 options_page
->yellowMin
->setValue(m_config
->getYellowMin());
72 options_page
->yellowMax
->setValue(m_config
->getYellowMax());
73 options_page
->redMin
->setValue(m_config
->getRedMin());
74 options_page
->redMax
->setValue(m_config
->getRedMax());
75 options_page
->factor
->setValue(m_config
->getFactor());
76 options_page
->decPlaces
->setValue(m_config
->getDecimalPlaces());
77 font
.fromString(m_config
->getFont());
78 options_page
->useOpenGL
->setChecked(m_config
->useOpenGL());
80 // Fills the combo boxes for the UAVObjects
81 ExtensionSystem::PluginManager
*pm
= ExtensionSystem::PluginManager::instance();
82 UAVObjectManager
*objManager
= pm
->getObject
<UAVObjectManager
>();
83 QList
< QList
<UAVDataObject
*> > objList
= objManager
->getDataObjects();
84 foreach(QList
<UAVDataObject
*> list
, objList
) {
85 foreach(UAVDataObject
* obj
, list
) {
86 options_page
->objectName
->addItem(obj
->getName());
89 // select saved UAV Object field values
90 if (options_page
->objectName
->findText(m_config
->getSourceDataObject()) != -1) {
91 options_page
->objectName
->setCurrentIndex(options_page
->objectName
->findText(m_config
->getSourceDataObject()));
92 // Now load the object field values:
93 UAVDataObject
*obj
= dynamic_cast<UAVDataObject
*>(objManager
->getObject(m_config
->getSourceDataObject()));
95 on_objectName_currentIndexChanged(m_config
->getSourceDataObject());
96 // And set the highlighed value from the settings:
97 options_page
->objectField
->setCurrentIndex(options_page
->objectField
->findText(m_config
->getSourceObjectField()));
101 connect(options_page
->objectName
, SIGNAL(currentIndexChanged(QString
)), this, SLOT(on_objectName_currentIndexChanged(QString
)));
102 connect(options_page
->fontPicker
, SIGNAL(clicked()), this, SLOT(on_fontPicker_clicked()));
103 connect(options_page
->minValue
, SIGNAL(valueChanged(double)), this, SLOT(on_rangeMin_valueChanged(double)));
104 connect(options_page
->maxValue
, SIGNAL(valueChanged(double)), this, SLOT(on_rangeMax_valueChanged(double)));
106 return optionsPageWidget
;
110 * Used to make sure the green/yellow/red ranges are consistent
111 * with the overall dial range
113 void LineardialGadgetOptionsPage::on_rangeMin_valueChanged(double val
)
115 options_page
->maxValue
->setMinimum(val
);
117 options_page
->greenMin
->setMinimum(val
);
118 options_page
->yellowMin
->setMinimum(val
);
119 options_page
->redMin
->setMinimum(val
);
121 options_page
->greenMax
->setMinimum(val
);
122 options_page
->yellowMax
->setMinimum(val
);
123 options_page
->redMax
->setMinimum(val
);
127 * Used to make sure the green/yellow/red ranges are consistent
128 * with the overall dial range
130 void LineardialGadgetOptionsPage::on_rangeMax_valueChanged(double val
)
132 options_page
->minValue
->setMaximum(val
);
135 options_page
->greenMax
->setMaximum(val
);
136 options_page
->yellowMax
->setMaximum(val
);
137 options_page
->redMax
->setMaximum(val
);
139 options_page
->greenMin
->setMaximum(val
);
140 options_page
->yellowMin
->setMaximum(val
);
141 options_page
->redMin
->setMaximum(val
);
146 * Called when the user presses apply or OK.
148 * Saves the current values
151 void LineardialGadgetOptionsPage::apply()
153 m_config
->setDialFile(options_page
->svgSourceFile
->path());
154 double rangeMin
= options_page
->minValue
->value();
155 double rangeMax
= options_page
->maxValue
->value();
156 m_config
->setRange(rangeMin
, rangeMax
);
157 m_config
->setGreenRange(options_page
->greenMin
->value(), options_page
->greenMax
->value());
158 m_config
->setYellowRange(options_page
->yellowMin
->value(), options_page
->yellowMax
->value());
159 m_config
->setRedRange(options_page
->redMin
->value(), options_page
->redMax
->value());
160 m_config
->setSourceDataObject(options_page
->objectName
->currentText());
161 m_config
->setSourceObjField(options_page
->objectField
->currentText());
162 m_config
->setFont(font
.toString());
163 m_config
->setDecimalPlaces(options_page
->decPlaces
->value());
164 m_config
->setFactor(options_page
->factor
->value());
165 m_config
->setUseOpenGL(options_page
->useOpenGL
->checkState());
169 * Opens a font picker.
172 void LineardialGadgetOptionsPage::on_fontPicker_clicked()
176 font
= QFontDialog::getFont(&ok
, font
, qobject_cast
<QWidget
*>(this));
181 Fills in the field1 combo box when value is changed in the
184 void LineardialGadgetOptionsPage::on_objectName_currentIndexChanged(QString val
)
186 options_page
->objectField
->clear();
187 ExtensionSystem::PluginManager
*pm
= ExtensionSystem::PluginManager::instance();
188 UAVObjectManager
*objManager
= pm
->getObject
<UAVObjectManager
>();
189 UAVDataObject
*obj
= dynamic_cast<UAVDataObject
*>(objManager
->getObject(val
));
190 QList
<UAVObjectField
*> fieldList
= obj
->getFields();
191 foreach(UAVObjectField
* field
, fieldList
) {
192 if (field
->getElementNames().count() > 1) {
193 foreach(QString elemName
, field
->getElementNames()) {
194 options_page
->objectField
->addItem(field
->getName() + "-" + elemName
);
197 options_page
->objectField
->addItem(field
->getName());
202 void LineardialGadgetOptionsPage::finish()