Kirigami2

TitleSubtitle.qml
1/*
2 * SPDX-FileCopyrightText: 2010 Marco Martin <notmart@gmail.com>
3 * SPDX-FileCopyrightText: 2022 ivan tkachenko <me@ratijas.tk>
4 * SPDX-FileCopyrightText: 2023 Arjen Hiemstra <ahiemstra@heimr.nl>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9import QtQuick
10import org.kde.kirigami as Kirigami
11
12/**
13 * A simple item containing a title and subtitle label.
14 *
15 * This is mainly intended as a replacement for a list delegate content item,
16 * but can be used as a replacement for other content items as well.
17 *
18 * When using it as a contentItem, make sure to bind the appropriate properties
19 * to those of the Control. Prefer binding to the Control's properties over
20 * setting the properties directly, as the Control's properties may affect other
21 * things like setting accessible names.
22 *
23 * Example usage as contentItem of an ItemDelegate:
24 *
25 * ```qml
26 * ItemDelegate {
27 * id: delegate
28 *
29 * text: "Example"
30 *
31 * contentItem: Kirigami.TitleSubtitle {
32 * title: delegate.text
33 * subtitle: "This is an example."
34 * font: delegate.font
35 * selected: delegate.highlighted || delegate.down
36 * }
37 * }
38 * ```
39 *
40 * \sa Kirigami::Delegates::IconTitleSubtitle
41 * \sa Kirigami::Delegates::ItemDelegate
42 */
43Item {
44 id: root
45
46 /**
47 * The title to display.
48 */
49 required property string title
50 /**
51 * The subtitle to display.
52 */
53 property string subtitle
54 /**
55 * The color to use for the title.
56 *
57 * By default this is `Kirigami.Theme.textColor` unless `selected` is true
58 * in which case this is `Kirigami.Theme.highlightedTextColor`.
59 */
60 property color color: selected ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
61 /**
62 * The color to use for the subtitle.
63 *
64 * By default this is `color` mixed with the background color.
65 */
66 property color subtitleColor: selected
67 ? Kirigami.Theme.highlightedTextColor
68 : Kirigami.ColorUtils.linearInterpolation(color, Kirigami.Theme.backgroundColor, 0.3)
69 /**
70 * The font used to display the title.
71 */
72 property font font: Kirigami.Theme.defaultFont
73 /**
74 * The font used to display the subtitle.
75 */
76 property font subtitleFont: Kirigami.Theme.smallFont
77 /**
78 * The text elision mode used for both the title and subtitle.
79 */
80 property int elide: Text.ElideRight
81 /**
82 * The text wrap mode used for both the title and subtitle.
83 */
84 property int wrapMode: Text.NoWrap
85 /**
86 * Make the implicit height use the subtitle's height even if no subtitle is set.
87 */
88 property bool reserveSpaceForSubtitle: false
89 /**
90 * Should this item be displayed in a selected style?
91 */
92 property bool selected: false
93 /**
94 * Is the subtitle visible?
95 */
96 // Note: Don't rely on subtitleItem.visible because visibility is an
97 // implicitly propagated property, and we don't wanna re-layout on
98 // hide/show events. Copy-paste its bound expression instead.
99 readonly property bool subtitleVisible: subtitle.length > 0 || reserveSpaceForSubtitle
100 /**
101 * Is the title or subtitle truncated?
102 */
103 readonly property bool truncated: labelItem.truncated || subtitleItem.truncated
105 /**
106 * @brief Emitted when the user clicks on a link embedded in the text of the title or subtitle.
107 */
108 signal linkActivated(string link)
109
110 /**
111 * @brief Emitted when the user hovers on a link embedded in the text of the title or subtitle.
112 */
113 signal linkHovered(string link)
114
115 implicitWidth: Math.max(labelItem.implicitWidth, subtitleItem.implicitWidth)
116 implicitHeight: labelItem.implicitHeight + (subtitleVisible ? subtitleItem.implicitHeight : 0)
117
118 Text {
119 id: labelItem
120
121 anchors {
122 left: parent.left
123 right: parent.right
124 verticalCenter: parent.verticalCenter
125 }
126
127 text: root.title
128 color: root.color
129 font: root.font
130 elide: root.elide
131 wrapMode: root.wrapMode
132
133 onLinkActivated: link => root.linkActivated(link)
134 onLinkHovered: link => root.linkHovered(link)
135
136 // Work around Qt bug where left aligned text is not right aligned
137 // in RTL mode unless horizontalAlignment is explicitly set.
138 // https://bugreports.qt.io/browse/QTBUG-95873
139 horizontalAlignment: Text.AlignLeft
140
141 renderType: Text.NativeRendering
142
143 // Note: Can't do this through ordinary bindings as the order between
144 // binding evaluation is not defined which leads to incorrect sizing or
145 // the QML engine complaining about not being able to anchor to null items.
146 states: State {
147 // Note: Same thing about visibility as in subtitleVisible above.
148 when: root.subtitle.length > 0
149 AnchorChanges {
150 target: labelItem
151 anchors.verticalCenter: undefined
152 anchors.bottom: subtitleItem.top
153 }
154 }
155 }
156
157 Text {
158 id: subtitleItem
159
160 anchors {
161 left: parent.left
162 right: parent.right
163 bottom: parent.bottom
164 }
165
166 text: root.subtitle
167 color: root.subtitleColor
168 font: root.subtitleFont
169 elide: root.elide
170 wrapMode: root.wrapMode
171
172 visible: text.length > 0
173
174 onLinkActivated: link => root.linkActivated(link)
175 onLinkHovered: link => root.linkHovered(link)
176
177 // Work around Qt bug where left aligned text is not right aligned
178 // in RTL mode unless horizontalAlignment is explicitly set.
179 // https://bugreports.qt.io/browse/QTBUG-95873
180 horizontalAlignment: Text.AlignLeft
181
182 renderType: Text.NativeRendering
183 }
184}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:49:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.