changeset 18944:b2c4d6d461f0 gui-release

fix conflict between main and editor menus when using alt keys (bug #42659) * file-editor.cc (enable_menu_shortcuts): new function enabling/disabling the alt-key accelerators; (m_add_menu): new function adding a menu and storing menu title with and without the char & indicating the accelerator in a hash; (construct): use m_add_menu for adding a new menu to the menu bar * file-editor-interface.h: new virtual function enable_menu_shortcuts * file-editor.h: new functions enable_menu_shortcuts amd m_add_menu, new hash for storing the menu titles * main-window.cc (connect_visibility_changed): disable editors menu shortcuts; (enable_menu_shortcuts): new function enabling/disabling the alt-key accelerators; (m_add_menu): new function adding a menu and storing menu title with and without the char & indicating the accelerator in a hash; (construct_file_menu, construct_edit_menu, construct_debug_menu, construct_window_menu, construct_help_menu, construct_news_menu): use m_add_menu for adding a new menu to the menu bar; (set_global_edit_shortcuts): enable/disable the main and the editors menu shortcuts
author Torsten <ttl@justmail.de>
date Sun, 20 Jul 2014 20:44:30 +0200
parents 58f1178f49ef
children d2100cb2331a
files libgui/src/m-editor/file-editor-interface.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/main-window.cc libgui/src/main-window.h
diffstat 5 files changed, 93 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-interface.h	Thu Jul 10 18:01:56 2014 +0200
+++ b/libgui/src/m-editor/file-editor-interface.h	Sun Jul 20 20:44:30 2014 +0200
@@ -64,6 +64,8 @@
 
   virtual void empty_script (bool, bool) = 0;
 
+  virtual void enable_menu_shortcuts (bool enable) = 0;
+
 public slots:
   virtual void request_new_file (const QString& command = QString ()) = 0;
   virtual void request_new_script (const QString& command = QString ()) = 0;
--- a/libgui/src/m-editor/file-editor.cc	Thu Jul 10 18:01:56 2014 +0200
+++ b/libgui/src/m-editor/file-editor.cc	Sun Jul 20 20:44:30 2014 +0200
@@ -1088,6 +1088,37 @@
 }
 
 void
+file_editor::enable_menu_shortcuts (bool enable)
+{
+  QHash<QMenu*, QStringList>::const_iterator i = _hash_menu_text.constBegin();
+
+ while (i != _hash_menu_text.constEnd())
+   {
+     i.key ()->setTitle (i.value ().at (! enable));
+     ++i;
+   }
+}
+
+QMenu*
+file_editor::m_add_menu (QMenuBar *p, QString name)
+{
+  QMenu *menu = p->addMenu (name);
+
+  QString base_name = name;  // get a copy
+  // replace intended '&' ("&&") by a temp. string
+  base_name.replace ("&&","___octave_amp_replacement___");
+  // remove single '&' (shortcut)
+  base_name.remove ("&");
+  // restore intended '&'
+  base_name.replace ("___octave_amp_replacement___","&&");
+
+  // remember names with and without shortcut
+  _hash_menu_text[menu] = QStringList () << name << base_name;
+
+  return menu;
+}
+
+void
 file_editor::construct (void)
 {
   QWidget *editor_widget = new QWidget (this);
@@ -1120,7 +1151,7 @@
 
   // file menu
 
-  _fileMenu = new QMenu (tr ("&File"), _menu_bar);
+  _fileMenu = m_add_menu (_menu_bar, tr ("&File"));
 
   // new and open menus are inserted later by the main window
   _mru_file_menu = new QMenu (tr ("&Recent Editor Files"), _fileMenu);
@@ -1157,11 +1188,9 @@
   _print_action = add_action (_fileMenu, QIcon (":/actions/icons/fileprint.png"),
           tr ("Print..."), SLOT (request_print_file (bool)));
 
-  _menu_bar->addMenu (_fileMenu);
-
   // edit menu
 
-  QMenu *editMenu = new QMenu (tr ("&Edit"), _menu_bar);
+  QMenu *editMenu = m_add_menu (_menu_bar, tr ("&Edit"));
 
   _undo_action = add_action (editMenu, QIcon (":/actions/icons/undo.png"),
           tr ("&Undo"), SLOT (request_undo (bool)));
@@ -1265,11 +1294,9 @@
           QIcon (":/actions/icons/configure.png"),
           tr ("&Styles Preferences..."), SLOT (request_styles_preferences (bool)));
 
