Debian Codegraph @

src/app/qml/ui/ImagePreview.qml

raw
1 /*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import Lomiri.Components.Popups 1.3
20
21 Page {
22 id: root
23 property var model
24 property alias source: imageRenderer.source
25 property var zoomable: true
26 property alias imageStatus: imageRenderer.status
27 property alias asynchronous: imageRenderer.asynchronous
28
29 header: PageHeader {
30 title: model.fileName
31 trailingActionBar.actions: [
32 Action {
33 iconName: "external-link"
34 onTriggered: {
35 openLocalFile(model.filePath)
36 }
37 },
38 Action {
39 iconName: "info"
40 onTriggered: {
41 var props = {
42 "model": model
43 }
44 PopupUtils.open(Qt.resolvedUrl("../ui/FileDetailsPopover.qml"), root, props)
45 }
46 }
47 ]
48 }
49
50 AnimatedImage {
51 source: model.filePath
52 anchors.fill: parent
53 fillMode: Image.PreserveAspectFit
54 visible: model.filePath.indexOf(".gif") !== -1
55 }
56
57 Flickable {
58 id: flickable
59 objectName: "flickable"
60 clip: true // FIXME maybe we can remove this, or just not clip in few cases
61 contentHeight: imageContainer.height
62 contentWidth: imageContainer.width
63
64 onHeightChanged: image.resetScale()
65 onWidthChanged: image.resetScale()
66 anchors {
67 top: header.bottom
68 fill: parent
69 }
70 visible: model.filePath.indexOf(".gif") === -1
71
72 Item {
73 id: imageContainer
74 objectName: "imageContainer"
75 width: Math.max(image.width * image.scale, flickable.width)
76 height: Math.max(image.height * image.scale, flickable.height)
77
78 Item {
79 id: image
80 objectName: "image"
81 property alias imageStatus: imageRenderer.status
82 property var prevScale
83 anchors.centerIn: parent
84
85 signal imageReloaded
86
87 Image {
88 id: imageRenderer
89 objectName: "imageRenderer"
90 source: model.filePath
91 smooth: !flickable.movingVertically
92 anchors.fill: parent
93 fillMode: Image.PreserveAspectFit
94
95 readonly property int sourceSizeMultiplier: 3
96
97 sourceSize.width: root.width * sourceSizeMultiplier <= root.height * sourceSizeMultiplier ? root.width * sourceSizeMultiplier : 0
98 sourceSize.height: root.height * sourceSizeMultiplier <= root.width * sourceSizeMultiplier ? root.height * sourceSizeMultiplier : 0
99
100 onStatusChanged: {
101 if (status === Image.Ready) {
102 image.imageReloaded();
103 }
104 }
105 }
106
107 onImageReloaded: {
108 image.height = imageRenderer.implicitHeight
109 image.width = imageRenderer.implicitWidth
110 image.resetScale();
111 }
112
113 function resetScale() {
114 image.scale = Math.min(flickable.width / image.width, flickable.height / image.height);
115 pinchArea.minScale = image.scale;
116 prevScale = Math.min(image.scale, 1);
117 }
118
119 onScaleChanged: {
120 var currentWidth = width * scale
121 var currentHeight = height * scale
122 var scaleRatio = scale / prevScale
123 if (currentWidth > flickable.width) {
124 var xpos = flickable.width / 2 + flickable.contentX;
125 var xoff = xpos * scaleRatio;
126 flickable.contentX = xoff - flickable.width / 2;
127 }
128 if (currentHeight > flickable.height) {
129 var ypos = flickable.height / 2 + flickable.contentY;
130 var yoff = ypos * scaleRatio;
131 flickable.contentY = yoff - flickable.height / 2;
132 }
133 prevScale = scale;
134 }
135 }
136 }
137
138 PinchArea {
139 id: pinchArea
140 objectName: "pinchArea"
141 property real minScale: 1.0
142 anchors.fill: parent
143 enabled: zoomable ? zoomable : false
144
145 pinch.target: image
146 pinch.minimumScale: minScale
147 pinch.maximumScale: 10
148
149 onPinchFinished: flickable.returnToBounds()
150 }
151
152 MouseArea {
153 id: mouseArea
154 objectName: "mouseArea"
155
156 anchors.fill: parent
157 enabled: zoomable ? zoomable : false
158
159 onWheel: {
160 var startScale = image.scale;
161 if (wheel.angleDelta.y > 0) {
162 image.scale = startScale + 0.1;
163 } else if (wheel.angleDelta.y < 0) {
164 if (image.scale > 0.1 && image.scale > pinchArea.minScale) {
165 image.scale = startScale - 0.1;
166 }
167 }
168 wheel.accepted = true;
169 }
170
171 onPressed: {
172 mouse.accepted = false;
173 }
174
175 onReleased: {
176 mouse.accepted = false;
177 }
178
179 onClicked: {
180 mouse.accepted = false;
181 }
182 }
183 }
184 }