~ci-train-bot/unity8/unity8-ubuntu-zesty-2167

« back to all changes in this revision

Viewing changes to Components/ResponsiveFlowView.qml

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 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
 
36
 
 
37
    height: flow.height + flow.anchors.topMargin
 
38
 
 
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
        }
 
49
 
 
50
        function pixelToGU(value) {
 
51
            return Math.floor(value / units.gu(1));
 
52
        }
 
53
 
 
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
        }
 
59
 
 
60
        function columnsForSpacing(space) {
 
61
            // minimum margin is half of the spacing
 
62
            return Math.floor((parent.width - space/2) / (referenceDelegateWidth + space));
 
63
        }
 
64
 
 
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
 
70
 
 
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
    }
 
81
}