2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
18 import Ubuntu.Components 0.1
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
36 height: flow.height + flow.anchors.topMargin
46 topMargin: verticalSpacing
49 function pixelToGU(value) {
50 return Math.floor(value / units.gu(1));
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);
59 function columnsForSpacing(space) {
60 // minimum margin is half of the spacing
61 return Math.floor((parent.width - space/2) / (referenceDelegateWidth + space));
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