Debian Codegraph @

import/Lomiri/Content/qml6/ResponsiveGridView.qml

raw
1 /*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 2.0
19
20 /*
21 Essentially a GridView where you can specify the maximum number of columns it can have.
22 */
23 Item {
24 property int minimumHorizontalSpacing: units.gu(0.5)
25 // property int minimumNumberOfColumns: 2 // FIXME: not implemented
26 property int maximumNumberOfColumns: 6
27 readonly property int columns: gridView.columns
28 property alias verticalSpacing: gridView.verticalSpacing
29 readonly property alias margins: gridView.margin
30 property int delegateWidth
31 property int delegateHeight
32 property alias model: gridView.model
33 property alias delegate: gridView.delegate
34 readonly property int cellWidth: gridView.cellWidth
35 readonly property int cellHeight: gridView.cellHeight
36 property alias interactive: gridView.interactive
37 readonly property alias flicking: gridView.flicking
38 readonly property alias moving: gridView.moving
39 readonly property alias pressDelay: gridView.pressDelay
40 property alias highlightIndex: gridView.highlightIndex
41 readonly property alias currentItem: gridView.currentItem
42
43 function contentHeightForRows(rows) {
44 return rows * cellHeight + verticalSpacing
45 }
46
47 GridView {
48 id: gridView
49 objectName: "responsiveGridViewGrid"
50
51 anchors {
52 fill: parent
53 leftMargin: margin/2 + horizontalSpacing/2
54 rightMargin: margin/2 - horizontalSpacing/2
55 topMargin: verticalSpacing
56 }
57
58 onModelChanged: {
59 clip = model && parent.height != contentHeightForRows(Math.ceil(model.count / columns))
60 }
61
62 function pixelToGU(value) {
63 return Math.floor(value / units.gu(1));
64 }
65
66 function spacingForColumns(columns) {
67 // spacing between columns as an integer number of GU, the remainder goes in the margins
68 var spacingGU = pixelToGU(allocatableHorizontalSpace / (columns+1));
69 return units.gu(spacingGU);
70 }
71
72 function columnsForSpacing(spacing) {
73 // minimum margin is half of the spacing
74 return Math.max(1, Math.floor(parent.width / (delegateWidth + spacing)));
75 }
76
77 property real allocatableHorizontalSpace: parent.width - columns * delegateWidth
78 property int columns: Math.min(columnsForSpacing(minimumHorizontalSpacing), maximumNumberOfColumns)
79 property real horizontalSpacing: spacingForColumns(columns)
80 property real verticalSpacing: horizontalSpacing
81 property int margin: allocatableHorizontalSpace - columns * horizontalSpacing
82 property int highlightIndex: -1
83
84 cellWidth: delegateWidth + horizontalSpacing
85 cellHeight: delegateHeight + verticalSpacing
86
87 onHighlightIndexChanged: {
88 if (highlightIndex != -1) {
89 currentIndex = highlightIndex
90 }
91 }
92 }
93 }