Skip to content

Commit

Permalink
introduce a plugin API: IPlugin::globalShortcuts()
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokin committed Feb 18, 2020
1 parent 562eb3c commit 40892ac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions include/IPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class IPlugin {
public:
virtual QMenu *menu(QWidget *parent = nullptr) = 0;

public:
// optional, overload this to provide actions that are available by global shortcuts
virtual QList<QAction *> globalShortcuts() { return {}; }

public:
// optional, overload these to have there contents added to a view's context menu
virtual QList<QAction *> cpuContextMenu() { return {}; }
Expand Down
24 changes: 16 additions & 8 deletions plugins/Assembler/Assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ namespace AssemblerPlugin {
* @param parent
*/
Assembler::Assembler(QObject *parent)
: QObject(parent) {
: QObject(parent),
action_assemble_(tr("&Assemble..."), this) {
action_assemble_.setShortcut(QKeySequence(tr("Space")));
connect(&action_assemble_, &QAction::triggered, this, &Assembler::showDialog);
}

/**
Expand All @@ -44,19 +47,24 @@ Assembler::~Assembler() {
}

/**
* @brief Assembler::cpuContextMenu
* @brief Assembler::globalShortcuts
* @return
*/
QList<QAction *> Assembler::cpuContextMenu() {
QList<QAction *> Assembler::globalShortcuts() {

QList<QAction *> ret;
ret << &action_assemble_;
return ret;
}

auto action_assemble = new QAction(tr("&Assemble..."), this);
action_assemble->setShortcut(QKeySequence(tr("Space")));

connect(action_assemble, &QAction::triggered, this, &Assembler::showDialog);
ret << action_assemble;
/**
* @brief Assembler::cpuContextMenu
* @return
*/
QList<QAction *> Assembler::cpuContextMenu() {

QList<QAction *> ret;
ret << &action_assemble_;
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/Assembler/Assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define ASSEMBLER_H_20130611_

#include "IPlugin.h"
#include <QAction>

class QMenu;
class QDialog;
Expand All @@ -39,6 +40,7 @@ class Assembler : public QObject, public IPlugin {

public:
QMenu *menu(QWidget *parent = nullptr) override;
QList<QAction *> globalShortcuts() override;
QList<QAction *> cpuContextMenu() override;
QWidget *optionsPage() override;

Expand All @@ -47,6 +49,7 @@ class Assembler : public QObject, public IPlugin {

private:
QPointer<QDialog> dialog_ = nullptr;
QAction action_assemble_;
};

}
Expand Down
7 changes: 2 additions & 5 deletions src/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,12 @@ void Debugger::finishPluginSetup() {
}

// setup the shortcuts for these actions
const QList<QAction *> register_actions = p->registerContextMenu();
const QList<QAction *> cpu_actions = p->cpuContextMenu();
const QList<QAction *> stack_actions = p->stackContextMenu();
const QList<QAction *> data_actions = p->dataContextMenu();
const QList<QAction *> actions = register_actions + cpu_actions + stack_actions + data_actions;
const QList<QAction *> actions = p->globalShortcuts();

for (QAction *action : actions) {
QKeySequence shortcut = action->shortcut();
if (!shortcut.isEmpty()) {
std::cerr << shortcut.toString().toUtf8().constData() << std::endl;
connect(new QShortcut(shortcut, this), &QShortcut::activated, action, &QAction::trigger);
}
}
Expand Down

0 comments on commit 40892ac

Please sign in to comment.