changeset 33538:4ef1d6ab26e4

documentations find widget as separate class * documentation.cc: (documentation): do not initialize shortcuts for forward & backwar search, use new find_widget for the find bar and connect required signals; (handle_search_result_clicked): use find widget method instead of direct addressing the line edit; (notice_settings): call notice_settings in find_widget, do not set forward & backward search shortcuts; (save_settings): call save_settings in find_widget; (activate_find): just call find_widget::activate_find; (find_backward): call find with backward flag; (find): two arguments: search text and backward flag, use methods from find_widget * documentation.h: include file widget header, find_widget is class variable instead of line edit of search text, new declaration of find, no more shortcuts for forward & backward search * find-widget.cc: new file with file_widget class; (find_widget): construct widget with actions and shortcuts; (activate_find): method for toggling visibility; (find): emitting the signal for the parents search routine; (find_backward): dito; (find_incremental): dito; (set_text): method for setting search text; (text): returns search text; (notice_settings): update settings from settings file; (save_settings): save settings (only a stub at the moment) * find-widget.h: declaration of class and methods * module.mk: add new files
author Torsten Lilge <ttl-octave@mailbox.org>
date Mon, 06 May 2024 20:47:55 +0200
parents a7248dd08efa
children 85c8686b78e9
files libgui/src/documentation.cc libgui/src/documentation.h libgui/src/find-widget.cc libgui/src/find-widget.h libgui/src/module.mk
diffstat 5 files changed, 284 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/documentation.cc	Mon May 06 10:46:26 2024 -0400
+++ b/libgui/src/documentation.cc	Mon May 06 20:47:55 2024 +0200
@@ -79,9 +79,7 @@
     m_prev_pages_menu (new QMenu (this)),
     m_next_pages_menu (new QMenu (this)),
     m_prev_pages_count (0),
-    m_next_pages_count (0),
-    m_findnext_shortcut (new QShortcut (this)),
-    m_findprev_shortcut (new QShortcut (this))
+    m_next_pages_count (0)
 {
   // Get original collection
   QString collection = getenv ("OCTAVE_QTHELP_COLLECTION");
@@ -180,52 +178,21 @@
   construct_tool_bar ();
 
   // Find bar
-  QWidget *find_footer = new QWidget (browser_find);
-  QLabel *find_label = new QLabel (tr ("Find:"), find_footer);
-  m_find_line_edit = new QLineEdit (find_footer);
-  connect (m_find_line_edit, &QLineEdit::returnPressed,
-           this, [this] () { find (); });
-  connect (m_find_line_edit, &QLineEdit::textEdited,
+  m_find_widget = new find_widget (this);
+  connect (m_find_widget, &find_widget::find_signal,
+           this, &documentation::find);
+  connect (m_find_widget, &find_widget::find_incremental_signal,
            this, &documentation::find_forward_from_anchor);
-  QToolButton *forward_button = new QToolButton (find_footer);
-  forward_button->setText (tr ("Search forward"));
-  forward_button->setToolTip (tr ("Search forward"));
-
-  gui_settings settings;
-
-  forward_button->setIcon (settings.icon ("go-down"));
-  connect (forward_button, &QToolButton::pressed,
-           this, [this] () { find (); });
-  QToolButton *backward_button = new QToolButton (find_footer);
-  backward_button->setText (tr ("Search backward"));
-  backward_button->setToolTip (tr ("Search backward"));
-  backward_button->setIcon (settings.icon ("go-up"));
-  connect (backward_button, &QToolButton::pressed,
-           this, &documentation::find_backward);
-  QHBoxLayout *h_box_find_footer = new QHBoxLayout (find_footer);
-  h_box_find_footer->addWidget (find_label);
-  h_box_find_footer->addWidget (m_find_line_edit);
-  h_box_find_footer->addWidget (forward_button);
-  h_box_find_footer->addWidget (backward_button);
-  h_box_find_footer->setContentsMargins (2, 2, 2, 2);
-  find_footer->setLayout (h_box_find_footer);
 
   QVBoxLayout *v_box_browser_find = new QVBoxLayout (browser_find);
   v_box_browser_find->addWidget (m_tool_bar);
   v_box_browser_find->addWidget (m_doc_browser);
-  v_box_browser_find->addWidget (find_footer);
+  v_box_browser_find->addWidget (m_find_widget);
   browser_find->setLayout (v_box_browser_find);
 
   notice_settings ();
 
-  m_findnext_shortcut->setContext (Qt::WidgetWithChildrenShortcut);
-  connect (m_findnext_shortcut, &QShortcut::activated,
-           this, [this] () { find (); });
-  m_findprev_shortcut->setContext (Qt::WidgetWithChildrenShortcut);
-  connect (m_findprev_shortcut, &QShortcut::activated,
-           this, &documentation::find_backward);
-
-  find_footer->hide ();
+  m_find_widget->hide ();
   m_search_anchor_position = 0;
 
   if (m_help_engine)
@@ -331,6 +298,7 @@
       insertWidget (1, browser_find);
       setStretchFactor (1, 1);
 
+      gui_settings settings;
       restoreState (settings.byte_array_value (dc_splitter_state));
     }
 }
@@ -601,11 +569,11 @@
   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 ();
+  m_find_widget->set_text (m_query_string);
+  m_find_widget->show ();
 
   // If no occurrence can be found go to the top of the page
-  if (! m_doc_browser->find (m_find_line_edit->text ()))
+  if (! m_doc_browser->find (m_find_widget->text ()))
     m_doc_browser->moveCursor (QTextCursor::Start);
   else
     {
@@ -613,7 +581,7 @@
       // search backwards until the last occurrence ensures the search text
       // is visible in the first line of the visible part of the text.
       m_doc_browser->moveCursor (QTextCursor::End);
-      while (m_doc_browser->find (m_find_line_edit->text (),
+      while (m_doc_browser->find (m_find_widget->text (),
                                   QTextDocument::FindBackward));
     }
 }
@@ -667,8 +635,6 @@
 
   // Shortcuts
   settings.set_shortcut (m_action_find, sc_edit_edit_find_replace);
-  settings.shortcut (m_findnext_shortcut, sc_edit_edit_find_next);
-  settings.shortcut (m_findprev_shortcut, sc_edit_edit_find_previous);
   settings.set_shortcut (m_action_zoom_in, sc_edit_view_zoom_in);
   settings.set_shortcut (m_action_zoom_out, sc_edit_view_zoom_out);
   settings.set_shortcut (m_action_zoom_original, sc_edit_view_zoom_normal);
@@ -679,6 +645,7 @@
 
   // Settings for the browser
   m_doc_browser->notice_settings ();
+  m_find_widget->notice_settings ();
 }
 
 void
@@ -689,6 +656,7 @@
   settings.setValue (dc_splitter_state.settings_key (), saveState ());
   m_doc_browser->save_settings ();
   m_bookmarks->save_settings ();
+  m_find_widget->save_settings ();
 }
 
 void
@@ -791,17 +759,7 @@
 void
 documentation::activate_find ()
 {
-  if (m_find_line_edit->parentWidget ()->isVisible ())
-    {
-      m_find_line_edit->parentWidget ()->hide ();
-      m_doc_browser->setFocus ();
-    }
-  else
-    {
-      m_find_line_edit->parentWidget ()->show ();
-      m_find_line_edit->selectAll ();
-      m_find_line_edit->setFocus ();
-    }
+  m_find_widget->activate_find ();
 }
 
 void
@@ -831,22 +789,16 @@
 }
 
 void
