Search code examples
qmltooltipkde-plasmaplasmoid

How to show/hide KDE plasmoid's tooltip programatically?


Is there a way to make a plasmoid tooltip to show/hide programatically?

I tried by setting a ToolTipArea over the compact representation, and trying to trigger it with a Timer - it does not work (the regular tooltip keeps showing but only when hovering the plasmoid icon (aka compactRepresentation):

import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PlasmaComponents
import org.kde.plasma.extras 2.0 as PlasmaExtras

Item {
    Layout.preferredWidth: 200
    Layout.preferredHeight: 300

    Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation

    Plasmoid.compactRepresentation: Item
    {
        anchors.fill: parent

        MouseArea
        {
            onClicked:
            {
                 plasmoid.expanded = !plasmoid.expanded
            }
        }

        PlasmaCore.ToolTipArea
        {
            id: toolTip
            width: parent.width
            height: parent.height
            anchors.fill: parent
            mainItem: tooltipContentItem
            active: false
            interactive: true
        }

        Timer
        {
            interval: 3000
            running: true
            repeat: true
            onTriggered:
            {
                if (tooltipContentItem.active == false)
                {
                    toolTip.showToolTip()
                    toolTip.active == true
                }

                else
                {
                    toolTip.hideToolTip()
                    toolTip.active == false
                }
            }
        }
    }

    Item
    {
        id: tooltipContentItem

        implicitWidth: 300
        implicitHeight: 200

        ColumnLayout
        {
            id: mainLayout
            anchors
            {
                left: parent.left
                top: parent.top
                margins: PlasmaCore.Units.gridUnit / 2
            }

            PlasmaExtras.Heading
            {
                id: tooltipMaintext
                level: 3
                Layout.minimumWidth: Math.min(implicitWidth, preferredTextWidth)
                Layout.maximumWidth: preferredTextWidth
                elide: Text.ElideRight
                text: "Test"
            }

            PlasmaComponents.Label
            {
                id: tooltipSubtext
                Layout.minimumWidth: Math.min(implicitWidth, preferredTextWidth)
                Layout.maximumWidth: preferredTextWidth
                text: "Testing text"
                opacity: 0.6
            }
        }
    }
}

There's the toolTipItem QQuickItem too, but I cannot figure out if it is possible to make it show or hide on command (this bit was borrowed from KDE's digital-clock plasmoid:

import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PlasmaComponents
import org.kde.plasma.extras 2.0 as PlasmaExtras

Item {
    Layout.preferredWidth: 200
    Layout.preferredHeight: 300

    Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation

    Plasmoid.compactRepresentation: Item
    {
        anchors.fill: parent

        MouseArea
        {
            onClicked:
            {
                 plasmoid.expanded = !plasmoid.expanded
            }
        }
    }

    plasmoid.toolTipItem: Loader
    {
        id: toolTipLoader
        source: "Tooltip.qml" // Just holds the tooltip contents
    }
}

Solution

  • After giving up and forgetting about this, @RokeJulianLockhart made me rembember this and could find a time to give this another try - this time fortunately I could figure it out.

    To sum up, setting the active property to false prevents the tooltip to show or hide automatically - it seems to take over the control of the visibility of the tooltip wether with the mouse cursor or programatically.

    After a few touches, this code toggles the visibility of the tooltip of the plasmoid every three seconds:

    import QtQuick 2.0
    import QtQuick.Layouts 1.1
    import org.kde.plasma.plasmoid 2.0
    import org.kde.plasma.core 2.0 as PlasmaCore
    import org.kde.plasma.components 3.0 as PlasmaComponents
    import org.kde.plasma.extras 2.0 as PlasmaExtras
    
    Item {
        Layout.preferredWidth: 200
        Layout.preferredHeight: 300
    
        Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
    
        Plasmoid.compactRepresentation: Item
        {
            anchors.fill: parent
    
            MouseArea
            {
                onClicked:
                {
                     plasmoid.expanded = !plasmoid.expanded
                }
            }
    
            PlasmaCore.ToolTipArea
            {
                id: toolTip
                width: parent.width
                height: parent.height
                anchors.fill: parent
                mainItem: tooltipContentItem
                active: true // Or just omit it, it's set to `true` by default
                interactive: true
            }
    
            Timer
            {
                property bool expanded: false
                interval: 3000
                running: true
                repeat: true
                onTriggered:
                {
                    if (expanded == false)
                    {
                        toolTip.showToolTip()
                        expanded = true
                        console.log('AAAAA')
                    }
    
                    else
                    {
                        toolTip.hideImmediately()
                        expanded = false
                        console.log('BBBB')
                    }
                }
            }
        }
    
        Item
        {
            id: tooltipContentItem
    
            implicitWidth: 300
            implicitHeight: 200
    
            ColumnLayout
            {
                id: mainLayout
                anchors
                {
                    left: parent.left
                    top: parent.top
                    margins: PlasmaCore.Units.gridUnit / 2
                }
    
                PlasmaExtras.Heading
                {
                    id: tooltipMaintext
                    level: 3
                    Layout.minimumWidth: Math.min(implicitWidth, preferredTextWidth)
                    Layout.maximumWidth: preferredTextWidth
                    elide: Text.ElideRight
                    text: "Test"
                }
    
                PlasmaComponents.Label
                {
                    id: tooltipSubtext
                    Layout.minimumWidth: Math.min(implicitWidth, preferredTextWidth)
                    Layout.maximumWidth: preferredTextWidth
                    text: "Testing text"
                    opacity: 0.6
                }
            }
        }
    }