# HG changeset patch
# User Torsten <mttl@mailbox.org>
# Date 1522128624 -7200
#      Tue Mar 27 07:30:24 2018 +0200
# Branch stable
# Node ID a32f86d8034402cafa6318ce0111b6b9c6ce3a96
# Parent  0e5ae3ecda1a95bd122bfba2b5c5f92ed2071aeb
documentation widget based on qt help engine (bug #53006)

diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/documentation-dock-widget.cc
--- a/libgui/src/documentation-dock-widget.cc	Thu Mar 22 16:16:26 2018 +0100
+++ b/libgui/src/documentation-dock-widget.cc	Tue Mar 27 07:30:24 2018 +0200
@@ -39,17 +39,23 @@ namespace octave
     set_title (tr ("Documentation"));
     setStatusTip (tr ("See the documentation for help."));
 
-    m_webinfo = new octave::webinfo (this);
-    setWidget (m_webinfo);
-    setFocusProxy (m_webinfo);
+    m_docs = new octave::documentation (this);
+    setWidget (m_docs);
+    setFocusProxy (m_docs);
 
     connect (p, SIGNAL (show_doc_signal (const QString&)),
              this, SLOT (showDoc (const QString&)));
   }
 
+  documentation_dock_widget::~documentation_dock_widget (void)
+  {
+    if (m_docs)
+      delete m_docs;
+  }
+
   void documentation_dock_widget::notice_settings (const QSettings *settings)
   {
-    m_webinfo->notice_settings (settings);
+    m_docs->notice_settings (settings);
   }
 
   void documentation_dock_widget::load_info_file (void)
@@ -59,22 +65,22 @@ namespace octave
 
     QString info_file = QString::fromStdString (help_sys.info_file ());
 
-    m_webinfo->load_info_file (info_file);
+    m_docs->load_info_file (info_file);
   }
 
   void documentation_dock_widget::copyClipboard (void)
   {
-    m_webinfo->copyClipboard ();
+    m_docs->copyClipboard ();
   }
 
   void documentation_dock_widget::pasteClipboard (void)
   {
-    m_webinfo->pasteClipboard ();
+    m_docs->pasteClipboard ();
   }
 
   void documentation_dock_widget::selectAll (void)
   {
-    m_webinfo->selectAll ();
+    m_docs->selectAll ();
   }
 
   void documentation_dock_widget::showDoc (const QString& name)
@@ -85,6 +91,6 @@ namespace octave
 
     raise ();
 
-    m_webinfo->load_ref (name);
+    m_docs->load_ref (name);
   }
 }
diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/documentation-dock-widget.h
--- a/libgui/src/documentation-dock-widget.h	Thu Mar 22 16:16:26 2018 +0100
+++ b/libgui/src/documentation-dock-widget.h	Tue Mar 27 07:30:24 2018 +0200
@@ -24,8 +24,7 @@ along with Octave; see the file COPYING.
 #define octave_documentation_dock_widget_h 1
 
 #include "octave-dock-widget.h"
-
-#include "webinfo.h"
+#include "documentation.h"
 
 namespace octave
 {
@@ -36,6 +35,7 @@ namespace octave
   public:
 
     documentation_dock_widget (QWidget *parent = nullptr);
+    ~documentation_dock_widget (void);
 
   public slots:
 
@@ -53,7 +53,7 @@ namespace octave
 
   private:
 
-    octave::webinfo *m_webinfo;
+    octave::documentation *m_docs;
   };
 }
 
diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/documentation.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/documentation.cc	Tue Mar 27 07:30:24 2018 +0200
@@ -0,0 +1,157 @@
+/*
+
+Copyright (C) 2018 Torsten <mttl@maibox.org>
+
+This file is part of Octave.
+
+Octave 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.
+
+Octave 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 Octave; see the file COPYING.  If not, see
+<https://www.gnu.org/licenses/>.
+
+*/
+
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include <QHelpContentWidget>
+#include <QHelpIndexWidget>
+#include <QTabWidget>
+#include <QMessageBox>
+
+#include "documentation.h"
+
+namespace octave
+{
+  // The documentation splitter, which is the main widget
+  // of the doc dock widget
+  documentation::documentation (QWidget *p)
+    : QSplitter (Qt::Horizontal, p)
+  {
+    QString collection = getenv ("OCTAVE_QTHELP_COLLECTION");
+
+   // Setup the help engine and load the help data
+    m_help_engine = new QHelpEngine(collection, this);
+    if (! m_help_engine->setupData())
+      {
+        QMessageBox::warning (this, tr ("Octave Documentation"),
+                              tr ("Could not setup the data required for the\n"
+                                  "documentation viewer. Only help texts in\n"
+                                  "the Console Widget will be available."));
+        if (m_help_engine)
+          {
+            delete m_help_engine;
+            m_help_engine = 0;
+          }
+      }
+
+    // Layout contents, index and search
+    QTabWidget *navi = new QTabWidget (this);
+
+    QHelpContentWidget *content = m_help_engine->contentWidget ();
+    navi->addTab (content, tr ("Contents"));
+
+    QHelpIndexWidget *index = m_help_engine->indexWidget ();
+    navi->addTab (index, tr ("Index"));
+
+    // The browser
+    m_doc_browser = new documentation_browser (m_help_engine, this);
+
+    // Fill the splitter
+    insertWidget (0, navi);
+    insertWidget (1, m_doc_browser);
+    setStretchFactor (1, 1);
+
+    // Connect signals for link activation
+    connect(m_help_engine->contentWidget (),
+            SIGNAL (linkActivated (const QUrl&)),
+            m_doc_browser, SLOT(handle_index_clicked (const QUrl&)));
+
+    connect(m_help_engine->indexWidget (),
+            SIGNAL (linkActivated (const QUrl&, const QString&)),
+            m_doc_browser, SLOT(handle_index_clicked (const QUrl&, const QString&)));
+
+    // Initial view
+    m_doc_browser->setSource (QUrl (
+        "qthelp://org.octave.interpreter-1.0/doc/octave.html/index.html"));
+  }
+
+  documentation::~documentation (void)
+  {
+    if (m_help_engine)
+      delete m_help_engine;
+  }
+
+  void documentation::notice_settings (const QSettings *)
+  {
+  }
+
+  void documentation::load_info_file (const QString&)
+  {
+  }
+
+  void documentation::copyClipboard (void)
+  {
+  }
+
+  void documentation::pasteClipboard (void)
+  {
+  }
+
+  void documentation::selectAll (void)
+  {
+  }
+
+  void documentation::load_ref (const QString& ref_name)
+  {
+    if (m_help_engine)
+      {
+        QMap<QString, QUrl> found_links
+            = m_help_engine->linksForIdentifier (ref_name);
+        if (found_links.count() > 0)
+          m_doc_browser->setSource (found_links.constBegin().value());
+      }
+    }
+
+
+
+// The documentation browser
+  documentation_browser::documentation_browser (QHelpEngine *he, QWidget *p)
+    : QTextBrowser (p), m_help_engine (he)
+  {
+  }
+
+  documentation_browser::~documentation_browser (void)
+  {
+  }
+
+  void documentation_browser::handle_index_clicked (const QUrl& url,
+                                                    const QString&)
+  {
+    setSource (url);
+  }
+
+  void documentation_browser::notice_settings (const QSettings *)
+  {
+  }
+
+  QVariant documentation_browser::loadResource (int type,
+                                                const QUrl &url)
+    {
+        if (url.scheme () == "qthelp")
+          return QVariant (m_help_engine->fileData(url));
+        else
+          return QTextBrowser::loadResource(type, url);
+    }
+
+}
diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/documentation.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/documentation.h	Tue Mar 27 07:30:24 2018 +0200
@@ -0,0 +1,88 @@
+/*
+
+Copyright (C) 2018 Torsten <mttl@maibox.org>
+
+This file is part of Octave.
+
+Octave 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.
+
+Octave 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 Octave; see the file COPYING.  If not, see
+<https://www.gnu.org/licenses/>.
+
+*/
+
+#if ! defined (octave_documentation_h)
+#define octave_documentation_h 1
+
+#include <QWidget>
+#include <QSettings>
+#include <QSplitter>
+#include <QTextBrowser>
+#include <QtHelp/QHelpEngine>
+
+namespace octave
+{
+  // The documentation browser
+  class documentation_browser : public QTextBrowser
+  {
+    Q_OBJECT
+
+  public:
+
+    documentation_browser (QHelpEngine *help_engine, QWidget *parent = nullptr);
+    ~documentation_browser (void);
+
+    virtual QVariant loadResource (int type, const QUrl &url);
+
+  public slots:
+
+    void handle_index_clicked (const QUrl& url, const QString& keyword = QString ());
+    void notice_settings (const QSettings *settings);
+
+  private:
+
+    QHelpEngine *m_help_engine;
+
+  };
+
+  // The splitter
+  class documentation : public QSplitter
+  {
+    Q_OBJECT
+
+  public:
+
+    documentation (QWidget *parent = nullptr);
+    ~documentation (void);
+
+  public slots:
+
+    void notice_settings (const QSettings *settings);
+
+    void load_info_file (const QString&);
+
+    void copyClipboard (void);
+    void pasteClipboard (void);
+    void selectAll (void);
+
+    void load_ref (const QString & name);
+
+  private:
+
+    QHelpEngine *m_help_engine;
+    documentation_browser *m_doc_browser;
+
+  };
+
+}
+
+#endif
diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/m-editor/file-editor.cc
--- a/libgui/src/m-editor/file-editor.cc	Thu Mar 22 16:16:26 2018 +0100
+++ b/libgui/src/m-editor/file-editor.cc	Tue Mar 27 07:30:24 2018 +0200
@@ -37,6 +37,7 @@ along with Octave; see the file COPYING.
 #include <QMessageBox>
 #include <QMimeData>
 #include <QProcess>
