Scribus Forums

Scribus => Scripts and Plugins => Topic started by: Zinal on February 07, 2024, 11:34:41 AM

Title: Getting/Setting table cell content?
Post by: Zinal on February 07, 2024, 11:34:41 AM
I've been looking through the Scripter API (https://impagina.org/scribus-scripter-api) but I'm unable to find a way to access or edit table cell content. Are there no such methods available? 
What I'm looking for specifically is the ability to get and set the text in the cell.

Being able to apply styling (Like in a normal text frame) would be a huge plus!
Title: Re: Getting/Setting table cell content?
Post by: Zinal on February 07, 2024, 09:02:57 PM
I completely missed the getCellText (https://impagina.org/scribus-scripter-api/text-frame/#getcelltext) and setCellText (https://impagina.org/scribus-scripter-api/text-frame/#setcelltext) methods!

Still haven't found a way to actually style the text inside of them through the Scripter API..
Title: Re: Getting/Setting table cell content?
Post by: a.l.e on February 07, 2024, 10:00:07 PM
the next time i update the api documentation on impagina, i will move those to functions to the "table" section.

this having been said, i don't see either, how it is possible to easily add the formatting of text inside of table cells.

i did a "simple" try but it was not successful:

in plugins/scriptplugin/cmdtext.cpp , i've removed the check for the frame type in scribus_settextfill().
then scribus does not complain about the item not being of the right type, but running

scribus.setTextColor("Red")

from the scripter console does still not give the expected result.

if you want, you can have a look at the ApplyCharstyleHelper::apply() function in the same file and check where it fails to correctly get the selection or look at the applyCharStyle that is called from there and see if is there is a check for frame type inside of it...

applying the styles from the content palette does work, so there is hope : - )
Title: Re: Getting/Setting table cell content?
Post by: a.l.e on February 07, 2024, 10:06:52 PM
i did one more check:

if i output item->itemName, i get the name of the table... that might be a big issue, if we don't want to recreate all the text formatting function with calls that are specific to table cells.
Title: Re: Getting/Setting table cell content?
Post by: Zinal on February 08, 2024, 07:45:14 AM
I see two solutions to this problem:

First solution is to replicate every formatting function specifically for table cells.. (Like you suggested)
This will essentially create a lot of duplicate code but might be the easier option.

The second solution is to make each text-frame (The table cells essentially) selectable from code and allow them to show up in getPageItems() (https://impagina.org/scribus-scripter-api/page/#getpageitems), etc..
The problem with this approach is that the cells inside the table aren't considered page-items in the traditional sense, since they are children of the table and only (currently) accessible by specifying a row and column.

I'm working on the first solution right now and it's a lot of functions to basically copy/paste.
I'll upload a diff file when I'm satisfied with the result!
Title: Re: Getting/Setting table cell content?
Post by: a.l.e on February 08, 2024, 08:41:58 AM
sorry, but i really don't think that duplicating the script functions is an acceptable solution.

i fear that the code would not be maintainable.
Title: Re: Getting/Setting table cell content?
Post by: a.l.e on February 08, 2024, 10:44:55 AM
i did some research.

and i got scribus_settextfill() to work with text selected in a table cell.

my suggestion would be to add a "GetUniqueTextItem()" that only would return a PageItem* if the selection (or name) can be used as a text frame (text frame, path text, table).

with it, we could then even remove

if (!item->isTextFrame() && !item->isPathText())

and, at the end, have less code instead of duplicating everything : - )
Title: Re: Getting/Setting table cell content?
Post by: a.l.e on February 08, 2024, 12:27:09 PM
I've now opened a ticket and put a draft patch in the bug tracker:

https://bugs.scribus.net/view.php?id=17156

Since the diff is a not as "clean" as it could be, here is the new scribus_settextfill():

PyObject *scribus_settextfill(PyObject* /* self */, PyObject* args)
{
char *Name = const_cast<char*>("");
char *Color;
if (!PyArg_ParseTuple(args, "es|es", "utf-8", &Color, "utf-8", &Name))
return nullptr;
if (!checkHaveDocument())
return nullptr;
PageItem *item = GetUniqueItemAsText(QString::fromUtf8(Name));
if (item == nullptr)
{
PyErr_SetString(WrongFrameTypeError, QObject::tr("Can only set text color on an item that behaves as a text frame.","python error").toLocal8Bit().constData());
return nullptr;
}
ApplyCharstyleHelper<QString>(item, QString::fromUtf8(Color)).apply(&CharStyle::setFillColor, 0, item->itemText.length());
Py_RETURN_NONE;
}