View Issue Details

IDProjectCategoryView StatusLast Update
0010748ScribusScripterpublic2016-05-02 01:52
Reportermochouinard Assigned Tocbradney  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Target Version1.5.2Fixed in Version1.5.2.svn 
Summary0010748: Scripter: Get and Apply a Master Page
DescriptionAllow to Get and Apply a Master page from a script. I've seperated them in 2 different patch
TagsNo tags attached.
PatchYes

Relationships

has duplicate 0011490 closed Add commands like "getMasterPage() and setMasterPage()" 

Activities

mochouinard

2012-06-13 14:51

reporter  

Scribus_scripter_getMasterPage_api.diff (3,172 bytes)   
diff --git a/scribus/plugins/scriptplugin/cmdpage.cpp b/scribus/plugins/scriptplugin/cmdpage.cpp
index 52ad3f2..fa0eb8e 100644
--- a/scribus/plugins/scriptplugin/cmdpage.cpp
+++ b/scribus/plugins/scriptplugin/cmdpage.cpp
@@ -87,6 +87,22 @@ PyObject *scribus_deletepage(PyObject* /* self */, PyObject* args)
 	Py_RETURN_NONE;
 }
 
+PyObject *scribus_getmasterpage(PyObject* /* self */, PyObject* args)
+{
+	int e;
+	if (!PyArg_ParseTuple(args, "i", &e))
+		return NULL;
+	if(!checkHaveDocument())
+		return NULL;
+	e--;
+	if ((e < 0) || (e > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count())-1))
+	{
+		PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error").toLocal8Bit().constData());
+		return NULL;
+	}
+	return PyString_FromString(ScCore->primaryMainWindow()->doc->DocPages.at(e)->MPageNam.toUtf8());
+}
+
 PyObject *scribus_gotopage(PyObject* /* self */, PyObject* args)
 {
 	int e;
@@ -513,5 +529,6 @@ void cmdpagedocwarnings()
 	  << scribus_getVguides__doc__     << scribus_setVguides__doc__
 	  << scribus_pagedimension__doc__  << scribus_getpageitems__doc__
 	  << scribus_getpagemargins__doc__ << scribus_importpage__doc__
-	  << scribus_pagensize__doc__      << scribus_pagenmargins__doc__;
+	  << scribus_pagensize__doc__      << scribus_pagenmargins__doc__
+	  << scribus_getmasterpage__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdpage.h b/scribus/plugins/scriptplugin/cmdpage.h
index c3880d5..e6b0df5 100644
--- a/scribus/plugins/scriptplugin/cmdpage.h
+++ b/scribus/plugins/scriptplugin/cmdpage.h
@@ -80,6 +80,17 @@ May raise IndexError if the page number is out of range\n\
 PyObject *scribus_deletepage(PyObject * /*self*/, PyObject* args);
 
 /*! docstring */
+PyDoc_STRVAR(scribus_getmasterpage__doc__,
+QT_TR_NOOP("getMasterPage(nr)\n\
+\n\
+Get Master Page of the page \"nr\".\n\
+\n\
+May raise IndexError if the page number is out of range.\n\
+"));
+/*! Get Master Page Name */
+PyObject *scribus_getmasterpage(PyObject * /*self*/, PyObject* args);
+
+/*! docstring */
 PyDoc_STRVAR(scribus_gotopage__doc__,
 QT_TR_NOOP("gotoPage(nr)\n\
 \n\
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index c4233f5..4d59eac 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -366,6 +366,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("getUnit"), (PyCFunction)scribus_getunit, METH_NOARGS, tr(scribus_getunit__doc__)},
 	{const_cast<char*>("getVGuides"), (PyCFunction)scribus_getVguides, METH_NOARGS, tr(scribus_getVguides__doc__)},
 	{const_cast<char*>("getXFontNames"), (PyCFunction)scribus_xfontnames, METH_NOARGS, tr(scribus_xfontnames__doc__)},
+	{const_cast<char*>("getMasterPage"), scribus_getmasterpage, METH_VARARGS, tr(scribus_getmasterpage__doc__)},
 	{const_cast<char*>("gotoPage"), scribus_gotopage, METH_VARARGS, tr(scribus_gotopage__doc__)},
 	{const_cast<char*>("groupObjects"), scribus_groupobj, METH_VARARGS, tr(scribus_groupobj__doc__)},
 	{const_cast<char*>("haveDoc"), (PyCFunction)scribus_havedoc, METH_NOARGS, tr(scribus_havedoc__doc__)},

mochouinard

2012-06-13 14:51

reporter  

Scribus_scripter_applyMasterPage_api.diff (3,375 bytes)   
diff --git a/scribus/plugins/scriptplugin/cmdpage.cpp b/scribus/plugins/scriptplugin/cmdpage.cpp
index 52ad3f2..7cbec6f 100644
--- a/scribus/plugins/scriptplugin/cmdpage.cpp
+++ b/scribus/plugins/scriptplugin/cmdpage.cpp
@@ -68,6 +68,31 @@ PyObject *scribus_savepageeps(PyObject* /* self */, PyObject* args)
 	return PyBool_FromLong(static_cast<long>(true));
 }
 
+PyObject *scribus_applymasterpage(PyObject* /* self */, PyObject* args)
+{
+	int e;
+	char *name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "esi", "utf-8", &name, &e))
+		return NULL;
+	if(!checkHaveDocument())
+		return NULL;
+	if (!ScCore->primaryMainWindow()->doc->MasterNames.contains(name))
+	{
+		PyErr_SetString(PyExc_IndexError, QObject::tr("Given master page name does not match any existing.","python error").toLocal8Bit().constData());
+		return NULL;
+	}
+	e--;
+	if ((e < 0) || (e > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count())-1))
+	{
+		PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error").toLocal8Bit().constData());
+		return NULL;
+	}
+
+	ScCore->primaryMainWindow()->doc->applyMasterPage(name, e);
+
+	Py_RETURN_NONE;
+}
+
 PyObject *scribus_deletepage(PyObject* /* self */, PyObject* args)
 {
 	int e;
@@ -513,5 +538,6 @@ void cmdpagedocwarnings()
 	  << scribus_getVguides__doc__     << scribus_setVguides__doc__
 	  << scribus_pagedimension__doc__  << scribus_getpageitems__doc__
 	  << scribus_getpagemargins__doc__ << scribus_importpage__doc__
-	  << scribus_pagensize__doc__      << scribus_pagenmargins__doc__;
+	  << scribus_pagensize__doc__      << scribus_pagenmargins__doc__
+	  << scribus_applymasterpage__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdpage.h b/scribus/plugins/scriptplugin/cmdpage.h
index c3880d5..792eaaf 100644
--- a/scribus/plugins/scriptplugin/cmdpage.h
+++ b/scribus/plugins/scriptplugin/cmdpage.h
@@ -28,6 +28,15 @@ May raise IndexError if the page number is out of range\n\
 PyObject *scribus_newpage(PyObject * /*self*/, PyObject* args);
 
 /*! docstring */
+PyDoc_STRVAR(scribus_applymasterpage__doc__,
+QT_TR_NOOP("applyMasterPage(masterpage, page)\n\
+\n\
+Apply Master Page to current document\n\
+"));
+/*! apply Master Page */
+PyObject *scribus_applymasterpage(PyObject * /*self*/, PyObject* args);
+
+/*! docstring */
 PyDoc_STRVAR(scribus_actualpage__doc__,
 QT_TR_NOOP("currentPage() -> integer\n\
 \n\
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index c4233f5..8bea68c 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -281,6 +281,7 @@ char* tr(const char* docstringConstant)
 PyMethodDef scribus_methods[] = {
 	// 2004/10/03 pv - aliases with common Python syntax - ClassName methodName
 	// 2004-11-06 cr - move aliasing to dynamically generated wrapper functions, sort methoddef
+	{const_cast<char*>("applyMasterPage"), scribus_applymasterpage, METH_VARARGS, tr(scribus_applymasterpage__doc__)},
 	{const_cast<char*>("changeColor"), scribus_setcolor, METH_VARARGS, tr(scribus_setcolor__doc__)},
 	{const_cast<char*>("closeDoc"), (PyCFunction)scribus_closedoc, METH_NOARGS, tr(scribus_closedoc__doc__)},
 	{const_cast<char*>("closeMasterPage"), (PyCFunction)scribus_closemasterpage, METH_NOARGS, tr(scribus_closemasterpage__doc__)},

jurajF

2016-04-29 13:07

reporter   ~0040598

I have updated mochouinard's getMasterPage patch by moving function from cmdpage.cpp file into cmddoc.cpp where rest of function handling Master Pages are located. I did not change anything else in his patch.

apllyMasterPage fuction was added in issue 0012758

Issue 0011490 can be closed as duplicate of this one.

jurajF

2016-04-29 13:07

reporter  

0001-Add-getMasterPage-function-to-scripter.patch (4,774 bytes)   
From ca7b28f78f633064cf1592655fbb562b9d399b14 Mon Sep 17 00:00:00 2001
From: Juraj Fedel <wtxnh-scribus@yahoo.com.au>
Date: Fri, 29 Apr 2016 14:43:23 +0200
Subject: [PATCH] Add getMasterPage function to scripter

Updated patch from mochouinard by moving function from file
cmdpage.cpp into cmddoc.cpp (where rest of function dealing with Master
Pages are located.
---
 scribus/plugins/scriptplugin/cmddoc.cpp       | 18 +++++++++++++++++-
 scribus/plugins/scriptplugin/cmddoc.h         | 10 ++++++++++
 scribus/plugins/scriptplugin/scriptplugin.cpp |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/scribus/plugins/scriptplugin/cmddoc.cpp b/scribus/plugins/scriptplugin/cmddoc.cpp
index eb8bdf4..7870866 100644
--- a/scribus/plugins/scriptplugin/cmddoc.cpp
+++ b/scribus/plugins/scriptplugin/cmddoc.cpp
@@ -390,6 +390,22 @@ PyObject* scribus_deletemasterpage(PyObject* /* self */, PyObject* args)
 	Py_RETURN_NONE;
 }
 
+PyObject *scribus_getmasterpage(PyObject* /* self */, PyObject* args)
+{
+	int e;
+	if (!PyArg_ParseTuple(args, "i", &e))
+		return NULL;
+	if(!checkHaveDocument())
+		return NULL;
+	e--;
+	if ((e < 0) || (e > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count())-1))
+	{
+		PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error").toLocal8Bit().constData());
+		return NULL;
+	}
+	return PyString_FromString(ScCore->primaryMainWindow()->doc->DocPages.at(e)->MPageNam.toUtf8());
+}
+
 PyObject* scribus_applymasterpage(PyObject* /* self */, PyObject* args)
 {
 	char* name = 0;
@@ -426,5 +442,5 @@ PV */
 void cmddocdocwarnings()
 {
 	QStringList s;
-	s << scribus_newdocument__doc__ << scribus_newdoc__doc__ <<  scribus_closedoc__doc__ << scribus_havedoc__doc__ << scribus_opendoc__doc__ << scribus_savedoc__doc__ << scribus_getdocname__doc__ << scribus_savedocas__doc__ << scribus_setinfo__doc__ <<scribus_setmargins__doc__ <<scribus_setunit__doc__ <<scribus_getunit__doc__ <<scribus_loadstylesfromfile__doc__ <<scribus_setdoctype__doc__ <<scribus_closemasterpage__doc__ <<scribus_masterpagenames__doc__ <<scribus_editmasterpage__doc__ <<scribus_createmasterpage__doc__ <<scribus_deletemasterpage__doc__ << scribus_setbaseline__doc__  << scribus_applymasterpage__doc__;
+	s << scribus_newdocument__doc__ << scribus_newdoc__doc__ <<  scribus_closedoc__doc__ << scribus_havedoc__doc__ << scribus_opendoc__doc__ << scribus_savedoc__doc__ << scribus_getdocname__doc__ << scribus_savedocas__doc__ << scribus_setinfo__doc__ <<scribus_setmargins__doc__ <<scribus_setunit__doc__ <<scribus_getunit__doc__ <<scribus_loadstylesfromfile__doc__ <<scribus_setdoctype__doc__ <<scribus_closemasterpage__doc__ <<scribus_masterpagenames__doc__ <<scribus_editmasterpage__doc__ <<scribus_createmasterpage__doc__ <<scribus_deletemasterpage__doc__ << scribus_setbaseline__doc__ << scribus_getmasterpage__doc__  << scribus_applymasterpage__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmddoc.h b/scribus/plugins/scriptplugin/cmddoc.h
index 8a9695c..c384509 100644
--- a/scribus/plugins/scriptplugin/cmddoc.h
+++ b/scribus/plugins/scriptplugin/cmddoc.h
@@ -277,6 +277,16 @@ Delete the named master page.\n\
 "));
 PyObject* scribus_deletemasterpage(PyObject* self, PyObject* args);
 
+PyDoc_STRVAR(scribus_getmasterpage__doc__,
+QT_TR_NOOP("getMasterPage(nr)\n\
+\n\
+Get Master Page of the page \"nr\".\n\
+\n\
+May raise IndexError if the page number is out of range.\n\
+"));
+/*! Get Master Page Name */
+PyObject *scribus_getmasterpage(PyObject * /*self*/, PyObject* args);
+
 PyDoc_STRVAR(scribus_applymasterpage__doc__,
 QT_TR_NOOP("applyMasterPage(masterPageName, pageNumber)\n\
 \n\
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 25d0c61..0beecd1 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -377,6 +377,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("getLineSpacing"), scribus_getlinespace, METH_VARARGS, tr(scribus_getlinespace__doc__)},
 	{const_cast<char*>("getLineStyle"), scribus_getlinestyle, METH_VARARGS, tr(scribus_getlinestyle__doc__)},
 	{const_cast<char*>("getLineWidth"), scribus_getlinewidth, METH_VARARGS, tr(scribus_getlinewidth__doc__)},
+	{const_cast<char*>("getMasterPage"), scribus_getmasterpage, METH_VARARGS, tr(scribus_getmasterpage__doc__)},
 	{const_cast<char*>("getPageItems"), (PyCFunction)scribus_getpageitems, METH_NOARGS, tr(scribus_getpageitems__doc__)},
 	{const_cast<char*>("getPageMargins"), (PyCFunction)scribus_getpagemargins, METH_NOARGS, tr(scribus_getpagemargins__doc__)},
 	{const_cast<char*>("getPageType"), (PyCFunction)scribus_pageposition, METH_VARARGS, tr(scribus_pageposition__doc__)},
-- 
2.1.4

Kunda

2016-04-29 13:54

updater   ~0040601

Thanks Juraj!
I've closed 0011490

Kunda

2016-04-29 13:56

updater   ~0040602

unassigned jainbasil + added to 1.5.3 unless devs want to merge it earlier ?

Kunda

2016-04-29 17:05

updater   ~0040607

Hey Greg, can you test this ?

jurajF

2016-04-30 07:02

reporter  

masterpage.py (963 bytes)   
scribus.newDocument(scribus.PAPER_A4,  (15,15,  20, 20),  scribus.PORTRAIT, 1, scribus.UNIT_POINTS,  scribus.PAGE_2, 1, 8)

def create_mp(name):
    scribus.createMasterPage(name)
    scribus.editMasterPage(name)
    txt=scribus.createText(250, 70, 120,  30)
    scribus.setText(name, txt)
    scribus.closeMasterPage()
    return name

odd  = create_mp('Odd  Master Page')
even = create_mp('Even Master Page')

for p in range(8):
    page = p+1
    name = odd
    if page%2 == 0:
        name = even
    scribus.applyMasterPage(name, page)
    scribus.gotoPage(page)
    txt=scribus.createText(250, 170, 120,  30)
    scribus.setText("Page %i" % page, txt)

for p in range(8):
    print p+1, scribus.getMasterPage(p+1)

scribus.saveDocAs('masterpage.sla')

pdf = scribus.PDFfile()
pdf.save()


# scribus.applyMasterPage('Wrong Name', 1)
# scribus.applyMasterPage(even, 42)

# p = -3
# print scribus.getMasterPage(p+1)
# p = 43
# print scribus.getMasterPage(p+1)
masterpage.py (963 bytes)   

jurajF

2016-04-30 07:03

reporter   ~0040621

Added small test script that shows how to use getMasterPage() and applyMasterPage() function.

Kunda

2016-04-30 23:19

updater   ~0040658

William, would you have a moment to test this ?

william

2016-05-01 06:03

updater   ~0040662

It looks good to me. I built the current 1.5.2 SVN + 0001-Add-getMasterPage-function-to-scripter.patch and tested that masterpage.py worked and that the conditions in the commented lines were caught when uncommented.

Could scribus_getmasterpage() show the page number in the error like scribus_applymasterpage() does by changing line 403 of cmddoc.cpp from
  PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error").toLocal8Bit().constData());
to
  PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range: '%1'.","python error").arg(e+1).toLocal8Bit().constData());

cbradney

2016-05-01 14:38

administrator   ~0040676

Thanks! Committed.

Kunda

2016-05-01 14:52

updater   ~0040677

cbradney applied jurajF's patch in r21268
without william's 0010748:0040662 refinement

William, do you mind making a quick patch and submitting it?

william

2016-05-01 17:15

updater   ~0040687

Kunda, I think that cbradney added the line. Try
  svn log --diff -r 21268

Kunda

2016-05-02 01:52

updater   ~0040695

Oh, apologies. I didn't see that. Thanks!
Ok, closing ticket! Thanks to all involved in the process.

Issue History

Date Modified Username Field Change
2012-06-13 14:51 mochouinard New Issue
2012-06-13 14:51 mochouinard File Added: Scribus_scripter_getMasterPage_api.diff
2012-06-13 14:51 mochouinard File Added: Scribus_scripter_applyMasterPage_api.diff
2012-06-13 14:53 ale Assigned To => jainbasil
2012-06-13 14:53 ale Status new => assigned
2016-04-29 13:07 jurajF Note Added: 0040598
2016-04-29 13:07 jurajF File Added: 0001-Add-getMasterPage-function-to-scripter.patch
2016-04-29 13:52 Kunda Relationship added has duplicate 0011490
2016-04-29 13:54 Kunda Note Added: 0040601
2016-04-29 13:54 Kunda Assigned To jainbasil =>
2016-04-29 13:54 Kunda Status assigned => confirmed
2016-04-29 13:55 Kunda Patch => No
2016-04-29 13:55 Kunda Target Version => 1.5.3
2016-04-29 13:55 Kunda Summary Get and Apply a Master Page => Scripter: Get and Apply a Master Page
2016-04-29 13:55 Kunda Patch No => Yes
2016-04-29 13:56 Kunda Note Added: 0040602
2016-04-29 17:05 Kunda Note Added: 0040607
2016-04-30 07:02 jurajF File Added: masterpage.py
2016-04-30 07:03 jurajF Note Added: 0040621
2016-04-30 23:19 Kunda Note Added: 0040658
2016-05-01 06:03 william Note Added: 0040662
2016-05-01 14:30 cbradney Fixed in Version => 1.5.2.svn
2016-05-01 14:30 cbradney Target Version 1.5.3 => 1.5.2
2016-05-01 14:31 cbradney Status confirmed => resolved
2016-05-01 14:31 cbradney Resolution open => fixed
2016-05-01 14:31 cbradney Assigned To => cbradney
2016-05-01 14:38 cbradney Note Added: 0040676
2016-05-01 14:52 Kunda Note Added: 0040677
2016-05-01 17:15 william Note Added: 0040687
2016-05-02 01:52 Kunda Note Added: 0040695
2016-05-02 01:52 Kunda Status resolved => closed