Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
Kate
Commits
8e07889c
Commit
8e07889c
authored
Aug 05, 2022
by
Pablo Rauzy
Committed by
Christoph Cullmann
Aug 14, 2022
Browse files
improve code quality thanks to Eric's and Waqar's suggestions in merge request
!823
parent
52887847
Changes
3
Hide whitespace changes
Inline
Side-by-side
addons/keyboardmacros/keyboardmacrosplugin.cpp
View file @
8e07889c
...
...
@@ -9,6 +9,7 @@
#include
<QApplication>
#include
<QCompleter>
#include
<QCoreApplication>
#include
<QDebug>
#include
<QDialog>
#include
<QFile>
#include
<QIODevice>
...
...
@@ -20,6 +21,7 @@
#include
<QKeySequence>
#include
<QLineEdit>
#include
<QList>
#include
<QLoggingCategory>
#include
<QObject>
#include
<QPointer>
#include
<QRegularExpression>
...
...
@@ -43,6 +45,8 @@
#include
"keycombination.h"
#include
"macro.h"
Q_LOGGING_CATEGORY
(
KM_DBG
,
"kate.plugin.keyboardmacros"
)
K_PLUGIN_FACTORY_WITH_JSON
(
KeyboardMacrosPluginFactory
,
"keyboardmacrosplugin.json"
,
registerPlugin
<
KeyboardMacrosPlugin
>
();)
KeyboardMacrosPlugin
::
KeyboardMacrosPlugin
(
QObject
*
parent
,
const
QList
<
QVariant
>
&
)
...
...
@@ -86,7 +90,7 @@ void KeyboardMacrosPlugin::displayMessage(const QString &text, KTextEditor::Mess
m_message
->
setIcon
(
QIcon
::
fromTheme
(
QStringLiteral
(
"input-keyboard"
)));
m_message
->
setWordWrap
(
true
);
m_message
->
setPosition
(
KTextEditor
::
Message
::
BottomInView
);
m_message
->
setAutoHide
(
type
==
KTextEditor
::
Message
::
Information
?
600000
:
1500
);
m_message
->
setAutoHide
(
type
==
KTextEditor
::
Message
::
Information
?
3
600000
:
1500
);
m_message
->
setAutoHideMode
(
KTextEditor
::
Message
::
Immediate
);
m_message
->
setView
(
view
);
view
->
document
()
->
postMessage
(
m_message
);
...
...
@@ -113,7 +117,7 @@ bool KeyboardMacrosPlugin::eventFilter(QObject *obj, QEvent *event)
}
// otherwise we add the keyboard event to the macro
KeyCombination
kc
(
keyEvent
);
qDebug
()
<<
"
[KeyboardMacrosPlugin]
key combination:"
<<
kc
;
q
C
Debug
(
KM_DBG
)
<<
"key combination:"
<<
kc
;
m_tape
.
append
(
kc
);
return
false
;
}
else
{
...
...
@@ -124,7 +128,7 @@ bool KeyboardMacrosPlugin::eventFilter(QObject *obj, QEvent *event)
void
KeyboardMacrosPlugin
::
record
()
{
// start recording
qDebug
()
<<
"
[KeyboardMacrosPlugin]
start recording"
;
q
C
Debug
(
KM_DBG
)
<<
"start recording"
;
// install our spy on currently focused widget
m_focusWidget
=
qApp
->
focusWidget
();
m_focusWidget
->
installEventFilter
(
this
);
...
...
@@ -144,7 +148,7 @@ void KeyboardMacrosPlugin::record()
void
KeyboardMacrosPlugin
::
stop
(
bool
save
)
{
// stop recording
qDebug
(
)
<<
"[KeyboardMacrosPlugin]"
<<
(
save
?
"end"
:
"cancel"
)
<<
"recording"
;
q
C
Debug
(
KM_DBG
)
<<
(
save
?
"end"
:
"cancel"
)
<<
"recording"
;
// uninstall our spy
m_focusWidget
->
removeEventFilter
(
this
);
// update recording status
...
...
@@ -180,27 +184,23 @@ void KeyboardMacrosPlugin::cancel()
bool
KeyboardMacrosPlugin
::
play
(
const
QString
&
name
)
{
Macro
m
;
Macro
m
acro
;
if
(
!
name
.
isEmpty
()
&&
m_namedMacros
.
contains
(
name
))
{
m
=
m_namedMacros
.
value
(
name
);
qDebug
()
<<
"
[KeyboardMacrosPlugin]
playing macro:"
<<
name
;
m
acro
=
m_namedMacros
.
value
(
name
);
q
C
Debug
(
KM_DBG
)
<<
"playing macro:"
<<
name
;
}
else
if
(
name
.
isEmpty
()
&&
!
m_macro
.
isEmpty
())
{
m
=
m_macro
;
qDebug
()
<<
"
[KeyboardMacrosPlugin]
playing macro!"
;
m
acro
=
m_macro
;
q
C
Debug
(
KM_DBG
)
<<
"playing macro!"
;
}
else
{
return
false
;
}
Macro
::
Iterator
it
;
for
(
it
=
m
.
begin
();
it
!=
m
.
end
();
it
++
)
{
QKeyEvent
*
keyEvent
;
for
(
const
auto
&
keyCombination
:
macro
)
{
// send key press
keyEvent
=
(
*
it
).
keyPress
();
qApp
->
sendEvent
(
qApp
->
focusWidget
(),
keyEvent
);
delete
keyEvent
;
QKeyEvent
keyPress
=
keyCombination
.
keyPress
();
qApp
->
sendEvent
(
qApp
->
focusWidget
(),
&
keyPress
);
// send key release
keyEvent
=
(
*
it
).
keyRelease
();
qApp
->
sendEvent
(
qApp
->
focusWidget
(),
keyEvent
);
delete
keyEvent
;
QKeyEvent
keyRelease
=
keyCombination
.
keyRelease
();
qApp
->
sendEvent
(
qApp
->
focusWidget
(),
&
keyRelease
);
}
return
true
;
}
...
...
@@ -211,7 +211,7 @@ bool KeyboardMacrosPlugin::save(const QString &name)
if
(
m_macro
.
isEmpty
())
{
return
false
;
}
qDebug
()
<<
"
[KeyboardMacrosPlugin]
saving macro:"
<<
name
;
q
C
Debug
(
KM_DBG
)
<<
"saving macro:"
<<
name
;
m_namedMacros
.
insert
(
name
,
m_macro
);
// update GUI
m_loadNamedAction
->
setEnabled
(
true
);
...
...
@@ -227,7 +227,7 @@ bool KeyboardMacrosPlugin::load(const QString &name)
if
(
!
m_namedMacros
.
contains
(
name
))
{
return
false
;
}
qDebug
()
<<
"
[KeyboardMacrosPlugin]
loading macro:"
<<
name
;
q
C
Debug
(
KM_DBG
)
<<
"loading macro:"
<<
name
;
// clear current macro
m_macro
.
clear
();
// load named macro
...
...
@@ -244,7 +244,7 @@ bool KeyboardMacrosPlugin::remove(const QString &name)
if
(
!
m_namedMacros
.
contains
(
name
))
{
return
false
;
}
qDebug
()
<<
"
[KeyboardMacrosPlugin]
removing macro:"
<<
name
;
q
C
Debug
(
KM_DBG
)
<<
"removing macro:"
<<
name
;
m_namedMacros
.
remove
(
name
);
// update GUI
m_loadNamedAction
->
setEnabled
(
!
m_namedMacros
.
isEmpty
());
...
...
@@ -272,7 +272,6 @@ QString KeyboardMacrosPlugin::queryName(const QString &query, const QString &act
if
(
dialog
.
exec
()
!=
QDialog
::
Accepted
)
{
return
QString
();
}
delete
completer
;
return
dialog
.
textValue
();
}
...
...
@@ -289,8 +288,7 @@ void KeyboardMacrosPlugin::loadNamedMacros()
sendMessage
(
i18n
(
"Malformed JSON file '%1': %2"
,
m_storage
,
parseError
.
errorString
()),
true
);
}
QJsonObject
json
=
jsonDoc
.
object
();
QJsonObject
::
ConstIterator
it
;
for
(
it
=
json
.
constBegin
();
it
!=
json
.
constEnd
();
++
it
)
{
for
(
auto
it
=
json
.
constBegin
();
it
!=
json
.
constEnd
();
++
it
)
{
m_namedMacros
.
insert
(
it
.
key
(),
Macro
(
it
.
value
()));
}
storage
.
close
();
...
...
@@ -312,9 +310,8 @@ void KeyboardMacrosPlugin::saveNamedMacros()
return
;
}
QJsonObject
json
;
QMap
<
QString
,
Macro
>::
ConstIterator
it
;
for
(
it
=
m_namedMacros
.
constBegin
();
it
!=
m_namedMacros
.
constEnd
();
++
it
)
{
json
.
insert
(
it
.
key
(),
it
.
value
().
toJson
());
for
(
const
auto
&
[
name
,
macro
]
:
m_namedMacros
.
toStdMap
())
{
json
.
insert
(
name
,
macro
.
toJson
());
}
storage
.
write
(
QJsonDocument
(
json
).
toJson
(
QJsonDocument
::
Compact
));
storage
.
close
();
...
...
@@ -322,20 +319,22 @@ void KeyboardMacrosPlugin::saveNamedMacros()
void
KeyboardMacrosPlugin
::
focusObjectChanged
(
QObject
*
focusObject
)
{
qDebug
()
<<
"
[KeyboardMacrosPlugin]
focusObjectChanged:"
<<
focusObject
;
QWidget
*
focusWidget
=
dynamic
_cast
<
QWidget
*>
(
focusObject
);
q
C
Debug
(
KM_DBG
)
<<
"focusObjectChanged:"
<<
focusObject
;
QPointer
<
QWidget
>
focusWidget
=
qobject
_cast
<
QWidget
*>
(
focusObject
);
if
(
focusWidget
==
nullptr
||
focusWidget
==
m_focusWidget
)
{
return
;
}
// update which widget we filter events from when the focus has changed
m_focusWidget
->
removeEventFilter
(
this
);
if
(
m_focusWidget
!=
nullptr
)
{
m_focusWidget
->
removeEventFilter
(
this
);
}
m_focusWidget
=
focusWidget
;
m_focusWidget
->
installEventFilter
(
this
);
}
void
KeyboardMacrosPlugin
::
applicationStateChanged
(
Qt
::
ApplicationState
state
)
{
qDebug
()
<<
"
[KeyboardMacrosPlugin]
applicationStateChanged:"
<<
state
;
q
C
Debug
(
KM_DBG
)
<<
"applicationStateChanged:"
<<
state
;
// somehow keeping our event filter on while the app is out of focus made Kate crash, we fix that here
switch
(
state
)
{
case
Qt
::
ApplicationSuspended
:
...
...
@@ -407,8 +406,7 @@ void KeyboardMacrosPlugin::slotLoadNamed()
// if (m_recording) {
// return;
// }
// QWidget *focused = qApp->focusWidget();
// qDebug() << focused;
// QPointer<QWidget> focused = qApp->focusWidget();
// QString name = queryName(i18n("Which named macro do you want to play?"), i18n("Play Macro"));
// if (name.isEmpty()) {
// return;
...
...
@@ -531,8 +529,8 @@ bool KeyboardMacrosPluginCommands::exec(KTextEditor::View *view, const QString &
msg
=
i18n
(
"Usage: %1 <name>."
,
actionAndName
.
at
(
0
));
return
false
;
}
QString
action
=
actionAndName
.
at
(
0
);
QString
name
=
actionAndName
.
at
(
1
);
const
QString
&
action
=
actionAndName
.
at
(
0
);
const
QString
&
name
=
actionAndName
.
at
(
1
);
if
(
action
==
QStringLiteral
(
"kmsave"
))
{
if
(
!
m_plugin
->
save
(
name
))
{
msg
=
i18n
(
"Cannot save empty keyboard macro."
);
...
...
@@ -566,30 +564,23 @@ bool KeyboardMacrosPluginCommands::exec(KTextEditor::View *view, const QString &
bool
KeyboardMacrosPluginCommands
::
help
(
KTextEditor
::
View
*
,
const
QString
&
cmd
,
QString
&
msg
)
{
QString
namedM
acros
;
QString
m
acros
;
if
(
!
m_plugin
->
m_namedMacros
.
keys
().
isEmpty
())
{
namedMacros
+=
QStringLiteral
(
"<p><b>Named macros:</b> "
);
QList
<
QString
>
names
=
m_plugin
->
m_namedMacros
.
keys
();
QList
<
QString
>::
ConstIterator
it
;
namedMacros
+=
names
.
first
();
for
(
it
=
++
names
.
constBegin
();
it
!=
names
.
constEnd
();
++
it
)
{
namedMacros
+=
QStringLiteral
(
", "
)
+
*
it
;
}
namedMacros
+=
QStringLiteral
(
".</p>"
);
macros
=
QStringLiteral
(
"<p><b>Named macros:</b> "
)
+
QStringList
(
m_plugin
->
m_namedMacros
.
keys
()).
join
(
QStringLiteral
(
", "
))
+
QStringLiteral
(
".</p>"
);
}
if
(
cmd
==
QStringLiteral
(
"kmsave"
))
{
msg
=
i18n
(
"<qt><p>Usage: <code>kmsave <name></code></p><p>Save current keyboard macro as <code><name></code>.</p>%1</qt>"
,
namedM
acros
);
msg
=
i18n
(
"<qt><p>Usage: <code>kmsave <name></code></p><p>Save current keyboard macro as <code><name></code>.</p>%1</qt>"
,
m
acros
);
return
true
;
}
else
if
(
cmd
==
QStringLiteral
(
"kmload"
))
{
msg
=
i18n
(
"<qt><p>Usage: <code>kmload <name></code></p><p>Load saved keyboard macro <code><name></code> as current macro.</p>%1</qt>"
,
namedM
acros
);
m
acros
);
return
true
;
}
else
if
(
cmd
==
QStringLiteral
(
"kmdelete"
))
{
msg
=
i18n
(
"<qt><p>Usage: <code>kmdelete <name></code></p><p>Delete saved keyboard macro <code><name></code>.</p>%1</qt>"
,
namedM
acros
);
msg
=
i18n
(
"<qt><p>Usage: <code>kmdelete <name></code></p><p>Delete saved keyboard macro <code><name></code>.</p>%1</qt>"
,
m
acros
);
return
true
;
}
else
if
(
cmd
==
QStringLiteral
(
"kmplay"
))
{
msg
=
i18n
(
"<qt><p>Usage: <code>kmplay <name></code></p><p>Play saved keyboard macro <code><name></code> without loading it.</p>%1</qt>"
,
namedM
acros
);
m
acros
);
return
true
;
}
return
false
;
...
...
addons/keyboardmacros/keyboardmacrosplugin.h
View file @
8e07889c
...
...
@@ -122,7 +122,7 @@ class KeyboardMacrosPluginCommands : public KTextEditor::Command
public:
explicit
KeyboardMacrosPluginCommands
(
KeyboardMacrosPlugin
*
plugin
);
bool
exec
(
KTextEditor
::
View
*
view
,
const
QString
&
cmd
,
QString
&
msg
,
const
KTextEditor
::
Range
&
=
KTextEditor
::
Range
::
invalid
()
)
override
;
bool
exec
(
KTextEditor
::
View
*
view
,
const
QString
&
cmd
,
QString
&
msg
,
const
KTextEditor
::
Range
&
)
override
;
bool
help
(
KTextEditor
::
View
*
,
const
QString
&
cmd
,
QString
&
msg
)
override
;
private:
...
...
addons/keyboardmacros/keycombination.h
View file @
8e07889c
...
...
@@ -39,17 +39,17 @@ public:
text
=
json
.
at
(
2
).
toString
();
};
QKeyEvent
*
keyPress
()
const
QKeyEvent
keyPress
()
const
{
return
new
QKeyEvent
(
QEvent
::
KeyPress
,
key
,
modifiers
,
text
);
return
QKeyEvent
(
QEvent
::
KeyPress
,
key
,
modifiers
,
text
);
};
QKeyEvent
*
keyRelease
()
const
QKeyEvent
keyRelease
()
const
{
return
new
QKeyEvent
(
QEvent
::
KeyRelease
,
key
,
modifiers
,
text
);
return
QKeyEvent
(
QEvent
::
KeyRelease
,
key
,
modifiers
,
text
);
};
QJsonArray
toJson
()
const
const
QJsonArray
toJson
()
const
{
QJsonArray
json
;
json
.
append
(
QJsonValue
(
key
));
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment