QGCParamSlider.cc 14.7 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 23
    ui(new Ui::QGCParamSlider)
{
    ui->setupUi(this);
24
    ui->intValueSpinBox->hide();
25 26
    ui->valueSlider->setEnabled(false);
    ui->doubleValueSpinBox->setEnabled(false);
27
    uas = NULL;
pixhawk's avatar
pixhawk committed
28 29 30

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

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

    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; }");

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

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

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

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

    if (sender)
    {
        QPoint point = mapToGlobal(pos());
        QToolTip::showText(point, sender->toolTip());
    }
}

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

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

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

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

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

131 132
void QGCParamSlider::setParamValue(double value)
{
133
    parameterValue = (float)value;
134 135 136
    ui->valueSlider->setValue(floatToScaledInt(value));
}

137 138 139 140 141 142
void QGCParamSlider::setParamValue(int value)
{
    parameterValue = value;
    ui->valueSlider->setValue(floatToScaledInt(value));
}

143 144 145 146 147 148 149
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

void QGCParamSlider::selectParameter(int paramIndex)
{
LM's avatar
LM committed
150
    // Set name
151
    parameterName = ui->editSelectParamComboBox->itemText(paramIndex);
LM's avatar
LM committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    // 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);
            }
174 175 176 177 178

            // Description
            QString text = uas->getParamManager()->getParamInfo(parameterName);
            ui->infoLabel->setText(text);
            showInfo(!(text.length() > 0));
LM's avatar
LM committed
179 180
        }
    }
181 182
}

183 184
void QGCParamSlider::startEditMode()
{
185
    ui->valueSlider->hide();
186 187
    ui->doubleValueSpinBox->hide();
    ui->intValueSpinBox->hide();
188 189 190 191
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

192
    ui->editInfoCheckBox->show();
pixhawk's avatar
pixhawk committed
193
    ui->editDoneButton->show();
194
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
195 196 197 198 199 200
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
201 202
    ui->writeButton->hide();
    ui->readButton->hide();
203 204
    ui->editLine1->show();
    ui->editLine2->show();
205 206 207 208 209
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
210 211 212 213 214 215 216 217 218 219
    // 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();

220
    ui->editInfoCheckBox->hide();
pixhawk's avatar
pixhawk committed
221
    ui->editDoneButton->hide();
222
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
223 224 225 226 227 228
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
229 230
    ui->editLine1->hide();
    ui->editLine2->hide();
231 232
    ui->writeButton->show();
    ui->readButton->show();
233
    ui->valueSlider->show();
234 235
    switch (parameterValue.type())
    {
236 237 238
    case QVariant::Char:
        ui->intValueSpinBox->show();
        break;
239 240 241 242 243 244 245 246 247 248 249 250 251
    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;
    }
252
    ui->nameLabel->show();
253
    isInEditMode = false;
lm's avatar
lm committed
254
    emit editingFinished();
255 256
}

257 258
void QGCParamSlider::sendParameter()
{
259 260
    if (uas)
    {
261
        // Set value, param manager handles retransmission
262 263 264 265 266 267 268 269 270 271 272
        if (uas->getParamManager())
        {
            uas->getParamManager()->setParameter(component, parameterName, parameterValue);
        }
        else
        {
            qDebug() << "UAS HAS NO PARAM MANAGER, DOING NOTHING";
        }
    }
    else
    {
273 274 275 276
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
277 278
void QGCParamSlider::setSliderValue(int sliderValue)
{
279 280
    switch (parameterValue.type())
    {
281 282 283 284
    case QVariant::Char:
        parameterValue = (int)scaledIntToFloat(sliderValue);
        ui->intValueSpinBox->setValue(parameterValue.toInt());
        break;
285
    case QVariant::Int:
286
        parameterValue = (int)scaledIntToFloat(sliderValue);
287 288 289
        ui->intValueSpinBox->setValue(parameterValue.toInt());
        break;
    case QVariant::UInt:
290
        parameterValue = (unsigned int)scaledIntToFloat(sliderValue);
291 292 293
        ui->intValueSpinBox->setValue(parameterValue.toUInt());
        break;
    case QMetaType::Float:
294
        parameterValue = scaledIntToFloat(sliderValue);
295 296 297 298 299 300
        ui->doubleValueSpinBox->setValue(parameterValue.toFloat());
        break;
    default:
        qCritical() << "ERROR: NO VALID PARAM TYPE";
        return;
    }
pixhawk's avatar
pixhawk committed
301 302
}

303 304 305 306 307 308
/**
 * @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
 */
309
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
pixhawk's avatar
pixhawk committed
310
{
311 312 313
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
314 315 316 317
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
318 319 320 321
            found = true;
        }
    }

322 323
    if (!found)
    {
324 325 326 327 328
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
329 330 331 332
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
333 334 335 336
            found = true;
        }
    }

