Force context menu to show shortcuts fix. Closes #1154 (#1163)

* Force context menu to show shortcuts fix. Closes #1154

* Mocking problem fixed
This commit is contained in:
a1ext 2019-02-05 10:46:39 +03:00 committed by Itay Cohen
parent 85a57f9e17
commit 8c82449423
2 changed files with 40 additions and 1 deletions

View File

@ -3,6 +3,7 @@
#include <QApplication>
#include <QFileOpenEvent>
#include <QEvent>
#include <QMenu>
#include <QMessageBox>
#include <QCommandLineParser>
#include <QTextCodec>
@ -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;
}
}
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<QMenu*>(widget)) {
const auto &actions = menu->actions();
for (auto action : actions) {
action->setShortcutVisibleInContextMenu(true);
}
}
#endif // QT_VERSION_CHECK(5, 10, 0) < QT_VERSION
}

View File

@ -4,6 +4,7 @@
#include <QEvent>
#include <QApplication>
#include <QList>
#include <QProxyStyle>
#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