Unity 8
PreviewZoomableImage.qml
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 Ubuntu.Components 1.3
19 import "../../Components"
20 
21 /*! \brief Preview widget for image.
22 
23  This widget shows image contained in widgetData["source"],
24  and falls back to widgetData["fallback"] if loading fails
25  can be zoomable accordingly with widgetData["zoomable"].
26  */
27 
28 PreviewWidget {
29  id: root
30  implicitWidth: units.gu(35)
31  implicitHeight: lazyImage.height
32 
33  widgetMargins: -units.gu(1)
34 
35  property Item rootItem: QuickUtils.rootItem(root)
36 
37  readonly property var imageUrl: widgetData["source"] || widgetData["fallback"] || ""
38 
39  LazyImage {
40  id: lazyImage
41  objectName: "lazyImage"
42  anchors {
43  left: parent.left
44  right: parent.right
45  }
46  scaleTo: "width"
47  source: root.imageUrl
48  asynchronous: true
49  useUbuntuShape: false
50  pressed: mouseArea.pressed
51 
52  MouseArea {
53  id: mouseArea
54  anchors.fill: parent
55  onClicked: {
56  overlay.initialX = rootItem.mapFromItem(parent, 0, 0).x;
57  overlay.initialY = rootItem.mapFromItem(parent, 0, 0).y;
58  overlay.show();
59  }
60  }
61 
62  Connections {
63  target: lazyImage.sourceImage
64  onStatusChanged: if (lazyImage.sourceImage.status === Image.Error) lazyImage.sourceImage.source = widgetData["fallback"];
65  }
66  Connections {
67  target: root
68  onImageUrlChanged: lazyImage.sourceImage.source = root.imageUrl;
69  }
70 
71  PreviewMediaToolbar {
72  id: toolbar
73  anchors {
74  left: parent.left
75  right: parent.right
76  bottom: parent.bottom
77  }
78  shareData: widgetData["share-data"]
79  }
80  }
81 
82  PreviewOverlay {
83  id: overlay
84  objectName: "overlay"
85  parent: rootItem
86  anchors.fill: parent
87  initialWidth: lazyImage.width
88  initialHeight: lazyImage.height
89 
90  delegate: ZoomableImage {
91  anchors.fill: parent
92  source: root.imageUrl
93  zoomable: widgetData["zoomable"] ? widgetData["zoomable"] : false
94  onStatusChanged: if (status === Image.Error) source = widgetData["fallback"];
95 
96  Connections {
97  target: root
98  onImageUrlChanged: source = root.imageUrl;
99  }
100  }
101  }
102 }