MainWindow.cc 15.8 KB
Newer Older
pixhawk's avatar
pixhawk committed
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK 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.

    PIXHAWK 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 PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

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

/**
 * @file
 *   @brief Implementation of main application window
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QSettings>
#include <QDockWidget>
#include <QNetworkInterface>
#include <QMessageBox>
#include <QDebug>
#include <QTimer>
#include <QHostInfo>

#include "MG.h"
#include "MAVLinkSimulationLink.h"
#include "SerialLink.h"
#include "UDPLink.h"
#include "MAVLinkProtocol.h"
#include "CommConfigurationWindow.h"
#include "WaypointList.h"
#include "MainWindow.h"
#include "JoystickWidget.h"
pixhawk's avatar
pixhawk committed
48
#include "GAudioOutput.h"
pixhawk's avatar
pixhawk committed
49 50 51 52 53 54 55 56 57 58 59


#include "LogCompressor.h"

/**
* Create new mainwindow. The constructor instantiates all parts of the user
* interface. It does NOT show the mainwindow. To display it, call the show()
* method.
*
* @see QMainWindow::show()
**/
pixhawk's avatar
pixhawk committed
60
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
pixhawk's avatar
pixhawk committed
61 62 63 64 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
{

    // Quick hack
    //comp = new LogCompressor("/home/pixhawk/Desktop/test.txt");

    mavlink = new MAVLinkProtocol();
    //as4link = new AS4Protocol();

    //    MG::DISPLAY::setPixelSize(0.224f);

    // Setup user interface
    ui.setupUi(this);

    // Initialize views, NOT show them yet, only initialize model and controller
    centerStack = new QStackedWidget(this);
    linechart = new LinechartWidget(this);
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), linechart, SLOT(setActivePlot(UASInterface*)));
    centerStack->addWidget(linechart);
    control = new UASControlWidget(this);
    //controlDock = new QDockWidget(this);
    //controlDock->setWidget(control);
    list = new UASListWidget(this);
    list->setVisible(false);
    waypoints = new WaypointList(this, NULL);
    waypoints->setVisible(false);
    info = new UASInfoWidget(this);
    info->setVisible(false);
    detection = new ObjectDetectionView("test", this);
    detection->setVisible(false);
    hud = new HUD(640, 480, this);
    hud->setVisible(false);
    debugConsole = new DebugConsole(this);
    debugConsole->setVisible(false);
    map = new MapWidget(this);
    map->setVisible(false);
    protocol = new XMLCommProtocolWidget(this);
    protocol->setVisible(false);
    parameters = new ParameterInterface(this);
    parameters->setVisible(false);
pixhawk's avatar
pixhawk committed
100 101 102 103 104 105

    QStringList* acceptList = new QStringList();
    acceptList->append("roll IMU");
    acceptList->append("pitch IMU");
    acceptList->append("yaw IMU");
    headDown1 = new HDDisplay(acceptList, this);
pixhawk's avatar
pixhawk committed
106
    headDown1->setVisible(false);
pixhawk's avatar
pixhawk committed
107 108 109 110 111 112

    QStringList* acceptList2 = new QStringList();
    acceptList2->append("Battery");
    acceptList2->append("Pressure");
    headDown2 = new HDDisplay(acceptList2, this);
    headDown2->setVisible(false);
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118 119
    centerStack->addWidget(map);
    centerStack->addWidget(hud);
    setCentralWidget(centerStack);

    // Get IPs
    QList<QHostAddress> hostAddresses = QNetworkInterface::allAddresses();

pixhawk's avatar
pixhawk committed
120 121
    QString windowname = qApp->applicationName() + " " + qApp->applicationVersion();
    /*
pixhawk's avatar
pixhawk committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    windowname.append(" (" + QHostInfo::localHostName() + ": ");
    bool prevAddr = false;
    for (int i = 0; i < hostAddresses.size(); i++)
    {
        // Exclude loopback IPv4 and all IPv6 addresses
        if (hostAddresses.at(i) != QHostAddress("127.0.0.1") && !hostAddresses.at(i).toString().contains(":"))
        {
            if(prevAddr) windowname.append("/");
            windowname.append(hostAddresses.at(i).toString());
            prevAddr = true;
        }
    }

    windowname.append(")");
pixhawk's avatar
pixhawk committed
136
    */
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    setWindowTitle(windowname);
#ifndef Q_WS_MAC
    //qApp->setWindowIcon(QIcon(":/core/images/qtcreator_logo_128.png"));
