CommConfigurationWindow.cc 8.24 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

lm's avatar
lm committed
3
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
4

lm's avatar
lm committed
5
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
6

lm's avatar
lm committed
7
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed
8

lm's avatar
lm committed
9
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12 13
    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.

lm's avatar
lm committed
14
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17 18 19
    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
lm's avatar
lm committed
20
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
21 22 23 24 25

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

/**
 * @file
lm's avatar
lm committed
26
 *   @brief Implementation of CommConfigurationWindow
pixhawk's avatar
pixhawk committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QDebug>

#include <QDir>
#include <QFileInfoList>
#include <QBoxLayout>
#include <QWidget>

#include "CommConfigurationWindow.h"
#include "SerialConfigurationWindow.h"
41
#include "SerialLink.h"
pixhawk's avatar
pixhawk committed
42
#include "UDPLink.h"
43 44 45
#include "MAVLinkSimulationLink.h"
#ifdef OPAL_RT
#include "OpalLink.h"
46
#include "OpalLinkConfigurationWindow.h"
47
#endif
pixhawk's avatar
pixhawk committed
48 49
#include "MAVLinkProtocol.h"
#include "MAVLinkSettingsWidget.h"
50
#include "QGCUDPLinkConfiguration.h"
51
#include "LinkManager.h"
pixhawk's avatar
pixhawk committed
52 53 54 55 56 57 58 59

CommConfigurationWindow::CommConfigurationWindow(LinkInterface* link, ProtocolInterface* protocol, QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags)
{
    this->link = link;

    // Setup the user interface according to link type
    ui.setupUi(this);

60
    // add link types
61 62 63 64 65 66
    ui.linkType->addItem(tr("Serial"), QGC_LINK_SERIAL);
    ui.linkType->addItem(tr("UDP"), QGC_LINK_UDP);
    ui.linkType->addItem(tr("Simulation"), QGC_LINK_SIMULATION);
    ui.linkType->addItem(tr("Opal-RT Link"), QGC_LINK_OPAL);
    ui.linkType->setEditable(false);
    //ui.linkType->setEnabled(false);
67 68

    ui.connectionType->addItem("MAVLink", QGC_PROTOCOL_MAVLINK);
69
    //ui.connectionType->addItem("GPS NMEA", QGC_PROTOCOL_NMEA);
70

pixhawk's avatar
pixhawk committed
71 72 73
    // Create action to open this menu
    // Create configuration action for this link
    // Connect the current UAS
74
    action = new QAction(QIcon(":/images/devices/network-wireless.svg"), "", this);
75 76
    LinkManager::instance()->add(link);
    action->setData(LinkManager::instance()->getLinks().indexOf(link));
77 78
    action->setEnabled(true);
    action->setVisible(true);
pixhawk's avatar
pixhawk committed
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
    setLinkName(link->getName());
    connect(action, SIGNAL(triggered()), this, SLOT(show()));

    // Make sure that a change in the link name will be reflected in the UI
    connect(link, SIGNAL(nameChanged(QString)), this, SLOT(setLinkName(QString)));

    // Setup user actions and link notifications
    connect(ui.connectButton, SIGNAL(clicked()), this, SLOT(setConnection()));
    connect(ui.closeButton, SIGNAL(clicked()), this->window(), SLOT(close()));
    connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(remove()));

    connect(this->link, SIGNAL(connected(bool)), this, SLOT(connectionState(bool)));

    // Fill in the current data
    if(this->link->isConnected()) ui.connectButton->setChecked(true);
    //connect(this->link, SIGNAL(connected(bool)), ui.connectButton, SLOT(setChecked(bool)));

    if(this->link->isConnected())
    {
        ui.connectionStatusLabel->setText(tr("Connected"));

        // TODO Deactivate all settings to force user to manually disconnect first
    }
    else
    {
        ui.connectionStatusLabel->setText(tr("Disconnected"));
    }

    // TODO Move these calls to each link so that dynamic casts vanish

    // Open details pane for serial link if necessary
110
    SerialLink* serial = dynamic_cast<SerialLink*>(link);
pixhawk's avatar
pixhawk committed
111 112 113
    if(serial != 0)
    {
        QWidget* conf = new SerialConfigurationWindow(serial, this);
lm's avatar
lm committed
114 115 116 117
        //QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, ui.linkGroupBox);
        //layout->addWidget(conf);
        ui.linkScrollArea->setWidget(conf);
        //ui.linkScrollArea->setLayout(layout);
118
        ui.linkGroupBox->setTitle(tr("Serial Link"));
119
        ui.linkType->setCurrentIndex(0);
pixhawk's avatar
pixhawk committed
120 121 122
        //ui.linkGroupBox->setTitle(link->getName());
        //connect(link, SIGNAL(nameChanged(QString)), ui.linkGroupBox, SLOT(setTitle(QString)));
    }
123 124
    UDPLink* udp = dynamic_cast<UDPLink*>(link);
    if (udp != 0)
pixhawk's avatar
pixhawk committed
125
    {
126
        QWidget* conf = new QGCUDPLinkConfiguration(udp, this);
lm's avatar
lm committed
127 128 129 130
        //QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, ui.linkGroupBox);
        //layout->addWidget(conf);
        //ui.linkGroupBox->setLayout(layout);
        ui.linkScrollArea->setWidget(conf);
131
        ui.linkGroupBox->setTitle(tr("UDP Link"));
132
        ui.linkType->setCurrentIndex(1);
pixhawk's avatar
pixhawk committed
133
    }
134 135 136
    MAVLinkSimulationLink* sim = dynamic_cast<MAVLinkSimulationLink*>(link);
    if (sim != 0)
    {
137
        ui.linkType->setCurrentIndex(2);
138 139
        ui.linkGroupBox->setTitle(tr("MAVLink Simulation Link"));
    }
140
#ifdef OPAL_RT
141 142
    OpalLink* opal = dynamic_cast<OpalLink*>(link);
    if (opal != 0)
143
    {
144 145 146 147
        QWidget* conf = new OpalLinkConfigurationWindow(opal, this);
        QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, ui.linkGroupBox);
        layout->addWidget(conf);
        ui.linkGroupBox->setLayout(layout);
148
        ui.linkType->setCurrentIndex(3);
149 150 151
        ui.linkGroupBox->setTitle(tr("Opal-RT Link"));
    }
#endif
152 153 154 155 156
    if (serial == 0 && udp == 0 && sim == 0
#ifdef OPAL_RT
        && opal == 0
#endif
        )
pixhawk's avatar
pixhawk committed
157 158 159 160
    {
        qDebug() << "Link is NOT a known link, can't open configuration window";
    }

161

pixhawk's avatar
pixhawk committed
162 163
    // Open details pane for MAVLink if necessary
    MAVLinkProtocol* mavlink = dynamic_cast<MAVLinkProtocol*>(protocol);
164
    if (mavlink != 0)
pixhawk's avatar
pixhawk committed
165 166
    {
        QWidget* conf = new MAVLinkSettingsWidget(mavlink, this);
lm's avatar
lm committed
167 168 169 170
        //QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, ui.protocolGroupBox);
        //layout->addWidget(conf);
        //ui.protocolGroupBox->setLayout(layout);
        ui.protocolScrollArea->setWidget(conf);
171
        ui.protocolGroupBox->setTitle(protocol->getName()+" (Global Settings)");
pixhawk's avatar
pixhawk committed
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
    }
    else
    {
        qDebug() << "Protocol is NOT MAVLink, can't open configuration window";
    }

    // Open details for UDP link if necessary
    // TODO

    // Display the widget
    this->window()->setWindowTitle(tr("Settings for ") + this->link->getName());
    this->hide();
}

CommConfigurationWindow::~CommConfigurationWindow() {

}

QAction* CommConfigurationWindow::getAction()
{
    return action;
}

void CommConfigurationWindow::setLinkType(int linktype)
{
    // Adjust the form layout per link type
lm's avatar
lm committed
198
    Q_UNUSED(linktype);
pixhawk's avatar
pixhawk committed
199 200
}

201 202 203 204 205
void CommConfigurationWindow::setProtocol(int protocol)
{
    qDebug() << "Changing to protocol" << protocol;
}

pixhawk's avatar
pixhawk committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219
void CommConfigurationWindow::setConnection()
{
    if(!link->isConnected())
    {
        link->connect();
    }
    else
    {
        link->disconnect();
    }
}

void CommConfigurationWindow::setLinkName(QString name)
{
220 221 222
    action->setText(tr("%1 Settings").arg(name));
    action->setStatusTip(tr("Adjust setting for link %1").arg(name));
    this->window()->setWindowTitle(tr("Settings for %1").arg(name));
pixhawk's avatar
pixhawk committed
223 224 225 226
}

void CommConfigurationWindow::remove()
{
227 228 229 230 231 232 233 234 235
    if(action) delete action; //delete action first since it has a pointer to link
    action=NULL;

    if(link)
    {
        LinkManager::instance()->removeLink(link); //remove link from LinkManager list
        link->disconnect(); //disconnect port, and also calls terminate() to stop the thread
        if (link->isRunning()) link->terminate(); // terminate() the serial thread just in case it is still running
        link->wait(); // wait() until thread is stoped before deleting
236
        link->deleteLater();
237 238 239
    }
    link=NULL;

pixhawk's avatar
pixhawk committed
240
    this->window()->close();
241
    this->deleteLater();
pixhawk's avatar
pixhawk committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
}

void CommConfigurationWindow::connectionState(bool connect)
{
    ui.connectButton->setChecked(connect);
    if(connect)
    {
        ui.connectionStatusLabel->setText(tr("Connected"));
        ui.connectButton->setText(tr("Disconnect"));
    }
    else
    {
        ui.connectionStatusLabel->setText(tr("Disconnected"));
        ui.connectButton->setText(tr("Connect"));
    }
}