Kirigami2

NavigationTabButton.qml
1/* SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
2 * SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6import QtQuick
7import QtQuick.Layouts
8import QtQuick.Controls as QQC2
9import QtQuick.Templates as T
10import org.kde.kirigami as Kirigami
11
12/**
13 * @brief Navigation buttons to be used for the NavigationTabBar component.
14 *
15 * It supplies its own padding, and also supports using the QQC2 AbstractButton ``display`` property to be used in column lists.
16 *
17 * Alternative way to the "actions" property on NavigationTabBar, as it can be used
18 * with Repeater to generate buttons from models.
19 *
20 * Example usage:
21 * @code{.qml}
22 * Kirigami.NavigationTabBar {
23 * id: navTabBar
24 * Kirigami.NavigationTabButton {
25 * visible: true
26 * icon.name: "document-save"
27 * text: `test ${tabIndex + 1}`
28 * QQC2.ButtonGroup.group: navTabBar.tabGroup
29 * }
30 * Kirigami.NavigationTabButton {
31 * visible: false
32 * icon.name: "document-send"
33 * text: `test ${tabIndex + 1}`
34 * QQC2.ButtonGroup.group: navTabBar.tabGroup
35 * }
36 * actions: [
37 * Kirigami.Action {
38 * visible: true
39 * icon.name: "edit-copy"
40 * icon.height: 32
41 * icon.width: 32
42 * text: `test 3`
43 * checked: true
44 * },
45 * Kirigami.Action {
46 * visible: true
47 * icon.name: "edit-cut"
48 * text: `test 4`
49 * checkable: true
50 * },
51 * Kirigami.Action {
52 * visible: false
53 * icon.name: "edit-paste"
54 * text: `test 5`
55 * },
56 * Kirigami.Action {
57 * visible: true
58 * icon.source: "../logo.png"
59 * text: `test 6`
60 * checkable: true
61 * }
62 * ]
63 * }
64 * @endcode
65 *
66 * @since 5.87
67 * @since org.kde.kirigami 2.19
68 * @inherit QtQuick.Templates.TabButton
69 */
70T.TabButton {
71 id: control
72
73 /**
74 * @brief This property tells the index of this tab within the tab bar.
75 */
76 readonly property int tabIndex: {
77 let tabIdx = 0
78 for (const child of parent.children) {
79 if (child === this) {
80 return tabIdx
81 }
82 // Checking for AbstractButtons because any AbstractButton can act as a tab
83 if (child instanceof T.AbstractButton) {
84 ++tabIdx
85 }
86 }
87 return -1
88 }
89
90 // FIXME: all those internal properties should go, and the button should style itself in a more standard way
91 // probably similar to view items
92 readonly property color __foregroundColor: Qt.alpha(Kirigami.Theme.textColor, 0.85)
93 readonly property color __highlightForegroundColor: Qt.alpha(Kirigami.Theme.textColor, 0.85)
94
95 readonly property color __pressedColor: Qt.alpha(Kirigami.Theme.highlightColor, 0.3)
96 readonly property color __hoverSelectColor: Qt.alpha(Kirigami.Theme.highlightColor, 0.2)
97 readonly property color __checkedBorderColor: Qt.alpha(Kirigami.Theme.highlightColor, 0.7)
98 readonly property color __pressedBorderColor: Qt.alpha(Kirigami.Theme.highlightColor, 0.9)
99
100 readonly property real __verticalMargins: (display === T.AbstractButton.TextBesideIcon) ? Kirigami.Units.largeSpacing : 0
101
102 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
103 implicitContentWidth + leftPadding + rightPadding)
104 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
105 implicitContentHeight + topPadding + bottomPadding)
106
107 display: T.AbstractButton.TextUnderIcon
108
109 Kirigami.Theme.colorSet: Kirigami.Theme.Window
110 Kirigami.Theme.inherit: false
111
112 hoverEnabled: true
113
114 padding: Kirigami.Units.smallSpacing
115 spacing: Kirigami.Units.smallSpacing
116
117 icon.height: display === T.AbstractButton.TextBesideIcon ? Kirigami.Units.iconSizes.small : Kirigami.Units.iconSizes.smallMedium
118 icon.width: display === T.AbstractButton.TextBesideIcon ? Kirigami.Units.iconSizes.small : Kirigami.Units.iconSizes.smallMedium
119 icon.color: checked ? __highlightForegroundColor : __foregroundColor
120
121 Kirigami.MnemonicData.enabled: enabled && visible
122 Kirigami.MnemonicData.controlType: Kirigami.MnemonicData.MenuItem
123 Kirigami.MnemonicData.label: text
124
125 Accessible.onPressAction: control.action.trigger()
126
127 background: Rectangle {
128 Kirigami.Theme.colorSet: Kirigami.Theme.Button
129 Kirigami.Theme.inherit: false
130
131 implicitHeight: (control.display === T.AbstractButton.TextBesideIcon) ? 0 : (Kirigami.Units.gridUnit * 3 + Kirigami.Units.smallSpacing * 2)
132
133 color: "transparent"
134
135 Rectangle {
136 width: parent.width - Kirigami.Units.largeSpacing
137 height: parent.height - Kirigami.Units.largeSpacing
138 anchors.centerIn: parent
139
140 radius: Kirigami.Units.smallSpacing
141 color: control.down ? control.__pressedColor : (control.checked || control.hovered ? control.__hoverSelectColor : "transparent")
142
143 border.color: control.checked ? control.__checkedBorderColor : (control.down ? control.__pressedBorderColor : color)
144 border.width: 1
145
146 Behavior on color { ColorAnimation { duration: Kirigami.Units.shortDuration } }
147 Behavior on border.color { ColorAnimation { duration: Kirigami.Units.shortDuration } }
148 }
149 }
150
151 contentItem: GridLayout {
152 columnSpacing: 0
153 rowSpacing: (label.visible && label.lineCount > 1) ? 0 : control.spacing
154
155 // if this is a row or a column
156 columns: control.display !== T.AbstractButton.TextBesideIcon ? 1 : 2
157
158 Kirigami.Icon {
159 id: icon
160 source: control.icon.name || control.icon.source
161 visible: (control.icon.name.length > 0 || control.icon.source.toString().length > 0) && control.display !== T.AbstractButton.TextOnly
162 color: control.icon.color
163
164 Layout.topMargin: control.__verticalMargins
165 Layout.bottomMargin: control.__verticalMargins
166 Layout.leftMargin: (control.display === T.AbstractButton.TextBesideIcon) ? Kirigami.Units.gridUnit : 0
167 Layout.rightMargin: (control.display === T.AbstractButton.TextBesideIcon) ? Kirigami.Units.gridUnit : 0
168
169 Layout.alignment: {
170 if (control.display === T.AbstractButton.TextBesideIcon) {
171 // row layout
172 return Qt.AlignVCenter | Qt.AlignRight;
173 } else {
174 // column layout
175 return Qt.AlignHCenter | ((!label.visible || label.lineCount > 1) ? Qt.AlignVCenter : Qt.AlignBottom);
176 }
177 }
178 implicitHeight: source ? control.icon.height : 0
179 implicitWidth: source ? control.icon.width : 0
180
181 Behavior on color { ColorAnimation { duration: Kirigami.Units.shortDuration } }
182 }
183 QQC2.Label {
184 id: label
185
186 text: control.Kirigami.MnemonicData.richTextLabel
187 horizontalAlignment: (control.display === T.AbstractButton.TextBesideIcon) ? Text.AlignLeft : Text.AlignHCenter
188
189 visible: control.display !== T.AbstractButton.IconOnly
190 wrapMode: Text.Wrap
191 elide: Text.ElideMiddle
192 color: control.checked ? control.__highlightForegroundColor : control.__foregroundColor
193
194 font.bold: control.checked
195 font.family: Kirigami.Theme.smallFont.family
196 font.pointSize: {
197 if (control.display === T.AbstractButton.TextBesideIcon) {
198 // row layout
199 return Kirigami.Theme.defaultFont.pointSize;
200 } else {
201 // column layout
202 return icon.visible ? Kirigami.Theme.smallFont.pointSize : Kirigami.Theme.defaultFont.pointSize * 1.20; // 1.20 is equivalent to level 2 heading
203 }
204 }
205
206 Behavior on color { ColorAnimation { duration: Kirigami.Units.shortDuration } }
207
208 Layout.topMargin: control.__verticalMargins
209 Layout.bottomMargin: control.__verticalMargins
210
211 Layout.alignment: {
212 if (control.display === T.AbstractButton.TextBesideIcon) {
213 // row layout
214 return Qt.AlignVCenter | Qt.AlignLeft;
215 } else {
216 // column layout
217 return icon.visible ? Qt.AlignHCenter | Qt.AlignTop : Qt.AlignCenter;
218 }
219 }
220
221 // Work around bold text changing implicit size
222 Layout.preferredWidth: boldMetrics.implicitWidth
223 Layout.preferredHeight: boldMetrics.implicitHeight * label.lineCount
224 Layout.fillWidth: true
225
226 Accessible.ignored: true
227
228 QQC2.Label {
229 id: boldMetrics
230 visible: false
231 text: label.text
232 font.bold: true
233 font.family: Kirigami.Theme.smallFont.family
234 font.pointSize: Kirigami.Theme.smallFont.pointSize
235 horizontalAlignment: Text.AlignHCenter
236 wrapMode: Text.Wrap
237 elide: Text.ElideMiddle
238
239 Accessible.ignored: true
240 }
241 }
242 }
243}
char * toString(const EngineQuery &query)
QString label(StandardShortcut id)
QString name(StandardShortcut id)
AlignBottom
ElideMiddle
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:46 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.