QGCComboBox.cc 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <QMenu>
#include <QContextMenuEvent>
#include <QSettings>
#include <QTimer>
#include <QToolTip>

#include "QGCComboBox.h"
#include "ui_QGCComboBox.h"
#include "UASInterface.h"
#include "UASManager.h"


QGCComboBox::QGCComboBox(QWidget *parent) :
14
    QGCToolWidgetItem("Combo", parent),
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    parameterName(""),
    parameterValue(0.0f),
    parameterScalingFactor(0.0),
    parameterMin(0.0f),
    parameterMax(0.0f),
    component(0),
    ui(new Ui::QGCComboBox)
{
    ui->setupUi(this);
    uas = NULL;


    ui->editInfoCheckBox->hide();
    ui->editDoneButton->hide();
    ui->editNameLabel->hide();
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editLine1->hide();
    ui->editLine2->hide();
    ui->editAddItemButton->hide();
    ui->editRemoveItemButton->hide();
    ui->editItemValueSpinBox->hide();
    ui->editItemNameLabel->hide();
    ui->itemValueLabel->hide();
    ui->itemNameLabel->hide();
42
    ui->infoLabel->hide();
43 44
    ui->editOptionComboBox->setEnabled(false);
    isDisabled = true;
45 46 47 48 49 50
    connect(ui->editDoneButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
    connect(ui->editOptionComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(comboBoxIndexChanged(QString)));
    connect(ui->editAddItemButton,SIGNAL(clicked()),this,SLOT(addButtonClicked()));
    connect(ui->editRemoveItemButton,SIGNAL(clicked()),this,SLOT(delButtonClicked()));

    // Sending actions
51
    connect(ui->writeButton, SIGNAL(clicked()), this, SLOT(setParamPending()));
52 53 54 55 56 57 58 59 60 61 62 63
    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)));
    //connect(ui->doubleValueSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setParamValue(double)));
    //connect(ui->intValueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setParamValue(int)));
    connect(ui->editNameLabel, SIGNAL(textChanged(QString)), ui->nameLabel, SLOT(setText(QString)));
    connect(ui->readButton, SIGNAL(clicked()), this, SLOT(requestParameter()));
    connect(ui->editRefreshParamsButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));
    connect(ui->editInfoCheckBox, SIGNAL(clicked(bool)), this, SLOT(showInfo(bool)));
    // connect to self
    connect(ui->infoLabel, SIGNAL(released()), this, SLOT(showTooltip()));
    // Set the current UAS if present
64

65
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
66

67 68 69 70 71 72 73 74 75 76 77 78 79
}

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

void QGCComboBox::showTooltip()
{
    QWidget* sender = dynamic_cast<QWidget*>(QObject::sender());

    if (sender)
    {
80
        QPoint point = mapToGlobal(ui->infoLabel->pos());
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        QToolTip::showText(point, sender->toolTip());
    }
}

void QGCComboBox::refreshParamList()
{
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
    if (uas)
    {
        uas->getParamManager()->requestParameterList();
        ui->editStatusLabel->setText(tr("Parameter list updating.."));
    }
}

void QGCComboBox::setActiveUAS(UASInterface* activeUas)
{
    if (activeUas)
    {
        if (uas)
        {
            disconnect(uas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)));
        }

        // Connect buttons and signals
        connect(activeUas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(setParameterValue(int,int,int,int,QString,QVariant)), Qt::UniqueConnection);
        uas = activeUas;
        // Update current param value
109
        //requestParameter();
110
        // Set param info
111 112 113

        QString text = uas->getParamDataModel()->getParamDescription(parameterName);
        if (!text.isEmpty()) {
114 115 116
            ui->infoLabel->setToolTip(text);
            ui->infoLabel->show();
        }
117
        // Force-uncheck and hide label if no description is available
118
        if (ui->editInfoCheckBox->isChecked())  {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            showInfo((text.length() > 0));
        }
    }
}

void QGCComboBox::requestParameter()
{
    if (!parameterName.isEmpty() && uas)
    {
        uas->getParamManager()->requestParameterUpdate(this->component, this->parameterName);
    }
}

void QGCComboBox::showInfo(bool enable)
{
    ui->editInfoCheckBox->setChecked(enable);
    ui->infoLabel->setVisible(enable);
}

