QGCParamSlider.cc 9.34 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
    parameterIndex(-1),
21 22 23
    ui(new Ui::QGCParamSlider)
{
    ui->setupUi(this);
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
    connect(ui->valueSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setParamValue(double)));
50 51
    connect(ui->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString)));
    connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter()));
52
    connect(ui->editRefreshParamsButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));
53 54 55

    // Set the current UAS if present
    setActiveUAS(UASManager::instance()->getActiveUAS());
56 57 58

    // Get param value
    QTimer::singleShot(1000, this, SLOT(requestParameter()));
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
    if (uas) {
71 72 73 74
        uas->getParamManager()->requestParameterList();
    }
}

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

        // Connect buttons and signals
        connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,float)), this, SLOT(setParameterValue(int,int,int,int,QString,float)), Qt::UniqueConnection);
        uas = activeUas;
    }
}

void QGCParamSlider::requestParameter()
{
90
    if (parameterIndex != -1) {
91 92 93 94
        uas->requestParameter(this->component, this->parameterIndex);
    }
}

95 96 97 98 99 100
void QGCParamSlider::setParamValue(double value)
{
    parameterValue = value;
    ui->valueSlider->setValue(floatToScaledInt(value));
}

101 102 103 104 105 106 107 108 109 110 111
void QGCParamSlider::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

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

112 113
void QGCParamSlider::startEditMode()
{
114 115 116 117 118 119
    ui->valueSlider->hide();
    ui->valueSpinBox->hide();
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

pixhawk's avatar
pixhawk committed
120
    ui->editDoneButton->show();
121
    ui->editNameLabel->show();
pixhawk's avatar
pixhawk committed
122 123 124 125 126 127
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->editMinSpinBox->show();
    ui->editMaxSpinBox->show();
128 129
    ui->writeButton->hide();
    ui->readButton->hide();
130 131
    ui->editLine1->show();
    ui->editLine2->show();
132 133 134 135 136
    isInEditMode = true;
}

void QGCParamSlider::endEditMode()
{
137 138 139 140 141 142 143 144 145 146
    // 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
147
    ui->editDoneButton->hide();
148
    ui->editNameLabel->hide();
pixhawk's avatar
pixhawk committed
149 150 151 152 153 154
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editMinSpinBox->hide();
    ui->editMaxSpinBox->hide();
155 156
    ui->editLine1->hide();
    ui->editLine2->hide();
157 158
    ui->writeButton->show();
    ui->readButton->show();
159 160 161
    ui->valueSlider->show();
    ui->valueSpinBox->show();
    ui->nameLabel->show();
162
    isInEditMode = false;
lm's avatar
lm committed
163
    emit editingFinished();
164 165
}

166 167
void QGCParamSlider::sendParameter()
{
168
    if (uas) {
169 170
        // Set value, param manager handles retransmission
        uas->getParamManager()->setParameter(component, parameterName, parameterValue);
171
    } else {
172 173 174 175
        qDebug() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
    }
}

pixhawk's avatar
pixhawk committed
176 177 178
void QGCParamSlider::setSliderValue(int sliderValue)
{
    parameterValue = scaledIntToFloat(sliderValue);
179 180 181
    ui->valueSpinBox->setValue(parameterValue);
//    QString unit("");
//    ui->valueLabel->setText(QString("%1 %2").arg(parameterValue, 6, 'f', 6, ' ').arg(unit));
pixhawk's avatar
pixhawk committed
182 183
}

184 185 186 187 188 189
/**
 * @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
 */
190
void QGCParamSlider::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, float value)
pixhawk's avatar
pixhawk committed
191
{
192 193 194
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
195 196
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i) {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt()) {
197 198 199 200
            found = true;
        }
    }

201
    if (!found) {
202 203 204 205 206
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

    // Parameter checking
    found = false;
207 208
    for (int i = 0; i < ui->editSelectParamComboBox->count(); ++i) {
        if (parameterName == ui->editSelectParamComboBox->itemText(i)) {
209 210 211 212
            found = true;
        }
    }

213
    if (!found) {
214 215 216
        ui->editSelectParamComboBox->addItem(parameterName, paramIndex);
    }

217
    Q_UNUSED(uas);
218
    if (component == this->component && parameterName == this->parameterName) {
219
        parameterValue = value;
220
        ui->valueSpinBox->setValue(value);
221 222
        ui->valueSlider->setValue(floatToScaledInt(value));
    }
pixhawk's avatar
pixhawk committed
223 224
}

225 226 227 228 229 230 231 232 233 234 235
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
236

pixhawk's avatar
pixhawk committed
237
float QGCParamSlider::scaledIntToFloat(int sliderValue)
lm's avatar
lm committed
238
{
239
    float result = (((double)sliderValue)/(double)scaledInt)*(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value());
240
    //qDebug() << "INT TO FLOAT: CONVERTED" << sliderValue << "TO" << result;
241
    return result;
pixhawk's avatar
pixhawk committed
242
}
lm's avatar
lm committed
243

pixhawk's avatar
pixhawk committed
244 245
int QGCParamSlider::floatToScaledInt(float value)
{
246
    int result = ((value - ui->editMinSpinBox->value())/(ui->editMaxSpinBox->value() - ui->editMinSpinBox->value()))*scaledInt;
247
    //qDebug() << "FLOAT TO INT: CONVERTED" << value << "TO" << result << "SCALEDINT" << scaledInt;
248
    return result;
lm's avatar
lm committed
249 250
}

pixhawk's avatar
pixhawk committed
251
void QGCParamSlider::writeSettings(QSettings& settings)
lm's avatar
lm committed
252
{
pixhawk's avatar
pixhawk committed
253 254 255
    settings.setValue("TYPE", "SLIDER");
    settings.setValue("QGC_PARAM_SLIDER_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
256 257 258
    settings.setValue("QGC_PARAM_SLIDER_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_SLIDER_PARAMINDEX", parameterIndex);
    settings.setValue("QGC_PARAM_SLIDER_COMPONENTID", component);
pixhawk's avatar
pixhawk committed
259 260 261
    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
262 263 264 265
}

void QGCParamSlider::readSettings(const QSettings& settings)
{
pixhawk's avatar
pixhawk committed
266
    ui->nameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
267
    ui->editNameLabel->setText(settings.value("QGC_PARAM_SLIDER_DESCRIPTION").toString());
pixhawk's avatar
pixhawk committed
268
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
269 270 271
    parameterIndex = settings.value("QGC_PARAM_SLIDER_PARAMINDEX", parameterIndex).toInt();
    ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_SLIDER_PARAMID").toString(), parameterIndex);
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt()), settings.value("QGC_PARAM_SLIDER_COMPONENTID").toInt());
272 273
    ui->editMinSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MIN").toFloat());
    ui->editMaxSpinBox->setValue(settings.value("QGC_PARAM_SLIDER_MAX").toFloat());
274 275
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
lm's avatar
lm committed
276
}