Unity 8
PreviewImageGallery.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 1.1
19 import "../../Components"
20 
21 /*! This preview widget shows a horizontal list of images.
22  * The URIs for the images should be an array in widgetData["sources"].
23  */
24 
25 PreviewWidget {
26  id: root
27  implicitHeight: units.gu(22)
28 
29  property Item rootItem: QuickUtils.rootItem(root)
30 
31  ListView {
32  id: previewImageListView
33  objectName: "previewImageListView"
34  spacing: units.gu(1)
35  anchors.fill: parent
36  orientation: ListView.Horizontal
37  cacheBuffer: width * 3
38  model: root.widgetData["sources"]
39  clip: true
40 
41  onCurrentIndexChanged: overlay.updateInitialItem()
42 
43  LazyImage {
44  objectName: "placeholderScreenshot"
45  anchors {
46  top: parent.top
47  bottom: parent.bottom
48  }
49  scaleTo: "height"
50  source: "broken_image"
51  initialWidth: units.gu(13)
52  visible: previewImageListView.count == 0
53  }
54 
55  delegate: LazyImage {
56  objectName: "previewImage" + index
57  anchors {
58  top: parent.top
59  bottom: parent.bottom
60  }
61  source: modelData ? modelData : ""
62  scaleTo: "height"
63  initialWidth: units.gu(13)
64  borderSource: mouseArea.pressed ? "radius_pressed.sci" : "radius_idle.sci"
65 
66  MouseArea {
67  id: mouseArea
68  anchors.fill: parent
69  onClicked: {
70  previewImageListView.currentIndex = index;
71  overlay.updateInitialItem();
72  overlay.show();
73  }
74  }
75  }
76  }
77 
78  PreviewOverlay {
79  id: overlay
80  objectName: "overlay"
81  parent: rootItem
82  anchors.fill: parent
83 
84  function updateInitialItem() {
85  initialX = rootItem.mapFromItem(previewImageListView.currentItem, 0, 0).x;
86  initialY = rootItem.mapFromItem(previewImageListView.currentItem, 0, 0).y;
87  initialWidth = previewImageListView.currentItem.width;
88  initialHeight = previewImageListView.currentItem.height;
89  }
90 
91  delegate: ListView {
92  id: overlayListView
93  objectName: "overlayListView"
94  anchors.fill: parent
95  orientation: ListView.Horizontal
96  highlightRangeMode: ListView.StrictlyEnforceRange
97  highlightMoveDuration: 0
98  snapMode: ListView.SnapOneItem
99  boundsBehavior: Flickable.DragAndOvershootBounds
100  model: root.widgetData["sources"]
101  currentIndex: previewImageListView.currentIndex
102 
103  onCurrentIndexChanged: {
104  // if the index changed while overlay is visible, it was from user interaction,
105  // let's update the index of the original listview
106  if (overlay.visible) {
107  previewImageListView.highlightMoveDuration = 0;
108  previewImageListView.highlightResizeDuration = 0;
109  previewImageListView.currentIndex = currentIndex;
110  previewImageListView.highlightMoveDuration = -1;
111  previewImageListView.highlightResizeDuration = -1;
112  }
113  }
114 
115  delegate: Image {
116  id: screenshot
117  anchors {
118  top: parent.top
119  bottom: parent.bottom
120  }
121  width: overlay.width
122  source: modelData ? modelData : ""
123  fillMode: Image.PreserveAspectFit
124  sourceSize { width: screenshot.width; height: screenshot.height }
125  }
126 
127  MouseArea {
128  anchors.fill: parent
129  onClicked: overlay.headerShown = !overlay.headerShown
130  }
131  }
132  }
133 }