QGCParamSlider.cc 13.3 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 21 22
    ui(new Ui::QGCParamSlider)
{
    ui->setupUi(this);
23
    ui->intValueSpinBox->hide();
24
    uas = NULL;
pixhawk's avatar
pixhawk committed
25 26 27

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

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

    // 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)));
50 51
    connect(ui->doubleValueSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setParamValue(double)));
    connect(ui->intValueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setParamValue(int)));
52 53
    connect(ui->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString)));
    connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter()));
54
    connect(ui->editRefreshParamsButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));
55
    connect(ui->editInfoCheckBox, SIGNAL(clicked(bool)), this, SLOT(showInfo(bool)));
56 57

    // Set the current UAS if present
58
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
59 60 61 62 63 64 65
}

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

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

76 77
void QGCParamSlider::setActiveUAS(UASInterface* activeUas)
{
78 79 80 81 82
    if (activeUas)
    {
        if (uas)
        {
            disconnect(uas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)));
83 84 85
        }

        // Connect buttons and signals
86
        connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)), Qt::UniqueConnection);
87
        uas = activeUas;
88
        // Update current param value
89 90 91 92 93 94
        requestParameter();
        // Set param info
        QString text = uas->getParamManager()->getParamInfo(parameterName);
        ui->infoLabel->setText(text);
        // Force-uncheck and hide label if no description is available
        if (ui->editInfoCheckBox->isChecked())
95
        {
96
            showInfo((text.length() > 0));
97
        }
98 99 100 101 102
    }
}

void QGCParamSlider::requestParameter()
{
103 104
    if (!parameterName.isEmpty() && uas)
    {
105
        uas->getParamManager()->requestParameterUpdate(this->component, this->parameterName);
106 107 108
    }
}

109 110 111 112 113 114
void QGCParamSlider::showInfo(bool enable)
{
    ui->editInfoCheckBox->setChecked(enable);
    ui->infoLabel->setVisible(enable);
}

115 116
void QGCParamSlider::setParamValue(double value)
{
117
    parameterValue = (float)value;
118 119 120
    ui->valueSlider->setValue(floatToScaledInt(value));
}

121 122 123 124 125 126
void QGCParamSlider::setParamValue(int value)
{
    parameterValue = value;
    ui->valueSlider->setValue(floatToScaledInt(value));
}

127 128 129 130 131 132 133
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

void QGCParamSlider::selectParameter(int paramIndex)
{
LM's avatar
LM committed
134
    // Set name
135
    parameterName = ui->editSelectParamComboBox->itemText(paramIndex);
LM's avatar
LM committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

    // 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);
            }
158 159 160 161 162

            // Description
            QString text = uas->getParamManager()->getParamInfo(parameterName);
            ui->infoLabel->setText(text);
            showInfo(!(text.length() > 0));
LM's avatar
LM committed
163 164
        }
    }
165 166
}

167 168
void QGCParamSlider::startEditMode()
{
169
    ui->valueSlider->hide();
170 171
    ui->doubleValueSpinBox->hide();
    ui->intValueSpinBox->hide();
172 173 174 175
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

176
    ui->editInfoCheckBox->show();
pixhawk's avatar
pixhawk committed
177
    ui->editDoneButton->show();
178
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
179 180 181 182 183 184
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
185 186
    ui->writeButton->hide();
    ui->readButton->hide();
187 188
    ui->editLine1->show();
    ui->editLine2->show();
189 190 191 192 193
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
194 195 196 197 198 199 200 201 202 203
    // 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();

204
    ui->editInfoCheckBox->hide();
pixhawk's avatar
pixhawk committed
205
    ui->editDoneButton->hide();
206
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
207 208 209 210 211 212
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
213 214
    ui->editLine1->hide();
    ui->editLine2->hide();
215 216
    ui->writeButton->show();
    ui->readButton->show();
217
    ui->valueSlider->show();
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    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;
    }
233
    ui->nameLabel->show();
234
    isInEditMode = false;
lm's avatar
lm committed
235
    emit editingFinished();
236 237
}

238 239
void QGCParamSlider::sendParameter()
{
240 241
    if (uas)
    {
242
        // Set value, param manager handles retransmission
243 244 245 246 247 248 249 250 251 252 253
        if (uas->getParamManager())
        {
            uas->getParamManager()->setParameter(component, parameterName, parameterValue);
        }
        else
        {
            qDebug() << "UAS HAS NO PARAM MANAGER, DOING NOTHING";
        }
    }
    else
    {
254 255 256 257
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
258 259
void QGCParamSlider::setSliderValue(int sliderValue)
{
260 261 262
    switch (parameterValue.type())
    {
    case QVariant::Int:
263
        parameterValue = (int)scaledIntToFloat(sliderValue);
264 265 266
        ui->intValueSpinBox->setValue(parameterValue.toInt());
        break;
    case QVariant::UInt:
267
        parameterValue = (unsigned int)scaledIntToFloat(sliderValue);
268 269 270
        ui->intValueSpinBox->setValue(parameterValue.toUInt());
        break;
    case QMetaType::Float:
271
        parameterValue = scaledIntToFloat(sliderValue);
272 273 274 275 276 277
        ui->doubleValueSpinBox->setValue(parameterValue.toFloat());
        break;
    default:
        qCritical() << "ERROR: NO VALID PARAM TYPE";
        return;
    }
pixhawk's avatar
pixhawk committed
278 279
}

280 281 282 283 284 285
/**
 * @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
 */
286
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
pixhawk's avatar
pixhawk committed
287
{
288 289 290
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
291 292 293 294
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
295 296 297 298
            found = true;
        }
    }

299 300
    if (!found)
    {
301 302 303 304 305
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
306 307 308 309
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
310 311 312 313
            found = true;
        }
    }

314 315
    if (!found)
    {
316 317 318
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

319
    Q_UNUSED(uas);
320 321
    if (component == this->component && parameterName == this->parameterName)
    {
322
        parameterValue = value;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        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()));
347
    }
pixhawk's avatar
pixhawk committed
348 349
}

350 351 352 353 354 355 356 357 358 359 360
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
361

pixhawk's avatar
pixhawk committed
362
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
363
{
364
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
365
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
366
    return result;
pixhawk's avatar
pixhawk committed
367
}
lm's avatar
lm committed
368

pixhawk's avatar
pixhawk committed
369 370
int QGCParamSlider::floatToScaledInt(float value)
{
371
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
372
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
373
    return result;
lm's avatar
lm committed
374 375
}

pixhawk's avatar
pixhawk committed
376
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
377
{
pixhawk's avatar
pixhawk committed
378 379 380
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
381 382
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
383 384
    settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
385
    settings.setValue("QGC_PARAM_SLIDER_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());
pixhawk's avatar
pixhawk committed
386
    settings.sync();
lm's avatar
lm committed
387 388 389 390
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
391 392
    parameterName = settings.value("QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt();
pixhawk's avatar
pixhawk committed
393
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
394
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
395
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
396 397
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
398
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
399 400
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
401
    showInfo(settings.value("QGC_PARAM_SLIDER_DISPLAY_INFO", true).toBool());
402 403
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
404 405 406 407 408

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

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