forked from scantailor/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FixDpiDialog.cpp
888 lines (738 loc) · 23 KB
/
FixDpiDialog.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
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) Joseph Artsimovich <[email protected]>
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 3 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FixDpiDialog.h"
#include "FixDpiDialog.h.moc"
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QModelIndex>
#include <QItemSelection>
#include <QHeaderView>
#include <QVariant>
#include <QIntValidator>
#include <QSize>
#include <QString>
#include <QColor>
#include <Qt>
#include <QDebug>
#ifndef Q_MOC_RUN
#include <boost/foreach.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#endif
#include <vector>
#include <algorithm>
#include <assert.h>
// To be able to use it in QVariant
Q_DECLARE_METATYPE(ImageMetadata)
static int const NEED_FIXING_TAB = 0;
static int const ALL_PAGES_TAB = 1;
// Requests a group of ImageMetadata objects folded into one.
static int const AGGREGATE_METADATA_ROLE = Qt::UserRole;
// Same as the one above, but only objects with .isDpiOK() == false
// will be considered.
static int const AGGREGATE_NOT_OK_METADATA_ROLE = Qt::UserRole + 1;
/**
* This class computes an aggregate ImageMetadata object from a group of other
* ImageMetadata objects. If all ImageMetadata objects in a group are equal,
* that will make it an aggregate metadata. Otherwise, a null (default
* constructed) ImageMetadata() object will be considered
* the DPIs within the group are not consistent,
* the aggregate Image metadata object will have zeros both for size and for
* DPI values. If the DPIs are consistent but sizes are not, the aggregate
* ImageMetadata will have the consistent DPI and zero size.
*/
class FixDpiDialog::DpiCounts
{
public:
void add(ImageMetadata const& metadata);
void remove(ImageMetadata const& metadata);
/**
* Checks if all ImageMetadata objects return true for ImageMetadata::isDpiOK().
*/
bool allDpisOK() const;
/**
* If all ImageMetadata objects are equal, one of them will be returned.
* Otherwise, a default-constructed ImageMetadata() object will be returned.
*/
ImageMetadata aggregate(Scope scope) const;
private:
struct MetadataComparator
{
bool operator()(ImageMetadata const& lhs, ImageMetadata const& rhs) const;
};
typedef std::map<ImageMetadata, int, MetadataComparator> Map;
Map m_counts;
};
/**
* This comparator puts objects that are not OK to the front.
*/
bool FixDpiDialog::DpiCounts::MetadataComparator::operator()(
ImageMetadata const& lhs, ImageMetadata const& rhs) const
{
bool const lhs_ok = lhs.isDpiOK();
bool const rhs_ok = rhs.isDpiOK();
if (lhs_ok != rhs_ok) {
return rhs_ok;
}
if (lhs.size().width() < rhs.size().width()) {
return true;
} else if (lhs.size().width() > rhs.size().width()) {
return false;
} else if (lhs.size().height() < rhs.size().height()) {
return true;
} else if (lhs.size().height() > rhs.size().height()) {
return false;
} else if (lhs.dpi().horizontal() < rhs.dpi().horizontal()) {
return true;
} else if (lhs.dpi().horizontal() > rhs.dpi().horizontal()) {
return false;
} else {
return lhs.dpi().vertical() < rhs.dpi().vertical();
}
}
class FixDpiDialog::SizeGroup
{
public:
struct Item
{
int fileIdx;
int imageIdx;
Item(int file_idx, int image_idx)
: fileIdx(file_idx), imageIdx(image_idx) {}
};
SizeGroup(QSize const& size) : m_size(size) {}
void append(Item const& item, ImageMetadata const& metadata);
QSize const& size() const { return m_size; }
std::vector<Item> const& items() const { return m_items; }
DpiCounts& dpiCounts() { return m_dpiCounts; }
DpiCounts const& dpiCounts() const { return m_dpiCounts; }
private:
QSize m_size;
std::vector<Item> m_items;
DpiCounts m_dpiCounts;
};
class FixDpiDialog::TreeModel : private QAbstractItemModel
{
public:
TreeModel(std::vector<ImageFileInfo> const& files);
std::vector<ImageFileInfo> const& files() const { return m_files; }
QAbstractItemModel* model() { return this; }
bool allDpisOK() const {
return m_dpiCounts.allDpisOK();
}
bool isVisibleForFilter(QModelIndex const& parent, int row) const;
void applyDpiToSelection(
Scope scope, Dpi const& dpi, QItemSelection const& selection);
private:
struct Tag {};
virtual int columnCount(QModelIndex const& parent) const;
virtual int rowCount(QModelIndex const& parent) const;
virtual QModelIndex index(int row, int column, QModelIndex const& parent) const;
virtual QModelIndex parent(QModelIndex const& index) const;
virtual QVariant data(QModelIndex const& index, int role) const;
void applyDpiToAllGroups(Scope scope, Dpi const& dpi);
void applyDpiToGroup(Scope scope, Dpi const& dpi, SizeGroup& group, DpiCounts& total_dpi_counts);
void applyDpiToItem(
Scope scope, ImageMetadata const& new_metadata, SizeGroup::Item item,
DpiCounts& total_dpi_counts, DpiCounts& group_dpi_counts);
void emitAllPagesChanged(QModelIndex const& idx);
void emitSizeGroupChanged(QModelIndex const& idx);
void emitItemChanged(QModelIndex const& idx);
SizeGroup& sizeGroupFor(QSize size);
static QString sizeToString(QSize size);
static Tag m_allPagesNodeId;
static Tag m_sizeGroupNodeId;
std::vector<ImageFileInfo> m_files;
std::vector<SizeGroup> m_sizes;
DpiCounts m_dpiCounts;
};
class FixDpiDialog::FilterModel : private QSortFilterProxyModel
{
public:
FilterModel(TreeModel& delegate);
QAbstractProxyModel* model() { return this; }
private:
virtual bool filterAcceptsRow(
int source_row, QModelIndex const& source_parent) const;
virtual QVariant data(QModelIndex const& index, int role) const;
TreeModel& m_rDelegate;
};
FixDpiDialog::FixDpiDialog(std::vector<ImageFileInfo> const& files, QWidget* parent)
: QDialog(parent),
m_ptrPages(new TreeModel(files)),
m_ptrUndefinedDpiPages(new FilterModel(*m_ptrPages))
{
setupUi(this);
m_normalPalette = xDpi->palette();
m_errorPalette = m_normalPalette;
m_errorPalette.setColor(QPalette::Text, Qt::red);
dpiCombo->addItem("300 x 300", QSize(300, 300));
dpiCombo->addItem("400 x 400", QSize(400, 400));
dpiCombo->addItem("600 x 600", QSize(600, 600));
tabWidget->setTabText(NEED_FIXING_TAB, tr("Need Fixing"));
tabWidget->setTabText(ALL_PAGES_TAB, tr("All Pages"));
undefinedDpiView->setModel(m_ptrUndefinedDpiPages->model()),
undefinedDpiView->header()->hide();
allPagesView->setModel(m_ptrPages->model());
allPagesView->header()->hide();
xDpi->setMaxLength(4);
yDpi->setMaxLength(4);
xDpi->setValidator(new QIntValidator(xDpi));
yDpi->setValidator(new QIntValidator(yDpi));
connect(
tabWidget, SIGNAL(currentChanged(int)),
this, SLOT(tabChanged(int))
);
connect(
undefinedDpiView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection const&, QItemSelection const&)),
this, SLOT(selectionChanged(QItemSelection const&))
);
connect(
allPagesView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection const&, QItemSelection const&)),
this, SLOT(selectionChanged(QItemSelection const&))
);
connect(
dpiCombo, SIGNAL(activated(int)),
this, SLOT(dpiComboChangedByUser(int))
);
connect(
xDpi, SIGNAL(textEdited(QString const&)),
this, SLOT(dpiValueChanged())
);
connect(
yDpi, SIGNAL(textEdited(QString const&)),
this, SLOT(dpiValueChanged())
);
connect(applyBtn, SIGNAL(clicked()), this, SLOT(applyClicked()));
enableDisableOkButton();
}
FixDpiDialog::~FixDpiDialog()
{
}
std::vector<ImageFileInfo> const&
FixDpiDialog::files() const
{
return m_ptrPages->files();
}
void
FixDpiDialog::tabChanged(int const tab)
{
QTreeView* views[2];
views[NEED_FIXING_TAB] = undefinedDpiView;
views[ALL_PAGES_TAB] = allPagesView;
updateDpiFromSelection(views[tab]->selectionModel()->selection());
}
void
FixDpiDialog::selectionChanged(QItemSelection const& selection)
{
updateDpiFromSelection(selection);
}
void
FixDpiDialog::dpiComboChangedByUser(int const index)
{
QVariant const data(dpiCombo->itemData(index));
if (data.isValid()) {
QSize const dpi(data.toSize());
xDpi->setText(QString::number(dpi.width()));
yDpi->setText(QString::number(dpi.height()));
dpiValueChanged();
}
}
void
FixDpiDialog::dpiValueChanged()
{
updateDpiCombo();
Dpi const dpi(xDpi->text().toInt(), yDpi->text().toInt());
ImageMetadata const metadata(m_selectedItemPixelSize, dpi);
decorateDpiInputField(xDpi, metadata.horizontalDpiStatus());
decorateDpiInputField(yDpi, metadata.verticalDpiStatus());
if (m_xDpiInitialValue == xDpi->text() &&
m_yDpiInitialValue == yDpi->text()) {
applyBtn->setEnabled(false);
return;
}
if (metadata.isDpiOK()) {
applyBtn->setEnabled(true);
return;
}
applyBtn->setEnabled(false);
}
void
FixDpiDialog::applyClicked()
{
Dpi const dpi(xDpi->text().toInt(), yDpi->text().toInt());
QItemSelectionModel* selection_model = 0;
if (tabWidget->currentIndex() == ALL_PAGES_TAB) {
selection_model = allPagesView->selectionModel();
QItemSelection const selection(selection_model->selection());
m_ptrPages->applyDpiToSelection(ALL, dpi, selection);
} else {
selection_model = undefinedDpiView->selectionModel();
QItemSelection const selection(
m_ptrUndefinedDpiPages->model()->mapSelectionToSource(
selection_model->selection()
)
);
m_ptrPages->applyDpiToSelection(NOT_OK, dpi, selection);
}
updateDpiFromSelection(selection_model->selection());
enableDisableOkButton();
}
void
FixDpiDialog::enableDisableOkButton()
{
bool const enable = m_ptrPages->allDpisOK();
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
}
/**
* This function work with both TreeModel and FilterModel selections.
* It is assumed that only a single item is selected.
*/
void
FixDpiDialog::updateDpiFromSelection(QItemSelection const& selection)
{
if (selection.isEmpty()) {
resetDpiForm();
dpiCombo->setEnabled(false);
xDpi->setEnabled(false);
yDpi->setEnabled(false);
// applyBtn is managed elsewhere.
return;
}
dpiCombo->setEnabled(true);
xDpi->setEnabled(true);
yDpi->setEnabled(true);
// FilterModel may replace AGGREGATE_METADATA_ROLE with AGGREGATE_NOT_OK_METADATA_ROLE.
QVariant const data(selection.front().topLeft().data(AGGREGATE_METADATA_ROLE));
if (data.isValid()) {
setDpiForm(data.value<ImageMetadata>());
} else {
resetDpiForm();
}
}
void
FixDpiDialog::resetDpiForm()
{
dpiCombo->setCurrentIndex(0);
m_xDpiInitialValue.clear();
m_yDpiInitialValue.clear();
xDpi->setText(m_xDpiInitialValue);
yDpi->setText(m_yDpiInitialValue);
dpiValueChanged();
}
void
FixDpiDialog::setDpiForm(ImageMetadata const& metadata)
{
Dpi const dpi(metadata.dpi());
if (dpi.isNull()) {
resetDpiForm();
return;
}
m_xDpiInitialValue = QString::number(dpi.horizontal());
m_yDpiInitialValue = QString::number(dpi.vertical());
m_selectedItemPixelSize = metadata.size();
xDpi->setText(m_xDpiInitialValue);
yDpi->setText(m_yDpiInitialValue);
dpiValueChanged();
}
void
FixDpiDialog::updateDpiCombo()
{
bool x_ok = true, y_ok = true;
QSize const dpi(xDpi->text().toInt(&x_ok), yDpi->text().toInt(&y_ok));
if (x_ok && y_ok) {
int const count = dpiCombo->count();
for (int i = 0; i < count; ++i) {
QVariant const data(dpiCombo->itemData(i));
if (data.isValid()) {
if (dpi == data.toSize()) {
dpiCombo->setCurrentIndex(i);
return;
}
}
}
}
dpiCombo->setCurrentIndex(0);
}
void
FixDpiDialog::decorateDpiInputField(QLineEdit* field, ImageMetadata::DpiStatus dpi_status) const
{
if (dpi_status == ImageMetadata::DPI_OK) {
field->setPalette(m_normalPalette);
} else {
field->setPalette(m_errorPalette);
}
switch (dpi_status) {
case ImageMetadata::DPI_OK:
case ImageMetadata::DPI_UNDEFINED:
field->setToolTip(QString());
break;
case ImageMetadata::DPI_TOO_LARGE:
field->setToolTip(tr("DPI is too large and most likely wrong."));
break;
case ImageMetadata::DPI_TOO_SMALL:
field->setToolTip(tr("DPI is too small. Even if it's correct, you are not going to get acceptable results with it."));
break;
case ImageMetadata::DPI_TOO_SMALL_FOR_THIS_PIXEL_SIZE:
field->setToolTip(tr("DPI is too small for this pixel size. Such combination would probably lead to out of memory errors."));
break;
}
}
/*====================== FixDpiDialog::DpiCounts ======================*/
void
FixDpiDialog::DpiCounts::add(ImageMetadata const& metadata)
{
++m_counts[metadata];
}
void
FixDpiDialog::DpiCounts::remove(ImageMetadata const& metadata)
{
if (--m_counts[metadata] == 0) {
m_counts.erase(metadata);
}
}
bool
FixDpiDialog::DpiCounts::allDpisOK() const
{
// We put wrong DPIs to the front, so if the first one is OK,
// the others are OK as well.
Map::const_iterator const it(m_counts.begin());
return (it == m_counts.end() || it->first.isDpiOK());
}
ImageMetadata
FixDpiDialog::DpiCounts::aggregate(Scope const scope) const
{
Map::const_iterator const it(m_counts.begin());
if (it == m_counts.end()) {
return ImageMetadata();
}
if (scope == NOT_OK && it->first.isDpiOK()) {
// If this one is OK, the following ones are OK as well.
return ImageMetadata();
}
Map::const_iterator next(it);
++next;
if (next == m_counts.end()) {
return it->first;
}
if (scope == NOT_OK && next->first.isDpiOK()) {
// If this one is OK, the following ones are OK as well.
return it->first;
}
return ImageMetadata();
}
/*====================== FixDpiDialog::SizeGroup ======================*/
void
FixDpiDialog::SizeGroup::append(Item const& item, ImageMetadata const& metadata)
{
m_items.push_back(item);
m_dpiCounts.add(metadata);
}
/*====================== FixDpiDialog::TreeModel ======================*/
FixDpiDialog::TreeModel::Tag FixDpiDialog::TreeModel::m_allPagesNodeId;
FixDpiDialog::TreeModel::Tag FixDpiDialog::TreeModel::m_sizeGroupNodeId;
FixDpiDialog::TreeModel::TreeModel(std::vector<ImageFileInfo> const& files)
: m_files(files)
{
int const num_files = m_files.size();
for (int i = 0; i < num_files; ++i) {
ImageFileInfo const& file = m_files[i];
int const num_images = file.imageInfo().size();
for (int j = 0; j < num_images; ++j) {
ImageMetadata const& metadata = file.imageInfo()[j];
SizeGroup& group = sizeGroupFor(metadata.size());
group.append(SizeGroup::Item(i, j), metadata);
m_dpiCounts.add(metadata);
}
}
}
bool
FixDpiDialog::TreeModel::isVisibleForFilter(QModelIndex const& parent, int row) const
{
void const* const ptr = parent.internalPointer();
if (!parent.isValid()) {
// 'All Pages'.
return !m_dpiCounts.allDpisOK();
} else if (ptr == &m_allPagesNodeId) {
// A size group.
return !m_sizes[row].dpiCounts().allDpisOK();
} else if (ptr == &m_sizeGroupNodeId) {
// An image.
SizeGroup const& group = m_sizes[parent.row()];
SizeGroup::Item const& item = group.items()[row];
ImageFileInfo const& file = m_files[item.fileIdx];
return !file.imageInfo()[item.imageIdx].isDpiOK();
} else {
// Should not happen.
return false;
}
}
void
FixDpiDialog::TreeModel::applyDpiToSelection(
Scope const scope, Dpi const& dpi, QItemSelection const& selection)
{
if (selection.isEmpty()) {
return;
}
QModelIndex const parent(selection.front().parent());
int const row = selection.front().top();
void const* const ptr = parent.internalPointer();
QModelIndex const idx(index(row, 0, parent));
if (!parent.isValid()) {
// Apply to all pages.
applyDpiToAllGroups(scope, dpi);
emitAllPagesChanged(idx);
} else if (ptr == &m_allPagesNodeId) {
// Apply to a size group.
SizeGroup& group = m_sizes[row];
applyDpiToGroup(scope, dpi, group, m_dpiCounts);
emitSizeGroupChanged(index(row, 0, parent));
} else if (ptr == &m_sizeGroupNodeId) {
// Images within a size group.
SizeGroup& group = m_sizes[parent.row()];
SizeGroup::Item const& item = group.items()[row];
ImageMetadata const metadata(group.size(), dpi);
applyDpiToItem(scope, metadata, item, m_dpiCounts, group.dpiCounts());
emitItemChanged(idx);
}
}
int
FixDpiDialog::TreeModel::columnCount(QModelIndex const& parent) const
{
return 1;
}
int
FixDpiDialog::TreeModel::rowCount(QModelIndex const& parent) const
{
void const* const ptr = parent.internalPointer();
if (!parent.isValid()) {
// The single 'All Pages' item.
return 1;
} else if (ptr == &m_allPagesNodeId) {
// Size groups.
return m_sizes.size();
} else if (ptr == &m_sizeGroupNodeId) {
// Images within a size group.
return m_sizes[parent.row()].items().size();
} else {
// Children of an image.
return 0;
}
}
QModelIndex
FixDpiDialog::TreeModel::index(int const row, int const column, QModelIndex const& parent) const
{
void const* const ptr = parent.internalPointer();
if (!parent.isValid()) {
// The 'All Pages' item.
return createIndex(row, column, &m_allPagesNodeId);
} else if (ptr == &m_allPagesNodeId) {
// A size group.
return createIndex(row, column, &m_sizeGroupNodeId);
} else if (ptr == &m_sizeGroupNodeId) {
// An image within some size group.
return createIndex(row, column, (void*)&m_sizes[parent.row()]);
}
return QModelIndex();
}
QModelIndex
FixDpiDialog::TreeModel::parent(QModelIndex const& index) const
{
void const* const ptr = index.internalPointer();
if (!index.isValid()) {
// Should not happen.
return QModelIndex();
} else if (ptr == &m_allPagesNodeId) {
// 'All Pages' -> tree root.
return QModelIndex();
} else if (ptr == &m_sizeGroupNodeId) {
// Size group -> 'All Pages'.
return createIndex(0, index.column(), &m_allPagesNodeId);
} else {
// Image -> size group.
SizeGroup const* group = static_cast<SizeGroup const*>(ptr);
return createIndex(group - &m_sizes[0], index.column(), &m_sizeGroupNodeId);
}
}
QVariant
FixDpiDialog::TreeModel::data(QModelIndex const& index, int const role) const
{
void const* const ptr = index.internalPointer();
if (!index.isValid()) {
// Should not happen.
return QVariant();
} else if (ptr == &m_allPagesNodeId) {
// 'All Pages'.
if (role == Qt::DisplayRole) {
return FixDpiDialog::tr("All Pages");
} else if (role == AGGREGATE_METADATA_ROLE) {
return QVariant::fromValue(m_dpiCounts.aggregate(ALL));
} else if (role == AGGREGATE_NOT_OK_METADATA_ROLE) {
return QVariant::fromValue(m_dpiCounts.aggregate(NOT_OK));
}
} else if (ptr == &m_sizeGroupNodeId) {
// Size group.
SizeGroup const& group = m_sizes[index.row()];
if (role == Qt::DisplayRole) {
return sizeToString(group.size());
} else if (role == AGGREGATE_METADATA_ROLE) {
return QVariant::fromValue(group.dpiCounts().aggregate(ALL));
} else if (role == AGGREGATE_NOT_OK_METADATA_ROLE) {
return QVariant::fromValue(group.dpiCounts().aggregate(NOT_OK));
}
} else {
// Image.
SizeGroup const* group = static_cast<SizeGroup const*>(ptr);
SizeGroup::Item const& item = group->items()[index.row()];
ImageFileInfo const& file = m_files[item.fileIdx];
if (role == Qt::DisplayRole) {
QString const& fname = file.fileInfo().fileName();
if (file.imageInfo().size() == 1) {
return fname;
} else {
return FixDpiDialog::tr(
"%1 (page %2)"
).arg(fname).arg(item.imageIdx + 1);
}
} else if (role == AGGREGATE_METADATA_ROLE || role == AGGREGATE_NOT_OK_METADATA_ROLE) {
return QVariant::fromValue(file.imageInfo()[item.imageIdx]);
}
}
return QVariant();
}
void
FixDpiDialog::TreeModel::applyDpiToAllGroups(Scope const scope, Dpi const& dpi)
{
int const num_groups = m_sizes.size();
for (int i = 0; i < num_groups; ++i) {
applyDpiToGroup(scope, dpi, m_sizes[i], m_dpiCounts);
}
}
void
FixDpiDialog::TreeModel::applyDpiToGroup(
Scope const scope, Dpi const& dpi,
SizeGroup& group, DpiCounts& total_dpi_counts)
{
DpiCounts& group_dpi_counts = group.dpiCounts();
ImageMetadata const metadata(group.size(), dpi);
std::vector<SizeGroup::Item> const& items = group.items();
int const num_items = items.size();
for (int i = 0; i < num_items; ++i) {
applyDpiToItem(
scope, metadata, items[i],
total_dpi_counts, group_dpi_counts
);
}
}
void
FixDpiDialog::TreeModel::applyDpiToItem(
Scope const scope, ImageMetadata const& new_metadata, SizeGroup::Item const item,
DpiCounts& total_dpi_counts, DpiCounts& group_dpi_counts)
{
ImageFileInfo& file = m_files[item.fileIdx];
ImageMetadata& old_metadata = file.imageInfo()[item.imageIdx];
if (scope == NOT_OK && old_metadata.isDpiOK()) {
return;
}
total_dpi_counts.add(new_metadata);
group_dpi_counts.add(new_metadata);
total_dpi_counts.remove(old_metadata);
group_dpi_counts.remove(old_metadata);
old_metadata = new_metadata;
}
void
FixDpiDialog::TreeModel::emitAllPagesChanged(QModelIndex const& idx)
{
int const num_groups = m_sizes.size();
for (int i = 0; i < num_groups; ++i) {
QModelIndex const group_node(index(i, 0, idx));
int const num_items = rowCount(group_node);
for (int j = 0; j < num_items; ++j) {
QModelIndex const image_node(index(j, 0, group_node));
emit dataChanged(image_node, image_node);
}
emit dataChanged(group_node, group_node);
}
// The 'All Pages' node.
emit dataChanged(idx, idx);
}
void
FixDpiDialog::TreeModel::emitSizeGroupChanged(QModelIndex const& idx)
{
// Every item in this size group.
emit dataChanged(index(0, 0, idx), index(rowCount(idx), 0, idx));
// The size group itself.
emit dataChanged(idx, idx);
// The 'All Pages' node.
QModelIndex const all_pages_node(idx.parent());
emit dataChanged(all_pages_node, all_pages_node);
}
void
FixDpiDialog::TreeModel::emitItemChanged(QModelIndex const& idx)
{
// The item itself.
emit dataChanged(idx, idx);
// The size group node.
QModelIndex const group_node(idx.parent());
emit dataChanged(group_node, group_node);
// The 'All Pages' node.
QModelIndex const all_pages_node(group_node.parent());
emit dataChanged(all_pages_node, all_pages_node);
}
FixDpiDialog::SizeGroup&
FixDpiDialog::TreeModel::sizeGroupFor(QSize const size)
{
using namespace boost::lambda;
std::vector<SizeGroup>::iterator const it(
std::find_if(
m_sizes.begin(), m_sizes.end(),
bind(&SizeGroup::size, _1) == size
)
);
if (it != m_sizes.end()) {
return *it;
} else {
m_sizes.push_back(SizeGroup(size));
return m_sizes.back();
}
}
QString
FixDpiDialog::TreeModel::sizeToString(QSize const size)
{
return QString("%1 x %2 px").arg(size.width()).arg(size.height());
}
/*====================== FixDpiDialog::FilterModel ======================*/
FixDpiDialog::FilterModel::FilterModel(TreeModel& delegate)
: m_rDelegate(delegate)
{
setDynamicSortFilter(true);
setSourceModel(delegate.model());
}
bool
FixDpiDialog::FilterModel::filterAcceptsRow(
int const source_row, QModelIndex const& source_parent) const
{
return m_rDelegate.isVisibleForFilter(source_parent, source_row);
}
QVariant
FixDpiDialog::FilterModel::data(QModelIndex const& index, int role) const
{
if (role == AGGREGATE_METADATA_ROLE) {
role = AGGREGATE_NOT_OK_METADATA_ROLE;
}
return QSortFilterProxyModel::data(index, role);
}