Debian Codegraph @

rc/qml/AlbumViewer/AlbumPageIndicator.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 * Eric Gregory <[email protected]>
19 */
20
21 import QtQuick 2.9
22 import Gallery 1.0
23 import Lomiri.Components 1.3
24
25 /*!
26 */
27 Rectangle {
28 id: pageIndicator
29
30 /*!
31 */
32 property Album album
33 /*!
34 */
35 property bool isPortrait: false
36 /*!
37 */
38 property int pagesPerSpread: 2
39
40 // Set this to the viewing page of the album spread.
41 property int viewingPage
42
43 // Read-only
44 property int indicatorDotCount: 0
45
46 /*!
47 */
48 signal selected(int page)
49
50 onAlbumChanged: pageIndicatorRepeater.filterModel()
51
52 Connections {
53 target: album
54 ignoreUnknownSignals: true
55 onContentPagesChanged: pageIndicatorRepeater.filterModel()
56 onPageCountChanged: pageIndicatorRepeater.filterModel()
57 }
58
59 onPagesPerSpreadChanged: pageIndicatorRepeater.filterModel();
60 onIsPortraitChanged: pageIndicatorRepeater.filterModel();
61
62 Row {
63 anchors.centerIn: parent
64
65 spacing: units.gu(5)
66
67 Repeater {
68 id: pageIndicatorRepeater
69
70 model: ListModel {}
71
72 // Eyeballed estimate of max indicators/width ratio.
73 property int maxIndicators: pageIndicator.parent.width / 90
74
75 // Re-compute when the # of max indicators has changed.
76 onMaxIndicatorsChanged: filterModel();
77
78 function filterModel() {
79 model.clear();
80
81 if (!album)
82 return;
83
84 // A "spread" is:
85 // Landscape mode: a pair of two adjacent pages
86 // Portrait mode: a single padge
87 var spreadCount = Math.ceil(
88 album.populatedContentPageCount / pagesPerSpread);
89
90 indicatorDotCount = spreadCount;
91
92 if(spreadCount > 1) {
93 var indicatorCount = Math.min(spreadCount, maxIndicators);
94
95 // Breakdown: we may have more pages than space for dots. At each
96 // step, we take the ratio (adjusted so that the last dot always has
97 // only the last one page), add it to an accumulator, then assign as
98 // many whole pages to each dot as were in the accumulator. At the
99 // end, due to rounding errors we may have a page left over, so we
100 // special-case it (and the last, single page).
101 var addend = (spreadCount - 1) / (indicatorCount - 1)
102 var accumulator = 0;
103 var currentSpread = 0;
104
105 for (var i = 0; i < indicatorCount - 2; ++i) {
106 accumulator += addend;
107 var spreads = Math.floor(accumulator);
108 accumulator -= spreads;
109
110 model.append({"firstPageIndex": currentSpread * pagesPerSpread +
111 album.firstContentPage, "pageCount": spreads * pagesPerSpread});
112
113 currentSpread += spreads;
114 }
115
116 model.append({"firstPageIndex": currentSpread * pagesPerSpread + album.firstContentPage,
117 "pageCount": (spreadCount - currentSpread - 1) * pagesPerSpread});
118
119 var lastSpread = (isPortrait
120 ? album.lastPopulatedContentPage
121 : album.lastContentPage - 1);
122 model.append({"firstPageIndex": lastSpread, "pageCount": pagesPerSpread});
123 }
124 }
125
126 Component.onCompleted: filterModel()
127
128 delegate: Item {
129 property int page: firstPageIndex
130 property bool isCurrent: (viewingPage >= firstPageIndex
131 && viewingPage < firstPageIndex + pageCount)
132
133 width: childrenRect.width
134 height: childrenRect.height
135
136 Image {
137 source: (isCurrent
138 ? "img/icon-pager-active.png"
139 : "img/icon-pager-inactive.png")
140 }
141
142 MouseArea {
143 anchors.fill: parent
144
145 enabled: !isCurrent
146
147 onClicked: selected(page);
148 }
149 }
150 }
151 }
152 }