QGCParamSlider.cc 18.6 KB
Newer Older
1 2
#include <QMenu>
#include <QContextMenuEvent>
3
#include <QSettings>
4
#include <QTimer>
Lorenz Meier's avatar
Lorenz Meier committed
5
#include <QToolTip>
6

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

12

13
QGCParamSlider::QGCParamSlider(QWidget *parent) :
14 15 16 17 18 19 20
    QGCToolWidgetItem("Slider", parent),
    parameterName(""),
    parameterValue(0.0f),
    parameterScalingFactor(0.0),
    parameterMin(0.0f),
    parameterMax(0.0f),
    component(0),
21 22
    ui(new Ui::QGCParamSlider)
{
23 24
    valueModLock = false;
    valueModLockParam = false;
25
    ui->setupUi(this);
26
    ui->intValueSpinBox->hide();
27 28
    ui->valueSlider->setEnabled(false);
    ui->doubleValueSpinBox->setEnabled(false);
29
    uas = NULL;
pixhawk's avatar
pixhawk committed
30 31 32

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

33
    ui->editInfoCheckBox->hide();
34 35 36 37 38 39 40 41
    ui->editDoneButton->hide();
    ui->editNameLabel->hide();
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
42 43
    ui->editLine1->hide();
    ui->editLine2->hide();
44
    ui->infoLabel->hide();
45

46 47
    //ui->editLine1->setStyleSheet("QWidget { border: 1px solid #66666B; border-radius: 3px; padding: 10px 0px 0px 0px; background: #111122; }");
    //ui->editLine2->setStyleSheet("QWidget { border: 1px solid #66666B; border-radius: 3px; padding: 10px 0px 0px 0px; background: #111122; }");
48

pixhawk's avatar
pixhawk committed
49
    connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
50 51 52 53 54 55

    // 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)));
56 57
    connect(ui->doubleValueSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setParamValue(double)));
    connect(ui->intValueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setParamValue(int)));
58 59
    connect(ui->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString)));
    connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter()));
60
    connect(ui->editRefreshParamsButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));
61
    connect(ui->editInfoCheckBox, SIGNAL(clicked(bool)), this, SLOT(showInfo(bool)));
Lorenz Meier's avatar
Lorenz Meier committed
62 63
    // connect to self
    connect(ui->infoLabel, SIGNAL(released()), this, SLOT(showTooltip()));
64
    // Set the current UAS if present
65
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
66 67 68 69 70 71 72
}

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

Lorenz Meier's avatar
Lorenz Meier committed
73 74 75 76 77 78
void QGCParamSlider::showTooltip()
{
    QWidget* sender = dynamic_cast<QWidget*>(QObject::sender());

    if (sender)
    {
79
        QPoint point = mapToGlobal(ui->infoLabel->pos());
Lorenz Meier's avatar
Lorenz Meier committed
80 81 82 83
        QToolTip::showText(point, sender->toolTip());
    }
}

84 85 86 87
void QGCParamSlider::refreshParamList()
{
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
88 89
    if (uas)
    {
90
        uas->getParamManager()->requestParameterList();
Lorenz Meier's avatar
Lorenz Meier committed
91
        ui->editStatusLabel->setText(tr("Parameter list updating.."));
92 93 94
    }
}

95 96
void QGCParamSlider::setActiveUAS(UASInterface* activeUas)
{
97 98 99 100 101
    if (activeUas)
    {
        if (uas)
        {
            disconnect(uas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)));
102 103 104
        }

        // Connect buttons and signals
105
        connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)), Qt::UniqueConnection);
106
        uas = activeUas;
107
        // Update current param value
108
        //requestParameter();
109 110
        // Set param info
        QString text = uas->getParamManager()->getParamInfo(parameterName);
111 112 113 114 115
        if (text != "")
        {
            ui->infoLabel->setToolTip(text);
            ui->infoLabel->show();
        }
116 117
        // Force-uncheck and hide label if no description is available
        if (ui->editInfoCheckBox->isChecked())
118
        {
119
            showInfo((text.length() > 0));
120
        }
121 122 123 124 125
    }
}

void QGCParamSlider::requestParameter()
{
126 127
    if (!parameterName.isEmpty() && uas)
    {
128
        uas->getParamManager()->requestParameterUpdate(this->component, this->parameterName);
129 130 131
    }
}

