MainWindow.cc 44.5 KB
Newer Older
1 2 3 4
/*=====================================================================

QGroundControl Open Source Ground Control Station

5
(c) 2009 - 2013 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
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

This file is part of the QGROUNDCONTROL project

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

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

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

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

#include <QSettings>
#include <QNetworkInterface>
#include <QDebug>
#include <QTimer>
#include <QHostInfo>
#include <QSplashScreen>
#include <QGCHilLink.h>
#include <QGCHilConfiguration.h>
#include <QGCHilFlightGearConfiguration.h>
39
#include <QQuickView>
40 41
#include <QDesktopWidget>

42 43 44 45 46 47
#include "QGC.h"
#include "MAVLinkSimulationLink.h"
#include "SerialLink.h"
#include "MAVLinkProtocol.h"
#include "QGCWaypointListMulti.h"
#include "MainWindow.h"
dogmaphobic's avatar
dogmaphobic committed
48
#ifndef __android__
49
#include "JoystickWidget.h"
dogmaphobic's avatar
dogmaphobic committed
50
#endif
51 52
#include "GAudioOutput.h"
#include "QGCMAVLinkLogPlayer.h"
Don Gagne's avatar
Don Gagne committed
53
#include "SettingsDialog.h"
54
#include "QGCMapTool.h"
55
#include "QGCMapDisplay.h"
56 57 58
#include "MAVLinkDecoder.h"
#include "QGCMAVLinkMessageSender.h"
#include "QGCRGBDView.h"
59
#include "UASQuickView.h"
60 61
#include "QGCDataPlot2D.h"
#include "Linecharts.h"
62 63
#include "QGCTabbedInfoView.h"
#include "UASRawStatusView.h"
dogmaphobic's avatar
dogmaphobic committed
64
#include "QGCFlightDisplay.h"
65
#include "SetupView.h"
66 67
#include "SerialSettingsDialog.h"
#include "terminalconsole.h"
68
#include "QGCUASFileViewMulti.h"
Don Gagne's avatar
Don Gagne committed
69
#include "QGCApplication.h"
70
#include "QGCFileDialog.h"
Don Gagne's avatar
Don Gagne committed
71
#include "QGCMessageBox.h"
72
#include "QGCDockWidget.h"
73

74 75 76 77
#ifdef UNITTEST_BUILD
#include "QmlControls/QmlTestWidget.h"
#endif

78 79 80 81 82 83
#ifdef QGC_OSG_ENABLED
#include "Q3DWidgetFactory.h"
#endif

#include "LogCompressor.h"

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
// Pixel size, instead of a physical thing is actually a philosophical question when
// it comes to Qt. Fonts are that and some heavy Kabalistic Voodoo added to the mix.
// The values below came from actually measuring the elements on the screen on these
// devices. I have yet to find a constant from Qt so these things can be properly
// computed at runtime.

#if defined(Q_OS_OSX)
double MainWindow::_pixelFactor    = 1.0;
double MainWindow::_fontFactor     = 1.0;
#elif defined(Q_OS_WIN)
double MainWindow::_pixelFactor    = 0.86;
double MainWindow::_fontFactor     = 0.63;
#elif defined(__android__)
double MainWindow::_pixelFactor    = 2.0;
double MainWindow::_fontFactor     = 1.23;
#elif defined(Q_OS_LINUX)
double MainWindow::_pixelFactor    = 1.0;
double MainWindow::_fontFactor     = 0.85;
#endif

104 105 106
/// The key under which the Main Window settings are saved
const char* MAIN_SETTINGS_GROUP = "QGC_MAINWINDOW";

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
const char* MainWindow::_uasControlDockWidgetName = "UNMANNED_SYSTEM_CONTROL_DOCKWIDGET";
const char* MainWindow::_uasListDockWidgetName = "UNMANNED_SYSTEM_LIST_DOCKWIDGET";
const char* MainWindow::_waypointsDockWidgetName = "WAYPOINT_LIST_DOCKWIDGET";
const char* MainWindow::_mavlinkDockWidgetName = "MAVLINK_INSPECTOR_DOCKWIDGET";
const char* MainWindow::_parametersDockWidgetName = "PARAMETER_INTERFACE_DOCKWIDGET";
const char* MainWindow::_filesDockWidgetName = "FILE_VIEW_DOCKWIDGET";
const char* MainWindow::_uasStatusDetailsDockWidgetName = "UAS_STATUS_DETAILS_DOCKWIDGET";
const char* MainWindow::_mapViewDockWidgetName = "MAP_VIEW_DOCKWIDGET";
const char* MainWindow::_hsiDockWidgetName = "HORIZONTAL_SITUATION_INDICATOR_DOCKWIDGET";
const char* MainWindow::_hdd1DockWidgetName = "HEAD_DOWN_DISPLAY_1_DOCKWIDGET";
const char* MainWindow::_hdd2DockWidgetName = "HEAD_DOWN_DISPLAY_2_DOCKWIDGET";
const char* MainWindow::_pfdDockWidgetName = "PRIMARY_FLIGHT_DISPLAY_DOCKWIDGET";
const char* MainWindow::_hudDockWidgetName = "HEAD_UP_DISPLAY_DOCKWIDGET";
const char* MainWindow::_uasInfoViewDockWidgetName = "UAS_INFO_INFOVIEW_DOCKWIDGET";
const char* MainWindow::_debugConsoleDockWidgetName = "COMMUNICATION_CONSOLE_DOCKWIDGET";

Don Gagne's avatar
Don Gagne committed
123 124
static MainWindow* _instance = NULL;   ///< @brief MainWindow singleton

125
MainWindow* MainWindow::_create(QSplashScreen* splashScreen)
126
{
Don Gagne's avatar
Don Gagne committed
127
    Q_ASSERT(_instance == NULL);
128
    new MainWindow(splashScreen);
Don Gagne's avatar
Don Gagne committed
129 130
    // _instance is set in constructor
    Q_ASSERT(_instance);
131 132 133
    return _instance;
}

Don Gagne's avatar
Don Gagne committed
134
MainWindow* MainWindow::instance(void)
135
{
Don Gagne's avatar
Don Gagne committed
136
    return _instance;
137 138
}

139 140
void MainWindow::deleteInstance(void)
{
Don Gagne's avatar
Don Gagne committed
141
    delete this;
142 143
}

Don Gagne's avatar
Don Gagne committed
144 145 146
/// @brief Private constructor for MainWindow. MainWindow singleton is only ever created
///         by MainWindow::_create method. Hence no other code should have access to
///         constructor.
dogmaphobic's avatar
dogmaphobic committed
147 148 149
MainWindow::MainWindow(QSplashScreen* splashScreen)
    : _autoReconnect(false)
    , _lowPowerMode(false)
150
    , _showStatusBar(false)
dogmaphobic's avatar
dogmaphobic committed
151 152 153 154 155 156
    , _centerStackActionGroup(new QActionGroup(this))
    , _simulationLink(NULL)
    , _centralLayout(NULL)
    , _currentViewWidget(NULL)
    , _splashScreen(splashScreen)
    , _currentView(VIEW_SETUP)
157
{
Don Gagne's avatar
Don Gagne committed
158 159
    Q_ASSERT(_instance == NULL);
    _instance = this;
160

161 162 163
    if (splashScreen) {
        connect(this, &MainWindow::initStatusChanged, splashScreen, &QSplashScreen::showMessage);
    }
164

dogmaphobic's avatar
dogmaphobic committed
165
    // Setup user interface
166
    loadSettings();
167
    emit initStatusChanged(tr("Setting up user interface"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
dogmaphobic's avatar
dogmaphobic committed
168 169
    _ui.setupUi(this);
    // Make sure tool bar elements all fit before changing minimum width
dogmaphobic's avatar
dogmaphobic committed
170
    setMinimumWidth(1008);
dogmaphobic's avatar
dogmaphobic committed
171
    configureWindowName();
172

173 174
    // Setup central widget with a layout to hold the views
    _centralLayout = new QVBoxLayout();
175
    _centralLayout->setContentsMargins(0,0,0,0);
176
    centralWidget()->setLayout(_centralLayout);
177 178 179
    // Set dock options
    setDockOptions(AnimatedDocks | AllowTabbedDocks | AllowNestedDocks);
    // Setup corners
180
    setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
181

182 183
    // Qt 4 on Ubuntu does place the native menubar correctly so on Linux we revert back to in-window menu bar.
    // TODO: Check that this is still necessary on Qt5 on Ubuntu
dogmaphobic's avatar
dogmaphobic committed
184

185 186 187
#ifdef Q_OS_LINUX
    menuBar()->setNativeMenuBar(false);
#endif
dogmaphobic's avatar
dogmaphobic committed
188 189 190 191

    // On Mobile devices, we don't want any main menus at all.
#ifdef __mobile__
    menuBar()->setNativeMenuBar(false);
dogmaphobic's avatar
dogmaphobic committed
192
#endif
dogmaphobic's avatar
dogmaphobic committed
193

194 195 196
#ifdef UNITTEST_BUILD
    QAction* qmlTestAction = new QAction("Test QML palette and controls", NULL);
    connect(qmlTestAction, &QAction::triggered, this, &MainWindow::_showQmlTestWidget);
dogmaphobic's avatar
dogmaphobic committed
197
    _ui.menuTools->addAction(qmlTestAction);
198
#endif
199

dogmaphobic's avatar
dogmaphobic committed
200 201 202 203
    // Load QML Toolbar
    QDockWidget* widget = new QDockWidget(this);
    widget->setObjectName("ToolBarDockWidget");
    qmlRegisterType<MainToolBar>("QGroundControl.MainToolBar", 1, 0, "MainToolBar");
204
    _mainToolBar = new MainToolBar(widget);
dogmaphobic's avatar
dogmaphobic committed
205 206 207 208
    widget->setWidget(_mainToolBar);
    widget->setFeatures(QDockWidget::NoDockWidgetFeatures);
    widget->setTitleBarWidget(new QWidget(this)); // Disables the title bar
    addDockWidget(Qt::TopDockWidgetArea, widget);
209

dogmaphobic's avatar
dogmaphobic committed
210 211 212
    // Setup UI state machines
    _centerStackActionGroup->setExclusive(true);
    // Status Bar
213
    setStatusBar(new QStatusBar(this));
214
    statusBar()->setSizeGripEnabled(true);
215

216
    emit initStatusChanged(tr("Building common widgets."), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
217
    _buildCommonWidgets();
218
    emit initStatusChanged(tr("Building common actions"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
219 220 221
    // Create actions
    connectCommonActions();
    // Connect user interface devices
222
    emit initStatusChanged(tr("Initializing joystick interface"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
dogmaphobic's avatar
dogmaphobic committed
223
#ifndef __android__
224
    joystick = new JoystickInput();
dogmaphobic's avatar
dogmaphobic committed
225
#endif
226
#ifdef QGC_MOUSE_ENABLED_WIN
227
    emit initStatusChanged(tr("Initializing 3D mouse interface"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
228 229
    mouseInput = new Mouse3DInput(this);
    mouse = new Mouse6dofInput(mouseInput);
230
#endif //QGC_MOUSE_ENABLED_WIN
231

232
#if QGC_MOUSE_ENABLED_LINUX
233
    emit initStatusChanged(tr("Initializing 3D mouse interface"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
234 235

    mouse = new Mouse6dofInput(this);
236
    connect(this, SIGNAL(x11EventOccured(XEvent*)), mouse, SLOT(handleX11Event(XEvent*)));
237
#endif //QGC_MOUSE_ENABLED_LINUX
238

239 240 241 242
    // These also cause the screen to redraw so we need to update any OpenGL canvases in QML controls
    connect(LinkManager::instance(), &LinkManager::linkConnected,    this, &MainWindow::_linkStateChange);
    connect(LinkManager::instance(), &LinkManager::linkDisconnected, this, &MainWindow::_linkStateChange);

243
    // Connect link
dogmaphobic's avatar
dogmaphobic committed
244
    if (_autoReconnect)
245
    {
246
        restoreLastUsedConnection();
247 248 249
    }

    // Set low power mode
dogmaphobic's avatar
dogmaphobic committed
250
    enableLowPowerMode(_lowPowerMode);
251
    emit initStatusChanged(tr("Restoring last view state"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
252
    // Restore the window setup
253
    _loadCurrentViewState();
254 255
#ifndef __android__

256
    // Restore the window position and size
dogmaphobic's avatar
dogmaphobic committed
257 258
    emit initStatusChanged(tr("Restoring last window size"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
    if (settings.contains(_getWindowGeometryKey()))
259
    {
dogmaphobic's avatar
dogmaphobic committed
260
        restoreGeometry(settings.value(_getWindowGeometryKey()).toByteArray());
261 262 263 264
    }
    else
    {
        // Adjust the size
265 266 267
        QScreen* scr = QApplication::primaryScreen();
        QSize scrSize = scr->availableSize();
        if (scrSize.width() <= 1280)
268
        {
269
            resize(scrSize.width(), scrSize.height());
270 271 272
        }
        else
        {
273 274 275
            int w = scrSize.width()  > 1600 ? 1600 : scrSize.width();
            int h = scrSize.height() >  800 ?  800 : scrSize.height();
            resize(w, h);
276
            move((scrSize.width() - w) / 2, (scrSize.height() - h) / 2);
277 278 279
        }
    }

280 281 282
    // Make sure the proper fullscreen/normal menu item is checked properly.
    if (isFullScreen())
    {
dogmaphobic's avatar
dogmaphobic committed
283 284
        _ui.actionFullscreen->setChecked(true);
        _ui.actionNormal->setChecked(false);
285 286 287
    }
    else
    {
dogmaphobic's avatar
dogmaphobic committed
288 289
        _ui.actionFullscreen->setChecked(false);
        _ui.actionNormal->setChecked(true);
290 291 292
    }

    // And that they will stay checked properly after user input
293 294
    connect(_ui.actionFullscreen, &QAction::triggered, this, &MainWindow::fullScreenActionItemCallback);
    connect(_ui.actionNormal,     &QAction::triggered, this, &MainWindow::normalActionItemCallback);
295 296
#endif

297
    connect(_ui.actionStatusBar,  &QAction::triggered, this, &MainWindow::showStatusBarCallback);
298

299 300
    // Set OS dependent keyboard shortcuts for the main window, non OS dependent shortcuts are set in MainWindow.ui
#ifdef Q_OS_MACX
dogmaphobic's avatar
dogmaphobic committed
301
    _ui.actionSetup->setShortcut(QApplication::translate("MainWindow", "Meta+1", 0));
302 303 304
    _ui.actionPlan->setShortcut(QApplication::translate("MainWindow", "Meta+2", 0));
    _ui.actionFlight->setShortcut(QApplication::translate("MainWindow", "Meta+3", 0));
    _ui.actionAnalyze->setShortcut(QApplication::translate("MainWindow", "Meta+4", 0));
305 306
    _ui.actionTerminalView->setShortcut(QApplication::translate("MainWindow", "Meta+5", 0));
    _ui.actionSimulationView->setShortcut(QApplication::translate("MainWindow", "Meta+6", 0));
dogmaphobic's avatar
dogmaphobic committed
307
    _ui.actionFullscreen->setShortcut(QApplication::translate("MainWindow", "Meta+Return", 0));
308
#else
dogmaphobic's avatar
dogmaphobic committed
309
    _ui.actionSetup->setShortcut(QApplication::translate("MainWindow", "Ctrl+1", 0));
Gus Grubba's avatar
Gus Grubba committed
310
    _ui.actionPlan->setShortcut(QApplication::translate("MainWindow", "Ctrl+2", 0));
311
    _ui.actionFlight->setShortcut(QApplication::translate("MainWindow", "Ctrl+3", 0));
Gus Grubba's avatar
Gus Grubba committed
312
    _ui.actionAnalyze->setShortcut(QApplication::translate("MainWindow", "Ctrl+4", 0));
313 314
    _ui.actionTerminalView->setShortcut(QApplication::translate("MainWindow", "Ctrl+5", 0));
    _ui.actionSimulationView->setShortcut(QApplication::translate("MainWindow", "Ctrl+6", 0));
dogmaphobic's avatar
dogmaphobic committed
315
    _ui.actionFullscreen->setShortcut(QApplication::translate("MainWindow", "Ctrl+Return", 0));
316 317
#endif

318 319
    connect(&windowNameUpdateTimer, SIGNAL(timeout()), this, SLOT(configureWindowName()));
    windowNameUpdateTimer.start(15000);
320
    emit initStatusChanged(tr("Done"), Qt::AlignLeft | Qt::AlignBottom, QColor(62, 93, 141));
321 322

    if (!qgcApp()->runningUnitTests()) {
323 324
        _ui.actionStatusBar->setChecked(_showStatusBar);
        showStatusBarCallback(_showStatusBar);
325 326 327
#ifdef __android__
        menuBar()->hide();
#endif
328
        show();
dogmaphobic's avatar
dogmaphobic committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#ifdef Q_OS_MAC
        // TODO HACK
        // This is a really ugly hack. For whatever reason, by having a QQuickWidget inside a
        // QDockWidget (MainToolBar above), the main menu is not shown when the app first
        // starts. I looked everywhere and I could not find a solution. What I did notice was
        // that if any other window gets focus, the menu comes up when you come back to QGC.
        // That is, if you were to click on another window and then back to QGC, the menus
        // would appear. This hack below creates a 0x0 dialog and immediately closes it.
        // That works around the issue and it will do until I find the root of the problem.
        QDialog qd(this);
        qd.show();
        qd.raise();
        qd.activateWindow();
        qd.close();
#endif
344
    }
345 346 347 348
}

MainWindow::~MainWindow()
{
dogmaphobic's avatar
dogmaphobic committed
349
    if (_simulationLink)
350
    {
dogmaphobic's avatar
dogmaphobic committed
351 352
        delete _simulationLink;
        _simulationLink = NULL;
353
    }
dogmaphobic's avatar
dogmaphobic committed
354
#ifndef __android__
355 356
    if (joystick)
    {
357 358
        joystick->shutdown();
        joystick->wait(5000);
359 360 361
        delete joystick;
        joystick = NULL;
    }
dogmaphobic's avatar
dogmaphobic committed
362
#endif
363
    // Delete all UAS objects
dogmaphobic's avatar
dogmaphobic committed
364
    for (int i=0;i<_commsWidgetList.size();i++)
365
    {
dogmaphobic's avatar
dogmaphobic committed
366
        _commsWidgetList[i]->deleteLater();
367
    }
Don Gagne's avatar
Don Gagne committed
368
    _instance = NULL;
369 370 371 372 373 374 375
}

void MainWindow::resizeEvent(QResizeEvent * event)
{
    QMainWindow::resizeEvent(event);
}

dogmaphobic's avatar
dogmaphobic committed
376
QString MainWindow::_getWindowStateKey()
377
{
378
	return QString::number(_currentView)+"_windowstate_";
379 380
}

dogmaphobic's avatar
dogmaphobic committed
381
QString MainWindow::_getWindowGeometryKey()
382 383 384 385
{
    return "_geometry";
}

386 387 388
void MainWindow::_createDockWidget(const QString& title, const QString& name, Qt::DockWidgetArea area, QWidget* innerWidget)
{
    Q_ASSERT(!_mapName2DockWidget.contains(name));
389
	
390 391 392 393 394
    // Add to menu
    QAction* action = new QAction(title, NULL);
    action->setCheckable(true);
    action->setData(name);
    connect(action, &QAction::triggered, this, &MainWindow::_showDockWidgetAction);
dogmaphobic's avatar
dogmaphobic committed
395
    _ui.menuTools->addAction(action);
396 397 398 399 400 401 402 403 404 405 406 407 408
	
	// Create widget
	QGCDockWidget* dockWidget = new QGCDockWidget(title, action, this);
	Q_CHECK_PTR(dockWidget);
	dockWidget->setObjectName(name);
	dockWidget->setVisible (false);
	if (innerWidget) {
		// Put inner widget inside QDockWidget
		innerWidget->setParent(dockWidget);
		dockWidget->setWidget(innerWidget);
		innerWidget->setVisible(true);
	}
	
409 410 411 412 413 414
    _mapName2DockWidget[name] = dockWidget;
    _mapDockWidget2Action[dockWidget] = action;
    addDockWidget(area, dockWidget);
}

void MainWindow::_buildCommonWidgets(void)
415 416
{
    // Add generic MAVLink decoder
dogmaphobic's avatar
dogmaphobic committed
417
    // TODO: This is never deleted
418
    mavlinkDecoder = new MAVLinkDecoder(MAVLinkProtocol::instance(), this);
John Tapsell's avatar
John Tapsell committed
419 420
    connect(mavlinkDecoder, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)),
                      this, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)));
421

422
    // Log player
dogmaphobic's avatar
dogmaphobic committed
423
    // TODO: Make this optional with a preferences setting or under a "View" menu
424
    logPlayer = new QGCMAVLinkLogPlayer(MAVLinkProtocol::instance(), statusBar());
425
    statusBar()->addPermanentWidget(logPlayer);
426

427 428 429
    // In order for Qt to save and restore state of widgets all widgets must be created ahead of time. We only create the QDockWidget
    // holders. We do not create the actual inner widget until it is needed. This saves memory and cpu from running widgets that are
    // never shown.
430

431 432 433 434 435
    struct DockWidgetInfo {
        const char* name;
        const char* title;
        Qt::DockWidgetArea area;
    };
436

437 438 439 440 441
    static const struct DockWidgetInfo rgDockWidgetInfo[] = {
        { _uasControlDockWidgetName,        "Control",                  Qt::LeftDockWidgetArea },
        { _uasListDockWidgetName,           "Unmanned Systems",         Qt::RightDockWidgetArea },
        { _waypointsDockWidgetName,         "Mission Plan",             Qt::BottomDockWidgetArea },
        { _mavlinkDockWidgetName,           "MAVLink Inspector",        Qt::RightDockWidgetArea },
442
        { _parametersDockWidgetName,        "Parameter Editor",			Qt::RightDockWidgetArea },
443 444 445 446 447 448 449 450 451
        { _filesDockWidgetName,             "Onboard Files",            Qt::RightDockWidgetArea },
        { _uasStatusDetailsDockWidgetName,  "Status Details",           Qt::RightDockWidgetArea },
        { _mapViewDockWidgetName,           "Map view",                 Qt::RightDockWidgetArea },
        { _hsiDockWidgetName,               "Horizontal Situation",     Qt::BottomDockWidgetArea },
        { _hdd1DockWidgetName,              "Flight Display",           Qt::RightDockWidgetArea },
        { _hdd2DockWidgetName,              "Actuator Status",          Qt::RightDockWidgetArea },
        { _pfdDockWidgetName,               "Primary Flight Display",   Qt::RightDockWidgetArea },
        { _hudDockWidgetName,               "Video Downlink",           Qt::RightDockWidgetArea },
        { _uasInfoViewDockWidgetName,       "Info View",                Qt::LeftDockWidgetArea },
452
        { _debugConsoleDockWidgetName,      "Communications Console",   Qt::LeftDockWidgetArea },
453 454
    };
    static const size_t cDockWidgetInfo = sizeof(rgDockWidgetInfo) / sizeof(rgDockWidgetInfo[0]);
455

456 457 458
    for (size_t i=0; i<cDockWidgetInfo; i++) {
        const struct DockWidgetInfo* pDockInfo = &rgDockWidgetInfo[i];
        _createDockWidget(pDockInfo->title, pDockInfo->name, pDockInfo->area, NULL /* no inner widget yet */);
