KTextEditor

lastchangerecorder.cpp
1/*
2 SPDX-FileCopyrightText: KDE Developers
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "lastchangerecorder.h"
8#include "completionreplayer.h"
9#include <vimode/inputmodemanager.h>
10#include <vimode/keyparser.h>
11
12using namespace KateVi;
13
14bool KateVi::isRepeatOfLastShortcutOverrideAsKeyPress(const QKeyEvent &currentKeyPress, const QList<KeyEvent> &keyEventLog)
15{
16 if (keyEventLog.empty()) {
17 return false;
18 }
19 const KeyEvent &lastKeyPress = keyEventLog.last();
20 if (lastKeyPress.type() == QEvent::ShortcutOverride && currentKeyPress.type() == QEvent::KeyPress && lastKeyPress.key() == currentKeyPress.key()
21 && lastKeyPress.modifiers() == currentKeyPress.modifiers()) {
22 return true;
23 }
24 return false;
25}
26
27LastChangeRecorder::LastChangeRecorder(InputModeManager *viInputModeManager)
28 : m_viInputModeManager(viInputModeManager)
29 , m_isReplaying(false)
30{
31}
32
33void LastChangeRecorder::record(const QKeyEvent &e)
34{
35 if (isRepeatOfLastShortcutOverrideAsKeyPress(e, m_changeLog)) {
36 return;
37 }
38
39 if (e.key() != Qt::Key_Shift && e.key() != Qt::Key_Control && e.key() != Qt::Key_Meta && e.key() != Qt::Key_Alt) {
40 m_changeLog.append(KeyEvent::fromQKeyEvent(e));
41 }
42}
43
44void LastChangeRecorder::dropLast()
45{
46 Q_ASSERT(!m_changeLog.isEmpty());
47 m_changeLog.pop_back();
48}
49
50void LastChangeRecorder::clear()
51{
52 m_changeLog.clear();
53}
54
55QString LastChangeRecorder::encodedChanges() const
56{
57 QString result;
58
59 QList<KeyEvent> keyLog = m_changeLog;
60
61 for (int i = 0; i < keyLog.size(); i++) {
62 int keyCode = keyLog.at(i).key();
63 QString text = keyLog.at(i).text();
64 int mods = keyLog.at(i).modifiers();
65 QChar key;
66
67 if (text.length() > 0) {
68 key = text.at(0);
69 }
70
71 if (text.isEmpty() || (text.length() == 1 && text.at(0) < QChar(0x20)) || (mods != Qt::NoModifier && mods != Qt::ShiftModifier)) {
73
74 keyPress.append(QLatin1Char('<'));
75 keyPress.append((mods & Qt::ShiftModifier) ? QStringLiteral("s-") : QString());
76 keyPress.append((mods & CONTROL_MODIFIER) ? QStringLiteral("c-") : QString());
77 keyPress.append((mods & Qt::AltModifier) ? QStringLiteral("a-") : QString());
78 keyPress.append((mods & META_MODIFIER) ? QStringLiteral("m-") : QString());
79 keyPress.append(keyCode <= 0xFF ? QChar(keyCode) : KeyParser::self()->qt2vi(keyCode));
80 keyPress.append(QLatin1Char('>'));
81
82 key = KeyParser::self()->encodeKeySequence(keyPress).at(0);
83 }
84
85 result.append(key);
86 }
87
88 return result;
89}
90
91bool LastChangeRecorder::isReplaying() const
92{
93 return m_isReplaying;
94}
95
96void LastChangeRecorder::replay(const QString &commands, const CompletionList &completions)
97{
98 m_isReplaying = true;
99 m_viInputModeManager->completionReplayer()->start(completions);
100 m_viInputModeManager->feedKeyPresses(commands);
101 m_viInputModeManager->completionReplayer()->stop();
102 m_isReplaying = false;
103}
QEvent wrapper for copying/storing key events.
Definition keyevent.h:19
for encoding keypresses w/ modifiers into an internal QChar representation and back again to a descri...
Definition keyparser.h:28
ShortcutOverride
Type type() const const
int key() const const
Qt::KeyboardModifiers modifiers() const const
const_reference at(qsizetype i) const const
bool empty() const const
T & last()
qsizetype size() const const
QString & append(QChar ch)
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const
Key_Shift
NoModifier
void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier, int delay)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.