Newer
Older
Michael Carpenter
committed
#include <QMenu>
#include <QContextMenuEvent>
#include <QSettings>
#include <QTimer>
#include <QToolTip>
#include "QGCComboBox.h"
#include "ui_QGCComboBox.h"
#include "UASInterface.h"
#include "UASManager.h"
QGCComboBox::QGCComboBox(QWidget *parent) :
Michael Carpenter
committed
QGCToolWidgetItem("Combo", parent),
Michael Carpenter
committed
parameterName(""),
parameterValue(0.0f),
parameterScalingFactor(0.0),
parameterMin(0.0f),
parameterMax(0.0f),
componentId(0),
Michael Carpenter
committed
ui(new Ui::QGCComboBox)
{
ui->setupUi(this);
uas = NULL;
ui->editInfoCheckBox->hide();
ui->editDoneButton->hide();
ui->editNameLabel->hide();
ui->editRefreshParamsButton->hide();
ui->editSelectParamComboBox->hide();
ui->editSelectComponentComboBox->hide();
ui->editStatusLabel->hide();
ui->editLine1->hide();
ui->editLine2->hide();
ui->editAddItemButton->hide();
ui->editRemoveItemButton->hide();
ui->editItemValueSpinBox->hide();
ui->editItemNameLabel->hide();
ui->itemValueLabel->hide();
ui->itemNameLabel->hide();
Michael Carpenter
committed
ui->infoLabel->hide();
ui->editOptionComboBox->setEnabled(false);
isDisabled = true;
Michael Carpenter
committed
connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
connect(ui->editOptionComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(comboBoxIndexChanged(QString)));
connect(ui->editAddItemButton,SIGNAL(clicked()),this,SLOT(addButtonClicked()));
connect(ui->editRemoveItemButton,SIGNAL(clicked()),this,SLOT(delButtonClicked()));
// Sending actions
connect(ui->writeButton, SIGNAL(clicked()), this, SLOT(setParamPending()));
Michael Carpenter
committed
connect(ui->editSelectComponentComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectComponent(int)));
connect(ui->editSelectParamComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectParameter(int)));
//connect(ui->valueSlider, SIGNAL(valueChanged(int)), this, SLOT(setSliderValue(int)));
//connect(ui->doubleValueSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setParamValue(double)));
//connect(ui->intValueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setParamValue(int)));
connect(ui->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString)));
connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter()));
connect(ui->editRefreshParamsButton, SIGNAL(clicked()), this, SLOT(refreshParameter()));
Michael Carpenter
committed
connect(ui->editInfoCheckBox, SIGNAL(clicked(bool)), this, SLOT(showInfo(bool)));
// connect to self
connect(ui->infoLabel, SIGNAL(released()), this, SLOT(showTooltip()));
// Set the current UAS if present
Michael Carpenter
committed
Michael Carpenter
committed
connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
Michael Carpenter
committed
Michael Carpenter
committed
}
QGCComboBox::~QGCComboBox()
{
delete ui;
}
void QGCComboBox::showTooltip()
{
QWidget* sender = dynamic_cast<QWidget*>(QObject::sender());
if (sender)
{
QPoint point = mapToGlobal(ui->infoLabel->pos());
Michael Carpenter
committed
QToolTip::showText(point, sender->toolTip());
}
}
void QGCComboBox::refreshParameter()
Michael Carpenter
committed
{
ui->editSelectParamComboBox->setEnabled(true);
ui->editSelectComponentComboBox->setEnabled(true);
if (uas && !parameterName.isEmpty()) {
uas->getParamManager()->requestParameterUpdate(componentId,parameterName);
ui->editStatusLabel->setText(tr("Requesting refresh..."));
Michael Carpenter
committed
}
}
void QGCComboBox::setActiveUAS(UASInterface* activeUas)
{
if (activeUas)
{
if (uas)
{
disconnect(uas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)));
}
// Connect buttons and signals
connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)), Qt::UniqueConnection);
uas = activeUas;
paramMgr = uas->getParamManager();
Michael Carpenter
committed
// Update current param value
Michael Carpenter
committed
//requestParameter();
Michael Carpenter
committed
// Set param info
QString text = paramMgr->dataModel()->getParamDescription(parameterName);
Michael Carpenter
committed
ui->infoLabel->setToolTip(text);
ui->infoLabel->show();
}
Michael Carpenter
committed
// Force-uncheck and hide label if no description is available
if (ui->editInfoCheckBox->isChecked()) {
Michael Carpenter
committed
showInfo((text.length() > 0));
}
}
}
void QGCComboBox::requestParameter()
{
if (!parameterName.isEmpty() && uas)
{
paramMgr->requestParameterUpdate(this->componentId, this->parameterName);
Michael Carpenter
committed
}
}
void QGCComboBox::showInfo(bool enable)
{
ui->editInfoCheckBox->setChecked(enable);
ui->infoLabel->setVisible(enable);
}
void QGCComboBox::selectComponent(int componentIndex)
{
this->componentId = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
Michael Carpenter
committed
}
void QGCComboBox::selectParameter(int paramIndex)
{
// Set name
parameterName = ui->editSelectParamComboBox->itemText(paramIndex);
// Update min and max values if available
UASParameterDataModel* dataModel = paramMgr->dataModel();
Michael Carpenter
committed
// Minimum
if (dataModel->isParamMinKnown(parameterName)) {
parameterMin = dataModel->getParamMin(parameterName);
Michael Carpenter
committed
}
// Maximum
if (dataModel->isParamMaxKnown(parameterName)) {
parameterMax = dataModel->getParamMax(parameterName);
Michael Carpenter
committed
}
// Description
QString text = dataModel->getParamDescription(parameterName);
Michael Carpenter
committed
//ui->infoLabel->setText(text);
Michael Carpenter
committed
showInfo(!(text.length() > 0));
}
}
}
John Tapsell
committed
void QGCComboBox::setEditMode(bool editMode)
Michael Carpenter
committed
{
John Tapsell
committed
if(!editMode) {
// Store component id
selectComponent(ui->editSelectComponentComboBox->currentIndex());
// Store parameter name and id
selectParameter(ui->editSelectParamComboBox->currentIndex());
}
Michael Carpenter
committed
John Tapsell
committed
ui->nameLabel->setVisible(!editMode);
ui->writeButton->setVisible(!editMode);
ui->readButton->setVisible(!editMode);
ui->editInfoCheckBox->setVisible(editMode);
ui->editDoneButton->setVisible(editMode);
ui->editNameLabel->setVisible(editMode);
ui->editRefreshParamsButton->setVisible(editMode);
ui->editSelectParamComboBox->setVisible(editMode);
ui->editSelectComponentComboBox->setVisible(editMode);
ui->editStatusLabel->setVisible(editMode);
ui->writeButton->setVisible(!editMode);
ui->readButton->setVisible(!editMode);
ui->editLine1->setVisible(editMode);
ui->editLine2->setVisible(editMode);
ui->editAddItemButton->setVisible(editMode);
ui->editRemoveItemButton->setVisible(editMode);
ui->editItemValueSpinBox->setVisible(editMode);
ui->editItemNameLabel->setVisible(editMode);
ui->itemValueLabel->setVisible(editMode);
ui->itemNameLabel->setVisible(editMode);
if (isDisabled)
{
John Tapsell
committed
ui->editOptionComboBox->setEnabled(editMode);
}
John Tapsell
committed
QGCToolWidgetItem::setEditMode(editMode);
Michael Carpenter
committed
}
void QGCComboBox::setParamPending()
Michael Carpenter
committed
{
uas->getParamManager()->setPendingParam(componentId, parameterName, parameterValue);
Michael Carpenter
committed
}
else {
qWarning() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
Michael Carpenter
committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
}
}
/**
* @brief uas Unmanned system sending the parameter
* @brief component UAS component sending the parameter
* @brief parameterName Key/name of the parameter
* @brief value Value of the parameter
*/
void QGCComboBox::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
{
Q_UNUSED(paramCount);
// Check if this component and parameter are part of the list
bool found = false;
for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
{
if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
{
found = true;
}
}
if (!found)
{
ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
}
// Parameter checking
found = false;
for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
{
if (parameterName == ui->editSelectParamComboBox->itemText(i))
{
found = true;
}
}
if (!found)
{
ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
}
Q_UNUSED(uas);
//comboBoxTextToValMap[ui->editItemNameLabel->text()] = ui->editItemValueSpinBox->value();
Michael Carpenter
committed
if (visibleParam != "")
{
if (parameterName == visibleParam)
{
if (visibleVal == value.toInt())
{
this->uas->requestParameter(this->componentId,this->parameterName);
Michael Carpenter
committed
visibleEnabled = true;
this->show();
}
else
{
//Disable the component here.
//ui->valueSlider->setEnabled(false);
//ui->intValueSpinBox->setEnabled(false);
//ui->doubleValueSpinBox->setEnabled(false);
visibleEnabled = false;
this->hide();
}
}
}
if (component == this->componentId && parameterName == this->parameterName)
Michael Carpenter
committed
{
Michael Carpenter
committed
if (!visibleEnabled)
{
return;
}
ui->editOptionComboBox->setEnabled(true);
isDisabled = false;
Michael Carpenter
committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
for (int i=0;i<ui->editOptionComboBox->count();i++)
{
if (comboBoxTextToValMap[ui->editOptionComboBox->itemText(i)] == value.toInt())
{
ui->editOptionComboBox->setCurrentIndex(i);
break;
}
}
}
if (paramIndex == paramCount - 1)
{
ui->editStatusLabel->setText(tr("Complete parameter list received."));
}
}
void QGCComboBox::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void QGCComboBox::writeSettings(QSettings& settings)
{
settings.setValue("TYPE", "COMBOBOX");
settings.setValue("QGC_PARAM_COMBOBOX_DESCRIPTION", ui->nameLabel->text());
//settings.setValue("QGC_PARAM_COMBOBOX_BUTTONTEXT", ui->actionButton->text());
settings.setValue("QGC_PARAM_COMBOBOX_PARAMID", parameterName);
settings.setValue("QGC_PARAM_COMBOBOX_COMPONENTID", componentId);
Michael Carpenter
committed
settings.setValue("QGC_PARAM_COMBOBOX_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());
settings.setValue("QGC_PARAM_COMBOBOX_COUNT", ui->editOptionComboBox->count());
for (int i=0;i<ui->editOptionComboBox->count();i++)
{
settings.setValue("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT",ui->editOptionComboBox->itemText(i));
settings.setValue("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL",comboBoxTextToValMap[ui->editOptionComboBox->itemText(i)]);
}
settings.sync();
}
Michael Carpenter
committed
void QGCComboBox::readSettings(const QString& pre,const QVariantMap& settings)
{
parameterName = settings.value(pre + "QGC_PARAM_COMBOBOX_PARAMID").toString();
componentId = settings.value(pre + "QGC_PARAM_COMBOBOX_COMPONENTID").toInt();
Michael Carpenter
committed
ui->nameLabel->setText(settings.value(pre + "QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
ui->editNameLabel->setText(settings.value(pre + "QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
//settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
ui->editSelectParamComboBox->addItem(settings.value(pre + "QGC_PARAM_COMBOBOX_PARAMID").toString());
ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value(pre + "QGC_PARAM_COMBOBOX_COMPONENTID").toInt()), settings.value(pre + "QGC_PARAM_COMBOBOX_COMPONENTID").toInt());
showInfo(settings.value(pre + "QGC_PARAM_COMBOBOX_DISPLAY_INFO", true).toBool());
ui->editSelectParamComboBox->setEnabled(true);
ui->editSelectComponentComboBox->setEnabled(true);
Michael Carpenter
committed
visibleParam = settings.value(pre+"QGC_PARAM_COMBOBOX_VISIBLE_PARAM","").toString();
visibleVal = settings.value(pre+"QGC_PARAM_COMBOBOX_VISIBLE_VAL",0).toInt();
QString type = settings.value(pre + "QGC_PARAM_COMBOBOX_TYPE","PARAM").toString();
Michael Carpenter
committed
int num = settings.value(pre + "QGC_PARAM_COMBOBOX_COUNT").toInt();
for (int i=0;i<num;i++)
{
Michael Carpenter
committed
QString pixmapfn = settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_IMG","").toString();
if (pixmapfn != "")
{
comboBoxIndexToPixmap[i] = QPixmap(pixmapfn);
}
Michael Carpenter
committed
ui->editOptionComboBox->addItem(settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString());
//qDebug() << "Adding val:" << settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString() << settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
comboBoxTextToValMap[settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString()] = settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
Michael Carpenter
committed
if (type == "INDIVIDUAL")
{
comboBoxTextToParamMap[settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString()] = settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_PARAM").toString();
ui->editOptionComboBox->setEnabled(true);
}
Michael Carpenter
committed
}
setActiveUAS(UASManager::instance()->getActiveUAS());
// Get param value after settings have been loaded
// requestParameter();
}
Michael Carpenter
committed
void QGCComboBox::readSettings(const QSettings& settings)
{
Michael Carpenter
committed
QVariantMap map;
foreach (QString key,settings.allKeys())
{
map[key] = settings.value(key);
}
Michael Carpenter
committed
Michael Carpenter
committed
readSettings("",map);
Michael Carpenter
committed
Michael Carpenter
committed
//parameterName = settings.value("QGC_PARAM_COMBOBOX_PARAMID").toString();
//component = settings.value("QGC_PARAM_COMBOBOX_COMPONENTID").toInt();
//ui->nameLabel->setText(settings.value("QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
//ui->editNameLabel->setText(settings.value("QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
//ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_COMBOBOX_PARAMID").toString());
//ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
//ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_COMBOBOX_COMPONENTID").toInt()), settings.value("QGC_PARAM_COMBOBOX_COMPONENTID").toInt());
//showInfo(settings.value("QGC_PARAM_COMBOBOX_DISPLAY_INFO", true).toBool());
//ui->editSelectParamComboBox->setEnabled(true);
//ui->editSelectComponentComboBox->setEnabled(true);
/*int num = settings.value("QGC_PARAM_COMBOBOX_COUNT").toInt();
Michael Carpenter
committed
for (int i=0;i<num;i++)
{
Michael Carpenter
committed
QString pixmapfn = settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_IMG","").toString();
if (pixmapfn != "")
{
comboBoxIndexToPixmap[i] = QPixmap(pixmapfn);
}
Michael Carpenter
committed
ui->editOptionComboBox->addItem(settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString());
qDebug() << "Adding val:" << settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i)).toString() << settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
comboBoxTextToValMap[settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString()] = settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
Michael Carpenter
committed
}*/
Michael Carpenter
committed
Michael Carpenter
committed
//setActiveUAS(UASManager::instance()->getActiveUAS());*/
Michael Carpenter
committed
// Get param value after settings have been loaded
Michael Carpenter
committed
//requestParameter();
Michael Carpenter
committed
}
void QGCComboBox::addButtonClicked()
{
ui->editOptionComboBox->addItem(ui->editItemNameLabel->text());
comboBoxTextToValMap[ui->editItemNameLabel->text()] = ui->editItemValueSpinBox->value();
}
void QGCComboBox::delButtonClicked()
{
int index = ui->editOptionComboBox->currentIndex();
comboBoxTextToValMap.remove(ui->editOptionComboBox->currentText());
ui->editOptionComboBox->removeItem(index);
}
void QGCComboBox::comboBoxIndexChanged(QString val)
{
Michael Carpenter
committed
ui->imageLabel->setPixmap(comboBoxIndexToPixmap[ui->editOptionComboBox->currentIndex()]);
Michael Carpenter
committed
if (comboBoxTextToParamMap.contains(ui->editOptionComboBox->currentText()))
{
parameterName = comboBoxTextToParamMap.value(ui->editOptionComboBox->currentText());
}
Michael Carpenter
committed
switch (parameterValue.type())
{
case QVariant::Char:
parameterValue = QVariant(QChar((unsigned char)comboBoxTextToValMap[val]));
break;
case QVariant::Int:
parameterValue = (int)comboBoxTextToValMap[val];
break;
case QVariant::UInt:
parameterValue = (unsigned int)comboBoxTextToValMap[val];
break;
case QMetaType::Float:
parameterValue =(float)comboBoxTextToValMap[val];
break;
default:
qCritical() << "ERROR: NO VALID PARAM TYPE";
return;
}
}