459
    }
460
}
461

462
void MainWindow::_buildPlanView(void)
463
{
464 465 466
    if (!_planView) {
        _planView = new QGCMapTool(this);
        _planView->setVisible(false);
467
    }
468
}
469

470
void MainWindow::_buildExperimentalPlanView(void)
471
{
472 473 474 475 476 477 478 479 480 481 482
    if (!_experimentalPlanView) {
        _experimentalPlanView = new QGCMapDisplay(this);
        _experimentalPlanView->setVisible(false);
    }
}

void MainWindow::_buildFlightView(void)
{
    if (!_flightView) {
        _flightView = new QGCFlightDisplay(this);
        _flightView->setVisible(false);
483
    }
484 485
}

486
void MainWindow::_buildSetupView(void)
487
{
488 489 490 491
    if (!_setupView) {
        _setupView = new SetupView(this);
        _setupView->setVisible(false);
    }
492 493
}

494
void MainWindow::_buildAnalyzeView(void)
495
{
496 497 498
    if (!_analyzeView) {
        _analyzeView = new QGCDataPlot2D(this);
        _analyzeView->setVisible(false);
499 500
    }
}
501

502 503 504 505 506 507
void MainWindow::_buildSimView(void)
{
    if (!_simView) {
        _simView = new QGCMapTool(this);
        _simView->setVisible(false);
    }
508
}
509

