QGCParamSlider.cc 11.4 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
    QTimer::singleShot(1000, 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 91
        // Update current param value
        requestParameter();
92 93 94 95 96
    }
}

void QGCParamSlider::requestParameter()
{
97 98 99
    if (!parameterName.isEmpty() && uas)
    {
        uas->requestParameter(this->component, this->parameterName);
100 101 102
    }
}

103 104 105 106 107 108
void QGCParamSlider::setParamValue(double value)
{
    parameterValue = value;
    ui->valueSlider->setValue(floatToScaledInt(value));
}

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

115 116 117 118 119 120 121 122 123 124
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

void QGCParamSlider::selectParameter(int paramIndex)
{
    parameterName = ui->editSelectParamComboBox->itemText(paramIndex);
}

125 126
void QGCParamSlider::startEditMode()
{
127
    ui->valueSlider->hide();
128 129
    ui->doubleValueSpinBox->hide();
    ui->intValueSpinBox->hide();
130 131 132 133
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

pixhawk's avatar
pixhawk committed
134
    ui->editDoneButton->show();
135
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
136 137 138 139 140 141
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
142 143
    ui->writeButton->hide();
    ui->readButton->hide();
144 145
    ui->editLine1->show();
    ui->editLine2->show();
146 147 148 149 150
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
151 152 153 154 155 156 157 158 159 160
    // 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
161
    ui->editDoneButton->hide();
162
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
163 164 165 166 167 168
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
169 170
    ui->editLine1->hide();
    ui->editLine2->hide();
171 172
    ui->writeButton->show();
    ui->readButton->show();
173
    ui->valueSlider->show();
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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;
    }
189
    ui->nameLabel->show();
190
    isInEditMode = false;
lm's avatar
lm committed
191
    emit editingFinished();
192 193
}

194 195
void QGCParamSlider::sendParameter()
{
196 197
    if (uas)
    {
198
        // Set value, param manager handles retransmission
199 200 201 202 203 204 205 206 207 208 209
        if (uas->getParamManager())
        {
            uas->getParamManager()->setParameter(component, parameterName, parameterValue);
        }
        else
        {
            qDebug() << "UAS HAS NO PARAM MANAGER, DOING NOTHING";
        }
    }
    else
    {
210 211 212 213
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
214 215 216
void QGCParamSlider::setSliderValue(int sliderValue)
{
    parameterValue = scaledIntToFloat(sliderValue);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    switch (parameterValue.type())
    {
    case QVariant::Int:
        ui->intValueSpinBox->setValue(parameterValue.toInt());
        break;
    case QVariant::UInt:
        ui->intValueSpinBox->setValue(parameterValue.toUInt());
        break;
    case QMetaType::Float:
        ui->doubleValueSpinBox->setValue(parameterValue.toFloat());
        break;
    default:
        qCritical() << "ERROR: NO VALID PARAM TYPE";
        return;
    }
pixhawk's avatar
pixhawk committed
232 233
}

234 235 236 237 238 239
/**
 * @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
 */
240
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
pixhawk's avatar
pixhawk committed
241
{
242 243 244
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
245 246 247 248
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
249 250 251 252
            found = true;
        }
    }

253 254
    if (!found)
    {
255 256 257 258 259
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
260 261 262 263
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i)
    {
        if (parameterName == ui->editSelectParamComboBox->itemText(i))
        {
264 265 266 267
            found = true;
        }
    }

268 269
    if (!found)
    {
270 271 272
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

273
    Q_UNUSED(uas);
274 275
    if (component == this->component && parameterName == this->parameterName)
    {
276
        parameterValue = value;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        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()));
301
    }
pixhawk's avatar
pixhawk committed
302 303
}

304 305 306 307 308 309 310 311 312 313 314
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
315

pixhawk's avatar
pixhawk committed
316
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
317
{
318
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
319
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
320
    return result;
pixhawk's avatar
pixhawk committed
321
}
lm's avatar
lm committed
322

pixhawk's avatar
pixhawk committed
323 324
int QGCParamSlider::floatToScaledInt(float value)
{
325
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
326
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
327
    return result;
lm's avatar
lm committed
328 329
}

pixhawk's avatar
pixhawk committed
330
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
331
{
pixhawk's avatar
pixhawk committed
332 333 334
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
335 336
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
337 338 339
    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
340 341 342 343
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
344 345
    parameterName = settings.value("QGC_PARAM_SLIDER_PARAMID").toString();
    component = settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt();
pixhawk's avatar
pixhawk committed
346
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
347
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
348
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
349 350
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
351
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
352 353
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
354 355
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
lm's avatar
lm committed
356
}