changeset 23276:ea143f4f76a5

Allow to close an editor tab with the middle mouse button (bug #44605) * file-editor.cc (file_editor::construct): change tab_widget into file_editor_tab_widget, connect the new signal for closing the tab with the already existing slot of the file editor; (file_editor_tab_widget::mousePressEvent): reimplemented mouse event of the tab bar, filtering out clicks with the middle button into the tab bar * file-editor.h: renamed the subclass tab_widget into file_editor_tab_widget, new signal close_current_tab_signal of the file_editor_tab_widget, reimplemented mousePressEvent in file_editor_tab_widget
author Torsten <mttl@mailbox.org>
date Tue, 14 Mar 2017 23:20:02 +0100
parents d00280b0d6bd
children df0fb2cb820b
files libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h
diffstat 2 files changed, 61 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Tue Mar 14 10:50:28 2017 +0100
+++ b/libgui/src/m-editor/file-editor.cc	Tue Mar 14 23:20:02 2017 +0100
@@ -1564,11 +1564,14 @@
 #endif
   _tool_bar = new QToolBar (editor_widget);
   _tool_bar->setMovable (true);
-  _tab_widget = new tab_widget (editor_widget);
+
+  _tab_widget = new file_editor_tab_widget (editor_widget);
   _tab_widget->setTabsClosable (true);
 #if defined (HAVE_QTABWIDGET_SETMOVABLE)
   _tab_widget->setMovable (true);
 #endif
+  connect (_tab_widget, SIGNAL (close_current_tab_signal (bool)),
+           this, SLOT (request_close_file (bool)));
 
   // the mru-list and an empty array of actions
   QSettings *settings = resource_manager::get_settings ();
@@ -2466,3 +2469,43 @@
 }
 
 #endif
+
+
+//
+// Functions of the the reimplemented tab widget
+//
+
+// Reimplement mouse event for filtering out the desired mouse clicks
+void
+file_editor_tab_widget::mousePressEvent(QMouseEvent *me)
+{
+  if (me->type () != QEvent::MouseButtonDblClick &&
+       me->button() == Qt::MiddleButton)
+    {
+      // Middle click into the tabbar -> close the tab
+      for (int i = 0; i < tabBar ()->count (); i++)
+        {
+          QPoint clickPos = mapToGlobal (me->pos ());
+          if (tabBar ()->tabRect (i).contains (tabBar ()->mapFromGlobal (clickPos)))
+            {
+              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;
+            }
+        }
+    }
+  else
+    {
+      // regular handling of the mouse event
+      QTabWidget::mousePressEvent (me);
+    }
+}
+
--- a/libgui/src/m-editor/file-editor.h	Tue Mar 14 10:50:28 2017 +0100
+++ b/libgui/src/m-editor/file-editor.h	Tue Mar 14 23:20:02 2017 +0100
@@ -39,17 +39,29 @@
 #include "file-editor-interface.h"
 #include "file-editor-tab.h"
 
-// subclassed QTabWidget for usable tab-bar
-class tab_widget : public QTabWidget
+// subclassed QTabWidget for usable tab-bar and reimplemented mouse event
+class file_editor_tab_widget : public QTabWidget
 {
   Q_OBJECT
 
 public:
-  tab_widget (QWidget *p) : QTabWidget (p) { }
-  ~tab_widget () { }
+
+  file_editor_tab_widget (QWidget *p) : QTabWidget (p) { }
+
+  ~file_editor_tab_widget () { }
+
   QTabBar* tabBar () const { return (QTabWidget::tabBar ()); }
+
+signals:
+
+  void close_current_tab_signal (bool);
+
+protected:
+
+  void mousePressEvent(QMouseEvent *event);
 };
 
+// the class for the file editor
 class file_editor : public file_editor_interface
 {
   Q_OBJECT
@@ -430,7 +442,7 @@
 
   QList<QAction*> _fetab_actions;
 
-  tab_widget *_tab_widget;
+  file_editor_tab_widget *_tab_widget;
 
   int _marker_breakpoint;