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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include "PythonPlugin.h"
#include "Log.h"
#include <QtCore>
static Juff::Document* CurrentDoc = NULL;
bool addArgument(PyObject* args, int index, const char* argument);
bool addArgument(PyObject* args, int index, int argument);
bool addArgument(PyObject* args, int index, bool argument);
/*static PyObject* method_preved (PyObject *self, PyObject *args) {
if ( !PyArg_ParseTuple(args, ":preved") ) {
return NULL;
}
return Py_BuildValue("s", "Preved!");
}
static PyMethodDef MedvedMethods[] = {
{ "preved", method_preved, METH_VARARGS, "Returns 123" },
{ NULL, NULL, 0, NULL },
};*/
static PyObject* getCursorPos(PyObject* self, PyObject* args) {
LOGGER;
Q_UNUSED(self);
Q_UNUSED(args);
if ( CurrentDoc == NULL ) {
return NULL;
}
int row, col;
CurrentDoc->getCursorPos(row, col);
return Py_BuildValue("[i,i]", row, col);
}
static PyObject* setCursorPos(PyObject* self, PyObject* args) {
LOGGER;
Q_UNUSED(self);
if ( CurrentDoc == NULL ) {
return NULL;
}
int row, col;
if ( !PyArg_ParseTuple(args, "i|i", &row, &col) ) {
return Py_BuildValue("s", "Error: can't parse arguments. Expecting 2 integer parameters.");
}
// qDebug() << row << "," << col;
CurrentDoc->setCursorPos(row, col);
return Py_BuildValue("s", "");
}
static PyMethodDef DocMethods[] = {
{ "getCursorPos", getCursorPos, METH_VARARGS, "" },
{ "setCursorPos", setCursorPos, METH_VARARGS, "" },
{ NULL, NULL, 0, NULL },
};
PythonPlugin::PythonPlugin(): QObject(), JuffPlugin() {
LOGGER;
globalDict_ = NULL;
// Open and execute the Python file
FILE* file = fopen("/home/mrz/script_plugin.py", "r");
Py_Initialize();
Py_InitModule("juffed_doc", DocMethods);
if ( file != NULL ) {
PyRun_SimpleFile(file, "");
// Get a reference to the main module and global dictionary
PyObject* mainModule = PyImport_AddModule("__main__");
if ( mainModule != NULL ) {
globalDict_ = PyModule_GetDict(mainModule);
}
else {
Log::debug("Couldn't get main module");
}
}
else {
Log::debug("Couldn't open script file");
}
}
PythonPlugin::~PythonPlugin() {
Py_DECREF(globalDict_);
Py_Finalize();
}
void PythonPlugin::init() {
LOGGER;
connect(api(), SIGNAL(docOpened(Juff::Document*, Juff::PanelIndex)), SLOT(onDocOpened(Juff::Document*, Juff::PanelIndex)));
connect(api(), SIGNAL(docActivated(Juff::Document*)), SLOT(onDocActivated(Juff::Document*)));
}
QString PythonPlugin::name() const {
PyObject* func = getFunction("name");
if ( func != NULL ) {
PyObject* res = PyObject_CallObject(func, NULL);
if ( res != NULL ) {
return QString::fromUtf8(PyString_AsString(res));
}
}
return "";
}
QString PythonPlugin::title() const {
return tr("Python support");
}
QString PythonPlugin::description() const {
return "";
}
QString PythonPlugin::targetEngine() const {
return "all";
}
Juff::ActionList PythonPlugin::getActions() const {
Juff::ActionList list;
PyObject* func = getFunction("getAction");
if ( func != NULL ) {
int index = 0;
PyObject* arg = PyTuple_New(1);
PyObject* res;
while ( true ) {
addArgument(arg, 0, index);
res = PyObject_CallObject(func, arg);
QString str = QString::fromUtf8(PyString_AsString(res));
// qDebug() << str;
if ( str.isEmpty() ) {
break;
}
QStringList fields = str.split('|');
if ( fields.count() >= 3 ) {
QAction* action = new QAction(fields[0], NULL);
action->setShortcut(QKeySequence(fields[1]));
action->setData(fields[2]);
connect(action, SIGNAL(triggered()), SLOT(onAction()));
list << action;
}
++index;
Py_DECREF(res);
}
Py_DECREF(arg);
}
return list;
}
void PythonPlugin::onAction() {
QAction* action = qobject_cast<QAction*>(sender());
if ( action != 0 ) {
QString funcName = action->data().toString();
if ( !funcName.isEmpty() ) {
PyObject* func = getFunction(funcName.toUtf8().constData());
if ( func != NULL ) {
PyObject_CallObject(func, NULL);
}
}
}
}
Juff::ActionList PythonPlugin::mainMenuActions(Juff::MenuID id) const {
Juff::ActionList list;
if ( Juff::MenuTools == id ) {
Juff::ActionList actions = getActions();
list << actions;
// list << act_;
}
return list;
}
void PythonPlugin::onDocOpened(Juff::Document* doc, Juff::PanelIndex index) {
LOGGER;
PyObject* func = getFunction("docOpened");
if ( func != NULL ) {
PyObject* args = PyTuple_New(2);
if ( !addArgument(args, 0, doc->fileName().toLocal8Bit().constData()) )
return;
if ( !addArgument(args, 1, (int)index) )
return;
PyObject_CallObject(func, args);
Py_DECREF(args);
}
}
void PythonPlugin::onDocActivated(Juff::Document* doc) {
LOGGER;
CurrentDoc = doc;
PyObject* func = getFunction("docActivated");
if ( func != NULL ) {
PyObject* args = PyTuple_New(1);
if ( !addArgument(args, 0, doc->fileName().toLocal8Bit().constData()) )
return;
PyObject_CallObject(func, args);
Py_DECREF(args);
}
}
void PythonPlugin::onDocClosed(Juff::Document* doc) {
LOGGER;
PyObject* func = getFunction("docClosed");
if ( func != NULL ) {
PyObject* args = PyTuple_New(1);
if ( !addArgument(args, 0, doc->fileName().toLocal8Bit().constData()) )
return;
PyObject_CallObject(func, args);
Py_DECREF(args);
}
}
void PythonPlugin::onDocRenamed(Juff::Document* doc, const QString& oldName) {
Q_UNUSED(doc);
Q_UNUSED(oldName);
}
void PythonPlugin::onDocModified(Juff::Document* doc) {
Q_UNUSED(doc);
}
////////////////////////////////////////////////////////////////////////////////
// Helper functions implementation
////////////////////////////////////////////////////////////////////////////////
PyObject* PythonPlugin::getFunction(const char* funcName) const {
LOGGER;
PyObject* func = PyDict_GetItemString(globalDict_, funcName);
if ( func != NULL ) {
if ( PyCallable_Check(func) ) {
return func;
}
else {
Py_DECREF(func);
}
}
return NULL;
}
bool reallyAddArgument(PyObject* args, int index, PyObject* value) {
LOGGER;
if ( value != NULL ) {
PyTuple_SetItem(args, index, value);
return true;
}
else {
Py_DECREF(args);
return false;
}
}
bool addArgument(PyObject* args, int index, const char* argument) {
LOGGER;
return reallyAddArgument(args, index, PyString_FromString(argument));
// return reallyAddArgument(args, index, Py_BuildValue("%s", argument));
}
bool addArgument(PyObject* args, int index, int argument) {
LOGGER;
return reallyAddArgument(args, index, PyInt_FromLong(argument));
// return reallyAddArgument(args, index, Py_BuildValue("%i", argument));
}
Q_EXPORT_PLUGIN2(python, PythonPlugin)
|