View Issue Details

IDProjectCategoryView StatusLast Update
0016001ScribusScripterpublic2019-12-10 13:29
Reporterale Assigned Toale  
PrioritynormalSeverityfeatureReproducibilityN/A
Status assignedResolutionopen 
Product Version1.5.6.svn 
Target VersionFixed in Version 
Summary0016001: [PATCH] Scripter: add getCharacterStyle()
Descriptionadd getCharacterStyle()
TagsNo tags attached.
PatchYes

Activities

ale

2019-12-10 09:33

manager  

get-character-style.diff (4,746 bytes)
diff --git a/scribus/plugins/scriptplugin/cmdobj.cpp b/scribus/plugins/scriptplugin/cmdobj.cpp
index 04534b865..6c29a9e9d 100644
--- a/scribus/plugins/scriptplugin/cmdobj.cpp
+++ b/scribus/plugins/scriptplugin/cmdobj.cpp
@@ -579,6 +579,39 @@ PyObject *scribus_objectexists(PyObject* /* self */, PyObject* args)
 	return PyBool_FromLong(static_cast<long>(false));
 }
 
+/*!
+  */
+PyObject* scribus_getcharstyle(PyObject* /* self */, PyObject* args)
+{
+	char* name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", &name))
+		return NULL;
+	if (!checkHaveDocument())
+		return NULL;
+	PageItem* item = GetUniqueItem(QString::fromUtf8(name));
+	if (item == NULL)
+		return NULL;
+	if ((item->itemType() != PageItem::TextFrame) && (item->itemType() != PageItem::PathText))
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Cannot get style of a non-text frame.", "python error").toLocal8Bit().constData());
+		return NULL;
+	}
+
+	int selectionLength = item->itemText.selectionLength();
+	if (selectionLength > 0)
+	{
+		int selectionStart = item->itemText.startOfSelection();
+		const CharStyle& currentStyle = item->itemText.charStyle(selectionStart);
+		if (currentStyle.hasParent())
+			return PyUnicode_FromString(currentStyle.parentStyle()->name().toUtf8());
+	}
+	else
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Selection length must be >0.", "python error").toLocal8Bit().constData());
+		return NULL;
+	}
+	Py_RETURN_NONE;
+};
 
 /*
  * Vaclav Smilauer, 2017-21-21
@@ -896,5 +929,5 @@ PV */
 void cmdobjdocwarnings()
 {
 	QStringList s;
-	s << scribus_newrect__doc__ <<scribus_newellipse__doc__ << scribus_newimage__doc__ << scribus_newtext__doc__ << scribus_newtable__doc__ << scribus_newline__doc__ <<scribus_polyline__doc__ << scribus_polygon__doc__ << scribus_bezierline__doc__ <<scribus_pathtext__doc__ <<scribus_deleteobj__doc__ <<scribus_textflow__doc__ <<scribus_objectexists__doc__ <<scribus_getstyle__doc__ <<scribus_setstyle__doc__ <<scribus_getstylenames__doc__ <<scribus_getcharstylenames__doc__ <<scribus_duplicateobject__doc__ <<scribus_copyobject__doc__ <<scribus_pasteobject__doc__;
+	s << scribus_newrect__doc__ <<scribus_newellipse__doc__ << scribus_newimage__doc__ << scribus_newtext__doc__ << scribus_newtable__doc__ << scribus_newline__doc__ <<scribus_polyline__doc__ << scribus_polygon__doc__ << scribus_bezierline__doc__ <<scribus_pathtext__doc__ <<scribus_deleteobj__doc__ <<scribus_textflow__doc__ <<scribus_objectexists__doc__ <<scribus_getcharstyle__doc__ <<scribus_getstyle__doc__ <<scribus_setcharstyle__doc__  <<scribus_setstyle__doc__ <<scribus_getstylenames__doc__ <<scribus_getcharstylenames__doc__ <<scribus_duplicateobject__doc__ <<scribus_copyobject__doc__ <<scribus_pasteobject__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdobj.h b/scribus/plugins/scriptplugin/cmdobj.h
index d2296fb41..ff48c8c65 100644
--- a/scribus/plugins/scriptplugin/cmdobj.h
+++ b/scribus/plugins/scriptplugin/cmdobj.h
@@ -234,6 +234,17 @@ ObjectName is now optional. When none set, search for selection...
 */
 PyObject *scribus_objectexists(PyObject * /*self*/, PyObject* args);
 
+PyDoc_STRVAR(scribus_getcharstyle__doc__,
+QT_TR_NOOP("getCharacterStyle([\"name\"])\n\
+\n\
+Return name of character style applied to object named \"name\". If \"name\" is not given,\n\
+the currently selected object is used. If current object has a text selection, \n\
+the name of character style applied to start of selection is returned. Otherwise an error is returned.\n\
+"));
+/**
+ * \author Michael Uhlenberg
+ */
+PyObject* scribus_getcharstyle(PyObject* /*self*/, PyObject* args);
 
 
 /*! docstring */
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 49d87150e..72a4940af 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -391,6 +391,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("getObjectAttributes"), scribus_getobjectattributes, METH_VARARGS, tr(scribus_getobjectattributes__doc__)},
 	{const_cast<char*>("getSelectedObject"), scribus_getselobjnam, METH_VARARGS, tr(scribus_getselobjnam__doc__)},
 	{const_cast<char*>("getSize"), scribus_getsize, METH_VARARGS, tr(scribus_getsize__doc__)},
