Kirigami2

CardsGridView.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.10
8 import QtQuick.Layouts 1.2
9 import org.kde.kirigami 2.4 as Kirigami
10 import "private" as P
11 
12 
13 //TODO KF6: remove the whole class?
14 /**
15  * @brief CardsGridView is used to display a grid of Cards generated from any model.
16  *
17  * The behavior is same as CardsLayout, and it allows cards to be put in one or two
18  * columns depending on the available width.
19  *
20  * GridView has the limitation that every Card must have the same exact height,
21  * so cellHeight must be manually set to a value in which the content fits
22  * for every item.
23  *
24  * If possible use cards only when you don't need to instantiate a lot
25  * and use CardsLayout instead.
26  *
27  * @see CardsLayout
28  * @see CardsListView
29  * @inherit QtQuick.GridView
30  * @since 2.4
31  */
32 P.CardsGridViewPrivate {
33  id: root
34 
35  /**
36  * @brief This property sets whether the view should fill the first row with columns
37  * even when there is not enough space.
38  *
39  * Set this to true if you want to stop the view from filling the first row with columns,
40  * even when delegates can't even fill the first row.
41  *
42  * default: ``true``
43  */
44  property bool extraColumns: true
45 
46  /**
47  * @brief This property holds the number of columns the gridview has.
48  * @since 2.5
49  */
50  readonly property int columns: {
51  const minFromWidth = Math.floor(width / minimumColumnWidth)
52  const maxFromWidth = Math.ceil(width / maximumColumnWidth)
53  const extraCount = extraColumns ? Infinity : count
54  return Math.max(1,Math.min(maximumColumns,minFromWidth,maxFromWidth,extraCount))
55  }
56 
57  /**
58  * @brief This property holds the maximum number of columns the gridview may have.
59  *
60  * default: ``Kirigami.Units.maximumInteger()``
61  *
62  * @since 2.5
63  */
64  property int maximumColumns: Kirigami.Units.maximumInteger
65 
66  /**
67  * @brief This property holds the maximum width that the columns may have.
68  *
69  * The cards will never become wider than this size; when the GridView is wider
70  * than maximumColumnWidth, it will switch from one to two columns.
71  *
72  * If the default needs to be overridden for some reason,
73  * it is advised to express this unit as a multiple
74  * of Kirigami.Units.gridUnit.
75  *
76  * default: ``20 * Kirigami.Units.gridUnit``
77  */
78  property int maximumColumnWidth: Kirigami.Units.gridUnit * 20
79 
80  /**
81  * @brief This property holds the minimum width that the columns may have.
82  *
83  * The cards will never become thinner than this.
84  *
85  * If the default needs to be overridden for some reason,
86  * it is advised to express this unit as a multiple
87  * of Kirigami.Units.gridUnit.
88  *
89  * default: ``12 * Kirigami.Units.gridUnit``
90  *
91  * @since 2.5
92  */
93  property int minimumColumnWidth: Kirigami.Units.gridUnit * 12
94 
95  cellWidth: Math.floor(width/columns)
96  cellHeight: Math.max(Kirigami.Units.gridUnit * 15, Math.min(cellWidth, maximumColumnWidth) / 1.2)
97 
98  /**
99  * @brief This property holds the delegate of the CardsGridView.
100  * @see QtQuick.GridView::delegate
101  */
102  default property alias delegate: root._delegateComponent
103 
104  topMargin: Kirigami.Units.largeSpacing * 2
105 
106  Keys.onPressed: event => {
107  if (event.key === Qt.Key_Home) {
108  positionViewAtBeginning();
109  currentIndex = 0;
110  event.accepted = true;
111  }
112  else if (event.key === Qt.Key_End) {
113  positionViewAtEnd();
114  currentIndex = count - 1;
115  event.accepted = true;
116  }
117  }
118 }
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.