CommConfigurationWindow.cc 12.1 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"
Don Gagne's avatar
Don Gagne committed
43
#include "TCPLink.h"
44
#include "MAVLinkSimulationLink.h"
45 46 47
#ifdef UNITTEST_BUILD
#include "MockLink.h"
#endif
48
#ifdef QGC_XBEE_ENABLED
49 50
#include "XbeeLink.h"
#include "XbeeConfigurationWindow.h"
51
#endif // QGC_XBEE_ENABLED
52
#ifdef QGC_RTLAB_ENABLED
53
#include "OpalLink.h"
54
#include "OpalLinkConfigurationWindow.h"
55
#endif
pixhawk's avatar
pixhawk committed
56 57
#include "MAVLinkProtocol.h"
#include "MAVLinkSettingsWidget.h"
58
#include "QGCUDPLinkConfiguration.h"
Don Gagne's avatar
Don Gagne committed
59
#include "QGCTCPLinkConfiguration.h"
60
#include "LinkManager.h"
oberion's avatar
oberion committed
61
#include "MainWindow.h"
pixhawk's avatar
pixhawk committed
62

63
CommConfigurationWindow::CommConfigurationWindow(LinkInterface* link, QWidget *parent) : QDialog(parent)
pixhawk's avatar
pixhawk committed
64 65 66 67 68 69
{
    this->link = link;

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

70 71 72 73 74
    // Initialize basic ui state

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

    // Connect UI element visibility to checkbox
78 79 80 81 82 83 84 85
    //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)));
86

87
    // add link types
88 89
    ui.linkType->addItem(tr("Serial"), QGC_LINK_SERIAL);
    ui.linkType->addItem(tr("UDP"), QGC_LINK_UDP);
Don Gagne's avatar
Don Gagne committed
90
    ui.linkType->addItem(tr("TCP"), QGC_LINK_TCP);
91
    
92 93 94 95
    if(dynamic_cast<MAVLinkSimulationLink*>(link)) {
        //Only show simulation option if already setup elsewhere as a simulation
        ui.linkType->addItem(tr("Simulation"), QGC_LINK_SIMULATION);
    }
96 97 98 99
    
#ifdef UNITTEST_BUILD
    ui.linkType->addItem(tr("Mock"), QGC_LINK_MOCK);
#endif
100

101
#ifdef QGC_RTLAB_ENABLED
102
    ui.linkType->addItem(tr("Opal-RT Link"), QGC_LINK_OPAL);
103
#endif
104
#ifdef QGC_XBEE_ENABLED
105
	ui.linkType->addItem(tr("Xbee API"),QGC_LINK_XBEE);
106
#endif // QGC_XBEE_ENABLED
107
    ui.linkType->setEditable(false);
108 109

    ui.connectionType->addItem("MAVLink", QGC_PROTOCOL_MAVLINK);
110

pixhawk's avatar
pixhawk committed
111 112 113
    // Create action to open this menu
    // Create configuration action for this link
    // Connect the current UAS
114
    action = new QAction(QIcon(":/files/images/devices/network-wireless.svg"), "", this);
oberion's avatar
oberion committed
115
	action->setData(link->getId());
116 117
    action->setEnabled(true);
    action->setVisible(true);
pixhawk's avatar
pixhawk committed
118 119 120 121 122 123 124 125 126 127 128
    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()));

129 130
    connect(link, &LinkInterface::connected, this, &CommConfigurationWindow::_linkConnected);
    connect(link, &LinkInterface::disconnected, this, &CommConfigurationWindow::_linkDisconnected);
131

pixhawk's avatar
pixhawk committed
132 133 134
    // Fill in the current data
    if(this->link->isConnected()) ui.connectButton->setChecked(true);

135
    if(this->link->isConnected()) {
pixhawk's avatar
pixhawk committed
136 137 138
        ui.connectionStatusLabel->setText(tr("Connected"));

        // TODO Deactivate all settings to force user to manually disconnect first
139
    } else {
pixhawk's avatar
pixhawk committed
140 141 142 143 144 145
        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
146
    SerialLink* serial = dynamic_cast<SerialLink*>(link);
147
    if(serial != 0) {
pixhawk's avatar
pixhawk committed
148
        QWidget* conf = new SerialConfigurationWindow(serial, this);
lm's avatar
lm committed
149
        ui.linkScrollArea->setWidget(conf);
150
        ui.linkGroupBox->setTitle(tr("Serial Link"));
151
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_SERIAL));
pixhawk's avatar
pixhawk committed
152
    }
