cursor.setPropertyToDefault( "CharPosture") removes color

Creating a macro - Writing a Script - Using the API

cursor.setPropertyToDefault( "CharPosture") removes color

Postby antalk » Thu Apr 29, 2021 4:31 pm

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?
Attachments
ex-setPropertyValue-loses-color.zip
(15.74 KiB) Downloaded 54 times
antalk
 
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

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

Postby RoryOF » Thu Apr 29, 2021 5:22 pm

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.
Apache OpenOffice 4.1.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32534
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Postby antalk » Thu Apr 29, 2021 5:46 pm

> 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.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
antalk
 
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

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

Postby FJCC » Thu Apr 29, 2021 6:02 pm

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")
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8109
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby JeJe » Thu Apr 29, 2021 11:25 pm

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1533
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby antalk » Fri Apr 30, 2021 12:04 am

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.
Attachments
ex-setPropertyValue-loses-color-workaround.zip
(24.49 KiB) Downloaded 61 times
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
antalk
 
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

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

Postby antalk » Fri Apr 30, 2021 1:02 pm

> 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.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
antalk
 
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

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

Postby JeJe » Fri Apr 30, 2021 1:53 pm

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)
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1533
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby Villeroy » Fri Apr 30, 2021 2:23 pm

Or something like this?
Code: Select all   Expand viewCollapse view
v = oCurs.getPropertyDefault("CharPosture")
oCurs.CharPosture = v
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
 
Posts: 29707
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby antalk » Fri Apr 30, 2021 3:01 pm

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.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
antalk
 
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

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

Postby JeJe » Fri Apr 30, 2021 3:32 pm

for the charstylename try

Code: Select all   Expand viewCollapse view
if tmp = "" then tmp ="Default"
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1533
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby antalk » Fri Apr 30, 2021 5:11 pm

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.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
antalk
 
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests