Skip to content
CommConfigurationWindow.cc 11.9 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================

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

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

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

lm's avatar
lm committed
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
    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
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
    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
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed

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

/**
 * @file
lm's avatar
lm committed
 *   @brief Implementation of CommConfigurationWindow
pixhawk's avatar
pixhawk committed
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QDebug>

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

#include "CommConfigurationWindow.h"
#include "SerialConfigurationWindow.h"
pixhawk's avatar
pixhawk committed
#include "UDPLink.h"
Don Gagne's avatar
Don Gagne committed
#include "TCPLink.h"
#ifdef QGC_XBEE_ENABLED
#include "XbeeLink.h"
#include "XbeeConfigurationWindow.h"
#endif // QGC_XBEE_ENABLED
#ifdef QGC_RTLAB_ENABLED
#include "OpalLinkConfigurationWindow.h"
pixhawk's avatar
pixhawk committed
#include "MAVLinkProtocol.h"
#include "MAVLinkSettingsWidget.h"
#include "QGCUDPLinkConfiguration.h"
Don Gagne's avatar
Don Gagne committed
#include "QGCTCPLinkConfiguration.h"
#include "LinkManager.h"
oberion's avatar
oberion committed
#include "MainWindow.h"
pixhawk's avatar
pixhawk committed

CommConfigurationWindow::CommConfigurationWindow(LinkInterface* link, ProtocolInterface* protocol, QWidget *parent) : QDialog(parent)
pixhawk's avatar
pixhawk committed
{
    this->link = link;

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

    // Initialize basic ui state

    // Do not allow changes here unless advanced is checked
    ui.connectionType->setEnabled(false);
    ui.protocolGroupBox->setVisible(false);
    ui.protocolTypeGroupBox->setVisible(false);

    // Connect UI element visibility to checkbox
    //connect(ui.advancedOptionsCheckBox, SIGNAL(clicked(bool)), ui.connectionType, SLOT(setEnabled(bool)));
    //connect(ui.advancedOptionsCheckBox, SIGNAL(clicked(bool)), ui.linkType, SLOT(setEnabled(bool)));
    //connect(ui.advancedOptionsCheckBox, SIGNAL(clicked(bool)), ui.protocolGroupBox, SLOT(setVisible(bool)));
    ui.advancedOptionsCheckBox->setVisible(false);
    //connect(ui.advCheckBox,SIGNAL(clicked(bool)),ui.advancedOptionsCheckBox,SLOT(setChecked(bool)));
    connect(ui.advCheckBox,SIGNAL(clicked(bool)),ui.protocolTypeGroupBox,SLOT(setVisible(bool)));
    connect(ui.advCheckBox, SIGNAL(clicked(bool)), ui.connectionType, SLOT(setEnabled(bool)));
    connect(ui.advCheckBox, SIGNAL(clicked(bool)), ui.protocolGroupBox, SLOT(setVisible(bool)));
    // add link types
    ui.linkType->addItem(tr("Serial"), QGC_LINK_SERIAL);
    ui.linkType->addItem(tr("UDP"), QGC_LINK_UDP);
Don Gagne's avatar
Don Gagne committed
    ui.linkType->addItem(tr("TCP"), QGC_LINK_TCP);
    if(dynamic_cast<MAVLinkSimulationLink*>(link)) {
        //Only show simulation option if already setup elsewhere as a simulation
        ui.linkType->addItem(tr("Simulation"), QGC_LINK_SIMULATION);
    }

#ifdef QGC_RTLAB_ENABLED
    ui.linkType->addItem(tr("Opal-RT Link"), QGC_LINK_OPAL);
#ifdef QGC_XBEE_ENABLED
	ui.linkType->addItem(tr("Xbee API"),QGC_LINK_XBEE);
#endif // QGC_XBEE_ENABLED
    ui.linkType->setEditable(false);

    ui.connectionType->addItem("MAVLink", QGC_PROTOCOL_MAVLINK);
pixhawk's avatar
pixhawk committed
    // Create action to open this menu
    // Create configuration action for this link
    // Connect the current UAS
    action = new QAction(QIcon(":/files/images/devices/network-wireless.svg"), "", this);
    LinkManager::instance()->add(link);
oberion's avatar
oberion committed
	action->setData(link->getId());
    action->setEnabled(true);
    action->setVisible(true);
pixhawk's avatar
pixhawk committed
    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)));

pixhawk's avatar
pixhawk committed
    // 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()) {
pixhawk's avatar
pixhawk committed
        ui.connectionStatusLabel->setText(tr("Connected"));

        // TODO Deactivate all settings to force user to manually disconnect first
pixhawk's avatar
pixhawk committed
        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
    SerialLink* serial = dynamic_cast<SerialLink*>(link);
    if(serial != 0) {
pixhawk's avatar
pixhawk committed
        QWidget* conf = new SerialConfigurationWindow(serial, this);
lm's avatar
lm committed
        ui.linkScrollArea->setWidget(conf);
        ui.linkGroupBox->setTitle(tr("Serial Link"));
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_SERIAL));
pixhawk's avatar
pixhawk committed
    }
    UDPLink* udp = dynamic_cast<UDPLink*>(link);
    if (udp != 0) {
        QWidget* conf = new QGCUDPLinkConfiguration(udp, this);
lm's avatar
lm committed
        ui.linkScrollArea->setWidget(conf);
        ui.linkGroupBox->setTitle(tr("UDP Link"));
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_UDP));
pixhawk's avatar
pixhawk committed
    }
Don Gagne's avatar
Don Gagne committed
    TCPLink* tcp = dynamic_cast<TCPLink*>(link);
    if (tcp != 0) {
Don Gagne's avatar
Don Gagne committed
        QWidget* conf = new QGCTCPLinkConfiguration(tcp, this);
        ui.linkScrollArea->setWidget(conf);
Don Gagne's avatar
Don Gagne committed
        ui.linkGroupBox->setTitle(tr("TCP Link"));
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_TCP));
    }
    MAVLinkSimulationLink* sim = dynamic_cast<MAVLinkSimulationLink*>(link);
    if (sim != 0) {
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_SIMULATION));
        ui.linkType->setEnabled(false); //Don't allow the user to change to a non-simulation
        ui.linkGroupBox->setTitle(tr("MAVLink Simulation Link"));
    }
#ifdef QGC_RTLAB_ENABLED
    OpalLink* opal = dynamic_cast<OpalLink*>(link);
    if (opal != 0) {
        QWidget* conf = new OpalLinkConfigurationWindow(opal, this);
        QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, ui.linkGroupBox);
        layout->addWidget(conf);
        ui.linkGroupBox->setLayout(layout);
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_OPAL));
        ui.linkGroupBox->setTitle(tr("Opal-RT Link"));
    }
#endif
#ifdef QGC_XBEE_ENABLED
	XbeeLink* xbee = dynamic_cast<XbeeLink*>(link); // new Konrad
	if(xbee != 0)
	{
		QWidget* conf = new XbeeConfigurationWindow(xbee,this); 
		ui.linkScrollArea->setWidget(conf);
		ui.linkGroupBox->setTitle(tr("Xbee Link"));
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_XBEE));
oberion's avatar
oberion committed
		connect(xbee,SIGNAL(tryConnectBegin(bool)),ui.actionConnect,SLOT(setDisabled(bool)));
		connect(xbee,SIGNAL(tryConnectEnd(bool)),ui.actionConnect,SLOT(setEnabled(bool)));
#endif // QGC_XBEE_ENABLED
Don Gagne's avatar
Don Gagne committed
    if (serial == 0 && udp == 0 && sim == 0 && tcp == 0
#ifdef QGC_RTLAB_ENABLED
            && opal == 0
#ifdef QGC_XBEE_ENABLED
			&& xbee == 0
#endif // QGC_XBEE_ENABLED
pixhawk's avatar
pixhawk committed
        qDebug() << "Link is NOT a known link, can't open configuration window";
    }

    connect(ui.linkType,SIGNAL(currentIndexChanged(int)),this,SLOT(linkCurrentIndexChanged(int)));
pixhawk's avatar
pixhawk committed
    // Open details pane for MAVLink if necessary
    MAVLinkProtocol* mavlink = dynamic_cast<MAVLinkProtocol*>(protocol);
    if (mavlink != 0) {
pixhawk's avatar
pixhawk committed
        QWidget* conf = new MAVLinkSettingsWidget(mavlink, this);
lm's avatar
lm committed
        ui.protocolScrollArea->setWidget(conf);
        ui.protocolGroupBox->setTitle(protocol->getName()+" (Global Settings)");
pixhawk's avatar
pixhawk committed
        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()
{
pixhawk's avatar
pixhawk committed

}

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

void CommConfigurationWindow::linkCurrentIndexChanged(int currentIndex)
{
    setLinkType(static_cast<qgc_link_t>(ui.linkType->itemData(currentIndex).toInt()));
}

void CommConfigurationWindow::setLinkType(qgc_link_t linktype)
pixhawk's avatar
pixhawk committed
{
oberion's avatar
oberion committed
	if(link->isConnected())
	{
		// close old configuration window
		this->window()->close();
	}
	else
	{
		// delete old configuration window
		this->remove();
	}
oberion's avatar
oberion committed

oberion's avatar
oberion committed
	LinkInterface *tmpLink(NULL);
	switch(linktype)
	{
#ifdef QGC_XBEE_ENABLED
			{
				XbeeLink *xbee = new XbeeLink();
oberion's avatar
oberion committed
				tmpLink = xbee;
				MainWindow::instance()->addLink(tmpLink);
				break;
			}
#endif // QGC_XBEE_ENABLED
			{
				UDPLink *udp = new UDPLink();
				tmpLink = udp;
				MainWindow::instance()->addLink(tmpLink);
				break;
			}
oberion's avatar
oberion committed
			
Don Gagne's avatar
Don Gagne committed
        case QGC_LINK_TCP:
Don Gagne's avatar
Don Gagne committed
            {
Don Gagne's avatar
Don Gagne committed
            TCPLink *tcp = new TCPLink();
            tmpLink = tcp;
            MainWindow::instance()->addLink(tmpLink);
            break;
Don Gagne's avatar
Don Gagne committed
            }
Don Gagne's avatar
Don Gagne committed

#ifdef QGC_RTLAB_ENABLED
			{
				OpalLink* opal = new OpalLink();
				tmpLink = opal;
				MainWindow::instance()->addLink(tmpLink);
				break;
			}
#endif // QGC_RTLAB_ENABLED
		default:
oberion's avatar
oberion committed
			{
			{
				SerialLink *serial = new SerialLink();
				tmpLink = serial;
				MainWindow::instance()->addLink(tmpLink);
oberion's avatar
oberion committed
				break;
			}
oberion's avatar
oberion committed
	// trigger new window
oberion's avatar
oberion committed

	const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(tmpLink));
	const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId());

oberion's avatar
oberion committed
	QList<QAction*> actions = MainWindow::instance()->listLinkMenuActions();
	foreach (QAction* act, actions) 
	{
oberion's avatar
oberion committed
        if (act->data().toInt() == linkID) 
lm's avatar
lm committed
        {
oberion's avatar
oberion committed
            act->trigger();
            break;
        }
    }
pixhawk's avatar
pixhawk committed
}

void CommConfigurationWindow::setProtocol(int protocol)
{
    qDebug() << "Changing to protocol" << protocol;
}

pixhawk's avatar
pixhawk committed
void CommConfigurationWindow::setConnection()
{
    if(!link->isConnected()) {
pixhawk's avatar
pixhawk committed
        link->connect();
        QGC::SLEEP::msleep(100);
        if (link->isConnected())
            // Auto-close window on connect
            this->window()->close();
pixhawk's avatar
pixhawk committed
        link->disconnect();
    }
}

void CommConfigurationWindow::setLinkName(QString name)
{
    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
}

void CommConfigurationWindow::remove()
{
    if(action) delete action; //delete action first since it has a pointer to link
    action=NULL;

        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
pixhawk's avatar
pixhawk committed
    this->window()->close();
pixhawk's avatar
pixhawk committed
}

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