From b42f062d1d6f575706f10757adbeb742b3f04f86 Mon Sep 17 00:00:00 2001 From: lm Date: Mon, 21 Feb 2011 22:42:02 +0100 Subject: [PATCH] Fixed dynamic param widget! Now ready to use! --- src/uas/QGCUASParamManager.h | 3 + src/ui/designer/QGCActionButton.ui | 15 +++ src/ui/designer/QGCParamSlider.cc | 141 ++++++++++++++++++++++++---- src/ui/designer/QGCParamSlider.h | 10 +- src/ui/designer/QGCParamSlider.ui | 91 ++++++++++++------ src/ui/designer/QGCToolWidget.cc | 5 + src/ui/designer/QGCToolWidget.ui | 3 + src/ui/designer/QGCToolWidgetItem.h | 2 +- 8 files changed, 222 insertions(+), 48 deletions(-) diff --git a/src/uas/QGCUASParamManager.h b/src/uas/QGCUASParamManager.h index 6e4296ee9..1846f183e 100644 --- a/src/uas/QGCUASParamManager.h +++ b/src/uas/QGCUASParamManager.h @@ -22,6 +22,9 @@ public: /** @brief Request an update for this specific parameter */ void requestParameterUpdate(int component, const QString& parameter); + /** @brief Request list of parameters from MAV */ + virtual void requestParameterList() = 0; + signals: void parameterChanged(int component, QString parameter, float value); void parameterChanged(int component, int parameterIndex, float value); diff --git a/src/ui/designer/QGCActionButton.ui b/src/ui/designer/QGCActionButton.ui index 2d0469379..16e38a95a 100644 --- a/src/ui/designer/QGCActionButton.ui +++ b/src/ui/designer/QGCActionButton.ui @@ -16,9 +16,18 @@ + + + 50 + 0 + + Description + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + @@ -54,6 +63,12 @@ + + + 30 + 0 + + Button name diff --git a/src/ui/designer/QGCParamSlider.cc b/src/ui/designer/QGCParamSlider.cc index d56a4d003..ee81c8d55 100644 --- a/src/ui/designer/QGCParamSlider.cc +++ b/src/ui/designer/QGCParamSlider.cc @@ -5,6 +5,7 @@ #include "QGCParamSlider.h" #include "ui_QGCParamSlider.h" #include "UASInterface.h" +#include "UASManager.h" QGCParamSlider::QGCParamSlider(QWidget *parent) : @@ -15,24 +16,37 @@ QGCParamSlider::QGCParamSlider(QWidget *parent) : parameterMin(0.0f), parameterMax(0.0f), component(0), + parameterIndex(-1), ui(new Ui::QGCParamSlider) { ui->setupUi(this); + uas = NULL; scaledInt = ui->valueSlider->maximum() - ui->valueSlider->minimum(); - ui->editDoneButton->show(); - ui->editMaxLabel->show(); - ui->editMinLabel->show(); - ui->editNameLabel->show(); - ui->editInstructionsLabel->show(); - ui->editRefreshParamsButton->show(); - ui->editSelectParamComboBox->show(); - ui->editSelectComponentComboBox->show(); - ui->editStatusLabel->show(); - ui->editMinSpinBox->show(); - ui->editMaxSpinBox->show(); + ui->editDoneButton->hide(); + ui->editMaxLabel->hide(); + ui->editMinLabel->hide(); + ui->editNameLabel->hide(); + ui->editInstructionsLabel->hide(); + ui->editRefreshParamsButton->hide(); + ui->editSelectParamComboBox->hide(); + ui->editSelectComponentComboBox->hide(); + ui->editStatusLabel->hide(); + ui->editMinSpinBox->hide(); + ui->editMaxSpinBox->hide(); connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode())); + + // Sending actions + connect(ui->writeButton, SIGNAL(clicked()), this, SLOT(sendParameter())); + 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->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString))); + connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter())); + + // Set the current UAS if present + setActiveUAS(UASManager::instance()->getActiveUAS()); } QGCParamSlider::~QGCParamSlider() @@ -40,6 +54,42 @@ QGCParamSlider::~QGCParamSlider() delete ui; } +void QGCParamSlider::setActiveUAS(UASInterface* activeUas) +{ + if (activeUas) + { + if (uas) + { + disconnect(uas, SIGNAL(parameterChanged(int,int,int,int,QString,float)), this, SLOT(setParameterValue(int,int,int,int,QString,float))); + disconnect(ui->editRefreshParamsButton, SIGNAL(clicked()), uas->getParamManager(), SLOT(requestParameterList())); + } + + // Connect buttons and signals + connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,float)), this, SLOT(setParameterValue(int,int,int,int,QString,float)), Qt::UniqueConnection); + connect(ui->editRefreshParamsButton, SIGNAL(clicked()), activeUas->getParamManager(), SLOT(requestParameterList()), Qt::UniqueConnection); + uas = activeUas; + } +} + +void QGCParamSlider::requestParameter() +{ + if (parameterIndex != -1) + { + uas->requestParameter(this->component, this->parameterIndex); + } +} + +void QGCParamSlider::selectComponent(int componentIndex) +{ + this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt(); +} + +void QGCParamSlider::selectParameter(int paramIndex) +{ + parameterName = ui->editSelectParamComboBox->itemText(paramIndex); + parameterIndex = ui->editSelectParamComboBox->itemData(paramIndex).toInt(); +} + void QGCParamSlider::startEditMode() { ui->editDoneButton->show(); @@ -53,11 +103,23 @@ void QGCParamSlider::startEditMode() ui->editStatusLabel->show(); ui->editMinSpinBox->show(); ui->editMaxSpinBox->show(); + ui->writeButton->hide(); + ui->readButton->hide(); isInEditMode = true; } void QGCParamSlider::endEditMode() { + // Store component id + selectComponent(ui->editSelectComponentComboBox->currentIndex()); + + // Store parameter name and id + selectParameter(ui->editSelectParamComboBox->currentIndex()); + + // Min/max + parameterMin = ui->editMinSpinBox->value(); + parameterMax = ui->editMaxSpinBox->value(); + ui->editDoneButton->hide(); ui->editMaxLabel->hide(); ui->editMinLabel->hide(); @@ -69,6 +131,8 @@ void QGCParamSlider::endEditMode() ui->editStatusLabel->hide(); ui->editMinSpinBox->hide(); ui->editMaxSpinBox->hide(); + ui->writeButton->show(); + ui->readButton->show(); isInEditMode = false; emit editingFinished(); } @@ -77,6 +141,7 @@ void QGCParamSlider::sendParameter() { if (QGCToolWidgetItem::uas) { + qDebug() << "SENDING" << component << parameterName << parameterValue; QGCToolWidgetItem::uas->setParameter(component, parameterName, parameterValue); } else @@ -89,7 +154,7 @@ void QGCParamSlider::setSliderValue(int sliderValue) { parameterValue = scaledIntToFloat(sliderValue); QString unit(""); - ui->valueLabel->setText(QString("%1 %2").arg(parameterValue, 0, 'f', 3).arg(unit)); + ui->valueLabel->setText(QString("%1 %2").arg(parameterValue, 6, 'f', 6, ' ').arg(unit)); } /** @@ -98,8 +163,39 @@ void QGCParamSlider::setSliderValue(int sliderValue) * @brief parameterName Key/name of the parameter * @brief value Value of the parameter */ -void QGCParamSlider::setParameterValue(int uas, int component, QString parameterName, float value) +void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, float 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); if (component == this->component && parameterName == this->parameterName) { @@ -124,12 +220,16 @@ void QGCParamSlider::changeEvent(QEvent *e) float QGCParamSlider::scaledIntToFloat(int sliderValue) { - return (((double)sliderValue)/scaledInt)*(parameterMax - parameterMin); + float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()); + qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result; + return result; } int QGCParamSlider::floatToScaledInt(float value) { - return ((value - parameterMin)/(parameterMax - parameterMin))*scaledInt; + int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt; + qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt; + return result; } void QGCParamSlider::writeSettings(QSettings& settings) @@ -137,8 +237,9 @@ void QGCParamSlider::writeSettings(QSettings& settings) settings.setValue("TYPE", "SLIDER"); settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text()); //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text()); - settings.setValue("QGC_PARAM_SLIDER_PARAMID", ui->editSelectParamComboBox->currentText()); - settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", ui->editSelectComponentComboBox->currentText()); + settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName); + settings.setValue("QGC_PARAM_SLIDER_PARAMINDEX", parameterIndex); + settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component); settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value()); settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value()); settings.sync(); @@ -147,9 +248,11 @@ void QGCParamSlider::writeSettings(QSettings& settings) void QGCParamSlider::readSettings(const QSettings& settings) { ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString()); + ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString()); //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text()); - ui->editSelectParamComboBox->setEditText(settings.value("QGC_PARAM_SLIDER_PARAMID").toString()); - ui->editSelectComponentComboBox->setEditText(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toString()); + parameterIndex = settings.value("QGC_PARAM_SLIDER_PARAMINDEX", parameterIndex).toInt(); + ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString(), parameterIndex); + ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()); ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat()); ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat()); qDebug() << "DONE READING SETTINGS"; diff --git a/src/ui/designer/QGCParamSlider.h b/src/ui/designer/QGCParamSlider.h index 07b9cdea3..ba941e5c6 100644 --- a/src/ui/designer/QGCParamSlider.h +++ b/src/ui/designer/QGCParamSlider.h @@ -27,9 +27,16 @@ public slots: /** @brief Set the slider value as parameter value */ void setSliderValue(int sliderValue); /** @brief Update the UI with the new parameter value */ - void setParameterValue(int uas, int component, QString parameterName, float value); + void setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, float value); void writeSettings(QSettings& settings); void readSettings(const QSettings& settings); + void setActiveUAS(UASInterface *uas); + void selectComponent(int componentIndex); + void selectParameter(int paramIndex); + +protected slots: + /** @brief Request the parameter of this widget from the MAV */ + void requestParameter(); protected: QString parameterName; ///< Key/Name of the parameter @@ -38,6 +45,7 @@ protected: float parameterMin; float parameterMax; int component; ///< ID of the MAV component to address + int parameterIndex; double scaledInt; void changeEvent(QEvent *e); diff --git a/src/ui/designer/QGCParamSlider.ui b/src/ui/designer/QGCParamSlider.ui index b2daaee69..3b5429f13 100644 --- a/src/ui/designer/QGCParamSlider.ui +++ b/src/ui/designer/QGCParamSlider.ui @@ -6,77 +6,103 @@ 0 0 - 499 - 175 + 606 + 179 Form - - + + Informal Name.. - + Min - + Max - + Name + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + - + 0.00 - - + + + + -999999999.000000000000000 + + + 999999999.000000000000000 + + - + + + + 300 + 16777215 + + + + 1000000 + Qt::Horizontal - - + + + + -999999999.000000000000000 + + + 999999999.000000000000000 + + - + Please configure the parameter slider now: - + - Please click on refresh to update list.. + Please click first on refresh to update selection menus.. - + Select component @@ -86,7 +112,7 @@ - + Select parameter @@ -94,14 +120,9 @@ Select parameter - - - Click on refresh.. - - - + Transmit the current slider value to the system @@ -118,7 +139,7 @@ - + Read the current parameter value on the system @@ -131,7 +152,7 @@ - + true @@ -141,13 +162,29 @@ - + Done + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + diff --git a/src/ui/designer/QGCToolWidget.cc b/src/ui/designer/QGCToolWidget.cc index 6246648fa..ccb980c69 100644 --- a/src/ui/designer/QGCToolWidget.cc +++ b/src/ui/designer/QGCToolWidget.cc @@ -105,6 +105,11 @@ QList QGCToolWidget::createWidgetsFromSettings(QWidget* parent) item = new QGCActionButton(newWidgets.at(i)); qDebug() << "CREATED BUTTON"; } + else if (type == "SLIDER") + { + item = new QGCParamSlider(newWidgets.at(i)); + qDebug() << "CREATED PARAM SLIDER"; + } if (item) { diff --git a/src/ui/designer/QGCToolWidget.ui b/src/ui/designer/QGCToolWidget.ui index e3bc6c754..f860bf401 100644 --- a/src/ui/designer/QGCToolWidget.ui +++ b/src/ui/designer/QGCToolWidget.ui @@ -14,6 +14,9 @@ Form + + 6 + diff --git a/src/ui/designer/QGCToolWidgetItem.h b/src/ui/designer/QGCToolWidgetItem.h index b477bec14..832c3ff02 100644 --- a/src/ui/designer/QGCToolWidgetItem.h +++ b/src/ui/designer/QGCToolWidgetItem.h @@ -22,7 +22,7 @@ public slots: virtual void setComponent(int comp) {_component = comp;} virtual void writeSettings(QSettings& settings) = 0; virtual void readSettings(const QSettings& settings) = 0; - void setActiveUAS(UASInterface *uas); + virtual void setActiveUAS(UASInterface *uas); signals: void editingFinished(); -- 2.22.0