132 133 134 135 136 137
void QGCParamSlider::showInfo(bool enable)
{
    ui->editInfoCheckBox->setChecked(enable);
    ui->infoLabel->setVisible(enable);
}

138 139
void QGCParamSlider::setParamValue(double value)
{
140
    parameterValue = (float)value;
141 142 143 144 145 146 147 148 149 150 151
     //disconnect(ui->valueSlider,SIGNAL(valueChanged(int)));
    if (!valueModLock && !valueModLockParam)
    {
        valueModLock = true;
        ui->valueSlider->setValue(floatToScaledInt(value));
    }
    else
    {
        valueModLock = false;
    }
    //connect(ui->valueSlider, SIGNAL(valueChanged(int)), this, SLOT(setSliderValue(int)));
152 153
}

154 155 156
void QGCParamSlider::setParamValue(int value)
{
    parameterValue = value;
157 158 159 160 161 162 163 164 165 166 167
    // disconnect(ui->valueSlider,SIGNAL(valueChanged(int)));
    if (!valueModLock && !valueModLockParam)
    {
        valueModLock = true;
        ui->valueSlider->setValue(floatToScaledInt(value));
    }
    else
    {
        valueModLock = false;
    }
    //connect(ui->valueSlider, SIGNAL(valueChanged(int)), this, SLOT(setSliderValue(int)));
168 169
}

170 171 172 173 174 175 176
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

void QGCParamSlider::selectParameter(int paramIndex)
{
LM's avatar
LM committed
177
    // Set name
178
    parameterName = ui->editSelectParamComboBox->itemText(paramIndex);
LM's avatar
LM committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

    // Update min and max values if available
    if (uas)
    {
        if (uas->getParamManager())
        {
            // Current value
            uas->getParamManager()->requestParameterUpdate(component, parameterName);

            // Minimum
            if (uas->getParamManager()->isParamMinKnown(parameterName))
            {
                parameterMin = uas->getParamManager()->getParamMin(parameterName);
                ui->editMinSpinBox->setValue(parameterMin);
            }

            // Maximum
            if (uas->getParamManager()->isParamMaxKnown(parameterName))
            {
                parameterMax = uas->getParamManager()->getParamMax(parameterName);
                ui->editMaxSpinBox->setValue(parameterMax);
            }
201 202

            // Description
203 204 205 206
            //QString text = uas->getParamManager()->getParamInfo(parameterName);
            //ui->infoLabel->setText(text);

            //showInfo(!(text.length() > 0));
LM's avatar
LM committed
207 208
        }
    }
209 210
}

211 212
void QGCParamSlider::startEditMode()
{
213
    ui->valueSlider->hide();
214 215
    ui->doubleValueSpinBox->hide();
    ui->intValueSpinBox->hide();
216 217 218 219
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

220
    ui->editInfoCheckBox->show();
pixhawk's avatar
pixhawk committed
221
    ui->editDoneButton->show();
222
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
223 224 225 226 227 228
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
229 230
    ui->writeButton->hide();
    ui->readButton->hide();
231 232
    ui->editLine1->show();
    ui->editLine2->show();
233 234 235 236 237
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
238 239 240 241 242 243 244 245 246 247
    // 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();

248
    ui->editInfoCheckBox->hide();
pixhawk's avatar
pixhawk committed
249
    ui->editDoneButton->hide();
250
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
251 252 253 254 255 256
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
257 258
    ui->editLine1->hide();
    ui->editLine2->hide();
259 260
    ui->writeButton->show();
    ui->readButton->show();
261
    ui->valueSlider->show();
262 263
    switch (parameterValue.type())
    {
264 265 266
    case QVariant::Char:
        ui->intValueSpinBox->show();
        break;
267 268 269 270 271 272 273 274 275 276 277 278 279
    case QVariant::Int:
        ui->intValueSpinBox->show();
        break;
    case QVariant::UInt:
        ui->intValueSpinBox->show();
        break;
    case QMetaType::Float:
        ui->doubleValueSpinBox->show();
        break;
    default:
        qCritical() << "ERROR: NO VALID PARAM TYPE";
        return;
    }
280
    ui->nameLabel->show();
281
    isInEditMode = false;
lm's avatar
lm committed
282
    emit editingFinished();
283 284
}

285 286
void QGCParamSlider::sendParameter()
{
287 288
    if (uas)
    {
289
        // Set value, param manager handles retransmission
290 291 292 293 294 295 296 297 298 299 300
        if (uas->getParamManager())
        {
            uas->getParamManager()->setParameter(component, parameterName, parameterValue);
        }
        else
        {
            qDebug() << "UAS HAS NO PARAM MANAGER, DOING NOTHING";
        }
    }
    else
    {
301 302 303 304
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
305 306
void QGCParamSlider::setSliderValue(int sliderValue)
{
307
    if (!valueModLock && !valueModLockParam)
308
    {
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        valueModLock = true;
        switch (parameterValue.type())
        {
        case QVariant::Char:
            parameterValue = QVariant(QChar((unsigned char)scaledIntToFloat(sliderValue)));
            ui->intValueSpinBox->setValue(parameterValue.toInt());
            break;
        case QVariant::Int:
            parameterValue = (int)scaledIntToFloat(sliderValue);
            ui->intValueSpinBox->setValue(parameterValue.toInt());
            break;
        case QVariant::UInt:
            parameterValue = (unsigned int)scaledIntToFloat(sliderValue);
            ui->intValueSpinBox->setValue(parameterValue.toUInt());
            break;
        case QMetaType::Float:
            parameterValue = scaledIntToFloat(sliderValue);
            ui->doubleValueSpinBox->setValue(parameterValue.toFloat());
            break;
        default:
            qCritical() << "ERROR: NO VALID PARAM TYPE";
            valueModLock = false;
            return;
        }
    }
    else
    {
        valueModLock = false;
337
    }
pixhawk's avatar
pixhawk committed
338 339
}

340 341 342 343 344 345
/**
 * @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
 */
346
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
pixhawk's avatar
pixhawk committed
347
{
348
    Q_UNUSED(paramCount);
349 350 351 352
    if (ui->nameLabel->text() == "Name")
    {
        ui->nameLabel->setText(parameterName);
    }
353 354
    // Check if this component and parameter are part of the list
    bool found = false;
355 356 357 358
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
359 360 361 362
            found = true;
        }
    }

363 364
    if (!found)
    {
365 366 367 368 369
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
370 371 372 373
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
374 375 376 377
            found = true;
        }
    }

378 379
    if (!found)
    {
380 381 382
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

383
    Q_UNUSED(uas);
384 385
    if (component == this->component && parameterName == this->parameterName)
    {
386
        parameterValue = value;
387
        ui->valueSlider->setEnabled(true);
388
        valueModLockParam = true;
389 390
        switch (value.type())
        {
391 392 393 394 395 396
        case QVariant::Char:
            ui->intValueSpinBox->show();
            ui->intValueSpinBox->setEnabled(true);
            ui->doubleValueSpinBox->hide();
            ui->intValueSpinBox->setValue(value.toUInt());
            ui->intValueSpinBox->setMinimum(-ui->intValueSpinBox->maximum());
397
            ui->valueSlider->setValue(floatToScaledInt(value.toUInt()));
398 399 400 401 402
            if (parameterMax == 0 && parameterMin == 0)
            {
                ui->editMaxSpinBox->setValue(255);
                ui->editMinSpinBox->setValue(0);
            }
403
            break;
404 405
        case QVariant::Int:
            ui->intValueSpinBox->show();
406
            ui->intValueSpinBox->setEnabled(true);
407
            ui->doubleValueSpinBox->hide();
408
            ui->intValueSpinBox->setValue(value.toInt());
409
            ui->valueSlider->setValue(floatToScaledInt(value.toInt()));
410
            ui->intValueSpinBox->setMinimum(-ui->intValueSpinBox->maximum());
411 412 413 414 415
            if (parameterMax == 0 && parameterMin == 0)
            {
                ui->editMaxSpinBox->setValue(65535);
                ui->editMinSpinBox->setValue(0);
            }
416 417 418
            break;
        case QVariant::UInt:
            ui->intValueSpinBox->show();
419
            ui->intValueSpinBox->setEnabled(true);
420
            ui->doubleValueSpinBox->hide();
421
            ui->intValueSpinBox->setValue(value.toUInt());
422
            ui->valueSlider->setValue(floatToScaledInt(value.toUInt()));
423
            ui->intValueSpinBox->setMinimum(0);
424 425 426 427 428
            if (parameterMax == 0 && parameterMin == 0)
            {
                ui->editMaxSpinBox->setValue(65535);
                ui->editMinSpinBox->setValue(0);
            }
429 430
            break;
        case QMetaType::Float:
431
            ui->doubleValueSpinBox->setValue(value.toFloat());
432
            ui->doubleValueSpinBox->show();
433
            ui->doubleValueSpinBox->setEnabled(true);
434
            ui->intValueSpinBox->hide();
435
            ui->valueSlider->setValue(floatToScaledInt(value.toFloat()));
436 437 438 439 440
            if (parameterMax == 0 && parameterMin == 0)
            {
                ui->editMaxSpinBox->setValue(10000);
                ui->editMinSpinBox->setValue(0);
            }
441 442 443
            break;
        default:
            qCritical() << "ERROR: NO VALID PARAM TYPE";
444
            valueModLockParam = false;
445 446
            return;
        }
447 448 449
        valueModLockParam = false;
        parameterMax = ui->editMaxSpinBox->value();
        parameterMin = ui->editMinSpinBox->value();
450
    }
Lorenz Meier's avatar
Lorenz Meier committed
451 452 453 454 455

    if (paramIndex == paramCount - 1)
    {
        ui->editStatusLabel->setText(tr("Complete parameter list received."));
    }
pixhawk's avatar
pixhawk committed
456 457
}

