[Solved] Looking for snippet on dialogs and libraries

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

[Solved] Looking for snippet on dialogs and libraries

Post by jza »

I am trying to complete some documentation for PyUNO regarding dialogs. I want to clear some doubts I came up with. Usually AOO relies on the Dialog designer, and base will have scripts to invoke these libraries, like the following:

Code: Select all

 Sub MyDialog()
 Dim oDialog As Object
     DialogLibraries.LoadLibrary( "Standard" )
     oDialog = CreateUnoDialog( DialogLibraries.Standard.getByName("myDialog") )
     oDialog.execute()
     oDialog.dispose()
 End Sub
However in Python, most examples assume there is no such dialog and are built from scratch, example:

Code: Select all

 def MyDialog(self, value, label=''):
     """ Shows value as a message with label. """
     if not self.dlg_info:
        dialog = self.smgr.createInstanceWithContext( 
             'com.sun.star.awt.UnoControlDialog',self.ctx)
        dialog_model = self.smgr.createInstanceWithContext( 
             'com.sun.star.awt.UnoControlDialogModel',self.ctx)
         dialog_model.setPropertyValues( 
              ('Height','PositionX','PositionY','Width',), 
              (136,30,50,176,) )
I would like to get some further verification and snippets if it's possible to invoke the CreateUnoDialog() service from PyUNO and iterate through the XDL files. Also if there is some IDL that will serialize the xdl fileformat for a faster way to access the objects through the code.
Last edited by jza on Fri Aug 30, 2013 12:22 pm, edited 1 time in total.
AOO 4.1.1 on Arch Linux
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Looking for snippet on dialogs and libraries

Post by Villeroy »

http://forum.openoffice.org/en/forum/vi ... og#p248042

This is a perfect example of non-documentation: The reference http://www.openoffice.org/api/docs/comm ... vider.html is useless without further internet research on a valid URL scheme.
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
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

Re: [Solved] Looking for snippet on dialogs and libraries

Post by jza »

Althought I already got what I want, I want to get some quesitons regarding my current snippet. The one shown on the example has a lot of stuff that I would like to gradually add to the guide I am working on. That means I need to go backwards, and so far I have been succesful but I want to get a better understanding of how the code is builted so I can explain it better without need to get the reader confused.

The current code is the createDialog function on this snippet.

I stripped to this:

Code: Select all

import uno
import unohelper

def createDialog():
    """Creamos un dialogo y le insertamos los parametros de Posicion, altura y anchura, asi como titulo"""
    try:
        ctx = uno.getComponentContext()
        smgr = ctx.ServiceManager
        dialogModel = smgr.createInstanceWithContext( 
            "com.sun.star.awt.UnoControlDialogModel", ctx)
 
        controlContainer = smgr.createInstanceWithContext( 
            "com.sun.star.awt.UnoControlDialog", ctx); 
        controlContainer.setModel(dialogModel);
        toolkit = smgr.createInstanceWithContext( 
            "com.sun.star.awt.ExtToolkit", ctx);       
 
        controlContainer.setVisible(False);       
        controlContainer.createPeer(toolkit, None);
        controlContainer.execute()
        controlContainer.dispose()
    except Exception,e:
        print str(e)
 
g_exportedScripts = createDialog,
My question is what does the UnoControlDialog is necessary for the dialogModel (UnoControlDialogModel) to show up. Basically I am trying to do something similar to this video, where they start from the very basic, and then build up to a more complex GUI with more services and controls.

I also want to understand better the different methods to start running a GUI on PyUNO, for example, I notice, only uno and unohelper where needed while on a different script (Paolo Montovani's info dialog) he uses a series of AWT module subcomponents like Rectangle, PosSize, WindowClass and VclWindowPeerAttribute. Althought what I understand he is just calling what he is going to use, I also want to understand what is needed on each step of the way.
AOO 4.1.1 on Arch Linux
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Looking for snippet on dialogs and libraries

Post by Villeroy »

The dialog model (like any of the UnoControl...Models) is the blue print as you see it in the graphical dialog designer. It is a collection of properties which can be written in some XML file. The dialog you create from this blue print is a "living window" with lots of methods. And you can build many dialogs from one dialog model.

In your daily coding you don't need any ControlContainer. Every dialog includes UnoControlContainer. Several years ago I did some experiments with independent ControlContainers but that is too advanced for a tutorial. The container provides a subset of a dialogs methods and properties. That's all.

For dynamic dialogs I use to throw ingredients into a pot (attach control models with labels and contents to a DialogModel).
Then I createUnoDialog from this model, calculate the PositionSizes of the labeled elements and call a routine to arrange the controls with some space between them. Finally I setVisible the whole thing. I find this easier to do than fiddling with the graphical designer.
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
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

Re: [Solved] Looking for snippet on dialogs and libraries

Post by jza »

Thanks for your help, I have minimized to what I think is the bare minimum.

Code: Select all

import uno
def miDialogo():
    ''' Creamos un dialogo en blanco usando la interfaz awt'''
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    oDM = smgr.createInstance("com.sun.star.awt.UnoControlDialogModel")
    oDialog = smgr.createInstance("com.sun.star.awt.UnoControlDialog")
    oDialog.setModel(oDM)
    oDialog.setVisible(True)
    oDialog.execute()
AOO 4.1.1 on Arch Linux
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Looking for snippet on dialogs and libraries

Post by Villeroy »

Right, this is the bare minimum. The following is a helper routine to add a control model to a dialog model passing a dictionary of properties:

Code: Select all

def addAwtModel(oDM,srv,sName,dProps):
    oCM = oDM.createInstance("com.sun.star.awt.UnoControl"+ srv +"Model")
    while dProps:
        prp = dProps.popitem()
        uno.invoke(oCM,"setPropertyValue",(prp[0],prp[1]))
        #works with awt.UnoControlDialogElement only:
        oCM.Name = sName
    oDM.insertByName(sName,oCM)
Building a DialogModel for a complete log-in dialog with label, name box, password box, OK button and Cancel button:

Code: Select all

def getLogin():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    oDM = smgr.createInstance("com.sun.star.awt.UnoControlDialogModel")
    oDM.Title = 'Login Dialog'
    addAwtModel(oDM,'FixedText','lblName',{
        'Label' : 'User Name',
        }
                )
    addAwtModel(oDM,'Edit','txtName',{})
    addAwtModel(oDM,'FixedText','lblPWD',{
        'Label' : 'Password',
        }
                )
    addAwtModel(oDM,'Edit','txtPWD',{
        'EchoChar' : 42,
        }
                )
    addAwtModel(oDM,'Button','btnOK',{
        'Label' : 'OK',
        'DefaultButton' : True,
        'PushButtonType' : 1,
        }
                )
    addAwtModel(oDM,'Button','btnCancel',{
        'Label' : 'Cancel',
        'PushButtonType' : 2,
        }
                )
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
Post Reply