Commit 40ec8280 authored by Christoph Roick's avatar Christoph Roick Committed by Christoph Cullmann
Browse files

Work around conflicting shortcuts in preview plugin

Do not let shortcuts issued within a preview widget
propagate to the application's shortcut event handler.
Instead handle the event while it's still a ShortcutOverride
event to prevent the detection of seemingly ambiguous
shortcuts.
Also do not let actions defined inside the preview KPart
capture shortcuts that are handled on the application level.

BUG: 386265
BUG: 407869
parent c9ea8e49
......@@ -11,6 +11,7 @@
// KF
#include <KTextEditor/Document>
#include <KActionCollection>
#include <KLocalizedString>
#include <KParts/BrowserExtension>
#include <KParts/ReadOnlyPart>
......@@ -20,6 +21,8 @@
// Qt
#include <QDesktopServices>
#include <QEvent>
#include <QKeyEvent>
#include <QKeySequence>
#include <QLabel>
#include <QTemporaryFile>
......@@ -67,6 +70,21 @@ KPartView::KPartView(const KPluginMetaData &service, QObject *parent)
connect(browserExtension, &KParts::BrowserExtension::openUrlRequestDelayed, this, &KPartView::handleOpenUrlRequest);
}
m_part->widget()->installEventFilter(this);
// Register all shortcuts of the KParts actionCollection to eat them in the
// event filter before they are handled by the application (and potentially
// identified as ambiguous).
// Also restrict the shortcuts to the m_part widget by setting the shortcut context.
m_shortcuts.clear();
auto ac = m_part->actionCollection();
for (auto action : ac->actions()) {
for (auto shortcut : action->shortcuts()) {
m_shortcuts[shortcut] = action;
}
if (action->shortcutContext() != Qt::WidgetShortcut) {
action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
}
}
}
}
......@@ -226,6 +244,14 @@ bool KPartView::eventFilter(QObject *object, QEvent *event)
updatePreview();
}
return true;
} else if (event->type() == QEvent::ShortcutOverride) {
auto keyevent = static_cast<QKeyEvent *>(event);
auto it = m_shortcuts.find(QKeySequence(keyevent->modifiers() | keyevent->key()));
if (it != m_shortcuts.end()) {
it.value()->activate(QAction::Trigger);
event->accept();
return true;
}
}
return QObject::eventFilter(object, event);
......
......@@ -11,6 +11,7 @@
#include <KPluginMetaData>
// Qt
#include <QMap>
#include <QObject>
#include <QTimer>
......@@ -22,6 +23,8 @@ namespace KParts
{
class ReadOnlyPart;
}
class QAction;
class QKeySequence;
class QLabel;
class QTemporaryFile;
......@@ -108,6 +111,7 @@ private:
QTimer m_updateSquashingTimerFast;
QTimer m_updateSquashingTimerSlow;
QTemporaryFile *m_bufferFile = nullptr;
QMap<QKeySequence, QAction *> m_shortcuts;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment