/
documentview.cpp
1393 lines (1131 loc) · 37.5 KB
/
documentview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* copyright : (C) 2012-2017 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* based on qpdfview Copyright 2012 Adam Reichold GPL *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/*
Copyright 2012 Adam Reichold
This file is part of qpdfview.
modified by Pascal Brachet
*/
#include "documentview.h"
#include <QProgressDialog>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QProgressBar>
#include <QScrollBar>
#include <QApplication>
#include <QInputDialog>
#include <QLineEdit>
bool DocumentView::s_openUrl = false;
bool DocumentView::s_autoRefresh = false;
bool DocumentView::s_antialiasing = true;
bool DocumentView::s_textAntialiasing = true;
bool DocumentView::s_textHinting = false;
bool DocumentView::s_prefetch = false;
qreal DocumentView::s_pageSpacing = 5.0;
qreal DocumentView::s_minimumScaleFactor = 0.1;
qreal DocumentView::s_maximumScaleFactor = 10.0;
bool DocumentView::openUrl()
{
return s_openUrl;
}
void DocumentView::setOpenUrl(bool openUrl)
{
s_openUrl = openUrl;
}
bool DocumentView::autoRefresh()
{
return s_autoRefresh;
}
void DocumentView::setAutoRefresh(bool autoRefresh)
{
s_autoRefresh = autoRefresh;
}
bool DocumentView::antialiasing()
{
return s_antialiasing;
}
void DocumentView::setAntialiasing(bool antialiasing)
{
s_antialiasing = antialiasing;
}
bool DocumentView::textAntialiasing()
{
return s_textAntialiasing;
}
void DocumentView::setTextAntialiasing(bool textAntialiasing)
{
s_textAntialiasing = textAntialiasing;
}
bool DocumentView::textHinting()
{
return s_textHinting;
}
void DocumentView::setTextHinting(bool textHinting)
{
s_textHinting = textHinting;
}
bool DocumentView::prefetch()
{
return s_prefetch;
}
void DocumentView::setPrefetch(bool prefetch)
{
s_prefetch = prefetch;
}
qreal DocumentView::pageSpacing()
{
return s_pageSpacing;
}
void DocumentView::setPageSpacing(qreal pageSpacing)
{
if(pageSpacing >= 0.0)
{
s_pageSpacing = pageSpacing;
}
}
qreal DocumentView::minimumScaleFactor()
{
return s_minimumScaleFactor;
}
qreal DocumentView::maximumScaleFactor()
{
return s_maximumScaleFactor;
}
DocumentView::DocumentView(QWidget* parent) : QGraphicsView(parent),
m_autoRefreshWatcher(0),
m_autoRefreshTimer(0),
m_prefetchTimer(0),
m_mutex(),
m_document(0),
m_filePath(),
m_numberOfPages(-1),
m_currentPage(-1),
m_returnToPage(-1),
m_returnToLeft(0.0),
m_returnToTop(0.0),
m_continuousMode(false),
m_twoPagesMode(false),
m_scaleMode(ScaleFactor),
m_scaleFactor(0.1),
m_rotation(QPdf::Rotate0),
m_pagesScene(0),
m_pages(),
m_heightToIndex(),
m_highlight(0),
m_outlineModel(0),
m_propertiesModel(0)
{
m_pagesScene = new QGraphicsScene(this);
m_outlineModel = new QPdfBookmarkModel(this);
m_propertiesModel = new QStandardItemModel(this);
m_filePath="";
m_currentPage=-1;
m_returnToPage=-1;
setScene(m_pagesScene);
setDragMode(QGraphicsView::ScrollHandDrag);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setCursor(Qt::ArrowCursor);
// setLineWidth(1);
setFrameShape(QFrame::Box);
setFrameShadow(QFrame::Plain);
//QApplication::setOverrideCursor(Qt::ArrowCursor);
setAcceptDrops(false);
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this,SLOT(on_verticalScrollBar_valueChanged(int)));
connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this,SLOT(on_horizontalScrollBar_valueChanged(int)));
// highlight
m_highlight = new QGraphicsRectItem();
m_highlight->setVisible(false);
scene()->addItem(m_highlight);
QColor highlightColor = QApplication::palette().color(QPalette::Highlight);
highlightColor.setAlpha(127);
m_highlight->setBrush(QBrush(highlightColor));
highlightColor.setAlpha(255);
m_highlight->setPen(QPen(highlightColor));
// auto-refresh
m_autoRefreshWatcher = new QFileSystemWatcher(this);
m_autoRefreshTimer = new QTimer(this);
m_autoRefreshTimer->setInterval(500);
m_autoRefreshTimer->setSingleShot(true);
connect(m_autoRefreshWatcher, SIGNAL(fileChanged(QString)), m_autoRefreshTimer, SLOT(start()));
connect(m_autoRefreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
// prefetch
m_prefetchTimer = new QTimer(this);
m_prefetchTimer->setInterval(250);
m_prefetchTimer->setSingleShot(true);
connect(this, SIGNAL(currentPageChanged(int)), m_prefetchTimer, SLOT(start()));
connect(this, SIGNAL(scaleModeChanged(DocumentView::ScaleMode)), m_prefetchTimer, SLOT(start()));
connect(this, SIGNAL(scaleFactorChanged(qreal)), m_prefetchTimer, SLOT(start()));
connect(this, SIGNAL(rotationChanged(QPdf::Rotation)), m_prefetchTimer, SLOT(start()));
connect(m_prefetchTimer, SIGNAL(timeout()), this,SLOT(on_prefetch_timeout()));
}
DocumentView::~DocumentView()
{
qDeleteAll(m_pages);
}
void DocumentView::clearAll()
{
disconnect(verticalScrollBar(), SIGNAL(valueChanged(int)), this,SLOT(on_verticalScrollBar_valueChanged(int)));
disconnect(horizontalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(on_horizontalScrollBar_valueChanged(int)));
disconnect(m_autoRefreshWatcher, SIGNAL(fileChanged(QString)), m_autoRefreshTimer, SLOT(start()));
disconnect(m_autoRefreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
disconnect(this, SIGNAL(currentPageChanged(int)), m_prefetchTimer, SLOT(start()));
disconnect(this, SIGNAL(scaleModeChanged(DocumentView::ScaleMode)), m_prefetchTimer, SLOT(start()));
disconnect(this, SIGNAL(scaleFactorChanged(qreal)), m_prefetchTimer, SLOT(start()));
disconnect(this, SIGNAL(rotationChanged(QPdf::Rotation)), m_prefetchTimer, SLOT(start()));
disconnect(m_prefetchTimer, SIGNAL(timeout()), this,SLOT(on_prefetch_timeout()));
m_pages.clear();
m_numberOfPages=-1;
scene()->clear();
m_pagesScene = new QGraphicsScene(this);
m_filePath="";
m_currentPage=-1;
m_returnToPage=-1;
setScene(m_pagesScene);
m_highlight = new QGraphicsRectItem();
m_highlight->setVisible(false);
scene()->addItem(m_highlight);
prepareScene();
prepareView(0,0);
QColor highlightColor = QApplication::palette().color(QPalette::Highlight);
highlightColor.setAlpha(127);
m_highlight->setBrush(QBrush(highlightColor));
highlightColor.setAlpha(255);
m_highlight->setPen(QPen(highlightColor));
update();
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), SLOT(on_verticalScrollBar_valueChanged(int)));
connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), SLOT(on_horizontalScrollBar_valueChanged(int)));
connect(m_autoRefreshWatcher, SIGNAL(fileChanged(QString)), m_autoRefreshTimer, SLOT(start()));
connect(m_autoRefreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
connect(this, SIGNAL(currentPageChanged(int)), m_prefetchTimer, SLOT(start()));
connect(this, SIGNAL(scaleModeChanged(DocumentView::ScaleMode)), m_prefetchTimer, SLOT(start()));
connect(this, SIGNAL(scaleFactorChanged(qreal)), m_prefetchTimer, SLOT(start()));
connect(this, SIGNAL(rotationChanged(QPdf::Rotation)), m_prefetchTimer, SLOT(start()));
connect(m_prefetchTimer, SIGNAL(timeout()), SLOT(on_prefetch_timeout()));
}
const QString& DocumentView::filePath() const
{
return m_filePath;
}
int DocumentView::numberOfPages() const
{
return m_numberOfPages;
}
int DocumentView::currentPage() const
{
return m_currentPage;
}
bool DocumentView::continousMode() const
{
return m_continuousMode;
}
void DocumentView::setContinousMode(bool continousMode)
{
if (m_pages.count()<1) return;
if(m_continuousMode != continousMode)
{
m_continuousMode = continousMode;
qreal left = 0.0, top = 0.0;
saveLeftAndTop(left, top);
prepareView(left, top);
emit continousModeChanged(m_continuousMode);
}
}
bool DocumentView::twoPagesMode() const
{
return m_twoPagesMode;
}
void DocumentView::setTwoPagesMode(bool twoPagesMode)
{
if (m_pages.count()<1) return;
if(m_twoPagesMode != twoPagesMode)
{
m_twoPagesMode = twoPagesMode;
if(m_twoPagesMode)
{
if(m_currentPage != (m_currentPage % 2 != 0 ? m_currentPage : m_currentPage - 1))
{
m_currentPage = m_currentPage % 2 != 0 ? m_currentPage : m_currentPage - 1;
emit currentPageChanged(m_currentPage);
}
}
prepareScene();
prepareView();
emit twoPagesModeChanged(m_twoPagesMode);
}
}
DocumentView::ScaleMode DocumentView::scaleMode() const
{
return m_scaleMode;
}
void DocumentView::setScaleMode(ScaleMode scaleMode)
{
if (m_pages.count()<1) return;
if(m_scaleMode != scaleMode)
{
m_scaleMode = scaleMode;
qreal left = 0.0, top = 0.0;
saveLeftAndTop(left, top);
prepareScene();
prepareView(left, top);
emit scaleModeChanged(m_scaleMode);
}
}
qreal DocumentView::scaleFactor() const
{
return m_scaleFactor;
}
qreal DocumentView::realScale() const
{
if (m_pages.count()<1) return 1;
return m_pages.at(0)->scaleFactor();
}
void DocumentView::setScaleFactor(qreal scaleFactor)
{
if (m_pages.count()<1) return;
if(m_scaleFactor != scaleFactor && scaleFactor >= s_minimumScaleFactor && scaleFactor <= s_maximumScaleFactor)
{
m_scaleFactor = scaleFactor;
if(m_scaleMode == ScaleFactor)
{
qreal left = 0.0, top = 0.0;
saveLeftAndTop(left, top);
prepareScene();
prepareView(left, top);
// #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
// viewport()->update();
// #endif
}
emit scaleFactorChanged(m_scaleFactor);
}
}
QPdfDocument* DocumentView::doc() const
{
return m_document;
}
QPdf::Rotation DocumentView::rotation() const
{
return m_rotation;
}
void DocumentView::setRotation(QPdf::Rotation rotation)
{
if (m_pages.count()<1) return;
if(m_rotation != rotation)
{
m_rotation = rotation;
prepareScene();
prepareView();
emit rotationChanged(m_rotation);
}
}
void DocumentView::clearPaths(int index)
{
if ((m_pages.count()<1) || (index>=m_pages.count())) return;
m_pages.at(index)->clearPaths();
}
void DocumentView::setHighlightPath(int index, const QPainterPath& path)
{
if ((m_pages.count()<1) || (index>=m_pages.count())) return;
m_pages.at(index)->setHighlightPath(path);
}
void DocumentView::setSearchPath(int index, const QPainterPath& path)
{
if ((m_pages.count()<1) || (index>=m_pages.count())) return;
m_pages.at(index)->setSearchPath(path);
}
QPdfBookmarkModel* DocumentView::outlineModel() const
{
return m_outlineModel;
}
QStandardItemModel* DocumentView::propertiesModel() const
{
return m_propertiesModel;
}
void DocumentView::show()
{
QGraphicsView::show();
prepareView();
}
bool DocumentView::open(const QString& filePath)
{
QPdfDocument* document=new QPdfDocument(this);
QPdfDocument::DocumentError result=document->load(filePath);
if (result== QPdfDocument::NoError)
{
m_filePath = filePath;
m_numberOfPages = document->pageCount();
m_currentPage = 1;
m_returnToPage = -1;
prepareDocument(document);
prepareScene();
prepareView();
emit filePathChanged(m_filePath);
emit numberOfPagesChanged(m_numberOfPages);
emit currentPageChanged(m_currentPage);
}
return result== QPdfDocument::NoError ;
}
bool DocumentView::refresh()
{
QPdfDocument* document=new QPdfDocument(this);
QPdfDocument::DocumentError result=document->load(m_filePath);
if (result== QPdfDocument::NoError)
{
qreal left = 0.0, top = 0.0;
saveLeftAndTop(left, top);
m_numberOfPages = document->pageCount();
m_currentPage = m_currentPage <= m_numberOfPages ? m_currentPage : 1;
m_returnToPage = m_returnToPage <= m_numberOfPages ? m_returnToPage : -1;
prepareDocument(document);
prepareScene();
prepareView(left, top);
emit numberOfPagesChanged(m_numberOfPages);
emit currentPageChanged(m_currentPage);
}
return result== QPdfDocument::NoError ;
}
bool DocumentView::print(QPrinter* printer)
{
int fromPage = printer->fromPage() != 0 ? printer->fromPage() : 1;
int toPage = printer->toPage() != 0 ? printer->toPage() : m_numberOfPages;
QProgressDialog* progressDialog = new QProgressDialog(this);
progressDialog->setLabelText(tr("Printing '%1'...").arg(m_filePath));
progressDialog->setRange(fromPage - 1, toPage);
QPainter painter;
painter.begin(printer);
for(int index = fromPage - 1; index <= toPage - 1; index++)
{
progressDialog->setValue(index);
QApplication::processEvents();
{
m_mutex.lock();
const QSizeF size = m_document->pageSize(index);
qreal pageWidth = printer->physicalDpiX() / 72.0 * size.width();
qreal pageHeight = printer->physicalDpiY() / 72.0 * size.height();
////????????????????????????
QImage image = m_document->render(index,QSize(size.width()*printer-> physicalDpiX()/72,size.height()*printer-> physicalDpiY()/72));
//QImage image = page->renderToImage(printer->physicalDpiX(), printer->physicalDpiY());
m_mutex.unlock();
qreal scaleFactor = qMin(printer->width() / pageWidth, printer->height() / pageHeight);
painter.setTransform(QTransform::fromScale(scaleFactor, scaleFactor));
painter.drawImage(QPointF(), image);
}
if(index < toPage - 1)
{
printer->newPage();
}
QApplication::processEvents();
if(progressDialog->wasCanceled())
{
delete progressDialog;
return false;
}
}
painter.end();
delete progressDialog;
return true;
}
void DocumentView::previousPage()
{
if (m_pages.count()<1) return;
jumpToPage(currentPage() - (m_twoPagesMode ? 2 : 1), 0.0, 0.0, false);
}
void DocumentView::nextPage()
{
if (m_pages.count()<1) return;
jumpToPage(currentPage() + (m_twoPagesMode ? 2 : 1), 0.0, 0.0, false);
}
void DocumentView::firstPage()
{
if (m_pages.count()<1) return;
jumpToPage(1);
}
void DocumentView::lastPage()
{
if (m_pages.count()<1) return;
jumpToPage(numberOfPages());
}
void DocumentView::jumpToPage(int page, qreal changeLeft, qreal changeTop, bool returnTo)
{
if (m_pages.count()<1) return;
if(page >= 1 && page <= m_numberOfPages)
{
qreal left = 0.0, top = 0.0;
saveLeftAndTop(left, top);
if(m_currentPage != currentPageForPage(page) || !qFuzzyCompare(1.0 + left, 1.0 + changeLeft) || !qFuzzyCompare(1.0 + top, 1.0 + changeTop))
{
if(returnTo)
{
m_returnToPage = m_currentPage;
m_returnToLeft = left; m_returnToTop = top;
}
m_currentPage = currentPageForPage(page);
prepareView(changeLeft, changeTop);
emit currentPageChanged(m_currentPage);
}
}
}
void DocumentView::zoomIn()
{
if (m_pages.count()<1) return;
if(scaleMode() != ScaleFactor)
{
PageItem* page = m_pages.at(m_currentPage - 1);
setScaleFactor(qMin(page->scaleFactor() + 0.1f, s_maximumScaleFactor));
setScaleMode(ScaleFactor);
}
else
{
setScaleFactor(qMin(scaleFactor() + 0.1f, s_maximumScaleFactor));
}
}
void DocumentView::zoomOut()
{
if (m_pages.count()<1) return;
if(scaleMode() != ScaleFactor)
{
PageItem* page = m_pages.at(m_currentPage - 1);
setScaleFactor(qMax(page->scaleFactor() - 0.1f, s_minimumScaleFactor));
setScaleMode(ScaleFactor);
}
else
{
setScaleFactor(qMax(scaleFactor() - 0.1f, s_minimumScaleFactor));
}
}
void DocumentView::originalSize()
{
setScaleFactor(1.0);
setScaleMode(ScaleFactor);
}
void DocumentView::rotateLeft()
{
switch(rotation())
{
case QPdf::Rotate0:
setRotation(QPdf::Rotate270);
break;
case QPdf::Rotate90:
setRotation(QPdf::Rotate0);
break;
case QPdf::Rotate180:
setRotation(QPdf::Rotate90);
break;
case QPdf::Rotate270:
setRotation(QPdf::Rotate180);
break;
}
}
void DocumentView::rotateRight()
{
switch(rotation())
{
case QPdf::Rotate0:
setRotation(QPdf::Rotate90);
break;
case QPdf::Rotate90:
setRotation(QPdf::Rotate180);
break;
case QPdf::Rotate180:
setRotation(QPdf::Rotate270);
break;
case QPdf::Rotate270:
setRotation(QPdf::Rotate0);
break;
}
}
void DocumentView::presentation()
{
if (m_pages.count()<1) return;
PresentationView* presentationView = new PresentationView(&m_mutex, m_document);
presentationView->jumpToPage(currentPage());
presentationView->show();
presentationView->setAttribute(Qt::WA_DeleteOnClose);
}
void DocumentView::on_horizontalScrollBar_valueChanged(int value)
{
if (m_pages.count()<1) return;
if ( horizontalScrollBar()->maximum()!=0) emit doHScroll(value);
}
void DocumentView::on_verticalScrollBar_valueChanged(int value)
{
if (m_pages.count()<1) return;
if(m_continuousMode)
{
QRectF visibleRect = mapToScene(viewport()->rect()).boundingRect();
foreach(PageItem* page, m_pages)
{
if(!page->boundingRect().translated(page->pos()).intersects(visibleRect))
{
page->cancelRender();
}
}
QMap< qreal, int >::const_iterator lowerBound = m_heightToIndex.lowerBound(-value);
if(lowerBound != m_heightToIndex.end())
{
int page = lowerBound.value() + 1;
if(m_currentPage != page)
{
m_currentPage = page;
emit currentPageChanged(m_currentPage);
}
}
}
}
void DocumentView::on_prefetch_timeout()
{
if (m_pages.count()<1) return;
int fromPage = m_currentPage - (m_twoPagesMode ? 2 : 1);
int toPage = m_currentPage + (m_twoPagesMode ? 3 : 1);
fromPage = fromPage >= 1 ? fromPage : 1;
toPage = toPage <= m_numberOfPages ? toPage : m_numberOfPages;
for(int index = fromPage - 1; index <= toPage - 1; index++)
{
PageItem* page = m_pages.at(index);
page->startRender(true);
}
}
void DocumentView::on_pages_linkClicked(int page, qreal left, qreal top)
{
if (m_pages.count()<1) return;
page = page >= 1 ? page : 1;
page = page <= m_numberOfPages ? page : m_numberOfPages;
left = left >= 0.0 ? left : 0.0;
left = left <= 1.0 ? left : 1.0;
top = top >= 0.0 ? top : 0.0;
top = top <= 1.0 ? top : 1.0;
emit gotoDest(page, left, top);
}
void DocumentView::on_pages_linkClicked(const QString& url)
{
QUrl theurl = QUrl::fromEncoded(url.toLatin1());
QDesktopServices::openUrl(theurl);
}
void DocumentView::resizeEvent(QResizeEvent* event)
{
QGraphicsView::resizeEvent(event);
if(m_scaleMode != ScaleFactor)
{
qreal left = 0.0, top = 0.0;
saveLeftAndTop(left, top);
prepareScene();
prepareView(left, top);
}
}
void DocumentView::keyPressEvent(QKeyEvent* event)
{
if (m_pages.count()<1) return;
if(event->modifiers() == Qt::NoModifier)
{
if(event->key() == Qt::Key_Return)
{
jumpToPage(m_returnToPage, m_returnToLeft, m_returnToTop);
event->accept();
return;
}
if(!m_continuousMode)
{
if(event->key() == Qt::Key_PageUp && verticalScrollBar()->value() == verticalScrollBar()->minimum() && m_currentPage > 1)
{
previousPage();
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
event->accept();
return;
}
else if(event->key() == Qt::Key_PageDown && verticalScrollBar()->value() == verticalScrollBar()->maximum() && m_currentPage != currentPageForPage(m_numberOfPages))
{
nextPage();
verticalScrollBar()->setValue(verticalScrollBar()->minimum());
event->accept();
return;
}
}
}
int qtKeyCode = event->key();
if(event->modifiers() & Qt::ShiftModifier) {
qtKeyCode += Qt::SHIFT;
}
if(event->modifiers() & Qt::ControlModifier) {
qtKeyCode += Qt::CTRL;
}
if(event->modifiers() & Qt::AltModifier) {
qtKeyCode += Qt::ALT;
}
if(event->modifiers() & Qt::MetaModifier) {
qtKeyCode += Qt::META;
}
QKeySequence s1 = QKeySequence(qtKeyCode);
if (hasFocus() && (s1.matches(QKeySequence(Qt::CTRL + Qt::Key_Left))==QKeySequence::ExactMatch)) rotateLeft();
else if (hasFocus() && (s1.matches(QKeySequence(Qt::CTRL + Qt::Key_Right))==QKeySequence::ExactMatch)) rotateRight();
else QGraphicsView::keyPressEvent(event);
}
void DocumentView::mouseReleaseEvent(QMouseEvent* event)
{
QApplication::setOverrideCursor(Qt::ArrowCursor);
QGraphicsView::mouseReleaseEvent(event);
}
void DocumentView::mousePressEvent(QMouseEvent* event)
{
QGraphicsView::mousePressEvent(event);
//QApplication::setOverrideCursor(Qt::ArrowCursor);
// if(!event->isAccepted() && event->modifiers() == Qt::NoModifier && event->button() == Qt::RightButton)
// {
// QMenu* menu = new QMenu();
//
// QAction* returnToPageAction = menu->addAction(tr("&Return to page %1").arg(m_returnToPage));
// returnToPageAction->setShortcut(QKeySequence(Qt::Key_Return));
// returnToPageAction->setIcon(QIcon::fromTheme("go-jump", QIcon(":icons/go-jump.svg")));
// returnToPageAction->setIconVisibleInMenu(true);
//
// returnToPageAction->setVisible(m_returnToPage != -1);
//
// QAction* previousPageAction = menu->addAction(tr("&Previous page"));
// previousPageAction->setShortcut(QKeySequence(Qt::Key_Backspace));
// previousPageAction->setIcon(QIcon::fromTheme("go-previous", QIcon(":icons/go-previous.svg")));
// previousPageAction->setIconVisibleInMenu(true);
//
// QAction* nextPageAction = menu->addAction(tr("&Next page"));
// nextPageAction->setShortcut(QKeySequence(Qt::Key_Space));
// nextPageAction->setIcon(QIcon::fromTheme("go-next", QIcon(":icons/go-next.svg")));
// nextPageAction->setIconVisibleInMenu(true);
//
// QAction* refreshAction = menu->addAction(tr("&Refresh"));
// refreshAction->setShortcut(QKeySequence::Refresh);
// refreshAction->setIcon(QIcon::fromTheme("view-refresh", QIcon(":icons/view-refresh.svg")));
// refreshAction->setIconVisibleInMenu(true);
//
// QAction* action = menu->exec(event->globalPos());
//
// if(action == returnToPageAction)
// {
// jumpToPage(m_returnToPage, m_returnToLeft, m_returnToTop);
// }
// else if(action == previousPageAction)
// {
// previousPage();
// }
// else if(action == nextPageAction)
// {
// nextPage();
// }
// else if(action == refreshAction)
// {
// refresh();
// }
//
// delete menu;
// }
}
void DocumentView::wheelEvent(QWheelEvent* event)
{
if (m_pages.count()<1) return;
if(event->modifiers() == Qt::ControlModifier)
{
if(event->delta() > 0)
{
emit requestZoomIn();//zoomIn();
}
else
{
emit requestZoomOut();//zoomOut();
}
event->accept();
return;
}
else if(event->modifiers() == Qt::ShiftModifier)
{
if(event->delta() > 0)
{
rotateLeft();
}
else
{
rotateRight();
}
event->accept();
return;
}
else if(event->modifiers() == Qt::NoModifier)
{
if(!m_continuousMode)
{
if(event->delta() > 0 && verticalScrollBar()->value() == verticalScrollBar()->minimum() && m_currentPage > 1)
{
previousPage();
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
event->accept();
return;
}
else if(event->delta() < 0 && verticalScrollBar()->value() == verticalScrollBar()->maximum() && m_currentPage != currentPageForPage(m_numberOfPages))
{
nextPage();
verticalScrollBar()->setValue(verticalScrollBar()->minimum());
event->accept();
return;
}
}
}
QGraphicsView::wheelEvent(event);
}
int DocumentView::currentPageForPage(int page)
{
if(m_twoPagesMode)
{
return page % 2 != 0 ? page : page - 1;
}
else
{
return page;
}
}
void DocumentView::saveLeftAndTop(qreal& left, qreal& top)
{
if ((m_pages.count()>0) && (m_currentPage>=1))
{
PageItem* page = m_pages.at(m_currentPage - 1);
QRectF boundingRect = page->boundingRect().translated(page->pos());
QPointF topLeft = mapToScene(viewport()->rect().topLeft());
left = (topLeft.x() - boundingRect.x()) / boundingRect.width();
top = (topLeft.y() - boundingRect.y()) / boundingRect.height();
}
}
void DocumentView::prepareDocument(QPdfDocument* document)
{
m_prefetchTimer->blockSignals(true);
m_prefetchTimer->stop();
qDeleteAll(m_pages);
if(m_document != 0)
{