MainWindow.cc 73.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

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

pixhawk's avatar
pixhawk committed
22 23 24 25
======================================================================*/

/**
 * @file
26
 *   @brief Implementation of class MainWindow
27
 *   @author Lorenz Meier <mail@qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
28 29 30 31 32 33 34 35 36 37
 */

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

38
#include "QGC.h"
pixhawk's avatar
pixhawk committed
39 40 41 42 43
#include "MAVLinkSimulationLink.h"
#include "SerialLink.h"
#include "UDPLink.h"
#include "MAVLinkProtocol.h"
#include "CommConfigurationWindow.h"
44
#include "QGCWaypointListMulti.h"
pixhawk's avatar
pixhawk committed
45 46
#include "MainWindow.h"
#include "JoystickWidget.h"
pixhawk's avatar
pixhawk committed
47
#include "GAudioOutput.h"
48
#include "QGCToolWidget.h"
49
#include "QGCMAVLinkLogPlayer.h"
50
#include "QGCSettingsWidget.h"
51
#include "QGCMapTool.h"
52

53
#ifdef QGC_OSG_ENABLED
54
#include "Q3DWidgetFactory.h"
55
#endif
pixhawk's avatar
pixhawk committed
56

lm's avatar
lm committed
57 58 59 60
// FIXME Move
#include "PxQuadMAV.h"
#include "SlugsMAV.h"

pixhawk's avatar
pixhawk committed
61

62
#include "LogCompressor.h"
pixhawk's avatar
pixhawk committed
63

64 65
MainWindow* MainWindow::instance()
{
66
    static MainWindow* _instance = 0;
67
    if(_instance == 0) {
68
        _instance = new MainWindow();
69

70
        /* Set the application as parent to ensure that this object
71
                 * will be destroyed when the main application exits */
72 73 74
        //_instance->setParent(qApp);
    }
    return _instance;
75 76
}

pixhawk's avatar
pixhawk committed
77 78 79 80 81 82 83
/**
* 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()
**/
84
MainWindow::MainWindow(QWidget *parent):
85 86 87 88 89 90 91
    QMainWindow(parent),
    toolsMenuActions(),
    currentView(VIEW_UNCONNECTED),
    aboutToCloseFlag(false),
    changingViewsFlag(false),
    styleFileName(QCoreApplication::applicationDirPath() + "/style-indoor.css"),
    autoReconnect(false),
92 93
    currentStyle(QGC_MAINWINDOW_STYLE_INDOOR),
    lowPowerMode(false)
pixhawk's avatar
pixhawk committed
94
{
95
    loadSettings();
96
    if (!settings.contains("CURRENT_VIEW")) {
97 98
        // Set this view as default view
        settings.setValue("CURRENT_VIEW", currentView);
99
    } else {
100 101 102
        // LOAD THE LAST VIEW
        VIEW_SECTIONS currentViewCandidate = (VIEW_SECTIONS) settings.value("CURRENT_VIEW", currentView).toInt();
        if (currentViewCandidate != VIEW_ENGINEER &&
103 104
                currentViewCandidate != VIEW_OPERATOR &&
                currentViewCandidate != VIEW_PILOT) {
105
            currentView = currentViewCandidate;
106
        }
107 108
    }

109
    setDefaultSettingsForAp();
110

111 112
    settings.sync();

pixhawk's avatar
pixhawk committed
113 114 115
    // Setup user interface
    ui.setupUi(this);

116 117
    setVisible(false);

118
    buildCommonWidgets();
119

120
    connectCommonWidgets();
121

122
    arrangeCommonCenterStack();
123 124

    configureWindowName();
pixhawk's avatar
pixhawk committed
125

126
    loadStyle(currentStyle);
127

128
    // Create actions
129
    connectCommonActions();
130

131 132 133
    // Set dock options
    setDockOptions(AnimatedDocks | AllowTabbedDocks | AllowNestedDocks);

134
    // Load mavlink view as default widget set
135
    //loadMAVLinkView();
136

lm's avatar
lm committed
137 138
    statusBar()->setSizeGripEnabled(true);

139
    // Restore the window position and size
140
    if (settings.contains(getWindowGeometryKey())) {
141
        // Restore the window geometry
142
        restoreGeometry(settings.value(getWindowGeometryKey()).toByteArray());
143
    } else {
144 145 146
        // Adjust the size
        adjustSize();
    }
pixhawk's avatar
pixhawk committed
147

148 149
    // Populate link menu
    QList<LinkInterface*> links = LinkManager::instance()->getLinks();
150
    foreach(LinkInterface* link, links) {
151 152
        this->addLink(link);
    }
153

154 155
    connect(LinkManager::instance(), SIGNAL(newLink(LinkInterface*)), this, SLOT(addLink(LinkInterface*)));

156
    // Connect user interface devices
157 158
    joystickWidget = 0;
    joystick = new JoystickInput();
159

160 161 162 163 164 165 166 167
    // Load Toolbar
    toolBar = new QGCToolBar(this);
    this->addToolBar(toolBar);
    // Add actions
    toolBar->addPerspectiveChangeAction(ui.actionOperatorsView);
    toolBar->addPerspectiveChangeAction(ui.actionEngineersView);
    toolBar->addPerspectiveChangeAction(ui.actionPilotsView);

lm's avatar
lm committed
168 169
    // Enable and update view
    presentView();
170 171

    // Connect link
172
    if (autoReconnect) {
173 174 175 176 177 178
        SerialLink* link = new SerialLink();
        // Add to registry
        LinkManager::instance()->add(link);
        LinkManager::instance()->addProtocol(link, mavlink);
        link->connect();
    }
179

180 181 182
    // Set low power mode
    enableLowPowerMode(lowPowerMode);

183 184
    // Initialize window state
    windowStateVal = windowState();
pixhawk's avatar
pixhawk committed
185 186
}

