Subversion Repositories Scribus

Rev

Rev 1345 | Rev 1395 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
82 Franz 1
/***************************************************************************
2
                          scriptplugin.cpp  -  description
3
                             -------------------
4
    begin                : Thu Oct 3 08:00:00 CEST 2002
5
    copyright            : (C) 2002 by Franz Schmid
6
    email                : Franz.Schmid@altmuehlnet.de
7
 ***************************************************************************/
8
 
9
/***************************************************************************
10
 *                                                                         *
11
 *   This program is free software; you can redistribute it and/or modify  *
12
 *   it under the terms of the GNU General Public License as published by  *
13
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 *   (at your option) any later version.                                   *
15
 *                                                                         *
16
 ***************************************************************************/
792 subik 17
 
18
// include cmdvar.h first, as it pulls in <Python.h>
19
#include "cmdvar.h"
20
 
103 Franz 21
#include "cmddialog.h"
22
#include "cmddoc.h"
23
#include "cmdpage.h"
24
#include "cmdobj.h"
25
#include "cmdgetprop.h"
26
#include "cmdsetprop.h"
27
#include "cmdtext.h"
28
#include "cmdmani.h"
29
#include "cmdcolor.h"
30
#include "cmdmisc.h"
82 Franz 31
#include "scriptplugin.h"
32
#include "scriptplugin.moc"
103 Franz 33
#include "cmdutil.h"
411 Franz 34
#include "objprinter.h"
418 Franz 35
#include "objpdffile.h"
203 Franz 36
#include "guiapp.h"
82 Franz 37
#include "customfdialog.h"
38
#include "helpbrowser.h"
265 Franz 39
#include "mpalette.h"
1269 cbradney 40
#include "menumanager.h"
41
#include "scraction.h"
128 Franz 42
 
1269 cbradney 43
 
661 fschmid 44
#ifdef _MSC_VER
45
 #if (_MSC_VER >= 1200)
46
  #include "win-config.h"
47
 #endif
128 Franz 48
#else
49
 #include "config.h"
50
#endif
51
 
82 Franz 52
#include <qmessagebox.h>
53
#include <qtextcodec.h>
54
#include <qdom.h>
55
#include <qtextstream.h>
56
#include <cstdlib>
57
 
853 subik 58
#include <iostream>
59
 
647 fschmid 60
// Exceptions; visible from cmdvar.h, set up in initscribus()
61
PyObject* ScribusException;
62
PyObject* NoDocOpenError;
853 subik 63
PyObject* WrongFrameTypeError;
64
PyObject* NoValidObjectError;
899 cbradney 65
PyObject* NotFoundError;
66
PyObject* NameExistsError;
647 fschmid 67
 
82 Franz 68
QString Name()
69
{
308 Franz 70
        return QObject::tr("S&cripter Manual...");
82 Franz 71
}
72
 
73
int Type()
74
{
75
        return 4;
76
}
77
 
660 fschmid 78
int ID()
79
{
80
        return 8;
81
}
82
 
1269 cbradney 83
QString actionName()
84
{
85
        return "Scripter";
86
}
87
 
88
QString actionKeySequence()
89
{
90
        return "";
91
}
92
 
93
QString actionMenu()
94
{
95
        return "Help";
96
}
97
 
98
QString actionMenuAfterName()
99
{
100
        return "Manual";
101
}
102
 
103
bool actionEnabledOnStartup()
104
{
105
        return true;
106
}
107
 
82 Franz 108
void InitPlug(QWidget *d, ScribusApp *plug)
109
{
110
        QString cm;
111
        Py_Initialize();
899 cbradney 112
        if (PyUnicode_SetDefaultEncoding("utf-8"))
113
        {
114
                qDebug("Failed to set default encoding to utf-8.\n");
115
                PyErr_Clear();
116
        }
82 Franz 117
        Carrier = plug;
118
        RetVal = 0;
119
        initscribus(Carrier);
1269 cbradney 120
 
82 Franz 121
        Tes = new MenuTest(d);
1269 cbradney 122
        Tes->menuMgr=Carrier->scrMenuMgr;
123
        Tes->scrScripterActions.clear();
124
        Tes->scrScripterActions.setAutoDelete(true);
125
        Tes->scrRecentScriptActions.clear();
126
        Tes->scrRecentScriptActions.setAutoDelete(true);
127
 
128
        Tes->scrScripterActions.insert("scripterExecuteScript", new ScrAction(QObject::tr("&Execute Script..."), QKeySequence(), Tes, "scripterExecuteScript"));
129
        Tes->scrScripterActions.insert("scripterShowConsole", new ScrAction(QObject::tr("Show &Console"), QKeySequence(), Tes, "scripterShowConsole"));
130
        Tes->scrScripterActions.insert("scripterAboutScript", new ScrAction(QObject::tr("&About Script..."), QKeySequence(), Tes, "scripterAboutScript"));
1345 subik 131
 
1269 cbradney 132
        Tes->scrScripterActions["scripterShowConsole"]->setToggleAction(true);
1345 subik 133
 
1269 cbradney 134
        QObject::connect( Tes->scrScripterActions["scripterExecuteScript"], SIGNAL(activated()) , Tes, SLOT(slotTest()) );
135
        QObject::connect( Tes->scrScripterActions["scripterShowConsole"], SIGNAL(toggled(bool)) , Tes, SLOT(slotInteractiveScript(bool)) );
136
        QObject::connect( Tes->scrScripterActions["scripterAboutScript"], SIGNAL(activated()) , Tes, SLOT(aboutScript()) );
1345 subik 137
 
1269 cbradney 138
        Tes->menuMgr->createMenu("Scripter", QObject::tr("&Script"));
139
        Tes->menuMgr->addMenuToMenuBarAfter("Scripter","Windows");
140
        Tes->menuMgr->createMenu("ScribusScripts", QObject::tr("&Scribus Scripts"), "Scripter");
141
        Tes->menuMgr->addMenuItem(Tes->scrScripterActions["scripterExecuteScript"], "Scripter");
142
        Tes->menuMgr->createMenu("RecentScripts", QObject::tr("&Recent Scripts"), "Scripter");
143
        Tes->menuMgr->addMenuSeparator("Scripter");
144
        Tes->menuMgr->addMenuItem(Tes->scrScripterActions["scripterShowConsole"], "Scripter");
145
        Tes->menuMgr->addMenuItem(Tes->scrScripterActions["scripterAboutScript"], "Scripter");
1345 subik 146
 
82 Franz 147
        Tes->SavedRecentScripts.clear();
148
        Tes->ReadPlugPrefs();
1269 cbradney 149
        Tes->buildScribusScriptsMenu();
150
        Tes->buildRecentScriptsMenu();
151
        Tes->pcon = new PConsole(d);
1345 subik 152
 
1269 cbradney 153
        QObject::connect(Tes->pcon->OutWin, SIGNAL(returnPressed()), Tes, SLOT(slotExecute()));
154
        QObject::connect(Tes->pcon, SIGNAL(paletteShown(bool)), Tes, SLOT(slotInteractiveScript(bool)));
155
}
156
 
157
void CleanUpPlug()
158
{
159
        Py_Finalize();
160
        Tes->SavePlugPrefs();
161
}
162
 
163
void Run(QWidget */*d*/, ScribusApp */*plug*/)
164
{
165
        QString pfad = DOCDIR;
166
        QString pfad2;
167
        pfad2 = QDir::convertSeparators(pfad + "en/Scripter/index.html");
168
        HelpBrowser *dia = new HelpBrowser(0, QObject::tr("Online Reference"), Carrier->getGuiLanguage(), "scripter");
169
        dia->show();
170
}
171
 
172
void MenuTest::buildScribusScriptsMenu()
173
{
872 cbradney 174
        QString pfad = SCRIPTSDIR;
282 Franz 175
        QString pfad2;
872 cbradney 176
        pfad2 = QDir::convertSeparators(pfad);
222 Franz 177
        QDir ds(pfad2, "*.py", QDir::Name, QDir::Files | QDir::NoSymLinks);
178
        if ((ds.exists()) && (ds.count() != 0))
179
        {
180
                for (uint dc = 0; dc < ds.count(); ++dc)
181
                {
182
                        QFileInfo fs(ds[dc]);
1269 cbradney 183
                        QString strippedName=fs.baseName(false);
1288 cbradney 184
                        scrScripterActions.insert(strippedName, new ScrAction( ScrAction::RecentScript, QIconSet(), strippedName, QKeySequence(), this, strippedName));
185
                        connect( scrScripterActions[strippedName], SIGNAL(activatedData(QString)), this, SLOT(StdScript(QString)) );
1269 cbradney 186
                        menuMgr->addMenuItem(scrScripterActions[strippedName], "ScribusScripts");
222 Franz 187
                }
188
        }
1269 cbradney 189
 
190
 
191
}
192
 
193
void MenuTest::rebuildRecentScriptsMenu()
194
{
195
        scrRecentScriptActions.clear();
196
        uint max = QMIN(Carrier->Prefs.RecentDCount, RecentScripts.count());
197
        for (uint m = 0; m < max; ++m)
282 Franz 198
        {
1269 cbradney 199
                QString strippedName=RecentScripts[m];
200
                strippedName.remove(QDir::separator());
1288 cbradney 201
                scrRecentScriptActions.insert(strippedName, new ScrAction( ScrAction::RecentScript, QIconSet(), RecentScripts[m], QKeySequence(), this, strippedName));
202
                connect( scrRecentScriptActions[strippedName], SIGNAL(activatedData(QString)), this, SLOT(RecentScript(QString)) );
1269 cbradney 203
                menuMgr->addMenuItem(scrRecentScriptActions[strippedName], "RecentScripts");
204
        }
205
}
206
 
207
void MenuTest::buildRecentScriptsMenu()
208
{
209
        RecentScripts.clear();
210
        scrRecentScriptActions.clear();
211
        if (SavedRecentScripts.count() != 0)
212
        {
213
                uint max = QMIN(Carrier->Prefs.RecentDCount, SavedRecentScripts.count());
87 Franz 214
                for (uint m = 0; m < max; ++m)
282 Franz 215
                {
1269 cbradney 216
                        QFileInfo fd(SavedRecentScripts[m]);
82 Franz 217
                        if (fd.exists())
282 Franz 218
                        {
1269 cbradney 219
                                QString strippedName=SavedRecentScripts[m];
220
                                strippedName.remove(QDir::separator());
1288 cbradney 221
                                scrRecentScriptActions.insert(strippedName, new ScrAction( ScrAction::RecentScript, QIconSet(), SavedRecentScripts[m], QKeySequence(), this, strippedName));
222
                                connect( scrRecentScriptActions[strippedName], SIGNAL(activatedData(QString)), this, SLOT(RecentScript(QString)) );
1269 cbradney 223
                                menuMgr->addMenuItem(scrRecentScriptActions[strippedName], "RecentScripts");
82 Franz 224
                        }
225
                }
282 Franz 226
        }
82 Franz 227
}
228
 
222 Franz 229
void MenuTest::FinishScriptRun()
230
{
231
        if (Carrier->HaveDoc)
282 Franz 232
        {
222 Franz 233
                Carrier->Mpal->SetDoc(Carrier->doc);
234
                Carrier->Mpal->updateCList();
716 cbradney 235
                Carrier->Mpal->Spal->setFormats(Carrier->doc);
222 Franz 236
                Carrier->Mpal->SetLineFormats(Carrier->doc);
237
                Carrier->Mpal->Cpal->SetColors(Carrier->doc->PageColors);
238
                Carrier->Lpal->setLayers(&Carrier->doc->Layers, &Carrier->doc->ActiveLayer);
239
                Carrier->Tpal->BuildTree(Carrier->view);
240
                Carrier->Sepal->SetView(Carrier->view);
241
                Carrier->Sepal->Rebuild();
662 fschmid 242
                if (Carrier->view->SelItem.count() != 0)
243
                        Carrier->HaveNewSel(Carrier->view->SelItem.at(0)->PType);
222 Franz 244
                else
245
                        Carrier->HaveNewSel(-1);
246
                Carrier->view->DrawNew();
282 Franz 247
        }
222 Franz 248
}
249
 
