~macslow/unity8/fix-1475678

« back to all changes in this revision

Viewing changes to Components/ResponsiveGridView.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
/*
 
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
    property int delegateWidth
 
30
    property int delegateHeight
 
31
    property alias model: gridView.model
 
32
    property alias delegate: gridView.delegate
 
33
    readonly property int cellWidth: gridView.cellWidth
 
34
    readonly property int cellHeight: gridView.cellHeight
 
35
    readonly property int totalContentHeight: Math.ceil(gridView.model.count / columns) * cellHeight + verticalSpacing
 
36
    property alias interactive: gridView.interactive
 
37
    readonly property alias flicking: gridView.flicking
 
38
    readonly property alias moving: gridView.moving
 
39
    readonly property alias pressDelay: gridView.pressDelay
 
40
 
 
41
    GridView {
 
42
        id: gridView
 
43
        objectName: "responsiveGridViewGrid"
 
44
        anchors {
 
45
            fill: parent
 
46
            leftMargin: margin/2
 
47
            rightMargin: margin/2
 
48
            topMargin: verticalSpacing
 
49
        }
 
50
        clip: parent.height != totalContentHeight
 
51
 
 
52
        function pixelToGU(value) {
 
53
            return Math.floor(value / units.gu(1));
 
54
        }
 
55
 
 
56
        function spacingForColumns(columns) {
 
57
            // spacing between columns as an integer number of GU, the remainder goes in the margins
 
58
            var spacingGU = pixelToGU(allocatableHorizontalSpace / columns);
 
59
            return units.gu(spacingGU);
 
60
        }
 
61
 
 
62
        function columnsForSpacing(spacing) {
 
63
            // minimum margin is half of the spacing
 
64
            return Math.floor((parent.width - spacing/2) / (delegateWidth + spacing));
 
65
        }
 
66
 
 
67
        property real allocatableHorizontalSpace: parent.width - columns * delegateWidth
 
68
        property int columns: Math.min(columnsForSpacing(minimumHorizontalSpacing), maximumNumberOfColumns)
 
69
        property real horizontalSpacing: spacingForColumns(columns)
 
70
        property real verticalSpacing: horizontalSpacing
 
71
        property int margin: allocatableHorizontalSpace - columns * horizontalSpacing
 
72
 
 
73
        cellWidth: delegateWidth + horizontalSpacing
 
74
        cellHeight: delegateHeight + verticalSpacing
 
75
    }
 
76
}