void QGCComboBox::selectComponent(int componentIndex)
{
    this->component = ui->editSelectComponentComboBox->itemData(componentIndex).toInt();
}

void QGCComboBox::selectParameter(int paramIndex)
{
    // Set name
    parameterName = ui->editSelectParamComboBox->itemText(paramIndex);

    // Update min and max values if available
149 150 151
    if (uas)  {
        UASParameterDataModel* dataModel =  uas->getParamDataModel();
        if (dataModel) {
152
            // Minimum
153 154
            if (dataModel->isParamMinKnown(parameterName)) {
                parameterMin = dataModel->getParamMin(parameterName);
155 156 157
            }

            // Maximum
158 159
            if (dataModel->isParamMaxKnown(parameterName)) {
                parameterMax = dataModel->getParamMax(parameterName);
160 161 162
            }

            // Description
163
            QString text = dataModel->getParamDescription(parameterName);
164
            //ui->infoLabel->setText(text);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
            showInfo(!(text.length() > 0));
        }
    }
}

void QGCComboBox::startEditMode()
{
    ui->nameLabel->hide();
    ui->writeButton->hide();
    ui->readButton->hide();

    ui->editInfoCheckBox->show();
    ui->editDoneButton->show();
    ui->editNameLabel->show();
    ui->editRefreshParamsButton->show();
    ui->editSelectParamComboBox->show();
    ui->editSelectComponentComboBox->show();
    ui->editStatusLabel->show();
    ui->writeButton->hide();
    ui->readButton->hide();
    ui->editLine1->show();
    ui->editLine2->show();
    ui->editAddItemButton->show();
    ui->editRemoveItemButton->show();
    ui->editItemValueSpinBox->show();
    ui->editItemNameLabel->show();
    ui->itemValueLabel->show();
    ui->itemNameLabel->show();
193 194 195 196
    if (isDisabled)
    {
        ui->editOptionComboBox->setEnabled(true);
    }
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    isInEditMode = true;
}

void QGCComboBox::endEditMode()
{
    // Store component id
    selectComponent(ui->editSelectComponentComboBox->currentIndex());

    // Store parameter name and id
    selectParameter(ui->editSelectParamComboBox->currentIndex());

    // Min/max

    ui->editInfoCheckBox->hide();
    ui->editDoneButton->hide();
    ui->editNameLabel->hide();
    ui->editRefreshParamsButton->hide();
    ui->editSelectParamComboBox->hide();
    ui->editSelectComponentComboBox->hide();
    ui->editStatusLabel->hide();
    ui->editLine1->hide();
    ui->editLine2->hide();
    ui->writeButton->show();
    ui->readButton->show();
    ui->editAddItemButton->hide();
    ui->editRemoveItemButton->hide();
    ui->editItemValueSpinBox->hide();
    ui->editItemNameLabel->hide();
    ui->itemValueLabel->hide();
    ui->itemNameLabel->hide();
    ui->nameLabel->show();
228 229 230 231
    if (isDisabled)
    {
        ui->editOptionComboBox->setEnabled(false);
    }
232 233 234 235
    isInEditMode = false;
    emit editingFinished();
}

236
void QGCComboBox::setParamPending()
237
{
238 239
    if (uas)  {
        uas->getParamManager()->setPendingParam(component, parameterName, parameterValue);
240
    }
241 242
    else  {
        qWarning() << __FILE__ << __LINE__ << "NO UAS SET, DOING NOTHING";
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    }
}


/**
 * @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
 */
void QGCComboBox::setParameterValue(int uas, int component, int paramCount, int paramIndex, QString parameterName, QVariant value)
{
    Q_UNUSED(paramCount);
    // Check if this component and parameter are part of the list
    bool found = false;
    for (int i = 0; i< ui->editSelectComponentComboBox->count(); ++i)
    {
        if (component == ui->editSelectComponentComboBox->itemData(i).toInt())
        {
            found = true;
        }
    }

    if (!found)
    {
        ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(component), component);
    }

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

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

    Q_UNUSED(uas);

     //comboBoxTextToValMap[ui->editItemNameLabel->text()] = ui->editItemValueSpinBox->value();
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    if (visibleParam != "")
    {
        if (parameterName == visibleParam)
        {
            if (visibleVal == value.toInt())
            {
                this->uas->requestParameter(this->component,this->parameterName);
                visibleEnabled = true;
                this->show();
            }
            else
            {
                //Disable the component here.
                //ui->valueSlider->setEnabled(false);
                //ui->intValueSpinBox->setEnabled(false);
                //ui->doubleValueSpinBox->setEnabled(false);
                visibleEnabled = false;
                this->hide();
            }
        }
    }