82 Franz 250
void MenuTest::slotTest()
251
{
252
        QString fileName;
91 Franz 253
        QString CurDirP = QDir::currentDirPath();
415 Franz 254
        QString scriptDir = Carrier->Prefs.ScriptDir;
255
        if (scriptDir == "")
256
                scriptDir = CurDirP;
257
        CustomFDialog diaf((QWidget*)parent(), scriptDir, QObject::tr("Open"), QObject::tr("Python Scripts (*.py);; All Files (*)"));
82 Franz 258
        if (diaf.exec())
282 Franz 259
        {
82 Franz 260
                fileName = diaf.selectedFile();
261
                slotRunScriptFile(fileName);
1269 cbradney 262
 
82 Franz 263
                if (RecentScripts.findIndex(fileName) == -1)
264
                        RecentScripts.prepend(fileName);
265
                else
282 Franz 266
                {
82 Franz 267
                        RecentScripts.remove(fileName);
268
                        RecentScripts.prepend(fileName);
282 Franz 269
                }
1269 cbradney 270
                rebuildRecentScriptsMenu();
282 Franz 271
        }
91 Franz 272
        QDir::setCurrent(CurDirP);
222 Franz 273
        FinishScriptRun();
82 Franz 274
}
275
 
1269 cbradney 276
void MenuTest::StdScript(QString basefilename)
222 Franz 277
{
872 cbradney 278
        QString pfad = SCRIPTSDIR;
282 Franz 279
        QString pfad2;
872 cbradney 280
        pfad2 = QDir::convertSeparators(pfad);
1269 cbradney 281
        QString fn = pfad2+basefilename+".py";
282
        //QString fn = pfad2+smen->text(id)+".py";
222 Franz 283
        QFileInfo fd(fn);
284
        if (!fd.exists())
285
                return;
286
        slotRunScriptFile(fn);
287
        FinishScriptRun();
288
}
289
 
1269 cbradney 290
void MenuTest::RecentScript(QString fn)
82 Franz 291
{
1269 cbradney 292
        //QString fn = rmen->text(id);
82 Franz 293
        QFileInfo fd(fn);
294
        if (!fd.exists())
282 Franz 295
        {
82 Franz 296
                RecentScripts.remove(fn);
1269 cbradney 297
                rebuildRecentScriptsMenu();
82 Franz 298
                return;
282 Franz 299
        }
82 Franz 300
        slotRunScriptFile(fn);
222 Franz 301
        FinishScriptRun();
82 Franz 302
}
303
 
304
void MenuTest::slotRunScriptFile(QString fileName)
305
{
173 Franz 306
        Carrier->ScriptRunning = true;
1060 subik 307
        qApp->setOverrideCursor(QCursor(waitCursor), false);
82 Franz 308
        char* comm[1];
309
        QFileInfo fi(fileName);
310
        QCString na = fi.fileName().latin1();
311
        QDir::setCurrent(fi.dirPath(true));
312
        PyThreadState *stateo = PyEval_SaveThread();
313
        PyThreadState *state = Py_NewInterpreter();
314
        initscribus(Carrier);
315
        comm[0] = na.data();
221 Franz 316
        // call python script
82 Franz 317
        PySys_SetArgv(1, comm);
934 subik 318
        PyObject* m = PyImport_AddModule((char*)"__main__");
655 fschmid 319
        if (m == NULL)
320
                qDebug("Failed to get __main__ - aborting script");
321
        else
332 Franz 322
        {
899 cbradney 323
                // FIXME: If filename contains chars outside 7bit ascii, might be problems
655 fschmid 324
                PyObject* globals = PyModule_GetDict(m);
325
                // Build the Python code to run the script
326
                QString cm = QString("import sys,StringIO,traceback\n");
327
                cm        += QString("sys.path[0] = \"%1\"\n").arg(fi.dirPath(true));
328
                cm        += QString("try:\n");
329
                cm        += QString("    execfile(\"%1\")\n").arg(fileName);
330
                cm        += QString("except SystemExit:\n");
331
                cm        += QString("    pass\n");
332
                // Capture the text of any other exception that's raised by the interpreter
333
                // into a StringIO buffer for later extraction.
334
                cm        += QString("except Exception, err:\n");
335
                cm        += QString("    f=StringIO.StringIO()\n");
336
                cm        += QString("    traceback.print_exc(file=f)\n");
337
                cm        += QString("    errorMsg = f.getvalue()\n");
338
                cm        += QString("    del(f)\n");
339
                // We re-raise the exception so the return value of PyRun_String reflects
340
                // the fact that an exception has ocurred.
341
                cm        += QString("    raise\n");
899 cbradney 342
                // FIXME: if cmd contains chars outside 7bit ascii, might be problems
655 fschmid 343
                QCString cmd = cm.latin1();
344
                // Now run the script in the interpreter's global scope
345
                PyObject* result = PyRun_String(cmd.data(), Py_file_input, globals, globals);
346
                // NULL is returned if an exception is set. We don't care about any
347
                // other return value (most likely None anyway) and can ignore it.
348
                if (result == NULL)
349
                {
934 subik 350
                        PyObject* errorMsgPyStr = PyMapping_GetItemString(globals, (char*)"errorMsg");
655 fschmid 351
                        if (errorMsgPyStr == NULL)
352
                        {
353
                                // It's rather unlikely that this will ever be reached - to get here
354
                                // we'd have to fail to retrive the string we just created.
355
                                qDebug("Error retrieving error message content after script exception!");
356
                                qDebug("Exception was:");
357
                                PyErr_Print();
358
                        }
359
                        else
360
                        {
361
                                QString errorMsg = PyString_AsString(errorMsgPyStr);
362
                                // Display a dialog to the user with the exception
363
                                QClipboard *cp = QApplication::clipboard();
364
                                cp->setText(errorMsg);
365
                                QMessageBox::warning(Carrier,
366
                                                                        tr("Script error"),
367
                                                                        tr("If you are running an official script report it at <a href=\"http://bugs.scribus.net\">bugs.scribus.net</a> please.")
368
                                                                        + "<pre>" +errorMsg + "</pre>"
369
                                                                        + tr("This message is in your clipboard too. Use Ctrl+V to paste it into bug tracker."));
370
                        }
371
                } // end if result == NULL
372
                // Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF not Py_DECREF
373
                Py_XDECREF(result);
374
        } // end if m == NULL
82 Franz 375
        Py_EndInterpreter(state);
376
        PyEval_RestoreThread(stateo);
173 Franz 377
        Carrier->ScriptRunning = false;
1060 subik 378
        qApp->restoreOverrideCursor();
82 Franz 379
}
380
 
381
QString MenuTest::slotRunScript(QString Script)
382
{
173 Franz 383
        Carrier->ScriptRunning = true;
1060 subik 384
        qApp->setOverrideCursor(QCursor(waitCursor), false);
82 Franz 385
        char* comm[1];
386
        QString cm;
387
        InValue = Script;
388
        QString CurDir = QDir::currentDirPath();
389
        if(PyThreadState_Get() != NULL)
282 Franz 390
        {
82 Franz 391
                initscribus(Carrier);
392
                if (RetVal == 0)
282 Franz 393
                {
899 cbradney 394
                        // FIXME: if CurDir contains chars outside 7bit ascii, might be problems
82 Franz 395
                        cm = "import sys\nsys.path[0] = \""+CurDir+"\"\n";
396
                        cm += "import cStringIO\n";
397
                        cm += "from scribus import *\n";
398
                        cm += "bu = cStringIO.StringIO()\n";
399
                        cm += "sys.stdout = bu\n";
400
                        cm += "sys.stderr = bu\n";
401
                        cm += "import code\n";
402
                        cm += "ia = code.InteractiveConsole(globals())\n";
282 Franz 403
                }
82 Franz 404
                cm += "sc = getval()\n";
405
                cm += "rv = ia.push(sc)\n";
406
                cm += "if rv == 1:\n";
407
                cm += "\tre = \"...\"\n";
408
                cm += "else:\n";
409
                cm += "\tre = bu.getvalue()\n";
410
                cm += "retval(re, rv)\n";
282 Franz 411
        }
899 cbradney 412
        // FIXME: if cmd contains chars outside 7bit ascii, might be problems
82 Franz 413
        QCString cmd = cm.latin1();
934 subik 414
        comm[0] = (char*)"scribus";
82 Franz 415
        PySys_SetArgv(1, comm);
416
        PyRun_SimpleString(cmd.data());
417
        if (RetVal == 0)
282 Franz 418
        {
82 Franz 419
                RetString += ">>>";
420
                pcon->OutWin->Prompt = ">>>";
282 Franz 421
        }
82 Franz 422
        else
423
                pcon->OutWin->Prompt = "...";
173 Franz 424
        Carrier->ScriptRunning = false;
1060 subik 425
        qApp->restoreOverrideCursor();
82 Franz 426
        return RetString;
427
}
428
 
1269 cbradney 429
void MenuTest::slotInteractiveScript(bool visible)
82 Franz 430
{
1269 cbradney 431
        scrScripterActions["scripterShowConsole"]->setOn(visible);
432
        pcon->setShown(visible);
82 Franz 433
}
434
 
435
void MenuTest::slotExecute()
436
{
437
        pcon->OutWin->append(slotRunScript(pcon->OutWin->LastComm));
438
        pcon->OutWin->moveCursor(QTextEdit::MoveEnd, false);
439
        pcon->OutWin->scrollToBottom();
440
        pcon->OutWin->ensureCursorVisible();
222 Franz 441
        FinishScriptRun();
82 Franz 442
}
443
 
444
void MenuTest::ReadPlugPrefs()
445
{
446
        QDomDocument docu("scriptrc");
282 Franz 447
        QString ho = QDir::homeDirPath();
1269 cbradney 448
        QFile f(QDir::convertSeparators(ho+"/.scribus/scripter13.rc"));
82 Franz 449
        if(!f.open(IO_ReadOnly))
450
                return;
451
        if(!docu.setContent(&f))
282 Franz 452
        {
82 Franz 453
                f.close();
454
                return;
282 Franz 455
        }
82 Franz 456
        f.close();
457
        QDomElement elem=docu.documentElement();
458
        if (elem.tagName() != "SCRIPTRC")
459
                return;
460
        QDomNode DOC=elem.firstChild();
461
        while(!DOC.isNull())
282 Franz 462
        {
82 Franz 463
                QDomElement dc=DOC.toElement();
464
                if (dc.tagName()=="RECENT")
465
                        SavedRecentScripts.append(dc.attribute("NAME"));
466
                DOC=DOC.nextSibling();
282 Franz 467
        }
82 Franz 468
}
469
 
470
void MenuTest::SavePlugPrefs()
471
{
472
        QDomDocument docu("scriptrc");
473
        QString st="<SCRIPTRC></SCRIPTRC>";
474
        docu.setContent(st);
475
        QDomElement elem=docu.documentElement();
1269 cbradney 476
        for (uint rd=0; rd < RecentScripts.count(); ++rd)
282 Franz 477
        {
82 Franz 478
                QDomElement rde=docu.createElement("RECENT");
1269 cbradney 479
                rde.setAttribute("NAME",RecentScripts[rd]);
82 Franz 480
                elem.appendChild(rde);
282 Franz 481
        }
482
        QString ho = QDir::homeDirPath();
1269 cbradney 483
        QFile f(QDir::convertSeparators(ho+"/.scribus/scripter13.rc"));
82 Franz 484
        if(!f.open(IO_WriteOnly))
485
                return;
486
        QTextStream s(&f);
487
        s<<docu.toCString();
488
        f.close();
489
}
490
 
651 fschmid 491
/* 11/1/2004 pv - Show docstring of the script to the user.
492
 * I don't know how to get docstring via e.g. pydoc because of
493
 * it needs to run script => error cannot find scribus module
494
 */
