2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
18 import Ubuntu.Components 1.3
21 Essentially a GridView
24 property int minimumHorizontalSpacing: units.gu(0.5)
25 readonly property int columns: gridView.columns
26 property alias verticalSpacing: gridView.verticalSpacing
27 readonly property alias margins: gridView.margin
28 property int delegateWidth
29 property int delegateHeight
30 property alias model: gridView.model
31 property alias delegate: gridView.delegate
32 readonly property int cellWidth: gridView.cellWidth
33 readonly property int cellHeight: gridView.cellHeight
34 readonly property int totalContentHeight: {
36 return contentHeightForRows(Math.ceil(gridView.model.count / columns), cellHeight)
41 property alias interactive: gridView.interactive
42 readonly property alias flicking: gridView.flicking
43 readonly property alias moving: gridView.moving
44 readonly property alias pressDelay: gridView.pressDelay
45 readonly property alias originY: gridView.originY
46 property alias displayMarginBeginning: gridView.displayMarginBeginning
47 property alias displayMarginEnd: gridView.displayMarginEnd
48 property alias highlightIndex: gridView.highlightIndex
49 property alias cacheBuffer: gridView.cacheBuffer
50 readonly property alias currentItem: gridView.currentItem
52 function contentHeightForRows(rows, height) {
58 objectName: "responsiveGridViewGrid"
64 clip: parent.height != totalContentHeight
66 function pixelToGU(value) {
67 return Math.floor(value / units.gu(1));
70 function spacingForColumns(columns) {
71 // spacing between columns as an integer number of GU, the remainder goes in the margins
72 var spacingGU = pixelToGU(allocatableHorizontalSpace / columns);
73 return units.gu(spacingGU);
76 function columnsForSpacing(spacing) {
77 // minimum margin is half of the spacing
78 return Math.max(1, Math.floor(parent.width / (delegateWidth + spacing)));
81 property real allocatableHorizontalSpace: parent.width - columns * delegateWidth
82 property int columns: columnsForSpacing(minimumHorizontalSpacing)
83 property real horizontalSpacing: spacingForColumns(columns)
84 property real verticalSpacing: horizontalSpacing
85 property int margin: allocatableHorizontalSpace - columns * horizontalSpacing
86 property int highlightIndex: -1
88 cellWidth: delegateWidth + horizontalSpacing
89 cellHeight: delegateHeight + verticalSpacing
91 onHighlightIndexChanged: {
92 if (highlightIndex != -1) {
93 currentIndex = highlightIndex