changeset 355:e7a3fa8f4eec

Merge from branch tonioni
author Chris Cannam
date Tue, 03 Jun 2014 11:03:09 +0100
parents 1d8cb0d92f4b (current diff) 4969e7921931 (diff)
children 42a5801da931 8b631ce73b1c 0876ea394902 dfcc5b355f33
files
diffstat 2 files changed, 72 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/framework/MainWindowBase.cpp	Fri May 23 11:29:17 2014 +0100
+++ b/framework/MainWindowBase.cpp	Tue Jun 03 11:03:09 2014 +0100
@@ -99,6 +99,7 @@
 #include <QRegExp>
 #include <QScrollArea>
 #include <QDesktopWidget>
+#include <QSignalMapper>
 
 #include <iostream>
 #include <cstdio>
@@ -270,6 +271,70 @@
 }
 
 void
+MainWindowBase::finaliseMenus()
+{
+    QMenuBar *mb = menuBar();
+    QList<QMenu *> menus = mb->findChildren<QMenu *>();
+    foreach (QMenu *menu, menus) {
+        if (menu) finaliseMenu(menu);
+    }
+}
+
+void
+MainWindowBase::finaliseMenu(QMenu *menu)
+{
+#ifdef Q_OS_MAC
+    // See https://bugreports.qt-project.org/browse/QTBUG-38256 and
+    // our issue #890 http://code.soundsoftware.ac.uk/issues/890 --
+    // single-key shortcuts that are associated only with a menu
+    // action do not work with Qt 5.x under OS/X.
+    // 
+    // Apparently Cocoa never handled them as a matter of course, but
+    // earlier versions of Qt picked them up as widget shortcuts and
+    // handled them anyway. That behaviour was removed to fix a crash
+    // when invoking a menu while its window was overridden by a modal
+    // dialog (https://bugreports.qt-project.org/browse/QTBUG-30657).
+    //
+    // This workaround restores the single-key shortcut behaviour by
+    // searching for single-key shortcuts in menus and replacing them
+    // with global application shortcuts that invoke the relevant
+    // actions, testing whether the actions are enabled on invocation.
+    // As it happens, it also replaces some shortcuts that were
+    // working fine (because they were also associated with toolbar
+    // buttons) but that seems to be OK so long as we remove the
+    // shortcuts from the actions as well as adding the new global
+    // shortcuts, to avoid an ambiguous shortcut error.
+    //
+    // If the Qt developers ever fix this in Qt (I couldn't think of
+    // an obvious fix myself) then presumably we can remove this.
+
+    QSignalMapper *mapper = new QSignalMapper(this);
+
+    connect(mapper, SIGNAL(mapped(QObject *)),
+            this, SLOT(menuActionMapperInvoked(QObject *)));
+
+    foreach (QAction *a, menu->actions()) {
+        QKeySequence sc = a->shortcut();
+        if (sc.count() == 1 && !(sc[0] & Qt::KeyboardModifierMask)) {
+            QShortcut *newSc = new QShortcut(sc, a->parentWidget());
+            QObject::connect(newSc, SIGNAL(activated()), mapper, SLOT(map()));
+            mapper->setMapping(newSc, a);
+            a->setShortcut(QKeySequence()); // avoid ambiguous shortcut error
+        }
+    }
+#endif
+}
+
+void
+MainWindowBase::menuActionMapperInvoked(QObject *o)
+{
+    QAction *a = qobject_cast<QAction *>(o);
+    if (a && a->isEnabled()) {
+        a->trigger();
+    }
+}
+
+void
 MainWindowBase::resizeConstrained(QSize size)
 {
     QDesktopWidget *desktop = QApplication::desktop();
--- a/framework/MainWindowBase.h	Fri May 23 11:29:17 2014 +0100
+++ b/framework/MainWindowBase.h	Tue Jun 03 11:03:09 2014 +0100
@@ -284,6 +284,8 @@
 
     virtual void newerVersionAvailable(QString) { }
 
+    virtual void menuActionMapperInvoked(QObject *);
+
 protected:
     QString                  m_sessionFile;
     QString                  m_audioFile;
@@ -412,6 +414,11 @@
     virtual void updateVisibleRangeDisplay(Pane *p) const = 0;
     virtual void updatePositionStatusDisplays() const = 0;
 
+    // Call this after setting up the menu bar, to fix up single-key
+    // shortcuts on OS/X
+    virtual void finaliseMenus();
+    virtual void finaliseMenu(QMenu *);
+
     virtual bool shouldCreateNewSessionForRDFAudio(bool *) { return true; }
 
     virtual void connectLayerEditDialog(ModelDataTableDialog *dialog);