-  _menu_bar->addMenu (editMenu);
-
   // view menu
 
-  QMenu *view_menu = new QMenu (tr ("&View"), _menu_bar);
+  QMenu *view_menu = m_add_menu (_menu_bar, tr ("&View"));
 
   _zoom_in_action = add_action (view_menu, QIcon (),
           tr ("Zoom &In"), SLOT (zoom_in (bool)));
@@ -1282,7 +1309,7 @@
 
   // debug menu
 
-  _debug_menu = new QMenu (tr ("&Debug"), _menu_bar);
+  _debug_menu = m_add_menu (_menu_bar, tr ("&Debug"));
 
   _toggle_breakpoint_action = add_action (_debug_menu,
           QIcon (":/actions/icons/bp_toggle.png"), tr ("Toggle &Breakpoint"),
@@ -1301,11 +1328,9 @@
 
   // The other debug actions will be added by the main window.
 
-  _menu_bar->addMenu (_debug_menu);
-
   // run menu
 
-  QMenu *_run_menu = new QMenu (tr ("&Run"), _menu_bar);
+  QMenu *_run_menu = m_add_menu (_menu_bar, tr ("&Run"));
 
   _run_action = add_action (_run_menu, QIcon (":/actions/icons/artsbuilderexecute.png"),
           tr ("Save File and Run"), SLOT (request_run_file (bool)));
@@ -1313,19 +1338,15 @@
           tr ("Run &Selection"), SLOT (request_context_run (bool)));
   _run_selection_action->setEnabled (false);
 
-  _menu_bar->addMenu (_run_menu);
-
   // help menu
 
-  QMenu *_help_menu = new QMenu (tr ("&Help"), _menu_bar);
+  QMenu *_help_menu = m_add_menu (_menu_bar, tr ("&Help"));
 
   _context_help_action = add_action (_help_menu, QIcon (),
           tr ("&Help on Keyword"), SLOT (request_context_help (bool)));
   _context_doc_action = add_action (_help_menu, QIcon (),
           tr ("&Documentation on Keyword"), SLOT (request_context_doc (bool)));
 
-  _menu_bar->addMenu (_help_menu);
-
   // toolbar
 
   // new and open actions are inserted later from main window
--- a/libgui/src/m-editor/file-editor.h	Thu Jul 10 18:01:56 2014 +0200
+++ b/libgui/src/m-editor/file-editor.h	Sun Jul 20 20:44:30 2014 +0200
@@ -64,6 +64,7 @@
 
   void check_actions (void);
   void empty_script (bool startup, bool visible);
+  void enable_menu_shortcuts (bool enable);
 
 signals:
 
@@ -238,7 +239,10 @@
   QAction *add_action (QMenu *menu, const QIcon &icon, const QString &text,
                        const char *member);
 
+  QMenu* m_add_menu (QMenuBar *p, QString text);
+
   std::map<QString, QWidget *> editor_tab_map;
+  QHash<QMenu*, QStringList> _hash_menu_text;
 
   QString ced;
 
@@ -322,7 +326,6 @@
   QMenu *_mru_file_menu;
   QAction *_mru_file_actions[MaxMRUFiles];
   QStringList _mru_files;
-
 };
 
 #endif // FILEEDITORMDISUBWINDOW_H
--- a/libgui/src/main-window.cc	Thu Jul 10 18:01:56 2014 +0200
+++ b/libgui/src/main-window.cc	Sun Jul 20 20:44:30 2014 +0200
@@ -1024,6 +1024,8 @@
 {
   foreach (octave_dock_widget *widget, dock_widget_list ())
     widget->connect_visibility_changed ();
+
+  editor_window->enable_menu_shortcuts (false);
 }
 
 void
@@ -1466,9 +1468,40 @@
 }
 
 void
