Debian Codegraph @

qml/components/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.12
18 import Lomiri.Components 0.1
19
20 // FIXME: this is a copy of Unity8's ResponsiveGridView
21
22 /*
23 Essentially a GridView where you can specify the maximum number of columns it can have.
24 */
25 Item {
26 property int minimumHorizontalSpacing: units.gu(0.5)
27 // property int minimumNumberOfColumns: 2 // FIXME: not implemented
28 property int maximumNumberOfColumns: 6
29 readonly property int columns: gridView.columns
30 property alias verticalSpacing: gridView.verticalSpacing
31 readonly property alias margins: gridView.margin
32 property int delegateWidth
33 property int delegateHeight
34 property alias model: gridView.model
35 property alias delegate: gridView.delegate
36 readonly property int cellWidth: gridView.cellWidth
37 readonly property int cellHeight: gridView.cellHeight
38 readonly property int totalContentHeight: {
39 if (gridView.model) {
40 return contentHeightForRows(Math.ceil(gridView.model.count / columns), cellHeight)
41 } else {
42 return 0;
43 }
44 }
45 property alias interactive: gridView.interactive
46 readonly property alias flicking: gridView.flicking
47 readonly property alias moving: gridView.moving
48 readonly property alias pressDelay: gridView.pressDelay
49 readonly property alias originY: gridView.originY
50 property alias displayMarginBeginning: gridView.displayMarginBeginning
51 property alias displayMarginEnd: gridView.displayMarginEnd
52 property alias highlightIndex: gridView.highlightIndex
53 property alias cacheBuffer: gridView.cacheBuffer
54 readonly property alias currentItem: gridView.currentItem
55 property alias header: gridView.header
56 property alias maximumFlickVelocity: gridView.maximumFlickVelocity
57 property alias flickDeceleration: gridView.flickDeceleration
58
59 function contentHeightForRows(rows, height) {
60 return rows * height
61 }
62
63 function positionViewAtIndex(index, mode) {
64 anim.running=false;
65 anim.from= gridView.contentY;
66 gridView.positionViewAtIndex(index, mode);
67 anim.to= gridView.contentY;
68 anim.running=true;
69 }
70
71 function indexAt(x,y) {
72 return gridView.indexAt(x,y);
73 }
74
75 NumberAnimation { id: anim; target: gridView; property: "contentY"; }
76
77 GridView {
78 id: gridView
79 objectName: "responsiveGridViewGrid"
80 anchors {
81 fill: parent
82 leftMargin: margin/2
83 rightMargin: margin/2
84 }
85 clip: parent.height != totalContentHeight
86
87 function pixelToGU(value) {
88 return (value / units.gu(1));
89 }
90
91 function spacingForColumns(columns) {
92 // spacing between columns as an integer number of GU, the remainder goes in the margins
93 var spacingGU = pixelToGU(allocatableHorizontalSpace / columns);
94 return units.gu(spacingGU);
95 }
96
97 function columnsForSpacing(spacing) {
98 // minimum margin is half of the spacing
99 return Math.max(1, Math.floor(parent.width / (delegateWidth + spacing)));
100 }
101
102 property real allocatableHorizontalSpace: parent.width - columns * delegateWidth
103 property int columns: Math.min(columnsForSpacing(minimumHorizontalSpacing), maximumNumberOfColumns)
104 property real horizontalSpacing: spacingForColumns(columns)
105 property real verticalSpacing: horizontalSpacing
106 property int margin: allocatableHorizontalSpace - columns * horizontalSpacing
107 property int highlightIndex: -1
108
109 cellWidth: delegateWidth + horizontalSpacing
110 cellHeight: delegateHeight + verticalSpacing
111
112 onHighlightIndexChanged: {
113 if (highlightIndex != -1) {
114 currentIndex = highlightIndex
115 }
116 }
117 }
118 }