Unity 8
 All Classes Functions
ResponsiveFlowView.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.0
18 import Ubuntu.Components 0.1
19 
20 Item {
21  id: root
22 
23  property int minimumHorizontalSpacing: units.gu(0.5)
24  // property int minimumNumberOfColumns: 2 // FIXME: not implemented
25  property int maximumNumberOfColumns: 6
26  readonly property int columns: flow.columns
27  property alias verticalSpacing: flow.verticalSpacing
28  property alias horizontalSpacing: flow.horizontalSpacing
29  property int referenceDelegateWidth
30  property alias model: repeater.model
31  property alias delegate: repeater.delegate
32  readonly property int cellWidth: referenceDelegateWidth + horizontalSpacing
33  readonly property int cellHeight: referenceDelegateWidth + verticalSpacing
34  property alias move: flow.move
35 
36  height: flow.height + flow.anchors.topMargin
37 
38  Flow {
39  id: flow
40  anchors {
41  left: parent.left
42  right: parent.right
43  top: parent.top
44  leftMargin: margin/2
45  rightMargin: margin/2
46  topMargin: verticalSpacing
47  }
48 
49  function pixelToGU(value) {
50  return Math.floor(value / units.gu(1));
51  }
52 
53  function spacingForColumns(columns) {
54  // spacing between columns as an integer number of GU, the remainder goes in the margins
55  var spacingGU = pixelToGU(allocatableVerticalSpace / columns);
56  return units.gu(spacingGU);
57  }
58 
59  function columnsForSpacing(space) {
60  // minimum margin is half of the spacing
61  return Math.floor((parent.width - space/2) / (referenceDelegateWidth + space));
62  }
63 
64  property real allocatableVerticalSpace: parent.width - columns * referenceDelegateWidth
65  property int columns: Math.min(columnsForSpacing(minimumHorizontalSpacing), maximumNumberOfColumns)
66  property real horizontalSpacing: spacingForColumns(columns)
67  property real verticalSpacing: horizontalSpacing
68  property int margin: allocatableVerticalSpace - columns * horizontalSpacing
69 
70  Repeater {
71  id: repeater
72  }
73  }
74 }