forked from greenantdotcom/diffpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
1957 lines (1791 loc) · 75 KB
/
mainwindow.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 © 2008-13 Qtrac Ltd. All rights reserved.
This program or module 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. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
*/
#include "aboutform.hpp"
#include "generic.hpp"
#include "helpform.hpp"
#include "label.hpp"
#include "lineedit.hpp"
#include "optionsform.hpp"
#include "mainwindow.hpp"
#include "sequence_matcher.hpp"
#include "textitem.hpp"
#ifdef DEBUG
#include <QtDebug>
#endif
#include <QApplication>
#include <QBoxLayout>
#include <QCheckBox>
#include <QComboBox>
#include <QDir>
#include <QDockWidget>
#include <QEvent>
#include <QFileDialog>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPainter>
#include <QPixmapCache>
#include <QPlainTextEdit>
#include <QPrinter>
#include <QPushButton>
#include <QRadioButton>
#include <QScrollArea>
#include <QScrollBar>
#include <QSettings>
#include <QSpinBox>
#include <QSplitter>
#include <QTextStream>
MainWindow::MainWindow(const Debug debug,
const InitialComparisonMode comparisonMode,
const QString &filename1, const QString &filename2,
const QString &language, QWidget *parent)
: QMainWindow(parent),
controlDockArea(Qt::RightDockWidgetArea),
actionDockArea(Qt::RightDockWidgetArea),
marginsDockArea(Qt::RightDockWidgetArea),
zoningDockArea(Qt::RightDockWidgetArea),
logDockArea(Qt::RightDockWidgetArea), cancel(false),
saveAll(true), savePages(SaveBothPages), language(language),
debug(debug), aboutForm(0), helpForm(0)
{
currentPath = QDir::homePath();
QSettings settings;
pen.setStyle(Qt::NoPen);
pen.setColor(Qt::red);
pen = settings.value("Outline", pen).value<QPen>();
brush.setColor(pen.color());
brush.setStyle(Qt::SolidPattern);
brush = settings.value("Fill", brush).value<QBrush>();
showToolTips = settings.value("ShowToolTips", true).toBool();
combineTextHighlighting = settings.value("CombineTextHighlighting",
true).toBool();
QPixmapCache::setCacheLimit(1000 *
qBound(1, settings.value("CacheSizeMB", 25).toInt(), 100));
createWidgets(filename1, filename2);
createCentralArea();
createDockWidgets();
createConnections();
restoreGeometry(settings.value("MainWindow/Geometry").toByteArray());
restoreState(settings.value("MainWindow/State").toByteArray());
controlDockLocationChanged(static_cast<Qt::DockWidgetArea>(
settings.value("MainWindow/ControlDockArea",
static_cast<int>(controlDockArea)).toInt()));
actionDockLocationChanged(static_cast<Qt::DockWidgetArea>(
settings.value("MainWindow/ActionDockArea",
static_cast<int>(actionDockArea)).toInt()));
zoningDockLocationChanged(static_cast<Qt::DockWidgetArea>(
settings.value("MainWindow/ZoningDockArea",
static_cast<int>(zoningDockArea)).toInt()));
marginsDockLocationChanged(static_cast<Qt::DockWidgetArea>(
settings.value("MainWindow/MarginsDockArea",
static_cast<int>(marginsDockArea)).toInt()));
controlDockWidget->resize(controlDockWidget->minimumSizeHint());
actionDockWidget->resize(actionDockWidget->minimumSizeHint());
zoningDockWidget->resize(zoningDockWidget->minimumSizeHint());
marginsDockWidget->resize(marginsDockWidget->minimumSizeHint());
//logDockWidget->resize(logDockWidget->minimumSizeHint());
setWindowTitle(tr("DiffPDF"));
setWindowIcon(QIcon(":/icon.png"));
compareComboBox->setCurrentIndex(comparisonMode);
QMetaObject::invokeMethod(this, "initialize", Qt::QueuedConnection,
Q_ARG(QString, filename1),
Q_ARG(QString, filename2));
}
void MainWindow::createWidgets(const QString &filename1,
const QString &filename2)
{
setFile1Button = new QPushButton(tr("File #&1..."));
setFile1Button->setToolTip(tr("<p>Choose the first (left hand) file "
"to be compared."));
filename1LineEdit = new LineEdit;
filename1LineEdit->setToolTip(tr("The first (left hand) file."));
filename1LineEdit->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
filename1LineEdit->setMinimumWidth(100);
filename1LineEdit->setText(filename1);
setFile2Button = new QPushButton(tr("File #&2..."));
setFile2Button->setToolTip(tr("<p>Choose the second (right hand) file "
"to be compared."));
filename2LineEdit = new LineEdit;
filename2LineEdit->setToolTip(tr("The second (right hand) file."));
filename2LineEdit->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
filename2LineEdit->setMinimumWidth(100);
filename2LineEdit->setText(filename2);
comparePages1Label = new QLabel(tr("Pa&ges:"));
pages1LineEdit = new QLineEdit;
comparePages1Label->setBuddy(pages1LineEdit);
pages1LineEdit->setToolTip(tr("<p>Pages can be specified using ranges "
"such as 1-10, and multiple ranges can be used, e.g., "
"1-10, 12-15, 20, 22, 35-39. This makes it "
"straighforward to compare similar documents where one "
"has one or more additional pages.<p>For example, if "
"file1.pdf has pages 1-30 and file2.pdf has pages 1-31 "
"with the extra page being page 14, the two page ranges "
"would be set to 1-30 for file1.pdf and 1-13, 15-31 for "
"file2.pdf."));
comparePages2Label = new QLabel(tr("&Pages:"));
pages2LineEdit = new QLineEdit;
comparePages2Label->setBuddy(pages2LineEdit);
pages2LineEdit->setToolTip(pages1LineEdit->toolTip());
compareButton = new QPushButton(tr("&Compare"));
compareButton->setEnabled(false);
compareButton->setDefault(true);
compareButton->setAutoDefault(true);
compareButton->setToolTip(tr("<p>Click to compare (or re-compare) "
"the documents—or to cancel a comparison that's "
"in progress."));
compareComboBox = new QComboBox;
compareComboBox->addItems(QStringList() << tr("Appearance")
<< tr("Characters") << tr("Words"));
compareComboBox->setToolTip(
tr("<p>If the <b>Words</b> comparison "
"mode is chosen, then each page's text is compared "
"word by word (best for alphabetic languages like "
"English). "
"If the <b>Characters</b> comparison mode is chosen, "
"then each page's text is compared character by "
"character (best for logographic languages like Chinese "
"and Japanese). "
"If the <b>Appearance</b> comparison mode is chosen "
"then each page's visual appearance is compared. "
"Comparing appearance can be slow for large documents "
"and can also produce false positives—but is "
"absolutely precise."));
compareLabel = new QLabel(tr("Co&mpare:"));
compareLabel->setBuddy(compareComboBox);
viewDiffLabel = new QLabel(tr("&View:"));
viewDiffLabel->setToolTip(tr("<p>Shows each pair of pages which "
"are different. The comparison is textual unless the "
"<b>Appearance</b> comparison mode is chosen, in "
"which case the comparison is done visually. "
"Visual differences can occur if a paragraph is "
"formated differently or if an embedded diagram or "
"image has changed."));
viewDiffComboBox = new QComboBox;
viewDiffComboBox->addItem(tr("(Not viewing)"));
viewDiffLabel->setBuddy(viewDiffComboBox);
viewDiffComboBox->setToolTip(viewDiffLabel->toolTip());
showLabel = new QLabel(tr("S&how:"));
showLabel->setToolTip(tr("<p>In show <b>Highlighting</b> mode the "
"pages are shown side by side with their differences "
"highlighted. All the other modes are composition "
"modes which show the first PDF as-is and the "
"composition (blend) of the two PDFs."));
showComboBox = new QComboBox;
showComboBox->addItem(tr("%1Highlighting%2")
.arg(QChar(0xAB)).arg(QChar(0xBB)), -1);
showComboBox->addItem(tr("Not Src Xor Dest"),
QPainter::RasterOp_NotSourceXorDestination);
showComboBox->addItem(tr("Difference"),
QPainter::CompositionMode_Difference);
showComboBox->addItem(tr("Exclusion"),
QPainter::CompositionMode_Exclusion);
showComboBox->addItem(tr("Src Xor Dest"),
QPainter::RasterOp_SourceXorDestination);
showLabel->setBuddy(showComboBox);
showComboBox->setToolTip(showLabel->toolTip());
previousButton = new QPushButton(tr("Previo&us"));
previousButton->setToolTip(
"<p>Navigate to the previous pair of pages.");
#if QT_VERSION >= 0x040600
previousButton->setIcon(QIcon(":/left.png"));
#endif
nextButton = new QPushButton(tr("Ne&xt"));
nextButton->setToolTip("<p>Navigate to the next pair of pages.");
#if QT_VERSION >= 0x040600
nextButton->setIcon(QIcon(":/right.png"));
#endif
zoomLabel = new QLabel(tr("&Zoom:"));
zoomLabel->setToolTip(tr("<p>Determines the scale at which the "
"pages are shown."));
zoomSpinBox = new QSpinBox;
zoomLabel->setBuddy(zoomSpinBox);
zoomSpinBox->setRange(20, 800);
zoomSpinBox->setSuffix(tr(" %"));
zoomSpinBox->setSingleStep(5);
QSettings settings;
zoomSpinBox->setValue(settings.value("Zoom", 100).toInt());
zoomSpinBox->setToolTip(zoomLabel->toolTip());
zoningGroupBox = new QGroupBox(tr("Zo&ning"));
zoningGroupBox->setToolTip(tr("<p>Zoning is a computationally "
"expensive experimental mode that can reduce or "
"eliminate false positives particularly for pages "
"that have tables or that mix alphabetic and "
"logographic languages—it can also increase "
"false positives! Zoning only applies to text "
"comparisons."));
zoningGroupBox->setCheckable(true);
zoningGroupBox->setChecked(false);
columnsLabel = new QLabel(tr("Co&lumns:"));
columnsSpinBox = new QSpinBox;
columnsSpinBox->setRange(1, 16);
columnsSpinBox->setValue(settings.value("Columns", 1).toInt());
columnsSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
columnsSpinBox->setToolTip(tr("<p>Use this to tell DiffPDF how "
"many columns the page has; this should improve the "
"zoning."));
columnsLabel->setBuddy(columnsSpinBox);
toleranceRLabel = new QLabel(tr("Tolerance/&R:"));
toleranceRSpinBox = new QSpinBox;
toleranceRSpinBox->setRange(4, 144);
toleranceRSpinBox->setValue(settings.value("Tolerance/R", 8).toInt());
toleranceRSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
toleranceRSpinBox->setToolTip(tr("<p>This is the maximum distance "
"between text (word) rectangles for the rectangles to "
"appear in the same zone."));
toleranceRLabel->setBuddy(toleranceRSpinBox);
toleranceYLabel = new QLabel(tr("Tolerance/&Y:"));
toleranceYSpinBox = new QSpinBox;
toleranceYSpinBox->setRange(0, 32);
toleranceYSpinBox->setValue(settings.value("Tolerance/Y", 10).toInt());
toleranceYSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
toleranceYSpinBox->setToolTip(tr("<p>Text position <i>y</i> "
"coordinates are rounded to the nearest Tolerance/Y "
"value when zoning."));
toleranceYLabel->setBuddy(toleranceYSpinBox);
showZonesCheckBox = new QCheckBox(tr("Sho&w Zones"));
showZonesCheckBox->setToolTip(tr("<p>This shows the zones that are "
"being used and may be helpful when adjusting "
"tolerances. (Its original purpose was for debugging.)"));
marginsGroupBox = new QGroupBox(tr("&Exclude Margins"));
marginsGroupBox->setToolTip(tr("<p>If this is checked, anything "
"outside non-zero margins is ignored when comparing."));
marginsGroupBox->setCheckable(true);
marginsGroupBox->setChecked(settings.value("Margins/Exclude",
false).toBool());
topMarginLabel = new QLabel(tr("&Top:"));
topMarginSpinBox = new QSpinBox;
topMarginSpinBox->setSuffix(" pt");
topMarginSpinBox->setValue(settings.value("Margins/Top", 0).toInt());
topMarginSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
topMarginSpinBox->setToolTip(tr("<p>The top margin in points. "
"Anything above this will be ignored."));
topMarginLabel->setBuddy(topMarginSpinBox);
bottomMarginLabel = new QLabel(tr("&Bottom:"));
bottomMarginSpinBox = new QSpinBox;
bottomMarginSpinBox->setSuffix(" pt");
bottomMarginSpinBox->setValue(settings.value("Margins/Bottom", 0)
.toInt());
bottomMarginSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
bottomMarginSpinBox->setToolTip(tr("<p>The bottom margin in points. "
"Anything below this will be ignored."));
bottomMarginLabel->setBuddy(bottomMarginSpinBox);
leftMarginLabel = new QLabel(tr("Le&ft:"));
leftMarginSpinBox = new QSpinBox;
leftMarginSpinBox->setSuffix(" pt");
leftMarginSpinBox->setValue(settings.value("Margins/Left", 0).toInt());
leftMarginSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
leftMarginSpinBox->setToolTip(tr("<p>The left margin in points. "
"Anything left of this will be ignored."));
leftMarginLabel->setBuddy(leftMarginSpinBox);
rightMarginLabel = new QLabel(tr("R&ight:"));
rightMarginSpinBox = new QSpinBox;
rightMarginSpinBox->setSuffix(" pt");
rightMarginSpinBox->setValue(settings.value("Margins/Right", 0)
.toInt());
rightMarginSpinBox->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
rightMarginSpinBox->setToolTip(tr("<p>The right margin in points. "
"Anything right of this will be ignored."));
rightMarginLabel->setBuddy(rightMarginSpinBox);
statusLabel = new QLabel(tr("Choose files..."));
statusLabel->setFrameStyle(QFrame::StyledPanel|QFrame::Sunken);
statusLabel->setMaximumHeight(statusLabel->minimumSizeHint().height());
optionsButton = new QPushButton(tr("&Options..."));
optionsButton->setToolTip(tr("Click to customize the application."));
saveButton = new QPushButton(tr("&Save As..."));
saveButton->setToolTip(tr("Save the differences."));
helpButton = new QPushButton(tr("Help"));
helpButton->setShortcut(tr("F1"));
helpButton->setToolTip(tr("Click for basic help."));
aboutButton = new QPushButton(tr("&About"));
aboutButton->setToolTip(tr("Click for copyright and credits."));
quitButton = new QPushButton(tr("&Quit"));
quitButton->setToolTip(tr("Click to terminate the application."));
page1Label = new Label;
page1Label->setAlignment(Qt::AlignTop|Qt::AlignLeft);
page1Label->setToolTip(tr("<p>Shows the first (left hand) document's "
"page that corresponds to the page shown in the "
"View Difference combobox."));
page2Label = new Label;
page2Label->setAlignment(Qt::AlignTop|Qt::AlignLeft);
page2Label->setToolTip(tr("<p>Shows the second (right hand) "
"document's page that corresponds to the page shown in "
"the View Difference combobox."));
logEdit = new QPlainTextEdit;
QList<QWidget*> widgets;
widgets << setFile1Button << filename1LineEdit << pages1LineEdit
<< page1Label << setFile2Button << filename2LineEdit
<< pages2LineEdit << page2Label << compareButton
<< compareComboBox << viewDiffLabel << viewDiffComboBox
<< showLabel << showComboBox << zoomLabel << zoomSpinBox
<< optionsButton << zoningGroupBox << columnsLabel
<< columnsSpinBox << toleranceRLabel << toleranceRSpinBox
<< toleranceYLabel << toleranceYSpinBox << marginsGroupBox
<< topMarginLabel << topMarginSpinBox << bottomMarginSpinBox
<< bottomMarginLabel << leftMarginLabel << leftMarginSpinBox
<< rightMarginLabel << rightMarginSpinBox << saveButton
<< helpButton << aboutButton << quitButton << logEdit
<< previousButton << nextButton << showZonesCheckBox;
foreach (QWidget *widget, widgets)
if (!widget->toolTip().isEmpty())
widget->installEventFilter(this);
}
void MainWindow::createCentralArea()
{
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(setFile1Button);
topLeftLayout->addWidget(filename1LineEdit, 3);
topLeftLayout->addWidget(comparePages1Label);
topLeftLayout->addWidget(pages1LineEdit, 2);
area1 = new QScrollArea;
area1->setWidget(page1Label);
area1->setWidgetResizable(true);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(area1, 1);
QWidget *leftWidget = new QWidget;
leftWidget->setLayout(leftLayout);
QHBoxLayout *topRightLayout = new QHBoxLayout;
topRightLayout->addWidget(setFile2Button);
topRightLayout->addWidget(filename2LineEdit, 3);
topRightLayout->addWidget(comparePages2Label);
topRightLayout->addWidget(pages2LineEdit, 2);
area2 = new QScrollArea;
area2->setWidget(page2Label);
area2->setWidgetResizable(true);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addLayout(topRightLayout);
rightLayout->addWidget(area2, 1);
QWidget *rightWidget = new QWidget;
rightWidget->setLayout(rightLayout);
splitter = new QSplitter(Qt::Horizontal);
splitter->addWidget(leftWidget);
splitter->addWidget(rightWidget);
QSettings settings;
splitter->restoreState(settings.value("MainWindow/ViewSplitter")
.toByteArray());
setCentralWidget(splitter);
}
void MainWindow::createDockWidgets()
{
setTabPosition(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea,
QTabWidget::North);
setTabPosition(Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea,
QTabWidget::West);
QDockWidget::DockWidgetFeatures features =
QDockWidget::DockWidgetMovable|
QDockWidget::DockWidgetFloatable;
controlDockWidget = new QDockWidget(tr("Controls"), this);
controlDockWidget->setObjectName("Controls");
controlDockWidget->setFeatures(features);
controlLayout = new QBoxLayout(QBoxLayout::TopToBottom);
compareLayout = new QHBoxLayout;
compareLayout->addWidget(compareLabel);
compareLayout->addWidget(compareComboBox, 1);
controlLayout->addLayout(compareLayout);
QHBoxLayout *viewLayout = new QHBoxLayout;
viewLayout->addWidget(viewDiffLabel);
viewLayout->addWidget(viewDiffComboBox, 1);
controlLayout->addLayout(viewLayout);
QHBoxLayout *showLayout = new QHBoxLayout;
showLayout->addWidget(showLabel);
showLayout->addWidget(showComboBox, 1);
controlLayout->addLayout(showLayout);
QHBoxLayout *navigationLayout = new QHBoxLayout;
navigationLayout->addWidget(previousButton);
navigationLayout->addWidget(nextButton);
controlLayout->addLayout(navigationLayout);
QHBoxLayout *zoomLayout = new QHBoxLayout;
zoomLayout->addWidget(zoomLabel);
zoomLayout->addWidget(zoomSpinBox);
controlLayout->addLayout(zoomLayout);
controlLayout->addWidget(statusLabel);
controlLayout->addStretch();
QWidget *widget = new QWidget;
widget->setLayout(controlLayout);
controlDockWidget->setWidget(widget);
addDockWidget(controlDockArea, controlDockWidget);
actionDockWidget = new QDockWidget(tr("Actions"), this);
actionDockWidget->setObjectName("Actions");
actionDockWidget->setFeatures(features);
actionLayout = new QBoxLayout(QBoxLayout::TopToBottom);
actionLayout->addWidget(compareButton);
QHBoxLayout *optionLayout = new QHBoxLayout;
optionLayout->addWidget(optionsButton);
optionLayout->addWidget(saveButton);
actionLayout->addLayout(optionLayout);
QHBoxLayout *helpLayout = new QHBoxLayout;
helpLayout->addWidget(helpButton);
helpLayout->addWidget(aboutButton);
actionLayout->addLayout(helpLayout);
actionLayout->addWidget(quitButton);
actionLayout->addStretch();
widget = new QWidget;
widget->setLayout(actionLayout);
actionDockWidget->setWidget(widget);
addDockWidget(actionDockArea, actionDockWidget);
marginsDockWidget = new QDockWidget(tr("Margins"), this);
marginsDockWidget->setObjectName("Margins");
marginsDockWidget->setFeatures(features|
QDockWidget::DockWidgetClosable);
marginsLayout = new QBoxLayout(QBoxLayout::TopToBottom);
QHBoxLayout *topMarginLayout = new QHBoxLayout;
topMarginLayout->addWidget(topMarginLabel);
topMarginLayout->addWidget(topMarginSpinBox);
marginsLayout->addLayout(topMarginLayout);
QHBoxLayout *bottomMarginLayout = new QHBoxLayout;
bottomMarginLayout->addWidget(bottomMarginLabel);
bottomMarginLayout->addWidget(bottomMarginSpinBox);
marginsLayout->addLayout(bottomMarginLayout);
QHBoxLayout *leftMarginLayout = new QHBoxLayout;
leftMarginLayout->addWidget(leftMarginLabel);
leftMarginLayout->addWidget(leftMarginSpinBox);
marginsLayout->addLayout(leftMarginLayout);
QHBoxLayout *rightMarginLayout = new QHBoxLayout;
rightMarginLayout->addWidget(rightMarginLabel);
rightMarginLayout->addWidget(rightMarginSpinBox);
marginsLayout->addLayout(rightMarginLayout);
marginsLayout->addStretch();
marginsGroupBox->setLayout(marginsLayout);
marginsDockWidget->setWidget(marginsGroupBox);
addDockWidget(marginsDockArea, marginsDockWidget);
zoningDockWidget = new QDockWidget(tr("Zoning"), this);
zoningDockWidget->setObjectName("Zoning");
zoningDockWidget->setFeatures(features|
QDockWidget::DockWidgetClosable);
zoningLayout = new QBoxLayout(QBoxLayout::TopToBottom);
QHBoxLayout *columnLayout = new QHBoxLayout;
columnLayout->addWidget(columnsLabel);
columnLayout->addWidget(columnsSpinBox);
zoningLayout->addLayout(columnLayout);
QHBoxLayout *toleranceLayout1 = new QHBoxLayout;
toleranceLayout1->addWidget(toleranceRLabel);
toleranceLayout1->addWidget(toleranceRSpinBox);
zoningLayout->addLayout(toleranceLayout1);
QHBoxLayout *toleranceLayout2 = new QHBoxLayout;
toleranceLayout2->addWidget(toleranceYLabel);
toleranceLayout2->addWidget(toleranceYSpinBox);
zoningLayout->addLayout(toleranceLayout2);
zoningLayout->addWidget(showZonesCheckBox);
zoningLayout->addStretch();
zoningGroupBox->setLayout(zoningLayout);
zoningDockWidget->setWidget(zoningGroupBox);
addDockWidget(zoningDockArea, zoningDockWidget);
logDockWidget = new QDockWidget(tr("Log"), this);
logDockWidget->setObjectName("Log");
logDockWidget->setFeatures(features|QDockWidget::DockWidgetClosable);
logDockWidget->setWidget(logEdit);
addDockWidget(Qt::RightDockWidgetArea, logDockWidget);
tabifyDockWidget(marginsDockWidget, controlDockWidget);
tabifyDockWidget(logDockWidget, zoningDockWidget);
tabifyDockWidget(zoningDockWidget, actionDockWidget);
}
void MainWindow::createConnections()
{
connect(area1->verticalScrollBar(), SIGNAL(valueChanged(int)),
area2->verticalScrollBar(), SLOT(setValue(int)));
connect(area2->verticalScrollBar(), SIGNAL(valueChanged(int)),
area1->verticalScrollBar(), SLOT(setValue(int)));
connect(area1->horizontalScrollBar(), SIGNAL(valueChanged(int)),
area2->horizontalScrollBar(), SLOT(setValue(int)));
connect(area2->horizontalScrollBar(), SIGNAL(valueChanged(int)),
area1->horizontalScrollBar(), SLOT(setValue(int)));
connect(filename1LineEdit, SIGNAL(textEdited(const QString&)),
this, SLOT(updateUi()));
connect(filename1LineEdit,
SIGNAL(filenamesDropped(const QStringList&)),
this, SLOT(setFiles1(const QStringList&)));
connect(filename2LineEdit, SIGNAL(textEdited(const QString&)),
this, SLOT(updateUi()));
connect(filename2LineEdit,
SIGNAL(filenamesDropped(const QStringList&)),
this, SLOT(setFiles2(const QStringList&)));
connect(page1Label, SIGNAL(filenamesDropped(const QStringList&)),
this, SLOT(setFiles1(const QStringList&)));
connect(page2Label, SIGNAL(filenamesDropped(const QStringList&)),
this, SLOT(setFiles2(const QStringList&)));
connect(compareComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateUi()));
connect(compareComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateViews()));
connect(viewDiffComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateViews(int)));
connect(viewDiffComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateUi()));
connect(showComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateViews()));
connect(previousButton, SIGNAL(clicked()),
this, SLOT(previousPages()));
connect(nextButton, SIGNAL(clicked()), this, SLOT(nextPages()));
connect(setFile1Button, SIGNAL(clicked()), this, SLOT(setFile1()));
connect(setFile2Button, SIGNAL(clicked()), this, SLOT(setFile2()));
connect(compareButton, SIGNAL(clicked()), this, SLOT(compare()));
connect(zoomSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(zoningGroupBox, SIGNAL(toggled(bool)),
this, SLOT(updateUi()));
connect(zoningGroupBox, SIGNAL(toggled(bool)),
this, SLOT(updateViews()));
connect(columnsSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(toleranceRSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(toleranceYSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(showZonesCheckBox, SIGNAL(toggled(bool)),
this, SLOT(updateViews()));
connect(marginsGroupBox, SIGNAL(toggled(bool)),
this, SLOT(updateUi()));
connect(marginsGroupBox, SIGNAL(toggled(bool)),
this, SLOT(updateViews()));
connect(leftMarginSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(rightMarginSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(topMarginSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(bottomMarginSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateViews()));
connect(page1Label, SIGNAL(clicked(const QPoint&)),
this, SLOT(setAMargin(const QPoint&)));
connect(page2Label, SIGNAL(clicked(const QPoint&)),
this, SLOT(setAMargin(const QPoint&)));
connect(optionsButton, SIGNAL(clicked()), this, SLOT(options()));
connect(saveButton, SIGNAL(clicked()), this, SLOT(save()));
connect(helpButton, SIGNAL(clicked()), this, SLOT(help()));
connect(aboutButton, SIGNAL(clicked()), this, SLOT(about()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(controlDockWidget,
SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
this, SLOT(controlDockLocationChanged(Qt::DockWidgetArea)));
connect(actionDockWidget,
SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
this, SLOT(actionDockLocationChanged(Qt::DockWidgetArea)));
connect(zoningDockWidget,
SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
this, SLOT(zoningDockLocationChanged(Qt::DockWidgetArea)));
connect(marginsDockWidget,
SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
this, SLOT(marginsDockLocationChanged(Qt::DockWidgetArea)));
connect(controlDockWidget, SIGNAL(topLevelChanged(bool)),
this, SLOT(controlTopLevelChanged(bool)));
connect(actionDockWidget, SIGNAL(topLevelChanged(bool)),
this, SLOT(actionTopLevelChanged(bool)));
connect(zoningDockWidget, SIGNAL(topLevelChanged(bool)),
this, SLOT(zoningTopLevelChanged(bool)));
connect(marginsDockWidget, SIGNAL(topLevelChanged(bool)),
this, SLOT(marginsTopLevelChanged(bool)));
connect(logDockWidget, SIGNAL(topLevelChanged(bool)),
this, SLOT(logTopLevelChanged(bool)));
}
void MainWindow::initialize(const QString &filename1,
const QString &filename2)
{
if (!filename1.isEmpty()) {
setFile1(filename1);
setFile2Button->setFocus();
if (!filename2.isEmpty()) {
setFile2(filename2);
compare();
}
}
else
updateUi();
}
void MainWindow::updateUi()
{
compareButton->setEnabled(!filename1LineEdit->text().isEmpty() &&
!filename2LineEdit->text().isEmpty());
saveButton->setEnabled(viewDiffComboBox->count() > 1);
if (!showZonesCheckBox->isEnabled())
showZonesCheckBox->setChecked(false);
if (compareComboBox->currentIndex() != CompareAppearance)
showComboBox->setCurrentIndex(0);
showComboBox->setEnabled(compareComboBox->currentIndex() ==
CompareAppearance);
QPushButton *button = qobject_cast<QPushButton*>(focusWidget());
bool enableNavigationButton = (button == previousButton ||
button == nextButton);
previousButton->setEnabled(viewDiffComboBox->count() > 1 &&
viewDiffComboBox->currentIndex() > 0);
nextButton->setEnabled(viewDiffComboBox->count() > 1 &&
viewDiffComboBox->currentIndex() + 1 <
viewDiffComboBox->count());
if (enableNavigationButton && !(previousButton->isEnabled() &&
nextButton->isEnabled())) {
if (previousButton->isEnabled())
previousButton->setFocus();
else
nextButton->setFocus();
}
if (marginsGroupBox->isChecked()) {
page1Label->setCursor(Qt::PointingHandCursor);
page2Label->setCursor(Qt::PointingHandCursor);
}
else {
page1Label->setCursor(Qt::ArrowCursor);
page2Label->setCursor(Qt::ArrowCursor);
}
}
void MainWindow::controlDockLocationChanged(Qt::DockWidgetArea area)
{
if (area == Qt::TopDockWidgetArea ||
area == Qt::BottomDockWidgetArea) {
controlLayout->setDirection(QBoxLayout::LeftToRight);
}
else {
controlLayout->setDirection(QBoxLayout::TopToBottom);
}
controlDockArea = area;
}
void MainWindow::actionDockLocationChanged(Qt::DockWidgetArea area)
{
if (area == Qt::TopDockWidgetArea ||
area == Qt::BottomDockWidgetArea)
actionLayout->setDirection(QBoxLayout::LeftToRight);
else
actionLayout->setDirection(QBoxLayout::TopToBottom);
actionDockArea = area;
}
void MainWindow::zoningDockLocationChanged(Qt::DockWidgetArea area)
{
if (area == Qt::TopDockWidgetArea ||
area == Qt::BottomDockWidgetArea)
zoningLayout->setDirection(QBoxLayout::LeftToRight);
else
zoningLayout->setDirection(QBoxLayout::TopToBottom);
zoningDockArea = area;
}
void MainWindow::marginsDockLocationChanged(Qt::DockWidgetArea area)
{
if (area == Qt::TopDockWidgetArea ||
area == Qt::BottomDockWidgetArea)
marginsLayout->setDirection(QBoxLayout::LeftToRight);
else
marginsLayout->setDirection(QBoxLayout::TopToBottom);
marginsDockArea = area;
}
void MainWindow::controlTopLevelChanged(bool floating)
{
controlLayout->setDirection(floating ? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight);
if (QWidget *widget = static_cast<QWidget*>(controlLayout->parent()))
widget->setFixedSize(floating ? widget->minimumSizeHint()
: QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
controlDockWidget->setWindowTitle(floating ? tr("DiffPDF — Controls")
: tr("Controls"));
}
void MainWindow::actionTopLevelChanged(bool floating)
{
actionLayout->setDirection(floating ? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight);
if (QWidget *widget = static_cast<QWidget*>(actionLayout->parent()))
widget->setFixedSize(floating ? widget->minimumSizeHint()
: QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
actionDockWidget->setWindowTitle(floating ? tr("DiffPDF — Actions")
: tr("Actions"));
}
void MainWindow::zoningTopLevelChanged(bool floating)
{
zoningLayout->setDirection(floating ? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight);
if (QWidget *widget = static_cast<QWidget*>(zoningLayout->parent()))
widget->setFixedSize(floating ? widget->minimumSizeHint()
: QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
zoningDockWidget->setWindowTitle(floating ? tr("DiffPDF — Zoning")
: tr("Zoning"));
}
void MainWindow::marginsTopLevelChanged(bool floating)
{
marginsLayout->setDirection(floating ? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight);
if (QWidget *widget = static_cast<QWidget*>(marginsLayout->parent()))
widget->setFixedSize(floating ? widget->minimumSizeHint()
: QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
marginsDockWidget->setWindowTitle(floating ? tr("DiffPDF — Margins")
: tr("Margins"));
}
void MainWindow::logTopLevelChanged(bool floating)
{
logDockWidget->setWindowTitle(floating ? tr("DiffPDF — Log")
: tr("Log"));
}
void MainWindow::previousPages()
{
int i = viewDiffComboBox->currentIndex();
if (i > 0)
viewDiffComboBox->setCurrentIndex(i - 1);
}
void MainWindow::nextPages()
{
int i = viewDiffComboBox->currentIndex();
if (i + 1 < viewDiffComboBox->count())
viewDiffComboBox->setCurrentIndex(i + 1);
}
void MainWindow::updateViews(int index)
{
if (index == 0) {
page1Label->clear();
page2Label->clear();
return;
}
else if (index == -1)
index = viewDiffComboBox->currentIndex();
PagePair pair = viewDiffComboBox->itemData(index).value<PagePair>();
if (pair.isNull())
return;
QString filename1 = filename1LineEdit->text();
PdfDocument pdf1 = getPdf(filename1);
if (!pdf1)
return;
PdfPage page1(pdf1->page(pair.left));
if (!page1)
return;
QString filename2 = filename2LineEdit->text();
PdfDocument pdf2 = getPdf(filename2);
if (!pdf2)
return;
PdfPage page2(pdf2->page(pair.right));
if (!page2)
return;
const QPair<QString, QString> keys = cacheKeys(index, pair);
const QPair<QPixmap, QPixmap> pixmaps = populatePixmaps(pdf1, page1,
pdf2, page2, pair.hasVisualDifference, keys.first,
keys.second);
page1Label->setPixmap(pixmaps.first);
page2Label->setPixmap(pixmaps.second);
if (showZonesCheckBox->isChecked())
showZones();
if (marginsGroupBox->isChecked())
showMargins();
}
const QPair<QString, QString> MainWindow::cacheKeys(const int index,
const PagePair &pair) const
{
int comparisonMode;
if (compareComboBox->currentIndex() == CompareAppearance)
comparisonMode = showComboBox->currentIndex();
else
comparisonMode = -compareComboBox->currentIndex();
QString zoning;
if (zoningGroupBox->isChecked())
zoning = QString("%1:%2:%3").arg(columnsSpinBox->value())
.arg(toleranceRSpinBox->value())
.arg(toleranceYSpinBox->value());
QString margins;
if (marginsGroupBox->isChecked())
margins = QString("%1:%2:%3:%4").arg(topMarginSpinBox->value())
.arg(bottomMarginSpinBox->value())
.arg(leftMarginSpinBox->value())
.arg(rightMarginSpinBox->value());
const QString key = QString("%1:%2:%3:%4:%5").arg(index)
.arg(zoomSpinBox->value()).arg(comparisonMode).arg(zoning)
.arg(margins);
const QString key1 = QString("1:%1:%2:%3").arg(key).arg(pair.left)
.arg(filename1LineEdit->text());
const QString key2 = QString("2:%1:%2:%3").arg(key).arg(pair.right)
.arg(filename2LineEdit->text());
return qMakePair(key1, key2);
}
const QPair<QPixmap, QPixmap> MainWindow::populatePixmaps(
const PdfDocument &pdf1, const PdfPage &page1,
const PdfDocument &pdf2, const PdfPage &page2,
bool hasVisualDifference, const QString &key1,
const QString &key2)
{
QPixmap pixmap1;
QPixmap pixmap2;
#if QT_VERSION >= 0x040600
if (!QPixmapCache::find(key1, &pixmap1) ||
!QPixmapCache::find(key2, &pixmap2)) {
#else
if (!QPixmapCache::find(key1, pixmap1) ||
!QPixmapCache::find(key2, pixmap2)) {
#endif
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
const int DPI = static_cast<int>(POINTS_PER_INCH *
(zoomSpinBox->value() / 100.0));
const bool compareText = compareComboBox->currentIndex() !=
CompareAppearance;
QImage plainImage1;
QImage plainImage2;
if (hasVisualDifference || !compareText) {
plainImage1 = page1->renderToImage(DPI, DPI);
plainImage2 = page2->renderToImage(DPI, DPI);
}
pdf1->setRenderHint(Poppler::Document::Antialiasing);
pdf1->setRenderHint(Poppler::Document::TextAntialiasing);
pdf2->setRenderHint(Poppler::Document::Antialiasing);
pdf2->setRenderHint(Poppler::Document::TextAntialiasing);
QImage image1 = page1->renderToImage(DPI, DPI);
QImage image2 = page2->renderToImage(DPI, DPI);
if (compareComboBox->currentIndex() != CompareAppearance ||
showComboBox->currentIndex() == 0) {
QPainterPath highlighted1;
QPainterPath highlighted2;
if (hasVisualDifference || !compareText)
computeVisualHighlights(&highlighted1, &highlighted2,
plainImage1, plainImage2);
else
computeTextHighlights(&highlighted1, &highlighted2, page1,
page2, DPI);
if (!highlighted1.isEmpty())
paintOnImage(highlighted1, &image1);
if (!highlighted2.isEmpty())
paintOnImage(highlighted2, &image2);
if (highlighted1.isEmpty() && highlighted2.isEmpty()) {
QFont font("Helvetica", 14);
font.setOverline(true);
font.setUnderline(true);
highlighted1.addText(DPI / 4, DPI / 4, font,
tr("DiffPDF: False Positive"));
paintOnImage(highlighted1, &image1);
}
pixmap1 = QPixmap::fromImage(image1);
pixmap2 = QPixmap::fromImage(image2);
} else {
pixmap1 = QPixmap::fromImage(image1);
QImage composed(image1.size(), image1.format());
QPainter painter(&composed);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(composed.rect(), Qt::transparent);
painter.setCompositionMode(
QPainter::CompositionMode_SourceOver);
painter.drawImage(0, 0, image1);
painter.setCompositionMode(
static_cast<QPainter::CompositionMode>(
showComboBox->itemData(
showComboBox->currentIndex()).toInt()));
painter.drawImage(0, 0, image2);
painter.setCompositionMode(
QPainter::CompositionMode_DestinationOver);
painter.fillRect(composed.rect(), Qt::white);
painter.end();
pixmap2 = QPixmap::fromImage(composed);
}
QPixmapCache::insert(key1, pixmap1);
QPixmapCache::insert(key2, pixmap2);
QApplication::restoreOverrideCursor();
}
return qMakePair(pixmap1, pixmap2);
}
void MainWindow::computeTextHighlights(QPainterPath *highlighted1,
QPainterPath *highlighted2, const PdfPage &page1,
const PdfPage &page2, const int DPI)
{
const bool ComparingWords = compareComboBox->currentIndex() ==
CompareWords;
QRectF rect1;
QRectF rect2;
QSettings settings;
const int OVERLAP = settings.value("Overlap", 5).toInt();
const bool COMBINE = settings.value("CombineTextHighlighting", true)
.toBool();
QRectF rect;
if (marginsGroupBox->isChecked())
rect = pointRectForMargins(page1->pageSize());
const TextBoxList list1 = getTextBoxes(page1, rect);
const TextBoxList list2 = getTextBoxes(page2, rect);
TextItems items1 = ComparingWords ? getWords(list1)
: getCharacters(list1);
TextItems items2 = ComparingWords ? getWords(list2)
: getCharacters(list2);
const int ToleranceY = toleranceYSpinBox->value();
if (zoningGroupBox->isChecked()) {
const int ToleranceR = toleranceRSpinBox->value();
const int Columns = columnsSpinBox->value();
items1.columnZoneYxOrder(page1->pageSize().width(), ToleranceR,
ToleranceY, Columns);
items2.columnZoneYxOrder(page2->pageSize().width(), ToleranceR,
ToleranceY, Columns);
}
if (debug >= DebugShowTexts) {
const bool Yx = debug == DebugShowTextsAndYX;
items1.debug(1, ToleranceY, ComparingWords, Yx);
items2.debug(2, ToleranceY, ComparingWords, Yx);
}
SequenceMatcher matcher(items1.texts(), items2.texts());
RangesPair rangesPair = computeRanges(&matcher);
rangesPair = invertRanges(rangesPair.first, items1.count(),
rangesPair.second, items2.count());
foreach (int index, rangesPair.first)
addHighlighting(&rect1, highlighted1, items1.at(index).rect,
OVERLAP, DPI, COMBINE);
if (!rect1.isNull() && !rangesPair.first.isEmpty())
highlighted1->addRect(rect1);
foreach (int index, rangesPair.second)
addHighlighting(&rect2, highlighted2, items2.at(index).rect,
OVERLAP, DPI, COMBINE);
if (!rect2.isNull() && !rangesPair.second.isEmpty())
highlighted2->addRect(rect2);
}