on fbf656a help sign in
unity8/Components/ResponsiveFlowView.qml
81 lines 3.0 kB history raw
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 */
17import QtQuick 2.0
18import Ubuntu.Components 0.1
20Item {
21 id: root
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 firstModel: repeater1.model
31 property alias secondModel: repeater2.model
32 property alias delegate: repeater1.delegate
33 readonly property int cellWidth: referenceDelegateWidth + horizontalSpacing
34 readonly property int cellHeight: referenceDelegateWidth + verticalSpacing
35 property alias move: flow.move
37 height: flow.height + flow.anchors.topMargin
39 Flow {
40 id: flow
41 anchors {
42 left: parent.left
43 right: parent.right
44 top: parent.top
45 leftMargin: margin/2
46 rightMargin: margin/2
47 topMargin: verticalSpacing
48 }
50 function pixelToGU(value) {
51 return Math.floor(value / units.gu(1));
52 }
54 function spacingForColumns(columns) {
55 // spacing between columns as an integer number of GU, the remainder goes in the margins
56 var spacingGU = pixelToGU(allocatableVerticalSpace / columns);
57 return units.gu(spacingGU);
58 }
60 function columnsForSpacing(space) {
61 // minimum margin is half of the spacing
62 return Math.floor((parent.width - space/2) / (referenceDelegateWidth + space));
63 }
65 property real allocatableVerticalSpace: parent.width - columns * referenceDelegateWidth
66 property int columns: Math.min(columnsForSpacing(minimumHorizontalSpacing), maximumNumberOfColumns)
67 property real horizontalSpacing: spacingForColumns(columns)
68 property real verticalSpacing: horizontalSpacing
69 property int margin: allocatableVerticalSpace - columns * horizontalSpacing
71 Repeater {
72 id: repeater1
73 model: (root.model) ? root.model[0] : null
74 }
75 Repeater {
76 id: repeater2
77 model: (root.model) ? root.model[1] : null
78 delegate: repeater1.delegate
79 }
80 }