Kirigami2

LinkButton.qml
1/*
2 * SPDX-FileCopyrightText: 2018 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick
8import QtQuick.Controls as QQC2
9import QtQuick.Templates as T
10import org.kde.kirigami as Kirigami
11
12/**
13 * @brief A button that looks like a link.
14 *
15 * It uses the link color settings and triggers an action when clicked.
16 *
17 * Maps to the Command Link in the HIG:
18 * https://develop.kde.org/hig/components/navigation/commandlink/
19 *
20 * @since 5.52
21 * @since org.kde.kirigami 2.6
22 * @inherit QtQuick.Controls.Label
23 */
24QQC2.Label {
25 id: control
26
27 property T.Action action
28
29 /**
30 * @brief This property holds the mouse buttons that the mouse area reacts to.
31 * @see QtQuick.MouseArea::acceptedButtons
32 * @property Qt::MouseButtons acceptedButtons
33 */
34 property alias acceptedButtons: area.acceptedButtons
35
36 /**
37 * @brief This property holds the mouse area element covering the button.
38 * @property MouseArea area
39 */
40 property alias mouseArea: area
41
42 /**
43 * @brief This property holds the normal color of the link when not pressed
44 * or disabled.
45 *
46 * default: Kirigami.Theme.linkColor
47 *
48 * @property color normalColor
49 */
50 property color normalColor: Kirigami.Theme.linkColor
51
52 /**
53 * @brief This property holds the color of the link while pressed.
54 *
55 * default: Whatever the normal color is set to, but 200% darker
56 *
57 * @property color pressedColor
58 */
59 property color pressedColor: Qt.darker(normalColor)
60
61 /**
62 * @brief This property holds the color of the link when disabled.
63 *
64 * default: Kirigami.Theme.textColor
65 *
66 * @property color disabledColor
67 */
68 property color disabledColor: Kirigami.Theme.textColor
69
70 activeFocusOnTab: true
71 Accessible.role: Accessible.Button
72 Accessible.name: text
73 Accessible.onPressAction: clicked({ button: Qt.LeftButton })
74
75 text: action?.text ?? ""
76 enabled: action?.enabled ?? true
77
78 onClicked: action?.trigger()
79
80 font.bold: activeFocus
81 font.underline: enabled
82 color: if (!enabled) {
83 return control.disabledColor;
84 } else if (area.containsPress) {
85 return control.pressedColor;
86 } else {
87 return control.normalColor;
88 }
89 horizontalAlignment: Text.AlignHCenter
90 verticalAlignment: Text.AlignVCenter
91 elide: Text.ElideRight
92
93 signal pressed(var mouse)
94 signal clicked(var mouse)
95
96 Keys.onPressed: event => {
97 switch (event.key) {
98 case Qt.Key_Space:
99 case Qt.Key_Enter:
100 case Qt.Key_Return:
101 case Qt.Key_Select:
102 control.clicked({ button: Qt.LeftButton });
103 event.accepted = true;
104 break;
105 case Qt.Key_Menu:
106 control.pressed({ button: Qt.RightButton });
107 event.accepted = true;
108 break;
109 }
110 }
111
112 MouseArea {
113 id: area
114 anchors.fill: parent
115 hoverEnabled: true
116 cursorShape: Qt.PointingHandCursor
117
118 onClicked: mouse => control.clicked(mouse)
119 onPressed: mouse => control.pressed(mouse)
120 }
121}
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:47:53 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.