+#include <QPushButton>
 #include <QStyle>
 #include <QTabBar>
 #include <QTextStream>
diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/main-window.cc
--- a/libgui/src/main-window.cc	Thu Mar 22 16:16:26 2018 +0100
+++ b/libgui/src/main-window.cc	Tue Mar 27 07:30:24 2018 +0200
@@ -40,6 +40,7 @@ along with Octave; see the file COPYING.
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QIcon>
+#include <QTextBrowser>
 #include <QTextStream>
 #include <QThread>
 #include <QDateTime>
diff -r 0e5ae3ecda1a -r a32f86d80344 libgui/src/module.mk
--- a/libgui/src/module.mk	Thu Mar 22 16:16:26 2018 +0100
+++ b/libgui/src/module.mk	Tue Mar 27 07:30:24 2018 +0200
@@ -106,6 +106,7 @@ OCTAVE_GUI_SRC_MOC = \
   %reldir%/moc-external-editor-interface.cc \
   %reldir%/moc-dialog.cc \
   %reldir%/moc-documentation-dock-widget.cc \
+  %reldir%/moc-documentation.cc \
   %reldir%/moc-files-dock-widget.cc \
   %reldir%/moc-history-dock-widget.cc \
   %reldir%/moc-main-window.cc \
@@ -152,6 +153,7 @@ noinst_HEADERS += \
   %reldir%/dialog.h \
   %reldir%/octave-dock-widget.h \
   %reldir%/documentation-dock-widget.h \
+  %reldir%/documentation.h \
   %reldir%/external-editor-interface.h \
   %reldir%/files-dock-widget.h \
   %reldir%/history-dock-widget.h \
@@ -187,6 +189,7 @@ noinst_HEADERS += \
 %canon_reldir%_%canon_reldir%_la_SOURCES = \
   %reldir%/dialog.cc \
   %reldir%/documentation-dock-widget.cc \
+  %reldir%/documentation.cc \
   %reldir%/external-editor-interface.cc \
   %reldir%/files-dock-widget.cc \
   %reldir%/history-dock-widget.cc \
diff -r 0e5ae3ecda1a -r a32f86d80344 m4/acinclude.m4
--- a/m4/acinclude.m4	Thu Mar 22 16:16:26 2018 +0100
+++ b/m4/acinclude.m4	Tue Mar 27 07:30:24 2018 +0200
@@ -1835,10 +1835,10 @@ AC_DEFUN([OCTAVE_CHECK_QT_VERSION], [AC_
   ## Check for Qt libraries
   case "$qt_version" in
     4)
-      QT_MODULES="QtCore QtGui QtNetwork QtOpenGL"
+      QT_MODULES="QtCore QtGui QtNetwork QtOpenGL QtHelp"
     ;;
     5)
-      QT_MODULES="Qt5Core Qt5Gui Qt5Network Qt5OpenGL Qt5PrintSupport"
+      QT_MODULES="Qt5Core Qt5Gui Qt5Network Qt5OpenGL Qt5PrintSupport Qt5Help"
     ;;
     *)
       AC_MSG_ERROR([Unrecognized Qt version $qt_version])
