changeset 26245:af99ea9c325f

Doc browser: go to search text when clicking a search result (bug #55228) * documentation.cc (documentation): Initialize new class variable, connect requestShowLink signal from result widget to new slot handle_search_result_clicked; (global_search): save the search string of global search in class variable; (handle_search_result_clicked): new slot opening the search link, highlight all occurrences of the search text and go to the first occurrence; (select_all_occurrences): new private method for highlighting all occurrences of a specific string * documentation.h: new slot handle_search_result_clicked new method select_all_occurrences, new class variable m_query_string
author Torsten <mttl@mailbox.org>
date Sun, 16 Dec 2018 11:20:00 +0100
parents 58b3107a00bc
children 7adb62e4cc39
files libgui/src/documentation.cc libgui/src/documentation.h
diffstat 2 files changed, 56 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/documentation.cc	Sun Dec 16 00:13:55 2018 -0800
+++ b/libgui/src/documentation.cc	Sun Dec 16 11:20:00 2018 +0100
@@ -58,6 +58,7 @@
     : QSplitter (Qt::Horizontal, p),
       m_doc_widget (p),
       m_tool_bar (new QToolBar (p)),
+      m_query_string (QString ()),
       m_prev_pages_menu (new QMenu (p)),
       m_next_pages_menu (new QMenu (p)),
       m_prev_pages_count (0),
@@ -246,8 +247,8 @@
 
     connect (search_engine->resultWidget (),
              SIGNAL (requestShowLink (const QUrl&)),
-             m_doc_browser,
-             SLOT(handle_index_clicked (const QUrl&)));
+             this,
+             SLOT(handle_search_result_clicked (const QUrl&)));
 
     // Fill the splitter
     insertWidget (0, navi);
@@ -390,11 +391,17 @@
 #if defined (HAVE_QHELPSEARCHQUERYWIDGET_SEARCHINPUT)
     QString queries
       = m_help_engine->searchEngine ()->queryWidget ()->searchInput ();
+    m_query_string = queries.split (" ").first ();
 #else
     QList<QHelpSearchQuery> queries
       = m_help_engine->searchEngine ()->queryWidget ()->query ();
+    if (queries.count ())
+      m_query_string = queries.first ().wordList ().first ();
+    else
+      m_query_string = "";
 #endif
 
+
     m_help_engine->searchEngine ()->search (queries);
   }
 
@@ -472,6 +479,49 @@
     qApp->restoreOverrideCursor();
   }
 
+  void documentation::handle_search_result_clicked (const QUrl& url)
+  {
+    // Open url with matching text
+    m_doc_browser->handle_index_clicked (url);
+
+    // Select all occurrences of matching text
+    select_all_occurrences (m_query_string);
+
+    // Open search widget with matching text as search string
+    m_find_line_edit->setText (m_query_string);
+    m_find_line_edit->parentWidget ()->show ();
+
+    find_forward ();
+  }
+
+  void documentation::select_all_occurrences (const QString& text)
+  {
+    // Get highlight background and text color
+    QPalette pal = QApplication::palette ();
+    QTextCharFormat format;
+    QColor col = pal.color (QPalette::Highlight);
+    col.setAlphaF (0.25);
+    format.setBackground (QBrush (col));
+    format.setForeground (QBrush (pal.color (QPalette::Text)));
+
+    // Create list for extra selected items
+    QList<QTextEdit::ExtraSelection> selected;
+    m_doc_browser->moveCursor (QTextCursor::Start);
+
+    // Find all occurrences and add them to the selection
+    while ( m_doc_browser->find (text) )
+      {
+        QTextEdit::ExtraSelection selected_item;
+        selected_item.cursor = m_doc_browser->textCursor ();
+        selected_item.format = format;
+        selected.append (selected_item);
+      }
+
+      // Apply selection and move back to the beginning
+      m_doc_browser->setExtraSelections (selected);
+      m_doc_browser->moveCursor (QTextCursor::Start);
+  }
+
   void documentation::notice_settings (const QSettings *settings)
   {
     // Icon size in the toolbar.
--- a/libgui/src/documentation.h	Sun Dec 16 00:13:55 2018 -0800
+++ b/libgui/src/documentation.h	Sun Dec 16 11:20:00 2018 +0100
@@ -120,6 +120,7 @@
     void find_forward_from_anchor (const QString& text);
     void record_anchor_position (void);
     void handle_cursor_position_change (void);
+    void handle_search_result_clicked (const QUrl& url);
 
     void update_history_menus (void);
     void open_hist_url (QAction *a);
@@ -136,6 +137,8 @@
                          QToolBar *tool_bar = nullptr);
     void update_history (int new_count, QAction **actions);
 
+    //! Select all occurrences of a string in the doc browser
+    void select_all_occurrences (const QString& text);
 
     QHelpEngine *m_help_engine;
     QString m_internal_search;
@@ -147,6 +150,7 @@
 
     QWidget *m_doc_widget;
     QToolBar *m_tool_bar;
+    QString m_query_string;
 
     QAction *m_action_go_home;
     QAction *m_action_go_prev;