r22113 by jghali -

scribus-commit scribus-commit at lists.scribus.net
Tue Jul 18 21:03:50 UTC 2017


Author: jghali
Date: Tue Jul 18 21:03:50 2017
New Revision: 22113

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=22113
Log:
#9350: apply character styles from the scripter

Modified:
    trunk/Scribus/doc/en/scripterapi-manobj.html
    trunk/Scribus/doc/fr/scripterapi-manobj.html
    trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.cpp
    trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.h
    trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp

Modified: trunk/Scribus/doc/en/scripterapi-manobj.html
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22113&path=/trunk/Scribus/doc/en/scripterapi-manobj.html
==============================================================================
--- trunk/Scribus/doc/en/scripterapi-manobj.html	(original)
+++ trunk/Scribus/doc/en/scripterapi-manobj.html	Tue Jul 18 21:03:50 2017
@@ -54,6 +54,10 @@
 <p>Scales the group the object "name" belongs to. Values greater than 1 enlarge the group, values smaller than 1 make the group smaller e.g a value of 0.5 scales the group to 50 % of its original size, a value of 1.5 scales the group to 150 % of its original size.  The value for "factor" must be greater than 0. If "name" is not given the currently selected item is used.</p>
 <p>May raise ValueError if an invalid scale factor is passed.</p></dd>
 
+<dt><a name="-setCharacterStyle"><strong>setCharacterStyle</strong></a>(...)</dt>
+<dd><code>setCharacterStyle("style" [, "name"])</code>
+<p>Apply the named character "style" to the object named "name". If object name is given, style is applied to the current text selection in object "name". If no object name is given, style is applied on selected object.</p></dd>
+
 <dt><a name="-setScaleImageToFrame"><strong>setScaleImageToFrame</strong></a>(...)</dt>
 <dd><code>setScaleImageToFrame(scaletoframe, proportional=None, name=<selection>)</code>
 <p>Sets the scale to frame on the selected or specified image frame to `scaletoframe'. If `proportional' is specified, set fixed aspect ratio scaling to `proportional'. Both `scaletoframe' and `proportional' are boolean.</p>

Modified: trunk/Scribus/doc/fr/scripterapi-manobj.html
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22113&path=/trunk/Scribus/doc/fr/scripterapi-manobj.html
==============================================================================
--- trunk/Scribus/doc/fr/scripterapi-manobj.html	(original)
+++ trunk/Scribus/doc/fr/scripterapi-manobj.html	Tue Jul 18 21:03:50 2017
@@ -50,6 +50,10 @@
 <p>Peut générer ValueError si le facteur de redimensionnement n'est
 pas valide.</p></dd>
 
+<dt><a name="-setCharacterStyle"><strong>setCharacterStyle</strong></a>(...)</dt>
+<dd><code><a href="#-setCharacterStyle">setCharacterStyle</a>("style" [, "nom"])</code>
+<p>Applique le "style" de caractère désigné à l'objet "nom". Si un nom d'objet est fourni, le style est appliqué à la sélection de texte courante de l'objet donné. Si aucun objet n'est fourni, le style est appliqué à l'objet sélectionné.</p></dd>
+
 <dt><a name="-setScaleImageToFrame"><strong>setScaleImageToFrame</strong></a>(...)</dt>
 <dd><code><a href="#-setScaleImageToFrame">setScaleImageToFrame</a>(redimAuCadre, proportionnel=None, nom=<selection>)</code> 	<p>Établit le mode de redimensionnement par rapport au cadre d'image sélectionné, ou spécifié dans 'redimAuCadre'. 
 Si 'proportionnel' est spécifié, établit le redimensionnement en maintenant des proportions fixes. 'redimAuCadre' et 'proportionnel' sont booléens.</p>
@@ -57,7 +61,7 @@
 
 <dt><a name="-setStyle"><strong>setStyle</strong></a>(...)</dt>
 <dd><code><a href="#-setStyle">setStyle</a>("style" [, "nom"])</code>
-<p>Établit le "style" désigné à l'objet "nom". Si un nom d'objet est fourni, le style est appliqué à la sélection de texte courante de l'objet donné. Si aucun objet n'est fourni, le style est appliqué à l'objet sélectionné.</p></dd>
+<p>Applique le "style" désigné à l'objet "nom". Si un nom d'objet est fourni, le style est appliqué à la sélection de texte courante de l'objet donné. Si aucun objet n'est fourni, le style est appliqué à l'objet sélectionné.</p></dd>
 
 <dt><a name="-sizeObject"><strong>sizeObject</strong></a>(...)</dt>
 <dd><code><a href="#-sizeObject">sizeObject</a>(largeur, hauteur [, "nom"])</code>