pixhawk's avatar
pixhawk committed
187
MainWindow::~MainWindow()
pixhawk's avatar
pixhawk committed
188
{
189 190 191
    // Store settings
    storeSettings();

192
    delete mavlink;
193
    delete joystick;
lm's avatar
lm committed
194

195 196 197 198 199 200
    // Get and delete all dockwidgets and contained
    // widgets
    QObjectList childList( this->children() );

    QObjectList::iterator i;
    QDockWidget* dockWidget;
201
    for (i = childList.begin(); i != childList.end(); ++i) {
202
        dockWidget = dynamic_cast<QDockWidget*>(*i);
203
        if (dockWidget) {
204 205 206 207 208 209
            // Remove dock widget from main window
            removeDockWidget(dockWidget);
            delete dockWidget->widget();
            delete dockWidget;
        }
    }
pixhawk's avatar
pixhawk committed
210 211
}

212 213 214 215 216 217 218 219 220
/**
 * Set default settings for this AP type.
 */
void MainWindow::setDefaultSettingsForAp()
{
    // Check if the settings exist, instantiate defaults if necessary

    // UNCONNECTED VIEW DEFAULT
    QString centralKey = buildMenuKey(SUB_SECTION_CHECKED, CENTRAL_MAP, VIEW_UNCONNECTED);
221
    if (!settings.contains(centralKey)) {
222
        settings.setValue(centralKey,true);
223 224 225 226 227

        // ENABLE UAS LIST
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_UAS_LIST, VIEW_UNCONNECTED), true);
        // ENABLE COMMUNICATION CONSOLE
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_DEBUG_CONSOLE, VIEW_UNCONNECTED), true);
228 229 230 231
    }

    // OPERATOR VIEW DEFAULT
    centralKey = buildMenuKey(SUB_SECTION_CHECKED, CENTRAL_MAP, VIEW_OPERATOR);
232
    if (!settings.contains(centralKey)) {
233 234 235 236
        settings.setValue(centralKey,true);

        // ENABLE UAS LIST
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_UAS_LIST,VIEW_OPERATOR), true);
237 238
        // ENABLE HUD TOOL WIDGET
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_HUD,VIEW_OPERATOR), true);
239 240
        // ENABLE WAYPOINTS
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_WAYPOINTS,VIEW_OPERATOR), true);
241 242 243 244
    }

    // ENGINEER VIEW DEFAULT
    centralKey = buildMenuKey(SUB_SECTION_CHECKED, CENTRAL_LINECHART, VIEW_ENGINEER);
245
    if (!settings.contains(centralKey)) {
246
        settings.setValue(centralKey,true);
247 248
        // Enable Parameter widget
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_PARAMETERS,VIEW_ENGINEER), true);
249 250 251 252
    }

    // MAVLINK VIEW DEFAULT
    centralKey = buildMenuKey(SUB_SECTION_CHECKED, CENTRAL_PROTOCOL, VIEW_MAVLINK);
253
    if (!settings.contains(centralKey)) {
254 255 256 257 258
        settings.setValue(centralKey,true);
    }

    // PILOT VIEW DEFAULT
    centralKey = buildMenuKey(SUB_SECTION_CHECKED, CENTRAL_HUD, VIEW_PILOT);
259
    if (!settings.contains(centralKey)) {
260
        settings.setValue(centralKey,true);
261 262
        // Enable Flight display
        settings.setValue(buildMenuKey(SUB_SECTION_CHECKED,MainWindow::MENU_HDD_1,VIEW_PILOT), true);
263 264 265
    }
}

lm's avatar
lm committed
266 267 268
void MainWindow::resizeEvent(QResizeEvent * event)
{
    Q_UNUSED(event);
269
    if (height() < 800) {
lm's avatar
lm committed
270
        ui.statusBar->setVisible(false);
271
    } else {
lm's avatar
lm committed
272
        ui.statusBar->setVisible(true);
273
        ui.statusBar->setSizeGripEnabled(true);
lm's avatar
lm committed
274 275 276
    }
}

277 278
QString MainWindow::getWindowStateKey()
{
279
    return QString::number(currentView)+"_windowstate";
280 281 282 283
}

QString MainWindow::getWindowGeometryKey()
{
284 285
    //return QString::number(currentView)+"_geometry";
    return "_geometry";
286 287
}

lm's avatar
lm committed
288 289 290
void MainWindow::buildCustomWidget()
{
    // Show custom widgets only if UAS is connected
291
    if (UASManager::instance()->getActiveUAS() != NULL) {
lm's avatar
lm committed
292 293 294 295 296 297
        // Enable custom widgets
        ui.actionNewCustomWidget->setEnabled(true);

        // Create custom widgets
        QList<QGCToolWidget*> widgets = QGCToolWidget::createWidgetsFromSettings(this);

298
        if (widgets.size() > 0) {
lm's avatar
lm committed
299 300 301
            ui.menuTools->addSeparator();
        }

302
        for(int i = 0; i < widgets.size(); ++i) {
lm's avatar
lm committed
303
            // Check if this widget already has a parent, do not create it in this case
304 305
            QGCToolWidget* tool = widgets.at(i);
            QDockWidget* dock = dynamic_cast<QDockWidget*>(tool->parentWidget());
306
            if (!dock) {
307 308 309 310
                QDockWidget* dock = new QDockWidget(tool->windowTitle(), this);
                dock->setObjectName(tool->objectName()+"_DOCK");
                dock->setWidget(tool);
                connect(tool, SIGNAL(destroyed()), dock, SLOT(deleteLater()));
lm's avatar
lm committed
311
                QAction* showAction = new QAction(widgets.at(i)->windowTitle(), this);
312
                showAction->setCheckable(true);
lm's avatar
lm committed
313 314 315 316
                connect(showAction, SIGNAL(triggered(bool)), dock, SLOT(setVisible(bool)));
                connect(dock, SIGNAL(visibilityChanged(bool)), showAction, SLOT(setChecked(bool)));
                widgets.at(i)->setMainMenuAction(showAction);
                ui.menuTools->addAction(showAction);
317 318 319 320 321 322 323 324

                // Load visibility for view (default is off)
                dock->setVisible(tool->isVisible(currentView));

                // Load dock widget location (default is bottom)
                Qt::DockWidgetArea location = static_cast <Qt::DockWidgetArea>(tool->getDockWidgetArea(currentView));

                addDockWidget(location, dock);
lm's avatar
lm committed
325 326 327 328 329
            }
        }
    }
}

