Kirigami2

CardsLayout.qml
1 /*
2  * SPDX-FileCopyrightText: 2018 Marco Martin <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 import QtQuick 2.6
8 import QtQuick.Layouts 1.2
9 import org.kde.kirigami 2.4 as Kirigami
10 
11 /**
12  * @brief A GridLayout optimized for showing a couple of columns of cards,
13  * depending on the available space.
14  *
15  * This should be used when the cards to be displayed, are not instantiated by
16  * a model or are instantiated by a model that always has very few items
17  * (in the case of a big model, use CardsListView or CardsGridview instead).
18  *
19  * The cards are presented in a grid of at least one column, which will remain
20  * centered. Note that the layout will automatically add and remove columns
21  * depending on the size available.
22  *
23  * @note A CardsLayout should always be contained within a ColumnLayout.
24  *
25  * @since org.kde.kirigami 2.4
26  * @inherit QtQuick.Layouts.GridLayout
27  */
28 GridLayout {
29  /**
30  * @brief This property holds the maximum number of columns.
31  *
32  * default: ``2``
33  *
34  * @since org.kde.kirigami 2.5
35  */
36  property int maximumColumns: 2
37 
38  /**
39  * @brief This property holds the maximum width the columns may have.
40  *
41  * If the default needs to be overridden for some reason,
42  * it is advised to express this unit as a multiple
43  * of Kirigami.Units.gridUnit.
44  *
45  * default: ``20 * Kirigami.Units.gridUnit``
46  */
47  property int maximumColumnWidth: Kirigami.Units.gridUnit * 20
48 
49  /**
50  * @brief This property holds the minimum width the columns may have.
51  *
52  * default: ``12 * Kirigami.Units.gridUnit``
53  *
54  * @since org.kde.kirigami 2.5
55  */
56  property int minimumColumnWidth: Kirigami.Units.gridUnit * 12
57 
58  columns: Math.max(1, Math.min(maximumColumns > 0 ? maximumColumns : Infinity,
59  Math.floor(width/minimumColumnWidth),
60  Math.ceil(width/maximumColumnWidth)));
61 
62  rowSpacing: Kirigami.Units.largeSpacing * columns
63  columnSpacing: Kirigami.Units.largeSpacing * columns
64 
65 
66  // NOTE: this default width which defaults to 2 columns is just to remove a binding loop on columns
67  width: maximumColumnWidth*2 + Kirigami.Units.largeSpacing
68  // same computation of columns, but on the parent size
69  Layout.preferredWidth: maximumColumnWidth * Math.max(1, Math.min(maximumColumns > 0 ? maximumColumns : Infinity,
70  Math.floor(parent.width/minimumColumnWidth),
71  Math.ceil(parent.width/maximumColumnWidth))) + Kirigami.Units.largeSpacing * (columns - 1)
72 
73  Layout.maximumWidth: Layout.preferredWidth
74  Layout.alignment: Qt.AlignHCenter
75 
76  Component.onCompleted: childrenChanged()
77  onChildrenChanged: {
78  for (let i = 0; i < children.length; ++i) {
79  children[i].Layout.fillHeight = true;
80  }
81  }
82 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:07:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.