View Issue Details

IDProjectCategoryView StatusLast Update
0008957ScribusScripterpublic2016-12-11 21:04
Reportermochouinard Assigned Tojainbasil  
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
PlatformInten 64bitOSUbuntuOS Version9.10
Product Version1.3.7svn 
Summary0008957: [patch] Add setTextPreserveStyle() that wont default back to the default style. (Silimar to Scribus 1.3.3 setText)
DescriptionI really like to keep my template formating in the template, and having to re apply the style in my scripts is not something I really like to do.
TagsNo tags attached.
PatchYes

Relationships

related to 0014453 new Scripter command setStyle() doesn't work 

Activities

jghali

2010-03-20 18:52

administrator   ~0023575

Last edited: 2010-03-20 18:53

I strongly advise you against using such patch. It relies on behaviors that will be changed in future version. Noticeably, the story text default style will not be modifiable anymore. As previously stated when modifying text by script, you should apply style and formatting only on text content :
- create text box
- add some text
- select text
- apply formatting on selected text

ale

2010-03-21 08:01

manager   ~0023581

imho creating a "applyParagraphFunction()" function would be a better solution to this problem.

and in the future, i think we should drop the idea that putting text into a frame clears its style settings.

jghali

2010-03-21 11:53

administrator   ~0023585

Last edited: 2010-03-21 11:53

a_l_e : a frame has no style anymore since 1.3.4. Only a text stream has and that default style is shared by all text frames the text stream runs in. That style will be made read only at a point. So not clearing style settings when putting text in frame will not avoid scripts which try to modify it to break.

setStyle() already allows to apply paragraph style.

mochouinard

2010-03-21 13:23

reporter   ~0023586

I don't totally understand what you guys talk about ;) but here is what I'm doing :
I create a template in scribus with prefilled sample data to prepare the style correctly. Then in my python script, I want to only modify the text, not the style. the setText() in 1.3.3 allowed me to do this, and it why I made setTextPreserveStyle()

Now, I don't really care how it implemented, as long as I can do this, I'll be happy. And I'm sure I won't be alone that will have this problem migrating from 1.3.3 to newer version.

jghali

2010-03-21 14:05

administrator   ~0023588

Last edited: 2010-03-21 14:09

This is as simple as this : a text box has no style setting anymore, so your setTextPreserveStyle() won't help in future versions as the story text default style won't be modifiable anymore in gui or in script. From now on, formatting or styling must be applied on real text content, not on a box. Otherwise your scripts will break again soon.

ale

2010-03-22 19:51

manager   ~0023596

there is an issue which generally affects text frames:

this code will replace the text in the current frame and set it to test without losing the formatting (as moc wants to do)

scribus.selectText(0, scribus.getTextLength());
scribus.deleteText();
scribus.insertText('test', -1);


this code will lose the formatting

scribus.deleteText();
scribus.insertText('test', -1);

this also lose the formatting:

scribus.setText('acbdefg');


i understand that the formatting wants to be applied to text and not to frames, but, then, an empty frame should always contain an empty string which holds the current formatting (it may be non trivial to find a reliable way to choose the format that applies to the text inserted after the cursor, but the must be a way!)

ale

2012-06-13 14:21

manager   ~0028124

jain, please check if this bug can't be solved by working on http://bugs.scribus.net/view.php?id=10742

mochouinard

2012-06-13 14:40

reporter   ~0028125

As discussed in IRC, an solution that would be acceptable and maybe more flexible, would be a call we can do to store into an object all the style at a particular position in a text. Then we would apply text that would override the formating, but we could apply that object style back on the text box. We could also allow to create a style from that object. Then I would just have to make my own local function that does the save of style, apply text, and reapply of the style.

Only reason why this current solution would be better, is there is less change of problem when people add new special style... They won't have to be added to the object(especially if we have pluggable style stuff in the future)

mochouinard

2012-06-13 14:40

reporter  