330 331 332 333
void MainWindow::buildCommonWidgets()
{
    //TODO:  move protocol outside UI
    mavlink     = new MAVLinkProtocol();
334
    connect(mavlink, SIGNAL(protocolStatusMessage(QString,QString)), this, SLOT(showCriticalMessage(QString,QString)), Qt::QueuedConnection);
335 336

    // Dock widgets
337
    if (!controlDockWidget) {
338
        controlDockWidget = new QDockWidget(tr("Control"), this);
339
        controlDockWidget->setObjectName("UNMANNED_SYSTEM_CONTROL_DOCKWIDGET");
340
        controlDockWidget->setWidget( new UASControlWidget(this) );
341
        addToToolsMenu (controlDockWidget, tr("Control"), SLOT(showToolWidget(bool)), MENU_UAS_CONTROL, Qt::LeftDockWidgetArea);
342
    }
343

344
    if (!listDockWidget) {
345 346
        listDockWidget = new QDockWidget(tr("Unmanned Systems"), this);
        listDockWidget->setWidget( new UASListWidget(this) );
347
        listDockWidget->setObjectName("UNMANNED_SYSTEMS_LIST_DOCKWIDGET");
348
        addToToolsMenu (listDockWidget, tr("Unmanned Systems"), SLOT(showToolWidget(bool)), MENU_UAS_LIST, Qt::RightDockWidgetArea);
349
    }
350

351
    if (!waypointsDockWidget) {
352
        waypointsDockWidget = new QDockWidget(tr("Mission Plan"), this);
353
        waypointsDockWidget->setWidget( new QGCWaypointListMulti(this) );
354
        waypointsDockWidget->setObjectName("WAYPOINT_LIST_DOCKWIDGET");
355
        addToToolsMenu (waypointsDockWidget, tr("Mission Plan"), SLOT(showToolWidget(bool)), MENU_WAYPOINTS, Qt::BottomDockWidgetArea);
356
    }
357

358
    if (!infoDockWidget) {
359 360
        infoDockWidget = new QDockWidget(tr("Status Details"), this);
        infoDockWidget->setWidget( new UASInfoWidget(this) );
pixhawk's avatar
pixhawk committed
361
        infoDockWidget->setObjectName("UAS_STATUS_DETAILS_DOCKWIDGET");
362
        addToToolsMenu (infoDockWidget, tr("Status Details"), SLOT(showToolWidget(bool)), MENU_STATUS, Qt::RightDockWidgetArea);
363
    }
364

365
    if (!debugConsoleDockWidget) {
366 367
        debugConsoleDockWidget = new QDockWidget(tr("Communication Console"), this);
        debugConsoleDockWidget->setWidget( new DebugConsole(this) );
368
        debugConsoleDockWidget->setObjectName("COMMUNICATION_DEBUG_CONSOLE_DOCKWIDGET");
369
        addToToolsMenu (debugConsoleDockWidget, tr("Communication Console"), SLOT(showToolWidget(bool)), MENU_DEBUG_CONSOLE, Qt::BottomDockWidgetArea);
370
    }
371

372
    if (!logPlayerDockWidget) {
373 374 375
        logPlayerDockWidget = new QDockWidget(tr("MAVLink Log Player"), this);
        logPlayerDockWidget->setWidget( new QGCMAVLinkLogPlayer(mavlink, this) );
        logPlayerDockWidget->setObjectName("MAVLINK_LOG_PLAYER_DOCKWIDGET");
376
        addToToolsMenu(logPlayerDockWidget, tr("MAVLink Log Replay"), SLOT(showToolWidget(bool)), MENU_MAVLINK_LOG_PLAYER, Qt::RightDockWidgetArea);
377 378
    }

379
    // Center widgets
380 381
    if (!mapWidget)
    {
382
        mapWidget = new QGCMapTool(this);
383 384
        addToCentralWidgetsMenu (mapWidget, "Maps", SLOT(showCentralWidget()),CENTRAL_MAP);
    }
385

386
    if (!protocolWidget) {
387 388 389
        protocolWidget    = new XMLCommProtocolWidget(this);
        addToCentralWidgetsMenu (protocolWidget, "Mavlink Generator", SLOT(showCentralWidget()),CENTRAL_PROTOCOL);
    }
lm's avatar
lm committed
390

391
    if (!dataplotWidget) {
lm's avatar
lm committed
392
        dataplotWidget    = new QGCDataPlot2D(this);
393
        addToCentralWidgetsMenu (dataplotWidget, "Logfile Plot", SLOT(showCentralWidget()),CENTRAL_DATA_PLOT);
lm's avatar
lm committed
394
    }
395

396

397
}
398

399

