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

« back to all changes in this revision

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

  • Committer: David Planella
  • Date: 2014-10-28 22:01:16 UTC
  • mfrom: (31.2.8 add-poppler-plugin)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: david.planella@ubuntu.com-20141028220116-bryaeq3if69o6r6o
Merged branch to add Poppler plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import QtQuick 2.3
2
2
import Ubuntu.Components 1.1
3
 
import org.docviewer.poppler 1.0
 
3
import org.ubuntu.popplerqmlplugin 1.0
4
4
 
5
5
import "utils.js" as Utils
6
6
 
8
8
    id: pageMain
9
9
    title: Utils.getNameOfFile(file.path);
10
10
 
11
 
    head.actions: [
12
 
        Action {
13
 
            text: i18n.tr("Details")
14
 
            iconName: "info"
15
 
            onTriggered: pageStack.push(Qt.resolvedUrl("DetailsPage.qml"))
16
 
        }
 
11
    // Disable header auto-hide
 
12
    flickable: null
 
13
 
 
14
    // TODO: Restore zooming
 
15
    ListView {
 
16
        id: pdfView
 
17
        anchors {
 
18
            fill: parent
 
19
            leftMargin: units.gu(2)
 
20
            rightMargin: units.gu(2)
 
21
        }
 
22
        spacing: units.gu(4)
 
23
 
 
24
        clip: true
 
25
        focus: false
 
26
        boundsBehavior: Flickable.StopAtBounds
 
27
 
 
28
        cacheBuffer: height
 
29
 
 
30
        highlightFollowsCurrentItem: false
 
31
        keyNavigationWraps: false
 
32
 
 
33
        // TODO: Not a good way to have spacing
 
34
        header: Item {
 
35
            width: parent.width
 
36
            height: units.gu(2)
 
37
        }
 
38
 
 
39
        footer: Item {
 
40
            width: parent.width
 
41
            height: units.gu(2)
 
42
        }
 
43
 
 
44
        model: Poppler {
 
45
            id: poppler
 
46
            path: file.path
 
47
 
 
48
            onPagesLoaded: {
 
49
                activity.running = false;
 
50
 
 
51
                pdfView.currentIndex = 0
 
52
 
 
53
                var title = getDocumentInfo("Title")
 
54
                if (title !== "")
 
55
                    titleLabel.text = title
 
56
            }
 
57
        }
 
58
 
 
59
        delegate: PdfPage {}
 
60
 
 
61
        onWidthChanged: {
 
62
            /* On resizing window, pages size changes but contentY is still the same.
 
63
               For that reason, it shows the wrong page (which is settled at the same contentY).
 
64
               We need to force flickable to show the current page. */
 
65
            //pdfView.positionViewAtIndex(currentIndex, ListView.Contain)
 
66
        }
 
67
 
 
68
        onContentYChanged: {
 
69
            // FIXME: On wheeling up, ListView automatically center currentItem to the view.
 
70
            //        This causes some strange "jump" of ~200px in contentY
 
71
            var i = pdfView.indexAt(pdfView.width * 0.5, contentY + (pdfView.height * 0.5))
 
72
 
 
73
            if (i < 0) {
 
74
                // returned index could be -1 when the delegate spacing is shown at the center of the view (e.g. while scrolling pages)
 
75
                i = pdfView.indexAt(pdfView.width * 0.5, contentY + (pdfView.height * 0.5) + units.gu(4))
 
76
            }
 
77
 
 
78
            if (i !== -1) {
 
79
                currentPageLabel.text = i18n.tr("Page %1 of %2").arg(i + 1).arg(pdfView.count)
 
80
 
 
81
                if (!pdfView.flickingVertically) {
 
82
                    pdfView.currentIndex = i
 
83
                }
 
84
            }
 
85
        }
 
86
    }
 
87
 
 
88
    ActivityIndicator {
 
89
        id: activity
 
90
        anchors.centerIn: parent
 
91
 
 
92
        running: true
 
93
    }
 
94
 
 
95
    // *** HEADER ***
 
96
    state: "default"
 
97
    states: [
 
98
        PageHeadState {
 
99
            name: "default"
 
100
            head: pageMain.head
 
101
 
 
102
            contents: Column {
 
103
                anchors.centerIn: parent
 
104
 
 
105
                Label {
 
106
                    id: titleLabel
 
107
                    text: Utils.getNameOfFile(file.path)
 
108
                    font.weight: Font.DemiBold
 
109
                    anchors.horizontalCenter: parent.horizontalCenter
 
110
                }
 
111
                Label {
 
112
                    id: currentPageLabel
 
113
                    text: i18n.tr("Page %1 of %2").arg(pdfView.currentIndex + 1).arg(pdfView.count)
 
114
                    fontSize: "small"
 
115
                    anchors.horizontalCenter: parent.horizontalCenter
 
116
                }
 
117
            }
 
118
 
 
119
            backAction: Action {
 
120
                iconName: "back"
 
121
            }
 
122
 
 
123
            actions: [
 
124
                Action {
 
125
                    iconName: "search"
 
126
                   // onTriggered: pageMain.state = "search"
 
127
                    //Disable it until we provide search in Poppler plugin.
 
128
                    enabled: false
 
129
                },
 
130
 
 
131
                Action {
 
132
                    iconName: "browser-tabs"
 
133
                    text: "Go to page..."
 
134
                    enabled: false
 
135
                },
 
136
 
 
137
                Action {
 
138
                    text: i18n.tr("Details")
 
139
                    iconName: "info"
 
140
                    onTriggered: pageStack.push(Qt.resolvedUrl("DetailsPage.qml"))
 
141
                }
 
142
            ]
 
143
        }
 
144
 
 
145
       /* PageHeadState {
 
146
            id: headerState
 
147
            name: "search"
 
148
            head: pageMain.head
 
149
            actions: [
 
150
                Action {
 
151
                    iconName: "go-up"
 
152
                },
 
153
 
 
154
                Action {
 
155
                    iconName: "go-down"
 
156
                }
 
157
            ]
 
158
 
 
159
            backAction: Action {
 
160
                id: leaveSearchAction
 
161
                text: "back"
 
162
                iconName: "back"
 
163
                onTriggered: pageMain.state = "default"
 
164
            }
 
165
        }*/
17
166
    ]
18
 
 
19
 
    Flickable {
20
 
        id: flickable
21
 
        anchors.fill: parent
22
 
 
23
 
        contentHeight: columnPages.height + 10
24
 
        contentWidth: parent.width
25
 
 
26
 
        PinchArea {
27
 
            id: pinchy
28
 
            anchors.fill: columnPages
29
 
            enabled: true
30
 
            pinch.target: flickable
31
 
 
32
 
            property real lastWidth
33
 
            /*property real lastHeight
34
 
        property double p1toC_X
35
 
        property double p1toC_Y
36
 
        property double contentInitX
37
 
        property double contentInitY*/
38
 
 
39
 
            onPinchStarted: {
40
 
                lastWidth = flickable.width
41
 
 
42
 
                /*contentInitX = flickImg.contentX
43
 
            contentInitY = flickImg.contentY*/
44
 
 
45
 
            }
46
 
 
47
 
            onPinchUpdated: {
48
 
 
49
 
                var newWidth = 0;
50
 
 
51
 
                newWidth = lastWidth*pinch.scale;
52
 
 
53
 
                /*if (newWidth < image.startWidth)
54
 
                newWidth = image.startWidth;
55
 
            else if (newWidth > image.sourceSize.width)
56
 
                newWidth = image.sourceSize.width;*/
57
 
 
58
 
                flickable.contentWidth = newWidth;
59
 
 
60
 
                /*flickImg.contentX = contentInitX-(lastWidth-newWidth)/2
61
 
            flickImg.contentY = contentInitY-(lastHeight-image.height)/2*/
62
 
 
63
 
            }
64
 
 
65
 
            onPinchFinished: {
66
 
 
67
 
                columnPages.shouldReloadImg = true;
68
 
                console.log("FINISHED");
69
 
            }
70
 
        }
71
 
 
72
 
 
73
 
        Poppler {
74
 
            id: popplerProp
75
 
            path: file.path
76
 
        }
77
 
 
78
 
        Column {
79
 
            id: columnPages
80
 
 
81
 
            property bool shouldReloadImg : false
82
 
 
83
 
            width: parent.width - 10
84
 
            x: 5
85
 
            y: 5
86
 
            spacing: 10
87
 
 
88
 
            onWidthChanged: {
89
 
                if (!pinchy.pinch.active)
90
 
                    shouldReloadImg=true
91
 
                else
92
 
                    shouldReloadImg=false
93
 
            }
94
 
 
95
 
        }
96
 
 
97
 
        Component.onCompleted: {
98
 
 
99
 
            var i=0;
100
 
            for(i=1; i <= popplerProp.numPages; i++)
101
 
            {
102
 
                var component = Qt.createComponent("PdfPage.qml");
103
 
 
104
 
                if (component.status === Component.Error)
105
 
                {
106
 
                    console.debug("Error creating component");
107
 
                }
108
 
                else
109
 
                {
110
 
                    var page = component.createObject(columnPages);
111
 
 
112
 
                    page.source = "image://poppler/page/"+i;
113
 
                }
114
 
            }
115
 
        }
116
 
    }
117
167
}