Plasma-framework

IconLabel.qml
1/*
2 SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
3 SPDX-FileCopyrightText: 2022 ivan (@ratijas) tkachenko <me@ratijas.tk>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8import QtQuick
9import QtQuick.Layouts
10import QtQuick.Templates as T
11import org.kde.kirigami as Kirigami
12
13Item {
14 id: root
15
16 // alignment is subject to mirroring
17 property int alignment: 0 // Null alignment
18 property int effectiveHorizontalAlignment: !LayoutMirroring.enabled ? alignment
19 : (alignment & Qt.AlignLeft) ? Qt.AlignRight
20 : (alignment & Qt.AlignRight) ? Qt.AlignLeft
21 : Qt.AlignHCenter
22 property int display: T.AbstractButton.TextBesideIcon
23
24 property alias iconItem: iconItem
25 property alias label: label
26
27 property real topPadding: 0
28 property real leftPadding: 0
29 property real rightPadding: 0
30 property real bottomPadding: 0
31
32 readonly property real availableWidth: width - leftPadding - rightPadding
33 readonly property real availableHeight: height - topPadding - bottomPadding
34
35 property real spacing: 0
36
37 property font font
38 // TODO KF6: This is not a correct formula for mirrored property.
39 // Explicitly setting `LayoutMirroring.enabled` to `false` should undone
40 // any mirroring imposed by LayoutMirroring inheritance or RTL locale.
41 // Fixed in Qt 6.2, see QTBUG-91227
42 property bool mirrored: false
43
44 implicitWidth: gridLayout.implicitWidth + leftPadding + rightPadding
45 implicitHeight: gridLayout.implicitHeight + topPadding + bottomPadding
46
47 GridLayout {
48 id: gridLayout
49
50 rowSpacing: root.spacing
51 columnSpacing: root.spacing
52 flow: root.display === T.AbstractButton.TextUnderIcon ? GridLayout.TopToBottom : GridLayout.LeftToRight
53 // Avoid manipulating layoutDirection directly, and we still need to
54 // set LayoutMirroring.enabled to some deterministic value which
55 // would not be affected by a random LayoutMirroring.childrenInherit
56 // up the parents chain.
57 LayoutMirroring.enabled: root.mirrored
58 x: {
59 if (root.effectiveHorizontalAlignment & Qt.AlignLeft) {
60 return root.leftPadding;
61 }
62 if (root.effectiveHorizontalAlignment & Qt.AlignRight) {
63 return root.width - width - root.rightPadding;
64 }
65 return Math.round((root.availableWidth - width) / 2);
66 }
67 y: {
68 if (root.alignment & Qt.AlignTop) {
69 return root.topPadding;
70 }
71 if (root.alignment & Qt.AlignBottom) {
72 return root.height - height - root.bottomPadding;
73 }
74 return Math.round((root.availableHeight - height) / 2);
75 }
76 width: Math.min(root.availableWidth, implicitWidth)
77 height: Math.min(root.availableHeight, implicitHeight)
78
79 Kirigami.Icon {
80 id: iconItem
81 visible: valid && width > 0 && height > 0 && root.display !== T.AbstractButton.TextOnly
82 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
83 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
84 Layout.alignment: Qt.AlignCenter
85 Layout.maximumWidth: implicitWidth > 0 ? implicitWidth : Number.POSITIVE_INFINITY
86 Layout.maximumHeight: implicitHeight > 0 ? implicitHeight : Number.POSITIVE_INFINITY
87 }
88
89 Text {
90 id: label
91 visible: text.length > 0 && root.display !== T.AbstractButton.IconOnly
92 font: root.font
93 color: Kirigami.Theme.textColor
94 linkColor: Kirigami.Theme.linkColor
95 elide: Text.ElideRight
96 Layout.alignment: Qt.AlignCenter
97 Layout.fillWidth: true
98 }
99 }
100}
QString label(StandardShortcut id)
qsizetype length() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.