400
void MainWindow::buildPxWidgets()
401
{
pixhawk's avatar
pixhawk committed
402 403
    //FIXME: memory of acceptList will never be freed again
    QStringList* acceptList = new QStringList();
pixhawk's avatar
pixhawk committed
404 405 406
    acceptList->append("-105,roll deg,deg,+105,s");
    acceptList->append("-105,pitch deg,deg,+105,s");
    acceptList->append("-105,heading deg,deg,+105,s");
407

pixhawk's avatar
pixhawk committed
408 409 410
    acceptList->append("-60,rollspeed d/s,deg/s,+60,s");
    acceptList->append("-60,pitchspeed d/s,deg/s,+60,s");
    acceptList->append("-60,yawspeed d/s,deg/s,+60,s");
411
    acceptList->append("0,airspeed,m/s,30");
412 413
    acceptList->append("0,groundspeed,m/s,30");
    acceptList->append("0,climbrate,m/s,30");
414
    acceptList->append("0,throttle,%,100");
415

pixhawk's avatar
pixhawk committed
416 417
    //FIXME: memory of acceptList2 will never be freed again
    QStringList* acceptList2 = new QStringList();
418 419 420 421 422 423 424 425
    acceptList2->append("900,servo #1,us,2100,s");
    acceptList2->append("900,servo #2,us,2100,s");
    acceptList2->append("900,servo #3,us,2100,s");
    acceptList2->append("900,servo #4,us,2100,s");
    acceptList2->append("900,servo #5,us,2100,s");
    acceptList2->append("900,servo #6,us,2100,s");
    acceptList2->append("900,servo #7,us,2100,s");
    acceptList2->append("900,servo #8,us,2100,s");
lm's avatar
lm committed
426
    acceptList2->append("0,abs pressure,hPa,65500");
427 428 429
    //acceptList2->append("-2048,accel. x,raw,2048,s");
    //acceptList2->append("-2048,accel. y,raw,2048,s");
    //acceptList2->append("-2048,accel. z,raw,2048,s");
430

431
    if (!linechartWidget) {
432 433
        // Center widgets
        linechartWidget   = new Linecharts(this);
434
        addToCentralWidgetsMenu(linechartWidget, tr("Realtime Plot"), SLOT(showCentralWidget()), CENTRAL_LINECHART);
435
    }
436 437


438
    if (!hudWidget) {
439
        hudWidget         = new HUD(320, 240, this);
440
        addToCentralWidgetsMenu(hudWidget, tr("Head Up Display"), SLOT(showCentralWidget()), CENTRAL_HUD);
441
    }
442

443
    if (!dataplotWidget) {
444
        dataplotWidget    = new QGCDataPlot2D(this);
445
        addToCentralWidgetsMenu(dataplotWidget, "Logfile Plot", SLOT(showCentralWidget()), CENTRAL_DATA_PLOT);
446
    }
447

448
#ifdef QGC_OSG_ENABLED
449
    if (!_3DWidget) {
450
        _3DWidget         = Q3DWidgetFactory::get("PIXHAWK");
451
        addToCentralWidgetsMenu(_3DWidget, tr("Local 3D"), SLOT(showCentralWidget()), CENTRAL_3D_LOCAL);
452
    }
453
#endif
454

455
#ifdef QGC_OSGEARTH_ENABLED
456
    if (!_3DMapWidget) {
457
        _3DMapWidget = Q3DWidgetFactory::get("MAP3D");
458
        addToCentralWidgetsMenu(_3DMapWidget, tr("OSG Earth 3D"), SLOT(showCentralWidget()), CENTRAL_OSGEARTH);
459
    }
460
#endif
lm's avatar
lm committed
461

462
#if (defined _MSC_VER) | (defined Q_OS_MAC)
463
    if (!gEarthWidget) {
464
        gEarthWidget = new QGCGoogleEarthView(this);
465
        addToCentralWidgetsMenu(gEarthWidget, tr("Google Earth"), SLOT(showCentralWidget()), CENTRAL_GOOGLE_EARTH);
466
    }
467

468
#endif
469

pixhawk's avatar
pixhawk committed
470
    // Dock widgets
471

472
    if (!detectionDockWidget) {
473 474
        detectionDockWidget = new QDockWidget(tr("Object Recognition"), this);
        detectionDockWidget->setWidget( new ObjectDetectionView("images/patterns", this) );
pixhawk's avatar
pixhawk committed
475
        detectionDockWidget->setObjectName("OBJECT_DETECTION_DOCK_WIDGET");
476
        addToToolsMenu (detectionDockWidget, tr("Object Recognition"), SLOT(showToolWidget(bool)), MENU_DETECTION, Qt::RightDockWidgetArea);
477
    }
478

479
    if (!parametersDockWidget) {
480
        parametersDockWidget = new QDockWidget(tr("Calibration and Onboard Parameters"), this);
481
        parametersDockWidget->setWidget( new ParameterInterface(this) );
pixhawk's avatar
pixhawk committed
482
        parametersDockWidget->setObjectName("PARAMETER_INTERFACE_DOCKWIDGET");
483
        addToToolsMenu (parametersDockWidget, tr("Calibration and Parameters"), SLOT(showToolWidget(bool)), MENU_PARAMETERS, Qt::RightDockWidgetArea);
484
    }
485

486
    if (!watchdogControlDockWidget) {
487 488
        watchdogControlDockWidget = new QDockWidget(tr("Process Control"), this);
        watchdogControlDockWidget->setWidget( new WatchdogControl(this) );
pixhawk's avatar
pixhawk committed
489
        watchdogControlDockWidget->setObjectName("WATCHDOG_CONTROL_DOCKWIDGET");
490
        addToToolsMenu (watchdogControlDockWidget, tr("Process Control"), SLOT(showToolWidget(bool)), MENU_WATCHDOG, Qt::BottomDockWidgetArea);
491
    }
492

493
    if (!hsiDockWidget) {
494 495
        hsiDockWidget = new QDockWidget(tr("Horizontal Situation Indicator"), this);
        hsiDockWidget->setWidget( new HSIDisplay(this) );
496
        hsiDockWidget->setObjectName("HORIZONTAL_SITUATION_INDICATOR_DOCK_WIDGET");
497
        addToToolsMenu (hsiDockWidget, tr("Horizontal Situation"), SLOT(showToolWidget(bool)), MENU_HSI, Qt::BottomDockWidgetArea);
498
    }
499

500
    if (!headDown1DockWidget) {
501 502
        headDown1DockWidget = new QDockWidget(tr("Flight Display"), this);
        headDown1DockWidget->setWidget( new HDDisplay(acceptList, "Flight Display", this) );
503
        headDown1DockWidget->setObjectName("HEAD_DOWN_DISPLAY_1_DOCK_WIDGET");
504
        addToToolsMenu (headDown1DockWidget, tr("Flight Display"), SLOT(showToolWidget(bool)), MENU_HDD_1, Qt::RightDockWidgetArea);
505
    }
506

507
    if (!headDown2DockWidget) {
508 509
        headDown2DockWidget = new QDockWidget(tr("Actuator Status"), this);
        headDown2DockWidget->setWidget( new HDDisplay(acceptList2, "Actuator Status", this) );
510
        headDown2DockWidget->setObjectName("HEAD_DOWN_DISPLAY_2_DOCK_WIDGET");
511
        addToToolsMenu (headDown2DockWidget, tr("Actuator Status"), SLOT(showToolWidget(bool)), MENU_HDD_2, Qt::RightDockWidgetArea);
512
    }
513

514
    if (!rcViewDockWidget) {
515 516
        rcViewDockWidget = new QDockWidget(tr("Radio Control"), this);
        rcViewDockWidget->setWidget( new QGCRemoteControlView(this) );
517
        rcViewDockWidget->setObjectName("RADIO_CONTROL_CHANNELS_DOCK_WIDGET");
518
        addToToolsMenu (rcViewDockWidget, tr("Radio Control"), SLOT(showToolWidget(bool)), MENU_RC_VIEW, Qt::BottomDockWidgetArea);
519
    }
520

521
    if (!headUpDockWidget) {
522 523
        headUpDockWidget = new QDockWidget(tr("HUD"), this);
        headUpDockWidget->setWidget( new HUD(320, 240, this));
524
        headUpDockWidget->setObjectName("HEAD_UP_DISPLAY_DOCK_WIDGET");
525
        addToToolsMenu (headUpDockWidget, tr("Head Up Display"), SLOT(showToolWidget(bool)), MENU_HUD, Qt::RightDockWidgetArea);
526
    }
527

528
    if (!video1DockWidget) {
pixhawk's avatar
pixhawk committed
529 530 531 532 533 534 535
        video1DockWidget = new QDockWidget(tr("Video Stream 1"), this);
        HUD* video1 =  new HUD(160, 120, this);
        video1->enableHUDInstruments(false);
        video1->enableVideo(true);
        // FIXME select video stream as well
        video1DockWidget->setWidget(video1);
        video1DockWidget->setObjectName("VIDEO_STREAM_1_DOCK_WIDGET");
536
        addToToolsMenu (video1DockWidget, tr("Video Stream 1"), SLOT(showToolWidget(bool)), MENU_VIDEO_STREAM_1, Qt::LeftDockWidgetArea);
pixhawk's avatar
pixhawk committed
537 538
    }

539
    if (!video2DockWidget) {
pixhawk's avatar
pixhawk committed
540 541 542 543 544 545 546
        video2DockWidget = new QDockWidget(tr("Video Stream 2"), this);
        HUD* video2 =  new HUD(160, 120, this);
        video2->enableHUDInstruments(false);
        video2->enableVideo(true);
        // FIXME select video stream as well
        video2DockWidget->setWidget(video2);
        video2DockWidget->setObjectName("VIDEO_STREAM_2_DOCK_WIDGET");
547
        addToToolsMenu (video2DockWidget, tr("Video Stream 2"), SLOT(showToolWidget(bool)), MENU_VIDEO_STREAM_2, Qt::LeftDockWidgetArea);
548
    }
549

pixhawk's avatar
pixhawk committed
550 551
    // Dialogue widgets
    //FIXME: free memory in destructor
552 553 554 555
}

