Commit 7f1b6742 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

KTextEditorPreview::KPartView: optimize shortcut code

This commit optimizes code added in
40ec8280.

* QHash should be faster than QMap;
* Make temporary container variables const to prevent detaching;
* `auto shortcut` => `const auto &shortcut` to prevent unnecessary
  copying of QKeySequence, which has non-inline copy constructor.
parent 15a581cd
Pipeline #85039 passed with stage
in 6 minutes and 39 seconds
......@@ -76,9 +76,10 @@ KPartView::KPartView(const KPluginMetaData &service, QObject *parent)
// 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()) {
const auto actions = m_part->actionCollection()->actions();
for (auto *action : actions) {
const auto shortcuts = action->shortcuts();
for (const auto &shortcut : shortcuts) {
m_shortcuts[shortcut] = action;
}
if (action->shortcutContext() != Qt::WidgetShortcut) {
......@@ -245,10 +246,10 @@ bool KPartView::eventFilter(QObject *object, QEvent *event)
}
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);
const auto keyEvent = static_cast<const QKeyEvent *>(event);
auto *const action = m_shortcuts.value(QKeySequence(keyEvent->modifiers() | keyEvent->key()));
if (action) {
action->trigger();
event->accept();
return true;
}
......
......@@ -11,7 +11,7 @@
#include <KPluginMetaData>
// Qt
#include <QMap>
#include <QHash>
#include <QObject>
#include <QTimer>
......@@ -111,7 +111,7 @@ private:
QTimer m_updateSquashingTimerFast;
QTimer m_updateSquashingTimerSlow;
QTemporaryFile *m_bufferFile = nullptr;
QMap<QKeySequence, QAction *> m_shortcuts;
QHash<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