Scribus_scipter_settextpreservestyle_api.diff (5,128 bytes)   
diff --git a/scribus/plugins/scriptplugin/cmdtext.cpp b/scribus/plugins/scriptplugin/cmdtext.cpp
index fff491b..811d24a 100644
--- a/scribus/plugins/scriptplugin/cmdtext.cpp
+++ b/scribus/plugins/scriptplugin/cmdtext.cpp
@@ -241,6 +241,40 @@ PyObject *scribus_gettext(PyObject* /* self */, PyObject* args)
 	return PyString_FromString(text.toUtf8());
 }
 
+PyObject *scribus_setboxtextpreservestyle(PyObject* /* self */, PyObject* args)
+{
+	char *Name = const_cast<char*>("");
+	char *Text;
+	if (!PyArg_ParseTuple(args, "es|es", "utf-8", &Text, "utf-8", &Name))
+		return NULL;
+	if(!checkHaveDocument())
+		return NULL;
+	PageItem *currItem = GetUniqueItem(QString::fromUtf8(Name));
+	if (currItem == NULL)
+		return NULL;
+	if (!(currItem->asTextFrame()) && !(currItem->asPathText()))
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Cannot set text of non-text frame.","python error").toLocal8Bit().constData());
+		return NULL;
+	}
+	QString Daten = QString::fromUtf8(Text);
+	Daten.replace("\r\n", SpecialChars::PARSEP);
+	Daten.replace(QChar('\n') , SpecialChars::PARSEP);
+	PyMem_Free(Text);
+	currItem->itemText.clearpreservestyle();
+	currItem->oldCPos = 0;
+	for (int a = 0; a < ScCore->primaryMainWindow()->doc->FrameItems.count(); ++a)
+	{
+		ScCore->primaryMainWindow()->doc->FrameItems.at(a)->ItemNr = a;
+	}
+	currItem->itemText.insertChars(0, Daten);
+	currItem->invalidateLayout();
+	currItem->Dirty = false;
+	//      Py_INCREF(Py_None);
+	//      return Py_None;
+	Py_RETURN_NONE;
+}
+
 PyObject *scribus_setboxtext(PyObject* /* self */, PyObject* args)
 {
 	char *Name = const_cast<char*>("");
@@ -1105,5 +1139,6 @@ void cmdtextdocwarnings()
 	  << scribus_setpdfbookmark__doc__ << scribus_ispdfbookmark__doc__
 	  << scribus_hyphenatetext__doc__ << scribus_dehyphenatetext__doc__
 	  << scribus_gettextdistances__doc__ << scribus_settextdistances__doc__
-	  << scribus_settextscalingh__doc__ << scribus_settextscalingv__doc__;
+	  << scribus_settextscalingh__doc__ << scribus_settextscalingv__doc__
+	  << scribus_setboxtextpreservestyle__doc__;
 }
diff --git a/scribus/plugins/scriptplugin/cmdtext.h b/scribus/plugins/scriptplugin/cmdtext.h
index 1a9a142..956c063 100644
--- a/scribus/plugins/scriptplugin/cmdtext.h
+++ b/scribus/plugins/scriptplugin/cmdtext.h
@@ -134,6 +134,18 @@ used.\n\
 PyObject *scribus_setboxtext(PyObject * /*self*/, PyObject* args);
 
 /*! docstring */