void MainWindow::buildSlugsWidgets()
{
556
    if (!linechartWidget) {
557 558
        // Center widgets
        linechartWidget   = new Linecharts(this);
559
        addToCentralWidgetsMenu(linechartWidget, tr("Realtime Plot"), SLOT(showCentralWidget()), CENTRAL_LINECHART);
560
    }
561

562
    if (!headUpDockWidget) {
563 564 565
        // Dock widgets
        headUpDockWidget = new QDockWidget(tr("Control Indicator"), this);
        headUpDockWidget->setWidget( new HUD(320, 240, this));
pixhawk's avatar
pixhawk committed
566
        headUpDockWidget->setObjectName("HEAD_UP_DISPLAY_DOCK_WIDGET");
567
        addToToolsMenu (headUpDockWidget, tr("Head Up Display"), SLOT(showToolWidget(bool)), MENU_HUD, Qt::LeftDockWidgetArea);
568
    }
569

570
    if (!rcViewDockWidget) {
571 572
        rcViewDockWidget = new QDockWidget(tr("Radio Control"), this);
        rcViewDockWidget->setWidget( new QGCRemoteControlView(this) );
pixhawk's avatar
pixhawk committed
573
        rcViewDockWidget->setObjectName("RADIO_CONTROL_CHANNELS_DOCK_WIDGET");
574
        addToToolsMenu (rcViewDockWidget, tr("Radio Control"), SLOT(showToolWidget(bool)), MENU_RC_VIEW, Qt::BottomDockWidgetArea);
575
    }
576

577
#if (defined _MSC_VER) | (defined Q_OS_MAC)
578
    if (!gEarthWidget) {
579 580 581 582 583 584
        gEarthWidget = new QGCGoogleEarthView(this);
        addToCentralWidgetsMenu(gEarthWidget, tr("Google Earth"), SLOT(showCentralWidget()), CENTRAL_GOOGLE_EARTH);
    }

#endif

585
    if (!slugsDataWidget) {
586 587 588 589 590 591
        // Dialog widgets
        slugsDataWidget = new QDockWidget(tr("Slugs Data"), this);
        slugsDataWidget->setWidget( new SlugsDataSensorView(this));
        slugsDataWidget->setObjectName("SLUGS_DATA_DOCK_WIDGET");
        addToToolsMenu (slugsDataWidget, tr("Telemetry Data"), SLOT(showToolWidget(bool)), MENU_SLUGS_DATA, Qt::RightDockWidgetArea);
    }
592

593

594
    if (!slugsHilSimWidget) {
595 596 597 598 599
        slugsHilSimWidget = new QDockWidget(tr("Slugs Hil Sim"), this);
        slugsHilSimWidget->setWidget( new SlugsHilSim(this));
        slugsHilSimWidget->setObjectName("SLUGS_HIL_SIM_DOCK_WIDGET");
        addToToolsMenu (slugsHilSimWidget, tr("HIL Sim Configuration"), SLOT(showToolWidget(bool)), MENU_SLUGS_HIL, Qt::LeftDockWidgetArea);
    }
600

601 602
    if (!controlParameterWidget) {
        controlParameterWidget = new QDockWidget(tr("Control Parameters"), this);
603 604 605
        controlParameterWidget->setObjectName("UNMANNED_SYSTEM_CONTROL_PARAMETERWIDGET");
        controlParameterWidget->setWidget( new UASControlParameters(this) );
        addToToolsMenu (controlParameterWidget, tr("Control Parameters"), SLOT(showToolWidget(bool)), MENU_UAS_CONTROL_PARAM, Qt::LeftDockWidgetArea);
606
    }
607

608
    if (!parametersDockWidget) {
609 610 611 612 613 614
        parametersDockWidget = new QDockWidget(tr("Calibration and Onboard Parameters"), this);
        parametersDockWidget->setWidget( new ParameterInterface(this) );
        parametersDockWidget->setObjectName("PARAMETER_INTERFACE_DOCKWIDGET");
        addToToolsMenu (parametersDockWidget, tr("Calibration and Parameters"), SLOT(showToolWidget(bool)), MENU_PARAMETERS, Qt::RightDockWidgetArea);
    }

615
    if (!slugsCamControlWidget) {
616 617 618 619 620
        slugsCamControlWidget = new QDockWidget(tr("Camera Control"), this);
        slugsCamControlWidget->setWidget(new SlugsPadCameraControl(this));
        slugsCamControlWidget->setObjectName("SLUGS_CAM_CONTROL_DOCK_WIDGET");
        addToToolsMenu (slugsCamControlWidget, tr("Camera Control"), SLOT(showToolWidget(bool)), MENU_SLUGS_CAMERA, Qt::BottomDockWidgetArea);
    }
621

622
}
623 624 625


