Ubuntu Pastebin

Paste from sverzegnassi at Thu, 22 Jan 2015 21:29:00 +0000

Download as text
  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
109
110
111
112
113
114
115
import QtQuick 2.3
import Ubuntu.Components 1.1

Flickable {
    id: flickable

    property int currentIndex: -1
    property var currentItem

    property alias zoomHelper: _zoomHelper
    readonly property real zoom: _canvas.scale

    property int spacing: _layout.spacing
    property int cacheBuffer: height

    property alias model: _repeater.model
    property alias delegate: _repeater.delegate
    property alias count: _repeater.count

    contentWidth: _zoomHelper.width * _zoomHelper.scale
    contentHeight: _zoomHelper.height * _zoomHelper.scale

    // Indipendent GU flickable speed workaround
    flickDeceleration: 1500 * units.gridUnit / 8
    maximumFlickVelocity: 2500 * units.gridUnit / 8

    function indexAt(x, y) {
        var item = _canvas.childAt(x,y) //_canvas.childAt(x, y);
        if (item) {
            return item.index
        }

        return -1
    }

    function positionViewAtIndex(index) {
        flickable.contentY = _repeater.itemAt(index).y
    }

    function _setCurrentIndex() {
        var i = flickable.indexAt(width * 0.5, contentY + (height * 0.5))

        if (i < 0) {
            // returned index could be -1 when the delegate spacing is shown at the center of the view (e.g. while scrolling pages)
            i = flickable.indexAt(width * 0.5, contentY + (height * 0.5) + units.gu(4))
        }

        if (i !== -1) {
                currentIndex = i
        }
    }

    // Ensures that the correct items are visible
    function _timedUpdateItemVisibility() {
        _timer.restart()
    }

    function _updateItemVisibility()
    {
        for (var i=0; i<_repeater.count; i++) {
            var item = _repeater.itemAt(i)
            item.visible = (item.y + item.height) > (flickable.contentY - flickable.cacheBuffer) &&
                           item.y < (flickable.contentY + flickable.height + flickable.cacheBuffer)

            if (item.visible) {
                item.opacity = 1.0
            } else {
                item.opacity = 0.0
            }

            QQuickView.releaseResources()
        }
    }

    // FIXME: currentPage should be kept shown
    onContentWidthChanged: _updateItemVisibility()
    onHeightChanged: _updateItemVisibility()
    onContentYChanged: _timedUpdateItemVisibility()
    Component.onCompleted: _updateItemVisibility()

    // This works only with touch events, but it's a good compromise at the moment (no convergence to desktop planned yet)
    onFlickingVerticallyChanged: if (!flickable.flickingVertically) { _setCurrentIndex(); }

    onCurrentIndexChanged: currentItem = _repeater.itemAt(currentIndex)

    Timer {
        id: _timer
        interval: 10
        onTriggered: _updateItemVisibility()
    }

    Item {
        id: _canvas
        anchors.fill: parent

        Repeater {
            id: _repeater

            onItemAdded: {
                if (index > 0) {
                    item.anchors.top = itemAt(index - 1).bottom
                    item.anchors.topMargin = flickable.spacing
                }

                _updateItemVisibility()
            }
        }
    }

    Item {
        id: _zoomHelper
        width: flickable.width
        height: if (count > 0) { _repeater.itemAt(count - 1).y + _repeater.itemAt(count - 1).height; }
    }
}
Download as text