QGCParamSlider.cc 13.9 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
    uas = NULL;
pixhawk's avatar
pixhawk committed
26 27 28

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

29
    ui->editInfoCheckBox->hide();
30 31 32 33 34 35 36 37
    ui->editDoneButton->hide();
    ui->editNameLabel->hide();
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
38 39 40 41 42 43
    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
44
    connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
45 46 47 48 49 50

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

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

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

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

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

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

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

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

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

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

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

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

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

    // 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);
            }
172 173 174 175 176

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

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

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

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

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

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

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

294 295 296 297 298 299
/**
 * @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
 */
300
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
pixhawk's avatar
pixhawk committed
301
{
302 303 304
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
305 306 307 308
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
309 310 311 312
            found = true;
        }
    }

313 314
    if (!found)
    {
315 316 317 318 319
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
320 321 322 323
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
324 325 326 327
            found = true;
        }
    }

328 329
    if (!found)
    {
330 331 332
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

333
    Q_UNUSED(uas);
334 335
    if (component == this->component && parameterName == this->parameterName)
    {
336
        parameterValue = value;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        switch (value.type())
        {
        case QVariant::Int:
            ui->intValueSpinBox->show();
            ui->doubleValueSpinBox->hide();
            ui->intValueSpinBox->setValue(value.toDouble());
            ui->intValueSpinBox->setMinimum(-ui->intValueSpinBox->maximum());
            break;
        case QVariant::UInt:
            ui->intValueSpinBox->show();
            ui->doubleValueSpinBox->hide();
            ui->intValueSpinBox->setValue(value.toDouble());
            ui->intValueSpinBox->setMinimum(0);
            break;
        case QMetaType::Float:
            ui->doubleValueSpinBox->setValue(value.toDouble());
            ui->doubleValueSpinBox->show();
            ui->intValueSpinBox->hide();
            break;
        default:
            qCritical() << "ERROR: NO VALID PARAM TYPE";
            return;
        }
        ui->valueSlider->setValue(floatToScaledInt(value.toDouble()));
361
    }
Lorenz Meier's avatar
Lorenz Meier committed
362 363 364 365 366

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

369 370 371 372 373 374 375 376 377 378 379
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
380

pixhawk's avatar
pixhawk committed
381
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
382
{
383
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
384
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
385
    return result;
pixhawk's avatar
pixhawk committed
386
}
lm's avatar
lm committed
387

pixhawk's avatar
pixhawk committed
388 389
int QGCParamSlider::floatToScaledInt(float value)
{
390
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
391
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
392
    return result;
lm's avatar
lm committed
393 394
}

pixhawk's avatar
pixhawk committed
395
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
396
{
pixhawk's avatar
pixhawk committed
397 398 399
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
400 401
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
402 403
    settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
404
    settings.setValue("QGC_PARAM_SLIDER_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());
pixhawk's avatar
pixhawk committed
405
    settings.sync();
lm's avatar
lm committed
406 407 408 409
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
410 411
    parameterName = settings.value("QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt();
pixhawk's avatar
pixhawk committed
412
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
413
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
414
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
415 416
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
417
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
418 419
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
420
    showInfo(settings.value("QGC_PARAM_SLIDER_DISPLAY_INFO", true).toBool());
421 422
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
423 424 425 426 427

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

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