153
    
154
    UDPLink* udp = dynamic_cast<UDPLink*>(link);
155
    if (udp != 0) {
156
        QWidget* conf = new QGCUDPLinkConfiguration(udp, this);
lm's avatar
lm committed
157
        ui.linkScrollArea->setWidget(conf);
158
        ui.linkGroupBox->setTitle(tr("UDP Link"));
159
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_UDP));
pixhawk's avatar
pixhawk committed
160
    }
161
    
Don Gagne's avatar
Don Gagne committed
162 163
    TCPLink* tcp = dynamic_cast<TCPLink*>(link);
    if (tcp != 0) {
Don Gagne's avatar
Don Gagne committed
164 165
        QWidget* conf = new QGCTCPLinkConfiguration(tcp, this);
        ui.linkScrollArea->setWidget(conf);
Don Gagne's avatar
Don Gagne committed
166 167 168
        ui.linkGroupBox->setTitle(tr("TCP Link"));
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_TCP));
    }
169
    
170
    MAVLinkSimulationLink* sim = dynamic_cast<MAVLinkSimulationLink*>(link);
171
    if (sim != 0) {
172 173
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_SIMULATION));
        ui.linkType->setEnabled(false); //Don't allow the user to change to a non-simulation
174 175
        ui.linkGroupBox->setTitle(tr("MAVLink Simulation Link"));
    }
176 177 178 179 180 181 182 183 184
    
#ifdef UNITTEST_BUILD
    MockLink* mock = dynamic_cast<MockLink*>(link);
    if (mock != 0) {
        ui.linkGroupBox->setTitle(tr("Mock Link"));
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_MOCK));
    }
#endif
    
185
#ifdef QGC_RTLAB_ENABLED
186
    OpalLink* opal = dynamic_cast<OpalLink*>(link);
187
    if (opal != 0) {
188 189 190 191
        QWidget* conf = new OpalLinkConfigurationWindow(opal, this);
        QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, ui.linkGroupBox);
        layout->addWidget(conf);
        ui.linkGroupBox->setLayout(layout);
192
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_OPAL));
193 194 195
        ui.linkGroupBox->setTitle(tr("Opal-RT Link"));
    }
#endif
196
    
197
#ifdef QGC_XBEE_ENABLED
198 199 200 201 202 203
	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"));
204
        ui.linkType->setCurrentIndex(ui.linkType->findData(QGC_LINK_XBEE));
oberion's avatar
oberion committed
205 206
		connect(xbee,SIGNAL(tryConnectBegin(bool)),ui.actionConnect,SLOT(setDisabled(bool)));
		connect(xbee,SIGNAL(tryConnectEnd(bool)),ui.actionConnect,SLOT(setEnabled(bool)));
207
	}
208
#endif // QGC_XBEE_ENABLED
209
    
Don Gagne's avatar
Don Gagne committed
210
    if (serial == 0 && udp == 0 && sim == 0 && tcp == 0
211 212 213
#ifdef UNITTEST_BUILD
            && mock == 0
#endif
214
#ifdef QGC_RTLAB_ENABLED
215
            && opal == 0
216
#endif
217
#ifdef QGC_XBEE_ENABLED
218
			&& xbee == 0
219
#endif // QGC_XBEE_ENABLED
220
       ) {
pixhawk's avatar
pixhawk committed
221 222 223
        qDebug() << "Link is NOT a known link, can't open configuration window";
    }

224
    connect(ui.linkType,SIGNAL(currentIndexChanged(int)),this,SLOT(linkCurrentIndexChanged(int)));
225

pixhawk's avatar
pixhawk committed
226
    // Open details pane for MAVLink if necessary
227
    MAVLinkProtocol* mavlink = MAVLinkProtocol::instance();
228 229 230
    QWidget* conf = new MAVLinkSettingsWidget(mavlink, this);
    ui.protocolScrollArea->setWidget(conf);
    ui.protocolGroupBox->setTitle(mavlink->getName()+" (Global Settings)");
pixhawk's avatar
pixhawk committed
231 232 233 234 235 236 237 238 239

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

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

240 241
CommConfigurationWindow::~CommConfigurationWindow()
{
pixhawk's avatar
pixhawk committed
242 243 244 245 246 247 248 249

}

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

