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