View Issue Details

IDProjectCategoryView StatusLast Update
0015483ScribusScripterpublic2019-06-04 20:56
Reporterdockattt Assigned Tojghali  
PrioritynormalSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Product Version1.5.4.svn 
Fixed in Version1.5.5.svn 
Summary0015483: add setJSActionScript and getJSActionScript python functions
DescriptionIn using Scribus I've had several occasions where I had to make multiple pdf buttons(perhaps up to 20) in a document. These needed JavaScript actions added to them. All of them used the same javascript function calls. The difference being that they passed different parameters into the functions. If I could have automated the populating of their javascript actions this would have saved me time.

The attached patch adds 2 python functions that would allowed me to do this: setJSActionScript and getJSActionScript.
I used JS as an abbreviation for JavaScript. The setJSActionScript allows the user to set the JavaScript on a particular pdf widget with 2 parameters. The type(0-9) and the script that will be associated with that type. The getJSActionScript returns the JavaScript for an action on a annotation.
Steps To Reproduceuse this python:

setJSActionScript(0, "print 'test'");
#this populates the annotation's action with the script
getJSActionScript(0)
#this returns the script that was just populated.
TagsNo tags attached.
PatchYes

Activities

dockattt

2018-11-13 20:30

reporter   ~0045615

forgot to add a null pointer check in the code. Added that in, so that's why its the V2 of the patch.
getsetJSActionScript.patch_v2 (7,814 bytes)   
Index: Scribus/scribus/plugins/scriptplugin/cmdannotations.cpp
===================================================================
--- Scribus/scribus/plugins/scriptplugin/cmdannotations.cpp	(revision 22671)
+++ Scribus/scribus/plugins/scriptplugin/cmdannotations.cpp	(working copy)
@@ -16,6 +16,169 @@
 static bool testPageItem(PageItem *i);
 static void add_text_to_dict(PyObject *drv, PageItem *i);
 
