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 one or two columns of cards,
13  * depending on the available space.
14  *
15  * It Should be used when the cards are not instantiated by a model or by a
16  * model which has always very few items (In the case of a big model
17  * CardsListView or CardsGridview should be used instead).
18  *
19  * They are presented as a grid of two columns which will remain
20  * centered if the application is really wide, or become a single
21  * column if there is not enough space for two columns,
22  * such as a mobile phone screen.
23  *
24  * A CardsLayout should always be contained within a ColumnLayout.
25  *
26  * @since 2.4
27  * @inherit QtQuick.Layouts.GridLayout
28  */
29 GridLayout {
30  /**
31  * @brief This property holds the maximum number of columns.
32  *
33  * This layout will never lay out the items in more columns than maximumColumns
34  *
35  * default: ``2``
36  *
37  * @since 2.5
38  */
39  property int maximumColumns: 2
40 
41  /**
42  * @brief This property holds the maximum width the columns may have.
43  *
44  * The cards will never become wider than this size; when the GridLayout is wider than
45  * maximumColumnWidth, it will switch from one to two columns.
46  *
47  * If the default needs to be overridden for some reason,
48  * it is advised to express this unit as a multiple
49  * of Kirigami.Units.gridUnit.
50  *
51  * default: ``20 * Kirigami.Units.gridUnit``
52  */
53  property int maximumColumnWidth: Kirigami.Units.gridUnit * 20
54 
55  /**
56  * @brief This property holds the minimum width the columns may have.
57  *
58  * The layout will try to dispose of items
59  * in a number of columns that will respect this size constraint.
60  *
61  * default: ``12 * Kirigami.Units.gridUnit``
62  *
63  * @since 2.5
64  */
65  property int minimumColumnWidth: Kirigami.Units.gridUnit * 12
66 
67  columns: Math.max(1, Math.min(maximumColumns > 0 ? maximumColumns : Infinity,
68  Math.floor(width/minimumColumnWidth),
69  Math.ceil(width/maximumColumnWidth)));
70 
71  rowSpacing: Kirigami.Units.largeSpacing * columns
72  columnSpacing: Kirigami.Units.largeSpacing * columns
73 
74 
75  // NOTE: this default width which defaults to 2 columns is just to remove a binding loop on columns
76  width: maximumColumnWidth*2 + Kirigami.Units.largeSpacing
77  // same computation of columns, but on the parent size
78  Layout.preferredWidth: maximumColumnWidth * Math.max(1, Math.min(maximumColumns > 0 ? maximumColumns : Infinity,
79  Math.floor(parent.width/minimumColumnWidth),
80  Math.ceil(parent.width/maximumColumnWidth))) + Kirigami.Units.largeSpacing * (columns - 1)
81 
82  Layout.maximumWidth: Layout.preferredWidth
83  Layout.alignment: Qt.AlignHCenter
84 
85  Component.onCompleted: childrenChanged()
86  onChildrenChanged: {
87  for (let i = 0; i < children.length; ++i) {
88  children[i].Layout.fillHeight = true;
89  }
90  }
91 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:14:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.