MAVLinkSettingsWidget.cc 8.79 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
lm's avatar
lm committed
9 10 11 12 13 14 15 16


/**
 * @file
 *   @brief Implementation of MAVLinkSettingsWidget
 *   @author Lorenz Meier <mail@qgroundcontrol.org>
 */

17
#include <QFileInfo>
18
#include <QStandardPaths>
19

pixhawk's avatar
pixhawk committed
20
#include "MAVLinkSettingsWidget.h"
21 22
#include "LinkManager.h"
#include "UDPLink.h"
23
#include "QGCApplication.h"
pixhawk's avatar
pixhawk committed
24
#include "ui_MAVLinkSettingsWidget.h"
25
#include <QSettings>
pixhawk's avatar
pixhawk committed
26 27 28 29 30 31 32 33

MAVLinkSettingsWidget::MAVLinkSettingsWidget(MAVLinkProtocol* protocol, QWidget *parent) :
    QWidget(parent),
    protocol(protocol),
    m_ui(new Ui::MAVLinkSettingsWidget)
{
    m_ui->setupUi(this);

34 35
    m_ui->gridLayout->setAlignment(Qt::AlignTop);

Lorenz Meier's avatar
Lorenz Meier committed
36 37 38
    // AUTH
    m_ui->droneOSCheckBox->setChecked(protocol->getAuthEnabled());
    QSettings settings;
39
    m_ui->droneOSComboBox->setCurrentIndex(m_ui->droneOSComboBox->findText(settings.value("DRONELINK_HOST", "dronelink.io:14555").toString()));
Lorenz Meier's avatar
Lorenz Meier committed
40 41
    m_ui->droneOSLineEdit->setText(protocol->getAuthKey());

lm's avatar
lm committed
42 43
    // Initialize state
    m_ui->versionCheckBox->setChecked(protocol->versionCheckEnabled());
44
    m_ui->multiplexingCheckBox->setChecked(protocol->multiplexingEnabled());
45
    m_ui->systemIdSpinBox->setValue(protocol->getSystemId());
46

47 48 49
    m_ui->paramGuardCheckBox->setChecked(protocol->paramGuardEnabled());
    m_ui->paramRetransmissionSpinBox->setValue(protocol->getParamRetransmissionTimeout());
    m_ui->paramRewriteSpinBox->setValue(protocol->getParamRewriteTimeout());
lm's avatar
lm committed
50

51 52 53
    m_ui->actionGuardCheckBox->setChecked(protocol->actionGuardEnabled());
    m_ui->actionRetransmissionSpinBox->setValue(protocol->getActionRetransmissionTimeout());

54
    // Version check
55 56
    connect(protocol, &MAVLinkProtocol::versionCheckChanged, m_ui->versionCheckBox, &QCheckBox::setChecked);
    connect(m_ui->versionCheckBox, &QCheckBox::toggled, protocol, &MAVLinkProtocol::enableVersionCheck);
57
    // System ID
58 59
    connect(protocol, &MAVLinkProtocol::systemIdChanged, m_ui->systemIdSpinBox, &QSpinBox::setValue);
    connect(m_ui->systemIdSpinBox,static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), protocol, &MAVLinkProtocol::setSystemId);
60
    // Multiplexing
61
    connect(protocol, &MAVLinkProtocol::multiplexingChanged, m_ui->multiplexingCheckBox, &QCheckBox::setChecked);
62
    connect(m_ui->multiplexingCheckBox, &QCheckBox::toggled, protocol, &MAVLinkProtocol::enableMultiplexing);
63
    // Parameter guard
64 65 66 67 68 69
    connect(protocol, &MAVLinkProtocol::paramGuardChanged, m_ui->paramGuardCheckBox, &QCheckBox::setChecked);
    connect(m_ui->paramGuardCheckBox, &QCheckBox::toggled, protocol, &MAVLinkProtocol::enableParamGuard);
    connect(protocol, &MAVLinkProtocol::paramRetransmissionTimeoutChanged, m_ui->paramRetransmissionSpinBox, &QSpinBox::setValue);
    connect(m_ui->paramRetransmissionSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), protocol, &MAVLinkProtocol::setParamRetransmissionTimeout);
    connect(protocol, &MAVLinkProtocol::paramRewriteTimeoutChanged, m_ui->paramRewriteSpinBox, &QSpinBox::setValue);
    connect(m_ui->paramRewriteSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), protocol, &MAVLinkProtocol::setParamRewriteTimeout);
70
    // Action guard
71 72 73 74
    connect(protocol, &MAVLinkProtocol::actionGuardChanged, m_ui->actionGuardCheckBox, &QCheckBox::setChecked);
    connect(m_ui->actionGuardCheckBox, &QCheckBox::toggled, protocol, &MAVLinkProtocol::enableActionGuard);
    connect(protocol, &MAVLinkProtocol::actionRetransmissionTimeoutChanged, m_ui->actionRetransmissionSpinBox, &QSpinBox::setValue);
    connect(m_ui->actionRetransmissionSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), protocol, &MAVLinkProtocol::setActionRetransmissionTimeout);
75
    // MAVLink AUTH
76 77 78 79
    connect(protocol, &MAVLinkProtocol::authChanged, m_ui->droneOSCheckBox, &QCheckBox::setChecked);
    connect(m_ui->droneOSCheckBox, &QCheckBox::toggled, this, &MAVLinkSettingsWidget::enableDroneOS);
    connect(protocol, &MAVLinkProtocol::authKeyChanged, m_ui->droneOSLineEdit, &QLineEdit::setText);
    connect(m_ui->droneOSLineEdit, &QLineEdit::textChanged, this, &MAVLinkSettingsWidget::setDroneOSKey);
80

81
    // Drone OS
