changeset 24694:9b4edcc62936

add contextmenu to the tab bar of the variable editor (part bug #53002) * variable-editor.cc (var_editor_tab_widget::var_editor_tab_widget): reimplementation of QTabWidget for using a custom tab bar; (var_editor_tab_widget::tabBar) returns the tab bar; (variable_editor::variable_editor): use custom tab bar tab_bar and add close actions to its context menu; (variable_editor::add_action): helper for adding an action ta widget or menu; (variable_editor::request_close_tab), (request_close_other_tabs), (request_close_all_tabs): slots for the new close actions (variable_editor::enable_actions): enabling tool bar and actions depending on the number of tabs; (edit_variable), (closeTab): call enable_actions; (notice_settings): set the shortcuts of the new actions * variable-editor.h: reimplemented QTabWidget for using custom tabbar, slots for the new actions, new methods add_action, new tab_bar, new actions
author Torsten <mttl@mailbox.org>
date Tue, 06 Feb 2018 20:01:30 +0100
parents fe9bc1129922
children 3048291bbed6
files libgui/src/variable-editor.cc libgui/src/variable-editor.h
diffstat 2 files changed, 152 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/variable-editor.cc	Tue Feb 06 20:01:25 2018 +0100
+++ b/libgui/src/variable-editor.cc	Tue Feb 06 20:01:30 2018 +0100
@@ -44,6 +44,7 @@
 #include <QVBoxLayout>
 
 #include "resource-manager.h"
+#include "shortcut-manager.h"
 #include "variable-editor.h"
 #include "variable-editor-model.h"
 
@@ -81,6 +82,36 @@
           : QString ("%1:%2").arg (from + 1).arg (to + 1));
 }
 
+
+
+//
+// Functions for reimplemented tab widget
+//
+
+var_editor_tab_widget::var_editor_tab_widget (QWidget *p)
+  : QTabWidget (p)
+{
+  tab_bar *bar;
+  bar = new tab_bar (this);
+
+  connect (bar, SIGNAL (close_current_tab_signal (bool)),
+           p->parent (), SLOT (request_close_tab (bool)));
+
+  this->setTabBar (bar);
+}
+
+QTabBar*
+var_editor_tab_widget::tabBar (void) const
+{
+  return (QTabWidget::tabBar ());
+}
+
+
+
+//
+// Variable editor
+//
+
 variable_editor::variable_editor (QWidget *p)
   : octave_dock_widget (p),
     m_default_width (30), m_default_height (100), m_add_font_height (0),
@@ -112,13 +143,29 @@
 
   // Tab Widget.
 
-  m_tab_widget = new QTabWidget (container);
+  m_tab_widget = new var_editor_tab_widget (container);
   m_tab_widget->setTabsClosable (true);
   m_tab_widget->setMovable (true);
 
   connect (m_tab_widget, SIGNAL (tabCloseRequested (int)),
            this, SLOT (closeTab (int)));
 
+   // Tab bar
+  m_tab_bar
+      = static_cast<tab_bar *>(m_tab_widget->tabBar ());
+
+  m_close_action = add_action (m_tab_bar->get_context_menu (),
+        resource_manager::icon ("window-close",false), tr ("&Close"),
+        SLOT (request_close_tab (bool)));
+  m_close_others_action = add_action (m_tab_bar->get_context_menu (),
+        resource_manager::icon ("window-close",false), tr ("Close &Other Tabs"),
+        SLOT (request_close_other_tabs (bool)));
+  m_close_all_action = add_action (m_tab_bar->get_context_menu (),
+        resource_manager::icon ("window-close",false), tr ("Close &All Tabs"),
+        SLOT (request_close_all_tabs (bool)));
+
+  enable_actions ();
+
   // Layout the widgets vertically with the toolbar on top
   QVBoxLayout *vbox_layout = new QVBoxLayout ();
   vbox_layout->setSpacing (0);
@@ -134,6 +181,68 @@
 }
 
 
