From d6bcee73fcb45911c8e39bc77c4436fcce1cb170 Mon Sep 17 00:00:00 2001 From: Don Gagne Date: Sat, 8 Jul 2017 17:14:38 -0700 Subject: [PATCH] Delay creation of MAVLinkDecoder until it is needed Helps battery life --- src/ui/MainWindow.cc | 36 +++++++++++++++++++++++------------- src/ui/MainWindow.h | 3 ++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index 0ffbf4df1..d72e20438 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -108,10 +108,11 @@ void MainWindow::deleteInstance(void) /// by MainWindow::_create method. Hence no other code should have access to /// constructor. MainWindow::MainWindow() - : _lowPowerMode(false) - , _showStatusBar(false) - , _mainQmlWidgetHolder(NULL) - , _forceClose(false) + : _mavlinkDecoder (NULL) + , _lowPowerMode (false) + , _showStatusBar (false) + , _mainQmlWidgetHolder (NULL) + , _forceClose (false) { _instance = this; @@ -272,10 +273,13 @@ MainWindow::MainWindow() MainWindow::~MainWindow() { - // Enforce thread-safe shutdown of the mavlink decoder - mavlinkDecoder->finish(); - mavlinkDecoder->wait(1000); - mavlinkDecoder->deleteLater(); + if (_mavlinkDecoder) { + // Enforce thread-safe shutdown of the mavlink decoder + _mavlinkDecoder->finish(); + _mavlinkDecoder->wait(1000); + _mavlinkDecoder->deleteLater(); + _mavlinkDecoder = NULL; + } // This needs to happen before we get into the QWidget dtor // otherwise the QML engine reads freed data and tries to @@ -290,12 +294,18 @@ QString MainWindow::_getWindowGeometryKey() } #ifndef __mobile__ -void MainWindow::_buildCommonWidgets(void) +MAVLinkDecoder* MainWindow::_mavLinkDecoderInstance(void) { - // Add generic MAVLink decoder - mavlinkDecoder = new MAVLinkDecoder(qgcApp()->toolbox()->mavlinkProtocol()); - connect(mavlinkDecoder.data(), &MAVLinkDecoder::valueChanged, this, &MainWindow::valueChanged); + if (_mavlinkDecoder) { + _mavlinkDecoder = new MAVLinkDecoder(qgcApp()->toolbox()->mavlinkProtocol()); + connect(_mavlinkDecoder, &MAVLinkDecoder::valueChanged, this, &MainWindow::valueChanged); + } + return _mavlinkDecoder; +} + +void MainWindow::_buildCommonWidgets(void) +{ // Log player // TODO: Make this optional with a preferences setting or under a "View" menu logPlayer = new QGCMAVLinkLogPlayer(statusBar()); @@ -363,7 +373,7 @@ bool MainWindow::_createInnerDockWidget(const QString& widgetName) widget = new HILDockWidget(widgetName, action, this); break; case ANALYZE: - widget = new Linecharts(widgetName, action, mavlinkDecoder, this); + widget = new Linecharts(widgetName, action, _mavLinkDecoderInstance(), this); break; } if(widget) { diff --git a/src/ui/MainWindow.h b/src/ui/MainWindow.h index aee454a76..5efc40ab7 100644 --- a/src/ui/MainWindow.h +++ b/src/ui/MainWindow.h @@ -127,7 +127,6 @@ protected: QSettings settings; - QPointer mavlinkDecoder; QGCMAVLinkLogPlayer* logPlayer; #ifdef QGC_MOUSE_ENABLED_WIN /** @brief 3d Mouse support (WIN only) */ @@ -181,7 +180,9 @@ private: void _showDockWidget(const QString &name, bool show); void _loadVisibleWidgetsSettings(void); void _storeVisibleWidgetsSettings(void); + MAVLinkDecoder* _mavLinkDecoderInstance(void); + MAVLinkDecoder* _mavlinkDecoder; bool _lowPowerMode; ///< If enabled, QGC reduces the update rates of all widgets bool _showStatusBar; QVBoxLayout* _centralLayout; -- 2.22.0