QGCParamSlider.cc 4.75 KB
Newer Older
1 2
#include <QMenu>
#include <QContextMenuEvent>
3
#include <QSettings>
4

5 6
#include "QGCParamSlider.h"
#include "ui_QGCParamSlider.h"
7
#include "UASInterface.h"
8

9

10
QGCParamSlider::QGCParamSlider(QWidget *parent) :
11 12 13 14 15 16 17
    QGCToolWidgetItem("Slider", parent),
    parameterName(""),
    parameterValue(0.0f),
    parameterScalingFactor(0.0),
    parameterMin(0.0f),
    parameterMax(0.0f),
    component(0),
18 19 20
    ui(new Ui::QGCParamSlider)
{
    ui->setupUi(this);
pixhawk's avatar
pixhawk committed
21 22 23 24 25 26

    scaledInt = ui->valueSlider->maximum() - ui->valueSlider->minimum();

    ui->editDoneButton->show();
    ui->editMaxLabel->show();
    ui->editMinLabel->show();
27
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
28 29 30 31 32 33 34 35
    ui->editInstructionsLabel->show();
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
    connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
36 37 38 39 40 41 42
}

QGCParamSlider::~QGCParamSlider()
{
    delete ui;
}

43 44
void QGCParamSlider::startEditMode()
{
pixhawk's avatar
pixhawk committed
45 46 47
    ui->editDoneButton->show();
    ui->editMaxLabel->show();
    ui->editMinLabel->show();
48
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
49 50 51 52 53 54 55
    ui->editInstructionsLabel->show();
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
56 57 58 59 60
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
pixhawk's avatar
pixhawk committed
61 62 63
    ui->editDoneButton->hide();
    ui->editMaxLabel->hide();
    ui->editMinLabel->hide();
64
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
65 66 67 68 69 70 71
    ui->editInstructionsLabel->hide();
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
72
    isInEditMode = false;
lm's avatar
lm committed
73
    emit editingFinished();
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87
void QGCParamSlider::sendParameter()
{
    if (QGCToolWidgetItem::uas)
    {
        QGCToolWidgetItem::uas->setParameter(component, parameterName, parameterValue);
    }
    else
    {
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
88 89 90 91 92 93 94
void QGCParamSlider::setSliderValue(int sliderValue)
{
    parameterValue = scaledIntToFloat(sliderValue);
    QString unit("");
    ui->valueLabel->setText(QString("%1 %2").arg(parameterValue, 0, 'f', 3).arg(unit));
}

95 96 97 98 99 100
/**
 * @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
 */
pixhawk's avatar
pixhawk committed
101 102
void QGCParamSlider::setParameterValue(int uas, int component, QString parameterName, float value)
{
103 104 105 106 107 108 109 110
    Q_UNUSED(uas);
    if (component == this->component && parameterName == this->parameterName)
    {
        parameterValue = value;
        QString unit("");
        ui->valueLabel->setText(QString("%1 %2").arg(value, 0, 'f', 3).arg(unit));
        ui->valueSlider->setValue(floatToScaledInt(value));
    }
pixhawk's avatar
pixhawk committed
111 112
}

113 114 115 116 117 118 119 120 121 122 123
void QGCParamSlider::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
lm's avatar
lm committed
124

pixhawk's avatar
pixhawk committed
125
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
126
{
pixhawk's avatar
pixhawk committed
127 128
    return (((double)sliderValue)/scaledInt)*(parameterMax - parameterMin);
}
lm's avatar
lm committed
129

pixhawk's avatar
pixhawk committed
130 131 132
int QGCParamSlider::floatToScaledInt(float value)
{
    return ((value - parameterMin)/(parameterMax - parameterMin))*scaledInt;
lm's avatar
lm committed
133 134
}

pixhawk's avatar
pixhawk committed
135
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
136
{
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144
    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_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
    settings.sync();
lm's avatar
lm committed
145 146 147 148
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
pixhawk's avatar
pixhawk committed
149 150
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
151 152 153 154
    ui->editSelectParamComboBox->setEditText(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectComponentComboBox->setEditText(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toString());
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
pixhawk's avatar
pixhawk committed
155
    qDebug() << "DONE READING SETTINGS";
lm's avatar
lm committed
156
}