+PyDoc_STRVAR(scribus_setboxtextpreservestyle__doc__,
+QT_TR_NOOP("setTextPreserveStyle(\"text\", [\"name\"])\n\
+\n\
+Sets the text of the text frame \"name\" to the text of the string \"text\".\n\
+Text must be UTF8 encoded - use e.g. unicode(text, 'iso-8859-2'). See the FAQ\n\
+for more details. Style is preserved. If \"name\" is not given the currently \n\
+selected item is used.\n\
+"));
+/*! Set text */
+PyObject *scribus_setboxtextpreservestyle(PyObject* /* self */, PyObject* args);
+
+/*! docstring */
 PyDoc_STRVAR(scribus_inserttext__doc__,
 QT_TR_NOOP("insertText(\"text\", pos, [\"name\"])\n\
 \n\
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index c4233f5..98cc3e9 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -465,6 +465,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("setTextAlignment"), scribus_setalign, METH_VARARGS, tr(scribus_setalign__doc__)},
 	{const_cast<char*>("setTextColor"), scribus_settextfill, METH_VARARGS, tr(scribus_settextfill__doc__)},
 	{const_cast<char*>("setText"), scribus_setboxtext, METH_VARARGS, tr(scribus_setboxtext__doc__)},
+	{const_cast<char*>("setTextPreserveStyle"), scribus_setboxtextpreservestyle, METH_VARARGS, tr(scribus_setboxtextpreservestyle__doc__)},
 	{const_cast<char*>("setTextScalingH"), scribus_settextscalingh, METH_VARARGS, tr(scribus_settextscalingh__doc__)},
 	{const_cast<char*>("setTextScalingV"), scribus_settextscalingv, METH_VARARGS, tr(scribus_settextscalingv__doc__)},
 	{const_cast<char*>("setTextShade"), scribus_settextshade, METH_VARARGS, tr(scribus_settextshade__doc__)},
diff --git a/scribus/text/storytext.cpp b/scribus/text/storytext.cpp
index 3097850..d32d7d0 100644
--- a/scribus/text/storytext.cpp
+++ b/scribus/text/storytext.cpp
@@ -201,6 +201,19 @@ void StoryText::clear()
 	invalidateAll();
 }
 
+void StoryText::clearpreservestyle()
+{
+	selFirst = 0;
+	selLast = -1;
+
+	firstFrameItem = 0;
+	lastFrameItem = -1;
+
+	d->clear();
+	d->len = 0;
+	invalidateAll();
+}
+
 void StoryText::insert(const StoryText& other, bool onlySelection)
 {
 	insert(d->cursorPosition, other, onlySelection);
diff --git a/scribus/text/storytext.h b/scribus/text/storytext.h
index f218f44..89247d1 100644
--- a/scribus/text/storytext.h
+++ b/scribus/text/storytext.h
@@ -108,6 +108,8 @@ class SCRIBUS_API StoryText : public QObject, public SaxIO
 	int  normalizedCursorPosition();
 
  	void clear();
+	void clearpreservestyle();
+
 	StoryText copy() const;
 	// Insert chars from another StoryText object at current cursor position
 	void insert(const StoryText& other, bool onlySelection = false);

Issue History

Date Modified Username Field Change
2010-03-20 16:51 mochouinard New Issue
2010-03-20 16:51 mochouinard File Added: scribus_setTextPreserveStyle.diff
2010-03-20 18:52 jghali Note Added: 0023575
2010-03-20 18:53 jghali Note Edited: 0023575
2010-03-21 08:01 ale Note Added: 0023581
2010-03-21 11:53 jghali Note Added: 0023585
2010-03-21 11:53 jghali Note Edited: 0023585
2010-03-21 13:23 mochouinard Note Added: 0023586
2010-03-21 14:05 jghali Note Added: 0023588
2010-03-21 14:09 jghali Note Edited: 0023588
2010-03-22 19:51 ale Note Added: 0023596
2012-06-13 14:20 ale Assigned To => jainbasil
2012-06-13 14:20 ale Status new => assigned
2012-06-13 14:21 ale Note Added: 0028124
2012-06-13 14:40 mochouinard Note Added: 0028125
2012-06-13 14:40 mochouinard File Added: Scribus_scipter_settextpreservestyle_api.diff
2012-06-13 14:41 mochouinard File Deleted: scribus_setTextPreserveStyle.diff
2014-10-24 23:00 Kunda Patch => Yes
2016-12-11 21:04 Kunda Relationship added related to 0014453