From 8c8244942379874a085afa5a3b02a0c17f2df794 Mon Sep 17 00:00:00 2001 From: a1ext Date: Tue, 5 Feb 2019 10:46:39 +0300 Subject: [PATCH] Force context menu to show shortcuts fix. Closes #1154 (#1163) * Force context menu to show shortcuts fix. Closes #1154 * Mocking problem fixed --- src/CutterApplication.cpp | 26 +++++++++++++++++++++++++- src/CutterApplication.h | 15 +++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/CutterApplication.cpp b/src/CutterApplication.cpp index 0129ac77..9ec1fe4b 100644 --- a/src/CutterApplication.cpp +++ b/src/CutterApplication.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -122,6 +123,11 @@ CutterApplication::CutterApplication(int &argc, char **argv) : QApplication(argc mainWindow = new MainWindow(); installEventFilter(mainWindow); + // set up context menu shortcut display fix +#if QT_VERSION_CHECK(5, 10, 0) < QT_VERSION + setStyle(new CutterProxyStyle()); +#endif // QT_VERSION_CHECK(5, 10, 0) < QT_VERSION + if (args.empty()) { if (analLevelSpecified) { printf("%s\n", @@ -284,4 +290,22 @@ bool CutterApplication::loadTranslations() qWarning() << "Cannot load Cutter's translation for " << language; } return false; -} \ No newline at end of file +} + + +void CutterProxyStyle::polish(QWidget *widget) +{ + QProxyStyle::polish(widget); +#if QT_VERSION_CHECK(5, 10, 0) < QT_VERSION + // HACK: This is the only way I've found to force Qt (5.10 and newer) to + // display shortcuts in context menus on all platforms. It's ugly, + // but it gets the job done. + if (auto menu = qobject_cast(widget)) { + const auto &actions = menu->actions(); + for (auto action : actions) { + action->setShortcutVisibleInContextMenu(true); + } + } +#endif // QT_VERSION_CHECK(5, 10, 0) < QT_VERSION +} + diff --git a/src/CutterApplication.h b/src/CutterApplication.h index dc7c5ac9..7a645504 100644 --- a/src/CutterApplication.h +++ b/src/CutterApplication.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "MainWindow.h" @@ -38,4 +39,18 @@ private: MainWindow *mainWindow; }; + +/*! + * \brief CutterProxyStyle is used to force shortcuts displaying in context menu + */ +class CutterProxyStyle : public QProxyStyle +{ + Q_OBJECT +public: + /*! + * \brief it is enough to get notification about QMenu polishing to force shortcut displaying + */ + void polish(QWidget *widget) override; +}; + #endif // CUTTERAPPLICATION_H