Script or other way to click on an image in a database form and open a full screen view of that image

Hi all,
I have a plant database that includes several images of parts of each species. I would like users to be able to click on an image (or a control adjacent to it) and open up a full screen view of that image. I’m open to using a script, if anyone knows of one.
Thanks

Have written this for the German Base Handbuch, also available in English Base Guide: Base Guide - Database Tasks: Displaying linked images and documents

It won’t work with click on the image, because this will execute to choose a new image. You will need a separate button for executing.

Thanks Robert

Does the macro need to be installed on every LibreOffice install that uses the database?

It has to be available, to be called to run.

  • You can include the macro in the .odb file, so every install will get it.
  • If you prefer to have it in central “my macros” (residing in user-profile) you have to copy it to every installation, where you want to use this. But you can use it from more than the original .odb

Thank you Wanderer. Now to install it and make it work. :slightly_frowning_face:

  • Open the database file.
  • Tools → Macros → Organize Macros → Basic…
  • Click on the database name on the left. Might be it is the last entry.
  • Click on the folder “Default” (“Standard”) of the database file and then to “New”.
  • New module “Module1” will be created with a defined procedure SUB Main

Now there are 2 macros in the source I have linked in the first post. If you won’t save the images inside the database and will only save the path to the images it will be the first solution:

SUB ViewImage(oEvent AS OBJECT)
 DIM oDoc AS OBJECT
 DIM oForm AS OBJECT
 DIM oField AS OBJECT
 DIM oShell AS OBJECT
 DIM stUrl AS STRING
 DIM stField AS STRING
 DIM arUrl_Start()
 oDoc = thisComponent
 oForm = oEvent.Source.Model.Parent
 oField = oForm.getByName("NameOfGraphicControl")
 stUrl = oField.BoundField.getString
 arUrl_Start = split(oDoc.Parent.Url,right(convertToUrl(oDoc.Parent.Title),
len(convertToUrl(oDoc.Parent.Title))-8))
 oShell = createUnoService("com.sun.star.system.SystemShellExecute")
 stField = convertToUrl(arUrl_Start(0) + stUrl)
 oShell.execute(stField,"",0)
END SUB

You have to create a button in the form of the graphic control, which shows the (little) images.

  • Push Button → Control Properties → Events → Execute Action
  • Press on the button with the three dots and search for the macro “ViewImage”.

If you have saved the images inside the database (not so good, will blow up the database file…) you have to use the separate macro code, which will extract the image from the database to a separate file and then will open the image with the application, which is linked to images on your system.

I’m confused about the format stField must have :thinking:
Event = click on the image.
DB: /home/crdf/EstudosDBs/AbrirFormulario.odb
picture: /home/crdf/Pictures/Adrar.jpg
 


ImageCode

When creating a database and linking pictures in the database I prefer to use a subfolder of the database folder. So I get relative links, not absolute links like you got.
.
stUrl looks here like Icons/MyIcon.png or directly in the folder of the database MyIcon.png. When trying to get a picture in another folder I get ../../../../../../Bilder/back.jpg
.
With absolute path stField = convertToUrl(stUrl) would be enough.
.


Have a look at the default values for saving here: Save URLs relative to file system. Might be you have unchecked this option.

:+1:
Now it opens up (via OS default) the full screen view as thread asks for.

I think this applies to inserting the photos via click on form’s image control and an OpenFileDialog/File Chooser. I inserted the string manually in table’s field.
Will check this another time…
 
EDIT
Save Urls relative to file system is the default checked option here in my LO.
And yes:

  1. created a ImagesBank subfolder in folder where the DBs are;
  2. inserted the photos via click on the ImageControl > File Chooser > Open;

and (of course) the code with relative path works fine: OS’s default Image Viewer opens up.

Alternative approach for a push button opening some form field’s URL. Works with any protocol or file type that can be handled by the system. The macro converts a push button into a URL button with the current record’s specified field as URL. A little bit of additional SQL is needed when the specified field does not contain absolute URLs.

Sub URLButton_Approve(oEv)
    openURL(oEv, False)
End Sub

Sub FileButton_Approve(oEv)
    openURL(oEv, True)
End Sub

Sub openURL(oEv, bConvertToURL)
    oModel = oEv.Source.getModel()
    oModel.ButtonType = com.sun.star.form.FormButtonType.URL
    oForm = oModel.getParent()
    oColumn = oForm.Columns.getByName(oModel.Tag)
    if bConvertToURL then
        sURL = convertToUrl(oColumn.getString())
    else
        sURL = oColumn.getString()
    endif
    oModel.TargetURL = sURL
End Sub

The button needs to be assigned to event “Approve action”.
The button’s “Additional info” property gets the column name from where to read the URL.
For emails, add a simple concatenation to the form source adding the mailto: protocol prefix.
Same with arbitrary files: SELECT *, 'C:\absolute\path\'|| "FileName" AS "PathName" FROM "TBL". Use this query as form source, use “PathName” as “Additional info” and assign “Approve action” to routine URLButton_Approve which does the conversion from sys path to URL.

This doesn’t sound as though it would work for multiple images in the same record

Maybe I’m just completely ignorant how to set things up, but I replaced the “stField = convertToUrl(arUrl_Start(0) + stUrl)” in the script from the book with “stField = convertToUrl(stUrl)”; now when I click on the push button, I don’t get an error message, but I don’t get an image window opening either. Nothing at all. XnView MP is the program associated with PNG and JPG files on my system, and just clicking on a file through WinExplorer opens them just fine.

First of all you must see via a print or MsgBox if stField is :ok:
That is: if it describes the path to the image.
Apparently it is an absolute one = images not stored in sub folder of your DB’s folder.
You can take a print screen of it and upload here.
 
And also take notice that you don’t need a button. You can just click on the image as you proposed. I think a much more elegant solution :thinking:

If you try it by click on the image it will open the viewer. If you won’t change the image it will be OK, but if you try to change the content (or input new content) it will start the viewer after you have chosen a new image 2 times.
Try this:

SUB ViewDirect(oEvent AS OBJECT)
	DIM oDoc AS OBJECT, oShell AS OBJECT
	DIM stUrl AS STring, stField AS STRING
	DIM arUrlStart()
	IF oEvent.Modifiers = 2 THEN
		'KeyModifier (ohne: 0 | Shift: 1 | Ctrl: 2 | Alt: 4 ...), Typen in com.sun.star.awt.KeyModifier
		oDoc = thisComponent
		stUrl = oEvent.Source.Model.BoundField.Value
		IF stUrl <> ""
			arUrl_Start = split(oDoc.Parent.Url,oDoc.Parent.Title)
			oShell = createUnoService("com.sun.star.system.SystemShellExecute")
			stField = convertToUrl(arUrl_Start(0) + stUrl)
			oShell.execute(stField,"",0)
		END IF
	END IF
END SUB

You have to press Ctrl together with click of the mouse so the macro will open the image in the viewer. No Button is needed.
Connect the macro to Properties: Image Control → Events → Mouse Button pressed

It works with one button per URL column.

It is a very subtile difference.

  • If one inserts string typing into the table field the absolute path to image, as I did at first, just convertToUrl is :ok:
  • If insertion is made via the Form clicking into the ImageControl, as @RobertG explains, and images are stored in a sub folder of the folder where DB is, it will be persisted (in the table) the relative path.
    In this so to speak “standard” way to do,
arUrl_Start = split(oDoc.Parent.Url,oDoc.Parent.Title)
stField = convertToUrl(arUrl_Start(0) + stUrl)

is needed to supply the absolute path to OS’s default viewer.