Modified: trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22113&path=/trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.cpp	(original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.cpp	Tue Jul 18 21:03:50 2017
@@ -622,10 +622,23 @@
 		// for current item only
 		if (ScCore->primaryMainWindow()->doc->m_Selection->count() == 0 || (strlen(name) > 0))
 		{
+			// Store text selection as clearing object selection
+			// will also clear text selection
+			int selectionStart = -1;
+			int selectionLength = item->itemText.lengthOfSelection();
+			if (selectionLength > 0)
+				selectionStart = item->itemText.startOfSelection();
 			// quick hack to always apply on the right frame - pv
 			ScCore->primaryMainWindow()->view->Deselect(true);
 			//CB I dont think we need to draw here. Its faster if we dont.
 			ScCore->primaryMainWindow()->view->SelectItem(item, false);
+			// Restore stext selection if necessary
+			if (selectionStart >= 0)
+			{
+				item->itemText.deselectAll();
+				item->itemText.select(selectionStart, selectionLength);
+				item->HasSel = true;
+			}
 			// Now apply the style.
 			int mode = ScCore->primaryMainWindow()->doc->appMode;
 			ScCore->primaryMainWindow()->doc->appMode = modeEdit;
@@ -651,6 +664,92 @@
 }
 
 /*
+* Jean Ghali, 2017-07-18
+* Apply the named character style to the currently selected object.
+* pv, 2004-09-13, optionaly param objectName + "check the page" stuff
+*/
+PyObject *scribus_setcharstyle(PyObject* /* self */, PyObject* args)
+{
+	char *style = const_cast<char*>("");
+	char *name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "es|es", "utf-8", &style, "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))
+	{
+		// First, find the style number associated with the requested style
+		// by scanning through the styles looking for the name. If
+		// we can't find it, raise PyExc_Exception.
+		// FIXME: Should use a more specific exception.
+		bool found = false;
+		QString charStyleName = QString::fromUtf8(style);
+		// We start at zero here because it's OK to match an internal name
+		ScribusDoc*  currentDoc = ScCore->primaryMainWindow()->doc;
+		ScribusView* currentView = ScCore->primaryMainWindow()->view;
+		ScribusMainWindow* currentWin = ScCore->primaryMainWindow();
+		const StyleSet<CharStyle> &docCharStyles = currentDoc->charStyles();
+		int docCharacterStylesCount = docCharStyles.count();
+		for (int i = 0; i < docCharacterStylesCount; ++i)
+		{
+			if (docCharStyles[i].name() == charStyleName) {
+				found = true;
+				break;
+			}
+		}
+		if (!found) {
+			// whoops, the user specified an invalid style, complain loudly.
+			PyErr_SetString(NotFoundError, QObject::tr("Character style not found.", "python error").toLocal8Bit().constData());
+			return NULL;
+		}
+		// for current item only
+		if (currentDoc->m_Selection->count() == 0 || (strlen(name) > 0))
+		{
+			// Store text selection as clearing object selection
+			// will also clear text selection
+			int selectionStart = -1;
+			int selectionLength = item->itemText.lengthOfSelection();
+			if (selectionLength > 0)
+				selectionStart = item->itemText.startOfSelection();
+			// quick hack to always apply on the right frame - pv
+			currentView->Deselect(true);
+			//CB I dont think we need to draw here. Its faster if we dont.
+			currentView->SelectItem(item, false);
+			// Restore stext selection if necessary
+			if (selectionStart >= 0)
+			{
+				item->itemText.deselectAll();
+				item->itemText.select(selectionStart, selectionLength);
+				item->HasSel = true;
+			}	
+			// Now apply the style.
+			int mode = ScCore->primaryMainWindow()->doc->appMode;
+			currentDoc->appMode = modeEdit;
+			currentWin->setNewCharStyle(charStyleName);
+			currentDoc->appMode = mode;
+		}
+		else // for multiple selection
+		{
+			int mode = currentDoc->appMode;
+			currentDoc->appMode = modeNormal;
+			currentDoc->itemSelection_SetNamedCharStyle(charStyleName);
+			currentDoc->appMode = mode;
+		}
+	}
+	else
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Cannot set character style on a non-text frame.", "python error").toLocal8Bit().constData());
+		return NULL;
+	}
+	//	Py_INCREF(Py_None);
+	//	return Py_None;
+	Py_RETURN_NONE;
+}
+
+/*
  * Craig Ringer, 2004-09-09
  * Enumerate all known paragraph styles
  */

Modified: trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22113&path=/trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.h
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.h	(original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/cmdobj.h	Tue Jul 18 21:03:50 2017
@@ -250,6 +250,19 @@
 PyObject *scribus_setstyle(PyObject * /*self*/, PyObject* args);
 
 /*! docstring */
+PyDoc_STRVAR(scribus_setcharstyle__doc__,
+	QT_TR_NOOP("setCharacterStyle(\"style\" [, \"name\"])\n\
+\n\
+Apply the named character \"style\" to the object named \"name\". If object name is\n\
+given, style is applied to the current text selection in object \"name\".\n\
+If no object name is given, style is applied on selected object.\n\
+"));
+/**
+Apply the named character style to the currently selected object.
+*/
+PyObject *scribus_setcharstyle(PyObject * /*self*/, PyObject* args);
+
+/*! docstring */
 PyDoc_STRVAR(scribus_getstylenames__doc__,
 QT_TR_NOOP("getAllStyles() -> list\n\
 \n\

Modified: trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22113&path=/trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp	(original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp	Tue Jul 18 21:03:50 2017
@@ -531,6 +531,7 @@
 	{const_cast<char*>("dehyphenateText"), scribus_dehyphenatetext, METH_VARARGS, tr(scribus_dehyphenatetext__doc__)},
 	{const_cast<char*>("setScaleImageToFrame"), (PyCFunction)scribus_setscaleimagetoframe, METH_KEYWORDS, tr(scribus_setscaleimagetoframe__doc__)},
 	{const_cast<char*>("setStyle"), scribus_setstyle, METH_VARARGS, tr(scribus_setstyle__doc__)},
+	{const_cast<char*>("setCharacterStyle"), scribus_setcharstyle, METH_VARARGS, tr(scribus_setcharstyle__doc__) },
 	{const_cast<char*>("setTableStyle"), scribus_settablestyle, METH_VARARGS, tr(scribus_settablestyle__doc__)},
 	{const_cast<char*>("setTableLeftBorder"), scribus_settableleftborder, METH_VARARGS, tr(scribus_settableleftborder__doc__)},
 	{const_cast<char*>("setTableRightBorder"), scribus_settablerightborder, METH_VARARGS, tr(scribus_settablerightborder__doc__)},




More information about the scribus-commit mailing list