Unity 8
ResponsiveGridView.qml
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 Ubuntu.Components 1.3
19 import "."
20 
21 /*
22  Essentially a GridView
23  */
24 Item {
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: {
36  if (gridView.model) {
37  return contentHeightForRows(Math.ceil(gridView.model.count / columns), cellHeight)
38  } else {
39  return 0;
40  }
41  }
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
52 
53  function contentHeightForRows(rows, height) {
54  return rows * height
55  }
56 
57  GridView {
58  id: gridView
59  objectName: "responsiveGridViewGrid"
60  anchors {
61  fill: parent
62  leftMargin: margin/2
63  rightMargin: margin/2
64  }
65  clip: parent.height != totalContentHeight
66 
67  function pixelToGU(value) {
68  return Math.floor(value / units.gu(1));
69  }
70 
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);
75  }
76 
77  function columnsForSpacing(spacing) {
78  // minimum margin is half of the spacing
79  return Math.max(1, Math.floor(parent.width / (delegateWidth + spacing)));
80  }
81 
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
88 
89  cellWidth: delegateWidth + horizontalSpacing
90  cellHeight: delegateHeight + verticalSpacing
91 
92  onHighlightIndexChanged: {
93  if (highlightIndex != -1) {
94  currentIndex = highlightIndex
95  }
96  }
97  }
98 }