From 43066bd0e5b63de396b2f739249a7c37ceb3a8e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jacob=20St=C3=B8ren?= <jacob.storen@ceetronsolutions.com>
Date: Mon, 3 Feb 2020 16:06:37 +0100
Subject: [PATCH] #5215 Use ui item name as a general way to start a search in
 the online help

---
 .../ApplicationCommands/RicHelpFeatures.cpp   | 53 ++++++++++++++++++-
 .../ApplicationCommands/RicHelpFeatures.h     | 14 +++++
 .../UserInterface/RiuMainWindow.cpp           |  3 ++
 .../UserInterface/RiuMainWindowBase.cpp       |  9 ++++
 .../UserInterface/RiuMainWindowBase.h         |  1 +
 .../UserInterface/RiuPlotMainWindow.cpp       |  3 ++
 6 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp b/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp
index 6a287abc47..cca17ed981 100644
--- a/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp
+++ b/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp
@@ -27,6 +27,7 @@
 #include "RiuMainWindow.h"
 
 #include "cafAboutDialog.h"
+#include "cafSelectionManager.h"
 #include "cafViewer.h"
 
 #include <QAction>
@@ -39,6 +40,7 @@ CAF_CMD_SOURCE_INIT( RicHelpAboutFeature, "RicHelpAboutFeature" );
 CAF_CMD_SOURCE_INIT( RicHelpCommandLineFeature, "RicHelpCommandLineFeature" );
 CAF_CMD_SOURCE_INIT( RicHelpSummaryCommandLineFeature, "RicHelpSummaryCommandLineFeature" );
 CAF_CMD_SOURCE_INIT( RicHelpOpenUsersGuideFeature, "RicHelpOpenUsersGuideFeature" );
+CAF_CMD_SOURCE_INIT( RicSearchHelpFeature, "RicSearchHelpFeature" );
 
 //--------------------------------------------------------------------------------------------------
 ///
@@ -251,7 +253,7 @@ void RicHelpOpenUsersGuideFeature::onActionTriggered( bool isChecked )
 {
     this->disableModelChangeContribution();
 
-    QString usersGuideUrl = "https://resinsight.org/getting-started/";
+    QString usersGuideUrl = "https://resinsight.org/getting-started/overview/";
 
     if ( !QDesktopServices::openUrl( usersGuideUrl ) )
     {
@@ -267,5 +269,54 @@ void RicHelpOpenUsersGuideFeature::setupActionLook( QAction* actionToSetup )
 {
     actionToSetup->setText( "&Users Guide" );
 
+    // applyShortcutWithHintToAction( actionToSetup, QKeySequence::HelpContents );
+}
+
+//--------------------------------------------------------------------------------------------------
+///
+//--------------------------------------------------------------------------------------------------
+bool RicSearchHelpFeature::isCommandEnabled()
+{
+    return true;
+}
+
+//--------------------------------------------------------------------------------------------------
+///
+//--------------------------------------------------------------------------------------------------
+void RicSearchHelpFeature::onActionTriggered( bool isChecked )
+{
+    this->disableModelChangeContribution();
+
+    QString usersGuideUrl = "https://resinsight.org/getting-started/overview/";
+
+    caf::PdmUiItem* uiItem = caf::SelectionManager::instance()->selectedItem();
+    if ( uiItem && !uiItem->uiName().isEmpty() )
+    {
+        usersGuideUrl = "https://resinsight.org/search/?q=" + uiItem->uiName();
+    }
+
+    if ( !QDesktopServices::openUrl( usersGuideUrl ) )
+    {
+        QErrorMessage* errorHandler = QErrorMessage::qtHandler();
+        errorHandler->showMessage( "Failed open browser with the following url\n\n" + usersGuideUrl );
+    }
+}
+
+//--------------------------------------------------------------------------------------------------
+///
+//--------------------------------------------------------------------------------------------------
+void RicSearchHelpFeature::setupActionLook( QAction* actionToSetup )
+{
+    caf::PdmUiItem* uiItem = caf::SelectionManager::instance()->selectedItem();
+
+    if ( uiItem && !uiItem->uiName().isEmpty() )
+    {
+        actionToSetup->setText( "Search Help For: " + uiItem->uiName() );
+    }
+    else
+    {
+        actionToSetup->setText( "Search Help" );
+    }
+
     applyShortcutWithHintToAction( actionToSetup, QKeySequence::HelpContents );
 }
diff --git a/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.h b/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.h
index 35722536c9..3b7cfa5501 100644
--- a/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.h
+++ b/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.h
@@ -75,3 +75,17 @@ protected:
     void onActionTriggered( bool isChecked ) override;
     void setupActionLook( QAction* actionToSetup ) override;
 };
+
+//==================================================================================================
+///
+//==================================================================================================
+class RicSearchHelpFeature : public caf::CmdFeature
+{
+    CAF_CMD_HEADER_INIT;
+
+protected:
+    // Overrides
+    bool isCommandEnabled() override;
+    void onActionTriggered( bool isChecked ) override;
+    void setupActionLook( QAction* actionToSetup ) override;
+};
diff --git a/ApplicationCode/UserInterface/RiuMainWindow.cpp b/ApplicationCode/UserInterface/RiuMainWindow.cpp
index a480e88efb..07b071e9fd 100644
--- a/ApplicationCode/UserInterface/RiuMainWindow.cpp
+++ b/ApplicationCode/UserInterface/RiuMainWindow.cpp
@@ -542,6 +542,9 @@ void RiuMainWindow::createMenus()
     helpMenu->addAction( cmdFeatureMgr->action( "RicHelpSummaryCommandLineFeature" ) );
     helpMenu->addSeparator();
     helpMenu->addAction( cmdFeatureMgr->action( "RicHelpOpenUsersGuideFeature" ) );
+    helpMenu->addAction( cmdFeatureMgr->action( "RicSearchHelpFeature" ) );
+
+    connect( helpMenu, SIGNAL( aboutToShow() ), SLOT( slotRefreshHelpActions() ) );
 }
 
 //--------------------------------------------------------------------------------------------------
diff --git a/ApplicationCode/UserInterface/RiuMainWindowBase.cpp b/ApplicationCode/UserInterface/RiuMainWindowBase.cpp
index e0620a9ddb..bd82370c5a 100644
--- a/ApplicationCode/UserInterface/RiuMainWindowBase.cpp
+++ b/ApplicationCode/UserInterface/RiuMainWindowBase.cpp
@@ -30,6 +30,7 @@
 #include "cafPdmObject.h"
 #include "cafPdmUiTreeView.h"
 
+#include "cafCmdFeatureManager.h"
 #include <QDockWidget>
 #include <QMdiArea>
 #include <QMdiSubWindow>
@@ -390,3 +391,11 @@ void RiuMainWindowBase::addViewerToMdiArea( QMdiArea*     mdiArea,
         }
     }
 }
+
+//--------------------------------------------------------------------------------------------------
+///
+//--------------------------------------------------------------------------------------------------
+void RiuMainWindowBase::slotRefreshHelpActions()
+{
+    caf::CmdFeatureManager::instance()->action( "RicSearchHelpFeature" );
+}
diff --git a/ApplicationCode/UserInterface/RiuMainWindowBase.h b/ApplicationCode/UserInterface/RiuMainWindowBase.h
index 43da2cf7e3..cacbbe0c32 100644
--- a/ApplicationCode/UserInterface/RiuMainWindowBase.h
+++ b/ApplicationCode/UserInterface/RiuMainWindowBase.h
@@ -90,6 +90,7 @@ protected:
 protected slots:
     void slotDockWidgetToggleViewActionTriggered();
     void addViewerToMdiArea( QMdiArea* mdiArea, QWidget* viewer, const QPoint& subWindowPos, const QSize& subWindowSize );
+    void slotRefreshHelpActions();
 
 protected:
     caf::PdmUiTreeView* m_projectTreeView;
diff --git a/ApplicationCode/UserInterface/RiuPlotMainWindow.cpp b/ApplicationCode/UserInterface/RiuPlotMainWindow.cpp
index d772221154..0e5a2fc45d 100644
--- a/ApplicationCode/UserInterface/RiuPlotMainWindow.cpp
+++ b/ApplicationCode/UserInterface/RiuPlotMainWindow.cpp
@@ -317,6 +317,9 @@ void RiuPlotMainWindow::createMenus()
     helpMenu->addAction( cmdFeatureMgr->action( "RicHelpSummaryCommandLineFeature" ) );
     helpMenu->addSeparator();
     helpMenu->addAction( cmdFeatureMgr->action( "RicHelpOpenUsersGuideFeature" ) );
+    helpMenu->addAction( cmdFeatureMgr->action( "RicSearchHelpFeature" ) );
+
+    connect( helpMenu, SIGNAL( aboutToShow() ), SLOT( slotRefreshHelpActions() ) );
 }
 
 //--------------------------------------------------------------------------------------------------