~mutse-young/ubuntu-docviewer-app/trunk

« back to all changes in this revision

Viewing changes to src/app/qml/PdfViewDelegate.qml

  • Committer: Tarmac
  • Author(s): Stefano Verzegnassi
  • Date: 2015-02-04 15:37:54 UTC
  • mfrom: (63.2.15 20-enable-zoom)
  • Revision ID: tarmac-20150204153754-1jrf9jzk34t9g3ho
Enable zoom in PDF view & multithreading support. Fixes: https://bugs.launchpad.net/bugs/1399978.

Approved by Ubuntu Phone Apps Jenkins Bot, Riccardo Padovani.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2013-2014 Canonical, Ltd.
 
2
 * Copyright (C) 2013-2015 Canonical, Ltd.
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by
13
13
 * You should have received a copy of the GNU General Public License
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 */
16
 
 
17
16
import QtQuick 2.3
18
17
import Ubuntu.Components 1.1
19
 
import QtGraphicalEffects 1.0
20
18
 
21
19
Rectangle {
22
20
    id: pdfPage
 
21
 
 
22
    property int index: model.index
 
23
    property bool _previewFetched: false
 
24
 
 
25
    property alias status: pageImg.status
 
26
 
23
27
    width: parent.width
24
 
 
25
28
    height: width * (model.height / model.width)
26
 
 
27
 
    border {
28
 
        width: 1
29
 
        color: "#808080"
 
29
    color: "#E6E6E6"
 
30
 
 
31
    // Preview page rendering. Used as placeholder while zooming the page.
 
32
    // We generate the low resolution preview from the texture of the PDF page,
 
33
    // so that we can keep page rendering as fast as possible.
 
34
    ShaderEffectSource {
 
35
        id: previewImg
 
36
        anchors.fill: parent
 
37
 
 
38
        // We cannot change its opacity or visibility, otherwise the texture will be refreshed,
 
39
        // even if live is false.
 
40
        live: false
 
41
        textureSize: Qt.size(256, 256 * (model.height / model.width))
30
42
    }
31
43
 
32
44
    Image {
33
 
        id: imagePage
34
 
        anchors {
35
 
            fill: parent
36
 
            margins: 1
37
 
        }
38
 
        asynchronous: true
39
 
        sourceSize.width: parent.width - 2
40
 
        fillMode: Image.PreserveAspectCrop
41
 
 
42
 
        Component.onCompleted: source = "image://poppler/page/" + model.index
43
 
    }
44
 
 
45
 
    Rectangle {
46
 
        anchors.fill: parent
47
 
        color: "white"
48
 
        visible: imagePage.status === Image.Loading
49
 
 
50
 
        ActivityIndicator {
51
 
            anchors.centerIn: parent
52
 
            running: parent.visible
53
 
        }
54
 
    }
55
 
 
56
 
    DropShadow {
57
 
        anchors.fill: parent
58
 
        cached: true;
59
 
        horizontalOffset: 0;
60
 
        verticalOffset: 2;
61
 
        radius: 8.0;
62
 
        samples: 16;
63
 
        color: "#80000000";
64
 
        smooth: true;
65
 
        source: parent;
66
 
        z: -10
 
45
        id: pageImg
 
46
        anchors.fill: parent
 
47
 
 
48
        source: "image://poppler" + (index % poppler.providersNumber) + "/page/" + index;
 
49
        sourceSize.width: pdfPage.width
 
50
 
 
51
        onStatusChanged: {
 
52
            // This is supposed to run the first time PdfViewDelegate gets the page rendering.
 
53
            if (!_previewFetched) {
 
54
                if (status == Image.Ready) {
 
55
                    previewImg.sourceItem = pageImg
 
56
                    // Re-assign sourceItem property, so the texture is not updated when Image status changes.
 
57
                    previewImg.sourceItem = pdfPage
 
58
                }
 
59
            }
 
60
        }
 
61
 
 
62
        // Request a new page rendering. The order, which pages are requested with, depends on the distance from the currentPage
 
63
        Timer {
 
64
            id: _zoomTimer
 
65
            interval: {
 
66
                var diff = Math.abs(pdfView.currentPageIndex - model.index)
 
67
                var prov = poppler.providersNumber * 0.5
 
68
 
 
69
                if (diff < prov)
 
70
                    return 0
 
71
                else
 
72
                    return (diff - prov) * 10
 
73
            }
 
74
 
 
75
            onTriggered: {
 
76
                pageImg.sourceSize.width = pdfPage.width;
 
77
            }
 
78
        }
 
79
    }
 
80
 
 
81
    // Page rendering depends on the width of PdfViewDelegate.
 
82
    // Because of this, we have multiple callings to ImageProvider while zooming.
 
83
    // Just avoid it.
 
84
    Connections {
 
85
        target: pinchy
 
86
 
 
87
        onPinchStarted: _zoomTimer.stop();
 
88
        onPinchUpdated: {
 
89
            // This ensures that page image is not reloaded when the maximumScale or minimumScale has already been reached.
 
90
            if ( !(_zoomHelper.scale >= 2.5 && pinch.scale > 1.0) && !(_zoomHelper.scale <= 1.0 && pinch.scale < 1.0) )
 
91
                pageImg.sourceSize.width = 0;
 
92
        }
 
93
        onPinchFinished: _zoomTimer.restart();
67
94
    }
68
95
}