QGCParamSlider.cc 18.4 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 44
    ui->editLine1->hide();
    ui->editLine2->hide();

45 46
    //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; }");
47

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

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

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

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

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

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

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

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

void QGCParamSlider::requestParameter()
{
121 122
    if (!parameterName.isEmpty() && uas)
    {
123
        uas->getParamManager()->requestParameterUpdate(this->component, this->parameterName);
124 125 126
    }
}

127 128 129 130 131 132
void QGCParamSlider::showInfo(bool enable)
{
    ui->editInfoCheckBox->setChecked(enable);
    ui->infoLabel->setVisible(enable);
}

133 134
void QGCParamSlider::setParamValue(double value)
{
135
    parameterValue = (float)value;
136 137 138 139 140 141 142 143 144 145 146
     //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)));
147 148
}

149 150 151
void QGCParamSlider::setParamValue(int value)
{
    parameterValue = value;
152 153 154 155 156 157 158 159 160 161 162
    // 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)));
163 164
}

165 166 167 168 169 170 171
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

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

    // 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);
            }
196 197 198 199 200

            // Description
            QString text = uas->getParamManager()->getParamInfo(parameterName);
            ui->infoLabel->setText(text);
            showInfo(!(text.length() > 0));
LM's avatar
LM committed
201 202
        }
    }
203 204
}

205 206
void QGCParamSlider::startEditMode()
{
207
    ui->valueSlider->hide();
208 209
    ui->doubleValueSpinBox->hide();
    ui->intValueSpinBox->hide();
210 211 212 213
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

214
    ui->editInfoCheckBox->show();
pixhawk's avatar
pixhawk committed
215
    ui->editDoneButton->show();
216
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
217 218 219 220 221 222
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
223 224
    ui->writeButton->hide();
    ui->readButton->hide();
225 226
    ui->editLine1->show();
    ui->editLine2->show();
227 228 229 230 231
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
232 233 234 235 236 237 238 239 240 241
    // 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();

242
    ui->editInfoCheckBox->hide();
pixhawk's avatar
pixhawk committed
243
    ui->editDoneButton->hide();
244
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
245 246 247 248 249 250
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
251 252
    ui->editLine1->hide();
    ui->editLine2->hide();
253 254
    ui->writeButton->show();
    ui->readButton->show();
255
    ui->valueSlider->show();
256 257
    switch (parameterValue.type())
    {
258 259 260
    case QVariant::Char:
        ui->intValueSpinBox->show();
        break;
261 262 263 264 265 266 267 268 269 270 271 272 273
    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;
    }
274
    ui->nameLabel->show();
275
    isInEditMode = false;
lm's avatar
lm committed
276
    emit editingFinished();
277 278
}

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

pixhawk's avatar
pixhawk committed
299 300
void QGCParamSlider::setSliderValue(int sliderValue)
{
301
    if (!valueModLock && !valueModLockParam)
302
    {
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        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;
331
    }
pixhawk's avatar
pixhawk committed
332 333
}

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

357 358
    if (!found)
    {
359 360 361 362 363
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
364 365 366 367
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
368 369 370 371
            found = true;
        }
    }

372 373
    if (!found)
    {
374 375 376
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

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

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

452 453 454 455 456 457 458 459 460 461 462
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
463

pixhawk's avatar
pixhawk committed
464
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
465
{
466
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
467
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
468
    return result;
pixhawk's avatar
pixhawk committed
469
}
lm's avatar
lm committed
470

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

pixhawk's avatar
pixhawk committed
478
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
479
{
pixhawk's avatar
pixhawk committed
480 481 482
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
483 484
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
485 486
    settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
487
    settings.setValue("QGC_PARAM_SLIDER_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());
pixhawk's avatar
pixhawk committed
488
    settings.sync();
lm's avatar
lm committed
489
}
490 491 492 493 494 495 496 497 498 499 500 501
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());
502 503 504 505
    parameterMax = ui->editMaxSpinBox->value();
    parameterMin = ui->editMinSpinBox->value();
    //ui->valueSlider->setMaximum(parameterMax);
    //ui->valueSlider->setMinimum(parameterMin);
506 507 508 509 510 511 512 513 514
    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
515 516 517

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

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

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