From 29405f762dc371ad45b48570fb01aa1227991b0c Mon Sep 17 00:00:00 2001
From: Juraj Fedel <wtxnh-scribus@yahoo.com.au>
Date: Sat, 21 Oct 2017 11:18:01 +0200
Subject: [PATCH 2/2] Create custom line style python command

---
 scribus/plugins/scriptplugin/cmdstyle.cpp     | 67 +++++++++++++++++++++++++++
 scribus/plugins/scriptplugin/cmdstyle.h       | 19 ++++++++
 scribus/plugins/scriptplugin/scriptplugin.cpp |  1 +
 3 files changed, 87 insertions(+)

diff --git a/scribus/plugins/scriptplugin/cmdstyle.cpp b/scribus/plugins/scriptplugin/cmdstyle.cpp
index c5b16a026..ad599ea86 100644
--- a/scribus/plugins/scriptplugin/cmdstyle.cpp
+++ b/scribus/plugins/scriptplugin/cmdstyle.cpp
@@ -170,6 +170,72 @@ PyObject *scribus_createcharstyle(PyObject* /* self */, PyObject* args, PyObject
 	Py_RETURN_NONE;
 }
 
+PyObject *scribus_createcustomlinestyle(PyObject * /* self */, PyObject* args)
+{
+	char *Name = const_cast<char*>("");
+	PyObject *obj;
+
+	if (!PyArg_ParseTuple(args, "esO", "utf-8", &Name, &obj))
+		return NULL;
+
+	if (!PyList_Check(obj)) {
+		PyErr_SetString(PyExc_TypeError, "'style' must be list.");
+		return NULL;
+	}
+
+	multiLine ml;
+	for (int i = 0; i < PyList_Size(obj); i++) {
+		PyObject *line = PyList_GetItem(obj, i);
+		if (!PyDict_Check(line)) {
+			PyErr_SetString(PyExc_TypeError, "elements of list must be Dictionary.");
+			return NULL;
+		}
+		struct SingleLine sl;
+		PyObject *val;
+		val = PyDict_GetItemString(line, "Color");
+		if (val) {
+			sl.Color = PyString_AsString(val);
+		} else 
+			sl.Color = ScCore->primaryMainWindow()->doc->itemToolPrefs().lineColor;;
+		val = PyDict_GetItemString(line, "Dash");
+		if (val) {
+			sl.Dash = PyInt_AsLong(val);
+		} else 
+			sl.Dash = Qt::SolidLine;
+		val = PyDict_GetItemString(line, "LineEnd");
+		if (val) {
+			sl.LineEnd = PyInt_AsLong(val);
+		} else 
+			sl.LineEnd = Qt::FlatCap;
+		val = PyDict_GetItemString(line, "LineJoin");
+		if (val) {
+			sl.LineJoin = PyInt_AsLong(val);
+		} else 
+			sl.LineJoin = Qt::MiterJoin;
+		val = PyDict_GetItemString(line, "Shade");
+		if (val) {
+			sl.Shade = PyInt_AsLong(val);
+		} else 
+			sl.Shade = ScCore->primaryMainWindow()->doc->itemToolPrefs().lineColorShade;
+		val = PyDict_GetItemString(line, "Width");
+		if (val) {
+			sl.Width = PyFloat_AsDouble(val);
+		} else 
+			sl.Width = ScCore->primaryMainWindow()->doc->itemToolPrefs().lineWidth;
+
+		val = PyDict_GetItemString(line, "Shortcut");
+		if (val) {
+			ml.shortcut = PyString_AsString(val);
+		} else 
+			ml.shortcut = "";
+		ml.push_back(sl);
+	}
+	if (ml.size() > 0)
+		ScCore->primaryMainWindow()->doc->MLineStyles[Name] = ml;
+	Py_RETURN_NONE;
+}
+
+
 /*! HACK: this removes "warning: 'blah' defined but not used" compiler warnings
 with header files structure untouched (docstrings are kept near declarations)
 PV */
@@ -177,4 +243,5 @@ void cmdstyledocwarnings()
 {
 	QStringList s;
 	s << scribus_createparagraphstyle__doc__ << scribus_createcharstyle__doc__;
+	s << scribus_createcustomlinestyle__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdstyle.h b/scribus/plugins/scriptplugin/cmdstyle.h
index 127988485..d2ae6e575 100644
--- a/scribus/plugins/scriptplugin/cmdstyle.h
+++ b/scribus/plugins/scriptplugin/cmdstyle.h
@@ -82,5 +82,24 @@ tracking [optional] -> tracking of the text\n\n\
   		Special thanks go to avox for helping me! */
 PyObject *scribus_createcharstyle(PyObject * /* self */, PyObject* args, PyObject* keywords);
 
+/* LINE STYLES */
+
+/*! docstring */
+PyDoc_STRVAR(scribus_createcustomlinestyle__doc__,
+QT_TR_NOOP("createCustomLineStyle(styleName, style)\n\n\
+Creates the custom line style 'styleName'.\n\n\
+styleName -> name of the custom line style to create\n\n\
+This function takes list of dictionary\n\
+as parameter for \"style\". Each dictionary represent\n\
+one subline within style. Dictionary can have those keys:\n\n\
+\tColor [optional] -> name of the color to use (string)\n\n\
+\tDash [optional] -> type of line to use (integer)\n\n\
+\tLineEnd [optional] -> type of LineEnd to use (integer)\n\n\
+\tLineJoin [optional] -> type of LineJoin to use (integer)\n\n\
+\tShade [optional] -> opacity of line (integer)\n\n\
+\tWidth [optional] -> width of line (double)\n\
+"));
+PyObject *scribus_createcustomlinestyle(PyObject * /* self */, PyObject* args);
+
 #endif
 
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 8585177a9..be0723944 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -320,6 +320,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("createTable"), scribus_newtable, METH_VARARGS, tr(scribus_newtable__doc__)},
 	{const_cast<char*>("createParagraphStyle"), (PyCFunction)scribus_createparagraphstyle, METH_KEYWORDS, tr(scribus_createparagraphstyle__doc__)},
 	{const_cast<char*>("createCharStyle"), (PyCFunction)scribus_createcharstyle, METH_KEYWORDS, tr(scribus_createcharstyle__doc__)},
+	{const_cast<char*>("createCustomLineStyle"), scribus_createcustomlinestyle, METH_VARARGS, tr(scribus_createcustomlinestyle__doc__)},
 	{const_cast<char*>("currentPage"), (PyCFunction)scribus_actualpage, METH_NOARGS, tr(scribus_actualpage__doc__)},
 	{const_cast<char*>("defineColor"), scribus_newcolor, METH_VARARGS, tr(scribus_newcolor__doc__)},
 	{const_cast<char*>("defineColorRGB"), scribus_newcolorrgb, METH_VARARGS, tr(scribus_newcolorrgb__doc__)},
-- 
2.15.0.rc1

