Commit 16eaade1 authored by Pablo Rauzy's avatar Pablo Rauzy Committed by Christoph Cullmann
Browse files

[WIP] capturing events, but still not able to emit them back

parent de55c41c
......@@ -5,25 +5,27 @@
#include "plugin_katekeyboardmacro.h"
#include <QAction>
#include <QCoreApplication>
#include <QString>
#include <KTextEditor/Editor>
#include <KTextEditor/Message>
#include <KLocalizedString>
#include <QAction>
#include <QString>
#include <KActionCollection>
#include <KPluginFactory>
#include <KXMLGUIFactory>
#include <iostream>
#include <qevent.h>
K_PLUGIN_FACTORY_WITH_JSON(KeyboardMacroPluginFactory, "keyboardmacroplugin.json", registerPlugin<PluginKateKeyboardMacro>();)
PluginKateKeyboardMacro::PluginKateKeyboardMacro(QObject *parent, const QList<QVariant> &)
: KTextEditor::Plugin(parent)
{
// register commands
// register "recmac" and "runmac" commands
m_recCommand = new PluginKateKeyboardMacroRecordCommand(this);
m_runCommand = new PluginKateKeyboardMacroRunCommand(this);
}
......@@ -40,17 +42,39 @@ QObject *PluginKateKeyboardMacro::createView(KTextEditor::MainWindow *mainWindow
return new PluginViewKateKeyboardMacro(this, mainWindow);
}
// https://doc.qt.io/qt-6/eventsandfilters.html
// https://doc.qt.io/qt-6/qobject.html#installEventFilter
// https://stackoverflow.com/questions/41631011/my-qt-eventfilter-doesnt-stop-events-as-it-should
// file:///usr/share/qt5/doc/qtcore/qobject.html#installEventFilter
// file:///usr/share/qt5/doc/qtcore/qcoreapplication.html#sendEvent
// also see postEvent, sendPostedEvents, etc
bool PluginKateKeyboardMacro::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = new QKeyEvent(*static_cast<QKeyEvent *>(event));
m_keyEvents.enqueue(keyEvent);
qDebug("Capture key press: %s", keyEvent->text().toUtf8().data());
return true;
} else {
return QObject::eventFilter(obj, event);
}
}
bool PluginKateKeyboardMacro::record(KTextEditor::View *)
{
if (m_recording) {
// stop recording
if (m_recording) { // end recording
// KTextEditor::Editor::instance()->application()->activeMainWindow()->window()->removeEventFilter(this);
QCoreApplication::instance()->removeEventFilter(this);
std::cerr << "stop recording" << std::endl;
m_recording = false;
m_macro = QStringLiteral("foobar");
return true; // if success
}
// start recording
std::cerr << "start recording" << std::endl;
QCoreApplication::instance()->installEventFilter(this);
m_recording = true;
return true;
}
......@@ -58,14 +82,17 @@ bool PluginKateKeyboardMacro::record(KTextEditor::View *)
bool PluginKateKeyboardMacro::run(KTextEditor::View *view)
{
if (m_recording) {
return false;
// end recording before running macro
record(view);
}
if (m_macro.isEmpty()) {
return false;
while (!m_keyEvents.isEmpty()) {
QKeyEvent *keyEvent = m_keyEvents.dequeue();
qDebug("Emit key press: %s", keyEvent->text().toUtf8().data());
QCoreApplication::sendEvent(QCoreApplication::instance(), keyEvent);
delete keyEvent;
}
view->insertText(m_macro);
return true;
}
......@@ -102,6 +129,40 @@ void PluginKateKeyboardMacro::slotRun()
run(view);
}
// BEGIN Plugin view to add our actions to the gui
PluginViewKateKeyboardMacro::PluginViewKateKeyboardMacro(PluginKateKeyboardMacro *plugin, KTextEditor::MainWindow *mainwindow)
: QObject(mainwindow)
, m_mainWindow(mainwindow)
{
// setup xml gui
KXMLGUIClient::setComponentName(QStringLiteral("keyboardmacro"), i18n("Keyboard Macro"));
setXMLFile(QStringLiteral("ui.rc"));
// create record action
QAction *rec = actionCollection()->addAction(QStringLiteral("record_macro"));
rec->setText(i18n("&Record Macro..."));
actionCollection()->setDefaultShortcut(rec, Qt::CTRL | Qt::SHIFT | Qt::Key_K);
connect(rec, &QAction::triggered, plugin, &PluginKateKeyboardMacro::slotRecord);
// create run action
QAction *run = actionCollection()->addAction(QStringLiteral("run_macro"));
run->setText(i18n("&Run Macro"));
actionCollection()->setDefaultShortcut(run, Qt::CTRL | Qt::ALT | Qt::Key_K);
connect(run, &QAction::triggered, plugin, &PluginKateKeyboardMacro::slotRun);
// register our gui elements
mainwindow->guiFactory()->addClient(this);
}
PluginViewKateKeyboardMacro::~PluginViewKateKeyboardMacro()
{
// remove us from the gui
m_mainWindow->guiFactory()->removeClient(this);
}
// END
// BEGIN commands
PluginKateKeyboardMacroRecordCommand::PluginKateKeyboardMacroRecordCommand(PluginKateKeyboardMacro *plugin)
......@@ -115,8 +176,7 @@ bool PluginKateKeyboardMacroRecordCommand::exec(KTextEditor::View *view, const Q
if (m_plugin->isRecording()) {
// remove from the recording the call to this command…
}
bool success = m_plugin->record(view);
if (!success) {
if (!m_plugin->record(view)) {
// display fail in toolview
}
return true;
......@@ -124,9 +184,7 @@ bool PluginKateKeyboardMacroRecordCommand::exec(KTextEditor::View *view, const Q
bool PluginKateKeyboardMacroRecordCommand::help(KTextEditor::View *, const QString &, QString &msg)
{
msg = i18n(
"<qt><p>Usage: <code>recmac</code></p>"
"<p>Start/stop recording a keyboard macro.</p></qt>");
msg = i18n("<qt><p>Usage: <code>recmac</code></p><p>Start/stop recording a keyboard macro.</p></qt>");
return true;
}
......@@ -138,8 +196,7 @@ PluginKateKeyboardMacroRunCommand::PluginKateKeyboardMacroRunCommand(PluginKateK
bool PluginKateKeyboardMacroRunCommand::exec(KTextEditor::View *view, const QString &, QString &, const KTextEditor::Range &)
{
bool success = m_plugin->run(view);
if (!success) {
if (!m_plugin->run(view)) {
// display fail in toolview
}
return true;
......@@ -147,43 +204,11 @@ bool PluginKateKeyboardMacroRunCommand::exec(KTextEditor::View *view, const QStr
bool PluginKateKeyboardMacroRunCommand::help(KTextEditor::View *, const QString &, QString &msg)
{
msg = i18n(
"<qt><p>Usage: <code>runmac</code></p>"
"<p>Run recorded keyboard macro.</p></qt>");
msg = i18n("<qt><p>Usage: <code>runmac</code></p><p>Run recorded keyboard macro.</p></qt>");
return true;
}
// END
PluginViewKateKeyboardMacro::PluginViewKateKeyboardMacro(PluginKateKeyboardMacro *plugin, KTextEditor::MainWindow *mainwindow)
: QObject(mainwindow)
, m_mainWindow(mainwindow)
{
// setup right xml gui data
KXMLGUIClient::setComponentName(QStringLiteral("keyboardmacro"), i18n("Keyboard Macro"));
setXMLFile(QStringLiteral("ui.rc"));
// create record action
QAction *rec = actionCollection()->addAction(QStringLiteral("record_macro"));
rec->setText(i18n("&Record Macro..."));
actionCollection()->setDefaultShortcut(rec, Qt::CTRL | Qt::SHIFT | Qt::Key_K);
connect(rec, &QAction::triggered, plugin, &PluginKateKeyboardMacro::slotRecord);
// create run action
QAction *run = actionCollection()->addAction(QStringLiteral("run_macro"));
run->setText(i18n("&Run Macro"));
actionCollection()->setDefaultShortcut(run, Qt::CTRL | Qt::ALT | Qt::Key_K);
connect(run, &QAction::triggered, plugin, &PluginKateKeyboardMacro::slotRun);
// register us at the UI
mainwindow->guiFactory()->addClient(this);
}
PluginViewKateKeyboardMacro::~PluginViewKateKeyboardMacro()
{
// remove us from the UI again
m_mainWindow->guiFactory()->removeClient(this);
}
// required for KeyboardMacroPluginFactory vtable
#include "plugin_katekeyboardmacro.moc"
......@@ -6,6 +6,9 @@
#ifndef PLUGIN_KATEKEYBOARDMACRO_H
#define PLUGIN_KATEKEYBOARDMACRO_H
#include <QKeyEvent>
#include <QQueue>
#include <KTextEditor/Application>
#include <KTextEditor/Command>
#include <KTextEditor/MainWindow>
......@@ -20,15 +23,14 @@ class PluginKateKeyboardMacro : public KTextEditor::Plugin
Q_OBJECT
public:
/**
* Plugin constructor.
*/
explicit PluginKateKeyboardMacro(QObject *parent = nullptr, const QList<QVariant> & = QList<QVariant>());
~PluginKateKeyboardMacro() override;
QObject *createView(KTextEditor::MainWindow *mainWindow) override;
bool eventFilter(QObject *obj, QEvent *event) override;
bool record(KTextEditor::View *view);
bool run(KTextEditor::View *view);
bool isRecording();
......@@ -37,7 +39,7 @@ private:
KTextEditor::MainWindow *m_mainWindow;
bool m_recording = false;
QString m_macro;
QQueue<QKeyEvent *> m_keyEvents;
PluginKateKeyboardMacroRecordCommand *m_recCommand;
PluginKateKeyboardMacroRunCommand *m_runCommand;
......@@ -48,32 +50,29 @@ public Q_SLOTS:
};
/**
* recmac command
* Plugin view to add our actions to the gui
*/
class PluginKateKeyboardMacroRecordCommand : public KTextEditor::Command
class PluginViewKateKeyboardMacro : public QObject, public KXMLGUIClient
{
Q_OBJECT
public:
PluginKateKeyboardMacroRecordCommand(PluginKateKeyboardMacro *plugin);
// Kate::Command
bool exec(KTextEditor::View *view, const QString &, QString &, const KTextEditor::Range & = KTextEditor::Range::invalid()) override;
bool help(KTextEditor::View *view, const QString &, QString &msg) override;
explicit PluginViewKateKeyboardMacro(PluginKateKeyboardMacro *plugin, KTextEditor::MainWindow *mainwindow);
~PluginViewKateKeyboardMacro() override;
private:
PluginKateKeyboardMacro *m_plugin;
KTextEditor::MainWindow *m_mainWindow;
};
/**
* runmac command
* recmac command
*/
class PluginKateKeyboardMacroRunCommand : public KTextEditor::Command
class PluginKateKeyboardMacroRecordCommand : public KTextEditor::Command
{
Q_OBJECT
public:
PluginKateKeyboardMacroRunCommand(PluginKateKeyboardMacro *plugin);
// Kate::Command
PluginKateKeyboardMacroRecordCommand(PluginKateKeyboardMacro *plugin);
bool exec(KTextEditor::View *view, const QString &, QString &, const KTextEditor::Range & = KTextEditor::Range::invalid()) override;
bool help(KTextEditor::View *view, const QString &, QString &msg) override;
......@@ -82,30 +81,19 @@ private:
};
/**
* Plugin view to merge the actions into the UI
* runmac command
*/
class PluginViewKateKeyboardMacro : public QObject, public KXMLGUIClient
class PluginKateKeyboardMacroRunCommand : public KTextEditor::Command
{
Q_OBJECT
public:
/**
* Construct plugin view
* @param plugin our plugin
* @param mainwindows the mainwindow for this view
*/
explicit PluginViewKateKeyboardMacro(PluginKateKeyboardMacro *plugin, KTextEditor::MainWindow *mainwindow);
/**
* Our Destructor
*/
~PluginViewKateKeyboardMacro() override;
PluginKateKeyboardMacroRunCommand(PluginKateKeyboardMacro *plugin);
bool exec(KTextEditor::View *view, const QString &, QString &, const KTextEditor::Range & = KTextEditor::Range::invalid()) override;
bool help(KTextEditor::View *view, const QString &, QString &msg) override;
private:
/**
* the main window we belong to
*/
KTextEditor::MainWindow *m_mainWindow;
PluginKateKeyboardMacro *m_plugin;
};
#endif
Supports Markdown
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