changeset 24724:fa60be0abc21

add tab move and switch shortcuts to the doc browser * webinfo.cc (webinfo): use new add_action for close actions, add actions for moving and switching tabs (add_action): new arg for different receiver; (notice_settings): get shortcuts for new actions * webinfo.h: new actions, new add_action * shortcut-manager.cc (do_init_data): reorder some shortcut entries; (do_fill_treewidget): move tab handling from editor to global * tab-bar.cc/h (switch_left_tab, switch_right_tab, move_tab_left, move_tab_right, switch_tab): move from file editor to here
author Torsten <mttl@mailbox.org>
date Sat, 10 Feb 2018 08:33:17 +0100
parents 5bb3874549a2
children edef866fba7b
files libgui/src/qtinfo/webinfo.cc libgui/src/qtinfo/webinfo.h libgui/src/shortcut-manager.cc libgui/src/tab-bar.cc libgui/src/tab-bar.h
diffstat 5 files changed, 114 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/qtinfo/webinfo.cc	Sat Feb 10 19:22:06 2018 +0100
+++ b/libgui/src/qtinfo/webinfo.cc	Sat Feb 10 08:33:17 2018 +0100
@@ -90,11 +90,11 @@
     hbox_layout->addWidget (_search_check_box);
 
     _close_action = add_action (_tab_bar->get_context_menu (),
-                                resource_manager::icon ("window-close",false), tr ("&Close"),
-                                SLOT (request_close_tab (bool)));
+          resource_manager::icon ("window-close",false), tr ("&Close"),
+          this, SLOT (request_close_tab (bool)));
     _close_others_action = add_action (_tab_bar->get_context_menu (),
-                                       resource_manager::icon ("window-close",false), tr ("Close &Other Tabs"),
-                                       SLOT (request_close_other_tabs (bool)));
+          resource_manager::icon ("window-close",false), tr ("Close &Other Tabs"),
+          this, SLOT (request_close_other_tabs (bool)));
     _close_action->setEnabled (false);
     _close_others_action->setEnabled (false);
 
@@ -108,28 +108,43 @@
     connect (_zoom_out_button, SIGNAL (clicked ()), this, SLOT (zoom_out ()));
     connect (_search_line_edit, SIGNAL (returnPressed ()), this, SLOT (search ()));
 
+    // Actions for tab navigation
+    m_switch_left_tab_action
+      = add_action (nullptr, QIcon (), "",
+                    _tab_bar, SLOT (switch_left_tab (void)));
+    m_switch_right_tab_action
+      = add_action (nullptr, QIcon (), "",
+                    _tab_bar, SLOT (switch_right_tab (void)));
+    m_move_tab_left_action
+      = add_action (nullptr, QIcon (), "",
+                    _tab_bar, SLOT (move_tab_left (void)));
+    m_move_tab_right_action
+      = add_action (nullptr, QIcon (), "",
+                    _tab_bar, SLOT (move_tab_right (void)));
+
     resize (500, 300);
   }
 
   // Add an action to a menu or the widget itself
-  QAction * webinfo::add_action (QMenu *menu, const QIcon& icon,
-                                 const QString& text, const char *member)
+  QAction* webinfo::add_action (QMenu *menu, const QIcon& icon,
+                                const QString& text,
+                                QWidget* receiver, const char *member)
   {
     QAction *a;
 
     if (menu)
-      a = menu->addAction (icon, text, this, member);
+      a = menu->addAction (icon, text, receiver, member);
     else
       {
         a = new QAction (this);
-        connect (a, SIGNAL (triggered ()), this, member);
+        connect (a, SIGNAL (triggered ()), receiver, member);
       }
 
-    addAction (a);  // important for shortcut context
-    a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
+      addAction (a);  // important for shortcut context
+      a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
 
-    return a;
-  }
+      return a;
+    }
 
   // Slot for the close tab action
   void webinfo::request_close_tab (bool)
