QGCCommConfiguration.cc 6.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Comm Link Configuration
 *   @author Gus Grubba <mavlink@grubba.com>
 *
 */

31 32
#include <QPushButton>

dogmaphobic's avatar
dogmaphobic committed
33
#ifndef __ios__
34 35
#include "SerialLink.h"
#include "SerialConfigurationWindow.h"
dogmaphobic's avatar
dogmaphobic committed
36
#endif
37
#include "QGCUDPLinkConfiguration.h"
38
#include "QGCTCPLinkConfiguration.h"
39
#ifdef QT_DEBUG
40
#include "MockLinkConfiguration.h"
41
#endif
dogmaphobic's avatar
dogmaphobic committed
42
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
43
#include "LogReplayLinkConfigurationWidget.h"
dogmaphobic's avatar
dogmaphobic committed
44
#endif
45 46 47 48 49 50 51 52 53 54 55
#include "QGCCommConfiguration.h"
#include "ui_QGCCommConfiguration.h"

QGCCommConfiguration::QGCCommConfiguration(QWidget *parent, LinkConfiguration *config) :
    QDialog(parent),
    _ui(new Ui::QGCCommConfiguration)
{
    _ui->setupUi(this);
    // Add link types
    _config = config;
    _ui->typeCombo->addItem(tr("Select Type"),  LinkConfiguration::TypeLast);
dogmaphobic's avatar
dogmaphobic committed
56
#ifndef __ios__
57
    _ui->typeCombo->addItem(tr("Serial"),       LinkConfiguration::TypeSerial);
dogmaphobic's avatar
dogmaphobic committed
58
#endif
59
    _ui->typeCombo->addItem(tr("UDP"),          LinkConfiguration::TypeUdp);
60
    _ui->typeCombo->addItem(tr("TCP"),          LinkConfiguration::TypeTcp);
dogmaphobic's avatar
dogmaphobic committed
61
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
62
    _ui->typeCombo->addItem(tr("Log replay"),   LinkConfiguration::TypeLogReplay);
dogmaphobic's avatar
dogmaphobic committed
63
#endif
64
#ifdef QT_DEBUG
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    _ui->typeCombo->addItem(tr("Mock"),         LinkConfiguration::TypeMock);
#endif

#if 0

#ifdef QGC_RTLAB_ENABLED
    _ui->typeCombo->addItem(tr("Opal-RT Link"), LinkConfiguration::TypeOpal);
#endif
#ifdef QGC_XBEE_ENABLED
    _ui->typeCombo->addItem(tr("Xbee API"),     LinkConfiguration::TypeXbee);
#endif
#endif

    _ui->typeCombo->setEditable(false);
    if(config && !config->name().isEmpty()) {
        _ui->nameEdit->setText(config->name());
    } else {
        _ui->nameEdit->setText(tr("Unnamed"));
    }
    if(!config) {
        setWindowTitle(tr("Add New Communication Link"));
    } else {
        setWindowTitle(tr("Edit Communication Link"));
        _loadTypeConfigWidget(config->type());
        _ui->typeCombo->setEnabled(false);
    }
    _updateUI();
}

QGCCommConfiguration::~QGCCommConfiguration()
{
    delete _ui;
}

void QGCCommConfiguration::on_typeCombo_currentIndexChanged(int index)
{
    int type = _ui->typeCombo->itemData(index).toInt();
    _changeLinkType(type);
}

void QGCCommConfiguration::_changeLinkType(int type)
{
    //-- Do we need to change anything?
    if(type == LinkConfiguration::TypeLast || (_config && _config->type() == type)) {
        return;
    }
    // Switching connection type. Delete old config.
    delete _config;
    // Create new config instance
    QString name = _ui->nameEdit->text();
    if(name.isEmpty()) {
        name = tr("Untitled");
        _ui->nameEdit->setText(name);
    }
    _config = LinkConfiguration::createSettings(type, name);
    Q_ASSERT(_config != NULL);
    _loadTypeConfigWidget(type);
    _updateUI();
}