+main_window::enable_menu_shortcuts (bool enable)
+{
+  QHash<QMenu*, QStringList>::const_iterator i = _hash_menu_text.constBegin();
+
+ while (i != _hash_menu_text.constEnd())
+   {
+     i.key ()->setTitle (i.value ().at (! enable));
+     ++i;
+   }
+}
+
+QMenu*
+main_window::m_add_menu (QMenuBar *p, QString name)
+{
+  QMenu *menu = p->addMenu (name);
+
+  QString base_name = name;  // get a copy
+  // replace intended '&' ("&&") by a temp. string
+  base_name.replace ("&&","___octave_amp_replacement___");
+  // remove single '&' (shortcut)
+  base_name.remove ("&");
+  // restore intended '&'
+  base_name.replace ("___octave_amp_replacement___","&&");
+
+  // remember names with and without shortcut
+  _hash_menu_text[menu] = QStringList () << name << base_name;
+
+  return menu;
+}
+
+void
 main_window::construct_file_menu (QMenuBar *p)
 {
-  QMenu *file_menu = p->addMenu (tr ("&File"));
+  QMenu *file_menu = m_add_menu (p, tr ("&File"));
 
   construct_new_menu (file_menu);
 
@@ -1554,7 +1587,7 @@
 void
 main_window::construct_edit_menu (QMenuBar *p)
 {
-  QMenu *edit_menu = p->addMenu (tr ("&Edit"));
+  QMenu *edit_menu = m_add_menu (p, tr ("&Edit"));
 
   QKeySequence ctrl_shift = Qt::ControlModifier + Qt::ShiftModifier;
 
@@ -1629,7 +1662,7 @@
 void
 main_window::construct_debug_menu (QMenuBar *p)
 {
-  _debug_menu = p->addMenu (tr ("De&bug"));
+  _debug_menu = m_add_menu (p, tr ("De&bug"));
 
   _debug_step_over = construct_debug_menu_item
                       (":/actions/icons/db_step.png", tr ("Step"),
@@ -1691,7 +1724,7 @@
 void
 main_window::construct_window_menu (QMenuBar *p)
 {
-  QMenu *window_menu = p->addMenu (tr ("&Window"));
+  QMenu *window_menu = m_add_menu (p, tr ("&Window"));
 
   _show_command_window_action = construct_window_menu_item
             (window_menu, tr ("Show Command Window"), true, command_window);
@@ -1740,7 +1773,7 @@
 void
 main_window::construct_help_menu (QMenuBar *p)
 {
-  QMenu *help_menu = p->addMenu (tr ("&Help"));
+  QMenu *help_menu = m_add_menu (p, tr ("&Help"));
 
   construct_documentation_menu (help_menu);
 
@@ -1782,7 +1815,7 @@
 void
 main_window::construct_news_menu (QMenuBar *p)
 {
-  QMenu *news_menu = p->addMenu (tr ("&News"));
+  QMenu *news_menu = m_add_menu (p, tr ("&News"));
 
   _release_notes_action = add_action (news_menu, QIcon (),
             tr ("Release Notes"), SLOT (display_release_notes ()));
@@ -2216,6 +2249,7 @@
   // this slot is called when editor gets/loses focus
   if (enable)
     { // editor loses focus, set the global shortcuts
+      // and disable the editor's menu
       shortcut_manager::set_shortcut (_copy_action, "main_edit:copy");
       shortcut_manager::set_shortcut (_paste_action, "main_edit:paste");
       shortcut_manager::set_shortcut (_undo_action, "main_edit:undo");
@@ -2223,12 +2257,17 @@
     }
   else
     { // disable shortcuts that are also provided by the editor itself
+      // and enable editor's menu
       QKeySequence no_key = QKeySequence ();
       _copy_action->setShortcut (no_key);
       _paste_action->setShortcut (no_key);
       _undo_action->setShortcut (no_key);
       _select_all_action->setShortcut (no_key);
     }
+
+  // enable/disable the main and the editor's menu shortcuts (alt-key)
+  editor_window->enable_menu_shortcuts (! enable);
+  enable_menu_shortcuts (enable);
 }
 
 void
--- a/libgui/src/main-window.h	Thu Jul 10 18:01:56 2014 +0200
+++ b/libgui/src/main-window.h	Sun Jul 20 20:44:30 2014 +0200
@@ -216,6 +216,8 @@
   QAction *add_action (QMenu *menu, const QIcon &icon, const QString &text,
                        const char *member, const QWidget *receiver = 0);
 
+  void enable_menu_shortcuts (bool enable);
+  QMenu* m_add_menu (QMenuBar *p, QString text);
   void construct_menu_bar (void);
   void construct_file_menu (QMenuBar *p);
   void construct_new_menu (QMenu *p);
@@ -276,6 +278,9 @@
 
   workspace_model *_workspace_model;
 
+  QHash<QMenu*, QStringList> _hash_menu_text;
+
+
   // Toolbars.
   QStatusBar *status_bar;