510
void MainWindow::_buildTerminalView(void)
511
{
512 513 514 515
    if (!_terminalView) {
        _terminalView = new TerminalConsole(this);
        _terminalView->setVisible(false);
    }
516 517
}

518 519
/// Shows or hides the specified dock widget, creating if necessary
void MainWindow::_showDockWidget(const QString& name, bool show)
520
{
521 522
    if (!_mapName2DockWidget.contains(name)) {
        qWarning() << "Attempt to show unknown dock widget" << name;
523 524
        return;
    }
525

526 527 528
    // Create the inner widget if we need to
    if (!_mapName2DockWidget[name]->widget()) {
        _createInnerDockWidget(name);
529
    }
530 531 532 533

    Q_ASSERT(_mapName2DockWidget.contains(name));
    QDockWidget* dockWidget = _mapName2DockWidget[name];
    Q_ASSERT(dockWidget);
534

535
    dockWidget->setVisible(show);
536

537 538 539 540 541 542 543 544 545
    Q_ASSERT(_mapDockWidget2Action.contains(dockWidget));
    _mapDockWidget2Action[dockWidget]->setChecked(show);
}

/// Creates the specified inner dock widget and adds to the QDockWidget
void MainWindow::_createInnerDockWidget(const QString& widgetName)
{
    Q_ASSERT(_mapName2DockWidget.contains(widgetName)); // QDockWidget should already exist
    Q_ASSERT(!_mapName2DockWidget[widgetName]->widget());     // Inner widget should not
546

547
    QWidget* widget = NULL;
548

549 550 551 552 553 554 555 556 557
    if (widgetName == _uasControlDockWidgetName) {
        widget = new UASControlWidget(this);
    } else if (widgetName == _uasListDockWidgetName) {
        widget = new UASListWidget(this);
    } else if (widgetName == _waypointsDockWidgetName) {
        widget = new QGCWaypointListMulti(this);
    } else if (widgetName == _mavlinkDockWidgetName) {
        widget = new QGCMAVLinkInspector(MAVLinkProtocol::instance(),this);
    } else if (widgetName == _parametersDockWidgetName) {
558
        widget = new ParameterEditorWidget(this);
559 560 561 562 563 564 565 566 567
    } else if (widgetName == _filesDockWidgetName) {
        widget = new QGCUASFileViewMulti(this);
    } else if (widgetName == _uasStatusDetailsDockWidgetName) {
        widget = new UASInfoWidget(this);
    } else if (widgetName == _mapViewDockWidgetName) {
        widget = new QGCMapTool(this);
    } else if (widgetName == _hsiDockWidgetName) {
        widget = new HSIDisplay(this);
    } else if (widgetName == _hdd1DockWidgetName) {
568 569 570 571
        QStringList acceptList;
        acceptList.append("-3.3,ATTITUDE.roll,rad,+3.3,s");
        acceptList.append("-3.3,ATTITUDE.pitch,deg,+3.3,s");
        acceptList.append("-3.3,ATTITUDE.yaw,deg,+3.3,s");
572 573
        HDDisplay *hddisplay = new HDDisplay(acceptList,"Flight Display",this);
        hddisplay->addSource(mavlinkDecoder);
574

575 576
        widget = hddisplay;
    } else if (widgetName == _hdd2DockWidgetName) {
577 578 579
        QStringList acceptList;
        acceptList.append("0,RAW_PRESSURE.pres_abs,hPa,65500");
        HDDisplay *hddisplay = new HDDisplay(acceptList,"Actuator Status",this);
580
        hddisplay->addSource(mavlinkDecoder);
581

582 583
        widget = hddisplay;
    } else if (widgetName == _pfdDockWidgetName) {
dogmaphobic's avatar
dogmaphobic committed
584
        widget = new QGCFlightDisplay(this);
585 586 587
    } else if (widgetName == _hudDockWidgetName) {
        widget = new HUD(320,240,this);
    } else if (widgetName == _uasInfoViewDockWidgetName) {
588 589 590
        QGCTabbedInfoView* pInfoView = new QGCTabbedInfoView(this);
        pInfoView->addSource(mavlinkDecoder);
        widget = pInfoView;
591 592 593 594
    } else if (widgetName == _debugConsoleDockWidgetName) {
        widget = new DebugConsole(this);
    } else {
        qWarning() << "Attempt to create unknown Inner Dock Widget" << widgetName;
595
    }
596

597 598 599 600 601
    if (widget) {
        QDockWidget* dockWidget = _mapName2DockWidget[widgetName];
        Q_CHECK_PTR(dockWidget);
        widget->setParent(dockWidget);
        dockWidget->setWidget(widget);
602 603
    }
}
604

605
void MainWindow::_showHILConfigurationWidgets(void)
606
{
607
    UASInterface* uas = UASManager::instance()->getActiveUAS();
608

609 610 611
    if (!uas) {
        return;
    }
612

613 614
    UAS* mav = dynamic_cast<UAS*>(uas);
    Q_ASSERT(mav);
615

616
    int uasId = mav->getUASID();
617

618
    if (!_mapUasId2HilDockWidget.contains(uasId)) {
619

620
        // Create QDockWidget
621
        QGCDockWidget* dockWidget = new QGCDockWidget(tr("HIL Config %1").arg(uasId), NULL, this);
622 623 624
        Q_CHECK_PTR(dockWidget);
        dockWidget->setObjectName(tr("HIL_CONFIG_%1").arg(uasId));
        dockWidget->setVisible (false);
625

626 627
        // Create inner widget and set it
        QWidget* widget = new QGCHilConfiguration(mav, dockWidget);
628

629 630
        widget->setParent(dockWidget);
        dockWidget->setWidget(widget);
631

632
        _mapUasId2HilDockWidget[uasId] = dockWidget;
633

634 635
        addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
    }
636

637 638 639 640 641
    if (_currentView == VIEW_SIMULATION) {
        // HIL dock widgets only show up on simulation view
        foreach (QDockWidget* dockWidget, _mapUasId2HilDockWidget) {
            dockWidget->setVisible(true);
        }
642 643 644
    }
}

645
void MainWindow::fullScreenActionItemCallback(bool)
646
{
dogmaphobic's avatar
dogmaphobic committed
647
    _ui.actionNormal->setChecked(false);
648 649
}

650
void MainWindow::normalActionItemCallback(bool)
651
{
dogmaphobic's avatar
dogmaphobic committed
652
    _ui.actionFullscreen->setChecked(false);
653 654
}

655 656 657 658
void MainWindow::showStatusBarCallback(bool checked)
{
    _showStatusBar = checked;
    checked ? statusBar()->show() : statusBar()->hide();
659
    _ui.actionStatusBar->setText(checked ? "Hide Status Bar" : "Show Status Bar");
660 661
}

662 663
void MainWindow::closeEvent(QCloseEvent *event)
{
664
    // Disallow window close if there are active connections
665
    if (LinkManager::instance()->anyConnectedLinks()) {
dogmaphobic's avatar
dogmaphobic committed
666 667 668 669 670 671
        QGCMessageBox::StandardButton button =
            QGCMessageBox::warning(
                tr("QGroundControl close"),
                tr("There are still active connections to vehicles. Do you want to disconnect these before closing?"),
                QMessageBox::Yes | QMessageBox::Cancel,
                QMessageBox::Cancel);
672
        if (button == QMessageBox::Yes) {
673
            LinkManager::instance()->disconnectAll();
674 675 676 677
        } else {
            event->ignore();
            return;
        }
678 679
    }

680 681
    // This will process any remaining flight log save dialogs
    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
682
    
683
    // Should not be any active connections
684 685
    Q_ASSERT(!LinkManager::instance()->anyConnectedLinks());
    
686
    _storeCurrentViewState();
687
    storeSettings();
688
    UASManager::instance()->storeSettings();
689
    event->accept();
690 691 692 693
}

void MainWindow::loadSettings()
{
694
    // Why the screaming?
695
    QSettings settings;
696
    settings.beginGroup(MAIN_SETTINGS_GROUP);
697 698 699 700 701
    _autoReconnect  = settings.value("AUTO_RECONNECT",      _autoReconnect).toBool();
    _lowPowerMode   = settings.value("LOW_POWER_MODE",      _lowPowerMode).toBool();
    _showStatusBar  = settings.value("SHOW_STATUSBAR",      _showStatusBar).toBool();
    _fontFactor     = settings.value("FONT_SIZE_FACTOR",    _fontFactor).toDouble();
    _pixelFactor    = settings.value("PIXEL_SIZE_FACTOR",   _pixelFactor).toDouble();
702
    settings.endGroup();
dogmaphobic's avatar
dogmaphobic committed
703 704 705
    // Select the proper view. Default to the flight view or load the last one used if it's supported.
    VIEW_SECTIONS currentViewCandidate = (VIEW_SECTIONS) settings.value("CURRENT_VIEW", _currentView).toInt();
    switch (currentViewCandidate) {
706 707 708
        case VIEW_ANALYZE:
        case VIEW_PLAN:
        case VIEW_EXPERIMENTAL_PLAN:
dogmaphobic's avatar
dogmaphobic committed
709 710 711 712 713 714 715 716 717 718 719 720
        case VIEW_FLIGHT:
        case VIEW_SIMULATION:
        case VIEW_SETUP:
        case VIEW_TERMINAL:
            _currentView = currentViewCandidate;
            break;
        default:
            // Leave _currentView to the default
            break;
    }
    // Put it back, which will set it to a valid value
    settings.setValue("CURRENT_VIEW", _currentView);
721 722 723 724 725
}

void MainWindow::storeSettings()
{
    QSettings settings;
726
    settings.beginGroup(MAIN_SETTINGS_GROUP);
727 728 729 730 731
    settings.setValue("AUTO_RECONNECT",     _autoReconnect);
    settings.setValue("LOW_POWER_MODE",     _lowPowerMode);
    settings.setValue("SHOW_STATUSBAR",     _showStatusBar);
    settings.setValue("FONT_SIZE_FACTOR",   _fontFactor);
    settings.setValue("PIXEL_SIZE_FACTOR",  _pixelFactor);
732
    settings.endGroup();
dogmaphobic's avatar
dogmaphobic committed
733
    settings.setValue(_getWindowGeometryKey(), saveGeometry());
734
	
735
    // Save the last current view in any case
736
    settings.setValue("CURRENT_VIEW", _currentView);
737
    settings.setValue(_getWindowStateKey(), saveState());
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
}

void MainWindow::configureWindowName()
{
    QList<QHostAddress> hostAddresses = QNetworkInterface::allAddresses();
    QString windowname = qApp->applicationName() + " " + qApp->applicationVersion();
    bool prevAddr = false;
    windowname.append(" (" + QHostInfo::localHostName() + ": ");
    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(")");
    setWindowTitle(windowname);
}

void MainWindow::enableAutoReconnect(bool enabled)
{
dogmaphobic's avatar
dogmaphobic committed
762
    _autoReconnect = enabled;
763 764 765 766 767 768 769 770 771
}

/**
* @brief Create all actions associated to the main window
*
**/
void MainWindow::connectCommonActions()
{
    // Bind together the perspective actions
dogmaphobic's avatar
dogmaphobic committed
772
    QActionGroup* perspectives = new QActionGroup(_ui.menuPerspectives);
773 774
    perspectives->addAction(_ui.actionAnalyze);
    perspectives->addAction(_ui.actionFlight);
dogmaphobic's avatar
dogmaphobic committed
775
    perspectives->addAction(_ui.actionSimulationView);
776
    perspectives->addAction(_ui.actionPlan);
dogmaphobic's avatar
dogmaphobic committed
777 778
    perspectives->addAction(_ui.actionSetup);
    perspectives->addAction(_ui.actionTerminalView);
779
    perspectives->addAction(_ui.actionExperimentalPlanView);
780 781 782
    perspectives->setExclusive(true);

    // Mark the right one as selected
783
    if (_currentView == VIEW_ANALYZE)
784
    {
785 786
        _ui.actionAnalyze->setChecked(true);
        _ui.actionAnalyze->activate(QAction::Trigger);
787
    }
788
    if (_currentView == VIEW_FLIGHT)
789
    {
790 791
        _ui.actionFlight->setChecked(true);
        _ui.actionFlight->activate(QAction::Trigger);
792
    }
793
    if (_currentView == VIEW_SIMULATION)
794
    {
dogmaphobic's avatar
dogmaphobic committed
795 796
        _ui.actionSimulationView->setChecked(true);
        _ui.actionSimulationView->activate(QAction::Trigger);
797
    }
798
    if (_currentView == VIEW_PLAN)
799
    {
800 801 802 803 804 805 806
        _ui.actionPlan->setChecked(true);
        _ui.actionPlan->activate(QAction::Trigger);
    }
    if (_currentView == VIEW_EXPERIMENTAL_PLAN)
    {
        _ui.actionExperimentalPlanView->setChecked(true);
        _ui.actionExperimentalPlanView->activate(QAction::Trigger);
807
    }
808
    if (_currentView == VIEW_SETUP)
809
    {
dogmaphobic's avatar
dogmaphobic committed
810 811
        _ui.actionSetup->setChecked(true);
        _ui.actionSetup->activate(QAction::Trigger);
812
    }
813
    if (_currentView == VIEW_TERMINAL)
814
    {
dogmaphobic's avatar
dogmaphobic committed
815 816
        _ui.actionTerminalView->setChecked(true);
        _ui.actionTerminalView->activate(QAction::Trigger);
817
    }
818 819

    // The UAS actions are not enabled without connection to system
dogmaphobic's avatar
dogmaphobic committed
820 821 822 823 824
    _ui.actionLiftoff->setEnabled(false);
    _ui.actionLand->setEnabled(false);
    _ui.actionEmergency_Kill->setEnabled(false);
    _ui.actionEmergency_Land->setEnabled(false);
    _ui.actionShutdownMAV->setEnabled(false);
825 826

    // Connect actions from ui
dogmaphobic's avatar
dogmaphobic committed
827
    connect(_ui.actionAdd_Link, SIGNAL(triggered()), this, SLOT(manageLinks()));
828 829 830 831 832

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

    // Unmanned System controls
dogmaphobic's avatar
dogmaphobic committed
833 834 835 836 837
    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.actionShutdownMAV, SIGNAL(triggered()), UASManager::instance(), SLOT(shutdownActiveUAS()));
838 839

    // Views actions
840
    connect(_ui.actionFlight, SIGNAL(triggered()), this, SLOT(loadFlightView()));
dogmaphobic's avatar
dogmaphobic committed
841
    connect(_ui.actionSimulationView, SIGNAL(triggered()), this, SLOT(loadSimulationView()));
842 843 844
    connect(_ui.actionAnalyze, SIGNAL(triggered()), this, SLOT(loadAnalyzeView()));
    connect(_ui.actionPlan, SIGNAL(triggered()), this, SLOT(loadPlanView()));
    connect(_ui.actionExperimentalPlanView, SIGNAL(triggered()), this, SLOT(loadOldPlanView()));
dogmaphobic's avatar
dogmaphobic committed
845
    connect(_ui.actionTerminalView,SIGNAL(triggered()),this,SLOT(loadTerminalView()));
846 847

    // Help Actions
dogmaphobic's avatar
dogmaphobic committed
848 849 850
    connect(_ui.actionOnline_Documentation, SIGNAL(triggered()), this, SLOT(showHelp()));
    connect(_ui.actionDeveloper_Credits, SIGNAL(triggered()), this, SLOT(showCredits()));
    connect(_ui.actionProject_Roadmap, SIGNAL(triggered()), this, SLOT(showRoadMap()));
851 852

    // Audio output
dogmaphobic's avatar
dogmaphobic committed
853 854 855
    _ui.actionMuteAudioOutput->setChecked(GAudioOutput::instance()->isMuted());
    connect(GAudioOutput::instance(), SIGNAL(mutedChanged(bool)), _ui.actionMuteAudioOutput, SLOT(setChecked(bool)));
    connect(_ui.actionMuteAudioOutput, SIGNAL(triggered(bool)), GAudioOutput::instance(), SLOT(mute(bool)));
856 857

    // Application Settings
dogmaphobic's avatar
dogmaphobic committed
858
    connect(_ui.actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
859

dogmaphobic's avatar
dogmaphobic committed
860 861 862
    connect(_ui.actionSimulate, SIGNAL(triggered(bool)), this, SLOT(simulateLink(bool)));

    // Update Tool Bar
863
    _mainToolBar->setCurrentView(_currentView);
864 865
}

Don Gagne's avatar
Don Gagne committed
866
void MainWindow::_openUrl(const QString& url, const QString& errorMessage)
867
{
Don Gagne's avatar
Don Gagne committed
868
    if(!QDesktopServices::openUrl(QUrl(url))) {
dogmaphobic's avatar
dogmaphobic committed
869 870 871 872
        QMessageBox::critical(
            this,
            tr("Could not open information in browser"),
            errorMessage);
873 874 875
    }
}

Don Gagne's avatar
Don Gagne committed
876 877
void MainWindow::showHelp()
{
dogmaphobic's avatar
dogmaphobic committed
878 879 880
    _openUrl(
        "http://qgroundcontrol.org/users/start",
        tr("To get to the online help, please open http://qgroundcontrol.org/user_guide in a browser."));
Don Gagne's avatar
Don Gagne committed
881 882
}

883 884
void MainWindow::showCredits()
{
dogmaphobic's avatar
dogmaphobic committed
885 886 887
    _openUrl(
        "http://qgroundcontrol.org/credits",
        tr("To get to the credits, please open http://qgroundcontrol.org/credits in a browser."));
888 889 890 891
}

void MainWindow::showRoadMap()
{
dogmaphobic's avatar
dogmaphobic committed
892 893 894
    _openUrl(
        "http://qgroundcontrol.org/dev/roadmap",
        tr("To get to the online help, please open http://qgroundcontrol.org/roadmap in a browser."));
895 896 897 898
}

void MainWindow::showSettings()
{
dogmaphobic's avatar
dogmaphobic committed
899
#ifndef __android__
Don Gagne's avatar
Don Gagne committed
900
    SettingsDialog settings(joystick, this);
dogmaphobic's avatar
dogmaphobic committed
901 902 903
#else
    SettingsDialog settings(this);
#endif
Don Gagne's avatar
Don Gagne committed
904
    settings.exec();
905 906
}