void QGCCommConfiguration::_loadTypeConfigWidget(int type)
{
    Q_ASSERT(_config != NULL);
    switch(type) {
dogmaphobic's avatar
dogmaphobic committed
129
#ifndef __ios__
130 131 132 133 134 135 136
        case LinkConfiguration::TypeSerial: {
            QWidget* conf = new SerialConfigurationWindow((SerialConfiguration*)_config, this);
            _ui->linkScrollArea->setWidget(conf);
            _ui->linkGroupBox->setTitle(tr("Serial Link"));
            _ui->typeCombo->setCurrentIndex(_ui->typeCombo->findData(LinkConfiguration::TypeSerial));
        }
        break;
dogmaphobic's avatar
dogmaphobic committed
137
#endif
138 139 140 141 142 143 144
        case LinkConfiguration::TypeUdp: {
            QWidget* conf = new QGCUDPLinkConfiguration((UDPConfiguration*)_config, this);
            _ui->linkScrollArea->setWidget(conf);
            _ui->linkGroupBox->setTitle(tr("UDP Link"));
            _ui->typeCombo->setCurrentIndex(_ui->typeCombo->findData(LinkConfiguration::TypeUdp));
        }
        break;
145 146 147 148 149 150 151
        case LinkConfiguration::TypeTcp: {
            QWidget* conf = new QGCTCPLinkConfiguration((TCPConfiguration*)_config, this);
            _ui->linkScrollArea->setWidget(conf);
            _ui->linkGroupBox->setTitle(tr("TCP Link"));
            _ui->typeCombo->setCurrentIndex(_ui->typeCombo->findData(LinkConfiguration::TypeTcp));
        }
        break;
dogmaphobic's avatar
dogmaphobic committed
152
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
153
        case LinkConfiguration::TypeLogReplay: {
dogmaphobic's avatar
dogmaphobic committed
154 155 156 157 158
                QWidget* conf = new LogReplayLinkConfigurationWidget((LogReplayLinkConfiguration*)_config, this);
                _ui->linkScrollArea->setWidget(conf);
                _ui->linkGroupBox->setTitle("Log Replay");
                _ui->typeCombo->setCurrentIndex(_ui->typeCombo->findData(LinkConfiguration::TypeLogReplay));
            }
Don Gagne's avatar
Don Gagne committed
159
            break;
dogmaphobic's avatar
dogmaphobic committed
160
#endif
161
#ifdef QT_DEBUG
162
        case LinkConfiguration::TypeMock: {
163 164
            QWidget* conf = new MockLinkConfiguration((MockConfiguration*)_config, this);
            _ui->linkScrollArea->setWidget(conf);
165 166 167 168
            _ui->linkGroupBox->setTitle(tr("Mock Link"));
            _ui->typeCombo->setCurrentIndex(_ui->typeCombo->findData(LinkConfiguration::TypeMock));
        }
        break;
169
#endif
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        // Cannot be the case, but in case it gets here, we cannot continue.
        default:
            reject();
            break;
    }
    // Remove "Select Type" once something is selected
    int idx = _ui->typeCombo->findData(LinkConfiguration::TypeLast);
    if(idx >= 0) {
        _ui->typeCombo->removeItem(idx);
    }
}

void QGCCommConfiguration::_updateUI()
{
    bool enableOK = false;
    if(_config) {
        if(!_ui->nameEdit->text().isEmpty()) {
            enableOK = true;
        }
    }
    QPushButton* ok = _ui->buttonBox->button(QDialogButtonBox::Ok);
    Q_ASSERT(ok != NULL);
    ok->setEnabled(enableOK);
}

void QGCCommConfiguration::on_buttonBox_accepted()
{
    if(_config) {
        _config->setName(_ui->nameEdit->text());
    }
    accept();
}

void QGCCommConfiguration::on_buttonBox_rejected()
{
    reject();
}

void QGCCommConfiguration::on_nameEdit_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    _updateUI();
212 213 214
    if(_config) {
        _config->setDynamic(false);
    }
215
}