337 338
    if (!found)
    {
339 340 341
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

342
    Q_UNUSED(uas);
343 344
    if (component == this->component && parameterName == this->parameterName)
    {
345
        parameterValue = value;
346 347
        switch (value.type())
        {
348 349 350 351 352 353 354
        case QVariant::Char:
            ui->intValueSpinBox->show();
            ui->intValueSpinBox->setEnabled(true);
            ui->doubleValueSpinBox->hide();
            ui->intValueSpinBox->setValue(value.toUInt());
            ui->intValueSpinBox->setMinimum(-ui->intValueSpinBox->maximum());
            break;
355 356
        case QVariant::Int:
            ui->intValueSpinBox->show();
357
            ui->intValueSpinBox->setEnabled(true);
358
            ui->doubleValueSpinBox->hide();
359
            ui->intValueSpinBox->setValue(value.toInt());
360 361 362 363
            ui->intValueSpinBox->setMinimum(-ui->intValueSpinBox->maximum());
            break;
        case QVariant::UInt:
            ui->intValueSpinBox->show();
364
            ui->intValueSpinBox->setEnabled(true);
365
            ui->doubleValueSpinBox->hide();
366
            ui->intValueSpinBox->setValue(value.toUInt());
367 368 369
            ui->intValueSpinBox->setMinimum(0);
            break;
        case QMetaType::Float:
370
            ui->doubleValueSpinBox->setValue(value.toFloat());
371
            ui->doubleValueSpinBox->show();
372 373
            ui->doubleValueSpinBox->setEnabled(true);

374 375 376 377 378 379
            ui->intValueSpinBox->hide();
            break;
        default:
            qCritical() << "ERROR: NO VALID PARAM TYPE";
            return;
        }
380
        ui->valueSlider->setEnabled(true);
381
        ui->valueSlider->setValue(floatToScaledInt(value.toDouble()));
382
    }
Lorenz Meier's avatar
Lorenz Meier committed
383 384 385 386 387

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

390 391 392 393 394 395 396 397 398 399 400
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
401

pixhawk's avatar
pixhawk committed
402
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
403
{
404
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
405
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
406
    return result;
pixhawk's avatar
pixhawk committed
407
}
lm's avatar
lm committed
408

pixhawk's avatar
pixhawk committed
409 410
int QGCParamSlider::floatToScaledInt(float value)
{
411
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
412
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
413
    return result;
lm's avatar
lm committed
414 415
}

pixhawk's avatar
pixhawk committed
416
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
417
{
pixhawk's avatar
pixhawk committed
418 419 420
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
421 422
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
423 424
    settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
425
    settings.setValue("QGC_PARAM_SLIDER_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());
pixhawk's avatar
pixhawk committed
426
    settings.sync();
lm's avatar
lm committed
427 428 429 430
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
431 432
    parameterName = settings.value("QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt();
pixhawk's avatar
pixhawk committed
433
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
434
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
435
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
436 437
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
438
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
439 440
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
441
    showInfo(settings.value("QGC_PARAM_SLIDER_DISPLAY_INFO", true).toBool());
442 443
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
444 445 446 447 448

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

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