OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[SOLVED][python] Opening a basic dialog embedded in a file

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
prShadoko
General User
General User


Joined: 11 Jun 2012
Posts: 5
Location: France

PostPosted: Mon Jun 11, 2012 10:36 am    Post subject: [SOLVED][python] Opening a basic dialog embedded in a file Reply with quote

Hello everyone,
Here is my problem :
I created a dialog with the dialog designer of OOo, and I want to open it from a python script.
I already tried the following code
Code:
import uno, unohelper

 psm = uno.getComponentContext().ServiceManager
 dp = psm.createInstance("com.sun.star.awt.DialogProvider")
 dlg = dp.createDialog("vnd.sun.star.script:Standard.myDialog?location=application")

 dlg.execute()

Taken from here, but it only works if the dialog is in "My Macros and Dialogs" and I want mine to be embedded in my calc document. How can I access it ?

Thanks for your help.


Last edited by prShadoko on Tue Jun 12, 2012 3:39 am; edited 1 time in total
Back to top
View user's profile Send private message
karolus
OOo Advocate
OOo Advocate


Joined: 22 Jun 2011
Posts: 208

PostPosted: Mon Jun 11, 2012 2:20 pm    Post subject: Reply with quote

Hallo
Not testet yet, but i think it should work if you change:
dlg = dp.createDialog("vnd.sun.star.script:Standard.myDialog?location=document")

and embed the Pythonscript in the Document too.

Karo
Back to top
View user's profile Send private message
prShadoko
General User
General User


Joined: 11 Jun 2012
Posts: 5
Location: France

PostPosted: Tue Jun 12, 2012 1:52 am    Post subject: Reply with quote

Thanks for your answer karolus.
Unfortunately I already tried to use
Code:
dlg = dp.createDialog("vnd.sun.star.script:Standard.myDialog?location=document")

or
Code:
dlg = dp.createDialog("vnd.sun.star.script:Standard.myDialog?location=user")
but I still get an error.
(I don't remember where I found the code which gave me the idea but it was for calling a function from a python script)
I also tried things like
Code:
dlg = dp.createDialog("vnd.sun.star.script:Standard.myDialog?location=my_file.ods")
and
Code:
dlg = dp.createDialog("vnd.sun.star.script:my_file.ods.Standard.myDialog?location=application")

but it didn't do the trick...
Any other idea ?
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Tue Jun 12, 2012 3:11 am    Post subject: Reply with quote

Quote:
Any other idea ?

Code:
def create_dialog2(ctx, doc, lib_name, dialog_name):
    dialog_lib = doc.DialogLibraries
    if not dialog_lib.isLibraryLoaded(lib_name):
        dialog_lib.loadLibrary(lib_name)
    lib = dialog_lib.getByName(lib_name)
    input_provider = lib.getByName(dialog_name)
    return ctx.getServiceManager().createInstanceWithArgumentsAndContext(
                "com.sun.star.awt.DialogProvider",
                (None, input_provider.createInputStream(), lib, None),
                ctx).createDialog("")


Last edited by hanya on Tue Jun 12, 2012 4:25 am; edited 1 time in total
Back to top
View user's profile Send private message
prShadoko
General User
General User


Joined: 11 Jun 2012
Posts: 5
Location: France

PostPosted: Tue Jun 12, 2012 3:38 am    Post subject: Reply with quote

Thanks a lot hanya ! It works ! Very Happy
I guess you took this code from a class, so I had to remove the 'self.' in the last line, but it opens my dialog correctly.
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Tue Jun 12, 2012 4:26 am    Post subject: Reply with quote

prShadoko wrote:
Thanks a lot hanya ! It works ! :D
I guess you took this code from a class, so I had to remove the 'self.' in the last line, but it opens my dialog correctly.
Sorry, fixed in my code written above.
Back to top
View user's profile Send private message
prShadoko
General User
General User


Joined: 11 Jun 2012
Posts: 5
Location: France

PostPosted: Wed Jun 13, 2012 8:29 am    Post subject: Reply with quote

Would you mind if I ask you some explanation about this line ? :
Code:
(None, input_provider.createInputStream(), lib, None),


Because, the API reference onlys says it's "Arguments" (here) but I would like to know where I can find what these should be. I did not find anything about these in the DialogProvider class reference either...

Currently I am trying to handle the controls events of my dialog box, and I suppose it has something to do with the XDialogEventHandler interface, but I still do not know how to use it.

If you can tell me where/how to find this kind of information, you would make me so happy ! Because I do not want to create a new topic everytime I am stuck... Confused
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Wed Jun 13, 2012 8:50 am    Post subject: Reply with quote

prShadoko wrote:
Would you mind if I ask you some explanation about this line ? :
Code:
(None, input_provider.createInputStream(), lib, None),

Initialization of the UNO component is not well described in the reference, so we need to see its source code. In this case there it is:
http://opengrok.libreoffice.org/xref/core/scripting/source/dlgprov/dlgprov.cxx#673
Passing 4 elements to initialize method is special case and it is used by basic library. So, my code tries to mimic the situation initialized by the dialog library.
Quote:
Currently I am trying to handle the controls events of my dialog box, and I suppose it has something to do with the XDialogEventHandler interface, but I still do not know how to use it.

css.awt.XDialogProvider2 interface is supported by the same component implementation with the component provides css.awt.XDialogProvider#createDialog method, see the above source line 786. So I think createDialogWithHandler method can be used to work with XDialogEventHandler interface (I have never tried the interface except for options dialog...).
You can assign listeners for your dialog controls by hands too.
Back to top
View user's profile Send private message
prShadoko
General User
General User


Joined: 11 Jun 2012
Posts: 5
Location: France

PostPosted: Thu Jun 14, 2012 2:35 am    Post subject: Reply with quote

Thanks A LOT !
That link was precisely was I was looking for. I think it will be much easier with this. Wink
Back to top
View user's profile Send private message
andkit
Newbie
Newbie


Joined: 09 Aug 2012
Posts: 1

PostPosted: Thu Aug 09, 2012 4:33 am    Post subject: Reply with quote

prShadoko wrote:
Thanks for your answer karolus.
Unfortunately I already tried to use
Code:
dlg = dp.createDialog("vnd.sun.star.script:Standard.myDialog?location=document")



In case anyone stumbles upon this again, the trick apears to be in the creation of the
dialog provider. At the bottom of this page: http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Scripting/Writing_Macros
is an example to create an embed dialog via it's url. Converting it from java to python my code looked like this:
Code:

def show_update_dlg(event):
    doc = XSCRIPTCONTEXT.getDocument()
    ctx = XSCRIPTCONTEXT.getComponentContext()
    sm = ctx.ServiceManager
    dp = sm.createInstanceWithArgumentsAndContext(
        "com.sun.star.awt.DialogProvider", (doc, ), ctx)
    dlg = dp.createDialog("vnd.sun.star.script:Standard.update_dlg?location=document")
    dlg.execute()
    dlg.dispose()


Might be a bit nicer than the above method using the input stream from the DialogLibrary if you want to create different dialogs embeded in the document
Back to top
View user's profile Send private message
malmeida
Newbie
Newbie


Joined: 21 Sep 2012
Posts: 1

PostPosted: Fri Sep 21, 2012 8:52 am    Post subject: Reply with quote

how can i execute a python function and get values from this dialog?

thaks
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Fri Sep 21, 2012 9:23 am    Post subject: Reply with quote

malmeida wrote:
how can i execute a python function and get values from this dialog?
Get control element from the resulting dialog object and set an instance as listener. Here is an example from my toys, see ButtonListener class and _init_ui method:
Code:
import uno
import unohelper
import xml.dom.minidom
import traceback

from com.sun.star.awt import XActionListener


class Viewer(object):
   
    DIALOG_URI = "vnd.sun.star.script:GOMI.Dialog1?location=application"
   
    TAG_ITEM = "item"
    TAG_PROP = "prop"
    TAG_VALUE = "value"
    ATTR_PATH = "oor:path"
    ATTR_NAME = "oor:name"
   
    class ButtonListener(unohelper.Base, XActionListener):
        def __init__(self, act):
            self.act = act
        def disposing(self, ev):
            self.act = None
        def actionPerformed(self, ev):
            try:
                self.act.button_pushed(ev.ActionCommand)
            except Exception, e:
                print(e)
                traceback.print_exc()
   
    def __init__(self, ctx):
        self.ctx = ctx
        self.dialog = None
        #self.nodes = {}
        self.items = None #{} # oor:path to [item nodes]
   
    def create_service(self, name):
        return self.ctx.getServiceManager().\
            createInstanceWithContext(name, self.ctx)
   
    def _destroy(self):
        self.dialog.dispose()
   
    def _init(self):
        url = self.create_service("com.sun.star.util.PathSubstitution").\
            substituteVariables("$(user)/registrymodifications.xcu", True)
        path = uno.fileUrlToSystemPath(url)
        self.dom = xml.dom.minidom.parse(path)
   
    def get(self, name):
        return self.dialog.getControl(name)
   
    def _init_ui(self):
        self.dialog = self.create_service(
            "com.sun.star.awt.DialogProvider").createDialog(self.DIALOG_URI)
        self.get("btn_read").addActionListener(self.ButtonListener(self))
        self.get("btn_read").setActionCommand("read")
        data_model = self.create_service("com.sun.star.awt.tree.MutableTreeDataModel")
        root = data_model.createNode("/", True)
        root.DataValue = "/"
        data_model.setRoot(root)
        self.get("tree").getModel().DataModel = data_model
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group