void MainWindow::addToCentralWidgetsMenu ( QWidget* widget,
626 627 628
        const QString title,
        const char * slotName,
        TOOLS_WIDGET_NAMES centralWidget)
629
{
630
    QAction* tempAction;
631

632

633
// Not needed any more - separate menu now available
634

635 636 637 638 639 640 641
//    // Add the separator that will separate tools from central Widgets
//    if (!toolsMenuActions[CENTRAL_SEPARATOR])
//    {
//        tempAction = ui.menuTools->addSeparator();
//        toolsMenuActions[CENTRAL_SEPARATOR] = tempAction;
//        tempAction->setData(CENTRAL_SEPARATOR);
//    }
642

643
    tempAction = ui.menuMain->addAction(title);
644

645 646
    tempAction->setCheckable(true);
    tempAction->setData(centralWidget);
647

648 649 650
    // populate the Hashes
    toolsMenuActions[centralWidget] = tempAction;
    dockWidgets[centralWidget] = widget;
651

652
    QString chKey = buildMenuKey(SUB_SECTION_CHECKED, centralWidget, currentView);
653

654
    if (!settings.contains(chKey)) {
655 656
        settings.setValue(chKey,false);
        tempAction->setChecked(false);
657
    } else {
658 659
        tempAction->setChecked(settings.value(chKey).toBool());
    }
660

661
    // connect the action
662
    connect(tempAction,SIGNAL(triggered(bool)),this, slotName);
663 664 665
}


lm's avatar
lm committed
666 667
void MainWindow::showCentralWidget()
{
668
    QAction* senderAction = qobject_cast<QAction *>(sender());
669 670 671 672

    // Block sender action while manipulating state
    senderAction->blockSignals(true);

673 674
    int tool = senderAction->data().toInt();
    QString chKey;
675

676
    // check the current action
677

678
    if (senderAction && dockWidgets[tool]) {
679 680
        // uncheck all central widget actions
        QHashIterator<int, QAction*> i(toolsMenuActions);
681
        while (i.hasNext()) {
682
            i.next();
683
            //qDebug() << "shCW" << i.key() << "read";
684
            if (i.value() && i.value()->data().toInt() > 255) {
685 686 687
                // Block signals and uncheck action
                // firing would be unneccesary
                i.value()->blockSignals(true);
688
                i.value()->setChecked(false);
689
                i.value()->blockSignals(false);
690 691 692 693 694 695

                // update the settings
                chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast<TOOLS_WIDGET_NAMES>(i.value()->data().toInt()), currentView);
                settings.setValue(chKey,false);
            }
        }
696

697
        // check the current action
698
        //qDebug() << senderAction->text();
699
        senderAction->setChecked(true);
700

701 702
        // update the central widget
        centerStack->setCurrentWidget(dockWidgets[tool]);
703

704 705 706
        // store the selected central widget
        chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast<TOOLS_WIDGET_NAMES>(tool), currentView);
        settings.setValue(chKey,true);
