View Issue Details

IDProjectCategoryView StatusLast Update
0017154ScribusScripterpublic2024-09-16 20:54
ReporterZinal Assigned Tojghali  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Product Version1.7.0.svn 
Fixed in Version1.7.0.svn 
Summary0017154: Allow setting gradient vector from scripts
DescriptionWe are able to set a gradient fill and add gradient stops through code (console) but there is currently no method to set the vectors, focal point, scale nor skew.

It would be quite easy to create a method to at least set the start/end vectors.. Something like this:
PyObject *scribus_setgradvectors(PyObject* /* self */, PyObject* args)
{
    char* Name = const_cast<char*>("");
    double startX, startY, endX, endY;
    if (!PyArg_ParseTuple(args, "dddd|es", &startX, &startY, &endX, &endY, "utf-8", &Name))
        return nullptr;
    if (!checkHaveDocument())
        return nullptr;

    PageItem* currItem = GetUniqueItem(QString::fromUtf8(Name));
    if (currItem == nullptr)
        return nullptr;

    currItem->setGradientStart(ValueToPoint(startX), ValueToPoint(startY));
    currItem->setGradientEnd(ValueToPoint(endX), ValueToPoint(endY));
    currItem->update();
    Py_RETURN_NONE;
}
Tagsfeature request, gradients, python, scripter
PatchNo

Activities

Zinal

2024-02-06 09:12

reporter   ~0050966

Works like a charm for me at least! I attached a .diff file which I hope is correct (Github user here.. Never used svn before...)
17154-gradient_vectors.diff (3,010 bytes)   
Index: scribus/plugins/scriptplugin/cmdsetprop.cpp
===================================================================
--- scribus/plugins/scriptplugin/cmdsetprop.cpp	(revision 26024)
+++ scribus/plugins/scriptplugin/cmdsetprop.cpp	(working copy)
@@ -9,6 +9,25 @@
 #include "scribuscore.h"
 #include "scribusdoc.h"
 
