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

« back to all changes in this revision

Viewing changes to src/app/qml/PdfView.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:
24
24
    id: pdfPage
25
25
    title: Utils.getNameOfFile(file.path);
26
26
 
27
 
    // Disable header auto-hide
 
27
    // Disable header auto-hide.
 
28
    // TODO: Show/hide header if a user taps the page
28
29
    flickable: null
29
30
 
30
 
    property string currentPage: i18n.tr("Page %1 of %2").arg(pdfView.currentIndex + 1).arg(pdfView.count)
 
31
    property string currentPage: i18n.tr("Page %1 of %2").arg(pdfView.currentPageIndex + 1).arg(pdfView.count)
31
32
 
32
 
    // TODO: Restore zooming
33
 
    ListView {
 
33
    PDF.VerticalView {
34
34
        id: pdfView
35
35
        objectName:"pdfView"
36
36
 
37
 
        anchors {
38
 
            fill: parent
39
 
            leftMargin: units.gu(1)
40
 
            rightMargin: units.gu(1)
41
 
        }
 
37
        anchors.fill: parent
42
38
        spacing: units.gu(2)
43
39
 
44
40
        clip: true
45
 
        focus: false
46
41
        boundsBehavior: Flickable.StopAtBounds
47
42
 
48
 
        cacheBuffer: height
49
 
 
50
 
        highlightFollowsCurrentItem: false
51
 
        keyNavigationWraps: false
52
 
 
53
 
        header: Item { width: parent.width; height: units.gu(2) }
54
 
        footer: Item { width: parent.width; height: units.gu(2) }
55
 
 
56
 
        model: PDF.Document {
57
 
            id: poppler
58
 
 
59
 
            /* FIXME: Don't set 'path' property directly, but set it through onCompleted signal.
60
 
               By doing otherwise, PDF pages are loaded two times, but only
61
 
               the first delegates are working. Asking to the image provider
62
 
               to get the second ones, makes the app instable.
63
 
               (e.g. We have a PDF document with 10 pages. The plugin loads
64
 
               them twice - 2x10 = 20 pages - but only the first 10 are shown.
65
 
               While trying to get the 11th, the app crashes). */
66
 
            Component.onCompleted: path = file.path
67
 
 
68
 
            onPagesLoaded: {
69
 
                activity.running = false;
70
 
 
71
 
                pdfView.currentIndex = 0
72
 
 
73
 
                var title = getDocumentInfo("Title")
74
 
                if (title !== "")
75
 
                    pdfPage.title = title
76
 
            }
77
 
        }
78
 
 
79
 
        delegate: PdfViewDelegate {}
80
 
 
81
 
        onWidthChanged: {
82
 
            /* On resizing window, pages size changes but contentY is still the same.
83
 
               For that reason, it shows the wrong page (which is settled at the same contentY).
84
 
               We need to force flickable to show the current page. */
85
 
            //pdfView.positionViewAtIndex(currentIndex, ListView.Contain)
86
 
        }
87
 
 
88
 
        onContentYChanged: {
89
 
            // FIXME: On wheeling up, ListView automatically center currentItem to the view.
90
 
            //        This causes some strange "jump" of ~200px in contentY
91
 
            var i = pdfView.indexAt(pdfView.width * 0.5, contentY + (pdfView.height * 0.5))
92
 
 
93
 
            if (i < 0) {
94
 
                // returned index could be -1 when the delegate spacing is shown at the center of the view (e.g. while scrolling pages)
95
 
                i = pdfView.indexAt(pdfView.width * 0.5, contentY + (pdfView.height * 0.5) + units.gu(4))
96
 
            }
97
 
 
98
 
            if (i !== -1) {
99
 
                currentPage = i18n.tr("Page %1 of %2").arg(i + 1).arg(pdfView.count)
100
 
 
101
 
                if (!pdfView.flickingVertically) {
102
 
                    pdfView.currentIndex = i
103
 
                }
104
 
            }
105
 
        }
106
 
    }
107
 
 
108
 
    ActivityIndicator {
109
 
        id: activity
110
 
        anchors.centerIn: parent
111
 
 
112
 
        running: true
113
 
    }
 
43
        cacheBuffer: height * poppler.providersNumber * _zoomHelper.scale * 0.5
 
44
 
 
45
        flickDeceleration: 1500 * units.gridUnit / 8
 
46
        maximumFlickVelocity: 2500 * units.gridUnit / 8
 
47
 
 
48
        model: poppler
 
49
        delegate: PdfViewDelegate {
 
50
            onWidthChanged: QQuickView.releaseResources()
 
51
            Component.onDestruction: QQuickView.releaseResources()
 
52
        }
 
53
 
 
54
        contentWidth: parent.width * _zoomHelper.scale
 
55
        PinchArea {
 
56
            id: pinchy
 
57
            anchors.fill: parent
 
58
 
 
59
            pinch {
 
60
                target: _zoomHelper
 
61
                minimumScale: 1.0
 
62
                maximumScale: 2.5
 
63
            }
 
64
 
 
65
            onPinchStarted: pdfView.interactive = false;
 
66
 
 
67
            // FIXME: On zooming, keep the same content position.
 
68
            // onPinchUpdated: {}
 
69
 
 
70
            onPinchFinished: {
 
71
                pdfView.interactive = true;
 
72
                pdfView.returnToBounds();
 
73
            }
 
74
        }
 
75
 
 
76
        Item { id: _zoomHelper }
 
77
    }
 
78
 
 
79
    PDF.Document {
 
80
        id: poppler
 
81
 
 
82
        property bool isLoading: true
 
83
 
 
84
        Component.onCompleted: path = file.path
 
85
        onPagesLoaded: {
 
86
            isLoading = false;
 
87
 
 
88
            var title = getDocumentInfo("Title")
 
89
            if (title !== "")
 
90
                pdfPage.title = title
 
91
        }
 
92
    }
 
93
 
114
94
 
115
95
    // *** HEADER ***
116
96
    state: "default"