#endif

    // Add status bar
    setStatusBar(createStatusBar());
    // Load widgets
    loadWidgets();
    // Adjust the size
    adjustSize();

    // Create actions
    connectActions();

    // Set the application style (not the same as a style sheet)
    // Set the style to Plastique
    qApp->setStyle("plastique");
    // Set style sheet as last step
    reloadStylesheet();

    joystick = new JoystickInput();

    // HAS TO BE THE LAST ACTION
    // to make sure that all components are initialized when the
    // first messages arrive
    udpLink = new UDPLink(QHostAddress::Any, 14550);
    LinkManager::instance()->addProtocol(udpLink, mavlink);
    CommConfigurationWindow* commWidget = new CommConfigurationWindow(udpLink, mavlink, this);
    ui.menuNetwork->addAction(commWidget->getAction());
    udpLink->connect();

pixhawk's avatar
pixhawk committed
170
    simulationLink = new MAVLinkSimulationLink(MG::DIR::getSupportFilesDirectory() + "/demo-log.txt");
pixhawk's avatar
pixhawk committed
171
    //connect(simulationLink, SIGNAL(valueChanged(int,QString,double,quint64)), linechart, SLOT(appendData(int,QString,double,quint64)));
pixhawk's avatar
pixhawk committed
172 173 174
    LinkManager::instance()->addProtocol(simulationLink, mavlink);
    //CommConfigurationWindow* simulationWidget = new CommConfigurationWindow(simulationLink, mavlink, this);
    //ui.menuNetwork->addAction(commWidget->getAction());
175
    simulationLink->connect();
pixhawk's avatar
pixhawk committed
176 177
}

pixhawk's avatar
pixhawk committed
178
MainWindow::~MainWindow()
pixhawk's avatar
pixhawk committed
179 180 181 182 183
{
    delete statusBar;
    statusBar = NULL;
}

pixhawk's avatar
pixhawk committed
184
QStatusBar* MainWindow::createStatusBar()
pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191 192
{
    QStatusBar* bar = new QStatusBar();
    /* Add status fields and messages */
    /* Enable resize grip in the bottom right corner */
    bar->setSizeGripEnabled(true);
    return bar;
}

pixhawk's avatar
pixhawk committed
193
void MainWindow::startVideoCapture()
pixhawk's avatar
pixhawk committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
{
    QString format = "bmp";
    QString initialPath = QDir::currentPath() + tr("/untitled.") + format;

    QString screenFileName = QFileDialog::getSaveFileName(this, tr("Save As"),
                                                          initialPath,
                                                          tr("%1 Files (*.%2);;All Files (*)")
                                                          .arg(format.toUpper())
                                                          .arg(format));
    delete videoTimer;
    videoTimer = new QTimer(this);
    videoTimer->setInterval(40);
    connect(videoTimer, SIGNAL(timeout()), this, SLOT(saveScreen()));
}

pixhawk's avatar
pixhawk committed
209
void MainWindow::stopVideoCapture()
pixhawk's avatar
pixhawk committed
210 211 212 213 214 215
{
    videoTimer->stop();

    // TODO Convert raw images to PNG
}

pixhawk's avatar
pixhawk committed
216
void MainWindow::saveScreen()
pixhawk's avatar
pixhawk committed
217 218 219 220 221 222 223 224 225 226
{
    QPixmap window = QPixmap::grabWindow(this->winId());
    QString format = "bmp";

    if (!screenFileName.isEmpty())
    {
        window.save(screenFileName, format.toAscii());
    }
}

pixhawk's avatar
pixhawk committed
227
void MainWindow::reloadStylesheet()
pixhawk's avatar
pixhawk committed
228 229 230 231 232 233 234 235 236 237 238 239 240
{
    // Load style sheet
    //QFile styleSheet(MG::DIR::getSupportFilesDirectory() + "/images/style-mission.css");
    QFile styleSheet(":/images/style-mission.css");
    if (styleSheet.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QString style = QString(styleSheet.readAll());
        style.replace("ICONDIR", MG::DIR::getIconDirectory());
        qApp->setStyleSheet(style);
    } else {
        qDebug() << "Style not set:" << styleSheet.fileName() << "opened: " << styleSheet.isOpen();
    }
}

pixhawk's avatar
pixhawk committed
241
void MainWindow::showStatusMessage(const QString& status, int timeout)
pixhawk's avatar
pixhawk committed
242 243 244 245
{
    statusBar->showMessage(status, timeout);
}

pixhawk's avatar
pixhawk committed
246
void MainWindow::setLastAction(QString status)
pixhawk's avatar
pixhawk committed
247 248 249 250
{
    showStatusMessage(status, 5);
}

pixhawk's avatar
pixhawk committed
251
void MainWindow::setLinkStatus(QString status)
pixhawk's avatar
pixhawk committed
252 253 254 255 256 257 258 259
{
    showStatusMessage(status, 15);
}

/**
* @brief Create all actions associated to the main window
*
**/
pixhawk's avatar
pixhawk committed
260
void MainWindow::connectActions()
pixhawk's avatar
pixhawk committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
{
    // Connect actions from ui
    connect(ui.actionAdd_Link, SIGNAL(triggered()), this, SLOT(addLink()));

    // Connect internal actions
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(UASCreated(UASInterface*)));

    // Connect user interface controls
    connect(ui.actionLiftoff, SIGNAL(triggered()), UASManager::instance(), SLOT(launchActiveUAS()));
    connect(ui.actionLand, SIGNAL(triggered()), UASManager::instance(), SLOT(returnActiveUAS()));
    connect(ui.actionEmergency_Land, SIGNAL(triggered()), UASManager::instance(), SLOT(stopActiveUAS()));
    connect(ui.actionEmergency_Kill, SIGNAL(triggered()), UASManager::instance(), SLOT(killActiveUAS()));

    connect(ui.actionConfiguration, SIGNAL(triggered()), UASManager::instance(), SLOT(configureActiveUAS()));

    // User interface actions
    connect(ui.actionPilotView, SIGNAL(triggered()), this, SLOT(loadPilotView()));
    connect(ui.actionEngineerView, SIGNAL(triggered()), this, SLOT(loadEngineerView()));
    connect(ui.actionOperatorView, SIGNAL(triggered()), this, SLOT(loadOperatorView()));
    connect(ui.actionSettingsView, SIGNAL(triggered()), this, SLOT(loadSettingsView()));
    connect(ui.actionStyleConfig, SIGNAL(triggered()), this, SLOT(reloadStylesheet()));

    // Joystick configuration
    connect(ui.actionJoystickSettings, SIGNAL(triggered()), this, SLOT(configure()));
}

pixhawk's avatar
pixhawk committed
287
void MainWindow::configure()
pixhawk's avatar
pixhawk committed
288 289 290 291
{
    joystickWidget = new JoystickWidget(joystick, this);
}

pixhawk's avatar
pixhawk committed
292
void MainWindow::addLink()
pixhawk's avatar
pixhawk committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
{
    SerialLink* link = new SerialLink();
    // TODO This should be only done in the dialog itself

    LinkManager::instance()->addProtocol(link, mavlink);

    CommConfigurationWindow* commWidget = new CommConfigurationWindow(link, mavlink, this);

    ui.menuNetwork->addAction(commWidget->getAction());

    commWidget->show();

    // TODO Implement the link removal!
}

pixhawk's avatar
pixhawk committed
308
void MainWindow::UASCreated(UASInterface* uas)
pixhawk's avatar
pixhawk committed
309 310
{
    // Connect the UAS to the full user interface
pixhawk's avatar
pixhawk committed
311
    ui.menuConnected_Systems->addAction(QIcon(":/images/mavs/generic.svg"), tr("View ") + uas->getUASName(), uas, SLOT(setSelected()));
pixhawk's avatar
pixhawk committed
312 313

    // Line chart
pixhawk's avatar
pixhawk committed
314 315 316 317 318 319 320
    // FIXME DO THIS ONLY FOR THE FIRST CONNECTED SYSTEM
    static bool sysPresent = false;
    if (!sysPresent)
    {
        connect(uas, SIGNAL(valueChanged(int,QString,double,quint64)), linechart, SLOT(appendData(int,QString,double,quint64)), Qt::QueuedConnection);
        sysPresent = true;
    }
pixhawk's avatar
pixhawk committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

    // Health / System status indicator
    info->addUAS(uas);

    // UAS List
    list->addUAS(uas);

    // Camera view
    //camera->addUAS(uas);

    // Revalidate UI
    // TODO Stylesheet reloading should in theory not be necessary
    reloadStylesheet();
}

