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 property alias readOnly: textEdit.readOnly
46 property alias selectByMouse: textEdit.selectByMouse
47 property alias color: textEdit.color
48 property alias selectedTextColor: textEdit.selectedTextColor
49 property alias selectionColor: textEdit.selectionColor
50 property alias text: textEdit.text
51 property alias baseUrl: textEdit.baseUrl
52 property var cursorShape
53 property alias horizontalAlignment: textEdit.horizontalAlignment
54 property alias verticalAlignment: textEdit.verticalAlignment
55 property alias textFormat: textEdit.textFormat
56 property alias wrapMode: textEdit.wrapMode
57
58 property alias activeFocusOnPress: textEdit.activeFocusOnPress
59 property alias cursorDelegate: textEdit.cursorDelegate
60 property alias cursorPosition: textEdit.cursorPosition
61 property alias cursorVisible: textEdit.cursorVisible
62 property alias inputMethodHints: textEdit.inputMethodHints
63 property alias mouseSelectionMode: textEdit.mouseSelectionMode
64 property alias overwriteMode: textEdit.overwriteMode
65 property alias persistentSelection: textEdit.persistentSelection
66 property alias renderType: textEdit.renderType
67 property alias selectByKeyboard: textEdit.selectByKeyboard
68 property alias tabStopDistance: textEdit.tabStopDistance
69 property alias textMargin: textEdit.textMargin
70
71 readonly property alias canPaste: textEdit.canPaste
72 readonly property alias canRedo: textEdit.canRedo
73 readonly property alias canUndo: textEdit.canUndo
74 readonly property alias inputMethodComposing: textEdit.inputMethodComposing
75 readonly property alias length: textEdit.length
76 readonly property alias lineCount: textEdit.lineCount
77 readonly property alias selectionEnd: textEdit.selectionEnd
78 readonly property alias selectionStart: textEdit.selectionStart
79 readonly property alias contentHeight: textEdit.contentHeight
80 readonly property alias contentWidth: textEdit.contentWidth
81 readonly property alias hoveredLink: textEdit.hoveredLink
82 readonly property alias preeditText: textEdit.preeditText
83 readonly property alias selectedText: textEdit.selectedText
84 readonly property alias cursorRectangle: textEdit.cursorRectangle
85 readonly property alias cursorSelection: textEdit.cursorSelection
86 readonly property alias effectiveHorizontalAlignment: textEdit.effectiveHorizontalAlignment
87 readonly property alias textDocument: textEdit.textDocument
88
89 signal editingFinished()
90 signal clicked()
91 signal linkActivated(string link)
92 signal linkHovered(string link)
93
94 onLinkActivated: link => Qt.openUrlExternally(link)
96//BEGIN TextArea dummy entries
97 property var flickable: undefined
98 property var placeholderText: undefined
99 property var placeholderTextColor: undefined
100
101 signal pressAndHold(MouseEvent event)
102 signal pressed(MouseEvent event)
103 signal released(MouseEvent event)
104//END TextArea dummy entries
105
106 contentItem: TextEdit {
107 id: textEdit
108
109 /**
110 * @brief This property holds the cursor shape that will appear whenever
111 * the mouse is hovering over the label.
112 *
113 * default: @c Qt.IBeamCursor
114 *
115 * @property Qt::CursorShape cursorShape
116 */
117 property alias cursorShape: hoverHandler.cursorShape
118
119 activeFocusOnTab: root.activeFocusOnTab
120 color: Kirigami.Theme.textColor
121 readOnly: true
122 selectByMouse: true
123 padding: 0
124 selectedTextColor: Kirigami.Theme.highlightedTextColor
125 selectionColor: Kirigami.Theme.highlightColor
126 textFormat: TextEdit.AutoText
127 verticalAlignment: TextEdit.AlignTop
128 wrapMode: TextEdit.WordWrap
129
130 onLinkActivated: root.linkActivated(textEdit.hoveredLink)
131 onLinkHovered: root.linkHovered(textEdit.hoveredLink)
132 onEditingFinished: root.editingFinished()
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 25 2024 11:54:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.