907
void MainWindow::simulateLink(bool simulate) {
Don Gagne's avatar
Don Gagne committed
908
    if (simulate) {
dogmaphobic's avatar
dogmaphobic committed
909 910 911
        if (!_simulationLink) {
            _simulationLink = new MAVLinkSimulationLink(":/demo-log.txt");
            Q_CHECK_PTR(_simulationLink);
Don Gagne's avatar
Don Gagne committed
912
        }
dogmaphobic's avatar
dogmaphobic committed
913
        LinkManager::instance()->connectLink(_simulationLink);
Don Gagne's avatar
Don Gagne committed
914
    } else {
dogmaphobic's avatar
dogmaphobic committed
915 916
        Q_ASSERT(_simulationLink);
        LinkManager::instance()->disconnectLink(_simulationLink);
Don Gagne's avatar
Don Gagne committed
917
    }
918 919
}

920 921
void MainWindow::commsWidgetDestroyed(QObject *obj)
{
922 923
    // Do not dynamic cast or de-reference QObject, since object is either in destructor or may have already
    // been destroyed.
dogmaphobic's avatar
dogmaphobic committed
924
    if (_commsWidgetList.contains(obj))
925
    {
dogmaphobic's avatar
dogmaphobic committed
926
        _commsWidgetList.removeOne(obj);
927 928
    }
}
929 930 931

void MainWindow::UASCreated(UASInterface* uas)
{
932
    // The UAS actions are not enabled without connection to system
dogmaphobic's avatar
dogmaphobic committed
933 934 935 936 937
    _ui.actionLiftoff->setEnabled(true);
    _ui.actionLand->setEnabled(true);
    _ui.actionEmergency_Kill->setEnabled(true);
    _ui.actionEmergency_Land->setEnabled(true);
    _ui.actionShutdownMAV->setEnabled(true);
938

John Tapsell's avatar
John Tapsell committed
939
    connect(uas, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)), this, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)));
940
    connect(uas, SIGNAL(misconfigurationDetected(UASInterface*)), this, SLOT(handleMisconfiguration(UASInterface*)));
941

942
    // HIL
943
    _showHILConfigurationWidgets();
944

945 946 947
    if (!linechartWidget)
    {
        linechartWidget = new Linecharts(this);
948
        linechartWidget->setVisible(false);
949
    }
950

951
    linechartWidget->addSource(mavlinkDecoder);
952
    if (_analyzeView != linechartWidget)
953
    {
954
        _analyzeView = linechartWidget;
955
    }
956 957
}

958 959
/// Stores the state of the toolbar, status bar and widgets associated with the current view
void MainWindow::_storeCurrentViewState(void)
960
{
961 962 963 964 965 966 967 968 969 970 971 972 973
    // HIL dock widgets are dynamic and are not part of the saved state
    _hideAllHilDockWidgets();
    // Save list of visible widgets
    bool firstWidget = true;
    QString widgetNames = "";
    foreach(QDockWidget* dockWidget, _mapName2DockWidget) {
        if (dockWidget->isVisible()) {
            if (!firstWidget) {
                widgetNames += ",";
            }
            widgetNames += dockWidget->objectName();
            firstWidget = false;
        }
974
    }
dogmaphobic's avatar
dogmaphobic committed
975 976 977
    settings.setValue(_getWindowStateKey() + "WIDGETS", widgetNames);
    settings.setValue(_getWindowStateKey(), saveState());
    settings.setValue(_getWindowGeometryKey(), saveGeometry());
978 979
}

980 981
/// Restores the state of the toolbar, status bar and widgets associated with the current view
void MainWindow::_loadCurrentViewState(void)
982
{
Don Gagne's avatar
Don Gagne committed
983
    QWidget* centerView = NULL;
984
    QString defaultWidgets;
985

986
    switch (_currentView) {
987
        case VIEW_SETUP:
988 989
            _buildSetupView();
            centerView = _setupView;
990
            break;
991

992 993 994
        case VIEW_ANALYZE:
            _buildAnalyzeView();
            centerView = _analyzeView;
995
            defaultWidgets = "MAVLINK_INSPECTOR_DOCKWIDGET,PARAMETER_INTERFACE_DOCKWIDGET,FILE_VIEW_DOCKWIDGET,HEAD_UP_DISPLAY_DOCKWIDGET";
996
            break;
997

998
        case VIEW_FLIGHT:
999 1000
            _buildFlightView();
            centerView = _flightView;
1001
            defaultWidgets = "COMMUNICATION_CONSOLE_DOCKWIDGET,UAS_INFO_INFOVIEW_DOCKWIDGET";
1002
            break;
1003

1004 1005 1006
        case VIEW_PLAN:
            _buildPlanView();
            centerView = _planView;
1007
            defaultWidgets = "UNMANNED_SYSTEM_LIST_DOCKWIDGET,WAYPOINT_LIST_DOCKWIDGET";
1008
            break;
1009

1010 1011 1012 1013 1014 1015
        case VIEW_EXPERIMENTAL_PLAN:
            _buildExperimentalPlanView();
            centerView = _experimentalPlanView;
            defaultWidgets.clear();
            break;

1016
        case VIEW_SIMULATION:
1017 1018 1019
            _buildSimView();
            centerView = _simView;
            defaultWidgets = "UNMANNED_SYSTEM_CONTROL_DOCKWIDGET,WAYPOINT_LIST_DOCKWIDGET,PARAMETER_INTERFACE_DOCKWIDGET,PRIMARY_FLIGHT_DISPLAY_DOCKWIDGET";
1020
            break;
1021

1022
        case VIEW_TERMINAL:
1023 1024
            _buildTerminalView();
            centerView = _terminalView;
1025
            break;
1026 1027 1028
            
        default:
            Q_ASSERT(false);
1029
            break;
1030
    }
1031

1032 1033 1034 1035 1036 1037 1038 1039
    // Remove old view
    if (_currentViewWidget) {
        _currentViewWidget->setVisible(false);
        Q_ASSERT(_centralLayout->count() == 1);
        QLayoutItem *child = _centralLayout->takeAt(0);
        Q_ASSERT(child);
        delete child;
    }
1040

1041 1042 1043 1044 1045
    // Add the new one
    Q_ASSERT(centerView);
    Q_ASSERT(_centralLayout->count() == 0);
    _currentViewWidget = centerView;
    _centralLayout->addWidget(_currentViewWidget);
1046
    _centralLayout->setContentsMargins(0, 0, 0, 0);
1047
    _currentViewWidget->setVisible(true);
1048

1049 1050 1051 1052
    // Hide all widgets from previous view
    _hideAllDockWidgets();

    // Restore the widgets for the new view
dogmaphobic's avatar
dogmaphobic committed
1053
    QString widgetNames = settings.value(_getWindowStateKey() + "WIDGETS", defaultWidgets).toString();
1054
    qDebug() << widgetNames;
1055 1056 1057 1058 1059
    if (!widgetNames.isEmpty()) {
        QStringList split = widgetNames.split(",");
        foreach (QString widgetName, split) {
            Q_ASSERT(!widgetName.isEmpty());
            _showDockWidget(widgetName, true);
1060 1061 1062
        }
    }

dogmaphobic's avatar
dogmaphobic committed
1063 1064
    if (settings.contains(_getWindowStateKey())) {
        restoreState(settings.value(_getWindowStateKey()).toByteArray());
1065
    }
1066

1067 1068 1069
    // HIL dock widget are dynamic and don't take part in the saved window state, so this
    // need to happen after we restore state
    _showHILConfigurationWidgets();
1070 1071 1072 1073

    // There is a bug in Qt where a Canvas element inside a QQuickWidget does not
    // receive update requests. Here we emit a signal for them to get repainted.
    emit repaintCanvas();
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
}

