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