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 font: root.font
136 onLinkActivated: root.linkActivated(textEdit.hoveredLink)
137 onLinkHovered: root.linkHovered(textEdit.hoveredLink)
138 onEditingFinished: root.editingFinished()
139
140 HoverHandler {
141 id: hoverHandler
142 // By default HoverHandler accepts the left button while it shouldn't accept anything,
143 // causing https://bugreports.qt.io/browse/QTBUG-106489.
144 // Qt.NoButton unfortunately is not a valid value for acceptedButtons.
145 // Disabling masks the problem, but
146 // there is no proper workaround other than an upstream fix
147 // See qqc2-desktop-style Label.qml
148 enabled: false
149 cursorShape: root.cursorShape ? root.cursorShape : (textEdit.hoveredLink ? Qt.PointingHandCursor : Qt.IBeamCursor)
150 }
151
152 TapHandler {
153 // For custom click actions we want selection to be turned off
154 enabled: !textEdit.selectByMouse
155
156 acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus
157 acceptedButtons: Qt.LeftButton
158
159 onTapped: root.clicked()
160 }
161
162 TapHandler {
163 enabled: textEdit.selectByMouse
164
165 acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus
166 acceptedButtons: Qt.RightButton
167
168 onPressedChanged: if (pressed) {
169 contextMenu.popup();
170 }
171 }
172
173 QQC2.Menu {
174 id: contextMenu
175 QQC2.MenuItem {
176 action: T.Action {
177 icon.name: "edit-copy-symbolic"
178 text: qsTr("Copy")
179 shortcut: StandardKey.Copy
180 }
181 enabled: textEdit.selectedText.length > 0
182 onTriggered: {
183 textEdit.copy();
184 textEdit.deselect();
185 }
186 }
187 QQC2.MenuSeparator {}
188 QQC2.MenuItem {
189 action: T.Action {
190 icon.name: "edit-select-all-symbolic"
191 text: qsTr("Select All")
192 shortcut: StandardKey.SelectAll
193 }
194 onTriggered: {
195 textEdit.selectAll();
196 }
197 }
198 }
199 }
200}
const QList< QKeySequence > & shortcut(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.