82
    connect(m_ui->droneOSComboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this, &MAVLinkSettingsWidget::setDroneOSHost);
83 84
    // FIXME Manually trigger this action here, this brings control code to UI = BAD!
    setDroneOSHost(m_ui->droneOSComboBox->currentText());
85 86 87 88 89

    // Update values
    m_ui->versionLabel->setText(tr("MAVLINK_VERSION: %1").arg(protocol->getVersion()));

    // Connect visibility updates
90
    connect(protocol, &MAVLinkProtocol::versionCheckChanged, m_ui->versionLabel, &QWidget::setVisible);
91
    m_ui->versionLabel->setVisible(protocol->versionCheckEnabled());
92

93 94 95 96 97
//    // Multiplexing visibility
//    connect(protocol, SIGNAL(multiplexingChanged(bool)), m_ui->multiplexingFilterCheckBox, SLOT(setVisible(bool)));
//    m_ui->multiplexingFilterCheckBox->setVisible(protocol->multiplexingEnabled());
//    connect(protocol, SIGNAL(multiplexingChanged(bool)), m_ui->multiplexingFilterLineEdit, SLOT(setVisible(bool)));
//    m_ui->multiplexingFilterLineEdit->setVisible(protocol->multiplexingEnabled());
98

99
    // Param guard visibility
100
    connect(protocol, &MAVLinkProtocol::paramGuardChanged, m_ui->paramRetransmissionSpinBox, &QWidget::setVisible);
101
    m_ui->paramRetransmissionSpinBox->setVisible(protocol->paramGuardEnabled());
102
    connect(protocol, &MAVLinkProtocol::paramGuardChanged, m_ui->paramRetransmissionLabel, &QWidget::setVisible);
103
    m_ui->paramRetransmissionLabel->setVisible(protocol->paramGuardEnabled());
104
    connect(protocol, &MAVLinkProtocol::paramGuardChanged, m_ui->paramRewriteSpinBox, &QWidget::setVisible);
105
    m_ui->paramRewriteSpinBox->setVisible(protocol->paramGuardEnabled());
106
    connect(protocol, &MAVLinkProtocol::paramGuardChanged, m_ui->paramRewriteLabel, &QWidget::setVisible);
107
    m_ui->paramRewriteLabel->setVisible(protocol->paramGuardEnabled());
108
    // Action guard visibility
109
    connect(protocol, &MAVLinkProtocol::actionGuardChanged, m_ui->actionRetransmissionSpinBox, &QWidget::setVisible);
110
    m_ui->actionRetransmissionSpinBox->setVisible(protocol->actionGuardEnabled());
111
    connect(protocol, &MAVLinkProtocol::actionGuardChanged, m_ui->actionRetransmissionLabel, &QWidget::setVisible);
112
    m_ui->actionRetransmissionLabel->setVisible(protocol->actionGuardEnabled());
113 114 115 116 117

    // TODO implement filtering
    // and then remove these two lines
    m_ui->multiplexingFilterCheckBox->setVisible(false);
    m_ui->multiplexingFilterLineEdit->setVisible(false);
118
}
pixhawk's avatar
pixhawk committed
119

120 121
void MAVLinkSettingsWidget::enableDroneOS(bool enable)
{
Lorenz Meier's avatar
Lorenz Meier committed
122 123
    // Enable multiplexing
    protocol->enableMultiplexing(enable);
124 125
    // Get current selected host and port
    QString hostString = m_ui->droneOSComboBox->currentText();
Lorenz Meier's avatar
Lorenz Meier committed
126
    //QString host = hostString.split(":").first();
127

Don Gagne's avatar
Don Gagne committed
128 129 130
    LinkManager*    linkMgr = qgcApp()->toolbox()->linkManager();
    UDPLink*        firstUdp = NULL;

131
    // Delete from all lists first
Don Gagne's avatar
Don Gagne committed
132 133 134 135
    for (int i=0; i<linkMgr->links()->count(); i++) {
        LinkInterface*  link = linkMgr->links()->value<LinkInterface*>(i);
        UDPLink*        udp = qobject_cast<UDPLink*>(link);

136 137
        if (udp)
        {
138 139
            if (!firstUdp) firstUdp = udp;
            // Remove current hosts
140 141
            for (int i = 0; i < m_ui->droneOSComboBox->count(); ++i)
            {
142 143 144 145 146 147 148 149
                QString oldHostString = m_ui->droneOSComboBox->itemText(i);
                oldHostString = hostString.split(":").first();
                udp->removeHost(oldHostString);
            }
        }
    }

    // Re-add if enabled
150 151 152 153
    if (enable)
    {
        if (firstUdp)
        {
154 155 156 157 158
            firstUdp->addHost(hostString);
        }
        // Set key
        protocol->setAuthKey(m_ui->droneOSLineEdit->text().trimmed());
        QSettings settings;
159
        settings.setValue("DRONELINK_HOST", m_ui->droneOSComboBox->currentText());
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }
    protocol->enableAuth(enable);
}

void MAVLinkSettingsWidget::setDroneOSKey(QString key)
{
    Q_UNUSED(key);
    enableDroneOS(m_ui->droneOSCheckBox->isChecked());
}

void MAVLinkSettingsWidget::setDroneOSHost(QString host)
{
    Q_UNUSED(host);
    enableDroneOS(m_ui->droneOSCheckBox->isChecked());
}

pixhawk's avatar
pixhawk committed
176 177 178 179 180 181 182 183
MAVLinkSettingsWidget::~MAVLinkSettingsWidget()
{
    delete m_ui;
}

void MAVLinkSettingsWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
184
    switch (e->type()) {
pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
192 193 194 195 196 197

void MAVLinkSettingsWidget::hideEvent(QHideEvent* event)
{
    Q_UNUSED(event);
    protocol->storeSettings();
}