changeset 23321:ac4d0a72927a

provide a context menu for the tab bar of the editor (bug #44605) * file-editor.cc (file_editor::construct): call new method create_context_menu of the tab bar for initializing the context menu with the close actions; (file_editor_tab_bar::file_editor_tab_bar): create empty context menu; (file_editor_tab_bar::~file_editor_tab_bar): delete context menu; (file_editor_tab_bar::create_context_menu): new method for adding actions of the file editor into the new context menu; (file_editor_tab_bar::mousePressEvent): check, whether click was into a tab, then get the mouse button and click type, finally do the desired action including showing the new context menu * file-editor.h: new method create_context_menu and new menu _context_menu in file_editor_tab_bar
author Torsten <mttl@mailbox.org>
date Tue, 21 Mar 2017 20:25:26 +0100
parents f77d840f0f7c
children c4aebfe51a17
files libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h
diffstat 2 files changed, 90 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Tue Mar 21 09:33:49 2017 -0400
+++ b/libgui/src/m-editor/file-editor.cc	Tue Mar 21 20:25:26 2017 +0100
@@ -1891,6 +1891,15 @@
   editor_widget->setLayout (vbox_layout);
   setWidget (editor_widget);
 
+  // create the context menu of the tab bar
+  file_editor_tab_bar *bar
+      = static_cast<file_editor_tab_bar *>(_tab_widget->tabBar ());
+  QList<QAction *> tab_bar_actions;
+  tab_bar_actions.append (_close_action);
+  tab_bar_actions.append (_close_all_action);
+  tab_bar_actions.append (_close_others_action);
+  bar->create_context_menu (&tab_bar_actions);
+
   // signals
   connect (this, SIGNAL (execute_command_in_terminal_signal (const QString&)),
            main_win (), SLOT (execute_command_in_terminal (const QString&)));
@@ -2473,38 +2482,94 @@
 
 file_editor_tab_bar::file_editor_tab_bar (QWidget *p)
   : QTabBar (p)
-{ }
+{
+  _context_menu = new QMenu (this);
+}
 
 file_editor_tab_bar::~file_editor_tab_bar ()
-{ }
+{
+  delete _context_menu;
+}
+
+// Create the context menu and fill it with actions from the editor
+void
+file_editor_tab_bar::create_context_menu (QList<QAction*> *actions)
+{
+  for (int i = 0; i < actions->count (); i++)
+    _context_menu->addAction (actions->at (i));
+}
 
 // Reimplement mouse event for filtering out the desired mouse clicks
 void
 file_editor_tab_bar::mousePressEvent (QMouseEvent *me)
 {
-  if ((me->type () == QEvent::MouseButtonDblClick &&
-       me->button() == Qt::LeftButton) ||
-      (me->type () != QEvent::MouseButtonDblClick &&
-       me->button() == Qt::MidButton))
+  QPoint click_pos;
+  int clicked_idx = -1;
+
+  // detect the tab where the click occured
+  for (int i = 0; i < count (); i++)
+    {
+      click_pos = mapToGlobal (me->pos ());
+      if (tabRect (i).contains (mapFromGlobal (click_pos)))
+        {
+          clicked_idx = i;
+          break;
+        }
+    }
+
+  // If a tab was clicked
+  if (clicked_idx >= 0)
     {
-      // Middle click into the tabbar -> close the tab
-      for (int i = 0; i < count (); i++)
+      int current_idx = currentIndex ();
+      // detect the mouse click
+      if ((me->type () == QEvent::MouseButtonDblClick &&
+           me->button() == Qt::LeftButton) ||
+          (me->type () != QEvent::MouseButtonDblClick &&
+           me->button() == Qt::MidButton))
         {
-          QPoint clickPos = mapToGlobal (me->pos ());
-          if (tabRect (i).contains (mapFromGlobal (clickPos)))
+          // Middle click or double click -> close the tab
+          // Make the clicked tab the current one and close it
+          setCurrentIndex (clicked_idx);
+          emit close_current_tab_signal (true);
+          // Was the closed tab before or after the previously current tab?
+          // According to the result, use previous index or reduce it by one
+          if (current_idx - clicked_idx > 0)
+            setCurrentIndex (current_idx - 1);
+          else if (current_idx - clicked_idx < 0)
+            setCurrentIndex (current_idx);
+        }
+      else if (me->type () != QEvent::MouseButtonDblClick &&
+               me->button() == Qt::RightButton)
+        {
+          // Right click, show context menu
+          setCurrentIndex (clicked_idx);
+          if (! _context_menu->exec (click_pos))
             {
-              int idx = currentIndex ();
-              // Make the clicked tab the current one and close it
-              setCurrentIndex (i);
-              emit close_current_tab_signal (true);
-              // Was the closed tab before or after the previously current tab?
-              // According to the result, use previous index or remove it by one
-              if (idx - i > 0)
-                setCurrentIndex (idx - 1);
-              else if (idx - i < 0)
-                setCurrentIndex (idx);
-              break;
+              // No action selected, back to previous tab
+              setCurrentIndex (current_idx);
             }
+          else
+            {
+              // Was the possibly only closed tab before or after the
+              // previously current tab? According to the result, use previous
+              // index or reduce it by one. Also prevent using a too large
+              // if other or all files were closed.
+              int new_idx = count () - 1;
+              if (new_idx > 0)
+                {
+                  if (current_idx - clicked_idx > 0)
+                    new_idx = current_idx - 1;
+                  else if (current_idx - clicked_idx < 0)
+                    new_idx = current_idx;
+                }
+              if (new_idx >= 0)
+                setCurrentIndex (new_idx);
+            }
+        }
+      else
+        {
+          // regular handling of the mouse event
+          QTabBar::mousePressEvent (me);
         }
     }
   else
--- a/libgui/src/m-editor/file-editor.h	Tue Mar 21 09:33:49 2017 -0400
+++ b/libgui/src/m-editor/file-editor.h	Tue Mar 21 20:25:26 2017 +0100
@@ -50,6 +50,7 @@
 
   file_editor_tab_bar (QWidget *p);
   ~file_editor_tab_bar ();
+  void create_context_menu (QList<QAction*> *actions);
 
 signals:
 
@@ -59,6 +60,9 @@
 
   void mousePressEvent(QMouseEvent *event);
 
+private:
+
+  QMenu *_context_menu;
 };