Manipulate object properties by layer
We want to be able to show or hide objects by clicking a button.
The below screenshot shows an example where clicking on the buttons "Squares" or "Circles" shows or hides the respective objects.
LibreOffice Draw permits the defining of layers in a given draw page. The use of layers enable us to access objects exclusive to some particular layer. Think of layers like different available workspaces which you can handle individually, such as their visibility or whether they should be locked or left out when printing, etc. By default, Draw proposes three layers (which can be seen at the bottom toolbar of the interface):
- Dimension Lines
We will create two more layers manually to be able to handle objects by layer:
- The squares will be created in the layer called Squares.
- The circles will be created in the layer called Circles.
Every layer has a Boolean property called IsVisible. The below macro toggles this property between True and False.
A unique macro is actuated by a the event Execute action for each button. The variable oEvent is passed as an argument to the macro which runs when button is clicked. This can be set by creating a Push Button, and right-click and select Control Properties, and finally in Events you can select a macro for the Execute action item.
From this we can trace the Label property of the button and find loop through the layer to see if we find an object of the same name. Another option is to use the Tag property to store the name of the layer in question.
The code also forsees a possible error by verifying that the document really has a layer by that name, by investigating the hasByName property.
In LibreOffice Basic:
Option Explicit Sub toggle_layer_visibility (oEvent) '''Triggered by button-clicked event, toggles the visibility of the layer indicated by the button''' Dim oLayerManager As Object ' com.sun.star.drawing.LayerManager oLayerManager = ThisComponent.LayerManager If oLayerManager.hasByName(oEvent.source.model.Label) Then oLayerManager.getByName(oEvent.source.model.Label).IsVisible = _ Not( oLayerManager.getByName(oEvent.source.model.Label).IsVisible ) Else MsgBox "There exists no layer called " & _ CHR$(34) & oEvent.source.model.Label & CHR$(34), _ 48, "toggle_layer_visibility()" End If End Sub ' toggle_layer_visibility