+PyObject* scribus_setgradvectors(PyObject* /* self */, PyObject* args)
+{
+	char* Name = const_cast<char*>("");
+	double startX, startY, endX, endY;
+	if (!PyArg_ParseTuple(args, "dddd|es", &startX, &startY, &endX, &endY, "utf-8", &Name))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+
+	PageItem* currItem = GetUniqueItem(QString::fromUtf8(Name));
+	if (currItem == nullptr)
+		return nullptr;
+
+	currItem->setGradientStart(ValueToPoint(startX), ValueToPoint(startY));
+	currItem->setGradientEnd(ValueToPoint(endX), ValueToPoint(endY));
+	currItem->update();
+	Py_RETURN_NONE;
+}
+
 PyObject *scribus_setgradfill(PyObject* /* self */, PyObject* args)
 {
 	char *Name = const_cast<char*>("");
Index: scribus/plugins/scriptplugin/cmdsetprop.h
===================================================================
--- scribus/plugins/scriptplugin/cmdsetprop.h	(revision 26024)
+++ scribus/plugins/scriptplugin/cmdsetprop.h	(working copy)
@@ -13,6 +13,17 @@
 /** Setting Object Properties */
 
 /*! docstring */
+PyDoc_STRVAR(scribus_setgradvectors__doc__,
+	QT_TR_NOOP("setGradientVectors(startX, startY, endX, endY, [\"name\"])\n\
+\n\
+Sets the gradient vectors of the object \"name\" to (startX, startY) -> (endX, endY).\n\
+The coordinates are given in the current measurement units of the document\n\
+(see UNIT constants).\n\
+"));
+/*! Set gradient vector */
+PyObject* scribus_setgradvectors(PyObject* /*self*/, PyObject* args);
+
+/*! docstring */
 PyDoc_STRVAR(scribus_setgradfill__doc__,
 QT_TR_NOOP("setGradientFill(type, \"color1\", shade1, \"color2\", shade2, [\"name\"])\n\
 \n\
Index: scribus/plugins/scriptplugin/scriptplugin.cpp
===================================================================
--- scribus/plugins/scriptplugin/scriptplugin.cpp	(revision 26024)
+++ scribus/plugins/scriptplugin/scriptplugin.cpp	(working copy)
@@ -533,6 +533,7 @@
 	{const_cast<char*>("setFontSize"), scribus_setfontsize, METH_VARARGS, tr(scribus_setfontsize__doc__)},
 	{const_cast<char*>("setGradientFill"), scribus_setgradfill, METH_VARARGS, tr(scribus_setgradfill__doc__)},
 	{const_cast<char*>("setGradientStop"), scribus_setgradstop, METH_VARARGS, tr(scribus_setgradstop__doc__)},
+	{const_cast<char*>("setGradientVectors"), scribus_setgradvectors, METH_VARARGS, tr(scribus_setgradvectors__doc__)},
 	{const_cast<char*>("setHGuides"), scribus_setHguides, METH_VARARGS, tr(scribus_setHguides__doc__)},
 	{const_cast<char*>("setImageBrightness"), scribus_setimagebrightness, METH_VARARGS, tr(scribus_setimagebrightness__doc__)},
 	{const_cast<char*>("setImageGrayscale"), scribus_setimagegrayscale, METH_VARARGS, tr(scribus_setimagegrayscale__doc__)},
17154-gradient_vectors.diff (3,010 bytes)   

ale

2024-02-06 11:29

manager   ~0050967

i did not try it but at first sight it looks ok...

just probably missing a small detail:

+ << scribus_setgradvectors__doc__

at the end of scribus/plugins/scriptplugin/cmdsetprop.cpp

Zinal

2024-02-07 01:27

reporter   ~0050971

Forgot about that. I fixed it. I've also added two other methods: getGradientStopsCount(["name"]) -> integer and getGradientStop(index, ["name"]) -> ("color", opacity, shade) for easier manipulation of gradients.
17154-gradient_methods.diff (8,485 bytes)   
Index: scribus/plugins/scriptplugin/cmdgetprop.cpp
===================================================================
--- scribus/plugins/scriptplugin/cmdgetprop.cpp	(revision 26024)
+++ scribus/plugins/scriptplugin/cmdgetprop.cpp	(working copy)
@@ -12,6 +12,58 @@
 #include "scribuscore.h"
 #include "scribusdoc.h"
 
+PyObject* scribus_getgradstop(PyObject* /* self */, PyObject* args)
+{
+	char* Name = const_cast<char*>("");
+	int stopIndex = 0;
+	if (!PyArg_ParseTuple(args, "i|es", &stopIndex, "utf-8", &Name))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+	PageItem* item = GetUniqueItem(QString::fromUtf8(Name));
+	if (item == nullptr)
+		return nullptr;
+
+	int gradStopsCount = item->fill_gradient.stops();
+
+	if (stopIndex < 0 || stopIndex >= gradStopsCount)
+	{
+		PyErr_SetString(PyExc_ValueError, QObject::tr("Stop index out of bounds, must be 0 <= index <= stopsCount.", "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	VColorStop* stop = item->fill_gradient.colorStops()[stopIndex];
+	const char* stopName = stop->name.toUtf8().constData();
+
+	return Py_BuildValue("(sdi)", stopName, stop->opacity, stop->shade);
+}
+
+PyObject* scribus_getgradstopscount(PyObject* /* self */, PyObject* args)
+{
+	char* Name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", &Name))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+	PageItem* item = GetUniqueItem(QString::fromUtf8(Name));
+	if (item == nullptr)
+		return nullptr;
+	return PyLong_FromLong(static_cast<long>(item->fill_gradient.stops()));
+}
+
+PyObject* scribus_getgradvectors(PyObject* /* self */, PyObject* args)
+{
+	char* Name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", &Name))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+	PageItem* item = GetUniqueItem(QString::fromUtf8(Name));
+	if (item == nullptr)
+		return nullptr;
+	return Py_BuildValue("(ffff)", PointToValue(item->GrStartX), PointToValue(item->GrStartY), PointToValue(item->GrEndX), PointToValue(item->GrEndY));
+}
+
 /* getObjectType(name) */
 PyObject *scribus_getobjecttype(PyObject* /* self */, PyObject* args)
 {
@@ -475,6 +527,9 @@
 	  << scribus_getfillblendmode__doc__
 	  << scribus_getfillshade__doc__ 
 	  << scribus_getfilltransparency__doc__
+	  << scribus_getgradstop__doc__ 
+	  << scribus_getgradstopscount__doc__ 
+	  << scribus_getgradvectors__doc__ 
 	  << scribus_getimagecolorspace__doc__
 	  << scribus_getimagefile__doc__
 	  << scribus_getimageoffset__doc__
Index: scribus/plugins/scriptplugin/cmdgetprop.h
===================================================================
--- scribus/plugins/scriptplugin/cmdgetprop.h	(revision 26024)
+++ scribus/plugins/scriptplugin/cmdgetprop.h	(working copy)
@@ -12,6 +12,30 @@
 
 /** Query-Functions */
 
+PyDoc_STRVAR(scribus_getgradstop__doc__,
+	QT_TR_NOOP("getGradientStop(index, [\"name\"]) -> (\"color\", opacity, shade)\n\
+\n\
+Returns a (\"color\", opacity, shade) tuple containing the stop at index on the gradient of the object\n\
+\"name\". If \"name\" is not given the currently selected item is used.\n\
+"));
+PyObject* scribus_getgradstop(PyObject* /*self*/, PyObject* args);
+
+PyDoc_STRVAR(scribus_getgradstopscount__doc__,
+	QT_TR_NOOP("getGradientStopsCount([\"name\"]) -> integer\n\
+\n\
+Returns the number of stops on the gradient of the object\n\
+\"name\". If \"name\" is not given the currently selected item is used.\n\
+"));
+PyObject* scribus_getgradstopscount(PyObject* /*self*/, PyObject* args);
+
+PyDoc_STRVAR(scribus_getgradvectors__doc__,
+QT_TR_NOOP("getGradientVectors([\"name\"]) -> (startX, startY, endX, endY)\n\
+\n\
+Returns a (startX, startY, endX, endY) tuple containing the gradient vector of the object\n\
+\"name\". If \"name\" is not given the currently selected item is used.\n\
+"));
+PyObject* scribus_getgradvectors(PyObject* /*self*/, PyObject* args);
+
 /*! docstring */
 PyDoc_STRVAR(scribus_getobjecttype__doc__,
 QT_TR_NOOP("getObjectType([\"name\"]) -> string\n\
Index: scribus/plugins/scriptplugin/cmdsetprop.cpp
===================================================================
--- scribus/plugins/scriptplugin/cmdsetprop.cpp	(revision 26024)
+++ scribus/plugins/scriptplugin/cmdsetprop.cpp	(working copy)
@@ -9,6 +9,25 @@
 #include "scribuscore.h"
 #include "scribusdoc.h"
 
+PyObject* scribus_setgradvectors(PyObject* /* self */, PyObject* args)
+{
+	char* Name = const_cast<char*>("");
+	double startX, startY, endX, endY;
+	if (!PyArg_ParseTuple(args, "dddd|es", &startX, &startY, &endX, &endY, "utf-8", &Name))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+
+	PageItem* currItem = GetUniqueItem(QString::fromUtf8(Name));
+	if (currItem == nullptr)
+		return nullptr;
+
+	currItem->setGradientStart(ValueToPoint(startX), ValueToPoint(startY));
+	currItem->setGradientEnd(ValueToPoint(endX), ValueToPoint(endY));
+	currItem->update();
+	Py_RETURN_NONE;
+}
+
 PyObject *scribus_setgradfill(PyObject* /* self */, PyObject* args)
 {
 	char *Name = const_cast<char*>("");
@@ -552,6 +571,7 @@
 	  << scribus_setfilltrans__doc__
 	  << scribus_setgradfill__doc__ 
 	  << scribus_setgradstop__doc__
+	  << scribus_setgradvectors__doc__ 
 	  << scribus_setitemname__doc__
 	  << scribus_setlineblend__doc__
 	  << scribus_setlinecap__doc__ 
Index: scribus/plugins/scriptplugin/cmdsetprop.h
===================================================================
--- scribus/plugins/scriptplugin/cmdsetprop.h	(revision 26024)
+++ scribus/plugins/scriptplugin/cmdsetprop.h	(working copy)
@@ -13,6 +13,17 @@
 /** Setting Object Properties */
 
 /*! docstring */
+PyDoc_STRVAR(scribus_setgradvectors__doc__,
+	QT_TR_NOOP("setGradientVectors(startX, startY, endX, endY, [\"name\"])\n\
+\n\
+Sets the gradient vectors of the object \"name\" to (startX, startY) -> (endX, endY).\n\
+The coordinates are given in the current measurement units of the document\n\
+(see UNIT constants).\n\ If \"name\" is not given the currently selected item is used.\n\
+"));
+/*! Set gradient vector */
+PyObject* scribus_setgradvectors(PyObject* /*self*/, PyObject* args);
+
+/*! docstring */
 PyDoc_STRVAR(scribus_setgradfill__doc__,
 QT_TR_NOOP("setGradientFill(type, \"color1\", shade1, \"color2\", shade2, [\"name\"])\n\
 \n\
Index: scribus/plugins/scriptplugin/scriptplugin.cpp
===================================================================
--- scribus/plugins/scriptplugin/scriptplugin.cpp	(revision 26024)
+++ scribus/plugins/scriptplugin/scriptplugin.cpp	(working copy)
@@ -364,6 +364,9 @@
 	{const_cast<char*>("getFontNames"), (PyCFunction)scribus_getfontnames, METH_NOARGS, tr(scribus_getfontnames__doc__)},
 	{const_cast<char*>("getFontSize"), scribus_getfontsize, METH_VARARGS, tr(scribus_getfontsize__doc__)},
 	{const_cast<char*>("getFrameText"), scribus_getframetext, METH_VARARGS, tr(scribus_getframetext__doc__)},
+	{const_cast<char*>("getGradientStop"), scribus_getgradstop, METH_VARARGS, tr(scribus_getgradstop__doc__)},
+	{const_cast<char*>("getGradientStopsCount"), scribus_getgradstopscount, METH_VARARGS, tr(scribus_getgradstopscount__doc__)},
+	{const_cast<char*>("getGradientVectors"), scribus_getgradvectors, METH_VARARGS, tr(scribus_getgradvectors__doc__)},
 	{const_cast<char*>("getGroupItems"), (PyCFunction)scribus_getGroupItems, METH_VARARGS|METH_KEYWORDS, tr(scribus_getGroupItems__doc__)},
 	{const_cast<char*>("getGuiLanguage"), (PyCFunction)scribus_getlanguage, METH_NOARGS, tr(scribus_getlanguage__doc__)},
 	{const_cast<char*>("getHGuides"), (PyCFunction)scribus_getHguides, METH_NOARGS, tr(scribus_getHguides__doc__)},
@@ -533,6 +536,7 @@
 	{const_cast<char*>("setFontSize"), scribus_setfontsize, METH_VARARGS, tr(scribus_setfontsize__doc__)},
 	{const_cast<char*>("setGradientFill"), scribus_setgradfill, METH_VARARGS, tr(scribus_setgradfill__doc__)},
 	{const_cast<char*>("setGradientStop"), scribus_setgradstop, METH_VARARGS, tr(scribus_setgradstop__doc__)},
+	{const_cast<char*>("setGradientVectors"), scribus_setgradvectors, METH_VARARGS, tr(scribus_setgradvectors__doc__)},
 	{const_cast<char*>("setHGuides"), scribus_setHguides, METH_VARARGS, tr(scribus_setHguides__doc__)},
 	{const_cast<char*>("setImageBrightness"), scribus_setimagebrightness, METH_VARARGS, tr(scribus_setimagebrightness__doc__)},
 	{const_cast<char*>("setImageGrayscale"), scribus_setimagegrayscale, METH_VARARGS, tr(scribus_setimagegrayscale__doc__)},
17154-gradient_methods.diff (8,485 bytes)   

jghali

2024-06-01 20:33

administrator   ~0051184

Thanks! I applied your patch with some changes and cleanups: the getGradientVectors() function was renamed to getGradientVector(). Similarly the setGradientVectors() function was renamed setGradientVector(). There is indeed only one gradient vector going from (startX, startY) to (endX, endY).

Issue History

Date Modified Username Field Change
2024-02-06 08:47 Zinal New Issue
2024-02-06 08:47 Zinal Tag Attached: feature request
2024-02-06 08:47 Zinal Tag Attached: gradients
2024-02-06 08:47 Zinal Tag Attached: python
2024-02-06 08:47 Zinal Tag Attached: scripter
2024-02-06 09:12 Zinal Note Added: 0050966
2024-02-06 09:12 Zinal File Added: 17154-gradient_vectors.diff
2024-02-06 11:29 ale Note Added: 0050967
2024-02-07 01:27 Zinal Note Added: 0050971
2024-02-07 01:27 Zinal File Added: 17154-gradient_methods.diff
2024-06-01 20:12 jghali Summary Allow setting Gradient Vectors from code/console => Allow setting Gradient Vectors from scripter
2024-06-01 20:12 jghali Summary Allow setting Gradient Vectors from scripter => Allow setting Gradient Vectors from scripts
2024-06-01 20:24 jghali Summary Allow setting Gradient Vectors from scripts => Allow setting gradient vector from scripts
2024-06-01 20:33 jghali Assigned To => jghali
2024-06-01 20:33 jghali Status new => resolved
2024-06-01 20:33 jghali Resolution open => fixed
2024-06-01 20:33 jghali Fixed in Version => 1.7.0.svn
2024-06-01 20:33 jghali Note Added: 0051184
2024-09-16 20:54 cbradney Status resolved => closed