+
+// Add an action to a menu or the widget itself
+QAction*
+variable_editor::add_action (QMenu *menu, const QIcon& icon, const QString& text,
+                         const char *member)
+{
+  QAction *a;
+
+  if (menu)
+    a = menu->addAction (icon, text, this, member);
+  else
+    {
+      a = new QAction (this);
+      connect (a, SIGNAL (triggered ()), this, member);
+    }
+
+  addAction (a);  // important for shortcut context
+  a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
+
+  return a;
+}
+
+// Slot for the close tab action
+void
+variable_editor::request_close_tab (bool)
+{
+  closeTab (m_tab_bar->currentIndex ());
+}
+
+// Slot for the close other tabs action
+void
+variable_editor::request_close_other_tabs (bool)
+{
+  int current = m_tab_bar->currentIndex ();
+
+  for (int index = m_tab_bar->count ()-1; index >= 0; index--)
+  {
+    if (current != index)
+      closeTab (index);
+  }
+}
+
+// Slot for closing all tabs
+void
+variable_editor::request_close_all_tabs (bool)
+{
+  for (int index = m_tab_bar->count ()-1; index >= 0; index--)
+    closeTab (index);
+}
+
+
+void
+variable_editor::enable_actions (void)
+{
+  const int count = m_tab_widget->count ();
+
+  m_tool_bar->setEnabled (count > 0);
+  m_close_action->setEnabled (count > 0);
+  m_close_all_action->setEnabled (count > 0);
+  m_close_others_action->setEnabled (count > 1);
+}
+
 void
 variable_editor::edit_variable (const QString& name, const octave_value& val)
 {
@@ -203,10 +312,7 @@
   int tab_idx = m_tab_widget->addTab (page, name);
   m_tab_widget->setCurrentIndex (tab_idx);
 
-  // Enable tool bar for when opening first tab.
-
-  if (m_tab_widget->count () == 1)
-    m_tool_bar->setEnabled (true);
+  enable_actions ();
 
   table->setFont (m_font);
   table->setStyleSheet (m_stylesheet);
@@ -380,6 +486,11 @@
     icon_size = st->pixelMetric (QStyle::PM_SmallIconSize);
 
   m_tool_bar->setIconSize (QSize (icon_size, icon_size));
+
+  // Shortcuts
+  shortcut_manager::set_shortcut (m_close_action, "editor_file:close");
+  shortcut_manager::set_shortcut (m_close_all_action, "editor_file:close_all");
+  shortcut_manager::set_shortcut (m_close_others_action, "editor_file:close_other");
 }
 
 void
@@ -400,10 +511,7 @@
   m_tab_widget->removeTab (idx);
   delete wdgt;
 
-  // Disable tool bar when closing last tab.
-
-  if (m_tab_widget->count () == 0)
-    m_tool_bar->setEnabled (false);
+  enable_actions ();
 }
 
 void
--- a/libgui/src/variable-editor.h	Tue Feb 06 20:01:25 2018 +0100
+++ b/libgui/src/variable-editor.h	Tue Feb 06 20:01:30 2018 +0100
@@ -29,6 +29,7 @@
 #include <QSettings>
 
 #include "octave-dock-widget.h"
+#include "tab-bar.h"
 
 class octave_value;
 
@@ -38,6 +39,25 @@
 class QTableView;
 class QModelIndex;
 
+
+// Subclassed QTabWidget for using custom tabbar
+
+class var_editor_tab_widget : public QTabWidget
+{
+  Q_OBJECT
+
+public:
+
+  var_editor_tab_widget (QWidget *p);
+
+  ~var_editor_tab_widget (void) = default;
+
+  QTabBar * tabBar (void) const;
+};
+
+
+// The variable editor class
+
 class variable_editor : public octave_dock_widget
 {
   Q_OBJECT
@@ -72,6 +92,10 @@
 
 protected slots:
 
+  void request_close_tab (bool);
+  void request_close_other_tabs (bool);
+  void request_close_all_tabs (bool);
+
   void closeEvent (QCloseEvent *);
 
   void closeTab (int idx);
@@ -118,6 +142,13 @@
 
 private:
 
+  QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text,
+                        const char *member);
+
+  void enable_actions (void);
+
+  tab_bar *m_tab_bar;
+
   QToolBar *m_tool_bar;
 
   QTabWidget *m_tab_widget;
@@ -153,6 +184,10 @@
   void update_colors (void);
 
   void construct_tool_bar (void);
+
+  QAction *m_close_action;
+  QAction *m_close_others_action;
+  QAction *m_close_all_action;
 };
 
 #endif