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.0
18 import Ubuntu.Components 0.1
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  implicitHeight: units.gu(22)
31 
32  property Item rootItem: QuickUtils.rootItem(root)
33 
34  LazyImage {
35  id: lazyImage
36  objectName: "lazyImage"
37  anchors {
38  top: parent.top
39  bottom: parent.bottom
40  horizontalCenter: parent.horizontalCenter
41  }
42  scaleTo: "height"
43  source: widgetData["source"]
44  asynchronous: true
45 
46  borderSource: mouseArea.pressed ? "radius_pressed.sci" : "radius_idle.sci"
47 
48  MouseArea {
49  id: mouseArea
50  anchors.fill: parent
51  onClicked: {
52  overlay.initialX = rootItem.mapFromItem(parent, 0, 0).x;
53  overlay.initialY = rootItem.mapFromItem(parent, 0, 0).y;
54  overlay.show();
55  }
56  }
57 
58  Connections {
59  target: lazyImage.sourceImage
60  // If modelData would change after failing to load it would not be
61  // reloaded since the source binding is destroyed by the source = fallback
62  // But at the moment the model never changes
63  onStatusChanged: if (lazyImage.sourceImage.status === Image.Error) lazyImage.sourceImage.source = widgetData["fallback"];
64  }
65  }
66 
67  PreviewOverlay {
68  id: overlay
69  objectName: "overlay"
70  parent: rootItem
71  anchors.fill: parent
72  initialWidth: lazyImage.width
73  initialHeight: lazyImage.height
74 
75  delegate: ZoomableImage {
76  anchors.fill: parent
77  source: widgetData["source"]
78  zoomable: widgetData["zoomable"] ? widgetData["zoomable"] : false
79  // If modelData would change after failing to load it would not be
80  // reloaded since the source binding is destroyed by the source = fallback
81  // But at the moment the model never changes
82  onStatusChanged: if (status === Image.Error) source = widgetData["fallback"];
83  }
84  }
85 }