From 0fcc718d2394dccddfb3b8c656e22e53a9179893 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Tue, 27 Oct 2015 13:58:26 -0200 Subject: [PATCH] Fix opening DockWidget views The dock widget opening system had several issues, 1 - it didn't deal with translations 2 - it didn't deal with accelerator keys 3 - it crashed if passed a invalid string (like when using KDE, happened for all options) This new way of using the views are slightly different: 1 - no more string comparissons, use integer comparissons (much faster) 2 - do not use a convoluted if/else - switch simplify. 3 - do not create two different static variable strings for the same stuff 4 - create an enum / an vector of strings (the enums bein an index of it) to acess the correct view. The overall code is clear than the old one, and faster. the only point where the code isn't as clear is on the call to showDockWidgetAction, since we passed a string that had the name of the Widget, but now the action only holds the index for the name in the string vector, I first need to get the correct string and send it. Tested here closing and reopening the app and verifying that the dock widgets that where left open are still open when reopening the application. Signed-off-by: Tomaz Canabrava --- src/ui/MainWindow.cc | 84 +++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index 7494392e1..9a369ca46 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -84,14 +84,29 @@ This file is part of the QGROUNDCONTROL project const char* MAIN_SETTINGS_GROUP = "QGC_MAINWINDOW"; #ifndef __mobile__ -static const char* _mavlinkDockWidgetName = "MAVLink Inspector"; -static const char* _customCommandWidgetName = "Custom Command"; -static const char* _filesDockWidgetName = "Onboard Files"; -static const char* _uasStatusDetailsDockWidgetName = "Status Details"; -static const char* _pfdDockWidgetName = "Primary Flight Display"; -static const char* _uasInfoViewDockWidgetName = "Info View"; -static const char* _hilDockWidgetName = "HIL Config"; -static const char* _analyzeDockWidgetName = "Analyze"; +enum DockWidgetTypes { + MAVLINK_INSPECTOR, + CUSTOM_COMMAND, + ONBOARD_FILES, + STATUS_DETAILS, + PRIMARY_FLIGHT_DISPLAY, + INFO_VIEW, + HIL_CONFIG, + ANALYZE +}; + +static const char *rgDockWidgetNames[] = { + "MAVLink Inspector", + "Custom Command", + "Onboard Files", + "Status Details", + "Primary Flight Display", + "Info View", + "HIL Config", + "Analyze" +}; + +#define ARRAY_SIZE(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0])) static const char* _visibleWidgetsKey = "VisibleWidgets"; #endif @@ -319,27 +334,16 @@ void MainWindow::_buildCommonWidgets(void) logPlayer = new QGCMAVLinkLogPlayer(statusBar()); statusBar()->addPermanentWidget(logPlayer); - static const char* rgDockWidgetNames[] = { - _mavlinkDockWidgetName, - _customCommandWidgetName, - _filesDockWidgetName, - _uasStatusDetailsDockWidgetName, - _uasInfoViewDockWidgetName, - _hilDockWidgetName, - _analyzeDockWidgetName, - }; - static const size_t cDockWidgetNames = sizeof(rgDockWidgetNames) / sizeof(rgDockWidgetNames[0]); - - for (size_t i=0; isetCheckable(true); - action->setData(pDockWidgetName); + action->setData(i); connect(action, &QAction::triggered, this, &MainWindow::_showDockWidgetAction); _ui.menuWidgets->addAction(action); - _mapName2Action[pDockWidgetName] = action; } } @@ -366,28 +370,22 @@ void MainWindow::_showDockWidget(const QString& name, bool show) void MainWindow::_createInnerDockWidget(const QString& widgetName) { QGCDockWidget* widget = NULL; - - if (widgetName == _mavlinkDockWidgetName) { - widget = new QGCMAVLinkInspector(widgetName, _mapName2Action[widgetName], MAVLinkProtocol::instance(),this); - } else if (widgetName == _customCommandWidgetName) { - widget = new CustomCommandWidget(widgetName, _mapName2Action[widgetName], this); - } else if (widgetName == _filesDockWidgetName) { - widget = new QGCUASFileViewMulti(widgetName, _mapName2Action[widgetName], this); - } else if (widgetName == _uasStatusDetailsDockWidgetName) { - widget = new UASInfoWidget(widgetName, _mapName2Action[widgetName], this); - } else if (widgetName == _hilDockWidgetName) { - widget = new HILDockWidget(widgetName, _mapName2Action[widgetName], this); - } else if (widgetName == _analyzeDockWidgetName) { - widget = new Linecharts(widgetName, _mapName2Action[widgetName], mavlinkDecoder, this); - } else if (widgetName == _uasInfoViewDockWidgetName) { - QGCTabbedInfoView* pInfoView = new QGCTabbedInfoView(widgetName, _mapName2Action[widgetName], this); - pInfoView->addSource(mavlinkDecoder); - widget = pInfoView; - } else { - qWarning() << "Attempt to create unknown Inner Dock Widget" << widgetName; - return; + QAction *action = _mapName2Action[widgetName]; + + switch(action->data().toInt()) { + case MAVLINK_INSPECTOR: widget = new QGCMAVLinkInspector(widgetName, action, MAVLinkProtocol::instance(),this); break; + case CUSTOM_COMMAND: widget = new CustomCommandWidget(widgetName, action, this); break; + case ONBOARD_FILES: widget = new QGCUASFileViewMulti(widgetName, action, this); break; + case STATUS_DETAILS: widget = new UASInfoWidget(widgetName, action, this); break; + case PRIMARY_FLIGHT_DISPLAY: widget = new FlightDisplayWidget(widgetName, action, this); break; + case HIL_CONFIG: widget = new HILDockWidget(widgetName, action, this); break; + case ANALYZE: widget = new Linecharts(widgetName, action, mavlinkDecoder, this); break; + case INFO_VIEW: widget= new QGCTabbedInfoView(widgetName, action, this); break; } + if(action->data().toInt() == INFO_VIEW) { + qobject_cast(widget)->addSource(mavlinkDecoder); + } _mapName2DockWidget[widgetName] = widget; } @@ -402,7 +400,7 @@ void MainWindow::_showDockWidgetAction(bool show) { QAction* action = qobject_cast(QObject::sender()); Q_ASSERT(action); - _showDockWidget(action->data().toString(), show); + _showDockWidget(rgDockWidgetNames[action->data().toInt()], show); } #endif -- 2.22.0