-
Notifications
You must be signed in to change notification settings - Fork 0
/
plginterface.cpp
118 lines (99 loc) · 4.03 KB
/
plginterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "plginterface.h"
#include "PythonQt/PythonQtCppWrapperFactory.h"
#include "PythonQt/PythonQt_QtAll.h"
#include "iwingpluginstructwrapper.h"
#include <QKeySequence>
#include <QListWidgetItem>
#include <QShortcut>
#include <QTemporaryFile>
PlgInterface *PlgInterface::m_instace = nullptr;
bool PlgInterface::init() {
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
PythonQt_QtAll::init();
auto py = PythonQt::self();
py->registerClass(&QListWidget::staticMetaObject, "QtGui");
py->registerClass(&QTableWidget::staticMetaObject, "QtGui");
py->registerClass(&QTreeWidget::staticMetaObject, "QtGui");
py->registerClass(&QTextBrowser::staticMetaObject, "QtGui");
py->registerCPPClass("plgreader", nullptr, "wingplg",
PythonQtCreateObject<WingPlugin::Reader>);
py->registerCPPClass("plgcontroller", nullptr, "wingplg",
PythonQtCreateObject<WingPlugin::Controller>);
py->registerCPPClass("plgservice", nullptr, "wingplg",
PythonQtCreateObject<PlgService>);
py->addDecorators(new IWingPluginStructWrapper());
qRegisterMetaType<HexPosition>("HexPosition");
qRegisterMetaType<BookMark>("BookMark");
qRegisterMetaType<ErrFile>("ErrFile");
qRegisterMetaType<FindResult>("FindResult");
qRegisterMetaType<HexMetadataAbsoluteItem>("HexMetadataAbsoluteItem");
qRegisterMetaType<HexMetadataItem>("HexMetadataItem");
qRegisterMetaType<HexLineMetadata>("HexLineMetadata");
mainContext = PythonQt::self()->getMainModule();
mainContext.addObject("reader", &plg->reader);
mainContext.addObject("controller", &plg->controller);
mainContext.addObject("service", new PlgService(this));
mainContext.evalScript("from PythonQt import *");
console = new PythonQtScriptingConsole(nullptr, mainContext);
m_instace = this;
auto shortcut = new QShortcut(
QKeySequence(Qt::KeyboardModifier::ControlModifier | Qt::Key_L), console);
connect(shortcut, &QShortcut::activated, this, [=] { console->clear(); });
return true;
}
void PlgInterface::initInfo(QListWidget *infolist, QTreeWidget *infotree,
QTableWidget *infotable, QTextBrowser *infotxt) {
mainContext.addObject("infotree", infotree);
mainContext.addObject("infotable", infotable);
mainContext.addObject("infolist", infolist);
mainContext.addObject("infotxt", infotxt);
}
PythonQtScriptingConsole *PlgInterface::getScriptingConsole() {
return console;
}
bool PlgInterface::RunPyFile(const QString &filename) {
if (mutex.tryLock()) {
auto cur = console->textCursor();
cur.movePosition(QTextCursor::End);
console->setTextCursor(cur);
console->insertPlainText(filename);
mainContext.evalFile(filename);
console->appendCommandPrompt();
if (plg->hasControl())
plg->requestRelease();
mutex.unlock();
return true;
}
return false;
}
bool PlgInterface::requestControl(int timeout) {
return plg->requestControl(timeout);
}
bool PlgInterface::requestRelease() { return plg->requestRelease(); }
bool PlgInterface::hasControl() { return plg->hasControl(); }
void PlgInterface::toast(const QIcon &icon, const QString &message) {
plg->toast(icon, message);
}
void PlgInterface::logWarn(const QString &message) { plg->warn(message); }
void PlgInterface::logError(const QString &message) { plg->error(message); }
void PlgInterface::logInfo(const QString &message) { plg->info(message); }
void PlgInterface::logDebug(const QString &message) { plg->debug(message); }
bool PlgInterface::RunPyText(const QString &content) {
if (mutex.tryLock()) {
auto cur = console->textCursor();
cur.movePosition(QTextCursor::End);
console->setTextCursor(cur);
console->insertPlainText(tr("[ExcuteFromScriptWindow]"));
mainContext.evalScript(content);
console->appendCommandPrompt();
if (plg->hasControl())
plg->requestRelease();
mutex.unlock();
return true;
}
return false;
}
PlgInterface::PlgInterface(IWingPlugin *parent) : QObject(parent) {
plg = parent;
}
PlgInterface *PlgInterface::instance() { return m_instace; }