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  LazyImage {
38  id: lazyImage
39  objectName: "lazyImage"
40  anchors {
41  left: parent.left
42  right: parent.right
43  }
44  scaleTo: "width"
45  source: widgetData["source"]
46  asynchronous: true
47  useUbuntuShape: false
48  pressed: mouseArea.pressed
49 
50  MouseArea {
51  id: mouseArea
52  anchors.fill: parent
53  onClicked: {
54  overlay.initialX = rootItem.mapFromItem(parent, 0, 0).x;
55  overlay.initialY = rootItem.mapFromItem(parent, 0, 0).y;
56  overlay.show();
57  }
58  }
59 
60  Connections {
61  target: lazyImage.sourceImage
62  // If modelData would change after failing to load it would not be
63  // reloaded since the source binding is destroyed by the source = fallback
64  // But at the moment the model never changes
65  onStatusChanged: if (lazyImage.sourceImage.status === Image.Error) lazyImage.sourceImage.source = widgetData["fallback"];
66  }
67 
68  PreviewMediaToolbar {
69  id: toolbar
70  anchors {
71  left: parent.left
72  right: parent.right
73  bottom: parent.bottom
74  }
75  shareData: widgetData["shareData"]
76  }
77  }
78 
79  PreviewOverlay {
80  id: overlay
81  objectName: "overlay"
82  parent: rootItem
83  anchors.fill: parent
84  initialWidth: lazyImage.width
85  initialHeight: lazyImage.height
86 
87  delegate: ZoomableImage {
88  anchors.fill: parent
89  source: widgetData["source"]
90  zoomable: widgetData["zoomable"] ? widgetData["zoomable"] : false
91  // If modelData would change after failing to load it would not be
92  // reloaded since the source binding is destroyed by the source = fallback
93  // But at the moment the model never changes
94  onStatusChanged: if (status === Image.Error) source = widgetData["fallback"];
95  }
96  }
97 }