Page 1 of 1

cursor.setPropertyToDefault( "CharPosture") removes color

PostPosted: Thu Apr 29, 2021 4:31 pm
by antalk
In the snippet below, cursor.setPropertyToDefault( "CharPosture") seems to
also remove font color. I observed similar interaction between other character properties as well when
using cursor.setPropertyToDefault
Runnable python3 example in the attachment.
Originally observed from java code.

LibreOffice
Version: 6.4.6.2
Build ID: 1:6.4.6-0ubuntu0.20.04.1


Code: Select all   Expand viewCollapse view
    #
    # *** TEST ***
    #
    text.insertString( cursor, "(upright black)" , 0 )

    cursor.setPropertyValue( "CharColor", 0xff4040 )
    cursor.setPropertyValue( "CharPosture", ITALIC )
    text.insertString( cursor, "(color italic)" , 0 )

    cursor.setPropertyToDefault( "CharPosture") ## removes italic (OK), but removes CharColor as well (not OK)
    text.insertString( cursor, "(upright, should have color)" , 0 )    ## lost the color



Could you suggest a workaround?

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Thu Apr 29, 2021 5:22 pm
by RoryOF
And what is the "Default" colour property?

See https://www.openoffice.org/api/docs/common/ref/com/sun/star/beans/module-ix.html for the methods available.

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Thu Apr 29, 2021 5:46 pm
by antalk
> And what is the "Default" colour property?

I do not know. The code does not ask for default color, it asks for default CharPosture,
which I expected to to have the effect of removing direct formatting. Without affecting color in any way.

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Thu Apr 29, 2021 6:02 pm
by FJCC
The following code results in the last inserted string being red and upright. I do not understand why the original code does not work and this does.
Code: Select all   Expand viewCollapse view
from com.sun.star.awt.FontSlant  import ITALIC
def togglePosture():
  doc = XSCRIPTCONTEXT.getDocument()
  oText = doc.getText()
  oCurs = oText.createTextCursor()
  oText.insertString( oCurs, "(upright black)" , 0 )
  oCurs.setPropertyValue( "CharColor", 0xff4040 )
  oCurs.setPropertyValue( "CharPosture", ITALIC )
  oText.insertString( oCurs, "(color italic)" , 0 )
  oCurs.setString("(upright, should have color)")
  oCurs.setPropertyToDefault( "CharPosture")

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Thu Apr 29, 2021 11:25 pm
by JeJe
The same thing happens in Basic and it appears to happen with any char property... whichever one you put in the setPropertyToDefault line, all the char properties are reset. Unless that's what you want you'll have to not use setPropertyToDefault.

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 12:04 am
by antalk
Your (FJCC) example suggested the strange behaviour may be related to
chaging properties on empty cursors. The attached workaround attempts
to avoid this by adding extra characters to "hold" the new properties and keeping them
around for the following inserts. Seems to work. Maybe the case of non-empty cursor
could be handled more simply by leaving out the bracketing calls.

Thank you for the example.

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 1:02 pm
by antalk
> you'll have to not use setPropertyToDefault.

Tricky. I am trying to interpret text with html-like markup into a text range within a document.
The idea was that closing tags, like "</b>" should restore the state to that before "<b>",
as reported by getPropertyValue.
This latter sometimes turned out to be "not directly formatted" (which can be checked by getPropertyState),
hence the attempt to remove "direct formatting" by using setPropertyToDefault.

Which turns out to modify properties I did not want to touch: the idea was that
if I insert into red or otherwise formatted text, the insertion should only differ from the surrounding
text in properties prescribed by the markup. But now the first closing tag removes the color
and other direct formatting. I hope the workaround in ex-setPropertyValue-loses-color-workaround.zip
will solve this, but I did not get there yet.

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 1:53 pm
by JeJe
Why can't you just use?

Code: Select all   Expand viewCollapse view
tmp =oCurs.getPropertyValue( whichever)
oCurs.setPropertyValue( whichever, whatever)
do something
oCurs.setPropertyValue( whichever, tmp)

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 2:23 pm
by Villeroy
Or something like this?
Code: Select all   Expand viewCollapse view
v = oCurs.getPropertyDefault("CharPosture")
oCurs.CharPosture = v

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 3:01 pm
by antalk
This is how I started.
Well, almost. I casted tmp to the type documented for the given poperties.
I do not do that in the example below.

A ran into (reproduction in python):
Code: Select all   Expand viewCollapse view
       # The code below assumes that  "CharStyleName" was not
       # set before.
        tmp = cursor.getPropertyValue( "CharStyleName" )
       # tmp = cursor.getPropertyDefault( "CharStyleName" ) gives the same result
        print(tmp) ## prints ""
        cursor.setPropertyValue( "CharStyleName", "Emphasis" )
        text.insertString( cursor, "thisEmphasis" , 0 )

        cursor.setPropertyValue( "CharStyleName", tmp )
        ## IllegalArgumentException
        text.insertString( cursor, "restoredCharStyleName" , 0 )


For "CharStyleName" restoring default or setting to default
with setPropertyValue results in IllegalArgumentException.
(setPropertyToDefault does not)


So I figured this is not the way to remove the effects,
and found
https://www.openoffice.org/api/docs/com ... State.html
and https://www.openoffice.org/api/docs/com ... ibute.html

PropertyAttribute.MAYBEDEFAULT always came out as false (in LibreOffice).
Finally setPropertyToDefault seemed to work, except for the unwanted side effects.

There is also a PropertyAttribute.REMOVEABLE ("indicates that the property can be removed (i.e., by calling XPropertyContainer::removeProperty)")
I did not try removeProperty.

I think I saw examples using setToDefault to remove direct formatting, so I went in that direction.

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 3:32 pm
by JeJe
for the charstylename try

Code: Select all   Expand viewCollapse view
if tmp = "" then tmp ="Default"

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

PostPosted: Fri Apr 30, 2021 5:11 pm
by antalk
Is setting CharStyleName to "Default" in a paragraph with a style prescribing
some properties, for example "Preformatted Text" equivalent to "not changing properties"?

Yes, it seems so. I did not realize that.

Thank you.