Subversion Repositories Scribus

Compare Revisions

Ignore whitespace Rev 5050 → Rev 5051

/branches/Version13x/Scribus/scribus/plugins/scriptplugin/scriptercore.h
24,12 → 24,17
ScripterCore(QWidget* parent);
~ScripterCore();
 
/** @brief String representation of result returned by last python console command */
QString returnString;
/** @brief String representation of line of code to be passed to the Python interactive interpreter */
QString inValue;
 
public slots:
void runScriptDialog();
void StdScript(QString filebasename);
void RecentScript(QString fn);
void slotRunScriptFile(QString fileName, bool inMainInterpreter = false);
QString slotRunScript(QString Script);
void slotRunScript(const QString Script);
void slotInteractiveScript(bool);
void slotExecute();
/*! \brief Show docstring of the script to the user.
63,8 → 68,6
//Internal members
//! \brief Reference to the "IDE" widget
PythonConsole *pcon;
int cons;
int about;
QStringList SavedRecentScripts;
QStringList RecentScripts;
MenuManager *menuMgr;
/branches/Version13x/Scribus/scribus/plugins/scriptplugin/cmdvar.h
38,13 → 38,6
/** @brief Initialize the 'scribus' Python module in the currently active interpreter */
extern "C" void initscribus(ScribusMainWindow *pl);
 
/** @brief String representation of result returned by last python console command */
extern QString RetString;
/** @brief String representation of line of code to be passed to the Python interactive interpreter */
extern QString InValue;
/** @brief Wish I knew, too... */
extern int RetVal;
 
/* Exceptions */
/*! Common scribus Exception */
extern PyObject* ScribusException;
/branches/Version13x/Scribus/scribus/plugins/scriptplugin/pconsole.cpp
144,7 → 144,7
commandEdit->clear();
// content is destroyed. This is to prevent overwritting
filename = QString::null;
outputEdit->append("\n>>> " + command);
outputEdit->append("\n>>> " + m_command);
emit runCommand();
}
 
151,15 → 151,15
void PythonConsole::parsePythonString()
{
if (commandEdit->hasSelectedText())
command = commandEdit->selectedText();
m_command = commandEdit->selectedText();
else
{
commandEdit->selectAll(true);
command = commandEdit->selectedText();
m_command = commandEdit->selectedText();
commandEdit->selectAll(false);
}
// prevent user's wrong selection
command += '\n';
m_command += '\n';
}
 
/*
/branches/Version13x/Scribus/scribus/plugins/scriptplugin/scriptercore.cpp
41,6 → 41,7
menuMgr = ScMW->scrMenuMgr;
scrScripterActions.clear();
scrRecentScriptActions.clear();
returnString = "init";
 
scrScripterActions.insert("scripterExecuteScript", new ScrAction(QObject::tr("&Execute Script..."), QKeySequence(), this, "scripterExecuteScript"));
scrScripterActions.insert("scripterShowConsole", new ScrAction(QObject::tr("Show &Console"), QKeySequence(), this, "scripterShowConsole"));
367,49 → 368,44
ScMW->ScriptRunning = false;
}
 
QString ScripterCore::slotRunScript(QString Script)
void ScripterCore::slotRunScript(const QString Script)
{
ScMW->ScriptRunning = true;
qApp->setOverrideCursor(QCursor(waitCursor), false);
InValue = Script;
QString CurDir = QDir::currentDirPath();
QString cm(
"# -*- coding: utf8 -*- \n"
// "try:\n"
);
if(PyThreadState_Get() != NULL)
inValue = Script;
QString cm;
cm = "# -*- coding: utf8 -*- \n";
if (PyThreadState_Get() != NULL)
{
initscribus(ScMW);
if (RetVal == 0)
cm += (
"scribus._bu = cStringIO.StringIO()\n"
"sys.stdout = scribus._bu\n"
"sys.stderr = scribus._bu\n"
);
/* HACK: following loop handles all input line by line.
It *should* use I.C. because of docstrings etc. I.I. cannot
handle docstrings right.
Calling all code in one command:
ia = code.InteractiveInterpreter() ia.runsource(getval())
works fine in plain Python. Not here. WTF?
*/
cm += (
works fine in plain Python. Not here. WTF? */
cm += ("import cStringIO\n"
"scribus._bu = cStringIO.StringIO()\n"
"sys.stdout = scribus._bu\n"
"sys.stderr = scribus._bu\n"
"sys.argv = ['scribus', 'ext']\n" // this is the PySys_SetArgv replacement
"for i in scribus.getval().splitlines():\n"
" scribus._ia.push(i)\n"
"scribus.retval(scribus._bu.getvalue(), 0)\n"
// "finally:\n"
"scribus.retval(scribus._bu.getvalue())\n"
"sys.stdout = sys.__stdout__\n"
"sys.stderr = sys.__stderr__\n"
);
"sys.stderr = sys.__stderr__\n");
}
QCString cmd = cm.utf8();
// Set up sys.argv
/* PV - WARNING: THIS IS EVIL! This code summons a crash - see
bug #3510. I don't know why as the Python C API is a little
bit magic for me. It looks like it replaces the cm QString or what...
"In file tools/qgarray.cpp, line 147: Out of memory"
Anyway - sys.argv is set above
char* comm[1];
comm[0] = const_cast<char*>("scribus");
// the scripter console runs everything in the main interpreter
// tell the code it's running there.
comm[1] = const_cast<char*>("ext");
PySys_SetArgv(2, comm);
PySys_SetArgv(2, comm); */
// then run the code
PyObject* m = PyImport_AddModule((char*)"__main__");
if (m == NULL)
417,7 → 413,7
else
{
PyObject* globals = PyModule_GetDict(m);
PyObject* result = PyRun_String(cmd.data(), Py_file_input, globals, globals);
PyObject* result = PyRun_String(cm.utf8().data(), Py_file_input, globals, globals);
if (result == NULL)
{
PyErr_Print();
428,8 → 424,6
}
}
ScMW->ScriptRunning = false;
qApp->restoreOverrideCursor();
return RetString;
}
 
void ScripterCore::slotInteractiveScript(bool visible)
444,7 → 438,8
 
void ScripterCore::slotExecute()
{
pcon->outputEdit->append(slotRunScript(pcon->command));
slotRunScript(pcon->command());
pcon->outputEdit->append(returnString);
pcon->commandEdit->ensureCursorVisible();
FinishScriptRun();
}
/branches/Version13x/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
89,11 → 89,6
PyObject* wrappedQApp;
ScripterCore* scripterCore;
 
// Variables used to pass data between the interpreter and
// Scribus by the console.
QString RetString;
QString InValue;
int RetVal;
 
int scriptplugin_getPluginAPIVersion()
{
166,13 → 161,9
qDebug("Failed to set default encoding to utf-8.\n");
PyErr_Clear();
}
RetVal = 0;
 
scripterCore = new ScripterCore(ScMW);
Q_CHECK_PTR(scripterCore);
// PV - FIXME: possible fix for crashing
RetString = " ";
Q_ASSERT(RetString);
initscribus(ScMW);
#ifdef HAVE_SCRIPTER2
scripter2_init();
224,24 → 215,23
static PyObject *scribus_retval(PyObject* /*self*/, PyObject* args)
{
char *Name = NULL;
int retV = 0;
if (!PyArg_ParseTuple(args, (char*)"si", &Name, &retV))
if (!PyArg_ParseTuple(args, (char*)"s", &Name))
return NULL;
// Because sysdefaultencoding is not utf-8, Python is returning utf-8 encoded
// 8-bit char* strings. Make sure Qt understands that the input is utf-8 not
// the default local encoding (usually latin-1) by using QString::fromUtf8()
RetString = QString::fromUtf8(Name);
RetVal = retV;
/*RetString = QString::fromUtf8(Name);
RetVal = retV;*/
scripterCore->returnString = QString::fromUtf8(Name);
return PyInt_FromLong(0L);
}
 
static PyObject *scribus_getval(PyObject* /*self*/)
{
return PyString_FromString(InValue.utf8().data());
return PyString_FromString(scripterCore->inValue.utf8().data());
}
 
/*!
* Translate a docstring. Small helper function for use with the
/*! \brief Translate a docstring. Small helper function for use with the
* PyMethodDef struct.
*/
char* tr(const char* docstringConstant)
448,7 → 438,7
{const_cast<char*>("getChildren"), (PyCFunction)scribus_getchildren, METH_KEYWORDS, tr(scribus_getchildren__doc__)},
{const_cast<char*>("getChild"), (PyCFunction)scribus_getchild, METH_KEYWORDS, tr(scribus_getchild__doc__)},
// Internal methods - Not for public use
{const_cast<char*>("retval"), scribus_retval, METH_VARARGS, const_cast<char*>("Scribus internal.")},
{const_cast<char*>("retval"), (PyCFunction)scribus_retval, METH_VARARGS, const_cast<char*>("Scribus internal.")},
{const_cast<char*>("getval"), (PyCFunction)scribus_getval, METH_NOARGS, const_cast<char*>("Scribus internal.")},
{NULL, (PyCFunction)(0), 0, NULL} /* sentinel */
};
/branches/Version13x/Scribus/scribus/plugins/scriptplugin/pconsole.h
42,8 → 42,9
PythonConsole( QWidget* parent = 0);
~PythonConsole();
 
//! String with the script to run (part of the all text)
QString command;
/*! \brief The command string
\retval QString with prepared Python commands */
QString command() { return m_command; };
//! File name for saving the contents
QString filename;
 
77,6 → 78,8
QGridLayout* gridLayout;
QVBoxLayout* editorsLayout;
QMenuBar* menuBar;
//! String with the script to run (part of the all text)
QString m_command;
 
protected slots:
virtual void languageChange();