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
39 padding: undefined
40 topPadding: undefined
41 leftPadding: undefined
42 rightPadding: undefined
43 bottomPadding: undefined
44
45 Accessible.name: textEdit.text
46 Accessible.role: Accessible.StaticText
47 Accessible.selectableText: true
48 Accessible.editable: false
49
50 property alias readOnly: textEdit.readOnly
51 property alias selectByMouse: textEdit.selectByMouse
52 property alias color: textEdit.color
53 property alias selectedTextColor: textEdit.selectedTextColor
54 property alias selectionColor: textEdit.selectionColor
55 property alias text: textEdit.text
56 property alias baseUrl: textEdit.baseUrl
57 property var cursorShape
58 property alias horizontalAlignment: textEdit.horizontalAlignment
59 property alias verticalAlignment: textEdit.verticalAlignment
60 property alias textFormat: textEdit.textFormat
61 property alias wrapMode: textEdit.wrapMode
62
63 property alias activeFocusOnPress: textEdit.activeFocusOnPress
64 property alias cursorDelegate: textEdit.cursorDelegate
65 property alias cursorPosition: textEdit.cursorPosition
66 property alias cursorVisible: textEdit.cursorVisible
67 property alias inputMethodHints: textEdit.inputMethodHints
68 property alias mouseSelectionMode: textEdit.mouseSelectionMode
69 property alias overwriteMode: textEdit.overwriteMode
70 property alias persistentSelection: textEdit.persistentSelection
71 property alias renderType: textEdit.renderType
72 property alias selectByKeyboard: textEdit.selectByKeyboard
73 property alias tabStopDistance: textEdit.tabStopDistance
74 property alias textMargin: textEdit.textMargin
75
76 readonly property alias canPaste: textEdit.canPaste
77 readonly property alias canRedo: textEdit.canRedo
78 readonly property alias canUndo: textEdit.canUndo
79 readonly property alias inputMethodComposing: textEdit.inputMethodComposing
80 readonly property alias length: textEdit.length
81 readonly property alias lineCount: textEdit.lineCount
82 readonly property alias selectionEnd: textEdit.selectionEnd
83 readonly property alias selectionStart: textEdit.selectionStart
84 readonly property alias contentHeight: textEdit.contentHeight
85 readonly property alias contentWidth: textEdit.contentWidth
86 readonly property alias hoveredLink: textEdit.hoveredLink
87 readonly property alias preeditText: textEdit.preeditText
88 readonly property alias selectedText: textEdit.selectedText
89 readonly property alias cursorRectangle: textEdit.cursorRectangle
90 readonly property alias cursorSelection: textEdit.cursorSelection
91 readonly property alias effectiveHorizontalAlignment: textEdit.effectiveHorizontalAlignment
92 readonly property alias textDocument: textEdit.textDocument
94 signal editingFinished()
95 signal clicked()
96 signal linkActivated(string link)
97 signal linkHovered(string link)
98
99 onLinkActivated: link => Qt.openUrlExternally(link)
100
101//BEGIN TextArea dummy entries
102 property var flickable: undefined
103 property var placeholderText: undefined
104 property var placeholderTextColor: undefined
106 signal pressAndHold(MouseEvent event)
107 signal pressed(MouseEvent event)
108 signal released(MouseEvent event)
109//END TextArea dummy entries
110
111 contentItem: TextEdit {
112 id: textEdit
113
114 /**
115 * @brief This property holds the cursor shape that will appear whenever
116 * the mouse is hovering over the label.
118 * default: @c Qt.IBeamCursor
119 *
120 * @property Qt::CursorShape cursorShape
121 */
122 property alias cursorShape: hoverHandler.cursorShape
124 activeFocusOnTab: root.activeFocusOnTab
125 color: Kirigami.Theme.textColor
126 readOnly: true
127 selectByMouse: true
128 padding: 0
129 selectedTextColor: Kirigami.Theme.highlightedTextColor
130 selectionColor: Kirigami.Theme.highlightColor
131 textFormat: TextEdit.AutoText
132 verticalAlignment: TextEdit.AlignTop
133 wrapMode: TextEdit.WordWrap
134
135 onLinkActivated: root.linkActivated(textEdit.hoveredLink)
136 onLinkHovered: root.linkHovered(textEdit.hoveredLink)
137 onEditingFinished: root.editingFinished()
139 HoverHandler {
140 id: hoverHandler
141 // By default HoverHandler accepts the left button while it shouldn't accept anything,
142 // causing https://bugreports.qt.io/browse/QTBUG-106489.
143 // Qt.NoButton unfortunately is not a valid value for acceptedButtons.
144 // Disabling masks the problem, but
145 // there is no proper workaround other than an upstream fix
146 // See qqc2-desktop-style Label.qml
147 enabled: false
148 cursorShape: root.cursorShape ? root.cursorShape : (textEdit.hoveredLink ? Qt.PointingHandCursor : Qt.IBeamCursor)
149 }
150
151 TapHandler {
152 // For custom click actions we want selection to be turned off
153 enabled: !textEdit.selectByMouse
154
155 acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus
156 acceptedButtons: Qt.LeftButton
157
158 onTapped: root.clicked()
159 }
160
161 TapHandler {
162 enabled: textEdit.selectByMouse
163
164 acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus
165 acceptedButtons: Qt.RightButton
166
167 onPressedChanged: if (pressed) {
168 contextMenu.popup();
169 }
170 }
171
172 QQC2.Menu {
173 id: contextMenu
174 QQC2.MenuItem {
175 action: T.Action {
176 icon.name: "edit-copy-symbolic"
177 text: qsTr("Copy")
178 shortcut: StandardKey.Copy
179 }
180 enabled: textEdit.selectedText.length > 0
181 onTriggered: {
182 textEdit.copy();
183 textEdit.deselect();
184 }
185 }
186 QQC2.MenuSeparator {}
187 QQC2.MenuItem {
188 action: T.Action {
189 icon.name: "edit-select-all-symbolic"
190 text: qsTr("Select All")
191 shortcut: StandardKey.SelectAll
192 }
193 onTriggered: {
194 textEdit.selectAll();
195 }
196 }
197 }
198 }
199}
const QList< QKeySequence > & shortcut(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:57:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.