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

KDE's Doxygen guidelines are available online.