Warning, /frameworks/kirigami/src/delegates/TitleSubtitle.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2010 Marco Martin <notmart@gmail.com>
0003  * SPDX-FileCopyrightText: 2022 ivan tkachenko <me@ratijas.tk>
0004  * SPDX-FileCopyrightText: 2023 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 import QtQuick
0010 import org.kde.kirigami as Kirigami
0011 
0012 /**
0013  * A simple item containing a title and subtitle label.
0014  *
0015  * This is mainly intended as a replacement for a list delegate content item,
0016  * but can be used as a replacement for other content items as well.
0017  *
0018  * When using it as a contentItem, make sure to bind the appropriate properties
0019  * to those of the Control. Prefer binding to the Control's properties over
0020  * setting the properties directly, as the Control's properties may affect other
0021  * things like setting accessible names.
0022  *
0023  * Example usage as contentItem of an ItemDelegate:
0024  *
0025  * ```qml
0026  * ItemDelegate {
0027  *     id: delegate
0028  *
0029  *     text: "Example"
0030  *
0031  *     contentItem: Kirigami.TitleSubtitle {
0032  *         title: delegate.text
0033  *         subtitle: "This is an example."
0034  *         font: delegate.font
0035  *         selected: delegate.highlighted || delegate.down
0036  *     }
0037  * }
0038  * ```
0039  *
0040  * \sa Kirigami::Delegates::IconTitleSubtitle
0041  * \sa Kirigami::Delegates::ItemDelegate
0042  */
0043 Item {
0044     id: root
0045 
0046     /**
0047      * The title to display.
0048      */
0049     required property string title
0050     /**
0051      * The subtitle to display.
0052      */
0053     property string subtitle
0054     /**
0055      * The color to use for the title.
0056      *
0057      * By default this is `Kirigami.Theme.textColor` unless `selected` is true
0058      * in which case this is `Kirigami.Theme.highlightedTextColor`.
0059      */
0060     property color color: selected ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
0061     /**
0062      * The color to use for the subtitle.
0063      *
0064      * By default this is `color` mixed with the background color.
0065      */
0066     property color subtitleColor: selected
0067         ? Kirigami.Theme.highlightedTextColor
0068         : Kirigami.ColorUtils.linearInterpolation(color, Kirigami.Theme.backgroundColor, 0.3)
0069     /**
0070      * The font used to display the title.
0071      */
0072     property font font: Kirigami.Theme.defaultFont
0073     /**
0074      * The font used to display the subtitle.
0075      */
0076     property font subtitleFont: Kirigami.Theme.smallFont
0077     /**
0078      * The text elision mode used for both the title and subtitle.
0079      */
0080     property int elide: Text.ElideRight
0081     /**
0082      * The text wrap mode used for both the title and subtitle.
0083      */
0084     property int wrapMode: Text.NoWrap
0085     /**
0086      * Make the implicit height use the subtitle's height even if no subtitle is set.
0087      */
0088     property bool reserveSpaceForSubtitle: false
0089     /**
0090      * Should this item be displayed in a selected style?
0091      */
0092     property bool selected: false
0093     /**
0094      * Is the subtitle visible?
0095      */
0096     // Note: Don't rely on subtitleItem.visible because visibility is an
0097     // implicitly propagated property, and we don't wanna re-layout on
0098     // hide/show events. Copy-paste its bound expression instead.
0099     readonly property bool subtitleVisible: subtitle.length > 0 || reserveSpaceForSubtitle
0100     /**
0101      * Is the title or subtitle truncated?
0102      */
0103     readonly property bool truncated: labelItem.truncated || subtitleItem.truncated
0104 
0105     implicitWidth: Math.max(labelItem.implicitWidth, subtitleItem.implicitWidth)
0106     implicitHeight: labelItem.implicitHeight + (subtitleVisible ? subtitleItem.implicitHeight : 0)
0107 
0108     Text {
0109         id: labelItem
0110 
0111         anchors {
0112             left: parent.left
0113             right: parent.right
0114             verticalCenter: parent.verticalCenter
0115         }
0116 
0117         text: root.title
0118         color: root.color
0119         font: root.font
0120         elide: root.elide
0121         wrapMode: root.wrapMode
0122 
0123         // Work around Qt bug where left aligned text is not right aligned
0124         // in RTL mode unless horizontalAlignment is explicitly set.
0125         // https://bugreports.qt.io/browse/QTBUG-95873
0126         horizontalAlignment: Text.AlignLeft
0127 
0128         renderType: Text.NativeRendering
0129 
0130         // Note: Can't do this through ordinary bindings as the order between
0131         // binding evaluation is not defined which leads to incorrect sizing or
0132         // the QML engine complaining about not being able to anchor to null items.
0133         states: State {
0134             // Note: Same thing about visibility as in subtitleVisible above.
0135             when: root.subtitle.length > 0
0136             AnchorChanges {
0137                 target: labelItem
0138                 anchors.verticalCenter: undefined
0139                 anchors.bottom: subtitleItem.top
0140             }
0141         }
0142     }
0143 
0144     Text {
0145         id: subtitleItem
0146 
0147         anchors {
0148             left: parent.left
0149             right: parent.right
0150             bottom: parent.bottom
0151         }
0152 
0153         text: root.subtitle
0154         color: root.subtitleColor
0155         font: root.subtitleFont
0156         elide: root.elide
0157         wrapMode: root.wrapMode
0158 
0159         visible: text.length > 0
0160 
0161         // Work around Qt bug where left aligned text is not right aligned
0162         // in RTL mode unless horizontalAlignment is explicitly set.
0163         // https://bugreports.qt.io/browse/QTBUG-95873
0164         horizontalAlignment: Text.AlignLeft
0165 
0166         renderType: Text.NativeRendering
0167     }
0168 }