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

6 7
#include "QGCParamSlider.h"
#include "ui_QGCParamSlider.h"
8
#include "UASInterface.h"
9
#include "UASManager.h"
10

11

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

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

28 29 30 31 32 33 34 35 36 37 38
    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();
pixhawk's avatar
pixhawk committed
39
    connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
40 41 42 43 44 45

    // 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)));
46
    connect(ui->valueSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setParamValue(double)));
47 48
    connect(ui->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString)));
    connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter()));
49
    connect(ui->editRefreshParamsButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));
50 51 52

    // Set the current UAS if present
    setActiveUAS(UASManager::instance()->getActiveUAS());
53 54 55

    // Get param value
    QTimer::singleShot(1000, this, SLOT(requestParameter()));
56 57 58 59 60 61 62
}

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

63 64 65 66 67 68 69 70 71 72
void QGCParamSlider::refreshParamList()
{
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
    if (uas)
    {
        uas->getParamManager()->requestParameterList();
    }
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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)));
        }

        // 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);
        uas = activeUas;
    }
}

void QGCParamSlider::requestParameter()
{
    if (parameterIndex != -1)
    {
        uas->requestParameter(this->component, this->parameterIndex);
    }
}

96 97 98 99 100 101
void QGCParamSlider::setParamValue(double value)
{
    parameterValue = value;
    ui->valueSlider->setValue(floatToScaledInt(value));
}

102 103 104 105 106 107 108 109 110 111 112
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();
}

113 114
void QGCParamSlider::startEditMode()
{
pixhawk's avatar
pixhawk committed
115 116 117
    ui->editDoneButton->show();
    ui->editMaxLabel->show();
    ui->editMinLabel->show();
118
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
119 120 121 122 123 124 125
    ui->editInstructionsLabel->show();
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
126 127
    ui->writeButton->hide();
    ui->readButton->hide();
128 129 130 131 132
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
133 134 135 136 137 138 139 140 141 142
    // 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();

pixhawk's avatar
pixhawk committed
143 144 145
    ui->editDoneButton->hide();
    ui->editMaxLabel->hide();
    ui->editMinLabel->hide();
146
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
147 148 149 150 151 152 153
    ui->editInstructionsLabel->hide();
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
154 155
    ui->writeButton->show();
    ui->readButton->show();
156
    isInEditMode = false;
lm's avatar
lm committed
157
    emit editingFinished();
158 159
}

160 161
void QGCParamSlider::sendParameter()
{
162
    if (uas)
163
    {
164 165
        // Set value, param manager handles retransmission
        uas->getParamManager()->setParameter(component, parameterName, parameterValue);
166 167 168 169 170 171 172
    }
    else
    {
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
173 174 175
void QGCParamSlider::setSliderValue(int sliderValue)
{
    parameterValue = scaledIntToFloat(sliderValue);
176 177 178
    ui->valueSpinBox->setValue(parameterValue);
//    QString unit("");
//    ui->valueLabel->setText(QString("%1 %2").arg(parameterValue, 6, 'f', 6, ' ').arg(unit));
pixhawk's avatar
pixhawk committed
179 180
}

181 182 183 184 185 186
/**
 * @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
 */
187
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, float value)
pixhawk's avatar
pixhawk committed
188
{
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    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);
    }

220 221 222 223
    Q_UNUSED(uas);
    if (component == this->component && parameterName == this->parameterName)
    {
        parameterValue = value;
224
        ui->valueSpinBox->setValue(value);
225 226
        ui->valueSlider->setValue(floatToScaledInt(value));
    }
pixhawk's avatar
pixhawk committed
227 228
}

229 230 231 232 233 234 235 236 237 238 239
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
240

pixhawk's avatar
pixhawk committed
241
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
242
{
243
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
244
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
245
    return result;
pixhawk's avatar
pixhawk committed
246
}
lm's avatar
lm committed
247

pixhawk's avatar
pixhawk committed
248 249
int QGCParamSlider::floatToScaledInt(float value)
{
250
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
251
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
252
    return result;
lm's avatar
lm committed
253 254
}

pixhawk's avatar
pixhawk committed
255
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
256
{
pixhawk's avatar
pixhawk committed
257 258 259
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
260 261 262
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_PARAMINDEX", parameterIndex);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
263 264 265
    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
266 267 268 269
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
pixhawk's avatar
pixhawk committed
270
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
271
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
272
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
273 274 275
    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());
276 277
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
278 279
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
lm's avatar
lm committed
280
}