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
22 Essentially a GridView
25 property int minimumHorizontalSpacing: units.gu(0.5)
26 readonly property int columns: gridView.columns
27 property alias verticalSpacing: gridView.verticalSpacing
28 readonly property alias margins: gridView.margin
29 property int delegateWidth
30 property int delegateHeight
31 property alias model: gridView.model
32 property alias delegate: gridView.delegate
33 readonly property int cellWidth: gridView.cellWidth
34 readonly property int cellHeight: gridView.cellHeight
35 readonly property int totalContentHeight: {
37 return contentHeightForRows(Math.ceil(gridView.model.count / columns), cellHeight)
42 property alias interactive: gridView.interactive
43 readonly property alias flicking: gridView.flicking
44 readonly property alias moving: gridView.moving
45 readonly property alias pressDelay: gridView.pressDelay
46 readonly property alias originY: gridView.originY
47 property alias displayMarginBeginning: gridView.displayMarginBeginning
48 property alias displayMarginEnd: gridView.displayMarginEnd
49 property alias highlightIndex: gridView.highlightIndex
50 property alias cacheBuffer: gridView.cacheBuffer
51 readonly property alias currentItem: gridView.currentItem
53 function contentHeightForRows(rows, height) {
59 objectName: "responsiveGridViewGrid"
65 clip: parent.height != totalContentHeight
67 function pixelToGU(value) {
68 return Math.floor(value / units.gu(1));
71 function spacingForColumns(columns) {
72 // spacing between columns as an integer number of GU, the remainder goes in the margins
73 var spacingGU = pixelToGU(allocatableHorizontalSpace / columns);
74 return units.gu(spacingGU);
77 function columnsForSpacing(spacing) {
78 // minimum margin is half of the spacing
79 return Math.max(1, Math.floor(parent.width / (delegateWidth + spacing)));
82 property real allocatableHorizontalSpace: parent.width - columns * delegateWidth
83 property int columns: columnsForSpacing(minimumHorizontalSpacing)
84 property real horizontalSpacing: spacingForColumns(columns)
85 property real verticalSpacing: horizontalSpacing
86 property int margin: allocatableHorizontalSpace - columns * horizontalSpacing
87 property int highlightIndex: -1
89 cellWidth: delegateWidth + horizontalSpacing
90 cellHeight: delegateHeight + verticalSpacing
92 onHighlightIndexChanged: {
93 if (highlightIndex != -1) {
94 currentIndex = highlightIndex