changeset 25398:a78e5b8d5ee7

provide shortcuts for closing widgets of the variable editor (bug #53002) * dw-main-window.cc/h (dw_main_window): new class derived from QMainWindow with action and shortcuts for closing dock widgets; (createPopupMenu): add new actions to the popup menu of QMainWindow; (add_action): convenience function for adding an action; (notice_settings): update the shortcuts from the settings file; (request_close): slot for closing active widget; (request_close_other): slot for closing other widgets; (request_close_all_files): slot for closing all widgets; * module.mk: add new files dw-main-window.cc/h * variable-editor.cc (variable_editor): use new class dw_main_window; (notice_settings): update settings of dw_main_window * variable-editor.h: use dw_main_window instead of QMainWindow
author Torsten <mttl@mailbox.org>
date Tue, 22 May 2018 22:41:12 +0200
parents 2cf750f5cb7d
children 6ca2c0d76d84
files libgui/src/dw-main-window.cc libgui/src/dw-main-window.h libgui/src/module.mk libgui/src/variable-editor.cc libgui/src/variable-editor.h
diffstat 5 files changed, 258 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/dw-main-window.cc	Tue May 22 22:41:12 2018 +0200
@@ -0,0 +1,168 @@
+/*
+
+Copyright (C) 2013-2018 Torsten <mttl@mailbox.org>
+
+This file is part of Octave.
+
+Octave is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Octave is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<https://www.gnu.org/licenses/>.
+
+*/
+
+/* This is the main window derived from QMainWindow for being used
+   as the main window in dock widgets like the variable editor or
+   the file editor
+*/
+
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include <QMenu>
+#include <QDockWidget>
+
+#include "resource-manager.h"
+#include "shortcut-manager.h"
+#include "dw-main-window.h"
+
+namespace octave
+{
+
+  dw_main_window::dw_main_window (QWidget *p)
+    : QMainWindow (p)
+  {
+    // Adding the actions for closing the dock widgets
+    m_close_action
+      = add_action (nullptr,
+                    resource_manager::icon ("window-close",false),
+                    tr ("&Close"),
+                    SLOT (request_close_file ()), this);
+
+    m_close_all_action
+      = add_action (nullptr,
+                    resource_manager::icon ("window-close",false),
+                    tr ("Close &All"),
+                    SLOT (request_close_all_files ()), this);
+
+    m_close_others_action
+      = add_action (nullptr,
+                    resource_manager::icon ("window-close",false),
+                    tr ("Close &Other"),
+                    SLOT (request_close_other_files ()), this);
+
+    notice_settings (resource_manager::get_settings ());
+  }
+
+
+  // Re-implementing the popup menu of the main window
+  QMenu *dw_main_window::createPopupMenu ()
+  {
+    QList<QAction *> new_actions = QList<QAction *> ();
+    new_actions.append (m_close_action);
+    new_actions.append (m_close_others_action);
+    new_actions.append (m_close_all_action);
+
+    QMenu *menu = QMainWindow::createPopupMenu ();
+    QList<QAction *> actions = menu->actions();
+
+    if (actions.length () > 0)
+      {
+        QAction *sep = menu->insertSeparator (actions.at (0));
+        menu->insertActions (sep, new_actions);
+      }
+    else
+      menu->addActions (new_actions);
+
+    return menu;
+  }
+
+
+  // Adding an action to the main window
+  QAction * dw_main_window::add_action (QMenu *menu, const QIcon& icon,
+                                        const QString& text, const char *member,
+                                        QWidget *receiver)
+  {
+    QAction *a;
+    QWidget *r = this;
+
+    if (receiver != nullptr)
+      r = receiver;
+
+    if (menu)
+      a = menu->addAction (icon, text, r, member);
+    else
+      {
+        a = new QAction (icon, text, this);
+        a->setEnabled (true);
+        connect (a, SIGNAL (triggered ()), r, member);
+      }
+
+    addAction (a);  // important for shortcut context
+    a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
+
+    return a;
+  }
+
+  // Update the settings
+  void dw_main_window::notice_settings (const QSettings*)
+  {
+    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");
+  }
+
+
+  // Slots for handling actions
+
+  // Close current widget
+  void dw_main_window::request_close_file ()
+  {
+    QList<QDockWidget *> list = findChildren<QDockWidget *>();
+
+    for (int i = 0; i < list.length (); i++)
+      {
+        if (list.at (i)->hasFocus ())
+          {
+            list.at (i)->close ();
+            if (i > 0)
+              list.at (i-1)->setFocus ();
+            break;
+          }
+      }
+  }
+
+  // Close other widgets
+  void dw_main_window::request_close_other_files ()
+  {
+    QList<QDockWidget *> list = findChildren<QDockWidget *>();
+
+    for (int i = list.length () - 1; i >= 0; i--)
+      {
+        if (! list.at (i)->hasFocus ())
+          list.at (i)->close ();
+      }
+  }
+
+  // Close all widgets
+  void dw_main_window::request_close_all_files ()
+  {
+    QList<QDockWidget *> list = findChildren<QDockWidget *>();
+
+    for (int i = list.length () - 1; i >= 0; i--)
+      list.at (i)->close ();
+  }
+
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/dw-main-window.h	Tue May 22 22:41:12 2018 +0200
@@ -0,0 +1,80 @@
+/*
+
+Copyright (C) 2013-2018 Torsten <mttl@mailbox.org>
+
+This file is part of Octave.
+
+Octave is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Octave is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<https://www.gnu.org/licenses/>.
+
+*/
+
+#if ! defined (dw_main_window_h)
+#define dw_main_window_h 1
+
+#include <QMainWindow>
+#include <QSettings>
+
+
+namespace octave
+{
+
+  class dw_main_window : public QMainWindow
+  {
+    Q_OBJECT
+
+  public:
+
+    dw_main_window (QWidget *parent = nullptr);
+
+    ~dw_main_window (void) = default;
+
+    // No copying!
+
+    dw_main_window (const dw_main_window&) = delete;
+
+    dw_main_window& operator = (const dw_main_window&) = delete;
+
+  public slots:
+
+    void notice_settings (const QSettings*);
+
+  protected slots:
+
+    virtual QMenu* createPopupMenu ();
+
+  private slots:
+
+    void request_close_file ();
+    void request_close_all_files ();
+    void request_close_other_files ();
+
+  signals:
+
+  protected:
+
+  private:
+
+    QAction *add_action (QMenu *menu, const QIcon& icon, const QString& text,
+                         const char *member, QWidget *receiver);
+
+    QAction *m_close_action;
+    QAction *m_close_all_action;
+    QAction *m_close_others_action;
+
+  };
+
+}
+
+#endif
--- a/libgui/src/module.mk	Sat May 19 13:54:04 2018 -0400
+++ b/libgui/src/module.mk	Tue May 22 22:41:12 2018 +0200
@@ -108,6 +108,7 @@
   %reldir%/moc-dialog.cc \
   %reldir%/moc-documentation-dock-widget.cc \
   %reldir%/moc-documentation.cc \
+  %reldir%/moc-dw-main-window.cc \
   %reldir%/moc-files-dock-widget.cc \
   %reldir%/moc-history-dock-widget.cc \
   %reldir%/moc-main-window.cc \
@@ -155,6 +156,7 @@
   %reldir%/octave-dock-widget.h \
   %reldir%/documentation-dock-widget.h \
   %reldir%/documentation.h \
+  %reldir%/dw-main-window.h \
   %reldir%/external-editor-interface.h \
   %reldir%/files-dock-widget.h \
   %reldir%/history-dock-widget.h \
@@ -190,6 +192,7 @@
   %reldir%/dialog.cc \
   %reldir%/documentation-dock-widget.cc \
   %reldir%/documentation.cc \
+  %reldir%/dw-main-window.cc \
   %reldir%/external-editor-interface.cc \
   %reldir%/files-dock-widget.cc \
   %reldir%/history-dock-widget.cc \
--- a/libgui/src/variable-editor.cc	Sat May 19 13:54:04 2018 -0400
+++ b/libgui/src/variable-editor.cc	Tue May 22 22:41:12 2018 +0200
@@ -47,6 +47,7 @@
 #include <QToolButton>
 #include <QVBoxLayout>
 
+#include "dw-main-window.h"
 #include "resource-manager.h"
 #include "shortcut-manager.h"
 #include "variable-editor.h"
@@ -1042,7 +1043,7 @@
 
   variable_editor::variable_editor (QWidget *p)
     : octave_dock_widget ("VariableEditor", p),
-      m_main (new QMainWindow ()),
+      m_main (new dw_main_window ()),
       m_tool_bar (new QToolBar (m_main)),
       m_default_width (30),
       m_default_height (100),
@@ -1358,7 +1359,7 @@
   void
   variable_editor::notice_settings (const QSettings *settings)
   {
-    // FIXME: Why use object->tostring->toint?  Why not just 100?
+    m_main->notice_settings (settings); // update settings in parent main win
 
     m_default_width = settings->value ("variable_editor/column_width",
                                        100).toInt ();
--- a/libgui/src/variable-editor.h	Sat May 19 13:54:04 2018 -0400
+++ b/libgui/src/variable-editor.h	Tue May 22 22:41:12 2018 +0200
@@ -32,6 +32,8 @@
 
 #include "octave-dock-widget.h"
 #include "tab-bar.h"
+#include "dw-main-window.h"
+
 
 class octave_value;
 
@@ -44,6 +46,7 @@
   class variable_editor_model;
   class variable_editor_view;
 
+
   // The individual variable subwindow class
 
   class variable_dock_widget : public label_dock_widget
@@ -365,7 +368,7 @@
     QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text,
                           const char *member);
 
-    QMainWindow *m_main;
+    dw_main_window *m_main;
 
     QToolBar *m_tool_bar;