on 95e9355 help sign in
unity8/qml/Components/ResponsiveGridView.qml
96 lines 3.8 kB history 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 */
17import QtQuick 2.3
18import Ubuntu.Components 0.1
19import "Flickables" as Flickables
21/*
22 Essentially a GridView where you can specify the maximum number of columns it can have.
23 */
24Item {
25 property int minimumHorizontalSpacing: units.gu(0.5)
26 // property int minimumNumberOfColumns: 2 // FIXME: not implemented
27 property int maximumNumberOfColumns: 6
28 readonly property int columns: gridView.columns
29 property alias verticalSpacing: gridView.verticalSpacing
30 readonly property alias margins: gridView.margin
31 property int delegateWidth
32 property int delegateHeight
33 property alias model: gridView.model
34 property alias delegate: gridView.delegate
35 readonly property int cellWidth: gridView.cellWidth
36 readonly property int cellHeight: gridView.cellHeight
37 readonly property int totalContentHeight: {
38 return contentHeightForRows(Math.ceil(gridView.model.count / columns), cellHeight)
39 }
40 property alias interactive: gridView.interactive
41 readonly property alias flicking: gridView.flicking
42 readonly property alias moving: gridView.moving
43 readonly property alias pressDelay: gridView.pressDelay
44 readonly property alias originY: gridView.originY
45 property alias displayMarginBeginning: gridView.displayMarginBeginning
46 property alias displayMarginEnd: gridView.displayMarginEnd
47 property alias highlightIndex: gridView.highlightIndex
48 property alias cacheBuffer: gridView.cacheBuffer
49 readonly property alias currentItem: gridView.currentItem
51 function contentHeightForRows(rows, height) {
52 return rows * height
53 }
55 Flickables.GridView {
56 id: gridView
57 objectName: "responsiveGridViewGrid"
58 anchors {
59 fill: parent
60 leftMargin: margin/2
61 rightMargin: margin/2
62 }
63 clip: parent.height != totalContentHeight
65 function pixelToGU(value) {
66 return Math.floor(value / units.gu(1));
67 }
69 function spacingForColumns(columns) {
70 // spacing between columns as an integer number of GU, the remainder goes in the margins
71 var spacingGU = pixelToGU(allocatableHorizontalSpace / columns);
72 return units.gu(spacingGU);
73 }
75 function columnsForSpacing(spacing) {
76 // minimum margin is half of the spacing
77 return Math.max(1, Math.floor(parent.width / (delegateWidth + spacing)));
78 }
80 property real allocatableHorizontalSpace: parent.width - columns * delegateWidth
81 property int columns: Math.min(columnsForSpacing(minimumHorizontalSpacing), maximumNumberOfColumns)
82 property real horizontalSpacing: spacingForColumns(columns)
83 property real verticalSpacing: horizontalSpacing
84 property int margin: allocatableHorizontalSpace - columns * horizontalSpacing
85 property int highlightIndex: -1
87 cellWidth: delegateWidth + horizontalSpacing
88 cellHeight: delegateHeight + verticalSpacing
90 onHighlightIndexChanged: {
91 if (highlightIndex != -1) {
92 currentIndex = highlightIndex
93 }
94 }
95 }