650 fschmid 495
void MenuTest::aboutScript()
496
{
497
        QString fname = Carrier->CFileDialog(".", "about", "Scripts (*.py)", "", 0, 0, 0, 0);
498
        QFileInfo fi = QFileInfo(fname);
499
        QString html = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/aboutScript.html");
500
        QFile input(fname);
501
        if(!input.open(IO_ReadOnly))
502
                return;
503
        QFile output(html);
504
        if(!output.open(IO_WriteOnly))
505
                return;
506
        QTextStream intputstream(&input);
507
        QTextStream outputstream(&output);
651 fschmid 508
        QString content = intputstream.read();
509
        QString docstring = content.section("\"\"\"", 1, 1);
510
        if (docstring != "")
511
        {
512
                outputstream << "<h1>Documentation for: " << fi.fileName() << "</h1><p>";
513
                outputstream << docstring.replace("\n\n", "<p>");
514
        }
515
        else
516
        {
517
                outputstream << "<pre>" << endl;
518
                outputstream << "<p><b>Script "<< fi.fileName() << " doesn't contain any docstring!</b></p>" << content;
519
                outputstream << "</pre>" << endl;
520
        }
650 fschmid 521
        output.close();
522
        input.close();
805 subik 523
        HelpBrowser *dia = new HelpBrowser(0, QObject::tr("About Script") + " " + fi.fileName(), "en", "", html);
650 fschmid 524
        dia->show();
525
}
526
 
655 fschmid 527
// This function builds a Python wrapper function called newName around the
528
// python function called oldName. The wrapper function prints a warning, then
529
// calls oldName with all passed arguments and returns the result from oldName.
530
// The wrapper is stored in the module dictionary passed, so it appears in the
531
// `scribus' module and will be imported by 'from scribus import'. A docstring
532
// is provided to direct the user to the correct function.
533
// By default the warning gets output only on the first use of the function in a given
534
// interpreter instance, but user scripts can change this.
535
void deprecatedFunctionAlias(PyObject* scribusdict, char* oldName, char* newName)
536
{
537
        // Build the Python code to create the wrapper function
538
        QString wrapperFunc = "";
539
        wrapperFunc += QString("def %1(*args, **kwargs):\n").arg(newName);
540
        wrapperFunc += QString("    \"\"\"Deprecated alias for function %1 - see help(%2).\"\"\"\n").arg(oldName).arg(oldName);
541
        wrapperFunc += QString("    warnings.warn(\"Warning, script function %1 is deprecated, use %2 instead.\\n\",exceptions.DeprecationWarning)\n").arg(newName).arg(oldName);
542
        wrapperFunc += QString("    return %1(*args,**kwargs)\n").arg(oldName);
899 cbradney 543
        QCString wsData = wrapperFunc.latin1(); //this should probably be utf8 now
655 fschmid 544
        // And run it in the namespace of the scribus module
545
        PyObject* result = PyRun_String(wsData, Py_file_input, scribusdict, scribusdict);
546
        // NULL is returned if an exception is set. We don't care about any other return value and
547
        // can ignore it.
548
        if (result == NULL)
549
        {
550
                qDebug("Failed to alias %s to %s in Python scripter - exception raised!", oldName, newName);
551
                PyErr_Print();
552
        }
553
        // Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF not Py_DECREF
554
        Py_XDECREF(result);
555
}
556
 
737 subik 557
// Copy constants by coping the reference to them. Equivalent to
558
// scribus.__dict__[newName] = scribus.__dict__[oldName].
559
void constantAlias(PyObject* scribusdict, const char* oldName, const char* newName)
733 subik 560
{
737 subik 561
        /* Work around the braindead Python/C API not using 'const' for
562
         * strings it won't change. */
563
        char* newNameTemp = strndup(newName, strlen(newName));
564
        char* oldNameTemp = strndup(oldName, strlen(oldName));
565
        assert(newNameTemp != NULL);
566
        assert(oldNameTemp != NULL);
567
        /* We use PyMapping_GetItemString because unlike PyDict_GetItemString it
568
         * returns a new (not borrowed) reference, while PyDict_SetItemString
569
         * consumes the reference. That means we don't have to Py_INCREF anything.
570
         */
571
        PyObject* item = PyMapping_GetItemString(scribusdict, oldNameTemp);
572
        assert(item != NULL);
573
        PyDict_SetItemString(scribusdict, newNameTemp, item);
574
        free(newNameTemp);
575
        free(oldNameTemp);
733 subik 576
}
577
 
82 Franz 578
/****************************************************************************************/
579
/*                                                                                      */
650 fschmid 580
/*   Definitions of the Python commands                                                 */
82 Franz 581
/*                                                                                      */
582
/****************************************************************************************/
583
 
934 subik 584
static PyObject *scribus_retval(PyObject */*self*/, PyObject* args)
82 Franz 585
{
899 cbradney 586
        char *Name = NULL;
587
        int retV = 0;
934 subik 588
        if (!PyArg_ParseTuple(args, (char*)"si", &Name, &retV))
82 Franz 589
                return NULL;
899 cbradney 590
        // Because sysdefaultencoding is not utf-8, Python is returning utf-8 encoded
591
        // 8-bit char* strings. Make sure Qt understands that the input is utf-8 not
592
        // the default local encoding (usually latin-1) by using QString::fromUtf8()
593
        RetString = QString::fromUtf8(Name);
82 Franz 594
        RetVal = retV;
595
        return PyInt_FromLong(0L);
596
}
597
 
934 subik 598
static PyObject *scribus_getval(PyObject */*self*/)
82 Franz 599
{
899 cbradney 600
        return PyString_FromString(InValue.utf8().data());
82 Franz 601
}
602
 
853 subik 603
/*!
604
 * Translate a docstring. Small helper function for use with the
605
 * PyMethodDef struct.
606
 */
607
char* tr(const char* docstringConstant)
608
{
899 cbradney 609
        // Alas, there's a lot of wasteful string copying going on
610
        // here.
611
        QString translated = QObject::tr(docstringConstant, "scripter docstring");
612
        /*
613
         * Python doesn't support 'unicode' object docstrings in the PyMethodDef,
614
         * and has no way to specify what encoding docstrings are in. The passed C
615
         * strings passed are made into 'str' objects as-is. These are interpreted
616
         * as being in the Python sysdefaultencoding, usually 'ascii', when used.
617
         * We now set systemdefaultencoding to 'utf-8' ...  so we're going to pass
618
         * Python an 8-bit utf-8 encoded string in a char* .  With
619
         * sysdefaultencoding set correctly, Python will interpret it correctly and
620
         * we'll have our unicode docstrings. It's not as ugly a hack as it sounds,
621
         * you just have to remember that C and Python strings can both be
622
         * considered 8-bit strings of binary data that can be later interpreted as
623
         * a text string in a particular text encoding.
624
         */
625
        //QCString utfTranslated = translated.utf8();
626
        const char* trch = translated.utf8().data();
627
        return strndup(trch, strlen(trch));
853 subik 628
}
629
 
899 cbradney 630
/* Now we're using the more pythonic convention for names:
631
 * class - ClassName
632
 * procedure/function/method - procedureName
633
 * etc. */