+	{const_cast<char*>("getCharacterStyle"), scribus_getcharstyle, METH_VARARGS, tr(scribus_getcharstyle__doc__) },
 	{const_cast<char*>("getStyle"), scribus_getstyle, METH_VARARGS, tr(scribus_getstyle__doc__) },
 	{const_cast<char*>("getTableRows"), scribus_gettablerows, METH_VARARGS, tr(scribus_gettablerows__doc__)},
 	{const_cast<char*>("getTableRowHeight"), scribus_gettablerowheight, METH_VARARGS, tr(scribus_gettablerowheight__doc__)},
get-character-style.diff (4,746 bytes)

ale

2019-12-10 09:38

manager   ~0047235

i will propose a second version that does not require a selection...

ale

2019-12-10 10:03

manager   ~0047236

here is a new patch that also works when there is no selection

get-character-style-2.diff (4,560 bytes)
diff --git a/scribus/plugins/scriptplugin/cmdobj.cpp b/scribus/plugins/scriptplugin/cmdobj.cpp
index 04534b865..c681697fd 100644
--- a/scribus/plugins/scriptplugin/cmdobj.cpp
+++ b/scribus/plugins/scriptplugin/cmdobj.cpp
@@ -579,6 +579,33 @@ PyObject *scribus_objectexists(PyObject* /* self */, PyObject* args)
 	return PyBool_FromLong(static_cast<long>(false));
 }
 
+/*!
+  */
+PyObject* scribus_getcharstyle(PyObject* /* self */, PyObject* args)
+{
+	char* name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", &name))
+		return NULL;
+	if (!checkHaveDocument())
+		return NULL;
+	PageItem* item = GetUniqueItem(QString::fromUtf8(name));
+	if (item == NULL)
+		return NULL;
+	if ((item->itemType() != PageItem::TextFrame) && (item->itemType() != PageItem::PathText))
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Cannot get style of a non-text frame.", "python error").toLocal8Bit().constData());
+		return NULL;
+	}
+
+	const CharStyle& currentStyle = item->itemText.selectionLength() > 0 ?
+		item->itemText.charStyle(item->itemText.startOfSelection()) :
+		item->itemText.charStyle(item->itemText.cursorPosition());
+
+	if (currentStyle.hasParent())
+		return PyUnicode_FromString(currentStyle.parentStyle()->name().toUtf8());
+
+	Py_RETURN_NONE;
+};
 
 /*
  * Vaclav Smilauer, 2017-21-21
@@ -896,5 +923,5 @@ PV */
 void cmdobjdocwarnings()
 {
 	QStringList s;
-	s << scribus_newrect__doc__ <<scribus_newellipse__doc__ << scribus_newimage__doc__ << scribus_newtext__doc__ << scribus_newtable__doc__ << scribus_newline__doc__ <<scribus_polyline__doc__ << scribus_polygon__doc__ << scribus_bezierline__doc__ <<scribus_pathtext__doc__ <<scribus_deleteobj__doc__ <<scribus_textflow__doc__ <<scribus_objectexists__doc__ <<scribus_getstyle__doc__ <<scribus_setstyle__doc__ <<scribus_getstylenames__doc__ <<scribus_getcharstylenames__doc__ <<scribus_duplicateobject__doc__ <<scribus_copyobject__doc__ <<scribus_pasteobject__doc__;
+	s << scribus_newrect__doc__ <<scribus_newellipse__doc__ << scribus_newimage__doc__ << scribus_newtext__doc__ << scribus_newtable__doc__ << scribus_newline__doc__ <<scribus_polyline__doc__ << scribus_polygon__doc__ << scribus_bezierline__doc__ <<scribus_pathtext__doc__ <<scribus_deleteobj__doc__ <<scribus_textflow__doc__ <<scribus_objectexists__doc__ <<scribus_getcharstyle__doc__ <<scribus_getstyle__doc__ <<scribus_setcharstyle__doc__  <<scribus_setstyle__doc__ <<scribus_getstylenames__doc__ <<scribus_getcharstylenames__doc__ <<scribus_duplicateobject__doc__ <<scribus_copyobject__doc__ <<scribus_pasteobject__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdobj.h b/scribus/plugins/scriptplugin/cmdobj.h
index d2296fb41..ff48c8c65 100644
--- a/scribus/plugins/scriptplugin/cmdobj.h
+++ b/scribus/plugins/scriptplugin/cmdobj.h
@@ -234,6 +234,17 @@ ObjectName is now optional. When none set, search for selection...
 */
 PyObject *scribus_objectexists(PyObject * /*self*/, PyObject* args);
 
+PyDoc_STRVAR(scribus_getcharstyle__doc__,
+QT_TR_NOOP("getCharacterStyle([\"name\"])\n\
+\n\
+Return name of character style applied to object named \"name\". If \"name\" is not given,\n\
+the currently selected object is used. If current object has a text selection, \n\
+the name of character style applied to start of selection is returned. Otherwise an error is returned.\n\
+"));
+/**
+ * \author Michael Uhlenberg
+ */
+PyObject* scribus_getcharstyle(PyObject* /*self*/, PyObject* args);
 
 
 /*! docstring */
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 49d87150e..72a4940af 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -391,6 +391,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("getObjectAttributes"), scribus_getobjectattributes, METH_VARARGS, tr(scribus_getobjectattributes__doc__)},
 	{const_cast<char*>("getSelectedObject"), scribus_getselobjnam, METH_VARARGS, tr(scribus_getselobjnam__doc__)},
 	{const_cast<char*>("getSize"), scribus_getsize, METH_VARARGS, tr(scribus_getsize__doc__)},
