changeset 29465:0e9319d40977

add all open tabs to the context menu of the editor tabs (bug #60276) * file-editor.cc (construct): add a separator add the beginning of the context menu to separate the tab list from the other basic menu actions * tab-bar.cc (mousePressEvent): in case of a right click, add all open tabs to the context menu and connect menu activation to the new slot; (ctx_menu_activated): new slot for selecting a tab if activated in the context menu * tab-bar.h: new private slot ctx_menu_activated, new class variable m_ctx_actions with all actions of the context menu
author Torsten Lilge <ttl-octave@mailbox.org>
date Sat, 27 Mar 2021 09:26:12 +0100
parents 7aa1994c0ca2
children 7c8a70e4daad
files libgui/src/m-editor/file-editor.cc libgui/src/tab-bar.cc libgui/src/tab-bar.h
diffstat 3 files changed, 38 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Thu Mar 25 17:57:27 2021 +0100
+++ b/libgui/src/m-editor/file-editor.cc	Sat Mar 27 09:26:12 2021 +0100
@@ -2291,9 +2291,11 @@
     editor_widget->setLayout (vbox_layout);
     setWidget (editor_widget);
 
-    // create the context menu of the tab bar
+    // Create the basic context menu of the tab bar with editor actions.
+    // Actions for selecting an tab are added when the menu is activated.
     tab_bar *bar = m_tab_widget->get_tab_bar ();
     QMenu *ctx_men = bar->get_context_menu ();
+    ctx_men->addSeparator ();
     ctx_men->addAction (m_close_action);
     ctx_men->addAction (m_close_all_action);
     ctx_men->addAction (m_close_others_action);
--- a/libgui/src/tab-bar.cc	Thu Mar 25 17:57:27 2021 +0100
+++ b/libgui/src/tab-bar.cc	Sat Mar 27 09:26:12 2021 +0100
@@ -166,7 +166,22 @@
           {
             // Right click, show context menu
             setCurrentIndex (clicked_idx);
-            if (! m_context_menu->exec (click_pos))
+
+            // Fill context menu with actions for selecting current tabs
+            m_ctx_actions = m_context_menu->actions (); // Copy of basic actions
+            QMenu ctx_menu;                             // The menu actually used
+            connect (&ctx_menu, SIGNAL (triggered (QAction*)),
+                     this, SLOT (ctx_menu_activated (QAction*)));
+            for (int i = count () - 1; i >= 0; i--)
+              {
+                // Prepend an action for each tab
+                QAction* a = new QAction (tabIcon (i), tabText (i));
+                m_ctx_actions.prepend (a);
+              }
+            // Add all actions to our menu
+            ctx_menu.insertActions (nullptr, m_ctx_actions);
+
+            if (! ctx_menu.exec (click_pos))
               {
                 // No action selected, back to previous tab
                 setCurrentIndex (current_idx);
@@ -202,4 +217,16 @@
         QTabBar::mousePressEvent (me);
       }
   }
+
+  // Slot if a menu entry in the context menu is activated
+  void tab_bar::ctx_menu_activated (QAction *a)
+  {
+    // If the index of the activated action is in the range of
+    // the current tabs, set the related current tab. The basic actions
+    // are handled by the editor
+    int i = m_ctx_actions.indexOf (a);
+    if ((i > -1) && (i < count ()))
+      setCurrentIndex (i);
+  }
+
 }
--- a/libgui/src/tab-bar.h	Thu Mar 25 17:57:27 2021 +0100
+++ b/libgui/src/tab-bar.h	Sat Mar 27 09:26:12 2021 +0100
@@ -61,15 +61,20 @@
     void move_tab_right (void);
     void sort_tabs_alph (void);
 
+  private slots:
+
+    void ctx_menu_activated (QAction *a);
+
   protected:
 
     void mousePressEvent(QMouseEvent *event);
 
   private:
 
-    QMenu *m_context_menu;
+    void switch_tab (int direction, bool movetab = false);
 
-    void switch_tab (int direction, bool movetab = false);
+    QMenu *m_context_menu;
+    QList <QAction *> m_ctx_actions;
   };
 }