void MainWindow::_hideAllHilDockWidgets(void)
{
    foreach(QDockWidget* dockWidget, _mapUasId2HilDockWidget) {
        dockWidget->setVisible(false);
    }
}

void MainWindow::_hideAllDockWidgets(void)
{
    foreach(QDockWidget* dockWidget, _mapName2DockWidget) {
        dockWidget->setVisible(false);
1087
    }
1088
    _hideAllHilDockWidgets();
1089
}
1090 1091

void MainWindow::_showDockWidgetAction(bool show)
1092
{
1093 1094 1095
    QAction* action = dynamic_cast<QAction*>(QObject::sender());
    Q_ASSERT(action);
    _showDockWidget(action->data().toString(), show);
1096
}
1097

1098

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
void MainWindow::handleMisconfiguration(UASInterface* uas)
{
    static QTime lastTime;
    // We have to debounce this signal
    if (!lastTime.isValid()) {
        lastTime.start();
    } else {
        if (lastTime.elapsed() < 10000) {
            lastTime.start();
            return;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
1111 1112 1113 1114 1115 1116 1117
    // Ask user if they want to handle this now
    QMessageBox::StandardButton button =
        QGCMessageBox::question(
            tr("Missing or Invalid Onboard Configuration"),
            tr("The onboard system configuration is missing or incomplete. Do you want to resolve this now?"),
            QMessageBox::Ok | QMessageBox::Cancel,
            QMessageBox::Ok);
Don Gagne's avatar
Don Gagne committed
1118
    if (button == QMessageBox::Ok) {
dogmaphobic's avatar
dogmaphobic committed
1119
        // They want to handle it, make sure this system is selected
1120 1121
        UASManager::instance()->setActiveUAS(uas);
        // Flick to config view
1122
        loadSetupView();
1123 1124 1125
    }
}

1126
void MainWindow::loadAnalyzeView()
1127
{
1128
    if (_currentView != VIEW_ANALYZE)
1129
    {
1130
        _storeCurrentViewState();
1131 1132
        _currentView = VIEW_ANALYZE;
        _ui.actionAnalyze->setChecked(true);
1133
        _loadCurrentViewState();
1134 1135 1136
    }
}

1137
void MainWindow::loadPlanView()
1138
{
1139
    if (_currentView != VIEW_PLAN)
1140
    {
1141
        _storeCurrentViewState();
1142 1143
        _currentView = VIEW_PLAN;
        _ui.actionPlan->setChecked(true);
1144
        _loadCurrentViewState();
1145 1146
    }
}
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

void MainWindow::loadOldPlanView()
{
    if (_currentView != VIEW_EXPERIMENTAL_PLAN)
    {
        _storeCurrentViewState();
        _currentView = VIEW_EXPERIMENTAL_PLAN;
        _ui.actionExperimentalPlanView->setChecked(true);
        _loadCurrentViewState();
    }
}

1159
void MainWindow::loadSetupView()
1160
{
1161
    if (_currentView != VIEW_SETUP)
1162
    {
1163 1164
        _storeCurrentViewState();
        _currentView = VIEW_SETUP;
dogmaphobic's avatar
dogmaphobic committed
1165
        _ui.actionSetup->setChecked(true);
1166
        _loadCurrentViewState();
1167 1168 1169
    }
}

1170 1171
void MainWindow::loadTerminalView()
{
1172
    if (_currentView != VIEW_TERMINAL)
1173
    {
1174 1175
        _storeCurrentViewState();
        _currentView = VIEW_TERMINAL;
dogmaphobic's avatar
dogmaphobic committed
1176
        _ui.actionTerminalView->setChecked(true);
1177
        _loadCurrentViewState();
1178 1179 1180
    }
}

1181
void MainWindow::loadFlightView()
1182
{
1183
    if (_currentView != VIEW_FLIGHT)
1184
    {
1185 1186
        _storeCurrentViewState();
        _currentView = VIEW_FLIGHT;
1187
        _ui.actionFlight->setChecked(true);
1188
        _loadCurrentViewState();
1189 1190 1191
    }
}

1192 1193
void MainWindow::loadSimulationView()
{
1194
    if (_currentView != VIEW_SIMULATION)
1195
    {
1196 1197
        _storeCurrentViewState();
        _currentView = VIEW_SIMULATION;
dogmaphobic's avatar
dogmaphobic committed
1198
        _ui.actionSimulationView->setChecked(true);
1199
        _loadCurrentViewState();
1200 1201 1202
    }
}

Don Gagne's avatar
Don Gagne committed
1203
/// @brief Hides the spash screen if it is currently being shown
1204
void MainWindow::hideSplashScreen(void)
Don Gagne's avatar
Don Gagne committed
1205 1206 1207 1208 1209 1210 1211
{
    if (_splashScreen) {
        _splashScreen->hide();
        _splashScreen = NULL;
    }
}

1212 1213
void MainWindow::manageLinks()
{
dogmaphobic's avatar
dogmaphobic committed
1214
#ifndef __android__
1215
    SettingsDialog settings(joystick, this, SettingsDialog::ShowCommLinks);
dogmaphobic's avatar
dogmaphobic committed
1216 1217 1218
#else
    SettingsDialog settings(this, SettingsDialog::ShowCommLinks);
#endif
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    settings.exec();
}

/// @brief Saves the last used connection
void MainWindow::saveLastUsedConnection(const QString connection)
{
    QSettings settings;
    QString key(MAIN_SETTINGS_GROUP);
    key += "/LAST_CONNECTION";
    settings.setValue(key, connection);
}

/// @brief Restore (and connects) the last used connection (if any)
void MainWindow::restoreLastUsedConnection()
{
    // TODO This should check and see of the port/whatever is present
    // first. That is, if the last connection was to a PX4 on some serial
    // port, it should check and see if the port is present before making
    // the connection.
    QSettings settings;
    QString key(MAIN_SETTINGS_GROUP);
    key += "/LAST_CONNECTION";
    if(settings.contains(key)) {
1242
        QString connection = settings.value(key).toString();
1243
        // Create a link for it
1244
        LinkManager::instance()->createConnectedLink(connection);
1245 1246
    }
}
Don Gagne's avatar
Don Gagne committed
1247

1248 1249 1250 1251 1252
void MainWindow::_linkStateChange(LinkInterface*)
{
    emit repaintCanvas();
}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
void MainWindow::setPixelSizeFactor(double size) {
    if(size < 0.1) {
        size = 0.1;
    }
    _pixelFactor = size;
    emit pixelSizeChanged();
}

void MainWindow::setFontSizeFactor(double size) {
    if(size < 0.1) {
        size = 0.1;
    }
    _fontFactor = size;
    emit fontSizeChanged();
}

1269
#ifdef QGC_MOUSE_ENABLED_LINUX
1270 1271 1272
bool MainWindow::x11Event(XEvent *event)
{
    emit x11EventOccured(event);
1273
    return false;
1274
}
1275
#endif // QGC_MOUSE_ENABLED_LINUX
1276 1277 1278 1279 1280 1281 1282

#ifdef UNITTEST_BUILD
void MainWindow::_showQmlTestWidget(void)
{
    new QmlTestWidget();
}
#endif