Kirigami2

SelectableLabel.qml
1/*
2 * SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
3 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4 * SPDX-FileCopyrightText: 2024 Akseli Lahtinen <akselmo@akselmo.dev>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9import QtQuick
10import org.kde.kirigami as Kirigami
11import QtQuick.Controls as QQC2
12import QtQuick.Templates as T
13
14/**
15 * @brief This is a label which supports text selection.
16 *
17 * The label uses TextEdit component, which is wrapped inside a Control component.
18 *
19 *
20 * Example usage:
21 * @code{.qml}
22 * Kirigami.SelectableLabel {
23 * text: "Label"
24 * }
25 * @endcode
26 *
27 * @see https://bugreports.qt.io/browse/QTBUG-14077
28 * @since org.kde.kirigami 2.20
29 * @since 5.95
30 * @inherit QtQuick.Control
31 */
32
33QQC2.Control {
34 id: root
35
36 //TODO KF7: Cleanup from unnecessary properties we dont need to expose for a label
37 activeFocusOnTab: false
38 padding: 0
39
40 property bool readOnly: true
41 property bool selectByMouse: true
42 property color color: Kirigami.Theme.textColor
43 property color selectedTextColor: Kirigami.Theme.highlightedTextColor
44 property color selectionColor: Kirigami.Theme.highlightColor
45 property string text
46 property var baseUrl
47 property var cursorShape
48 property var horizontalAlignment
49 property var textFormat: TextEdit.AutoText
50 property var verticalAlignment: TextEdit.AlignTop
51 property var wrapMode: TextEdit.WordWrap
52
53 readonly property bool canPaste: textEdit.canPaste
54 readonly property bool canRedo: textEdit.canRedo
55 readonly property bool canUndo: textEdit.canUndo
56 readonly property bool inputMethodComposing: textEdit.inputMethodComposing
57 readonly property int length: textEdit.length
58 readonly property int lineCount: textEdit.lineCount
59 readonly property int selectionEnd: textEdit.selectionEnd
60 readonly property int selectionStart: textEdit.selectionStart
61 readonly property real contentHeight: textEdit.contentHeight
62 readonly property real contentWidth: textEdit.contentWidth
63 readonly property string hoveredLink: textEdit.hoveredLink
64 readonly property string preeditText: textEdit.preeditText
65 readonly property string selectedText: textEdit.selectedText
66 readonly property var cursorRectangle: textEdit.cursorRectangle
67 readonly property var cursorSelection: textEdit.cursorSelection
68 readonly property var effectiveHorizontalAlignment: textEdit.effectiveHorizontalAlignment
69 readonly property var textDocument: textEdit.textDocument
71 signal clicked()
72 signal linkActivated(string link)
73 signal linkHovered(string link)
74
75 onLinkActivated: link => Qt.openUrlExternally(link)
76//BEGIN TextArea dummy entries
77 property var flickable: undefined
78 property var placeholderText: undefined
79 property var placeholderTextColor: undefined
80
81 signal pressAndHold(MouseEvent event)
82 signal pressed(MouseEvent event)
83 signal released(MouseEvent event)
84//END TextArea dummy entries
86//BEGIN TextEdit dummy entries
87 property var activeFocusOnPress: undefined
88 property var cursorDelegate: undefined
89 property var cursorPosition: undefined
90 property var cursorVisible: undefined
91 property var inputMethodHints: undefined
92 property var mouseSelectionMode: undefined
93 property var overwriteMode: undefined
94 property var persistentSelection: undefined
95 property var renderType: undefined
96 property var selectByKeyboard: undefined
97 property var tabStopDistance: undefined
98 property var textMargin: undefined
99
100 signal editingFinished()
101//END TextEdit dummy entries
102
103 contentItem: TextEdit {
104 id: textEdit
105
106 /**
107 * @brief This property holds the cursor shape that will appear whenever
108 * the mouse is hovering over the label.
110 * default: @c Qt.IBeamCursor
111 *
112 * @property Qt::CursorShape cursorShape
113 */
114 property alias cursorShape: hoverHandler.cursorShape
115
116 activeFocusOnTab: root.activeFocusOnTab
117 baseUrl: root.baseUrl
118 color: root.color
119 horizontalAlignment: root.horizontalAlignment
120 padding: 0
121 readOnly: root.readOnly
122 selectByMouse: root.selectByMouse
123 selectedTextColor: root.selectedTextColor
124 selectionColor: root.selectionColor
125 textFormat: root.textFormat
126 verticalAlignment: root.verticalAlignment
127 wrapMode: root.wrapMode
128
129 onLinkActivated: root.linkActivated(textEdit.hoveredLink)
130 onLinkHovered: root.linkHovered(textEdit.hoveredLink)
131
132 text: root.text
133
134 Accessible.selectableText: true
135 Accessible.editable: false
136 HoverHandler {
137 id: hoverHandler
138 // By default HoverHandler accepts the left button while it shouldn't accept anything,
139 // causing https://bugreports.qt.io/browse/QTBUG-106489.
140 // Qt.NoButton unfortunately is not a valid value for acceptedButtons.
141 // Disabling masks the problem, but
142 // there is no proper workaround other than an upstream fix
143 // See qqc2-desktop-style Label.qml
144 enabled: false
145 cursorShape: root.cursorShape ? root.cursorShape : (textEdit.hoveredLink ? Qt.PointingHandCursor : Qt.IBeamCursor)
146 }
147
148 TapHandler {
149 // For custom click actions we want selection to be turned off
150 enabled: !textEdit.selectByMouse
151
152 acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus
153 acceptedButtons: Qt.LeftButton
154
155 onTapped: root.clicked()
156 }
157
158 TapHandler {
159 enabled: textEdit.selectByMouse
160
161 acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus
162 acceptedButtons: Qt.RightButton
163
164 onTapped: {
165 contextMenu.popup();
166 }
167 }
168
169 QQC2.Menu {
170 id: contextMenu
171 QQC2.MenuItem {
172 action: T.Action {
173 icon.name: "edit-copy-symbolic"
174 text: qsTr("Copy")
175 shortcut: StandardKey.Copy
176 }
177 enabled: textEdit.selectedText.length > 0
178 onTriggered: {
179 textEdit.copy();
180 textEdit.deselect();
181 }
182 }
183 QQC2.MenuSeparator {}
184 QQC2.MenuItem {
185 action: T.Action {
186 icon.name: "edit-select-all-symbolic"
187 text: qsTr("Select All")
188 shortcut: StandardKey.SelectAll
189 }
190 onTriggered: {
191 textEdit.selectAll();
192 }
193 }
194 }
195 }
196}
const QList< QKeySequence > & shortcut(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:13:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.