pixhawk's avatar
pixhawk committed
336
void MainWindow::clearView()
pixhawk's avatar
pixhawk committed
337 338 339
{ 
    // Halt HUD
    hud->stop();
pixhawk's avatar
pixhawk committed
340 341
    headDown1->stop();
    headDown2->stop();
pixhawk's avatar
pixhawk committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    // Remove all dock widgets
    QList<QObject*> list = this->children();

    QList<QObject*>::iterator i;
    for (i = list.begin(); i != list.end(); ++i)
    {
        QDockWidget* widget = dynamic_cast<QDockWidget*>(*i);
        if (widget)
        {
            // Hide widgets
            QWidget* childWidget = dynamic_cast<QWidget*>(widget->widget());
            if (childWidget) childWidget->setVisible(false);
            // Remove dock widget
            this->removeDockWidget(widget);
            //delete widget;
        }
    }
}

pixhawk's avatar
pixhawk committed
362
void MainWindow::loadPilotView()
pixhawk's avatar
pixhawk committed
363 364
{
    clearView();
pixhawk's avatar
pixhawk committed
365
    GAudioOutput::instance()->say("Switched to Pilot View");
pixhawk's avatar
pixhawk committed
366 367 368 369 370 371

    // HEAD UP DISPLAY
    centerStack->setCurrentWidget(hud);
    hud->start();

    //connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), pfd, SLOT(setActiveUAS(UASInterface*)));
pixhawk's avatar
pixhawk committed
372
    QDockWidget* container1 = new QDockWidget(tr("Primary Flight Display"), this);
pixhawk's avatar
pixhawk committed
373 374 375
    container1->setWidget(headDown1);
    addDockWidget(Qt::RightDockWidgetArea, container1);

pixhawk's avatar
pixhawk committed
376
    QDockWidget* container2 = new QDockWidget(tr("Payload Status"), this);
pixhawk's avatar
pixhawk committed
377 378
    container2->setWidget(headDown2);
    addDockWidget(Qt::RightDockWidgetArea, container2);
pixhawk's avatar
pixhawk committed
379 380 381 382

    headDown1->start();
    headDown2->start();

pixhawk's avatar
pixhawk committed
383 384 385 386

    this->show();
}

pixhawk's avatar
pixhawk committed
387
void MainWindow::loadOperatorView()
pixhawk's avatar
pixhawk committed
388 389 390
{
    clearView();

pixhawk's avatar
pixhawk committed
391 392
    GAudioOutput::instance()->say("Switched to Operator View");

pixhawk's avatar
pixhawk committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    // LINE CHART
    centerStack->setCurrentWidget(map);

    // UAS CONTROL
    QDockWidget* container1 = new QDockWidget(tr("Control"), this);
    container1->setWidget(control);
    addDockWidget(Qt::LeftDockWidgetArea, container1);

    // UAS LIST
    QDockWidget* container4 = new QDockWidget(tr("Unmanned Systems"), this);
    container4->setWidget(list);
    addDockWidget(Qt::BottomDockWidgetArea, container4);

    // UAS STATUS
    QDockWidget* container3 = new QDockWidget(tr("Status Details"), this);
    container3->setWidget(info);
    addDockWidget(Qt::LeftDockWidgetArea, container3);

    // WAYPOINT LIST
    QDockWidget* container5 = new QDockWidget(tr("Waypoint List"), this);
    container5->setWidget(waypoints);
    addDockWidget(Qt::BottomDockWidgetArea, container5);

    // DEBUG CONSOLE
    QDockWidget* container7 = new QDockWidget(tr("Communication Console"), this);
    container7->setWidget(debugConsole);
    addDockWidget(Qt::BottomDockWidgetArea, container7);

    // OBJECT DETECTION
    QDockWidget* container6 = new QDockWidget(tr("Object Recognition"), this);
    container6->setWidget(detection);
    addDockWidget(Qt::RightDockWidgetArea, container6);

    this->show();
}

pixhawk's avatar
pixhawk committed
429
void MainWindow::loadSettingsView()
pixhawk's avatar
pixhawk committed
430 431 432
{
    clearView();

pixhawk's avatar
pixhawk committed
433 434
    GAudioOutput::instance()->say("Switched to Settings View");

pixhawk's avatar
pixhawk committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448
    // LINE CHART
    centerStack->setCurrentWidget(linechart);

    // COMM XML
    QDockWidget* container1 = new QDockWidget(tr("MAVLink XML to C Code Generator"), this);
    container1->setWidget(protocol);
    addDockWidget(Qt::LeftDockWidgetArea, container1);

    // ONBOARD PARAMETERS
    QDockWidget* container6 = new QDockWidget(tr("Onboard Parameters"), this);
    container6->setWidget(parameters);
    addDockWidget(Qt::RightDockWidgetArea, container6);
}

pixhawk's avatar
pixhawk committed
449
void MainWindow::loadEngineerView()
pixhawk's avatar
pixhawk committed
450 451 452 453
{
    clearView();
    // Engineer view, used in EMAV2009

pixhawk's avatar
pixhawk committed
454 455
    GAudioOutput::instance()->say("Switched to Engineer View");

pixhawk's avatar
pixhawk committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    // LINE CHART
    centerStack->setCurrentWidget(linechart);

    // UAS CONTROL
    QDockWidget* container1 = new QDockWidget(tr("Control"), this);
    container1->setWidget(control);
    addDockWidget(Qt::LeftDockWidgetArea, container1);

    // UAS LIST
    QDockWidget* container4 = new QDockWidget(tr("Unmanned Systems"), this);
    container4->setWidget(list);
    addDockWidget(Qt::BottomDockWidgetArea, container4);

    // UAS STATUS
    QDockWidget* container3 = new QDockWidget(tr("Status Details"), this);
    container3->setWidget(info);
    addDockWidget(Qt::LeftDockWidgetArea, container3);

    // WAYPOINT LIST
    QDockWidget* container5 = new QDockWidget(tr("Waypoint List"), this);
    container5->setWidget(waypoints);
    addDockWidget(Qt::BottomDockWidgetArea, container5);

    // DEBUG CONSOLE
    QDockWidget* container7 = new QDockWidget(tr("Communication Console"), this);
    container7->setWidget(debugConsole);
    addDockWidget(Qt::BottomDockWidgetArea, container7);

    this->show();
}

pixhawk's avatar
pixhawk committed
487
void MainWindow::loadWidgets()
pixhawk's avatar
pixhawk committed
488 489 490 491 492 493 494
{
    loadOperatorView();
    //loadEngineerView();
    //loadPilotView();
}

/*
pixhawk's avatar
pixhawk committed
495
void MainWindow::removeCommConfAct(QAction* action)
pixhawk's avatar
pixhawk committed
496 497 498 499
{
    ui.menuNetwork->removeAction(action);
}*/

pixhawk's avatar
pixhawk committed
500
//void MainWindow::startUAS()
pixhawk's avatar
pixhawk committed
501 502 503 504
//{
//    UASManager::instance()->getActiveUAS()->launch();
//}
//
pixhawk's avatar
pixhawk committed
505
//void MainWindow::returnUAS()
pixhawk's avatar
pixhawk committed
506 507 508 509
//{
//   UASManager::instance()->getActiveUAS()->home();
//}
//
pixhawk's avatar
pixhawk committed
510
//void MainWindow::stopUAS()
pixhawk's avatar
pixhawk committed
511 512 513 514
//{
//    UASManager::instance()->getActiveUAS()->emergencySTOP();
//}
//
pixhawk's avatar
pixhawk committed
515
//void MainWindow::killUAS()
pixhawk's avatar
pixhawk committed
516 517 518 519
//{
//    UASManager::instance()->getActiveUAS()->emergencyKILL();
//}

pixhawk's avatar
pixhawk committed
520
void MainWindow::runTests()
pixhawk's avatar
pixhawk committed
521 522 523 524 525 526 527 528
{
    // TODO Remove after debugging: Add fake data
    static double testvalue = 0.0f;
    testvalue += 0.01f;
    linechart->appendData(126, "test data", testvalue, MG::TIME::getGroundTimeNow());
}