310 311
    if (component == this->component && parameterName == this->parameterName)
    {
312 313 314 315
        if (!visibleEnabled)
        {
            return;
        }
316 317
        ui->editOptionComboBox->setEnabled(true);
        isDisabled = false;
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        for (int i=0;i<ui->editOptionComboBox->count();i++)
        {
            if (comboBoxTextToValMap[ui->editOptionComboBox->itemText(i)] == value.toInt())
            {
                ui->editOptionComboBox->setCurrentIndex(i);
                break;
            }
        }
    }

    if (paramIndex == paramCount - 1)
    {
        ui->editStatusLabel->setText(tr("Complete parameter list received."));
    }
}

void QGCComboBox::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void QGCComboBox::writeSettings(QSettings& settings)
{
    settings.setValue("TYPE", "COMBOBOX");
    settings.setValue("QGC_PARAM_COMBOBOX_DESCRIPTION", ui->nameLabel->text());
    //settings.setValue("QGC_PARAM_COMBOBOX_BUTTONTEXT", ui->actionButton->text());
    settings.setValue("QGC_PARAM_COMBOBOX_PARAMID", parameterName);
    settings.setValue("QGC_PARAM_COMBOBOX_COMPONENTID", component);
    settings.setValue("QGC_PARAM_COMBOBOX_DISPLAY_INFO", ui->editInfoCheckBox->isChecked());

    settings.setValue("QGC_PARAM_COMBOBOX_COUNT", ui->editOptionComboBox->count());
    for (int i=0;i<ui->editOptionComboBox->count();i++)
    {
        settings.setValue("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT",ui->editOptionComboBox->itemText(i));
        settings.setValue("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL",comboBoxTextToValMap[ui->editOptionComboBox->itemText(i)]);
    }
    settings.sync();
}
364 365 366 367 368 369 370 371 372 373 374 375 376
void QGCComboBox::readSettings(const QString& pre,const QVariantMap& settings)
{
    parameterName = settings.value(pre + "QGC_PARAM_COMBOBOX_PARAMID").toString();
    component = settings.value(pre + "QGC_PARAM_COMBOBOX_COMPONENTID").toInt();
    ui->nameLabel->setText(settings.value(pre + "QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
    ui->editNameLabel->setText(settings.value(pre + "QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
    //settings.setValue("QGC_PARAM_SLIDER_BUTTONTEXT", ui->actionButton->text());
    ui->editSelectParamComboBox->addItem(settings.value(pre + "QGC_PARAM_COMBOBOX_PARAMID").toString());
    ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
    ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value(pre + "QGC_PARAM_COMBOBOX_COMPONENTID").toInt()), settings.value(pre + "QGC_PARAM_COMBOBOX_COMPONENTID").toInt());
    showInfo(settings.value(pre + "QGC_PARAM_COMBOBOX_DISPLAY_INFO", true).toBool());
    ui->editSelectParamComboBox->setEnabled(true);
    ui->editSelectComponentComboBox->setEnabled(true);
377 378 379
    visibleParam = settings.value(pre+"QGC_PARAM_COMBOBOX_VISIBLE_PARAM","").toString();
    visibleVal = settings.value(pre+"QGC_PARAM_COMBOBOX_VISIBLE_VAL",0).toInt();
    QString type = settings.value(pre + "QGC_PARAM_COMBOBOX_TYPE","PARAM").toString();
380 381 382
    int num = settings.value(pre + "QGC_PARAM_COMBOBOX_COUNT").toInt();
    for (int i=0;i<num;i++)
    {
383 384 385 386 387
        QString pixmapfn = settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_IMG","").toString();
        if (pixmapfn != "")
        {
            comboBoxIndexToPixmap[i] = QPixmap(pixmapfn);
        }
388 389 390
        ui->editOptionComboBox->addItem(settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString());
        //qDebug() << "Adding val:" << settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString() << settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
        comboBoxTextToValMap[settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString()] = settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
391 392 393 394 395
        if (type == "INDIVIDUAL")
        {
            comboBoxTextToParamMap[settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString()] = settings.value(pre + "QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_PARAM").toString();
            ui->editOptionComboBox->setEnabled(true);
        }
396 397 398 399 400 401 402
    }

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

    // Get param value after settings have been loaded
   // requestParameter();
}
403 404
void QGCComboBox::readSettings(const QSettings& settings)
{
405 406 407 408 409
    QVariantMap map;
    foreach (QString key,settings.allKeys())
    {
        map[key] = settings.value(key);
    }
410

411
    readSettings("",map);
412

413 414 415 416 417 418 419 420 421 422 423 424 425
    //parameterName = settings.value("QGC_PARAM_COMBOBOX_PARAMID").toString();
    //component = settings.value("QGC_PARAM_COMBOBOX_COMPONENTID").toInt();
    //ui->nameLabel->setText(settings.value("QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
    //ui->editNameLabel->setText(settings.value("QGC_PARAM_COMBOBOX_DESCRIPTION").toString());
    //ui->editSelectParamComboBox->addItem(settings.value("QGC_PARAM_COMBOBOX_PARAMID").toString());
    //ui->editSelectParamComboBox->setCurrentIndex(ui->editSelectParamComboBox->count()-1);
    //ui->editSelectComponentComboBox->addItem(tr("Component #%1").arg(settings.value("QGC_PARAM_COMBOBOX_COMPONENTID").toInt()), settings.value("QGC_PARAM_COMBOBOX_COMPONENTID").toInt());
    //showInfo(settings.value("QGC_PARAM_COMBOBOX_DISPLAY_INFO", true).toBool());
    //ui->editSelectParamComboBox->setEnabled(true);
    //ui->editSelectComponentComboBox->setEnabled(true);


    /*int num = settings.value("QGC_PARAM_COMBOBOX_COUNT").toInt();
426 427
    for (int i=0;i<num;i++)
    {
428 429 430 431 432
        QString pixmapfn = settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_IMG","").toString();
        if (pixmapfn != "")
        {
            comboBoxIndexToPixmap[i] = QPixmap(pixmapfn);
        }
433 434 435
        ui->editOptionComboBox->addItem(settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString());
        qDebug() << "Adding val:" << settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i)).toString() << settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
        comboBoxTextToValMap[settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_TEXT").toString()] = settings.value("QGC_PARAM_COMBOBOX_ITEM_" + QString::number(i) + "_VAL").toInt();
436
    }*/
437

438
    //setActiveUAS(UASManager::instance()->getActiveUAS());*/
439 440

    // Get param value after settings have been loaded
441
    //requestParameter();
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}
void QGCComboBox::addButtonClicked()
{
    ui->editOptionComboBox->addItem(ui->editItemNameLabel->text());
    comboBoxTextToValMap[ui->editItemNameLabel->text()] = ui->editItemValueSpinBox->value();
}

void QGCComboBox::delButtonClicked()
{
    int index = ui->editOptionComboBox->currentIndex();
    comboBoxTextToValMap.remove(ui->editOptionComboBox->currentText());
    ui->editOptionComboBox->removeItem(index);
}
void QGCComboBox::comboBoxIndexChanged(QString val)
{
457
    ui->imageLabel->setPixmap(comboBoxIndexToPixmap[ui->editOptionComboBox->currentIndex()]);
458 459 460 461
    if (comboBoxTextToParamMap.contains(ui->editOptionComboBox->currentText()))
    {
        parameterName = comboBoxTextToParamMap.value(ui->editOptionComboBox->currentText());
    }
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    switch (parameterValue.type())
    {
    case QVariant::Char:
        parameterValue = QVariant(QChar((unsigned char)comboBoxTextToValMap[val]));
        break;
    case QVariant::Int:
        parameterValue = (int)comboBoxTextToValMap[val];
        break;
    case QVariant::UInt:
        parameterValue = (unsigned int)comboBoxTextToValMap[val];
        break;
    case QMetaType::Float:
        parameterValue =(float)comboBoxTextToValMap[val];
        break;
    default:
        qCritical() << "ERROR: NO VALID PARAM TYPE";
        return;
    }
}