-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmainwindow.cpp
1709 lines (1503 loc) · 59.9 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "./optionsdialog.h"
#include "./createprogress.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QInputDialog>
#include "settings.h"
#include "util.h"
#include "progressdialog.h"
#include "par2calc.h"
#include "sourcefilelistitem.h"
#include "outpreviewlistitem.h"
#include "clientinfo.h"
#include <math.h>
#include <QSet>
#include <QFile>
#include <QDir>
#include <QClipboard>
#include <QTemporaryFile>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, dlgOptions(this)
, outPreview(this)
{
ui->setupUi(this);
ui->tvDest->hide();
ui->txtInsliceCount->setMainWindow(this);
const auto& settings = Settings::getInstance();
ui->stkSource->setCurrentIndex(settings.uiExpSource() ? 1 : 0);
ui->btnDestPreview->setChecked(settings.uiExpDest());
// handle things which change in Designer
ui->stkDestSizing->setCurrentIndex(0);
this->resize(600, 500); // set an initial size to expand on
ui->tvSource->sortByColumn(0, Qt::SortOrder::AscendingOrder);
ui->tvDest->sortByColumn(0, Qt::SortOrder::AscendingOrder);
ui->txtInsliceSize->blockSignals(true);
ui->txtInsliceSize->setText(settings.allocSliceSize());
ui->txtInsliceSize->blockSignals(false);
ui->txtInsliceCount->blockSignals(true);
ui->txtInsliceCount->setValue(settings.allocSliceCount());
ui->txtInsliceCount->blockSignals(false);
switch(settings.allocSliceMode()) {
case SettingsDefaultAllocIn::ALLOC_IN_SIZE:
ui->optInsliceSize->blockSignals(true);
ui->optInsliceSize->setChecked(true);
ui->optInsliceSize->blockSignals(false);
break;
case SettingsDefaultAllocIn::ALLOC_IN_COUNT:
ui->optInsliceCount->blockSignals(true);
ui->optInsliceCount->setChecked(true);
ui->optInsliceCount->blockSignals(false);
break;
case SettingsDefaultAllocIn::ALLOC_IN_RATIO:
// handle later
break;
}
ui->txtOutsliceRatio->blockSignals(true);
ui->txtOutsliceRatio->setValue(settings.allocRecoveryRatio());
ui->txtOutsliceRatio->blockSignals(false);
ui->txtOutsliceCount->blockSignals(true);
ui->txtOutsliceCount->setValue(settings.allocRecoveryCount());
ui->txtOutsliceCount->blockSignals(false);
ui->txtOutsliceSize->blockSignals(true);
ui->txtOutsliceSize->setText(settings.allocRecoverySize());
ui->txtOutsliceSize->blockSignals(false);
switch(settings.allocRecoveryMode()) {
case SettingsDefaultAllocRec::ALLOC_REC_RATIO:
ui->optOutsliceRatio->blockSignals(true);
ui->optOutsliceRatio->setChecked(true);
ui->optOutsliceRatio->blockSignals(false);
break;
case SettingsDefaultAllocRec::ALLOC_REC_COUNT:
ui->optOutsliceCount->blockSignals(true);
ui->optOutsliceCount->setChecked(true);
ui->optOutsliceCount->blockSignals(false);
break;
case SettingsDefaultAllocRec::ALLOC_REC_SIZE:
ui->optOutsliceSize->blockSignals(true);
ui->optOutsliceSize->setChecked(true);
ui->optOutsliceSize->blockSignals(false);
break;
}
par2SrcSize = 0;
par2FileCount = 0;
srcBaseChosen = false;
destFileChosen = false;
auto& clientInfo = ClientInfo::getInstance();
connect(&clientInfo, &ClientInfo::failed, this, [=](const QString& error) {
QMessageBox::warning(this, tr("ParPar Execute Failed"), tr("Failed to retrieve information from ParPar client. Please ensure that ParPar is available, executable and/or configured in the Options dialog.\n\nDetail: %1").arg(error));
});
connect(&clientInfo, &ClientInfo::updated, this, [=]() {
// mostly because the creator could've changed
this->updateDestPreview();
});
this->optionSliceMultiple = 4;
this->optionSliceLimit = settings.sliceLimit();
auto updateMultiple = [=](bool binaryChanged) {
const auto& settings = Settings::getInstance();
quint64 newMultiple = sizeToBytes(settings.sliceMultiple());
if(!newMultiple) newMultiple = 4;
else if(newMultiple & 3) // invalid multiple - can't be used
newMultiple = 4;
if(this->optionSliceLimit != settings.sliceLimit()) {
this->optionSliceLimit = settings.sliceLimit();
this->optionSliceMultiple = newMultiple;
this->updateInsliceInfo();
this->checkSourceFileCount(tr("Change slice limit"));
}
else if(this->optionSliceMultiple != newMultiple) {
this->optionSliceMultiple = newMultiple;
this->updateInsliceInfo();
}
if(binaryChanged) { // TODO: maybe update regardless of change (e.g. if user renamed files correct)
// dest preview will be updated by the following, so don't need to double update
ClientInfo::getInstance().refresh();
} else {
this->updateDestPreview(); // a number of options affect this, so always regen
}
};
connect(&dlgOptions, &OptionsDialog::settingsUpdated, this, updateMultiple);
updateMultiple(true);
// handle application arguments
const auto& argv = QCoreApplication::arguments();
QStringList loadFiles;
for(int i=1; i<argv.size(); i++) {
#ifdef Q_OS_WINDOWS
QChar prefix('/');
#else
QChar prefix('-');
#endif
if(argv.at(i).startsWith(prefix)) {
QString option = argv.at(i).mid(1);
// do something with option
} else {
loadFiles.append(argv.at(i));
}
}
if(!loadFiles.isEmpty()) {
sourceAddFiles(loadFiles);
autoSelectDestFile();
checkSourceFileCount();
updateSrcFilesState();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::rescale() {
int w = ui->tvSource->width();
ui->tvSource->header()->setUpdatesEnabled(false);
ui->tvSource->header()->resizeSection(0, w>400 ? w-250 : 150);
ui->tvSource->header()->resizeSection(1, 150);
ui->tvSource->header()->resizeSection(2, 80);
ui->tvSource->header()->setUpdatesEnabled(true);
w = ui->tvDest->width();
ui->tvDest->header()->setUpdatesEnabled(false);
ui->tvDest->header()->resizeSection(0, w>320 ? w-240 : 80);
ui->tvDest->header()->resizeSection(1, 80);
ui->tvDest->header()->resizeSection(2, 70);
ui->tvDest->header()->resizeSection(3, 70);
ui->tvDest->header()->setUpdatesEnabled(true);
}
void MainWindow::adjustExpansion(bool allowExpand) {
bool inExp = ui->stkSource->currentIndex() == 1;
bool outExp = ui->btnDestPreview->isChecked();
ui->splitter->setUpdatesEnabled(false);
for(int i=0; i<ui->splitter->count(); i++)
ui->splitter->handle(i)->setEnabled(inExp && outExp);
ui->splitter->setUpdatesEnabled(true);
ui->tvDest->setVisible(outExp);
ui->btnDestPreview->setArrowType(outExp ? Qt::UpArrow : Qt::DownArrow);
ui->stkSource->setUpdatesEnabled(false);
if(inExp) {
ui->stkSource->setMinimumHeight(ui->stkSourceAdv->minimumHeight());
ui->stkSource->setMaximumHeight(ui->stkSourceAdv->maximumHeight());
} else {
ui->stkSource->setFixedHeight(ui->stkSourceBasic->layout()->sizeHint().height());
}
ui->stkSource->setUpdatesEnabled(true);
ui->stkDestSizing->setFixedHeight(ui->stkDestSizing->currentWidget()->layout()->sizeHint().height());
ui->stkDestSizing->updateGeometry();
auto destOptsMargin = ui->fraDestOpts->layout()->contentsMargins();
destOptsMargin.setBottom(outExp ? destOptsMargin.top() : 0);
ui->fraDestOpts->layout()->setContentsMargins(destOptsMargin);
auto destPolicy = ui->grpDest->sizePolicy();
destPolicy.setVerticalPolicy(outExp ? QSizePolicy::Expanding : QSizePolicy::Maximum);
ui->grpDest->setUpdatesEnabled(false);
ui->grpDest->setSizePolicy(destPolicy);
ui->grpDest->adjustSize();
ui->grpDest->setUpdatesEnabled(true);
this->setUpdatesEnabled(false);
if(inExp || outExp) {
this->setMaximumHeight(16777215);
ui->scrollArea->setMaximumHeight(16777215);
if(allowExpand) {
// TODO: detect screen height and restrict resize?
/*
// the default QTreeWidget size is a bit too high - try restricting it and let the layout compute the appropriate size
ui->tvSource->setFixedHeight(100);
int scrollDiff = ui->scrollAreaContents->layout()->sizeHint().height() - ui->scrollAreaContents->height();
if(scrollDiff > 0) {
// try to eliminate scrollbar
this->resize(this->width(), this->height() + scrollDiff);
}
ui->tvSource->setMaximumHeight(INT_MAX);
*/
int wantedMinHeight = this->layout()->sizeHint().height();
if(inExp) wantedMinHeight += 150;
if(outExp) wantedMinHeight += ui->tvDest->minimumHeight();
if(this->height() < wantedMinHeight)
this->resize(this->width(), wantedMinHeight);
}
} else {
// for some reason, the layout engine overscales by default, so manually size the major containers
auto scrollMargins = ui->scrollAreaContents->layout()->contentsMargins();
int scrollHeight = ui->stkSourceBasic->layout()->sizeHint().height()
+ ui->fraSlices->sizeHint().height()
+ ui->grpDest->sizeHint().height()
+ scrollMargins.bottom() + scrollMargins.top()
+ ui->scrollAreaContents->layout()->spacing()*2;
//ui->scrollArea->setFixedHeight(scrollHeight);
this->setFixedHeight(scrollHeight + ui->lytBottomButtons->sizeHint().height());
}
this->setMinimumWidth(575); // for some reason, this can get lost?
this->setUpdatesEnabled(true);
}
void MainWindow::showEvent(QShowEvent *event) {
QMainWindow::showEvent(event);
on_cboDestDist_currentIndexChanged(ui->cboDestDist->currentIndex());
adjustExpansion(true); // TODO: above calls adjustExpansion - need to avoid a double-call
rescale();
/*
// check ParPar executable
bool parparMissing;
auto parparBin = Settings::getInstance().parparBin(&parparMissing);
if(!parparMissing) {
for(const auto& part : parparBin) {
if(!QFile::exists(part)) {
parparMissing = true;
break;
}
}
}
if(parparMissing) {
dlgOptions.open();
QMessageBox::warning(&dlgOptions, tr("ParPar Executable"), tr("The ParPar executable was not found. Please configure it in this Options dialog."));
dlgOptions.focusWidget(); // TODO: this still doesn't seem to prioritize focus onto the dialog
} else {
QFileInfo binInfo(parparBin[0]);
if(binInfo.isReadable() && !binInfo.isExecutable()) {
// possibly common problem on Unixes where the executable bit isn't set
if(QMessageBox::Yes == QMessageBox::warning(this, tr("ParPar Executable"), tr("The Node/ParPar executable is not executable. Do you want to try and enable it?"), QMessageBox::Yes | QMessageBox::No)) {
QFile exe(parparBin[0]);
auto ans = QMessageBox::Retry;
while(ans == QMessageBox::Retry) {
if(exe.setPermissions(exe.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup | QFileDevice::ExeOther))
break;
ans = QMessageBox::critical(this, tr("ParPar Executable"), tr("Failed to set executable permissions to the Node/ParPar executable."), QMessageBox::Retry | QMessageBox::Ignore);
}
}
}
}
*/
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
rescale();
}
void MainWindow::on_btnAbout_clicked()
{
QMessageBox::information(this, tr("About ParPar GUI"), QString("ParPar GUI v%1\nParPar v%2")
.arg(QCoreApplication::applicationVersion(),
ClientInfo::version())
);
}
void MainWindow::on_btnOptions_clicked()
{
dlgOptions.open();
}
void MainWindow::on_txtInsliceCount_valueChanged(int value)
{
ui->optInsliceCount->blockSignals(true);
ui->optInsliceCount->setChecked(true);
ui->optInsliceCount->blockSignals(false);
par2SliceSize = Par2Calc::sliceSizeFromCount(value, optionSliceMultiple, optionSliceLimit, par2SrcFiles, par2FileCount);
ui->txtInsliceSize->setBytesApprox(par2SliceSize, true);
updateOutsliceInfo(false);
}
void MainWindow::on_txtInsliceCount_editingFinished()
{
if(!ui->optInsliceCount->isChecked()) return;
int newValue = ui->txtInsliceCount->value();
par2SliceSize = Par2Calc::sliceSizeFromCount(newValue, optionSliceMultiple, optionSliceLimit, par2SrcFiles, par2FileCount);
if(newValue != ui->txtInsliceCount->value()) {
ui->txtInsliceCount->blockSignals(true);
ui->txtInsliceCount->setValue(newValue);
ui->txtInsliceCount->blockSignals(false);
}
ui->txtInsliceSize->setBytesApprox(par2SliceSize, true);
updateOutsliceInfo();
}
void MainWindow::on_btnSourceAdd_clicked()
{
auto files = QFileDialog::getOpenFileNames(this, tr("Add source files"), ui->txtSourcePath->text(), tr("All files (*.*)"));
if(!files.isEmpty()) {
// TODO: select items that were just added
sourceAddFiles(files);
if(ui->txtDestFile->text().isEmpty())
autoSelectDestFile();
checkSourceFileCount();
updateSrcFilesState();
}
}
void MainWindow::on_btnSourceAddDir_clicked()
{
auto dir = QFileDialog::getExistingDirectory(this, tr("Add source files from directory"), ui->txtSourcePath->text());
if(!dir.isEmpty()) {
sourceAddDir(dir);
if(ui->txtDestFile->text().isEmpty())
autoSelectDestFile();
checkSourceFileCount();
updateSrcFilesState();
}
}
static void sourceDelHelper(QTreeWidgetItem* item, SrcFileList& files, quint64& totalSize, int& totalCount, bool isRecur = false)
{
int children = item->childCount();
while(children--)
sourceDelHelper(item->child(children), files, totalSize, totalCount, true);
auto key = item->data(0, Qt::UserRole).toString();
if(!key.isEmpty()) { // key will be empty for directories
if(files[key].size()) {
totalSize -= files[key].size();
totalCount--;
}
files.remove(key);
}
auto parent = item->parent();
delete item;
// remove empty parents
if(isRecur) return;
while(parent && parent->childCount() == 0) {
item = parent;
parent = item->parent();
delete item;
}
}
void MainWindow::on_btnSourceDel_clicked()
{
auto selections = ui->tvSource->selectedItems();
ui->tvSource->setUpdatesEnabled(false);
while(!selections.isEmpty()) {
sourceDelHelper(selections.at(0), par2SrcFiles, par2SrcSize, par2FileCount);
selections = ui->tvSource->selectedItems();
}
ui->tvSource->setUpdatesEnabled(true);
updateSrcFilesState();
if(par2SrcFiles.isEmpty()) {
// assume we're starting afresh
srcBaseChosen = false;
destFileChosen = false;
}
}
static void recurseAddDir(const QDir& dir, SrcFileList& dest, ProgressDialog& progress)
{
// TODO: options for symlinks/system/hidden (system probably undesirable on Unix) files
auto list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
// since we don't know how deep this is going to get, we'll overallocate the progress and fix it at the end
progress.setMaximum(progress.maximum() + list.size()*2);
for(auto& info : list) {
if(progress.wasCanceled()) return;
auto key = info.canonicalFilePath();
if(info.isDir()) {
recurseAddDir(key, dest, progress);
} else {
#ifdef Q_OS_WINDOWS
key = key.toLower();
#endif
dest.insert(key, info);
}
progress.inc();
}
progress.setMaximum(progress.maximum() - list.size());
}
void MainWindow::sourceAddFiles(const QStringList &files)
{
ProgressDialog progress(this, tr("Adding files..."));
progress.setMaximum(files.size());
par2SrcFiles.reserve(par2SrcFiles.size() + files.size());
for(const auto& file : files) {
if(progress.wasCanceled()) break;
QFileInfo info(file);
if(info.isDir()) {
recurseAddDir(file, par2SrcFiles, progress);
} else {
auto key = info.canonicalFilePath();
#ifdef Q_OS_WINDOWS
key = key.toLower();
#endif
par2SrcFiles.insert(key, info);
progress.inc();
}
}
progress.end();
if(!srcBaseChosen)
ui->txtSourcePath->setText(srcFilesCommonPath().replace("/", QDir::separator()));
reloadSourceFiles();
}
void MainWindow::sourceAddDir(const QString& dir)
{
ProgressDialog progress(this, tr("Adding files..."));
progress.setMaximum(1); // we don't know the maximum, so add it as we go; can't set to 0 as it'd close the window
bool previouslyEmpty = par2SrcFiles.isEmpty();
recurseAddDir(dir, par2SrcFiles, progress);
progress.end();
QString basePath = dir;
if(!previouslyEmpty)
basePath = srcFilesCommonPath();
if(!srcBaseChosen)
ui->txtSourcePath->setText(basePath.replace("/", QDir::separator()));
reloadSourceFiles();
}
void MainWindow::on_btnSourcePathBrowse_clicked()
{
auto dir = QFileDialog::getExistingDirectory(this, tr("Select base path"), ui->txtSourcePath->text());
if(!dir.isEmpty()) {
ui->txtSourcePath->setText(dir.replace("/", QDir::separator()));
srcBaseChosen = true;
reloadSourceFiles();
updateDestPreview();
}
}
void MainWindow::on_btnDestPreview_clicked()
{
bool show = ui->btnDestPreview->isChecked();
adjustExpansion(show);
ui->scrollArea->ensureWidgetVisible(ui->tvDest);
Settings::getInstance().setUiExpDest(show);
updateDestPreview();
}
void MainWindow::on_cboSourcePaths_currentIndexChanged(int index)
{
bool enablePath = (index == 1);
ui->txtSourcePath->setEnabled(enablePath);
ui->btnSourcePathBrowse->setEnabled(enablePath);
ui->tvSource->setItemsExpandable(index != 0);
ui->tvSource->setRootIsDecorated(index != 0);
reloadSourceFiles();
updateDestPreview();
}
void MainWindow::on_btnDestFileBrowse_clicked()
{
auto file = QFileDialog::getSaveFileName(this, tr("Select destination recovery base file"), ui->txtDestFile->text(), tr("PAR2 files (*.par2)"));
if(!file.isEmpty()) {
ui->txtDestFile->setText(file.replace("/", QDir::separator()));
destFileChosen = true;
updateSrcFilesState();
updateDestPreview();
}
}
void MainWindow::on_txtInsliceSize_valueChanged(quint64 size, bool finished)
{
if(finished) {
if(!ui->optInsliceSize->isChecked()) return;
} else {
ui->optInsliceSize->blockSignals(true);
ui->optInsliceSize->setChecked(true);
ui->optInsliceSize->blockSignals(false);
}
par2SliceSize = size;
int count = Par2Calc::sliceCountFromSize(par2SliceSize, optionSliceMultiple, optionSliceLimit, par2SrcFiles, par2FileCount);
if(finished && size != par2SliceSize) {
ui->txtInsliceSize->setBytes(par2SliceSize);
}
ui->txtInsliceCount->blockSignals(true);
ui->txtInsliceCount->setValue(count);
ui->txtInsliceCount->blockSignals(false);
updateOutsliceInfo(finished);
}
void MainWindow::on_txtOutsliceRatio_valueChanged(double arg1)
{
ui->optOutsliceRatio->blockSignals(true);
ui->optOutsliceRatio->setChecked(true);
ui->optOutsliceRatio->blockSignals(false);
int srcSlices = ui->txtInsliceCount->value();
int destSlices = ceil(srcSlices * (arg1/100));
if(destSlices > 65535) destSlices = 65535;
ui->txtOutsliceCount->blockSignals(true);
ui->txtOutsliceCount->setValue(destSlices);
ui->txtOutsliceCount->blockSignals(false);
ui->txtOutsliceSize->setBytesApprox(par2SliceSize * destSlices, true);
updateDestInfo(false);
}
void MainWindow::on_txtOutsliceRatio_editingFinished()
{
if(!ui->optOutsliceRatio->isChecked()) return;
double val = ui->txtOutsliceRatio->value();
int srcSlices = ui->txtInsliceCount->value();
int destSlices = ceil(srcSlices * (val/100));
if(destSlices > 65535) {
val = 65535*100;
val /= srcSlices;
ui->txtOutsliceRatio->blockSignals(true);
ui->txtOutsliceRatio->setValue(val);
ui->txtOutsliceRatio->blockSignals(false);
} else {
ui->txtOutsliceCount->blockSignals(true);
ui->txtOutsliceCount->setValue(destSlices);
ui->txtOutsliceCount->blockSignals(false);
ui->txtOutsliceSize->setBytesApprox(par2SliceSize * destSlices, true);
updateDestInfo();
}
}
void MainWindow::txtOutsliceCount_updated(bool editingFinished)
{
int val = ui->txtOutsliceCount->value();
double perc = val*100;
perc /= ui->txtInsliceCount->value();
ui->txtOutsliceRatio->blockSignals(true);
ui->txtOutsliceRatio->setValue(perc);
ui->txtOutsliceRatio->blockSignals(false);
ui->txtOutsliceSize->setBytesApprox(par2SliceSize * val, true);
updateDestInfo(editingFinished);
}
void MainWindow::on_txtOutsliceCount_valueChanged(int arg1)
{
ui->optOutsliceCount->blockSignals(true);
ui->optOutsliceCount->setChecked(true);
ui->optOutsliceCount->blockSignals(false);
txtOutsliceCount_updated(false);
}
void MainWindow::on_txtOutsliceCount_editingFinished()
{
if(!ui->optOutsliceCount->isChecked()) return;
txtOutsliceCount_updated(true);
}
void MainWindow::on_txtOutsliceSize_valueChanged(quint64 size, bool finished)
{
if(finished) {
if(!ui->optOutsliceSize->isChecked()) return;
} else {
ui->optOutsliceSize->blockSignals(true);
ui->optOutsliceSize->setChecked(true);
ui->optOutsliceSize->blockSignals(false);
}
int destSlices = (size + par2SliceSize-1) / par2SliceSize; // round up
if(destSlices > 65535) {
destSlices = 65535;
if(finished) {
size = 65535 * par2SliceSize;
ui->txtOutsliceSize->setBytes(size, true);
return;
}
}
ui->txtOutsliceCount->blockSignals(true);
ui->txtOutsliceCount->setValue(destSlices);
ui->txtOutsliceCount->blockSignals(false);
double perc = destSlices*100;
perc /= ui->txtInsliceCount->value();
ui->txtOutsliceRatio->blockSignals(true);
ui->txtOutsliceRatio->setValue(perc);
ui->txtOutsliceRatio->blockSignals(false);
updateDestInfo(finished);
}
void MainWindow::on_cboDestDist_currentIndexChanged(int index)
{
int selection = ui->cboDestDist->currentIndex();
if(selection == 0 || selection == 2) {
ui->stkDestSizing->setCurrentIndex(0);
adjustExpansion(false);
} else {
ui->stkDestSizing->setCurrentIndex(selection/2 + 1);
adjustExpansion(true);
}
updateDestPreview();
}
void MainWindow::on_btnSourceAdv_clicked()
{
ui->btnSourceAdv->setChecked(false);
ui->stkSource->setCurrentIndex(1);
ui->btnSourceAdv2->focusWidget();
adjustExpansion(true);
Settings::getInstance().setUiExpSource(true);
}
void MainWindow::on_btnSourceAdv2_clicked()
{
ui->btnSourceAdv2->setChecked(true);
ui->stkSource->setCurrentIndex(0);
ui->btnSourceAdv->focusWidget();
adjustExpansion(false);
Settings::getInstance().setUiExpSource(false);
}
void MainWindow::on_btnComment_clicked()
{
bool accepted;
auto newComment = QInputDialog::getMultiLineText(this, tr("PAR2 Comment"), tr("Enter a comment for this PAR2"), par2Comment, &accepted);
if(accepted) {
par2Comment = newComment;
#ifdef Q_OS_WINDOWS
if(par2Comment.length() > 5000) { // command limit is 32767 chars, but we'll warn the user if they could exceed the 8191 limit of cmd.exe
QMessageBox::warning(this, tr("Set Comment"), tr("Comment has been set, however long comments may cause issues with Windows' command length limits. Keeping comments short is recommended."));
}
#endif
updateDestPreview();
ui->btnComment->setText(par2Comment.isEmpty() ? tr("Set Comme&nt...") : tr("Edit Comme&nt..."));
}
}
void MainWindow::on_btnSourceSetFiles_clicked()
{
auto files = QFileDialog::getOpenFileNames(this, tr("Add source files"), "", tr("All files (*.*)"));
if(!files.isEmpty()) {
par2SrcFiles.clear();
srcBaseChosen = false;
sourceAddFiles(files);
autoSelectDestFile();
checkSourceFileCount();
updateSrcFilesState();
}
}
void MainWindow::on_btnSourceSetDir_clicked()
{
auto dir = QFileDialog::getExistingDirectory(this, tr("Add source files from directory"), "");
if(!dir.isEmpty()) {
par2SrcFiles.clear();
srcBaseChosen = false;
sourceAddDir(dir);
autoSelectDestFile();
checkSourceFileCount();
updateSrcFilesState();
}
}
QString MainWindow::srcFilesCommonPath() const
{
if(par2SrcFiles.isEmpty()) return "";
auto keys = par2SrcFiles.keys();
QString common = keys.at(0);
int p = common.lastIndexOf('/');
if(p < 0) return ""; // invalid
common = common.left(p+1);
for(int i=1; i<keys.length(); i++) {
while(!keys.at(i).startsWith(common)) {
p = common.lastIndexOf('/', -2);
if(p < 0) return "";
common = common.left(p+1);
}
}
if(common.isEmpty()) return "";
// retrieve proper case from source list
for(const auto& key : keys) {
if(key.startsWith(common)) { // should always be true
QString name = par2SrcFiles[key].canonicalFilePath();
return QDir(name.left(common.length())).absolutePath();
}
}
return ""; // invalid
}
void MainWindow::reloadSourceFiles()
{
int pathOpt = ui->cboSourcePaths->currentIndex();
auto tv = ui->tvSource;
par2SrcSize = 0;
par2FileCount = 0;
if(par2SrcFiles.isEmpty()) {
tv->clear();
return;
}
tv->setUpdatesEnabled(false);
tv->clear();
ProgressDialog progress(this, tr("Building file list..."));
progress.setMaximum(par2SrcFiles.size() + 1);
progress.setCancelButton(nullptr);
// TODO: also disable window close button
if(pathOpt == 0) {
// add without pathing
QList<QTreeWidgetItem*> items;
auto srcKeys = par2SrcFiles.keys();
for(const auto& key : srcKeys) {
auto& file = par2SrcFiles[key];
auto item = SourceFileListItem::create(nullptr, file);
item->setData(0, Qt::UserRole, key);
items.append(item);
if(file.size()) {
par2SrcSize += file.size();
par2FileCount++;
}
file.par2name = file.fileName();
progress.inc();
}
tv->addTopLevelItems(items);
} else {
// if absolute, add base path as first item
QString basePath = "";
bool isRelative = (pathOpt == 1);
if(isRelative) {
basePath = ui->txtSourcePath->text();
basePath = basePath.replace(QDir::separator(), "/");
if(!basePath.isEmpty()) {
if(!basePath.endsWith('/')) basePath += "/";
} else // if there's no common path defined, treat as absolute
isRelative = false;
}
// TODO: for absolute paths (and relative as well) support merging single directories
QDir baseDir(basePath);
QHash<QString, QTreeWidgetItem*> dirNodes;
QList<QTreeWidgetItem*> topNodes;
auto it = QMutableHashIterator<QString, SourceFile>(par2SrcFiles);
while(it.hasNext()) {
it.next();
auto& file = it.value();
auto relPath = isRelative ? baseDir.relativeFilePath(file.canonicalFilePath()) : file.canonicalFilePath();
#ifdef Q_OS_WINDOWS
auto pathKey = relPath.toLower().split('/');
#else
auto pathKey = relPath.split('/');
#endif
if(pathKey.length() > 1) {
// child node - ensure folder nodes are present
QString key = pathKey[0];
if(!dirNodes.contains(key)) {
// create top level directory
auto dirItem = SourceFileListItem::create(nullptr, relPath.left(key.length()));
topNodes.append(dirItem);
dirNodes.insert(key, dirItem);
}
for(int i=1; i<pathKey.length()-1; i++) {
QString newKey = key + "/" + pathKey[i];
if(!dirNodes.contains(newKey)) {
auto dirItem = SourceFileListItem::create(
dirNodes[key],
relPath.mid(key.length()+1, pathKey[i].length())
);
dirNodes.insert(newKey, dirItem);
dirNodes[key]->addChild(dirItem);
}
key = newKey;
}
// insert actual node
auto newItem = SourceFileListItem::create(dirNodes[key], file);
newItem->setData(0, Qt::UserRole, it.key());
dirNodes[key]->addChild(newItem);
file.par2name = relPath;
file.par2name.replace("/", QDir::separator());
} else {
// top level file
auto newItem = SourceFileListItem::create(nullptr, file);
newItem->setData(0, Qt::UserRole, it.key());
topNodes.append(newItem);
file.par2name = file.fileName();
}
if(file.size()) {
par2SrcSize += file.size();
par2FileCount++;
}
progress.inc();
}
tv->addTopLevelItems(topNodes);
// expand top-level folders
for(auto node : topNodes) {
if(node->childCount() > 0)
node->setExpanded(true);
}
}
tv->setUpdatesEnabled(true);
progress.end();
}
static QString wrapFile(const QString& val)
{
if(!val.isEmpty() && val.at(0) == '-')
return QString(".") + QDir::separator() + val;
return val;
}
QStringList MainWindow::getCmdArgs(QHash<QString, QString>& env) const
{
QStringList list;
// slice sizing
if(ui->optInsliceSize->isChecked())
list << "--input-slices" << ui->txtInsliceSize->getSizeString();
if(ui->optInsliceCount->isChecked())
list << "--input-slices" << QString::number(ui->txtInsliceCount->value());
if(ui->optOutsliceRatio->isChecked())
list << "--recovery-slices" << (QString::number(ui->txtOutsliceRatio->value()) + "%");
if(ui->optOutsliceCount->isChecked())
list << "--recovery-slices" << QString::number(ui->txtOutsliceCount->value());
if(ui->optOutsliceSize->isChecked())
list << "--recovery-slices" << ui->txtOutsliceSize->getSizeString();
// input options
switch(ui->cboSourcePaths->currentIndex()) {
case 0: list << "--filepath-format" << "basename"; break;
case 1: list << "--filepath-format" << "path" << "--filepath-base" << wrapFile(ui->txtSourcePath->text()); break;
case 2: list << "--filepath-format" << "keep"; break;
}
// output options
list << "--out" << wrapFile(ui->txtDestFile->text()) << "--overwrite";
if(ui->txtDestOffset->value() > 0)
list << "--recovery-offset" << QString::number(ui->txtDestOffset->value());
if(ui->cboDestDist->isEnabled()) {
switch(ui->cboDestDist->currentIndex()) {
case 0: list << "--slice-dist" << "equal" << "--noindex"; break;
case 1: list << "--slice-dist" << "uniform";
if(ui->optDestFiles->isChecked())
list << "--recovery-files" << QString::number(ui->txtDestFiles->value());
if(ui->optDestCount->isChecked())
list << "--slices-per-file" << QString::number(ui->txtDestCount->value());
if(ui->optDestSize->isChecked())
list << "--slices-per-file" << ui->txtDestSize->getSizeString();
break;
case 2: list << "--slice-dist" << "pow2"; break;
case 3: list << "--slice-dist" << "pow2";
if(ui->optDestMaxLfile->isChecked())
list << "--slices-per-file" << "1l";
if(ui->optDestMaxCount->isChecked())
list << "--slices-per-file" << QString::number(ui->txtDestMaxCount->value());
if(ui->optDestMaxSize->isChecked())
list << "--slices-per-file" << ui->txtDestMaxSize->getSizeString();
break;
}
}
if(!par2Comment.isEmpty()) {
if(par2Comment.at(0) == '-')
list << QString("--comment=") + par2Comment;
else
list << "--comment" << par2Comment;
}
// processing options
const auto& settings = Settings::getInstance();
if(settings.unicode() != AUTO)
list << (settings.unicode() == INCLUDE ? "--unicode" : "--no-unicode");
if(settings.charset() != "utf8")
list << "--ascii-charset" << settings.charset();
if(settings.packetRepMin() != 1)
list << "--min-packet-redundancy" << QString::number(settings.packetRepMin());
if(settings.packetRepMax() != 16 && settings.packetRepMin() < 16)
list << "--max-packet-redundancy" << QString::number(settings.packetRepMax());
if(settings.stdNaming())
list << "--std-naming";
// TODO: should we include the slice size multiple?
if(settings.outputSync())
list << "--write-sync";
list << "--seq-read-size" << settings.readSize()
<< "--read-buffers" << QString::number(settings.readBuffers())
<< "--read-hash-queue" << QString::number(settings.hashQueue())
<< "--min-chunk-size" << settings.minChunk()
<< "--chunk-read-threads" << QString::number(settings.chunkReadThreads())
<< "--recovery-buffers" << QString::number(settings.recBuffers())
<< "--md5-batch-size" << QString::number(settings.hashBatch())
<< "--cpu-minchunk" << settings.cpuMinChunk();
if(settings.hashMethod() != "auto")
list << "--hash-method" << settings.hashMethod();
if(settings.md5Method() != "auto")
list << "--md5-method" << settings.md5Method();
if(settings.procBatch() >= 0)
list << "--proc-batch-size" << QString::number(settings.procBatch());
if(!settings.memLimit().isEmpty())
list << "--memory" << settings.memLimit();
if(!settings.gfMethod().isEmpty())
list << "--method" << settings.gfMethod();
if(!settings.tileSize().isEmpty())
list << "--loop-tile-size" << settings.tileSize();
if(settings.threadNum() >= 0)
list << "--threads" << QString::number(settings.threadNum());
const auto openclDevices = settings.openclDevices();
for(const auto& dev : openclDevices) {
QString devLine("device=%1,process=%2,minchunk=%3,method=%4");
QString devName(dev.name);
devName.remove(',');
devLine = devLine.arg(devName, QString::number(dev.alloc) + "%", dev.minChunk, dev.gfMethod);
if(!dev.memLimit.isEmpty())
devLine += QString(",memory=") + dev.memLimit;
if(dev.batch)
devLine += QString(",batch-size=") + QString::number(dev.batch);
if(dev.iters)
devLine += QString(",iter-count=") + QString::number(dev.iters);
if(dev.outputs)
devLine += QString(",grouping=") + QString::number(dev.outputs);
list << "--opencl" << devLine;
}
if(settings.chunkReadThreads() >= 3)
env.insert("UV_THREADPOOL_SIZE", QString::number(settings.chunkReadThreads()+2));
return list;
}