250 251 252 253 254 255
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
256
{
oberion's avatar
oberion committed
257 258 259 260 261 262 263 264 265 266
	if(link->isConnected())
	{
		// close old configuration window
		this->window()->close();
	}
	else
	{
		// delete old configuration window
		this->remove();
	}
oberion's avatar
oberion committed
267

oberion's avatar
oberion committed
268
	LinkInterface *tmpLink(NULL);
269 270
	switch(linktype)
	{
271
#ifdef QGC_XBEE_ENABLED
272
        case QGC_LINK_XBEE:
273 274
			{
				XbeeLink *xbee = new XbeeLink();
oberion's avatar
oberion committed
275
				tmpLink = xbee;
276 277
				break;
			}
278
#endif // QGC_XBEE_ENABLED
279
            
280
        case QGC_LINK_UDP:
281 282 283 284 285
			{
				UDPLink *udp = new UDPLink();
				tmpLink = udp;
				break;
			}
oberion's avatar
oberion committed
286
			
Don Gagne's avatar
Don Gagne committed
287
        case QGC_LINK_TCP:
Don Gagne's avatar
Don Gagne committed
288
            {
Don Gagne's avatar
Don Gagne committed
289 290 291
            TCPLink *tcp = new TCPLink();
            tmpLink = tcp;
            break;
Don Gagne's avatar
Don Gagne committed
292
            }
Don Gagne's avatar
Don Gagne committed
293

294
#ifdef QGC_RTLAB_ENABLED
295
        case QGC_LINK_OPAL:
296 297 298 299 300
			{
				OpalLink* opal = new OpalLink();
				tmpLink = opal;
				break;
			}
301
#endif // QGC_RTLAB_ENABLED
302 303 304 305 306 307 308 309 310 311
            
#ifdef UNITTEST_BUILD
        case QGC_LINK_MOCK:
        {
            MockLink* mock = new MockLink;
            tmpLink = mock;
            break;
        }
#endif
            
312
		default:
313
        case QGC_LINK_SERIAL:
314 315 316
			{
				SerialLink *serial = new SerialLink();
				tmpLink = serial;
oberion's avatar
oberion committed
317 318
				break;
			}
319
	}
320 321 322 323
    
    if (tmpLink) {
        LinkManager::instance()->addLink(tmpLink);
    }
oberion's avatar
oberion committed
324
	// trigger new window
oberion's avatar
oberion committed
325 326 327 328

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

oberion's avatar
oberion committed
329 330 331
	QList<QAction*> actions = MainWindow::instance()->listLinkMenuActions();
	foreach (QAction* act, actions) 
	{
oberion's avatar
oberion committed
332
        if (act->data().toInt() == linkID) 
lm's avatar
lm committed
333
        {
oberion's avatar
oberion committed
334 335 336 337
            act->trigger();
            break;
        }
    }
pixhawk's avatar
pixhawk committed
338 339
}

340 341 342 343 344
void CommConfigurationWindow::setProtocol(int protocol)
{
    qDebug() << "Changing to protocol" << protocol;
}

pixhawk's avatar
pixhawk committed
345 346
void CommConfigurationWindow::setConnection()
{
347
    if(!link->isConnected()) {
348
        LinkManager::instance()->connectLink(link);
349 350 351 352
        QGC::SLEEP::msleep(100);
        if (link->isConnected())
            // Auto-close window on connect
            this->window()->close();
353
    } else {
354
        LinkManager::instance()->disconnectLink(link);
pixhawk's avatar
pixhawk committed
355 356 357 358 359
    }
}

void CommConfigurationWindow::setLinkName(QString name)
{
360 361 362
    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
363 364 365 366
}

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

370
    if(link) {
371
        link->deleteLater();
372 373 374
    }
    link=NULL;

pixhawk's avatar
pixhawk committed
375
    this->window()->close();
376
    this->deleteLater();
pixhawk's avatar
pixhawk committed
377 378
}

379 380 381 382 383 384 385 386 387
void CommConfigurationWindow::_linkConnected(void) {
    _connectionState(true);
}

void CommConfigurationWindow::_linkDisconnected(void) {
    _connectionState(false);
}

void CommConfigurationWindow::_connectionState(bool connect)
pixhawk's avatar
pixhawk committed
388 389
{
    ui.connectButton->setChecked(connect);
390
    if(connect) {
pixhawk's avatar
pixhawk committed
391 392
        ui.connectionStatusLabel->setText(tr("Connected"));
        ui.connectButton->setText(tr("Disconnect"));
393
    } else {
pixhawk's avatar
pixhawk committed
394 395 396 397
        ui.connectionStatusLabel->setText(tr("Disconnected"));
        ui.connectButton->setText(tr("Connect"));
    }
}