707

708 709 710
        // Unblock sender action
        senderAction->blockSignals(false);

711 712
        presentView();
    }
713
}
714

lm's avatar
lm committed
715 716 717 718
/**
 * Adds a widget to the tools menu and sets it visible if it was
 * enabled last time.
 */
719
void MainWindow::addToToolsMenu ( QWidget* widget,
720 721 722 723
                                  const QString title,
                                  const char * slotName,
                                  TOOLS_WIDGET_NAMES tool,
                                  Qt::DockWidgetArea location)
lm's avatar
lm committed
724
{
725 726
    QAction* tempAction;
    QString posKey, chKey;
727

728

729
    if (toolsMenuActions[CENTRAL_SEPARATOR]) {
730 731 732
        tempAction = new QAction(title, this);
        ui.menuTools->insertAction(toolsMenuActions[CENTRAL_SEPARATOR],
                                   tempAction);
733
    } else {
734 735
        tempAction = ui.menuTools->addAction(title);
    }
736

737 738
    tempAction->setCheckable(true);
    tempAction->setData(tool);
739

740 741 742
    // populate the Hashes
    toolsMenuActions[tool] = tempAction;
    dockWidgets[tool] = widget;
743
    //qDebug() << widget;
744

745
    posKey = buildMenuKey (SUB_SECTION_LOCATION,tool, currentView);
746

747
    if (!settings.contains(posKey)) {
748 749
        settings.setValue(posKey,location);
        dockWidgetLocations[tool] = location;
750
    } else {
751
        dockWidgetLocations[tool] = static_cast <Qt::DockWidgetArea> (settings.value(posKey, Qt::RightDockWidgetArea).toInt());
752
    }
753

754
    chKey = buildMenuKey(SUB_SECTION_CHECKED,tool, currentView);
755

756
    if (!settings.contains(chKey)) {
757 758
        settings.setValue(chKey,false);
        tempAction->setChecked(false);
759
        widget->setVisible(false);
760
    } else {
761
        tempAction->setChecked(settings.value(chKey, false).toBool());
762
        widget->setVisible(settings.value(chKey, false).toBool());
763
    }
764

765
    // connect the action
766 767 768
    connect(tempAction,SIGNAL(toggled(bool)),this, slotName);

    connect(qobject_cast <QDockWidget *>(dockWidgets[tool]),
769
            SIGNAL(visibilityChanged(bool)), this, SLOT(showToolWidget(bool)));
770

771 772
    //  connect(qobject_cast <QDockWidget *>(dockWidgets[tool]),
    //          SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibilitySettings(bool)));
773

774 775
    connect(qobject_cast <QDockWidget *>(dockWidgets[tool]),
            SIGNAL(dockLocationChanged(Qt::DockWidgetArea)), this, SLOT(updateLocationSettings(Qt::DockWidgetArea)));
776 777
}

778
void MainWindow::showToolWidget(bool visible)
779
{
780
    if (!aboutToCloseFlag && !changingViewsFlag) {
781 782
        QAction* action = qobject_cast<QAction *>(sender());

783
        // Prevent this to fire if undocked
784
        if (action) {
785
            int tool = action->data().toInt();
786

787
            QDockWidget* dockWidget = qobject_cast<QDockWidget *> (dockWidgets[tool]);
788

789 790
            if (dockWidget && dockWidget->isVisible() != visible) {
                if (visible) {
791 792
                    addDockWidget(dockWidgetLocations[tool], dockWidget);
                    dockWidget->show();
793
                } else {
794 795
                    removeDockWidget(dockWidget);
                }
796 797

                QHashIterator<int, QWidget*> i(dockWidgets);
798
                while (i.hasNext()) {
799
                    i.next();
800
                    if ((static_cast <QDockWidget *>(dockWidgets[i.key()])) == dockWidget) {
801 802
                        QString chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast<TOOLS_WIDGET_NAMES>(i.key()), currentView);
                        settings.setValue(chKey,visible);
803
                        //qDebug() << "showToolWidget(): Set key" << chKey << "to" << visible;
804 805 806
                        break;
                    }
                }
807
            }
808
        }
809 810 811

        QDockWidget* dockWidget = qobject_cast<QDockWidget*>(QObject::sender());

812
        //qDebug() << "Trying to cast dockwidget" << dockWidget << "isvisible" << visible;
813

814
        if (dockWidget) {
815 816 817
            // Get action
            int tool = dockWidgets.key(dockWidget);

818
            //qDebug() << "Updating widget setting" << tool << "to" << visible;
819 820 821 822 823 824 825

            QAction* action = toolsMenuActions[tool];
            action->blockSignals(true);
            action->setChecked(visible);
            action->blockSignals(false);

            QHashIterator<int, QWidget*> i(dockWidgets);
826
            while (i.hasNext()) {
827
                i.next();
828
                if ((static_cast <QDockWidget *>(dockWidgets[i.key()])) == dockWidget) {
829 830
                    QString chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast<TOOLS_WIDGET_NAMES>(i.key()), currentView);
                    settings.setValue(chKey,visible);
831
                    // qDebug() << "showToolWidget(): Set key" << chKey << "to" << visible;
832 833 834
                    break;
                }
            }
835
        }
836 837 838 839
    }
}