458 459 460 461 462 463 464 465 466 467 468
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
469

pixhawk's avatar
pixhawk committed
470
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
471
{
472
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
473
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
474
    return result;
pixhawk's avatar
pixhawk committed
475
}
lm's avatar
lm committed
476

pixhawk's avatar
pixhawk committed
477 478
int QGCParamSlider::floatToScaledInt(float value)
{
479
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
480
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
481
    return result;
lm's avatar
lm committed
482 483
}

pixhawk's avatar
pixhawk committed
484
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
485
{
pixhawk's avatar
pixhawk committed
486 487 488
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
489 490
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
491 492
    settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
493
    settings.setValue("QGC_PARAM_SLIDER_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());
pixhawk's avatar
pixhawk committed
494
    settings.sync();
lm's avatar
lm committed
495
}
496 497 498 499 500 501 502 503 504 505 506 507
void QGCParamSlider::readSettings(const QString& pre,const QVariantMap& settings)
{
    parameterName = settings.value(pre + "QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value(pre + "QGC_PARAM_SLIDER_COMPONENTID").toInt();
    ui->nameLabel->setText(settings.value(pre + "QGC_PARAM_SLIDER_DESCRIPTION").toString());
    ui->editNameLabel->setText(settings.value(pre + "QGC_PARAM_SLIDER_DESCRIPTION").toString());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
    ui->editSelectParamComboBox->addItem(settings.value(pre + "QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value(pre + "QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value(pre + "QGC_PARAM_SLIDER_COMPONENTID").toInt());
    ui->editMinSpinBox->setValue(settings.value(pre + "QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value(pre + "QGC_PARAM_SLIDER_MAX").toFloat());
508 509 510 511
    parameterMax = ui->editMaxSpinBox->value();
    parameterMin = ui->editMinSpinBox->value();
    //ui->valueSlider->setMaximum(parameterMax);
    //ui->valueSlider->setMinimum(parameterMin);
512 513 514 515 516 517 518 519 520
    showInfo(settings.value(pre + "QGC_PARAM_SLIDER_DISPLAY_INFO", true).toBool());
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);

    setActiveUAS(UASManager::instance()->getActiveUAS());

    // Get param value after settings have been loaded
    //requestParameter();
}
lm's avatar
lm committed
521 522 523

void QGCParamSlider::readSettings(const QSettings& settings)
{
524 525
    parameterName = settings.value("QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt();
pixhawk's avatar
pixhawk committed
526
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
527
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
528
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
529 530
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
531
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
532 533
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
534
    showInfo(settings.value("QGC_PARAM_SLIDER_DISPLAY_INFO", true).toBool());
535 536
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
537 538 539 540

    setActiveUAS(UASManager::instance()->getActiveUAS());

    // Get param value after settings have been loaded
541
    //requestParameter();
lm's avatar
lm committed
542
}