| View previous topic :: View next topic |
| Author |
Message |
prShadoko General User

Joined: 11 Jun 2012 Posts: 5 Location: France
|
Posted: Mon Jun 11, 2012 10:36 am Post subject: [SOLVED][python] Opening a basic dialog embedded in a file |
|
|
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 |
|
 |
karolus OOo Advocate

Joined: 22 Jun 2011 Posts: 208
|
Posted: Mon Jun 11, 2012 2:20 pm Post subject: |
|
|
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 |
|
 |
prShadoko General User

Joined: 11 Jun 2012 Posts: 5 Location: France
|
Posted: Tue Jun 12, 2012 1:52 am Post subject: |
|
|
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 |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Tue Jun 12, 2012 3:11 am Post subject: |
|
|
| 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 |
|
 |
prShadoko General User

Joined: 11 Jun 2012 Posts: 5 Location: France
|
Posted: Tue Jun 12, 2012 3:38 am Post subject: |
|
|
Thanks a lot hanya ! It works !
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 |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Tue Jun 12, 2012 4:26 am Post subject: |
|
|
| 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 |
|
 |
prShadoko General User

Joined: 11 Jun 2012 Posts: 5 Location: France
|
Posted: Wed Jun 13, 2012 8:29 am Post subject: |
|
|
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...  |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Wed Jun 13, 2012 8:50 am Post subject: |
|
|
| 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 |
|
 |
prShadoko General User

Joined: 11 Jun 2012 Posts: 5 Location: France
|
Posted: Thu Jun 14, 2012 2:35 am Post subject: |
|
|
Thanks A LOT !
That link was precisely was I was looking for. I think it will be much easier with this.  |
|
| Back to top |
|
 |
andkit Newbie

Joined: 09 Aug 2012 Posts: 1
|
Posted: Thu Aug 09, 2012 4:33 am Post subject: |
|
|
| 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 |
|
 |
malmeida Newbie

Joined: 21 Sep 2012 Posts: 1
|
Posted: Fri Sep 21, 2012 8:52 am Post subject: |
|
|
how can i execute a python function and get values from this dialog?
thaks |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Fri Sep 21, 2012 9:23 am Post subject: |
|
|
| 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 |
|
 |
|