@@ -352,6 +367,10 @@
   {
     shortcut_manager::set_shortcut (_close_action, "editor_file:close");
     shortcut_manager::set_shortcut (_close_others_action, "editor_file:close_other");
+    shortcut_manager::set_shortcut (m_switch_left_tab_action, "editor_tabs:switch_left_tab");
+    shortcut_manager::set_shortcut (m_switch_right_tab_action, "editor_tabs:switch_right_tab");
+    shortcut_manager::set_shortcut (m_move_tab_left_action, "editor_tabs:move_tab_left");
+    shortcut_manager::set_shortcut (m_move_tab_right_action, "editor_tabs:move_tab_right");
   }
 }
 
--- a/libgui/src/qtinfo/webinfo.h	Sat Feb 10 19:22:06 2018 +0100
+++ b/libgui/src/qtinfo/webinfo.h	Sat Feb 10 08:33:17 2018 +0100
@@ -78,9 +78,12 @@
 
   private:
 
-    QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text,
-                          const char *member);
-    void tab_state_changed (void);
+    QAction *_close_action;
+    QAction *_close_others_action;
+    QAction *m_switch_left_tab_action;
+    QAction *m_switch_right_tab_action;
+    QAction *m_move_tab_left_action;
+    QAction *m_move_tab_right_action;
 
     QTextBrowser *_text_browser;
     tab_bar *_tab_bar;
@@ -94,8 +97,11 @@
     QFont _font_web;
 
     QTextBrowser * addNewTab (const QString& name);
-    QAction *_close_action;
-    QAction *_close_others_action;
+
+    QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text,
+                          QWidget *receiver, const char *member);
+
+    void tab_state_changed (void);
 
   };
 }
--- a/libgui/src/shortcut-manager.cc	Sat Feb 10 19:22:06 2018 +0100
+++ b/libgui/src/shortcut-manager.cc	Sat Feb 10 08:33:17 2018 +0100
@@ -320,6 +320,14 @@
     init (tr ("Close Tab"), "editor_file:close", QKeySequence::Close);
     init (tr ("Close All Tabs"), "editor_file:close_all", QKeySequence ());
     init (tr ("Close Other Tabs"), "editor_file:close_other", QKeySequence ());
+    init (tr ("Switch to Left Tab"), "editor_tabs:switch_left_tab",
+          QKeySequence (ctrl + Qt::Key_PageUp));
+    init (tr ("Switch to Right Tab"), "editor_tabs:switch_right_tab",
+          QKeySequence (ctrl + Qt::Key_PageDown));
+    init (tr ("Move Tab Left"), "editor_tabs:move_tab_left",
+          QKeySequence (Qt::AltModifier + Qt::Key_PageUp));
+    init (tr ("Move Tab Right"), "editor_tabs:move_tab_right",
+          QKeySequence (Qt::AltModifier + Qt::Key_PageDown));
 
     // actions of the editor
 
@@ -459,16 +467,6 @@
     init (tr ("Document on Keyword"), "editor_help:doc_keyword",
           QKeySequence (Qt::SHIFT + Qt::Key_F1));
 
-    // tab navigation
-    init (tr ("Switch to Left Tab"), "editor_tabs:switch_left_tab",
-          QKeySequence (ctrl + Qt::Key_PageUp));
-    init (tr ("Switch to Right Tab"), "editor_tabs:switch_right_tab",
-          QKeySequence (ctrl + Qt::Key_PageDown));
-    init (tr ("Move Tab Left"), "editor_tabs:move_tab_left",
-          QKeySequence (Qt::AltModifier + Qt::Key_PageUp));
-    init (tr ("Move Tab Right"), "editor_tabs:move_tab_right",
-          QKeySequence (Qt::AltModifier + Qt::Key_PageDown));
-
   }
 
   // write one or all actual shortcut set(s) into a settings file
@@ -547,6 +545,7 @@
     m_level_hash["main_help"]   = main_help;
     m_level_hash["main_news"]   = main_news;
     m_level_hash["main_tabs"]   = main_tabs;
+    m_level_hash["editor_tabs"]   = main_tabs;
 
     QTreeWidgetItem *editor = new QTreeWidgetItem (tree_view);
     editor->setText (0, tr ("Editor"));
@@ -563,8 +562,6 @@
     editor_run->setText (0, tr ("Run Menu"));
     QTreeWidgetItem *editor_help = new QTreeWidgetItem (editor);
     editor_help->setText (0, tr ("Help Menu"));
-    QTreeWidgetItem *editor_tabs = new QTreeWidgetItem (editor);
-    editor_tabs->setText (0, tr ("Tab Handling"));
 
     m_level_hash["editor_file"] = editor_file;
     m_level_hash["editor_edit"] = editor_edit;
@@ -572,7 +569,6 @@
     m_level_hash["editor_debug"] = editor_debug;
     m_level_hash["editor_run"] = editor_run;
     m_level_hash["editor_help"] = editor_help;
-    m_level_hash["editor_tabs"] = editor_tabs;
 
     connect (tree_view, SIGNAL (itemDoubleClicked (QTreeWidgetItem*, int)),
              this, SLOT (handle_double_clicked (QTreeWidgetItem*, int)));
--- a/libgui/src/tab-bar.cc	Sat Feb 10 19:22:06 2018 +0100
+++ b/libgui/src/tab-bar.cc	Sat Feb 10 08:33:17 2018 +0100
@@ -23,6 +23,10 @@
 // This file implements a tab bar derived from QTabBar with a contextmenu
 // and possibility to close a tab via double-left and middle mouse click.
 
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
 #include "tab-bar.h"
 
 namespace octave
@@ -36,6 +40,56 @@
     delete m_context_menu;
   }
 
+  // slots for tab navigation
+  void tab_bar::switch_left_tab (void)
+  {
+    switch_tab (-1);
+  }
+
+  void tab_bar::switch_right_tab (void)
+  {
+    switch_tab (1);
+  }
+
+  void tab_bar::move_tab_left (void)
+  {
+#if defined (HAVE_QTABWIDGET_SETMOVABLE)
+    switch_tab (-1, true);
+#endif
+  }
+
+  void tab_bar::move_tab_right (void)
+  {
+#if defined (HAVE_QTABWIDGET_SETMOVABLE)
+    switch_tab (1, true);
+#endif
+  }
+
+  void tab_bar::switch_tab (int direction, bool movetab)
+  {
+    int tabs = count ();
+
+    if (tabs < 2)
+      return;
+
+    int old_pos = currentIndex ();
+    int new_pos = currentIndex () + direction;
+
+    if (new_pos < 0 || new_pos >= tabs)
+      new_pos = new_pos - direction*tabs;
+
+    if (movetab)
+      {
+#if defined (HAVE_QTABWIDGET_SETMOVABLE)
+        moveTab (old_pos, new_pos);
+        setCurrentIndex (old_pos);
+        setCurrentIndex (new_pos);
+#endif
+      }
+    else
+      setCurrentIndex (new_pos);
+  }
+
   // Reimplement mouse event for filtering out the desired mouse clicks
   void tab_bar::mousePressEvent (QMouseEvent *me)
   {
--- a/libgui/src/tab-bar.h	Sat Feb 10 19:22:06 2018 +0100
+++ b/libgui/src/tab-bar.h	Sat Feb 10 08:33:17 2018 +0100
@@ -50,6 +50,13 @@
 
     void close_current_tab_signal (bool);
 
+  public slots:
+
+    void switch_left_tab (void);
+    void switch_right_tab (void);
+    void move_tab_left (void);
+    void move_tab_right (void);
+
   protected:
 
     void mousePressEvent(QMouseEvent *event);
@@ -57,6 +64,8 @@
   private:
 
     QMenu *m_context_menu;
+
+    void switch_tab (int direction, bool movetab = false);
   };
 }