Kirigami2

UrlButton.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 org.kde.kirigami as Kirigami
9import org.kde.kirigami.private as KirigamiPrivate
10import QtQuick.Controls as QQC2
11
12/**
13 * @brief A link button that contains a URL.
14 *
15 * It will open the url by default, allow to copy it if triggered with the
16 * secondary mouse button.
17 *
18 * @since 5.63
19 * @since org.kde.kirigami 2.6
20 * @inherit QtQuick.LinkButton
21 */
22Kirigami.LinkButton {
23 id: button
24
25 /**
26 * This property holds the url used by the link button.
27 */
28 property string url
29
30 /**
31 * This property holds whether the url is an external link.
32 *
33 * External links will have a small icon on their right to show that the link goes to an external website.
34 *
35 * default: true
36 * @since 6.11
37 */
38 property bool externalLink: true
39
40 text: url
41 enabled: url.length > 0
42 visible: text.length > 0
43 acceptedButtons: Qt.LeftButton | Qt.RightButton
44
45 Accessible.name: text
46 Accessible.description: text !== url
47 ? qsTr("Open link %1", "@info:whatsthis").arg(url)
48 : qsTr("Open link", "@info:whatsthis")
49
50 rightPadding: LayoutMirroring.enabled || !icon.visible ? 0 : icon.size + Kirigami.Units.smallSpacing
51 leftPadding: LayoutMirroring.enabled && icon.visible ? icon.size + Kirigami.Units.smallSpacing : 0
52
53 Kirigami.Icon {
54 id: icon
55
56 readonly property int size: Kirigami.Units.iconSizes.sizeForLabels
57
58 x: LayoutMirroring.enabled ? button.width - button.implicitWidth : button.implicitWidth - size
59 width: size
60 height: size
61
62 visible: button.externalLink
63
64 source: "open-link-symbolic"
65 fallback: "link-symbolic"
66 color: button.color
67
68 anchors.verticalCenter: button.verticalCenter
69 }
70
71 onPressed: mouse => {
72 if (mouse.button === Qt.RightButton) {
73 menu.popup();
74 }
75 }
76
77 onClicked: mouse => {
78 if (mouse.button !== Qt.RightButton) {
79 Qt.openUrlExternally(url);
80 }
81 }
82
83 QQC2.ToolTip {
84 // If button's text has been overridden, show a tooltip to expose the raw URL
85 visible: button.text !== button.url && button.mouseArea.containsMouse
86 text: button.url
87 }
88
89 QQC2.Menu {
90 id: menu
91 QQC2.MenuItem {
92 text: qsTr("Copy Link to Clipboard")
93 icon.name: "edit-copy"
94 onClicked: KirigamiPrivate.CopyHelperPrivate.copyTextToClipboard(button.url)
95 }
96 }
97}
Class for rendering an icon in UI.
Definition icon.h:35
string url
This property holds the url used by the link button.
Definition UrlButton.qml:25
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:51:21 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.