Ask Your Question
0

Inserting image in Macro is not undoable

asked 2015-09-14 13:09:02 +0200

jdm gravatar image

I'm trying to write a macro that inserts images. My problem is that the insertion does not appear in the undo stack, so the user cannot undo it. I'm doing something like (in Python):

document = XSCRIPTCONTEXT.getDocument()
page = document.getCurrentController().getCurrentPage()

graphic = document.createInstance('com.sun.star.drawing.GraphicObjectShape')
graphic.setSize(Size(3000,2000))
graphic.setPosition(Point(1000,1000))
page.add(graphic)

Ultimately, I'd like to wrap this into an undo context, so there is only one undo step for the whole macro (which inserts multiple images). Any ideas how to get this to appear on the undo stack?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2017-08-26 01:19:24 +0200

librebel gravatar image

Hello @jdm,

you could use the Document UndoManager for defining your Undo Context. Like so:

# This works for a Writer Document:
def add5Graphics():
    oDesktop = XSCRIPTCONTEXT.getDesktop()
    oDoc = oDesktop.getCurrentComponent()
    if hasattr( oDoc, "Text" ):
        oDrawPage = oDoc.getDrawPage()

        oUndoManager = oDoc.getUndoManager()
        oUndoManager.enterUndoContext( "Add Five Graphic Objects" )

        from com.sun.star.awt import Size
        from com.sun.star.awt import Point

        for i in range(5):
            oGraphic = oDoc.createInstance( "com.sun.star.drawing.GraphicObjectShape" )
            oGraphic.setSize( Size(3000,2000) )
            oGraphic.setPosition( Point( i*3000, 1000 ) )
            oDrawPage.add( oGraphic )

        oUndoManager.leaveUndoContext()
edit flag offensive delete link more
0

answered 2015-10-05 21:33:43 +0200

Alex Kemp gravatar image
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-09-14 13:09:02 +0200

Seen: 440 times

Last updated: Aug 26 '17