1 | /* |
---|
2 | |
---|
3 | Copyright 2014 S. Razi Alavizadeh |
---|
4 | Copyright 2013-2014 Adam Reichold |
---|
5 | |
---|
6 | This file is part of qpdfview. |
---|
7 | |
---|
8 | qpdfview is free software: you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation, either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | qpdfview is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | GNU General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | |
---|
21 | */ |
---|
22 | |
---|
23 | #include "documentlayout.h" |
---|
24 | |
---|
25 | #include "settings.h" |
---|
26 | #include "pageitem.h" |
---|
27 | |
---|
28 | namespace |
---|
29 | { |
---|
30 | |
---|
31 | using namespace qpdfview; |
---|
32 | |
---|
33 | const qreal viewportPadding = 6.0f; |
---|
34 | |
---|
35 | } // anonymous |
---|
36 | |
---|
37 | namespace qpdfview |
---|
38 | { |
---|
39 | |
---|
40 | Settings* DocumentLayout::s_settings = 0; |
---|
41 | |
---|
42 | DocumentLayout::DocumentLayout() |
---|
43 | { |
---|
44 | if(s_settings == 0) |
---|
45 | { |
---|
46 | s_settings = Settings::instance(); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | DocumentLayout* DocumentLayout::fromLayoutMode(LayoutMode layoutMode) |
---|
51 | { |
---|
52 | switch(layoutMode) |
---|
53 | { |
---|
54 | default: |
---|
55 | case SinglePageMode: return new SinglePageLayout; |
---|
56 | case TwoPagesMode: return new TwoPagesLayout; |
---|
57 | case TwoPagesWithCoverPageMode: return new TwoPagesWithCoverPageLayout; |
---|
58 | case MultiplePagesMode: return new MultiplePagesLayout; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | bool DocumentLayout::isCurrentPage(const QRectF& visibleRect, const QRectF& pageRect) const |
---|
63 | { |
---|
64 | // Works with vertically scrolling layouts, i.e. all currently implemented layouts. |
---|
65 | const qreal pageVisibleHeight = pageRect.intersected(visibleRect).height(); |
---|
66 | const qreal pageTopOffset = pageRect.top() - visibleRect.top(); |
---|
67 | |
---|
68 | if(visibleRect.height() > 2.0f * pageRect.height()) // Are more than two pages visible? |
---|
69 | { |
---|
70 | const qreal halfPageHeight = 0.5f * pageRect.height(); |
---|
71 | |
---|
72 | return pageVisibleHeight >= halfPageHeight && pageTopOffset < halfPageHeight && pageTopOffset >= -halfPageHeight; |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | return pageVisibleHeight >= 0.5f * visibleRect.height(); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | qreal DocumentLayout::visibleHeight(int viewportHeight) const |
---|
81 | { |
---|
82 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
83 | |
---|
84 | return viewportHeight - 2.0f * pageSpacing; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | int SinglePageLayout::currentPage(int page) const |
---|
89 | { |
---|
90 | return page; |
---|
91 | } |
---|
92 | |
---|
93 | int SinglePageLayout::previousPage(int page) const |
---|
94 | { |
---|
95 | return qMax(page - 1, 1); |
---|
96 | } |
---|
97 | |
---|
98 | int SinglePageLayout::nextPage(int page, int count) const |
---|
99 | { |
---|
100 | return qMin(page + 1, count); |
---|
101 | } |
---|
102 | |
---|
103 | QPair< int, int > SinglePageLayout::prefetchRange(int page, int count) const |
---|
104 | { |
---|
105 | const int prefetchDistance = s_settings->documentView().prefetchDistance(); |
---|
106 | |
---|
107 | return qMakePair(qMax(page - prefetchDistance / 2, 1), |
---|
108 | qMin(page + prefetchDistance, count)); |
---|
109 | } |
---|
110 | |
---|
111 | int SinglePageLayout::leftIndex(int index) const |
---|
112 | { |
---|
113 | return index; |
---|
114 | } |
---|
115 | |
---|
116 | int SinglePageLayout::rightIndex(int index, int count) const |
---|
117 | { |
---|
118 | Q_UNUSED(count); |
---|
119 | |
---|
120 | return index; |
---|
121 | } |
---|
122 | |
---|
123 | qreal SinglePageLayout::visibleWidth(int viewportWidth) const |
---|
124 | { |
---|
125 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
126 | |
---|
127 | return viewportWidth - viewportPadding - 2.0f * pageSpacing; |
---|
128 | } |
---|
129 | |
---|
130 | void SinglePageLayout::prepareLayout(const QVector< PageItem* >& pageItems, bool /* rightToLeft */, |
---|
131 | qreal& left, qreal& right, qreal& height) |
---|
132 | { |
---|
133 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
134 | qreal pageHeight = 0.0f; |
---|
135 | |
---|
136 | for(int index = 0; index < pageItems.count(); ++index) |
---|
137 | { |
---|
138 | PageItem* page = pageItems.at(index); |
---|
139 | const QRectF boundingRect = page->boundingRect(); |
---|
140 | |
---|
141 | page->setPos(-boundingRect.left() - 0.5f * boundingRect.width(), height - boundingRect.top()); |
---|
142 | |
---|
143 | pageHeight = boundingRect.height(); |
---|
144 | |
---|
145 | left = qMin(left, -0.5f * boundingRect.width() - pageSpacing); |
---|
146 | right = qMax(right, 0.5f * boundingRect.width() + pageSpacing); |
---|
147 | height += pageHeight + pageSpacing; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | int TwoPagesLayout::currentPage(int page) const |
---|
153 | { |
---|
154 | return page % 2 != 0 ? page : page - 1; |
---|
155 | } |
---|
156 | |
---|
157 | int TwoPagesLayout::previousPage(int page) const |
---|
158 | { |
---|
159 | return qMax(page - 2, 1); |
---|
160 | } |
---|
161 | |
---|
162 | int TwoPagesLayout::nextPage(int page, int count) const |
---|
163 | { |
---|
164 | return qMin(page + 2, count); |
---|
165 | } |
---|
166 | |
---|
167 | QPair< int, int > TwoPagesLayout::prefetchRange(int page, int count) const |
---|
168 | { |
---|
169 | const int prefetchDistance = s_settings->documentView().prefetchDistance(); |
---|
170 | |
---|
171 | return qMakePair(qMax(page - prefetchDistance, 1), |
---|
172 | qMin(page + 2 * prefetchDistance + 1, count)); |
---|
173 | } |
---|
174 | |
---|
175 | int TwoPagesLayout::leftIndex(int index) const |
---|
176 | { |
---|
177 | return index % 2 == 0 ? index : index - 1; |
---|
178 | } |
---|
179 | |
---|
180 | int TwoPagesLayout::rightIndex(int index, int count) const |
---|
181 | { |
---|
182 | return qMin(index % 2 == 0 ? index + 1 : index, count - 1); |
---|
183 | } |
---|
184 | |
---|
185 | qreal TwoPagesLayout::visibleWidth(int viewportWidth) const |
---|
186 | { |
---|
187 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
188 | |
---|
189 | return (viewportWidth - viewportPadding - 3.0f * pageSpacing) / 2.0f; |
---|
190 | } |
---|
191 | |
---|
192 | void TwoPagesLayout::prepareLayout(const QVector< PageItem* >& pageItems, bool rightToLeft, |
---|
193 | qreal& left, qreal& right, qreal& height) |
---|
194 | { |
---|
195 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
196 | qreal pageHeight = 0.0f; |
---|
197 | |
---|
198 | for(int index = 0; index < pageItems.count(); ++index) |
---|
199 | { |
---|
200 | PageItem* page = pageItems.at(index); |
---|
201 | const QRectF boundingRect = page->boundingRect(); |
---|
202 | |
---|
203 | const qreal leftPos = -boundingRect.left() - boundingRect.width() - 0.5f * pageSpacing; |
---|
204 | const qreal rightPos = -boundingRect.left() + 0.5f * pageSpacing; |
---|
205 | |
---|
206 | if(index == leftIndex(index)) |
---|
207 | { |
---|
208 | page->setPos(rightToLeft ? rightPos : leftPos, height - boundingRect.top()); |
---|
209 | |
---|
210 | pageHeight = boundingRect.height(); |
---|
211 | |
---|
212 | if(rightToLeft) |
---|
213 | { |
---|
214 | right = qMax(right, boundingRect.width() + 1.5f * pageSpacing); |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | left = qMin(left, -boundingRect.width() - 1.5f * pageSpacing); |
---|
219 | } |
---|
220 | |
---|
221 | if(index == rightIndex(index, pageItems.count())) |
---|
222 | { |
---|
223 | right = qMax(right, 0.5f * pageSpacing); |
---|
224 | height += pageHeight + pageSpacing; |
---|
225 | } |
---|
226 | } |
---|
227 | else |
---|
228 | { |
---|
229 | page->setPos(rightToLeft ? leftPos : rightPos, height - boundingRect.top()); |
---|
230 | |
---|
231 | pageHeight = qMax(pageHeight, boundingRect.height()); |
---|
232 | |
---|
233 | if(rightToLeft) |
---|
234 | { |
---|
235 | left = qMin(left, -boundingRect.width() - 1.5f * pageSpacing); |
---|
236 | } |
---|
237 | else |
---|
238 | { |
---|
239 | right = qMax(right, boundingRect.width() + 1.5f * pageSpacing); |
---|
240 | } |
---|
241 | |
---|
242 | height += pageHeight + pageSpacing; |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | int TwoPagesWithCoverPageLayout::currentPage(int page) const |
---|
249 | { |
---|
250 | return page == 1 ? page : (page % 2 == 0 ? page : page - 1); |
---|
251 | } |
---|
252 | |
---|
253 | int TwoPagesWithCoverPageLayout::leftIndex(int index) const |
---|
254 | { |
---|
255 | return index == 0 ? index : (index % 2 != 0 ? index : index - 1); |
---|
256 | } |
---|
257 | |
---|
258 | int TwoPagesWithCoverPageLayout::rightIndex(int index, int count) const |
---|
259 | { |
---|
260 | return qMin(index % 2 != 0 ? index + 1 : index, count - 1); |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | int MultiplePagesLayout::currentPage(int page) const |
---|
265 | { |
---|
266 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
267 | |
---|
268 | return page - ((page - 1) % pagesPerRow); |
---|
269 | } |
---|
270 | |
---|
271 | int MultiplePagesLayout::previousPage(int page) const |
---|
272 | { |
---|
273 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
274 | |
---|
275 | return qMax(page - pagesPerRow, 1); |
---|
276 | } |
---|
277 | |
---|
278 | int MultiplePagesLayout::nextPage(int page, int count) const |
---|
279 | { |
---|
280 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
281 | |
---|
282 | return qMin(page + pagesPerRow, count); |
---|
283 | } |
---|
284 | |
---|
285 | QPair<int, int> MultiplePagesLayout::prefetchRange(int page, int count) const |
---|
286 | { |
---|
287 | const int prefetchDistance = s_settings->documentView().prefetchDistance(); |
---|
288 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
289 | |
---|
290 | return qMakePair(qMax(page - pagesPerRow * (prefetchDistance / 2), 1), |
---|
291 | qMin(page + pagesPerRow * (prefetchDistance + 1) - 1, count)); |
---|
292 | } |
---|
293 | |
---|
294 | int MultiplePagesLayout::leftIndex(int index) const |
---|
295 | { |
---|
296 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
297 | |
---|
298 | return index - (index % pagesPerRow); |
---|
299 | } |
---|
300 | |
---|
301 | int MultiplePagesLayout::rightIndex(int index, int count) const |
---|
302 | { |
---|
303 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
304 | |
---|
305 | return qMin(index - (index % pagesPerRow) + pagesPerRow - 1, count - 1); |
---|
306 | } |
---|
307 | |
---|
308 | qreal MultiplePagesLayout::visibleWidth(int viewportWidth) const |
---|
309 | { |
---|
310 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
311 | const int pagesPerRow = s_settings->documentView().pagesPerRow(); |
---|
312 | |
---|
313 | return (viewportWidth - viewportPadding - (pagesPerRow + 1) * pageSpacing) / pagesPerRow; |
---|
314 | } |
---|
315 | |
---|
316 | void MultiplePagesLayout::prepareLayout(const QVector< PageItem* >& pageItems, bool rightToLeft, |
---|
317 | qreal& left, qreal& right, qreal& height) |
---|
318 | { |
---|
319 | const qreal pageSpacing = s_settings->documentView().pageSpacing(); |
---|
320 | qreal pageHeight = 0.0; |
---|
321 | |
---|
322 | for(int index = 0; index < pageItems.count(); ++index) |
---|
323 | { |
---|
324 | PageItem* page = pageItems.at(index); |
---|
325 | const QRectF boundingRect = page->boundingRect(); |
---|
326 | |
---|
327 | const qreal leftPos = left - boundingRect.left() + pageSpacing; |
---|
328 | const qreal rightPos = right - boundingRect.left() - boundingRect.width() - pageSpacing; |
---|
329 | |
---|
330 | page->setPos(rightToLeft ? rightPos : leftPos, height - boundingRect.top()); |
---|
331 | |
---|
332 | pageHeight = qMax(pageHeight, boundingRect.height()); |
---|
333 | |
---|
334 | if(rightToLeft) |
---|
335 | { |
---|
336 | right -= boundingRect.width() + pageSpacing; |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | left += boundingRect.width() + pageSpacing; |
---|
341 | } |
---|
342 | |
---|
343 | if(index == rightIndex(index, pageItems.count())) |
---|
344 | { |
---|
345 | height += pageHeight + pageSpacing; |
---|
346 | pageHeight = 0.0f; |
---|
347 | |
---|
348 | if(rightToLeft) |
---|
349 | { |
---|
350 | left = qMin(left, right - pageSpacing); |
---|
351 | right = 0.0f; |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | right = qMax(right, left + pageSpacing); |
---|
356 | left = 0.0f; |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | } // qpdfview |
---|