Commit 7e13e633 authored by Pablo Rauzy's avatar Pablo Rauzy Committed by Christoph Cullmann
Browse files

make the play action work, use menus instead of input dialog, also refactoring...

make the play action work, use menus instead of input dialog, also refactoring code for cosmetics + adding GUI cosmetics
parent 63fabeef
......@@ -15,6 +15,8 @@
#include <QVariant>
#include <QVariantMap>
#include <KActionMenu>
#include <KTextEditor/Application>
#include <KTextEditor/Command>
#include <KTextEditor/MainWindow>
......@@ -34,68 +36,67 @@ class KeyboardMacrosPlugin : public KTextEditor::Plugin
friend KeyboardMacrosPluginView;
friend KeyboardMacrosPluginCommands;
// GUI elements
KTextEditor::MainWindow *m_mainWindow = nullptr;
KeyboardMacrosPluginView *m_pluginView = nullptr;
QAction *m_recordAction = nullptr;
QAction *m_cancelAction = nullptr;
QAction *m_playAction = nullptr;
QAction *m_saveAction = nullptr;
QPointer<QWidget> m_focusWidget;
KeyboardMacrosPluginCommands *m_commands;
// state and logic
bool m_recording = false;
Macro m_tape;
Macro m_macro;
QString m_storage;
QMap<QString, Macro> m_namedMacros;
QSet<QString> m_deletedMacros;
public:
// Plugin creation and destruction
explicit KeyboardMacrosPlugin(QObject *parent = nullptr, const QList<QVariant> & = QList<QVariant>());
~KeyboardMacrosPlugin() override;
QObject *createView(KTextEditor::MainWindow *mainWindow) override;
private:
void loadNamedMacros();
void saveNamedMacros();
// GUI feedback helpers
void sendMessage(const QString &text, bool error);
void displayMessage(const QString &text, KTextEditor::Message::MessageType type);
Q_SIGNALS:
void message(const QVariantMap &message);
// Events filter and focus management helpers
public:
bool eventFilter(QObject *obj, QEvent *event) override;
private:
KTextEditor::MainWindow *m_mainWindow = nullptr;
QPointer<QWidget> m_focusWidget;
QPointer<KTextEditor::Message> m_message;
KeyboardMacrosPluginCommands *m_commands;
QAction *m_recordAction = nullptr;
QAction *m_cancelAction = nullptr;
QAction *m_playAction = nullptr;
QAction *m_saveNamedAction = nullptr;
QAction *m_loadNamedAction = nullptr;
// QAction *m_playNamedAction = nullptr;
QAction *m_deleteNamedAction = nullptr;
void focusObjectChanged(QObject *focusObject);
void applicationStateChanged(Qt::ApplicationState state);
// Core functions
void record();
void stop(bool save);
void cancel();
bool play(const QString &name = QString());
bool save(const QString &name);
bool load(const QString &name);
bool remove(const QString &name);
QString queryName(const QString &query, const QString &action);
bool m_recording = false;
Macro m_tape;
Macro m_macro;
QMap<QString, Macro> m_namedMacros;
QString m_storage;
void loadNamedMacros();
void saveNamedMacros();
void focusObjectChanged(QObject *focusObject);
void applicationStateChanged(Qt::ApplicationState state);
// Action slots
public Q_SLOTS:
void slotRecord();
void slotCancel();
void slotPlay();
void slotSaveNamed();
void slotLoadNamed();
// void slotPlayNamed();
void slotDeleteNamed();
void slotSave();
void slotLoadNamed(const QString &name = QString());
void slotPlayNamed(const QString &name = QString());
void slotDeleteNamed(const QString &name = QString());
};
/**
......@@ -109,8 +110,19 @@ public:
explicit KeyboardMacrosPluginView(KeyboardMacrosPlugin *plugin, KTextEditor::MainWindow *mainwindow);
~KeyboardMacrosPluginView() override;
void addNamedMacro(const QString &name, const Macro &macro);
void removeNamedMacro(const QString &name);
private:
KeyboardMacrosPlugin *m_plugin;
KTextEditor::MainWindow *m_mainWindow;
QMap<QString, QAction *> m_namedMacrosLoadActions;
QMap<QString, QAction *> m_namedMacrosPlayActions;
QMap<QString, QAction *> m_namedMacrosDeleteActions;
KActionMenu *m_loadMenu;
KActionMenu *m_playMenu;
KActionMenu *m_deleteMenu;
};
/**
......
......@@ -6,27 +6,30 @@
#ifndef KEYBOARDMACROSPLUGIN_KEYCOMBINATION_H
#define KEYBOARDMACROSPLUGIN_KEYCOMBINATION_H
#include <QApplication>
#include <QDebug>
#include <QJsonArray>
#include <QJsonValue>
#include <QKeyEvent>
#include <QKeySequence>
#include <QList>
#include <QPointer>
#include <QString>
#include <QWidget>
#include <QtGlobal>
class KeyCombination
{
private:
int key;
Qt::KeyboardModifiers modifiers;
QString text;
int m_key;
Qt::KeyboardModifiers m_modifiers;
QString m_text;
public:
explicit KeyCombination(const QKeyEvent *keyEvent)
: key(keyEvent->key())
, modifiers(keyEvent->modifiers())
, text(keyEvent->text()){};
: m_key(keyEvent->key())
, m_modifiers(keyEvent->modifiers())
, m_text(keyEvent->text()){};
explicit KeyCombination(const QJsonArray &json)
{
......@@ -34,33 +37,46 @@ public:
Q_ASSERT(json.at(0).type() == QJsonValue::Double);
Q_ASSERT(json.at(1).type() == QJsonValue::Double);
Q_ASSERT(json.at(2).type() == QJsonValue::String);
key = json.at(0).toInt(0);
modifiers = static_cast<Qt::KeyboardModifiers>(json.at(1).toInt(0));
text = json.at(2).toString();
m_key = json.at(0).toInt(0);
m_modifiers = static_cast<Qt::KeyboardModifiers>(json.at(1).toInt(0));
m_text = json.at(2).toString();
};
const QKeyEvent keyPress() const
{
return QKeyEvent(QEvent::KeyPress, key, modifiers, text);
return QKeyEvent(QEvent::KeyPress, m_key, m_modifiers, m_text);
};
const QKeyEvent keyRelease() const
{
return QKeyEvent(QEvent::KeyRelease, key, modifiers, text);
return QKeyEvent(QEvent::KeyRelease, m_key, m_modifiers, m_text);
};
const QJsonArray toJson() const
{
QJsonArray json;
json.append(QJsonValue(key));
json.append(QJsonValue(static_cast<int>(modifiers)));
json.append(QJsonValue(text));
json.append(QJsonValue(m_key));
json.append(QJsonValue(static_cast<int>(m_modifiers)));
json.append(QJsonValue(m_text));
return json;
};
const QString toString() const
{
if (isVisibleInput()) {
return m_text;
}
return QKeySequence(m_key | m_modifiers).toString();
};
bool isVisibleInput() const
{
return m_text.size() == 1 && (m_modifiers == Qt::NoModifier || m_modifiers == Qt::ShiftModifier) && m_text.at(0).isPrint();
}
friend QDebug operator<<(QDebug dbg, const KeyCombination &kc)
{
return dbg << QKeySequence(kc.key | kc.modifiers).toString();
return dbg << kc.toString();
};
};
......
......@@ -39,6 +39,24 @@ public:
}
return json;
};
const QString toString() const
{
QString str;
for (const auto &kc : *this) {
if (kc.isVisibleInput()) {
str += kc.toString();
} else {
str += QStringLiteral(" ") + kc.toString() + QStringLiteral(" ");
}
}
int len = str.size();
str = str.trimmed().replace(QStringLiteral(" "), QStringLiteral(" ")).left(32);
if (len > str.size()) {
str += QStringLiteral("...");
}
return str;
};
};
#endif
......@@ -10,10 +10,10 @@
<Action name="keyboardmacros_record"/>
<Action name="keyboardmacros_cancel"/>
<Action name="keyboardmacros_play"/>
<Action name="keyboardmacros_save"/>
<Separator/>
<Action name="keyboardmacros_named_save"/>
<Action name="keyboardmacros_named_load"/>
<!-- <Action name="keyboardmacros_named_play"/> -->
<Action name="keyboardmacros_named_play"/>
<Action name="keyboardmacros_named_delete"/>
</Menu>
</Menu>
......
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