Debian Codegraph @

rc/qml/AlbumViewer/AlbumSpreadViewer.qml

raw
1 /*
2 * Copyright (C) 2012-2015 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 version 3 as
6 * published by the Free Software Foundation.
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 * Authors:
17 * Jim Nelson <[email protected]>
18 * Charles Lindsay <[email protected]>
19 */
20
21 import QtQuick 2.9
22 import Gallery 1.0
23 import "../../js/GalleryUtility.js" as GalleryUtility
24 import "AlbumInternals"
25
26 // The AlbumSpreadViewer is a specialty controller for displaying and viewing
27 // albums one spread (two pages side by side like an open book) at a time, and
28 // also one page at a time in the "magical" portrait layout where every page is
29 // on the right hand side of the album. Also encapsulates the logic of turning
30 // the pages from a swipe or via a page indicator button (see AlbumPageFlipper
31 // for details).
32 Item {
33 id: albumSpreadViewer
34
35 /*!
36 */
37 signal pageReleased()
38 /*!
39 */
40 signal pageFlipped()
41
42 // public
43 /*!
44 */
45 property Album album
46 /*!
47 */
48 property alias destinationPage: flipper.destinationPage
49 /*!
50 */
51 property alias duration: flipper.duration
52 /*!
53 */
54 property alias flipFraction: flipper.flipFraction
55 /*!
56 */
57 property bool load: false
58 /*!
59 */
60 property bool freeze: false // See AlbumPageComponent
61 /*!
62 */
63 property bool showCover: true // See AlbumPageComponent
64
65 // readonly
66 /*!
67 */
68 property alias isFlipping: flipper.isFlipping
69 /*!
70 */
71 property alias isRunning: flipper.isRunning
72 /*!
73 */
74 property bool isPortrait: application.isPortrait
75 // Same as album.currentPage, unless we're in portrait mode in which case it
76 // can be either the left OR right page.
77 property int viewingPage: -1
78 /*!
79 */
80 property int pagesPerSpread: (isPortrait ? 1 : 2)
81 /*!
82 */
83 property alias leftPageComponent: left
84 /*!
85 */
86 property alias rightPageComponent: right
87
88 /*!
89 */
90 function flip() {
91 flipper.flipToDestination();
92 }
93
94 /*!
95 */
96 function release() {
97 flipper.flipToOrigin();
98 }
99
100 /*!
101 */
102 function flipTo(page) {
103 destinationPage = page;
104 flipFraction = 0;
105 flip();
106 }
107
108 // Returns whether the page is a content page that has any photos on it.
109 function isPopulatedContentPage(page) {
110 return (album && page >= album.firstContentPage
111 && page <= album.lastPopulatedContentPage);
112 }
113
114 // Converts a page number into the appropriate page number to place on the
115 // left-hand side of the component
116 function getLeftHandPageNumber(pageNumber) {
117 if (pageNumber <= album.firstValidCurrentPage)
118 return album.firstValidCurrentPage;
119
120 if (pageNumber >= album.lastValidCurrentPage)
121 return album.lastValidCurrentPage;
122
123 return GalleryUtility.isOdd(pageNumber) ? pageNumber : pageNumber - 1;
124 }
125
126 // public
127 /*!
128 */
129 function hitTestFrame(x, y, relativeTo) {
130 // current visible photos are on the back of the left page and the front
131 // of the right page
132 var hit = hitTestPage(left.back, x, y, relativeTo);
133 if (hit)
134 return hit;
135
136 return hitTestPage(right.front, x, y, relativeTo);
137 }
138
139 // internal
140 /*!
141 */
142 function hitTestPage(page, x, y, relativeTo) {
143 if (!page.mediaFrames)
144 return undefined;
145
146 var ctr;
147 for (ctr = 0; ctr < page.mediaFrames.length; ctr++) {
148 var rect = GalleryUtility.getRectRelativeTo(page.mediaFrames[ctr], relativeTo);
149 if (GalleryUtility.doesPointIntersectRect(x, y, rect))
150 return page.mediaFrames[ctr];
151 }
152
153 return undefined;
154 }
155
156 // public
157 /*!
158 */
159 function getRectOfMediaSource(media) {
160 // current visible photos are on the back of the left page and the front of
161 // the right page
162 var rect = searchPageForMedia(left.back, media);
163 if (rect)
164 return rect;
165
166 return searchPageForMedia(right.front, media);
167 }
168
169 // private
170 /*!
171 */
172 function searchPageForMedia(page, media) {
173 if (!page.mediaFrames)
174 return undefined;
175
176 var ctr;
177 for (ctr = 0; ctr < page.mediaFrames.length; ctr++) {
178 if (page.mediaFrames[ctr].mediaSource && page.mediaFrames[ctr].mediaSource.equals(media))
179 return page.mediaFrames[ctr];
180 }
181
182 return undefined;
183 }
184
185 onAlbumChanged: viewingPage = (album ? album.currentPage : -1)
186 onIsPortraitChanged: {
187 // Make sure we're on a valid currentPage when switching back to regular
188 // mode from portrait.
189 if (!isPortrait && GalleryUtility.isEven(viewingPage))
190 --viewingPage;
191 }
192
193 Item {
194 id: pageArea
195
196 anchors.top: parent.top
197 anchors.bottom: parent.bottom
198 anchors.left: (isPortrait ? parent.left : parent.horizontalCenter)
199 anchors.right: parent.right
200
201 AlbumPageComponent {
202 id: left
203
204 anchors.fill: parent
205
206 visible: freeze || (backPage >= 0 && !isPortrait)
207 load: albumSpreadViewer.load
208
209 album: albumSpreadViewer.album
210
211 backPage: leftPageForCurrent(flipper.isFlipInProgress
212 ? flipper.firstPage
213 : viewingPage)
214
215 flipFraction: 1
216 freeze: albumSpreadViewer.freeze
217 showCover: albumSpreadViewer.showCover
218 }
219
220 AlbumPageComponent {
221 id: right
222
223 anchors.fill: parent
224
225 visible: freeze || (Boolean(album) && frontPage < album.totalPageCount)
226 load: albumSpreadViewer.load
227
228 album: albumSpreadViewer.album
229
230 frontPage: {
231 var page = (flipper.isFlipInProgress ? flipper.lastPage : viewingPage);
232 return (isPortrait ? page : rightPageForCurrent(page));
233 }
234
235 flipFraction: 0
236 freeze: albumSpreadViewer.freeze
237 showCover: albumSpreadViewer.showCover
238 }
239
240 AlbumPageFlipper {
241 id: flipper
242
243 // Read only.
244 // Used instead of isFlipping to enforce the proper order of operations;
245 // isFlipping will be set to false before the page number is updated in
246 // onFlipFinished, resulting in an extraneous page reload.
247 property bool isFlipInProgress: false
248
249 anchors.fill: parent
250
251 visible: isFlipInProgress
252
253 album: albumSpreadViewer.album
254 currentPage: viewingPage
255 isPortrait: albumSpreadViewer.isPortrait
256 pagesPerSpread: albumSpreadViewer.pagesPerSpread
257
258 onIsFlippingChanged: {
259 if (isFlipping)
260 isFlipInProgress = true;
261 }
262
263 onFlipFinished: {
264 if (!album)
265 return;
266
267 if (toDestination) {
268 if (destinationPage == album.firstValidCurrentPage) {
269 album.closed = true;
270 } else {
271 album.currentPage = getLeftHandPageNumber(destinationPage);
272 album.closed = false;
273 }
274 viewingPage = destinationPage;
275 isFlipInProgress = false; // must be set AFTER viewingPage!
276 pageFlipped();
277 } else {
278 pageReleased();
279 }
280 }
281 }
282 }
283 }