840 841 842 843 844
void MainWindow::showTheWidget (TOOLS_WIDGET_NAMES widget, VIEW_SECTIONS view)
{
    bool tempVisible;
    Qt::DockWidgetArea tempLocation;
    QDockWidget* tempWidget = static_cast <QDockWidget *>(dockWidgets[widget]);
845

846
    tempVisible =  settings.value(buildMenuKey(SUB_SECTION_CHECKED,widget,view), false).toBool();
847

848
    //qDebug() << "showTheWidget(): Set key" << buildMenuKey(SUB_SECTION_CHECKED,widget,view) << "to" << tempVisible;
849

850
    if (tempWidget) {
851 852
        toolsMenuActions[widget]->setChecked(tempVisible);
    }
853 854


855
    //qDebug() <<  buildMenuKey (SUB_SECTION_CHECKED,widget,view) << tempVisible;
856

857 858
    tempLocation = static_cast <Qt::DockWidgetArea>(settings.value(buildMenuKey (SUB_SECTION_LOCATION,widget, view), QVariant(Qt::RightDockWidgetArea)).toInt());

859 860
    if (tempWidget != NULL) {
        if (tempVisible) {
861 862 863
            addDockWidget(tempLocation, tempWidget);
            tempWidget->show();
        }
864 865
    }
}
866

867 868 869 870 871
QString MainWindow::buildMenuKey(SETTINGS_SECTIONS section, TOOLS_WIDGET_NAMES tool, VIEW_SECTIONS view)
{
    // Key is built as follows: autopilot_type/section_menu/view/tool/section
    int apType;

872 873 874 875 876
//    apType = (UASManager::instance() && UASManager::instance()->silentGetActiveUAS())?
//             UASManager::instance()->getActiveUAS()->getAutopilotType():
//             -1;

    apType = 1;
877

878 879 880 881 882
    return (QString::number(apType) + "_" +
            QString::number(SECTION_MENU) + "_" +
            QString::number(view) + "_" +
            QString::number(tool) + "_" +
            QString::number(section) + "_" );
883 884
}

885 886
void MainWindow::closeEvent(QCloseEvent *event)
{
887
    storeSettings();
888
    aboutToCloseFlag = true;
889
    mavlink->storeSettings();
890
    UASManager::instance()->storeSettings();
891 892
    QMainWindow::closeEvent(event);
}
893

894 895
void MainWindow::showDockWidget (bool vis)
{
896
    if (!aboutToCloseFlag && !changingViewsFlag) {
897 898
        QDockWidget* temp = qobject_cast<QDockWidget *>(sender());

899
        if (temp) {
900
            QHashIterator<int, QWidget*> i(dockWidgets);
901
            while (i.hasNext()) {
902
                i.next();
903
                if ((static_cast <QDockWidget *>(dockWidgets[i.key()])) == temp) {
904 905 906 907 908 909 910 911 912
                    QString chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast<TOOLS_WIDGET_NAMES>(i.key()), currentView);
                    settings.setValue(chKey,vis);
                    toolsMenuActions[i.key()]->setChecked(vis);
                    break;
                }
            }
        }
    }
}
913

914 915
void MainWindow::updateVisibilitySettings (bool vis)
{
916
    if (!aboutToCloseFlag && !changingViewsFlag) {
917 918
        QDockWidget* temp = qobject_cast<QDockWidget *>(sender());

919
        if (temp) {
920
            QHashIterator<int, QWidget*> i(dockWidgets);
921
            while (i.hasNext()) {
922
                i.next();
923
                if ((static_cast <QDockWidget *>(dockWidgets[i.key()])) == temp) {
924 925
                    QString chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast<TOOLS_WIDGET_NAMES>(i.key()), currentView);
                    settings.setValue(chKey,vis);
926 927 928 929 930 931
                    toolsMenuActions[i.key()]->setChecked(vis);
                    break;
                }
            }
        }
    }
932 933
}

934 935 936
void MainWindow::updateLocationSettings (Qt::DockWidgetArea location)
{
    QDockWidget* temp = qobject_cast<QDockWidget *>(sender());
937

938
    QHashIterator<int, QWidget*> i(dockWidgets);
939
    while (i.hasNext()) {
940
        i.next();
941
        if ((static_cast <QDockWidget *>(dockWidgets[i.key()])) == temp) {
942 943 944 945 946
            QString posKey = buildMenuKey (SUB_SECTION_LOCATION,static_cast<TOOLS_WIDGET_NAMES>(i.key()), currentView);
            settings.setValue(posKey,location);
            break;
        }
    }
947 948
}

949

950 951 952
/**
 * Connect the signals and slots of the common window widgets
 */
953
void MainWindow::connectCommonWidgets()
954
{
955
    if (infoDockWidget && infoDockWidget->widget()) {
pixhawk's avatar
pixhawk committed
956 957 958
        connect(mavlink, SIGNAL(receiveLossChanged(int, float)),
                infoDockWidget->widget(), SLOT(updateSendLoss(int, float)));
    }
lm's avatar
lm committed
959 960 961 962 963
//    //TODO temporaly debug
//    if (slugsHilSimWidget && slugsHilSimWidget->widget()) {
//        connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)),
//                slugsHilSimWidget->widget(), SLOT(activeUasSet(UASInterface*)));
//    }
964
}
965

966 967
void MainWindow::createCustomWidget()
{
968 969
    QDockWidget* dock = new QDockWidget("Unnamed Tool", this);
    QGCToolWidget* tool = new QGCToolWidget("Unnamed Tool", dock);
lm's avatar
lm committed
970

971
    if (QGCToolWidget::instances()->size() < 2) {
lm's avatar
lm committed
972 973 974 975 976
        // This is the first widget
        ui.menuTools->addSeparator();
    }

    connect(tool, SIGNAL(destroyed()), dock, SLOT(deleteLater()));
977
    dock->setWidget(tool);