853 subik 634
PyMethodDef scribus_methods[] = {
421 Franz 635
        // 2004/10/03 pv - aliases with common Python syntax - ClassName methodName
655 fschmid 636
        // 2004-11-06 cr - move aliasing to dynamically generated wrapper functions, sort methoddef
934 subik 637
        {const_cast<char*>("changeColor"), scribus_setcolor, METH_VARARGS, tr(scribus_setcolor__doc__)},
638
        {const_cast<char*>("closeDoc"), (PyCFunction)scribus_closedoc, METH_NOARGS, tr(scribus_closedoc__doc__)},
639
        {const_cast<char*>("createBezierLine"), scribus_bezierline, METH_VARARGS, tr(scribus_bezierline__doc__)},
640
        {const_cast<char*>("createEllipse"), scribus_newellipse, METH_VARARGS, tr(scribus_newellipse__doc__)},
641
        {const_cast<char*>("createImage"), scribus_newimage, METH_VARARGS, tr(scribus_newimage__doc__)},
642
        {const_cast<char*>("createLayer"), scribus_createlayer, METH_VARARGS, tr(scribus_createlayer__doc__)},
643
        {const_cast<char*>("createLine"), scribus_newline, METH_VARARGS, tr(scribus_newline__doc__)},
644
        {const_cast<char*>("createPathText"), scribus_pathtext, METH_VARARGS, tr(scribus_pathtext__doc__)},
645
        {const_cast<char*>("createPolygon"), scribus_polygon, METH_VARARGS, tr(scribus_polygon__doc__)},
646
        {const_cast<char*>("createPolyLine"), scribus_polyline, METH_VARARGS, tr(scribus_polyline__doc__)},
647
        {const_cast<char*>("createRect"), scribus_newrect, METH_VARARGS, tr(scribus_newrect__doc__)},
648
        {const_cast<char*>("createText"), scribus_newtext, METH_VARARGS, tr(scribus_newtext__doc__)},
649
        {const_cast<char*>("currentPage"), (PyCFunction)scribus_actualpage, METH_NOARGS, tr(scribus_actualpage__doc__)},
650
        {const_cast<char*>("defineColor"), scribus_newcolor, METH_VARARGS, tr(scribus_newcolor__doc__)},
651
        {const_cast<char*>("deleteColor"), scribus_delcolor, METH_VARARGS, tr(scribus_delcolor__doc__)},
652
        {const_cast<char*>("deleteLayer"), scribus_removelayer, METH_VARARGS, tr(scribus_removelayer__doc__)},
653
        {const_cast<char*>("deleteObject"), scribus_deleteobj, METH_VARARGS, tr(scribus_deleteobj__doc__)},
654
        {const_cast<char*>("deletePage"), scribus_deletepage, METH_VARARGS, tr(scribus_deletepage__doc__)},
655
        {const_cast<char*>("deleteText"), scribus_deletetext, METH_VARARGS, tr(scribus_deletetext__doc__)},
656
        {const_cast<char*>("deselectAll"), (PyCFunction)scribus_deselect, METH_NOARGS, tr(scribus_deselect__doc__)},
657
        {const_cast<char*>("docChanged"), scribus_docchanged, METH_VARARGS, tr(scribus_docchanged__doc__)},
658
        {const_cast<char*>("fileDialog"), (PyCFunction)scribus_filedia, METH_VARARGS|METH_KEYWORDS, tr(scribus_filedia__doc__)},
659
        {const_cast<char*>("getActiveLayer"), (PyCFunction)scribus_getactlayer, METH_NOARGS, tr(scribus_getactlayer__doc__)},
660
        {const_cast<char*>("getAllObjects"), scribus_getallobj, METH_VARARGS, tr(scribus_getallobj__doc__)},
661
        {const_cast<char*>("getAllStyles"), (PyCFunction)scribus_getstylenames, METH_NOARGS, tr(scribus_getstylenames__doc__)},
662
        {const_cast<char*>("getAllText"), scribus_gettext, METH_VARARGS, tr(scribus_gettext__doc__)},
663
        {const_cast<char*>("getColorNames"), (PyCFunction)scribus_colornames, METH_NOARGS, tr(scribus_colornames__doc__)},
664
        {const_cast<char*>("getColor"), scribus_getcolor, METH_VARARGS, tr(scribus_getcolor__doc__)},
665
        {const_cast<char*>("getColumnGap"), scribus_getcolumngap, METH_VARARGS, tr(scribus_getcolumngap__doc__)},
666
        {const_cast<char*>("getColumns"), scribus_getcolumns, METH_VARARGS, tr(scribus_getcolumns__doc__)},
667
        {const_cast<char*>("getCornerRadius"), scribus_getcornerrad, METH_VARARGS, tr(scribus_getcornerrad__doc__)},
668
        {const_cast<char*>("getFillColor"), scribus_getfillcolor, METH_VARARGS, tr(scribus_getfillcolor__doc__)},
669
        {const_cast<char*>("getFillShade"), scribus_getfillshade, METH_VARARGS, tr(scribus_getfillshade__doc__)},
670
        {const_cast<char*>("getFontNames"), (PyCFunction)scribus_fontnames, METH_NOARGS, tr(scribus_fontnames__doc__)},
671
        {const_cast<char*>("getFont"), scribus_getfont, METH_VARARGS, tr(scribus_getfont__doc__)},
672
        {const_cast<char*>("getFontSize"), scribus_getfontsize, METH_VARARGS, tr(scribus_getfontsize__doc__)},
673
        {const_cast<char*>("getGuiLanguage"), (PyCFunction)scribus_getlanguage, METH_NOARGS, tr(scribus_getlanguage__doc__)},
674
        {const_cast<char*>("getHGuides"), (PyCFunction)scribus_getHguides, METH_NOARGS, tr(scribus_getHguides__doc__)},
675
        {const_cast<char*>("getImageFile"), scribus_getimgname, METH_VARARGS, tr(scribus_getimgname__doc__)},
676
        {const_cast<char*>("getImageScale"), scribus_getimgscale, METH_VARARGS, tr(scribus_getimgscale__doc__)},
677
        {const_cast<char*>("getLayers"), (PyCFunction)scribus_getlayers, METH_NOARGS, tr(scribus_getlayers__doc__)},
678
        {const_cast<char*>("getLineCap"), scribus_getlineend, METH_VARARGS, tr(scribus_getlineend__doc__)},
679
        {const_cast<char*>("getLineColor"), scribus_getlinecolor, METH_VARARGS, tr(scribus_getlinecolor__doc__)},
680
        {const_cast<char*>("getLineJoin"), scribus_getlinejoin, METH_VARARGS, tr(scribus_getlinejoin__doc__)},
681
        {const_cast<char*>("getLineShade"), scribus_getlineshade, METH_VARARGS, tr(scribus_getlineshade__doc__)},
682
        {const_cast<char*>("getLineSpacing"), scribus_getlinespace, METH_VARARGS, tr(scribus_getlinespace__doc__)},
683
        {const_cast<char*>("getLineStyle"), scribus_getlinestyle, METH_VARARGS, tr(scribus_getlinestyle__doc__)},
684
        {const_cast<char*>("getLineWidth"), scribus_getlinewidth, METH_VARARGS, tr(scribus_getlinewidth__doc__)},
685
        {const_cast<char*>("getPageItems"), (PyCFunction)scribus_getpageitems, METH_NOARGS, tr(scribus_getpageitems__doc__)},
686
        {const_cast<char*>("getPageMargins"), (PyCFunction)scribus_getpagemargins, METH_NOARGS, tr(scribus_getpagemargins__doc__)},
687
        {const_cast<char*>("getPageSize"), (PyCFunction)scribus_pagedimension, METH_NOARGS, tr(scribus_pagedimension__doc__)}, // just an alias to PageDimension()
688
        {const_cast<char*>("getPosition"), scribus_getposi, METH_VARARGS, tr(scribus_getposi__doc__)},
689
        {const_cast<char*>("getRotation"), scribus_getrotation, METH_VARARGS, tr(scribus_getrotation__doc__)},
690
        {const_cast<char*>("getSelectedObject"), scribus_getselobjnam, METH_VARARGS, tr(scribus_getselobjnam__doc__)},
691
        {const_cast<char*>("getSize"), scribus_getsize, METH_VARARGS, tr(scribus_getsize__doc__)},
692
        {const_cast<char*>("getTextColor"), scribus_getlinecolor, METH_VARARGS, tr(scribus_getlinecolor__doc__)},
693
        {const_cast<char*>("getTextLength"), scribus_gettextsize, METH_VARARGS, tr(scribus_gettextsize__doc__)},
694
        {const_cast<char*>("getText"), scribus_getframetext, METH_VARARGS, tr(scribus_getframetext__doc__)},
695
        {const_cast<char*>("getTextShade"), scribus_getlineshade, METH_VARARGS, tr(scribus_getlineshade__doc__)},
696
        {const_cast<char*>("getUnit"), (PyCFunction)scribus_getunit, METH_NOARGS, tr(scribus_getunit__doc__)},
697
        {const_cast<char*>("getVGuides"), (PyCFunction)scribus_getVguides, METH_NOARGS, tr(scribus_getVguides__doc__)},
698
        {const_cast<char*>("getXFontNames"), (PyCFunction)scribus_xfontnames, METH_NOARGS, tr(scribus_xfontnames__doc__)},
699
        {const_cast<char*>("gotoPage"), scribus_gotopage, METH_VARARGS, tr(scribus_gotopage__doc__)},
700
        {const_cast<char*>("groupObjects"), scribus_groupobj, METH_VARARGS, tr(scribus_groupobj__doc__)},
701
        {const_cast<char*>("haveDoc"), (PyCFunction)scribus_havedoc, METH_NOARGS, tr(scribus_havedoc__doc__)},
702
        {const_cast<char*>("insertText"), scribus_inserttext, METH_VARARGS, tr(scribus_inserttext__doc__)},
703
        {const_cast<char*>("isLayerPrintable"), scribus_glayerprint, METH_VARARGS, tr(scribus_glayerprint__doc__)},
704
        {const_cast<char*>("isLayerVisible"), scribus_glayervisib, METH_VARARGS, tr(scribus_glayervisib__doc__)},
705
        {const_cast<char*>("isLocked"), scribus_islocked, METH_VARARGS, tr(scribus_islocked__doc__)},
706
        {const_cast<char*>("linkTextFrames"), scribus_linktextframes, METH_VARARGS, tr(scribus_linktextframes__doc__)},
707
        {const_cast<char*>("loadImage"), scribus_loadimage, METH_VARARGS, tr(scribus_loadimage__doc__)},
708
        {const_cast<char*>("loadStylesFromFile"), scribus_loadstylesfromfile, METH_VARARGS, tr(scribus_loadstylesfromfile__doc__)},
709
        {const_cast<char*>("lockObject"), scribus_lockobject, METH_VARARGS, tr(scribus_lockobject__doc__)},
710
        {const_cast<char*>("messagebarText"), scribus_messagebartext, METH_VARARGS, tr(scribus_messagebartext__doc__)},
711
        {const_cast<char*>("messageBox"), (PyCFunction)scribus_messdia, METH_VARARGS|METH_KEYWORDS, tr(scribus_messdia__doc__)},
712
        {const_cast<char*>("moveObjectAbs"), scribus_moveobjabs, METH_VARARGS, tr(scribus_moveobjabs__doc__)},
713
        {const_cast<char*>("moveObject"), scribus_moveobjrel, METH_VARARGS, tr(scribus_moveobjrel__doc__)},
714
        {const_cast<char*>("newDocDialog"), (PyCFunction)scribus_newdocdia, METH_NOARGS, tr(scribus_newdocdia__doc__)},
715
        {const_cast<char*>("newDoc"), scribus_newdoc, METH_VARARGS, tr(scribus_newdoc__doc__)},
716
        {const_cast<char*>("newPage"), scribus_newpage, METH_VARARGS, tr(scribus_newpage__doc__)},
717
        {const_cast<char*>("objectExists"),scribus_objectexists, METH_VARARGS, tr(scribus_objectexists__doc__)},
718
        {const_cast<char*>("openDoc"), scribus_opendoc, METH_VARARGS, tr(scribus_opendoc__doc__)},
719
        {const_cast<char*>("pageCount"), (PyCFunction)scribus_pagecount, METH_NOARGS, tr(scribus_pagecount__doc__)},
720
        {const_cast<char*>("pageDimension"), (PyCFunction)scribus_pagedimension, METH_NOARGS, tr(scribus_pagedimension__doc__)},
721
        {const_cast<char*>("progressReset"), (PyCFunction)scribus_progressreset, METH_NOARGS, tr(scribus_progressreset__doc__)},
722
        {const_cast<char*>("progressSet"), scribus_progresssetprogress, METH_VARARGS, tr(scribus_progresssetprogress__doc__)},
723
        {const_cast<char*>("progressTotal"), scribus_progresssettotalsteps, METH_VARARGS, tr(scribus_progresssettotalsteps__doc__)},
724
        {const_cast<char*>("redrawAll"), (PyCFunction)scribus_redraw, METH_NOARGS, tr(scribus_redraw__doc__)},
725
        {const_cast<char*>("renderFont"), scribus_renderfont, METH_VARARGS, tr(scribus_renderfont__doc__)},
726
        {const_cast<char*>("replaceColor"), scribus_replcolor, METH_VARARGS, tr(scribus_replcolor__doc__)},
727
        {const_cast<char*>("rotateObjectAbs"), scribus_rotobjabs, METH_VARARGS, tr(scribus_rotobjabs__doc__)},
728
        {const_cast<char*>("rotateObject"), scribus_rotobjrel, METH_VARARGS, tr(scribus_rotobjrel__doc__)},
729
        {const_cast<char*>("saveDocAs"), scribus_savedocas, METH_VARARGS, tr(scribus_savedocas__doc__)},
730
        {const_cast<char*>("saveDoc"), (PyCFunction)scribus_savedoc, METH_NOARGS, tr(scribus_savedoc__doc__)},
731
        {const_cast<char*>("savePageAsEPS"), scribus_savepageeps, METH_VARARGS, tr(scribus_savepageeps__doc__)},
732
        {const_cast<char*>("scaleGroup"), scribus_scalegroup, METH_VARARGS, tr(scribus_scalegroup__doc__)},
733
        {const_cast<char*>("scaleImage"), scribus_scaleimage, METH_VARARGS, tr(scribus_scaleimage__doc__)},
734
        {const_cast<char*>("selectionCount"), (PyCFunction)scribus_selcount, METH_NOARGS, tr(scribus_selcount__doc__)},
735
        {const_cast<char*>("selectObject"), scribus_selectobj, METH_VARARGS, tr(scribus_selectobj__doc__)},
736
        {const_cast<char*>("selectText"), scribus_selecttext, METH_VARARGS, tr(scribus_selecttext__doc__)},
737
        {const_cast<char*>("sentToLayer"), scribus_senttolayer, METH_VARARGS, tr(scribus_senttolayer__doc__)},
738
        {const_cast<char*>("setActiveLayer"), scribus_setactlayer, METH_VARARGS, tr(scribus_setactlayer__doc__)},
739
        {const_cast<char*>("setColumnGap"), scribus_setcolumngap, METH_VARARGS, tr(scribus_setcolumngap__doc__)},
740
        {const_cast<char*>("setColumns"), scribus_setcolumns, METH_VARARGS, tr(scribus_setcolumns__doc__)},
741
        {const_cast<char*>("setCornerRadius"), scribus_setcornerrad, METH_VARARGS, tr(scribus_setcornerrad__doc__)},
742
        {const_cast<char*>("setCursor"), scribus_setcursor, METH_VARARGS, tr(scribus_setcursor__doc__)},
743
        {const_cast<char*>("setDocType"), scribus_setdoctype, METH_VARARGS, tr(scribus_setdoctype__doc__)},
744
        {const_cast<char*>("setFillColor"), scribus_setfillcolor, METH_VARARGS, tr(scribus_setfillcolor__doc__)},
745
        {const_cast<char*>("setFillShade"), scribus_setfillshade, METH_VARARGS, tr(scribus_setfillshade__doc__)},
746
        {const_cast<char*>("setFont"), scribus_setfont, METH_VARARGS, tr(scribus_setfont__doc__)},
747
        {const_cast<char*>("setFontSize"), scribus_setfontsize, METH_VARARGS, tr(scribus_setfontsize__doc__)},
748
        {const_cast<char*>("setGradientFill"), scribus_setgradfill, METH_VARARGS, tr(scribus_setgradfill__doc__)},
749
        {const_cast<char*>("setHGuides"), scribus_setHguides, METH_VARARGS, tr(scribus_setHguides__doc__)},
750
        {const_cast<char*>("setInfo"), scribus_setinfo, METH_VARARGS, tr(scribus_setinfo__doc__)},
751
        {const_cast<char*>("setLayerPrintable"), scribus_layerprint, METH_VARARGS, tr(scribus_layerprint__doc__)},
752
        {const_cast<char*>("setLayerVisible"), scribus_layervisible, METH_VARARGS, tr(scribus_layervisible__doc__)},
753
        {const_cast<char*>("setLineCap"), scribus_setlineend, METH_VARARGS, tr(scribus_setlineend__doc__)},
754
        {const_cast<char*>("setLineColor"), scribus_setlinecolor, METH_VARARGS, tr(scribus_setlinecolor__doc__)},
755
        {const_cast<char*>("setLineJoin"), scribus_setlinejoin, METH_VARARGS, tr(scribus_setlinejoin__doc__)},
756
        {const_cast<char*>("setLineShade"), scribus_setlineshade, METH_VARARGS, tr(scribus_setlineshade__doc__)},
757
        {const_cast<char*>("setLineSpacing"), scribus_setlinespace, METH_VARARGS, tr(scribus_setlinespace__doc__)},
758
        {const_cast<char*>("setLineStyle"), scribus_setlinestyle, METH_VARARGS, tr(scribus_setlinestyle__doc__)},
759
        {const_cast<char*>("setLineWidth"), scribus_setlinewidth, METH_VARARGS, tr(scribus_setlinewidth__doc__)},
760
        {const_cast<char*>("setMargins"), scribus_setmargins, METH_VARARGS, tr(scribus_setmargins__doc__)},
761
        {const_cast<char*>("setMultiLine"), scribus_setmultiline, METH_VARARGS, tr(scribus_setmultiline__doc__)},
733 subik 762
        // duplicity? {"setMultiLine", scribus_setmultiline, METH_VARARGS, "TODO: docstring"},
934 subik 763
        {const_cast<char*>("setRedraw"), scribus_setredraw, METH_VARARGS, tr(scribus_setredraw__doc__)},
733 subik 764
        // missing? {"setSelectedObject", scribus_setselobjnam, METH_VARARGS, "Returns the Name of the selecteted Object. \"nr\" if given indicates the Number of the selected Object, e.g. 0 means the first selected Object, 1 means the second selected Object and so on."},
1381 cbradney 765
        {const_cast<char*>("setScaleImageToFrame"), (PyCFunction)scribus_setscaleimagetoframe, METH_KEYWORDS, tr(scribus_setscaleimagetoframe__doc__)},
934 subik 766
        {const_cast<char*>("setStyle"), scribus_setstyle, METH_VARARGS, tr(scribus_setstyle__doc__)},
767
        {const_cast<char*>("setTextAlignment"), scribus_setalign, METH_VARARGS, tr(scribus_setalign__doc__)},
768
        {const_cast<char*>("setTextColor"), scribus_settextfill, METH_VARARGS, tr(scribus_settextfill__doc__)},
769
        {const_cast<char*>("setText"), scribus_setboxtext, METH_VARARGS, tr(scribus_setboxtext__doc__)},
770
        {const_cast<char*>("setTextShade"), scribus_settextshade, METH_VARARGS, tr(scribus_settextshade__doc__)},
771
        {const_cast<char*>("setTextStroke"), scribus_settextstroke, METH_VARARGS, tr(scribus_settextstroke__doc__)},
772
        {const_cast<char*>("setUnit"), scribus_setunit, METH_VARARGS, tr(scribus_setunit__doc__)},
773
        {const_cast<char*>("setVGuides"), scribus_setVguides, METH_VARARGS, tr(scribus_setVguides__doc__)},
774
        {const_cast<char*>("sizeObject"), scribus_sizeobjabs, METH_VARARGS, tr(scribus_sizeobjabs__doc__)},
775
        {const_cast<char*>("statusMessage"), scribus_messagebartext, METH_VARARGS, tr(scribus_messagebartext__doc__)},
776
        {const_cast<char*>("textFlowsAroundFrame"), scribus_textflow, METH_VARARGS, tr(scribus_textflow__doc__)},
777
        {const_cast<char*>("traceText"), scribus_tracetext, METH_VARARGS, tr(scribus_tracetext__doc__)},
778
        {const_cast<char*>("unGroupObject"), scribus_ungroupobj, METH_VARARGS, tr(scribus_ungroupobj__doc__)},
779
        {const_cast<char*>("unlinkTextFrames"), scribus_unlinktextframes, METH_VARARGS, tr(scribus_unlinktextframes__doc__)},
780
        {const_cast<char*>("valueDialog"), scribus_valdialog, METH_VARARGS, tr(scribus_valdialog__doc__)},
421 Franz 781
        // end of aliases
934 subik 782
        {const_cast<char*>("retval"), scribus_retval, METH_VARARGS, const_cast<char*>("Scribus internal.")},
783
        {const_cast<char*>("getval"), (PyCFunction)scribus_getval, METH_NOARGS, const_cast<char*>("Scribus internal.")},
1043 subik 784
        {NULL, (PyCFunction)(0), 0, NULL} /* sentinel */
82 Franz 785
};
786
 
787
void initscribus(ScribusApp *pl)
788
{
789
        PyObject *m, *d;
934 subik 790
        PyImport_AddModule((char*)"scribus");
411 Franz 791
        PyType_Ready(&Printer_Type);
418 Franz 792
        PyType_Ready(&PDFfile_Type);
934 subik 793
        m = Py_InitModule((char*)"scribus", scribus_methods);
411 Franz 794
        Py_INCREF(&Printer_Type);
934 subik 795
        PyModule_AddObject(m, (char*)"Printer", (PyObject *) &Printer_Type);
418 Franz 796
        Py_INCREF(&PDFfile_Type);
934 subik 797
        PyModule_AddObject(m, (char*)"PDFfile", (PyObject *) &PDFfile_Type);
82 Franz 798
        d = PyModule_GetDict(m);
647 fschmid 799
 
800
        // Set up the module exceptions
801
        // common exc.
934 subik 802
        ScribusException = PyErr_NewException((char*)"scribus.ScribusException", NULL, NULL);
647 fschmid 803
        Py_INCREF(ScribusException);
934 subik 804
        PyModule_AddObject(m, (char*)"ScribusException", ScribusException);
647 fschmid 805
        // no doc open
934 subik 806
        NoDocOpenError = PyErr_NewException((char*)"scribus.NoDocOpenError", ScribusException, NULL);
647 fschmid 807
        Py_INCREF(NoDocOpenError);
934 subik 808
        PyModule_AddObject(m, (char*)"NoDocOpenError", NoDocOpenError);
853 subik 809
        // wrong type of frame for operation
810
        WrongFrameTypeError = PyErr_NewException((char*)"scribus.WrongFrameTypeError", ScribusException, NULL);
811
        Py_INCREF(WrongFrameTypeError);
812
        PyModule_AddObject(m, (char*)"WrongFrameTypeError", WrongFrameTypeError);
813
        // Couldn't find named object, or no named object and no selection
814
        NoValidObjectError = PyErr_NewException((char*)"scribus.NoValidObjectError", ScribusException, NULL);
815
        Py_INCREF(NoValidObjectError);
816
        PyModule_AddObject(m, (char*)"NoValidObjectError", NoValidObjectError);
899 cbradney 817
        // Couldn't find the specified resource - font, color, etc.
818
        NotFoundError = PyErr_NewException((char*)"scribus.NotFoundError", ScribusException, NULL);
819
        Py_INCREF(NotFoundError);
820
        PyModule_AddObject(m, (char*)"NotFoundError", NotFoundError);
821
        // Tried to create an object with the same name as one that already exists
822
        NameExistsError = PyErr_NewException((char*)"scribus.NameExistsError", ScribusException, NULL);
823
        Py_INCREF(NameExistsError);
824
        PyModule_AddObject(m, (char*)"NameExistsError", NameExistsError);
647 fschmid 825
        // Done with exception setup
826
 
421 Franz 827
        // CONSTANTS
934 subik 828
        PyDict_SetItemString(d, const_cast<char*>("UNIT_POINTS"), Py_BuildValue(const_cast<char*>("i"), 0));
829
        PyDict_SetItemString(d, const_cast<char*>("UNIT_MILLIMETERS"), Py_BuildValue(const_cast<char*>("i"), 1));
830
        PyDict_SetItemString(d, const_cast<char*>("UNIT_INCHES"), Py_BuildValue(const_cast<char*>("i"), 2));
831
        PyDict_SetItemString(d, const_cast<char*>("UNIT_PICAS"), Py_BuildValue(const_cast<char*>("i"), 3));
832
        PyDict_SetItemString(d, const_cast<char*>("PORTRAIT"), Py_BuildValue(const_cast<char*>("i"), 0));
833
        PyDict_SetItemString(d, const_cast<char*>("LANDSCAPE"), Py_BuildValue(const_cast<char*>("i"), 1));
834
        PyDict_SetItemString(d, const_cast<char*>("NOFACINGPAGES"), Py_BuildValue(const_cast<char*>("i"), 0));
835
        PyDict_SetItemString(d, const_cast<char*>("FACINGPAGES"),  Py_BuildValue(const_cast<char*>("i"), 1));
836
        PyDict_SetItemString(d, const_cast<char*>("FIRSTPAGERIGHT"), Py_BuildValue(const_cast<char*>("i"), 0));
837
        PyDict_SetItemString(d, const_cast<char*>("FIRSTPAGELEFT"), Py_BuildValue(const_cast<char*>("i"), 1));
838
        PyDict_SetItemString(d, const_cast<char*>("ALIGN_LEFT"), Py_BuildValue(const_cast<char*>("i"), 0));
839
        PyDict_SetItemString(d, const_cast<char*>("ALIGN_RIGHT"), Py_BuildValue(const_cast<char*>("i"), 2));
840
        PyDict_SetItemString(d, const_cast<char*>("ALIGN_CENTERED"), Py_BuildValue(const_cast<char*>("i"), 1));
841
        PyDict_SetItemString(d, const_cast<char*>("ALIGN_BLOCK"), Py_BuildValue(const_cast<char*>("i"), 4));
842
        PyDict_SetItemString(d, const_cast<char*>("ALIGN_FORCED"), Py_BuildValue(const_cast<char*>("i"), 4));
843
        PyDict_SetItemString(d, const_cast<char*>("FILL_NOG"), Py_BuildValue(const_cast<char*>("i"), 0));
844
        PyDict_SetItemString(d, const_cast<char*>("FILL_HORIZONTALG"), Py_BuildValue(const_cast<char*>("i"), 1));
845
        PyDict_SetItemString(d, const_cast<char*>("FILL_VERTICALG"), Py_BuildValue(const_cast<char*>("i"), 2));
846
        PyDict_SetItemString(d, const_cast<char*>("FILL_DIAGONALG"), Py_BuildValue(const_cast<char*>("i"), 3));
847
        PyDict_SetItemString(d, const_cast<char*>("FILL_CROSSDIAGONALG"), Py_BuildValue(const_cast<char*>("i"), 4));
848
        PyDict_SetItemString(d, const_cast<char*>("FILL_RADIALG"), Py_BuildValue(const_cast<char*>("i"), 5));
849
        PyDict_SetItemString(d, const_cast<char*>("LINE_SOLID"), Py_BuildValue(const_cast<char*>("i"), Qt::SolidLine));
850
        PyDict_SetItemString(d, const_cast<char*>("LINE_DASH"), Py_BuildValue(const_cast<char*>("i"), Qt::DashLine));
851
        PyDict_SetItemString(d, const_cast<char*>("LINE_DOT"), Py_BuildValue(const_cast<char*>("i"), Qt::DotLine));
852
        PyDict_SetItemString(d, const_cast<char*>("LINE_DASHDOT"), Py_BuildValue(const_cast<char*>("i"), Qt::DashDotLine));
853
        PyDict_SetItemString(d, const_cast<char*>("LINE_DASHDOTDOT"), Py_BuildValue(const_cast<char*>("i"), Qt::DashDotDotLine));
854
        PyDict_SetItemString(d, const_cast<char*>("JOIN_MITTER"), Py_BuildValue(const_cast<char*>("i"), Qt::MiterJoin));
855
        PyDict_SetItemString(d, const_cast<char*>("JOIN_BEVEL"), Py_BuildValue(const_cast<char*>("i"), Qt::BevelJoin));
856
        PyDict_SetItemString(d, const_cast<char*>("JOIN_ROUND"), Py_BuildValue(const_cast<char*>("i"), Qt::RoundJoin));
857
        PyDict_SetItemString(d, const_cast<char*>("CAP_FLAT"), Py_BuildValue(const_cast<char*>("i"), Qt::FlatCap));
858
        PyDict_SetItemString(d, const_cast<char*>("CAP_SQUARE"), Py_BuildValue(const_cast<char*>("i"), Qt::SquareCap));
859
        PyDict_SetItemString(d, const_cast<char*>("CAP_ROUND"), Py_BuildValue(const_cast<char*>("i"), Qt::RoundCap));
860
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_NONE"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::NoButton));
861
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_OK"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Ok));
862
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_CANCEL"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Cancel));
863
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_YES"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Yes));
864
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_NO"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::No));
865
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_ABORT"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Abort));
866
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_RETRY"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Retry));
867
        PyDict_SetItemString(d, const_cast<char*>("BUTTON_IGNORE"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Ignore));
868
        PyDict_SetItemString(d, const_cast<char*>("ICON_NONE"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::NoIcon));
869
        PyDict_SetItemString(d, const_cast<char*>("ICON_INFORMATION"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Information));
870
        PyDict_SetItemString(d, const_cast<char*>("ICON_WARNING"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Warning));
871
        PyDict_SetItemString(d, const_cast<char*>("ICON_CRITICAL"), Py_BuildValue(const_cast<char*>("i"), QMessageBox::Critical));
872
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A0"), Py_BuildValue(const_cast<char*>("(ff)"), 2380.0, 3368.0));
873
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A1"), Py_BuildValue(const_cast<char*>("(ff)"), 1684.0, 2380.0));
874
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A2"), Py_BuildValue(const_cast<char*>("(ff)"), 1190.0, 1684.0));
875
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A3"), Py_BuildValue(const_cast<char*>("(ff)"), 842.0, 1190.0));
876
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A4"), Py_BuildValue(const_cast<char*>("(ff)"), 595.0, 842.0));
877
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A5"), Py_BuildValue(const_cast<char*>("(ff)"), 421.0, 595.0));
878
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A6"), Py_BuildValue(const_cast<char*>("(ff)"), 297.0, 421.0));
879
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A7"), Py_BuildValue(const_cast<char*>("(ff)"), 210.0, 297.0));
880
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A8"), Py_BuildValue(const_cast<char*>("(ff)"), 148.0, 210.0));
881
        PyDict_SetItemString(d, const_cast<char*>("PAPER_A9"), Py_BuildValue(const_cast<char*>("(ff)"), 105.0, 148.0));
882
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B0"), Py_BuildValue(const_cast<char*>("(ff)"), 2836.0, 4008.0));
883
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B1"), Py_BuildValue(const_cast<char*>("(ff)"), 2004.0, 2836.0));
884
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B2"), Py_BuildValue(const_cast<char*>("(ff)"), 1418.0, 2004.0));
885
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B3"), Py_BuildValue(const_cast<char*>("(ff)"), 1002.0, 1418.0));
886
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B4"), Py_BuildValue(const_cast<char*>("(ff)"), 709.0, 1002.0));
887
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B5"), Py_BuildValue(const_cast<char*>("(ff)"), 501.0, 709.0));
888
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B6"), Py_BuildValue(const_cast<char*>("(ff)"), 355.0, 501.0));
889
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B7"), Py_BuildValue(const_cast<char*>("(ff)"), 250.0, 355.0));
890
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B8"), Py_BuildValue(const_cast<char*>("(ff)"), 178.0, 250.0));
891
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B9"), Py_BuildValue(const_cast<char*>("(ff)"), 125.0, 178.0));
892
        PyDict_SetItemString(d, const_cast<char*>("PAPER_B10"), Py_BuildValue(const_cast<char*>("(ff)"), 89.0, 125.0));
893
        PyDict_SetItemString(d, const_cast<char*>("PAPER_C5E"), Py_BuildValue(const_cast<char*>("(ff)"), 462.0, 649.0));
894
        PyDict_SetItemString(d, const_cast<char*>("PAPER_COMM10E"), Py_BuildValue(const_cast<char*>("(ff)"), 298.0, 683.0));
895
        PyDict_SetItemString(d, const_cast<char*>("PAPER_DLE"),  Py_BuildValue(const_cast<char*>("(ff)"), 312.0, 624.0));
896
        PyDict_SetItemString(d, const_cast<char*>("PAPER_EXECUTIVE"), Py_BuildValue(const_cast<char*>("(ff)"), 542.0, 720.0));
897
        PyDict_SetItemString(d, const_cast<char*>("PAPER_FOLIO"), Py_BuildValue(const_cast<char*>("(ff)"), 595.0, 935.0));
898
        PyDict_SetItemString(d, const_cast<char*>("PAPER_LEDGER"), Py_BuildValue(const_cast<char*>("(ff)"), 1224.0, 792.0));
899
        PyDict_SetItemString(d, const_cast<char*>("PAPER_LEGAL"), Py_BuildValue(const_cast<char*>("(ff)"), 612.0, 1008.0));
900
        PyDict_SetItemString(d, const_cast<char*>("PAPER_LETTER"), Py_BuildValue(const_cast<char*>("(ff)"), 612.0, 792.0));
901
        PyDict_SetItemString(d, const_cast<char*>("PAPER_TABLOID"), Py_BuildValue(const_cast<char*>("(ff)"), 792.0, 1224.0));
733 subik 902
 
1043 subik 903
        // Export the Scribus version into the module namespace so scripts know what they're running in
904
        PyDict_SetItemString(d, const_cast<char*>("scribus_version"), PyString_FromString(const_cast<char*>(VERSION)));
905
        // Now build a version tuple like that provided by Python in sys.version_info
906
        // The tuple is of the form (major, minor, patchlevel, extraversion, reserved)
907
        QRegExp version_re("(\\d+)\\.(\\d+)\\.(\\d+)(.*)");
908
        int pos = version_re.search(QString(VERSION));
909
        // We ignore errors, causing the scribus_version_info attribute to simply not be created.
910
        // This will make acceses raise AttrbuteError.
911
        if (pos > -1)
912
        {
913
                int majorVersion = version_re.cap(1).toInt();
914
                int minorVersion = version_re.cap(2).toInt();
915
                int patchVersion = version_re.cap(3).toInt();
916
                QString extraVersion = version_re.cap(4);
917
                PyObject* versionTuple = Py_BuildValue(const_cast<char*>("(iiisi)"),\
918
                                majorVersion, minorVersion, patchVersion, (const char*)extraVersion.utf8(), 0);
919
                if (versionTuple != NULL)
920
                        PyDict_SetItemString(d, const_cast<char*>("scribus_version_info"), versionTuple);
921
                else
922
                        qDebug("Failed to build version tuple for version string '%s' in scripter", VERSION);
923
        }
924
        else
925
                qDebug("Couldn't parse version string '%s' in scripter", VERSION);
926
 
82 Franz 927
        Carrier = pl;
655 fschmid 928
        // Function aliases for compatibility
929
        // We need to import the __builtins__, warnings and exceptions modules to be able to run
930
        // the generated Python functions from inside the `scribus' module's context.
931
        // This code makes it possible to extend the `scribus' module by running Python code
932
        // from C in other ways too.
934 subik 933
        PyObject* builtinModule = PyImport_ImportModuleEx(const_cast<char*>("__builtin__"),
934
                        d, d, Py_BuildValue(const_cast<char*>("[]")));
655 fschmid 935
        if (builtinModule == NULL)
936
        {
937
                qDebug("Failed to import __builtin__ module. Something is probably broken with your Python.");
938
                return;
939
        }
934 subik 940
        PyDict_SetItemString(d, const_cast<char*>("__builtin__"), builtinModule);
941
        PyObject* exceptionsModule = PyImport_ImportModuleEx(const_cast<char*>("exceptions"),
942
                        d, d, Py_BuildValue(const_cast<char*>("[]")));
655 fschmid 943
        if (exceptionsModule == NULL)
944
        {
945
                qDebug("Failed to import exceptions module. Something is probably broken with your Python.");
946
                return;
947
        }
934 subik 948
        PyDict_SetItemString(d, const_cast<char*>("exceptions"), exceptionsModule);
949
        PyObject* warningsModule = PyImport_ImportModuleEx(const_cast<char*>("warnings"),
950
                        d, d, Py_BuildValue(const_cast<char*>("[]")));
655 fschmid 951
        if (warningsModule == NULL)
952
        {
953
                qDebug("Failed to import warnings module. Something is probably broken with your Python.");
954
                return;
955
        }
934 subik 956
        PyDict_SetItemString(d, const_cast<char*>("warnings"), warningsModule);
655 fschmid 957
        // Now actually add the aliases
934 subik 958
        deprecatedFunctionAlias(d, const_cast<char*>("changeColor"), const_cast<char*>("ChangeColor"));
959
        deprecatedFunctionAlias(d, const_cast<char*>("closeDoc"), const_cast<char*>("CloseDoc"));
960
        deprecatedFunctionAlias(d, const_cast<char*>("createBezierLine"), const_cast<char*>("CreateBezierLine"));
961
        deprecatedFunctionAlias(d, const_cast<char*>("createEllipse"), const_cast<char*>("CreateEllipse"));
962
        deprecatedFunctionAlias(d, const_cast<char*>("createImage"), const_cast<char*>("CreateImage"));
963
        deprecatedFunctionAlias(d, const_cast<char*>("createLayer"), const_cast<char*>("CreateLayer"));
964
        deprecatedFunctionAlias(d, const_cast<char*>("createLine"), const_cast<char*>("CreateLine"));
965
        deprecatedFunctionAlias(d, const_cast<char*>("createPathText"), const_cast<char*>("CreatePathText"));
966
        deprecatedFunctionAlias(d, const_cast<char*>("createPolygon"), const_cast<char*>("CreatePolygon"));
967
        deprecatedFunctionAlias(d, const_cast<char*>("createPolyLine"), const_cast<char*>("CreatePolyLine"));
968
        deprecatedFunctionAlias(d, const_cast<char*>("createRect"), const_cast<char*>("CreateRect"));
969
        deprecatedFunctionAlias(d, const_cast<char*>("createText"), const_cast<char*>("CreateText"));
970
        deprecatedFunctionAlias(d, const_cast<char*>("currentPage"), const_cast<char*>("CurrentPage"));
971
        deprecatedFunctionAlias(d, const_cast<char*>("defineColor"), const_cast<char*>("DefineColor"));
972
        deprecatedFunctionAlias(d, const_cast<char*>("deleteColor"), const_cast<char*>("DeleteColor"));
973
        deprecatedFunctionAlias(d, const_cast<char*>("deleteLayer"), const_cast<char*>("DeleteLayer"));
974
        deprecatedFunctionAlias(d, const_cast<char*>("deleteObject"), const_cast<char*>("DeleteObject"));
975
        deprecatedFunctionAlias(d, const_cast<char*>("deletePage"), const_cast<char*>("DeletePage"));
976
        deprecatedFunctionAlias(d, const_cast<char*>("deleteText"), const_cast<char*>("DeleteText"));
977
        deprecatedFunctionAlias(d, const_cast<char*>("deselectAll"), const_cast<char*>("DeselectAll"));
978
        deprecatedFunctionAlias(d, const_cast<char*>("docChanged"), const_cast<char*>("DocChanged"));
979
        deprecatedFunctionAlias(d, const_cast<char*>("fileDialog"), const_cast<char*>("FileDialog"));
980
        deprecatedFunctionAlias(d, const_cast<char*>("getActiveLayer"), const_cast<char*>("GetActiveLayer"));
981
        deprecatedFunctionAlias(d, const_cast<char*>("getAllObjects"), const_cast<char*>("GetAllObjects"));
982
        deprecatedFunctionAlias(d, const_cast<char*>("getAllStyles"), const_cast<char*>("GetAllStyles"));
983
        deprecatedFunctionAlias(d, const_cast<char*>("getAllText"), const_cast<char*>("GetAllText"));
984
        deprecatedFunctionAlias(d, const_cast<char*>("getColor"), const_cast<char*>("GetColor"));
985
        deprecatedFunctionAlias(d, const_cast<char*>("getColorNames"), const_cast<char*>("GetColorNames"));
986
        deprecatedFunctionAlias(d, const_cast<char*>("getColumnGap"), const_cast<char*>("GetColumnGap"));
987
        deprecatedFunctionAlias(d, const_cast<char*>("getColumns"), const_cast<char*>("GetColumns"));
988
        deprecatedFunctionAlias(d, const_cast<char*>("getCornerRadius"), const_cast<char*>("GetCornerRadius"));
989
        deprecatedFunctionAlias(d, const_cast<char*>("getFillColor"), const_cast<char*>("GetFillColor"));
990
        deprecatedFunctionAlias(d, const_cast<char*>("getFillShade"), const_cast<char*>("GetFillShade"));
991
        deprecatedFunctionAlias(d, const_cast<char*>("getFont"), const_cast<char*>("GetFont"));
992
        deprecatedFunctionAlias(d, const_cast<char*>("getFontNames"), const_cast<char*>("GetFontNames"));
993
        deprecatedFunctionAlias(d, const_cast<char*>("getFontSize"), const_cast<char*>("GetFontSize"));
994
        deprecatedFunctionAlias(d, const_cast<char*>("getGuiLanguage"), const_cast<char*>("GetGuiLanguage"));
995
        deprecatedFunctionAlias(d, const_cast<char*>("getHGuides"), const_cast<char*>("GetHGuides"));
996
        deprecatedFunctionAlias(d, const_cast<char*>("getImageFile"), const_cast<char*>("GetImageFile"));
997
        deprecatedFunctionAlias(d, const_cast<char*>("getImageScale"), const_cast<char*>("GetImageScale"));
998
        deprecatedFunctionAlias(d, const_cast<char*>("getLayers"), const_cast<char*>("GetLayers"));
999
        deprecatedFunctionAlias(d, const_cast<char*>("getLineCap"), const_cast<char*>("GetLineCap"));
1000
        deprecatedFunctionAlias(d, const_cast<char*>("getLineColor"), const_cast<char*>("GetLineColor"));
1001
        deprecatedFunctionAlias(d, const_cast<char*>("getLineJoin"), const_cast<char*>("GetLineJoin"));
1002
        deprecatedFunctionAlias(d, const_cast<char*>("getLineShade"), const_cast<char*>("GetLineShade"));
1003
        deprecatedFunctionAlias(d, const_cast<char*>("getLineSpacing"), const_cast<char*>("GetLineSpacing"));
1004
        deprecatedFunctionAlias(d, const_cast<char*>("getLineStyle"), const_cast<char*>("GetLineStyle"));
1005
        deprecatedFunctionAlias(d, const_cast<char*>("getLineWidth"), const_cast<char*>("GetLineWidth"));
1006
        deprecatedFunctionAlias(d, const_cast<char*>("getPageItems"), const_cast<char*>("GetPageItems"));
1007
        deprecatedFunctionAlias(d, const_cast<char*>("getPageMargins"), const_cast<char*>("GetPageMargins"));
1008
        deprecatedFunctionAlias(d, const_cast<char*>("getPageSize"), const_cast<char*>("GetPageSize"));
1009
        deprecatedFunctionAlias(d, const_cast<char*>("getPosition"), const_cast<char*>("GetPosition"));
1010
        deprecatedFunctionAlias(d, const_cast<char*>("getRotation"), const_cast<char*>("GetRotation"));
1011
        deprecatedFunctionAlias(d, const_cast<char*>("getSelectedObject"), const_cast<char*>("GetSelectedObject"));
1012
        deprecatedFunctionAlias(d, const_cast<char*>("getSize"), const_cast<char*>("GetSize"));
1013
        deprecatedFunctionAlias(d, const_cast<char*>("getText"), const_cast<char*>("GetText"));
1014
        deprecatedFunctionAlias(d, const_cast<char*>("getTextColor"), const_cast<char*>("GetTextColor"));
1015
        deprecatedFunctionAlias(d, const_cast<char*>("getTextLength"), const_cast<char*>("GetTextLength"));
1016
        deprecatedFunctionAlias(d, const_cast<char*>("getTextShade"), const_cast<char*>("GetTextShade"));
1017
        deprecatedFunctionAlias(d, const_cast<char*>("getUnit"), const_cast<char*>("GetUnit"));
1018
        deprecatedFunctionAlias(d, const_cast<char*>("getVGuides"), const_cast<char*>("GetVGuides"));
1019
        deprecatedFunctionAlias(d, const_cast<char*>("getXFontNames"), const_cast<char*>("GetXFontNames"));
1020
        deprecatedFunctionAlias(d, const_cast<char*>("gotoPage"), const_cast<char*>("GotoPage"));
1021
        deprecatedFunctionAlias(d, const_cast<char*>("groupObjects"), const_cast<char*>("GroupObjects"));
1022
        deprecatedFunctionAlias(d, const_cast<char*>("haveDoc"), const_cast<char*>("HaveDoc"));
1023
        deprecatedFunctionAlias(d, const_cast<char*>("insertText"), const_cast<char*>("InsertText"));
1024
        deprecatedFunctionAlias(d, const_cast<char*>("isLayerPrintable"), const_cast<char*>("IsLayerPrintable"));
1025
        deprecatedFunctionAlias(d, const_cast<char*>("isLayerVisible"), const_cast<char*>("IsLayerVisible"));
1026
        deprecatedFunctionAlias(d, const_cast<char*>("isLocked"), const_cast<char*>("IsLocked"));
1027
        deprecatedFunctionAlias(d, const_cast<char*>("linkTextFrames"), const_cast<char*>("LinkTextFrames"));
1028
        deprecatedFunctionAlias(d, const_cast<char*>("loadImage"), const_cast<char*>("LoadImage"));
1029
        deprecatedFunctionAlias(d, const_cast<char*>("loadStylesFromFile"), const_cast<char*>("LoadStylesFromFile"));
1030
        deprecatedFunctionAlias(d, const_cast<char*>("lockObject"), const_cast<char*>("LockObject"));
1031
        deprecatedFunctionAlias(d, const_cast<char*>("messagebarText"), const_cast<char*>("MessagebarText"));
1032
        deprecatedFunctionAlias(d, const_cast<char*>("messageBox"), const_cast<char*>("MessageBox"));
1033
        deprecatedFunctionAlias(d, const_cast<char*>("moveObject"), const_cast<char*>("MoveObject"));
1034
        deprecatedFunctionAlias(d, const_cast<char*>("moveObjectAbs"), const_cast<char*>("MoveObjectAbs"));
1035
        deprecatedFunctionAlias(d, const_cast<char*>("newDoc"), const_cast<char*>("NewDoc"));
1036
        deprecatedFunctionAlias(d, const_cast<char*>("newDocDialog"), const_cast<char*>("NewDocDialog"));
1037
        deprecatedFunctionAlias(d, const_cast<char*>("newPage"), const_cast<char*>("NewPage"));
1038
        deprecatedFunctionAlias(d, const_cast<char*>("objectExists"), const_cast<char*>("ObjectExists"));
1039
        deprecatedFunctionAlias(d, const_cast<char*>("openDoc"), const_cast<char*>("OpenDoc"));
1040
        deprecatedFunctionAlias(d, const_cast<char*>("pageCount"), const_cast<char*>("PageCount"));
1041
        deprecatedFunctionAlias(d, const_cast<char*>("pageDimension"), const_cast<char*>("PageDimension"));
1042
        deprecatedFunctionAlias(d, const_cast<char*>("progressReset"), const_cast<char*>("ProgressReset"));
1043
        deprecatedFunctionAlias(d, const_cast<char*>("progressSet"), const_cast<char*>("ProgressSet"));
1044
        deprecatedFunctionAlias(d, const_cast<char*>("progressTotal"), const_cast<char*>("ProgressTotal"));
1045
        deprecatedFunctionAlias(d, const_cast<char*>("redrawAll"), const_cast<char*>("RedrawAll"));
1046
        deprecatedFunctionAlias(d, const_cast<char*>("renderFont"), const_cast<char*>("RenderFont"));
1047
        deprecatedFunctionAlias(d, const_cast<char*>("replaceColor"), const_cast<char*>("ReplaceColor"));
1048
        deprecatedFunctionAlias(d, const_cast<char*>("rotateObject"), const_cast<char*>("RotateObject"));
1049
        deprecatedFunctionAlias(d, const_cast<char*>("rotateObjectAbs"), const_cast<char*>("RotateObjectAbs"));
1050
        deprecatedFunctionAlias(d, const_cast<char*>("saveDoc"), const_cast<char*>("SaveDoc"));
1051
        deprecatedFunctionAlias(d, const_cast<char*>("saveDocAs"), const_cast<char*>("SaveDocAs"));
1052
        deprecatedFunctionAlias(d, const_cast<char*>("savePageAsEPS"), const_cast<char*>("SavePageAsEPS"));
1053
        deprecatedFunctionAlias(d, const_cast<char*>("scaleGroup"), const_cast<char*>("ScaleGroup"));
1054
        deprecatedFunctionAlias(d, const_cast<char*>("scaleImage"), const_cast<char*>("ScaleImage"));
1055
        deprecatedFunctionAlias(d, const_cast<char*>("selectionCount"), const_cast<char*>("SelectionCount"));
1056
        deprecatedFunctionAlias(d, const_cast<char*>("selectObject"), const_cast<char*>("SelectObject"));
1057
        deprecatedFunctionAlias(d, const_cast<char*>("selectText"), const_cast<char*>("SelectText"));
1058
        deprecatedFunctionAlias(d, const_cast<char*>("sentToLayer"), const_cast<char*>("SentToLayer"));
1059
        deprecatedFunctionAlias(d, const_cast<char*>("setActiveLayer"), const_cast<char*>("SetActiveLayer"));
1060
        deprecatedFunctionAlias(d, const_cast<char*>("setColumnGap"), const_cast<char*>("SetColumnGap"));
1061
        deprecatedFunctionAlias(d, const_cast<char*>("setColumns"), const_cast<char*>("SetColumns"));
1062
        deprecatedFunctionAlias(d, const_cast<char*>("setCornerRadius"), const_cast<char*>("SetCornerRadius"));
1063
        deprecatedFunctionAlias(d, const_cast<char*>("setCursor"), const_cast<char*>("SetCursor"));
1064
        deprecatedFunctionAlias(d, const_cast<char*>("setDocType"), const_cast<char*>("SetDocType"));
1065
        deprecatedFunctionAlias(d, const_cast<char*>("setFillColor"), const_cast<char*>("SetFillColor"));
1066
        deprecatedFunctionAlias(d, const_cast<char*>("setFillShade"), const_cast<char*>("SetFillShade"));
1067
        deprecatedFunctionAlias(d, const_cast<char*>("setFont"), const_cast<char*>("SetFont"));
1068
        deprecatedFunctionAlias(d, const_cast<char*>("setFontSize"), const_cast<char*>("SetFontSize"));
1069
        deprecatedFunctionAlias(d, const_cast<char*>("setGradientFill"), const_cast<char*>("SetGradientFill"));
1070
        deprecatedFunctionAlias(d, const_cast<char*>("setHGuides"), const_cast<char*>("SetHGuides"));
1071
        deprecatedFunctionAlias(d, const_cast<char*>("setInfo"), const_cast<char*>("SetInfo"));
1072
        deprecatedFunctionAlias(d, const_cast<char*>("setLayerPrintable"), const_cast<char*>("SetLayerPrintable"));
1073
        deprecatedFunctionAlias(d, const_cast<char*>("setLayerVisible"), const_cast<char*>("SetLayerVisible"));
1074
        deprecatedFunctionAlias(d, const_cast<char*>("setLineCap"), const_cast<char*>("SetLineCap"));
1075
        deprecatedFunctionAlias(d, const_cast<char*>("setLineColor"), const_cast<char*>("SetLineColor"));
1076
        deprecatedFunctionAlias(d, const_cast<char*>("setLineJoin"), const_cast<char*>("SetLineJoin"));
1077
        deprecatedFunctionAlias(d, const_cast<char*>("setLineShade"), const_cast<char*>("SetLineShade"));
1078
        deprecatedFunctionAlias(d, const_cast<char*>("setLineSpacing"), const_cast<char*>("SetLineSpacing"));
1079
        deprecatedFunctionAlias(d, const_cast<char*>("setLineStyle"), const_cast<char*>("SetLineStyle"));
1080
        deprecatedFunctionAlias(d, const_cast<char*>("setLineWidth"), const_cast<char*>("SetLineWidth"));
1081
        deprecatedFunctionAlias(d, const_cast<char*>("setMargins"), const_cast<char*>("SetMargins"));
1082
        deprecatedFunctionAlias(d, const_cast<char*>("setMultiLine"), const_cast<char*>("SetMultiLine"));
1083
        deprecatedFunctionAlias(d, const_cast<char*>("setMultiLine"), const_cast<char*>("SetMultiLine"));
1084
        deprecatedFunctionAlias(d, const_cast<char*>("setRedraw"), const_cast<char*>("SetRedraw"));
1085
        deprecatedFunctionAlias(d, const_cast<char*>("setSelectedObject"), const_cast<char*>("SetSelectedObject"));
1086
        deprecatedFunctionAlias(d, const_cast<char*>("setStyle"), const_cast<char*>("SetStyle"));
1087
        deprecatedFunctionAlias(d, const_cast<char*>("setText"), const_cast<char*>("SetText"));
1088
        deprecatedFunctionAlias(d, const_cast<char*>("setTextAlignment"), const_cast<char*>("SetTextAlignment"));
1089
        deprecatedFunctionAlias(d, const_cast<char*>("setTextColor"), const_cast<char*>("SetTextColor"));
1090
        deprecatedFunctionAlias(d, const_cast<char*>("setTextShade"), const_cast<char*>("SetTextShade"));
1091
        deprecatedFunctionAlias(d, const_cast<char*>("setTextStroke"), const_cast<char*>("SetTextStroke"));
1092
        deprecatedFunctionAlias(d, const_cast<char*>("setUnit"), const_cast<char*>("SetUnit"));
1093
        deprecatedFunctionAlias(d, const_cast<char*>("setVGuides"), const_cast<char*>("SetVGuides"));
1094
        deprecatedFunctionAlias(d, const_cast<char*>("sizeObject"), const_cast<char*>("SizeObject"));
1095
        deprecatedFunctionAlias(d, const_cast<char*>("statusMessage"), const_cast<char*>("StatusMessage"));
1096
        deprecatedFunctionAlias(d, const_cast<char*>("textFlowsAroundFrame"), const_cast<char*>("TextFlowsAroundFrame"));
1097
        deprecatedFunctionAlias(d, const_cast<char*>("traceText"), const_cast<char*>("TraceText"));
1098
        deprecatedFunctionAlias(d, const_cast<char*>("unGroupObject"), const_cast<char*>("UnGroupObject"));
1099
        deprecatedFunctionAlias(d, const_cast<char*>("unlinkTextFrames"), const_cast<char*>("UnlinkTextFrames"));
1100
        deprecatedFunctionAlias(d, const_cast<char*>("valueDialog"), const_cast<char*>("ValueDialog"));
655 fschmid 1101
        // end function aliases
737 subik 1102
        // legacy constants - alas, we can't print warnings when these
1103
        // are used.
1104
        constantAlias(d, "UNIT_POINTS", "Points");
1105
        constantAlias(d, "UNIT_MILLIMETERS", "Millimeters");
1106
        constantAlias(d, "UNIT_INCHES", "Inches");
1107
        constantAlias(d, "UNIT_PICAS", "Picas");
1108
        constantAlias(d, "PORTRAIT", "Portrait");
1109
        constantAlias(d, "LANDSCAPE", "Landscape");
1110
        constantAlias(d, "NOFACINGPAGES", "NoFacingPages");
1111
        constantAlias(d, "FACINGPAGES", "FacingPages");
1112
        constantAlias(d, "FIRSTPAGERIGHT", "FirstPageRight");
1113
        constantAlias(d, "FIRSTPAGELEFT", "FirstPageLeft");
1114
        constantAlias(d, "ALIGN_LEFT", "LeftAlign");
1115
        constantAlias(d, "ALIGN_RIGHT", "RightAlign");
1116
        constantAlias(d, "ALIGN_CENTERED", "Centered");
1117
        constantAlias(d, "ALIGN_FORCED", "Forced");
1118
        constantAlias(d, "FILL_NOG", "NoGradient");
1119
        constantAlias(d, "FILL_HORIZONTALG", "HorizontalGradient");
1120
        constantAlias(d, "FILL_VERTICALG", "VerticalGradient");
1121
        constantAlias(d, "FILL_DIAGONALG", "DiagonalGradient");
1122
        constantAlias(d, "FILL_CROSSDIAGONALG", "CrossDiagonalGradient");
1123
        constantAlias(d, "FILL_RADIALG", "RadialGradient");
1124
        constantAlias(d, "LINE_SOLID", "SolidLine");
1125
        constantAlias(d, "LINE_DASH", "DashLine");
1126
        constantAlias(d, "LINE_DOT", "DotLine");
1127
        constantAlias(d, "LINE_DASHDOT", "DashDotLine");
1128
        constantAlias(d, "LINE_DASHDOTDOT", "DashDotDotLine");
1129
        constantAlias(d, "JOIN_MITTER", "MiterJoin");
1130
        constantAlias(d, "JOIN_BEVEL", "BevelJoin");
1131
        constantAlias(d, "JOIN_ROUND", "RoundJoin");
1132
        constantAlias(d, "CAP_FLAT", "FlatCap");
1133
        constantAlias(d, "CAP_SQUARE", "SquareCap");
1134
        constantAlias(d, "CAP_ROUND", "RoundCap");
1135
        constantAlias(d, "BUTTON_NONE", "NoButton");
1136
        constantAlias(d, "BUTTON_OK", "Ok");
1137
        constantAlias(d, "BUTTON_CANCEL", "Cancel");
1138
        constantAlias(d, "BUTTON_YES", "Yes");
1139
        constantAlias(d, "BUTTON_NO", "No");
1140
        constantAlias(d, "BUTTON_ABORT", "Abort");
1141
        constantAlias(d, "BUTTON_RETRY", "Retry");
1142
        constantAlias(d, "BUTTON_IGNORE", "Ignore");
1143
        constantAlias(d, "ICON_NONE", "NoIcon");
1144
        constantAlias(d, "ICON_INFORMATION", "Information");
1145
        constantAlias(d, "ICON_WARNING", "Warning");
1146
        constantAlias(d, "ICON_CRITICAL", "Critical");
1147
        constantAlias(d, "PAPER_A0", "Paper_A0");
1148
        constantAlias(d, "PAPER_A1", "Paper_A1");
1149
        constantAlias(d, "PAPER_A2", "Paper_A2");
1150
        constantAlias(d, "PAPER_A3", "Paper_A3");
1151
        constantAlias(d, "PAPER_A4", "Paper_A4");
1152
        constantAlias(d, "PAPER_A5", "Paper_A5");
1153
        constantAlias(d, "PAPER_A6", "Paper_A6");
1154
        constantAlias(d, "PAPER_A7", "Paper_A7");
1155
        constantAlias(d, "PAPER_A8", "Paper_A8");
1156
        constantAlias(d, "PAPER_A9", "Paper_A9");
1157
        constantAlias(d, "PAPER_B0", "Paper_B0");
1158
        constantAlias(d, "PAPER_B1", "Paper_B1");
1159
        constantAlias(d, "PAPER_B2", "Paper_B2");
1160
        constantAlias(d, "PAPER_B3", "Paper_B3");
1161
        constantAlias(d, "PAPER_B4", "Paper_B4");
1162
        constantAlias(d, "PAPER_B5", "Paper_B5");
1163
        constantAlias(d, "PAPER_B6", "Paper_B6");
1164
        constantAlias(d, "PAPER_B7", "Paper_B7");
1165
        constantAlias(d, "PAPER_B8", "Paper_B8");
1166
        constantAlias(d, "PAPER_B9", "Paper_B9");
1167
        constantAlias(d, "PAPER_B10", "Paper_B10");
1168
        constantAlias(d, "PAPER_C5E", "Paper_C5E");
1169
        constantAlias(d, "PAPER_COMM10E", "Paper_Comm10E");
1170
        constantAlias(d, "PAPER_DLE", "Paper_DLE");
1171
        constantAlias(d, "PAPER_EXECUTIVE", "Paper_Executive");
1172
        constantAlias(d, "PAPER_FOLIO", "Paper_Folio");
1173
        constantAlias(d, "PAPER_LEDGER", "Paper_Ledger");
1174
        constantAlias(d, "PAPER_LEGAL", "Paper_Legal");
1175
        constantAlias(d, "PAPER_LETTER", "Paper_Letter");
1176
        constantAlias(d, "PAPER_TABLOID", "Paper_Tabloid");
733 subik 1177
        // end of deprecated cosntants
853 subik 1178
 
1179
        // Create the module-level docstring. This can be a proper unicode string, unlike
1180
        // the others, because we can just create a Unicode object and insert it in our
1181
        // module dictionary.
1182
        QString docstring = QObject::tr("Scribus Python interface module\n\
1345 subik 1183
\n\
1184
This module is the Python interface for Scribus. It provides functions\n\
1185
to control scribus and to manipulate objects on the canvas. Each\n\
1186
function is documented individually below.\n\
1187
\n\
1188
A few things are common across most of the interface.\n\
1189
\n\
1190
Most functions operate on frames. Frames are identified by their name,\n\
1191
a string - they are not real Python objects. Many functions take an\n\
1192
optional (non-keyword) parameter, a frame name.\n\
1193
Many exceptions are also common across most functions. These are\n\
1194
not currently documented in the docstring for each function.\n\
1195
- Many functions will raise a NoDocOpenError if you try to use them\
1196
without a document to operate on.\n\
1197
- If you do not pass a frame name to a function that requires one,\
1198
the function will use the currently selected frame, if any, or\
1199
raise a NoValidObjectError if it can't find anything to operate\
1200
on.\n\
1201
- Many functions will raise WrongFrameTypeError if you try to use them\n\
1202
on a frame type that they do not make sense with. For example, setting\n\
1203
the text colour on a graphics frame doesn't make sense, and will result\n\
1204
in this exception being raised.\n\
1205
- Errors resulting from calls to the underlying Python API will be\n\
1206
passed through unaltered. As such, the list of exceptions thrown by\n\
1207
any function as provided here and in its docstring is incomplete.\n\
1208
\n\
1209
Details of what exceptions each function may throw are provided on the\n\
1210
function's documentation.");
853 subik 1211
 
1212
        // Py_UNICODE is a typedef for unsigned short
934 subik 1213
        PyObject* uniDocStr = Py_BuildValue(const_cast<char*>("u"), (Py_UNICODE*)(docstring.ucs2()));
853 subik 1214
        if (uniDocStr == NULL)
1215
                qDebug("Failed to create module-level docstring object!");
1216
        else
934 subik 1217
                PyDict_SetItemString(d, const_cast<char*>("__doc__"), uniDocStr);
853 subik 1218
        Py_DECREF(uniDocStr);
82 Franz 1219
}
1220