kate-deft/src/plugin/ollieplugin.h

99 lines
3.0 KiB
C++

/*
* SPDX-License-Identifier: LGPL-2.0-or-later
*
* The Ollie command-palette plugin for Kate / KTextEditor.
*
* OlliePlugin is the KTextEditor::Plugin entry point. For every MainWindow the
* host creates an OllieView, which registers the "Command Palette (M-x)" action
* (Alt+X) and, when triggered, gathers every QAction currently exposed by the
* window's GUI clients into a PaletteWidget for orderless fuzzy selection.
*/
#ifndef KATECUSTOM_OLLIEPLUGIN_H
#define KATECUSTOM_OLLIEPLUGIN_H
#include <KTextEditor/Plugin>
#include <QObject>
#include <QPointer>
#include <QHash>
#include <QList>
class QAction;
namespace KTextEditor
{
class MainWindow;
}
namespace katecustom
{
class PaletteWidget;
class RadialMenu;
struct RadialDef;
class OlliePlugin : public KTextEditor::Plugin
{
Q_OBJECT
public:
explicit OlliePlugin(QObject *parent, const QVariantList &args = {});
~OlliePlugin() override;
QObject *createView(KTextEditor::MainWindow *mainWindow) override;
};
class OllieView : public QObject
{
Q_OBJECT
public:
OllieView(OlliePlugin *plugin, KTextEditor::MainWindow *mainWindow);
~OllieView() override;
private Q_SLOTS:
void showCommandPalette();
void runAction(const QString &actionId);
private:
// One harvested, runnable action with the provenance needed to build a
// unique id and to disambiguate labels shared by several commands.
struct ActionEntry {
QPointer<QAction> action;
QString label; // visible text, ampersands stripped
QString component; // owning collection's display name (provenance)
QString shortcut; // current shortcut, native text
QString id; // unique, stable key used on activation
};
// Collect every unique, enabled, visible QAction reachable from the
// window's GUI factory clients, deduplicated by pointer identity.
QList<ActionEntry> collectActions() const;
// Find a Kate action by its raw objectName across all GUI clients. Used to
// resolve radial-menu slice ids (which reference Kate action names).
QAction *findActionByName(const QString &objectName) const;
// Build the configured radials and bind each to its trigger key.
void setupRadials();
void showRadial(int defIndex); // centre on the caret
void showRadialAt(int defIndex, const QPoint &globalPos); // centre on a point
void onRadialActivated(const QString &actionId);
// Catches configured mouse-button gestures inside the active view.
bool eventFilter(QObject *obj, QEvent *event) override;
KTextEditor::MainWindow *m_mainWindow;
PaletteWidget *m_palette = nullptr;
RadialMenu *m_radial = nullptr;
QList<RadialDef> m_radialDefs;
QHash<int, int> m_buttonToRadial; // Qt::MouseButton (int) -> def index
bool m_gestureActive = false; // an RMB drag gesture is in progress
bool m_leftDownInView = false; // LMB held inside the view (Acme chord anchor)
QHash<QString, QPointer<QAction>> m_actionsById;
};
} // namespace katecustom
#endif