-documentation::find_backward ()
-{
-  find (true);
-}
-
-void
-documentation::find (bool backward)
+documentation::find (const QString& text, bool backward)
 {
   if (! m_help_engine)
     return;
 
-  QTextDocument::FindFlags find_flags;
-  if (backward)
-    find_flags = QTextDocument::FindBackward;
+  QTextDocument::FindFlags flags;
+    if (backward)
+      flags = QTextDocument::FindBackward;
 
-  if (! m_doc_browser->find (m_find_line_edit->text (), find_flags))
+  if (! m_doc_browser->find (text, flags))
     {
       // Nothing was found, restart search from the begin or end of text
       QTextCursor textcur = m_doc_browser->textCursor ();
@@ -855,7 +807,7 @@
       else
         textcur.movePosition (QTextCursor::Start);
       m_doc_browser->setTextCursor (textcur);
-      m_doc_browser->find (m_find_line_edit->text (), find_flags);
+      m_doc_browser->find (text, flags);
     }
 
   record_anchor_position ();
--- a/libgui/src/documentation.h	Mon May 06 10:46:26 2024 -0400
+++ b/libgui/src/documentation.h	Mon May 06 20:47:55 2024 +0200
@@ -37,6 +37,8 @@
 #include <QWidget>
 #include <QtHelp/QHelpEngine>
 
+#include "find-widget.h"
+
 OCTAVE_BEGIN_NAMESPACE(octave)
 
 class documentation;
@@ -138,8 +140,7 @@
   void global_search_finished (int hits);
   void filter_update (const QString& expression);
   void filter_update_history ();
-  void find (bool backward = false);
-  void find_backward ();
+  void find (const QString&, bool);
   void find_forward_from_anchor (const QString& text);
   void record_anchor_position ();
   void handle_cursor_position_change ();
@@ -163,12 +164,12 @@
   QString m_internal_search;
   documentation_browser *m_doc_browser;
   documentation_bookmarks *m_bookmarks;
-  QLineEdit *m_find_line_edit;
   int m_search_anchor_position;
   QComboBox *m_filter;
   QString m_collection;
 
   QWidget *m_doc_widget;
+  find_widget *m_find_widget;
   QToolBar *m_tool_bar;
   QString m_query_string;
 
@@ -188,10 +189,7 @@
   QAction *m_next_pages_actions[max_history_entries];
 
   QAction *m_action_bookmark;
-
   QAction *m_action_find;
-  QShortcut *m_findnext_shortcut;
-  QShortcut *m_findprev_shortcut;
 
   QAction *m_action_zoom_in;
   QAction *m_action_zoom_out;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/find-widget.cc	Mon May 06 20:47:55 2024 +0200
@@ -0,0 +1,154 @@
+////////////////////////////////////////////////////////////////////////
+//
+// Copyright (C) 2024 The Octave Project Developers
+//
+// See the file COPYRIGHT.md in the top-level directory of this
+// distribution or <https://octave.org/copyright/>.
+//
+// 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 "gui-settings.h"
+#include "gui-preferences-sc.h"
+#include "find-widget.h"
+
+
+OCTAVE_BEGIN_NAMESPACE(octave)
+
+// The documentation splitter, which is the main widget
+// of the doc dock widget
+find_widget::find_widget (QWidget *p)
+  : QWidget (p),
+    m_findnext_shortcut (new QShortcut (this)),
+    m_findprev_shortcut (new QShortcut (this))
+{
+  gui_settings settings;
+
+  QLabel *find_label = new QLabel (tr ("Find:"), this);
+
+  m_find_line_edit = new QLineEdit (this);
+  connect (m_find_line_edit, &QLineEdit::returnPressed,
+           this, &find_widget::find);
+  connect (m_find_line_edit, &QLineEdit::textEdited,
+           this, &find_widget::find_incremental);
+
+  QToolButton *forward_button = new QToolButton (this);
+  forward_button->setText (tr ("Search forward"));
+  forward_button->setToolTip (tr ("Search forward"));
+  forward_button->setIcon (settings.icon ("go-down"));
+  connect (forward_button, &QToolButton::pressed,
+           this, &find_widget::find);
+
+  QToolButton *backward_button = new QToolButton (this);
+  backward_button->setText (tr ("Search backward"));
+  backward_button->setToolTip (tr ("Search backward"));
+  backward_button->setIcon (settings.icon ("go-up"));
+  connect (backward_button, &QToolButton::pressed,
+           this, &find_widget::find_backward);
+
+  QToolButton *close_button = new QToolButton (this);
+//  close_button->setText (tr ("Close"));
+  close_button->setToolTip (tr ("Close find dialog"));
+  close_button->setIcon (settings.icon ("window-close"));
+  connect (close_button, &QToolButton::pressed,
+           this, [this] () { close (); });
+
+  QHBoxLayout *h_box_this = new QHBoxLayout (this);
+  h_box_this->addWidget (find_label);
+  h_box_this->addWidget (m_find_line_edit);
+  h_box_this->addWidget (forward_button);
+  h_box_this->addWidget (backward_button);
+  h_box_this->addWidget (close_button);
+  h_box_this->setContentsMargins (2, 2, 2, 2);
+  this->setLayout (h_box_this);
+
+  notice_settings ();
+
+  m_findnext_shortcut->setContext (Qt::WidgetWithChildrenShortcut);
+  connect (m_findnext_shortcut, &QShortcut::activated,
+           this, &find_widget::find);
+
+  m_findprev_shortcut->setContext (Qt::WidgetWithChildrenShortcut);
+  connect (m_findprev_shortcut, &QShortcut::activated,
+           this, &find_widget::find_backward);
+}
+
+void
+find_widget::activate_find ()
+{
+  if (isVisible ())
+    {
+      hide ();
+    }
+  else
+    {
+      show ();
+      m_find_line_edit->selectAll ();
+      m_find_line_edit->setFocus ();
+    }
+}
+
+void
+find_widget::find ()
+{
+  emit find_signal (m_find_line_edit->text (), false);
+}
+
+void
+find_widget::find_backward ()
+{
+  emit find_signal (m_find_line_edit->text (), true);
+}
+
+void
+find_widget::find_incremental ()
+{
+  emit find_incremental_signal (m_find_line_edit->text ());
+}
+
+void
+find_widget::set_text (const QString& text)
+{
+  m_find_line_edit->setText (text);
+}
+
+QString
+find_widget::text ()
+{
+  return m_find_line_edit->text ();
+}
+
+void
+find_widget::notice_settings ()
+{
+  gui_settings settings;
+
+  settings.shortcut (m_findnext_shortcut, sc_edit_edit_find_next);
+  settings.shortcut (m_findprev_shortcut, sc_edit_edit_find_previous);
+}
+
+void
+find_widget::save_settings ()
+{
+}
+
+OCTAVE_END_NAMESPACE(octave)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/find-widget.h	Mon May 06 20:47:55 2024 +0200
@@ -0,0 +1,102 @@
+////////////////////////////////////////////////////////////////////////
+//
+// Copyright (C) 2024 The Octave Project Developers
+//
+// See the file COPYRIGHT.md in the top-level directory of this
+// distribution or <https://octave.org/copyright/>.
+//
+// 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/>.
+//
+////////////////////////////////////////////////////////////////////////
+
+// ---------------------------------------------------------------------
+//
+//  This class provides a horizontal layout containing a small
+//  find dialog.
+//
+//  Usage:
+//    1. Create widget and include it in the widget's layout
+//    2. Connect to the signals
+//         find_widget::find_signal (const QString& text, bool backward)
+//         find_widget::find_incremental_signal (const QString& text)
+//       for searching forward or backward for "text" and for providing
+//       an incremental search while typing
+//    3. Provide suitable actions in the slots of the signals above
+//    4. Call the find widget's methods notice_settings when settings
+//       are updated and save_settings when settings are saved
+//    5. Other methods are
+//        - file_widget::text (): get current search text
+//        - file_widget::set_text (const QString& text): set search text
+//
+// ---------------------------------------------------------------------
+
+#if ! defined (octave_find_widget_h)
+#define octave_find_widget_h 1
+
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QLineEdit>
+#include <QShortcut>
+#include <QString>
+#include <QToolButton>
+#include <QWidget>
+
+OCTAVE_BEGIN_NAMESPACE(octave)
+
+class find_widget : public QWidget
+{
+  Q_OBJECT
+
+public:
+
+  find_widget (QWidget *parent = nullptr);
+  ~find_widget () = default;
+
+  void activate_find (void);
+  QString text (void);
+  void set_text (const QString& text);
+  void notice_settings (void);
+  void save_settings (void);
+
+public slots:
+
+private slots:
+
+  void find (void);
+  void find_backward (void);
+  void find_incremental (void);
+
+signals:
+
+  void find_signal (const QString&, bool);
+  void find_incremental_signal (const QString&);
+
+protected:
+
+
+private:
+
+  QLineEdit *m_find_line_edit;
+
+  QShortcut *m_findnext_shortcut;
+  QShortcut *m_findprev_shortcut;
+
+};
+
+OCTAVE_END_NAMESPACE(octave)
+
+#endif
--- a/libgui/src/module.mk	Mon May 06 10:46:26 2024 -0400
+++ b/libgui/src/module.mk	Mon May 06 20:47:55 2024 +0200
@@ -288,6 +288,7 @@
   %reldir%/moc-documentation-bookmarks.cc \
   %reldir%/moc-dw-main-window.cc \
   %reldir%/moc-files-dock-widget.cc \
+  %reldir%/moc-find-widget.cc \
   %reldir%/moc-gui-settings.cc \
   %reldir%/moc-history-dock-widget.cc \
   %reldir%/moc-interpreter-qobject.cc \
@@ -363,6 +364,7 @@
   %reldir%/gui-settings.h \
   %reldir%/external-editor-interface.h \
   %reldir%/files-dock-widget.h \
+  %reldir%/find-widget.h \
   %reldir%/graphics-init.h \
   %reldir%/history-dock-widget.h \
   %reldir%/interpreter-qobject.h \
@@ -408,6 +410,7 @@
   %reldir%/dw-main-window.cc \
   %reldir%/external-editor-interface.cc \
   %reldir%/files-dock-widget.cc \
+  %reldir%/find-widget.cc \
   %reldir%/graphics-init.cc \
   %reldir%/gui-preferences-cs.cc \
   %reldir%/gui-preferences-dc.cc \