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

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

    // Set the current UAS if present
56
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
57
    setActiveUAS(UASManager::instance()->getActiveUAS());
58

59 60
    // Get param value after settings have been loaded
    QTimer::singleShot(300, this, SLOT(requestParameter()));
61 62 63 64 65 66 67
}

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

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

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

        // Connect buttons and signals
88
        connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)), Qt::UniqueConnection);
89
        uas = activeUas;
90
        // Update current param value
91 92 93 94 95 96 97 98
        if (parameterName == "")
        {
            refreshParamList();
        }
        else
        {
            requestParameter();
        }
99 100 101 102 103
    }
}

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

110 111
void QGCParamSlider::setParamValue(double value)
{
112
    parameterValue = (float)value;
113 114 115
    ui->valueSlider->setValue(floatToScaledInt(value));
}

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

122 123 124 125 126 127 128
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

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

    // 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);
            }
        }
    }
155 156
}

157 158
void QGCParamSlider::startEditMode()
{
159
    ui->valueSlider->hide();
160 161
    ui->doubleValueSpinBox->hide();
    ui->intValueSpinBox->hide();
162 163 164 165
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

pixhawk's avatar
pixhawk committed
166
    ui->editDoneButton->show();
167
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
168 169 170 171 172 173
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
174 175
    ui->writeButton->hide();
    ui->readButton->hide();
176 177
    ui->editLine1->show();
    ui->editLine2->show();
178 179 180 181 182
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
183 184 185 186 187 188 189 190 191 192
    // 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();

pixhawk's avatar
pixhawk committed
193
    ui->editDoneButton->hide();
194
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
195 196 197 198 199 200
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
201 202
    ui->editLine1->hide();
    ui->editLine2->hide();
203 204
    ui->writeButton->show();
    ui->readButton->show();
205
    ui->valueSlider->show();
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    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;
    }
221
    ui->nameLabel->show();
222
    isInEditMode = false;
lm's avatar
lm committed
223
    emit editingFinished();
224 225
}

226 227
void QGCParamSlider::sendParameter()
{
228 229
    if (uas)
    {
230
        // Set value, param manager handles retransmission
231 232 233 234 235 236 237 238 239 240 241
        if (uas->getParamManager())
        {
            uas->getParamManager()->setParameter(component, parameterName, parameterValue);
        }
        else
        {
            qDebug() << "UAS HAS NO PARAM MANAGER, DOING NOTHING";
        }
    }
    else
    {
242 243 244 245
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
246 247
void QGCParamSlider::setSliderValue(int sliderValue)
{
248 249 250
    switch (parameterValue.type())
    {
    case QVariant::Int:
251
        parameterValue = (int)scaledIntToFloat(sliderValue);
252 253 254
        ui->intValueSpinBox->setValue(parameterValue.toInt());
        break;
    case QVariant::UInt:
255
        parameterValue = (unsigned int)scaledIntToFloat(sliderValue);
256 257 258
        ui->intValueSpinBox->setValue(parameterValue.toUInt());
        break;
    case QMetaType::Float:
259
        parameterValue = scaledIntToFloat(sliderValue);
260 261 262 263 264 265
        ui->doubleValueSpinBox->setValue(parameterValue.toFloat());
        break;
    default:
        qCritical() << "ERROR: NO VALID PARAM TYPE";
        return;
    }
pixhawk's avatar
pixhawk committed
266 267
}

268 269 270 271 272 273
/**
 * @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
 */
274
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
pixhawk's avatar
pixhawk committed
275
{
276 277 278
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
279 280 281 282
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
283 284 285 286
            found = true;
        }
    }

287 288
    if (!found)
    {
289 290 291 292 293
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
294 295 296 297
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
298 299 300 301
            found = true;
        }
    }

302 303
    if (!found)
    {
304 305 306
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

307
    Q_UNUSED(uas);
308 309
    if (component == this->component && parameterName == this->parameterName)
    {
310
        parameterValue = value;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        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()));
335
    }
pixhawk's avatar
pixhawk committed
336 337
}

338 339 340 341 342 343 344 345 346 347 348
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
349

pixhawk's avatar
pixhawk committed
350
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
351
{
352
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
353
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
354
    return result;
pixhawk's avatar
pixhawk committed
355
}
lm's avatar
lm committed
356

pixhawk's avatar
pixhawk committed
357 358
int QGCParamSlider::floatToScaledInt(float value)
{
359
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
360
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
361
    return result;
lm's avatar
lm committed
362 363
}

pixhawk's avatar
pixhawk committed
364
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
365
{
pixhawk's avatar
pixhawk committed
366 367 368
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
369 370
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
371 372 373
    settings.setValue("QGC_PARAM_SLIDER_MIN", ui->editMinSpinBox->value());
    settings.setValue("QGC_PARAM_SLIDER_MAX", ui->editMaxSpinBox->value());
    settings.sync();
lm's avatar
lm committed
374 375 376 377
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
378 379
    parameterName = settings.value("QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt();
pixhawk's avatar
pixhawk committed
380
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
381
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
382
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
383 384
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
385
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
386 387
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
388 389
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
lm's avatar
lm committed
390
}