From f24403372bf445fb1370c57d31c31c7d1da3a553 Mon Sep 17 00:00:00 2001
From: DonLakeFlyer <don@thegagnes.com>
Date: Thu, 12 Dec 2019 10:20:53 +0300
Subject: [PATCH] Prevent view switching apis support a stack

---
 src/VehicleSetup/SetupView.qml |  6 +++---
 src/ui/AppSettings.qml         |  2 +-
 src/ui/MainRootWindow.qml      | 28 ++++++++++++++++++++++++----
 src/ui/toolbar/MainToolBar.qml | 10 +++++-----
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/src/VehicleSetup/SetupView.qml b/src/VehicleSetup/SetupView.qml
index 4a845ef..4719e8e 100644
--- a/src/VehicleSetup/SetupView.qml
+++ b/src/VehicleSetup/SetupView.qml
@@ -41,7 +41,7 @@ Rectangle {
 
     function showSummaryPanel()
     {
-        if (mainWindow.preventViewSwitch) {
+        if (mainWindow.preventViewSwitch()) {
             return
         }
         if (_fullParameterVehicleAvailable) {
@@ -58,7 +58,7 @@ Rectangle {
     }
 
     function showPanel(button, qmlSource) {
-        if (mainWindow.preventViewSwitch) {
+        if (mainWindow.preventViewSwitch()) {
             return
         }
         button.checked = true
@@ -67,7 +67,7 @@ Rectangle {
 
     function showVehicleComponentPanel(vehicleComponent)
     {
-        if (mainWindow.preventViewSwitch) {
+        if (mainWindow.preventViewSwitch()) {
             return
         }
         var autopilotPlugin = QGroundControl.multiVehicleManager.activeVehicle.autopilot
diff --git a/src/ui/AppSettings.qml b/src/ui/AppSettings.qml
index ec41b27..0f8da22 100644
--- a/src/ui/AppSettings.qml
+++ b/src/ui/AppSettings.qml
@@ -74,7 +74,7 @@ Rectangle {
                     Layout.fillWidth:   true
 
                     onClicked: {
-                        if (mainWindow.preventViewSwitch) {
+                        if (mainWindow.preventViewSwitch()) {
                             return
                         }
                         if (__rightPanel.source !== modelData.url) {
diff --git a/src/ui/MainRootWindow.qml b/src/ui/MainRootWindow.qml
index ff8e280..47a3b06 100644
--- a/src/ui/MainRootWindow.qml
+++ b/src/ui/MainRootWindow.qml
@@ -38,6 +38,9 @@ ApplicationWindow {
         }
     }
 
+    property var                _rgPreventViewSwitch:       [ false ]
+
+
     readonly property real      _topBottomMargins:          ScreenTools.defaultFontPixelHeight * 0.5
     readonly property string    _mainToolbar:               QGroundControl.corePlugin.options.mainToolbarUrl
     readonly property string    _planToolbar:               QGroundControl.corePlugin.options.planToolbarUrl
@@ -49,7 +52,6 @@ ApplicationWindow {
     property bool               communicationLost:          activeVehicle ? activeVehicle.connectionLost : false
     property string             formatedMessage:            activeVehicle ? activeVehicle.formatedMessage : ""
     property real               availableHeight:            mainWindow.height - mainWindow.header.height - mainWindow.footer.height
-    property bool               preventViewSwitch:          false
 
     property var                currentPlanMissionItem:     planMasterControllerPlan ? planMasterControllerPlan.missionController.currentPlanViewItem : null
     property var                planMasterControllerPlan:   null
@@ -73,6 +75,25 @@ ApplicationWindow {
     //-------------------------------------------------------------------------
     //-- Global Scope Functions
 
+    /// Prevent view switching
+    function pushPreventViewSwitch() {
+        _rgPreventViewSwitch.push(true)
+    }
+
+    /// Allow view switching
+    function popPreventViewSwitch() {
+        if (_rgPreventViewSwitch.length == 1) {
+            console.warning("mainWindow.popPreventViewSwitch called when nothing pushed")
+            return
+        }
+        _rgPreventViewSwitch.pop()
+    }
+
+    /// @return true: View switches are not currently allowed
+    function preventViewSwitch() {
+        return _rgPreventViewSwitch[_rgPreventViewSwitch.length - 1]
+    }
+
     function viewSwitch(isPlanView) {
         settingsWindow.visible  = false
         setupWindow.visible     = false
@@ -152,8 +173,7 @@ ApplicationWindow {
         mainWindowDialog.dialogComponent = component
         mainWindowDialog.dialogTitle = title
         mainWindowDialog.dialogButtons = buttons
-        console.log("Prevent view switch")
-        mainWindow.preventViewSwitch = true
+        mainWindow.pushPreventViewSwitch()
         mainWindowDialog.open()
         if (buttons & StandardButton.Cancel || buttons & StandardButton.Close || buttons & StandardButton.Discard || buttons & StandardButton.Abort || buttons & StandardButton.Ignore) {
             mainWindowDialog.closePolicy = Popup.NoAutoClose;
@@ -188,7 +208,7 @@ ApplicationWindow {
         }
         onClosed: {
             console.log("View switch ok")
-            mainWindow.preventViewSwitch = false
+            mainWindow.popPreventViewSwitch()
             dlgLoader.source = ""
         }
     }
diff --git a/src/ui/toolbar/MainToolBar.qml b/src/ui/toolbar/MainToolBar.qml
index ba99ad2..ea55f12 100644
--- a/src/ui/toolbar/MainToolBar.qml
+++ b/src/ui/toolbar/MainToolBar.qml
@@ -124,7 +124,7 @@ Item {
                     logo:               true
                     visible:            !QGroundControl.corePlugin.options.combineSettingsAndSetup
                     onClicked: {
-                        if (mainWindow.preventViewSwitch) {
+                        if (mainWindow.preventViewSwitch()) {
                             return
                         }
                         buttonRow.clearAllChecks()
@@ -138,7 +138,7 @@ Item {
                     Layout.fillHeight:  true
                     icon.source:        "/qmlimages/Gears.svg"
                     onClicked: {
-                        if (mainWindow.preventViewSwitch) {
+                        if (mainWindow.preventViewSwitch()) {
                             return
                         }
                         buttonRow.clearAllChecks()
@@ -152,7 +152,7 @@ Item {
                     Layout.fillHeight:  true
                     icon.source:        "/qmlimages/Plan.svg"
                     onClicked: {
-                        if (mainWindow.preventViewSwitch) {
+                        if (mainWindow.preventViewSwitch()) {
                             return
                         }
                         buttonRow.clearAllChecks()
@@ -166,7 +166,7 @@ Item {
                     Layout.fillHeight:  true
                     icon.source:        "/qmlimages/PaperPlane.svg"
                     onClicked: {
-                        if (mainWindow.preventViewSwitch) {
+                        if (mainWindow.preventViewSwitch()) {
                             return
                         }
                         buttonRow.clearAllChecks()
@@ -181,7 +181,7 @@ Item {
                     icon.source:        "/qmlimages/Analyze.svg"
                     visible:            QGroundControl.corePlugin.showAdvancedUI
                     onClicked: {
-                        if (mainWindow.preventViewSwitch) {
+                        if (mainWindow.preventViewSwitch()) {
                             return
                         }
                         buttonRow.clearAllChecks()