+	{const_cast<char*>("getCharacterStyle"), scribus_getcharstyle, METH_VARARGS, tr(scribus_getcharstyle__doc__) },
 	{const_cast<char*>("getStyle"), scribus_getstyle, METH_VARARGS, tr(scribus_getstyle__doc__) },
 	{const_cast<char*>("getTableRows"), scribus_gettablerows, METH_VARARGS, tr(scribus_gettablerows__doc__)},
 	{const_cast<char*>("getTableRowHeight"), scribus_gettablerowheight, METH_VARARGS, tr(scribus_gettablerowheight__doc__)},

ale

2019-12-10 13:29

manager   ~0047239

feedback from jean in irc: cursor is not (really) defined when scribus is not in edit mode.

third patch...

get-character-style-3.diff (4,696 bytes)
diff --git a/scribus/plugins/scriptplugin/cmdobj.cpp b/scribus/plugins/scriptplugin/cmdobj.cpp
index 04534b865..00a48406d 100644
--- a/scribus/plugins/scriptplugin/cmdobj.cpp
+++ b/scribus/plugins/scriptplugin/cmdobj.cpp
@@ -579,6 +579,36 @@ PyObject *scribus_objectexists(PyObject* /* self */, PyObject* args)
 	return PyBool_FromLong(static_cast<long>(false));
 }
 
+/*!
+  */
+PyObject* scribus_getcharstyle(PyObject* /* self */, PyObject* args)
+{
+	char* name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", &name))
+		return NULL;
+	if (!checkHaveDocument())
+		return NULL;
+	PageItem* item = GetUniqueItem(QString::fromUtf8(name));
+	if (item == NULL)
+		return NULL;
+	if ((item->itemType() != PageItem::TextFrame) && (item->itemType() != PageItem::PathText))
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Cannot get style of a non-text frame.", "python error").toLocal8Bit().constData());
+		return NULL;
+	}
+
+
+	auto  currentDoc = ScCore->primaryMainWindow()->doc;
+	const auto storyText = item->itemText;
+	const CharStyle& currentStyle = storyText.selectionLength() > 0 ?
+		storyText.charStyle(storyText.startOfSelection()) :
+		(currentDoc->appMode == modeEdit ? storyText.charStyle() : storyText.defaultStyle().charStyle());
+
+	if (currentStyle.hasParent())
+		return PyUnicode_FromString(currentStyle.parentStyle()->name().toUtf8());
+
+	Py_RETURN_NONE;
+};
 
 /*
  * Vaclav Smilauer, 2017-21-21
@@ -896,5 +926,5 @@ PV */
 void cmdobjdocwarnings()
 {
 	QStringList s;
-	s << scribus_newrect__doc__ <<scribus_newellipse__doc__ << scribus_newimage__doc__ << scribus_newtext__doc__ << scribus_newtable__doc__ << scribus_newline__doc__ <<scribus_polyline__doc__ << scribus_polygon__doc__ << scribus_bezierline__doc__ <<scribus_pathtext__doc__ <<scribus_deleteobj__doc__ <<scribus_textflow__doc__ <<scribus_objectexists__doc__ <<scribus_getstyle__doc__ <<scribus_setstyle__doc__ <<scribus_getstylenames__doc__ <<scribus_getcharstylenames__doc__ <<scribus_duplicateobject__doc__ <<scribus_copyobject__doc__ <<scribus_pasteobject__doc__;
+	s << scribus_newrect__doc__ <<scribus_newellipse__doc__ << scribus_newimage__doc__ << scribus_newtext__doc__ << scribus_newtable__doc__ << scribus_newline__doc__ <<scribus_polyline__doc__ << scribus_polygon__doc__ << scribus_bezierline__doc__ <<scribus_pathtext__doc__ <<scribus_deleteobj__doc__ <<scribus_textflow__doc__ <<scribus_objectexists__doc__ <<scribus_getcharstyle__doc__ <<scribus_getstyle__doc__ <<scribus_setcharstyle__doc__  <<scribus_setstyle__doc__ <<scribus_getstylenames__doc__ <<scribus_getcharstylenames__doc__ <<scribus_duplicateobject__doc__ <<scribus_copyobject__doc__ <<scribus_pasteobject__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdobj.h b/scribus/plugins/scriptplugin/cmdobj.h
index d2296fb41..e75f5f317 100644
--- a/scribus/plugins/scriptplugin/cmdobj.h
+++ b/scribus/plugins/scriptplugin/cmdobj.h
@@ -234,6 +234,17 @@ ObjectName is now optional. When none set, search for selection...
 */
 PyObject *scribus_objectexists(PyObject * /*self*/, PyObject* args);
 
+PyDoc_STRVAR(scribus_getcharstyle__doc__,
+QT_TR_NOOP("getCharacterStyle([\"name\"])\n\
+\n\
+Return name of character style applied to object named \"name\". If \"name\" is not given,\n\
+the currently selected object is used. If current object has a text selection, \n\
+the name of character style applied to start of selection is returned. Otherwise an error is returned.\n\
+"));
+/**
+ * \author Michael Uhlenberg & Ale Rimoldi
+ */
+PyObject* scribus_getcharstyle(PyObject* /*self*/, PyObject* args);
 
 
 /*! docstring */
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 49d87150e..72a4940af 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -391,6 +391,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("getObjectAttributes"), scribus_getobjectattributes, METH_VARARGS, tr(scribus_getobjectattributes__doc__)},
 	{const_cast<char*>("getSelectedObject"), scribus_getselobjnam, METH_VARARGS, tr(scribus_getselobjnam__doc__)},
 	{const_cast<char*>("getSize"), scribus_getsize, METH_VARARGS, tr(scribus_getsize__doc__)},
+	{const_cast<char*>("getCharacterStyle"), scribus_getcharstyle, METH_VARARGS, tr(scribus_getcharstyle__doc__) },
 	{const_cast<char*>("getStyle"), scribus_getstyle, METH_VARARGS, tr(scribus_getstyle__doc__) },
 	{const_cast<char*>("getTableRows"), scribus_gettablerows, METH_VARARGS, tr(scribus_gettablerows__doc__)},
 	{const_cast<char*>("getTableRowHeight"), scribus_gettablerowheight, METH_VARARGS, tr(scribus_gettablerowheight__doc__)},

Issue History

Date Modified Username Field Change
2019-12-10 09:33 ale New Issue
2019-12-10 09:33 ale File Added: get-character-style.diff
2019-12-10 09:33 ale Summary SCripter: add getCharacterStyle() => [PATCH] Scripter: add getCharacterStyle()
2019-12-10 09:33 ale Assigned To => ale
2019-12-10 09:33 ale Status new => assigned
2019-12-10 09:38 ale Note Added: 0047235
2019-12-10 10:03 ale File Added: get-character-style-2.diff
2019-12-10 10:03 ale Note Added: 0047236
2019-12-10 13:29 ale File Added: get-character-style-3.diff
2019-12-10 13:29 ale Note Added: 0047239