File indexing completed on 2025-01-05 05:19:56

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Pablo Rauzy <r .at. uzy .dot. me>
0003  * SPDX-License-Identifier: LGPL-2.0-or-later
0004  */
0005 
0006 #pragma once
0007 
0008 #include <QEvent>
0009 #include <QKeySequence>
0010 #include <QList>
0011 #include <QObject>
0012 #include <QPointer>
0013 #include <QString>
0014 #include <QVariant>
0015 
0016 #include <KTextEditor/Application>
0017 #include <KTextEditor/MainWindow>
0018 #include <KTextEditor/Message>
0019 #include <KTextEditor/Plugin>
0020 #include <KTextEditor/View>
0021 
0022 #include "macro.h"
0023 
0024 class KeyboardMacrosPluginView;
0025 class KeyboardMacrosPluginCommands;
0026 
0027 class KeyboardMacrosPlugin : public KTextEditor::Plugin
0028 {
0029     Q_OBJECT
0030 
0031     friend KeyboardMacrosPluginView;
0032     friend KeyboardMacrosPluginCommands;
0033 
0034     // GUI elements
0035     QList<QPointer<KeyboardMacrosPluginView>> m_pluginViews;
0036     KeyboardMacrosPluginCommands *m_commands;
0037 
0038     // State and logic
0039     bool m_recording = false;
0040     QPointer<QWidget> m_focusWidget;
0041     QKeySequence m_recordActionShortcut;
0042     QKeySequence m_playActionShortcut;
0043     Macro m_tape;
0044     Macro m_macro;
0045     QString m_storage;
0046     bool m_namedMacrosLoaded = false;
0047     std::map<QString, Macro> m_namedMacros;
0048     QSet<QString> m_wipedMacros;
0049 
0050     // Plugin creation and destruction
0051 public:
0052     explicit KeyboardMacrosPlugin(QObject *parent = nullptr, const QVariantList & = QVariantList());
0053     ~KeyboardMacrosPlugin() override;
0054     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
0055 
0056 private:
0057     void loadNamedMacros();
0058     void saveNamedMacros();
0059 
0060     // GUI feedback helpers
0061     void sendMessage(const QString &text, bool error);
0062     void displayMessage(const QString &text, KTextEditor::Message::MessageType type);
0063 
0064     // Events filter and focus management helpers
0065 public:
0066     bool eventFilter(QObject *obj, QEvent *event) override;
0067 
0068 private:
0069     void focusObjectChanged(QObject *focusObject);
0070     void applicationStateChanged(Qt::ApplicationState state);
0071 
0072     // Core functions
0073     void record();
0074     void cancel();
0075     void stop(bool save);
0076     bool play(const QString &name = QString());
0077     bool save(const QString &name);
0078     bool load(const QString &name);
0079     bool wipe(const QString &name);
0080 };