+
+PyObject *scribus_setjsactionscript(PyObject * /*self*/, PyObject* args){
+
+
+	/*
+			Java_ReleaseButton	= 0,
+			Java_PressButton	= 1,
+			Java_EnterWidget	= 2,
+			Java_LeaveWidget	= 3,
+			Java_FocusIn		= 4,
+			Java_FocusOut		= 5,
+			Java_SelectionChg	= 6,
+			Java_FieldFormat	= 7,
+			Java_FieldValidate	= 8,
+			Java_FieldCalculate	= 9 */
+
+	int action;
+	char *script = const_cast<char*>("");
+	char *name = const_cast<char*>("");
+
+	if (!PyArg_ParseTuple(args, "is|es",&action,&script,"utf-8", &name))
+		return nullptr;
+
+	if (action < 0 || action > 9){
+		QString qnum = QString("%1").arg(action);
+		PyErr_SetString(PyExc_RuntimeError,
+			QObject::tr("Action must be be 0-9 " + qnum.toUtf8(), "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+
+	if (!checkHaveDocument())
+		return nullptr;
+
+
+	PageItem *i = GetUniqueItem(QString::fromUtf8(name));
+	if (i == nullptr)
+		return nullptr;
+
+	if(i->isAnnotation() == false){
+		PyErr_SetString(PyExc_RuntimeError,
+			QObject::tr("Page item must be an annotation", "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+        Annotation &annotation = i->annotation();
+	annotation.setActionType(Annotation::Action_JavaScript);
+	QString javascript = QString::fromUtf8(script); 
+
+
+	switch (action)
+	{
+	case Annotation::Java_ReleaseButton:
+		annotation.setAction(javascript);
+		break;
+	case Annotation::Java_PressButton:
+		annotation.setD_act(javascript);
+		break;
+	case Annotation::Java_EnterWidget:
+		annotation.setE_act(javascript);
+		break;
+	case Annotation::Java_LeaveWidget:
+		annotation.setX_act(javascript);
+		break;
+	case Annotation::Java_FocusIn:
+		annotation.setFo_act(javascript);
+		break;
+	case Annotation::Java_FocusOut:
+		annotation.setBl_act(javascript);
+		break;
+	case Annotation::Java_SelectionChg:
+		annotation.setK_act(javascript);
+		break;
+	case Annotation::Java_FieldFormat:
+		annotation.setF_act(javascript);
+		break;
+	case Annotation::Java_FieldValidate:
+		annotation.setV_act(javascript);
+		break;
+	case Annotation::Java_FieldCalculate:
+		annotation.setC_act(javascript);
+		break;
+	}
+
+	Py_RETURN_NONE;
+
+}
+
+PyObject *scribus_getjsactionscript(PyObject * /*self*/, PyObject* args){
+
+	int action;
+	char *name = const_cast<char*>("");
+
+	if (!PyArg_ParseTuple(args, "i|es",&action,"utf-8", &name))
+		return nullptr;
+
+	if (action < 0 || action > 9){
+		QString qnum = QString("%1").arg(action);
+		PyErr_SetString(PyExc_RuntimeError,
+			QObject::tr("Action must be be 0-9 " + qnum.toUtf8(), "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+
+	if (!checkHaveDocument())
+		return nullptr;
+
+
+	PageItem *i = GetUniqueItem(QString::fromUtf8(name));
+	if (i == nullptr)
+		return nullptr;
+
+	if(i->isAnnotation() == false){
+		PyErr_SetString(PyExc_RuntimeError,
+			QObject::tr("Page item must be an annotation", "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+        Annotation &annotation = i->annotation();
+	if(annotation.ActionType() != Annotation::Action_JavaScript){
+		Py_RETURN_NONE;
+	}
+
+	QString rv;
+	switch (action)
+	{
+	case Annotation::Java_ReleaseButton:
+		rv = annotation.Action();
+		break;
+	case Annotation::Java_PressButton:
+		rv = annotation.D_act();
+		break;
+	case Annotation::Java_EnterWidget:
+		rv = annotation.E_act();
+		break;
+	case Annotation::Java_LeaveWidget:
+		rv = annotation.X_act();
+		break;
+	case Annotation::Java_FocusIn:
+		rv = annotation.Fo_act();
+		break;
+	case Annotation::Java_FocusOut:
+		rv = annotation.Bl_act();
+		break;
+	case Annotation::Java_SelectionChg:
+		rv = annotation.K_act();
+		break;
+	case Annotation::Java_FieldFormat:
+		rv = annotation.F_act();
+		break;
+	case Annotation::Java_FieldValidate:
+		rv = annotation.V_act();
+		break;
+	case Annotation::Java_FieldCalculate:
+		rv = annotation.C_act();
+		break;
+	}
+	PyObject *rstr = PyString_FromString(rv.toUtf8());
+	return rstr;
+}
+
+
+
 PyObject *scribus_isannotated(PyObject * /*self*/, PyObject* args, PyObject *keywds)
 {
 	char *name = const_cast<char*>("");  
@@ -446,7 +609,9 @@
       << scribus_setfileannotation__doc__
       << scribus_seturiannotation__doc__
       << scribus_settextannotation__doc__
-      << scribus_createpdfannotation__doc__;
+      << scribus_createpdfannotation__doc__
+      << scribus_setjsactionscript__doc__
+      << scribus_getjsactionscript__doc__;
 }
 
 
Index: Scribus/scribus/plugins/scriptplugin/cmdannotations.h
===================================================================
--- Scribus/scribus/plugins/scriptplugin/cmdannotations.h	(revision 22671)
+++ Scribus/scribus/plugins/scriptplugin/cmdannotations.h	(working copy)
@@ -157,4 +157,43 @@
 /*!creates a pdf annotation and a text frame.*/
 PyObject *scribus_createpdfannotation(PyObject * /*self*/, PyObject* args);
 
+/*! docstring */
+PyDoc_STRVAR(scribus_setjsactionscript__doc__,
+ QT_TR_NOOP("setJSActionScript(which,script,[\"name\"])\n\
+\n\
+Sets the JavaScript action for a particular event\n\
+\"which\" is one of the following:\n\
+(0 Mouse Up, 1 Mouse Down, 2 Mouse Enter,\n\
+3 Mouse Exit, 4 Focus In, 5 Focus Out,\n\
+6 Selection Change, 7 Field Format,\n\
+8 Field Validate, 9 Field Calculate)\n\
+\"script\" is the JavaScript set to the action.\n\
+\"name\" uses the currently selected item if not given.\n\
+Also sets the annotation's action to JavaScript.\n\
+Page item must be an annotation or an error will be raised\n\
+Returns:\n\
+None\n\
+"));
+/*!sets the JavaScript on an annotation action.*/
+PyObject *scribus_setjsactionscript(PyObject * /*self*/, PyObject* args);
+
+/*! docstring */
+PyDoc_STRVAR(scribus_getjsactionscript__doc__,
+ QT_TR_NOOP("getJSActionScript(which,[\"name\"])\n\
+\n\
+Gets the JavaScript action for a particular event\n\
+\"which\" is one of the following:\n\
+(0 Mouse Up, 1 Mouse Down, 2 Mouse Enter,\n\
+3 Mouse Exit, 4 Focus In, 5 Focus Out,\n\
+6 Selection Change, 7 Field Format,\n\
+8 Field Validate, 9 Field Calculate)\n\
+\"name\" uses the currently selected item if not given.\n\
+Page item must be an annotation or an error will be raised\n\
+Returns:\n\
+None if action is not JavaScript. String/Script is returned\n\
+if it is a JavaScript action.\n\
+"));
+/*!gets the JavaScript for an annotation action.*/
+PyObject *scribus_getjsactionscript(PyObject * /*self*/, PyObject* args);
+
 #endif
Index: Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
===================================================================
--- Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp	(revision 22671)
+++ Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp	(working copy)
@@ -593,6 +593,8 @@
 	{const_cast<char*>("setTextAnnotation"), scribus_settextannotation, METH_VARARGS,tr(scribus_settextannotation__doc__)},
 	{const_cast<char*>("createPdfAnnotation"), scribus_createpdfannotation, METH_VARARGS,tr(scribus_createpdfannotation__doc__)},
 	{const_cast<char*>("isAnnotated"),(PyCFunction)scribus_isannotated, METH_VARARGS|METH_KEYWORDS,tr(scribus_isannotated__doc__)},
+	{const_cast<char*>("setJSActionScript"), scribus_setjsactionscript, METH_VARARGS,tr(scribus_setjsactionscript__doc__)},
+	{const_cast<char*>("getJSActionScript"), scribus_getjsactionscript, METH_VARARGS,tr(scribus_getjsactionscript__doc__)},
 	{nullptr, (PyCFunction)(nullptr), 0, nullptr} /* sentinel */
 };
 
getsetJSActionScript.patch_v2 (7,814 bytes)   

jghali

2018-11-18 14:58

administrator   ~0045636

Applied, thanks!

Issue History

Date Modified Username Field Change
2018-11-13 20:18 dockattt New Issue
2018-11-13 20:18 dockattt File Added: getsetJSActionScript.patch
2018-11-13 20:29 dockattt File Deleted: getsetJSActionScript.patch
2018-11-13 20:30 dockattt File Added: getsetJSActionScript.patch_v2
2018-11-13 20:30 dockattt Note Added: 0045615
2018-11-18 14:58 jghali Assigned To => jghali
2018-11-18 14:58 jghali Status new => resolved
2018-11-18 14:58 jghali Resolution open => fixed
2018-11-18 14:58 jghali Fixed in Version => 1.5.5.svn
2018-11-18 14:58 jghali Note Added: 0045636
2019-06-04 20:56 cbradney Status resolved => closed