summaryrefslogtreecommitdiffstats
path: root/src/mobile/qml/PagedGrid.qml
blob: 9543ad0852ad5417558a6bc5695f4c14d9d72953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/****************************************************************************
 *   Copyright (C) 2012  Instituto Nokia de Tecnologia (INdT)               *
 *                                                                          *
 *   This file may be used under the terms of the GNU Lesser                *
 *   General Public License version 2.1 as published by the Free Software   *
 *   Foundation and appearing in the file LICENSE.LGPL included in the      *
 *   packaging of this file.  Please review the following information to    *
 *   ensure the GNU Lesser General Public License version 2.1 requirements  *
 *   will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.   *
 *                                                                          *
 *   This program is distributed in the hope that it will be useful,        *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU Lesser General Public License for more details.                    *
 ****************************************************************************/

import QtQuick 2.0
import Snowshoe 1.0

Item {
    id: pagedGrid

    // Read/write properties.
    property QtObject model: null
    property Component delegate: null
    property Component emptyItemDelegate: null
    property int extraMargin: 40
    property int itemWidth: 192
    property int itemHeight: 263

    property int rowsPerPage: 2
    property int columnsPerPage: 2
    property int spacing: 16
    property int currentPage: 0
    property int maxPages: 1

    // Read only properties.
    property int page: 0
    property int pageWidth: columnsPerPage * itemWidth + (columnsPerPage - 1) * spacing
    property int pageHeight: rowsPerPage * itemHeight + (rowsPerPage - 1) * spacing
    property int itemsPerPage: rowsPerPage * columnsPerPage
    property alias pageCount: grid.pageCount

    width: grid.pageWidth + 2 * extraMargin
    height: grid.pageHeight
    clip: true

    // x and y will be given in coordinates relative to the clicked item.
    signal itemClicked(int index, int x, int y);

    function itemAt(index) {
        return grid.itemAt(index)
    }

    PageFillGrid {
        id: grid
        model: pagedGrid.model
        delegate: pagedGrid.delegate
        spacing: pagedGrid.spacing
        itemWidth: pagedGrid.itemWidth
        itemHeight: pagedGrid.itemHeight
        rowsPerPage: pagedGrid.rowsPerPage
        columnsPerPage: pagedGrid.columnsPerPage
        maxPages: pagedGrid.maxPages
        emptyItemDelegate: pagedGrid.emptyItemDelegate
        x: extraMargin - currentPage * (pageWidth + pagedGrid.spacing)

        Behavior on x {
            NumberAnimation { duration: 100 }
        }
    }

    SwipeArea {
        id: swipeArea
        anchors.fill: parent
        z: 1

        onClicked: {
            // Track down which item has been pressed on current page.
            var x = mouse.x - extraMargin;
            var y = mouse.y;
            if (!pageCount || x < 0 || x >= grid.pageWidth)
                return;

            var row = Math.floor(y / (itemHeight + spacing)), column = Math.floor(x / (itemWidth + spacing));
            var topLeftX = (itemWidth + spacing) * column, topLeftY = (itemHeight + spacing) * row;
            var bottomRightX = topLeftX + itemWidth, bottomRightY = topLeftY + itemHeight;
            if (x >= topLeftX && x <= bottomRightX && y >= topLeftY && y <= bottomRightY) {
                var itemIndex = currentPage * grid.itemsPerPage + row * columnsPerPage + column
                var item = grid.itemAt(itemIndex)
                if (item != null) {
                    // Emit a signal pointing the item clicked and the internal position of the click.
                    pagedGrid.itemClicked(itemIndex, x - topLeftX, y - topLeftY);
                }
            }
        }

        onSwipeLeft: {
            if (currentPage < pageCount - 1)
                ++currentPage;
        }

        onSwipeRight: {
            if (currentPage > 0)
                --currentPage;
        }
    }
}