-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcomparison.cpp
576 lines (498 loc) · 26.7 KB
/
comparison.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
#include <QMessageBox>
#include <QWheelEvent>
#include "comparison.h"
#include "ui_comparison.h"
Comparison::Comparison(const QVector<Video *> &videosParam, const Prefs &prefsParam) :
QDialog(prefsParam._mainwPtr, Qt::Window), _videos(videosParam), _prefs(prefsParam)
{
ui = new Ui::Comparison;
ui->setupUi(this);
connect(this, SIGNAL(sendStatusMessage(const QString &)), _prefs._mainwPtr, SLOT(addStatusMessage(const QString &)));
connect(this, SIGNAL(switchComparisonMode(const int &)), _prefs._mainwPtr, SLOT(setComparisonMode(const int &)));
connect(this, SIGNAL(adjustThresholdSlider(const int &)), _prefs._mainwPtr, SLOT(on_thresholdSlider_valueChanged(const int &)));
if(_prefs._comparisonMode == _prefs._SSIM)
ui->selectSSIM->setChecked(true);
ui->thresholdSlider->setValue(QVariant(_prefs._thresholdSSIM * 100).toInt());
ui->progressBar->setMaximum(_prefs._numberOfVideos * (_prefs._numberOfVideos - 1) / 2);
on_nextVideo_clicked();
}
Comparison::~Comparison()
{
delete ui;
}
void Comparison::reportMatchingVideos()
{
int64_t combinedFilesize = 0;
int foundMatches = 0;
QVector<Video*>::const_iterator left, right, end = _videos.cend();
for(left=_videos.cbegin(); left<end; left++)
for(right=left+1; right<end; right++)
if(bothVideosMatch(*left, *right))
{ //smaller of two matching videos is likely the one to be deleted
combinedFilesize += std::min((*left)->size , (*right)->size);
foundMatches++;
break;
}
if(foundMatches)
emit sendStatusMessage(QStringLiteral("\n[%1] Found %2 video(s) (%3) with one or more matches")
.arg(QTime::currentTime().toString()).arg(foundMatches).arg(readableFileSize(combinedFilesize)));
}
void Comparison::confirmToExit()
{
int confirm = QMessageBox::Yes;
if(!ui->leftFileName->text().isEmpty())
{
QMessageBox msgBox;
msgBox.setWindowTitle(QStringLiteral("Out of videos to compare"));
msgBox.setText(QStringLiteral("Close window? "));
msgBox.setIcon(QMessageBox::QMessageBox::Question);
msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
confirm = msgBox.exec();
}
if(confirm == QMessageBox::Yes)
{
if(_videosDeleted)
emit sendStatusMessage(QStringLiteral("\n%1 file(s) deleted, %2 freed")
.arg(_videosDeleted).arg(readableFileSize(_spaceSaved)));
if(!ui->leftFileName->text().isEmpty())
emit sendStatusMessage(QStringLiteral("\nPressing Find duplicates button opens comparison window "
"again if thumbnail mode and directories remain the same"));
else
emit sendStatusMessage(QStringLiteral("\nComparison window closed because no matching videos found "
"(a lower threshold may help to find more matches)"));
QKeyEvent *closeEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
QApplication::postEvent(this, closeEvent); //"pressing" ESC closes dialog
}
}
void Comparison::on_prevVideo_clicked()
{
_seekForwards = false;
QVector<Video*>::const_iterator left, right, begin = _videos.cbegin();
for(_rightVideo--, left=begin+_leftVideo; left>=begin; left--, _leftVideo--)
{
for(right=begin+_rightVideo; right>left; right--, _rightVideo--)
if(bothVideosMatch(*left, *right) && QFileInfo::exists((*left)->filename) && QFileInfo::exists((*right)->filename))
{
showVideo(QStringLiteral("left"));
showVideo(QStringLiteral("right"));
highlightBetterProperties();
updateUI();
return;
}
ui->progressBar->setValue(comparisonsSoFar());
_rightVideo = _prefs._numberOfVideos - 1;
}
on_nextVideo_clicked(); //went over limit, go forwards until first match
}
void Comparison::on_nextVideo_clicked()
{
_seekForwards = true;
const int oldLeft = _leftVideo;
const int oldRight = _rightVideo;
QVector<Video*>::const_iterator left, right, begin = _videos.cbegin(), end = _videos.cend();
for(left=begin+_leftVideo; left<end; left++, _leftVideo++)
{
for(_rightVideo++, right=begin+_rightVideo; right<end; right++, _rightVideo++)
if(bothVideosMatch(*left, *right) && QFileInfo::exists((*left)->filename) && QFileInfo::exists((*right)->filename))
{
showVideo(QStringLiteral("left"));
showVideo(QStringLiteral("right"));
highlightBetterProperties();
updateUI();
return;
}
ui->progressBar->setValue(comparisonsSoFar());
_rightVideo = _leftVideo + 1;
}
_leftVideo = oldLeft; //went over limit, go to last matching pair
_rightVideo = oldRight;
confirmToExit();
}
bool Comparison::bothVideosMatch(const Video *left, const Video *right)
{
bool theyMatch = false;
_phashSimilarity = 0;
const int hashes = _prefs._thumbnails == cutEnds? 2 : 1;
for(int hash=0; hash<hashes; hash++)
{ //if cutEnds mode: similarity is always the best one of both comparisons
_phashSimilarity = qMax( _phashSimilarity, phashSimilarity(left, right, hash));
if(_prefs._comparisonMode == _prefs._PHASH)
{
if(_phashSimilarity >= _prefs._thresholdPhash)
theyMatch = true;
} //ssim comparison is slow, only do it if pHash differs at most 20 bits of 64
else if(_phashSimilarity >= qMax(_prefs._thresholdPhash, 44))
{
_ssimSimilarity = ssim(left->grayThumb[hash], right->grayThumb[hash], _prefs._ssimBlockSize);
_ssimSimilarity = _ssimSimilarity + _durationModifier / 64.0; // b/64 bits (phash) <=> p/100 % (ssim)
if(_ssimSimilarity > _prefs._thresholdSSIM)
theyMatch = true;
}
if(theyMatch) //if cutEnds mode: first comparison matched already, skip second
break;
}
return theyMatch;
}
int Comparison::phashSimilarity(const Video *left, const Video *right, const int &nthHash)
{
if(left->hash[nthHash] == 0 && right->hash[nthHash] == 0)
return 0;
int distance = 64;
uint64_t differentBits = left->hash[nthHash] ^ right->hash[nthHash]; //XOR to value (only ones for differing bits)
while(differentBits)
{
differentBits &= differentBits - 1; //count number of bits of value
distance--;
}
if( qAbs(left->duration - right->duration) <= 1000 )
_durationModifier = 0 + _prefs._sameDurationModifier; //lower distance if both durations within 1s
else
_durationModifier = 0 - _prefs._differentDurationModifier; //raise distance if both durations differ 1s
distance = distance + _durationModifier;
return distance > 64? 64 : distance;
}
void Comparison::showVideo(const QString &side) const
{
int thisVideo = _leftVideo;
if(side == QLatin1String("right"))
thisVideo = _rightVideo;
auto *Image = this->findChild<ClickableLabel *>(side + QStringLiteral("Image"));
QBuffer pixels(&_videos[thisVideo]->thumbnail);
QImage image;
image.load(&pixels, QByteArrayLiteral("JPG"));
Image->setPixmap(QPixmap::fromImage(image).scaled(Image->width(), Image->height(), Qt::KeepAspectRatio));
auto *FileName = this->findChild<ClickableLabel *>(side + QStringLiteral("FileName"));
FileName->setText(QFileInfo(_videos[thisVideo]->filename).fileName());
FileName->setToolTip(QStringLiteral("%1\nOpen in file manager")
.arg(QDir::toNativeSeparators(_videos[thisVideo]->filename)));
QFileInfo videoFile(_videos[thisVideo]->filename);
auto *PathName = this->findChild<QLabel *>(side + QStringLiteral("PathName"));
PathName->setText(QDir::toNativeSeparators(videoFile.absolutePath()));
auto *FileSize = this->findChild<QLabel *>(side + QStringLiteral("FileSize"));
FileSize->setText(readableFileSize(_videos[thisVideo]->size));
auto *Duration = this->findChild<QLabel *>(side + QStringLiteral("Duration"));
Duration->setText(readableDuration(_videos[thisVideo]->duration));
auto *Modified = this->findChild<QLabel *>(side + QStringLiteral("Modified"));
Modified->setText(_videos[thisVideo]->modified.toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")));
const QString resolutionString = QStringLiteral("%1x%2").
arg(_videos[thisVideo]->width).arg(_videos[thisVideo]->height);
auto *Resolution = this->findChild<QLabel *>(side + QStringLiteral("Resolution"));
Resolution->setText(resolutionString);
auto *FrameRate = this->findChild<QLabel *>(side + QStringLiteral("FrameRate"));
const double fps = _videos[thisVideo]->framerate;
if(fps == 0.0)
FrameRate->clear();
else
FrameRate->setText(QStringLiteral("%1 FPS").arg(fps));
auto *BitRate = this->findChild<QLabel *>(side + QStringLiteral("BitRate"));
BitRate->setText(readableBitRate(_videos[thisVideo]->bitrate));
auto *Codec = this->findChild<QLabel *>(side + QStringLiteral("Codec"));
Codec->setText(_videos[thisVideo]->codec);
auto *Audio = this->findChild<QLabel *>(side + QStringLiteral("Audio"));
Audio->setText(_videos[thisVideo]->audio);
}
QString Comparison::readableDuration(const int64_t &milliseconds) const
{
if(milliseconds == 0)
return QStringLiteral("");
const int hours = milliseconds / (1000*60*60) % 24;
const int minutes = milliseconds / (1000*60) % 60;
const int seconds = milliseconds / 1000 % 60;
QString readableDuration;
if(hours > 0)
readableDuration = QStringLiteral("%1h").arg(hours);
if(minutes > 0)
readableDuration = QStringLiteral("%1%2m").arg(readableDuration).arg(minutes);
if(seconds > 0)
readableDuration = QStringLiteral("%1%2s").arg(readableDuration).arg(seconds);
return readableDuration;
}
QString Comparison::readableFileSize(const int64_t &filesize) const
{
if(filesize < 1024 * 1024)
return(QStringLiteral("%1 kB").arg(QString::number(filesize / 1024.0, 'i', 0))); //even kBs
else if(filesize < 1024 * 1024 * 1024) //larger files have one decimal point
return QStringLiteral("%1 MB").arg(QString::number(filesize / (1024.0 * 1024.0), 'f', 1));
else
return QStringLiteral("%1 GB").arg(QString::number(filesize / (1024.0 * 1024.0 * 1024.0), 'f', 1));
}
QString Comparison::readableBitRate(const double &kbps) const
{
if(kbps == 0.0)
return QStringLiteral("");
return QStringLiteral("%1 kb/s").arg(kbps);
}
void Comparison::highlightBetterProperties() const
{
ui->leftFileSize->setStyleSheet(QStringLiteral(""));
ui->rightFileSize->setStyleSheet(QStringLiteral("")); //both filesizes within 100 kb
if(qAbs(_videos[_leftVideo]->size - _videos[_rightVideo]->size) <= 1024*100)
{
ui->leftFileSize->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
ui->rightFileSize->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
}
else if(_videos[_leftVideo]->size > _videos[_rightVideo]->size)
ui->leftFileSize->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
else if(_videos[_leftVideo]->size < _videos[_rightVideo]->size)
ui->rightFileSize->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
ui->leftDuration->setStyleSheet(QStringLiteral(""));
ui->rightDuration->setStyleSheet(QStringLiteral("")); //both runtimes within 1 second
if(qAbs(_videos[_leftVideo]->duration - _videos[_rightVideo]->duration) <= 1000)
{
ui->leftDuration->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
ui->rightDuration->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
}
else if(_videos[_leftVideo]->duration > _videos[_rightVideo]->duration)
ui->leftDuration->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
else if(_videos[_leftVideo]->duration < _videos[_rightVideo]->duration)
ui->rightDuration->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
ui->leftBitRate->setStyleSheet(QStringLiteral(""));
ui->rightBitRate->setStyleSheet(QStringLiteral(""));
if(_videos[_leftVideo]->bitrate == _videos[_rightVideo]->bitrate)
{
ui->leftBitRate->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
ui->rightBitRate->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
}
else if(_videos[_leftVideo]->bitrate > _videos[_rightVideo]->bitrate)
ui->leftBitRate->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
else if(_videos[_leftVideo]->bitrate < _videos[_rightVideo]->bitrate)
ui->rightBitRate->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
ui->leftFrameRate->setStyleSheet(QStringLiteral(""));
ui->rightFrameRate->setStyleSheet(QStringLiteral("")); //both framerates within 0.1 fps
if(qAbs(_videos[_leftVideo]->framerate - _videos[_rightVideo]->framerate) <= 0.1)
{
ui->leftFrameRate->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
ui->rightFrameRate->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
}
else if(_videos[_leftVideo]->framerate > _videos[_rightVideo]->framerate)
ui->leftFrameRate->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
else if(_videos[_leftVideo]->framerate < _videos[_rightVideo]->framerate)
ui->rightFrameRate->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
ui->leftModified->setStyleSheet(QStringLiteral(""));
ui->rightModified->setStyleSheet(QStringLiteral(""));
if(_videos[_leftVideo]->modified == _videos[_rightVideo]->modified)
{
ui->leftModified->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
ui->rightModified->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
}
else if(_videos[_leftVideo]->modified < _videos[_rightVideo]->modified)
ui->leftModified->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
else if(_videos[_leftVideo]->modified > _videos[_rightVideo]->modified)
ui->rightModified->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
ui->leftResolution->setStyleSheet(QStringLiteral(""));
ui->rightResolution->setStyleSheet(QStringLiteral(""));
if(_videos[_leftVideo]->width * _videos[_leftVideo]->height ==
_videos[_rightVideo]->width * _videos[_rightVideo]->height)
{
ui->leftResolution->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
ui->rightResolution->setStyleSheet(QStringLiteral("QLabel { color : peru; }"));
}
else if(_videos[_leftVideo]->width * _videos[_leftVideo]->height >
_videos[_rightVideo]->width * _videos[_rightVideo]->height)
ui->leftResolution->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
else if(_videos[_leftVideo]->width * _videos[_leftVideo]->height <
_videos[_rightVideo]->width * _videos[_rightVideo]->height)
ui->rightResolution->setStyleSheet(QStringLiteral("QLabel { color : green; }"));
}
void Comparison::updateUI()
{
if(ui->leftPathName->text() == ui->rightPathName->text()) //gray out move button if both videos in same folder
{
ui->leftMove->setDisabled(true);
ui->rightMove->setDisabled(true);
}
else
{
ui->leftMove->setDisabled(false);
ui->rightMove->setDisabled(false);
}
if(_prefs._comparisonMode == _prefs._PHASH)
ui->identicalBits->setText(QString("%1/64 same bits").arg(_phashSimilarity));
if(_prefs._comparisonMode == _prefs._SSIM)
ui->identicalBits->setText(QString("%1 SSIM index").arg(QString::number(qMin(_ssimSimilarity, 1.0), 'f', 3)));
_zoomLevel = 0;
ui->progressBar->setValue(comparisonsSoFar());
}
int Comparison::comparisonsSoFar() const
{
const int cmpFirst = _prefs._numberOfVideos; //comparisons done for first video
const int cmpThis = cmpFirst - _leftVideo; //comparisons done for current video
const int remaining = cmpThis * (cmpThis - 1) / 2; //comparisons for remaining videos
const int maxComparisons = cmpFirst * (cmpFirst - 1) / 2; //comparing all videos with each other
return maxComparisons - remaining + _rightVideo - _leftVideo;
}
void Comparison::openFileManager(const QString &filename) const
{
#if defined(Q_OS_WIN)
QProcess::startDetached(QStringLiteral("explorer /select, \"%1\"").arg(QDir::toNativeSeparators(filename)));
#endif
#if defined(Q_OS_MACX)
QProcess::startDetached(QStringLiteral("open -R \"%1\"").arg(filename));
#endif
#if defined(Q_OS_X11)
QProcess::startDetached(QStringLiteral("xdg-open \"%1\"").arg(filename.left(filename.lastIndexOf("/"))));
#endif
}
void Comparison::deleteVideo(const int &side)
{
const QString filename = _videos[side]->filename;
const QString onlyFilename = filename.right(filename.length() - filename.lastIndexOf("/") - 1);
const Db cache(filename); //generate unique id before file has been deleted
const QString id = cache.uniqueId();
if(!QFileInfo::exists(filename)) //video was already manually deleted, skip to next
{
_seekForwards? on_nextVideo_clicked() : on_prevVideo_clicked();
return;
}
if(QMessageBox::question(this, "Delete file", QString("Are you sure you want to delete this file?\n\n%1")
.arg(onlyFilename), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
if(!QFile::remove(filename))
QMessageBox::information(this, "", "Could not delete file. Check file permissions.");
else
{
_videosDeleted++;
_spaceSaved = _spaceSaved + _videos[side]->size;
cache.removeVideo(id);
emit sendStatusMessage(QString("Deleted %1").arg(QDir::toNativeSeparators(filename)));
_seekForwards? on_nextVideo_clicked() : on_prevVideo_clicked();
}
}
}
void Comparison::moveVideo(const QString &from, const QString &to)
{
if(!QFileInfo::exists(from))
{
_seekForwards? on_nextVideo_clicked() : on_prevVideo_clicked();
return;
}
const QString fromPath = from.left(from.lastIndexOf("/"));
const QString toPath = to.left(to.lastIndexOf("/"));
const QString question = QString("Are you sure you want to move this file?\n\nFrom: %1\nTo: %2")
.arg(QDir::toNativeSeparators(fromPath), QDir::toNativeSeparators(toPath));
if(QMessageBox::question(this, "Move", question, QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
{
QFile moveThisFile(from);
if(!moveThisFile.rename(QString("%1/%2").arg(toPath, from.right(from.length() - from.lastIndexOf("/") - 1))))
QMessageBox::information(this, "", "Could not move file. Check file permissions and available disk space.");
else
{
emit sendStatusMessage(QString("Moved %1 to %2").arg(QDir::toNativeSeparators(from), toPath));
_seekForwards? on_nextVideo_clicked() : on_prevVideo_clicked();
}
}
}
void Comparison::on_swapFilenames_clicked() const
{
const QFileInfo leftVideoFile(_videos[_leftVideo]->filename);
const QString leftPathname = leftVideoFile.absolutePath();
const QString oldLeftFilename = leftVideoFile.fileName();
const QString oldLeftNoExtension = oldLeftFilename.left(oldLeftFilename.lastIndexOf("."));
const QString leftExtension = oldLeftFilename.right(oldLeftFilename.length() - oldLeftFilename.lastIndexOf("."));
const QFileInfo rightVideoFile(_videos[_rightVideo]->filename);
const QString rightPathname = rightVideoFile.absolutePath();
const QString oldRightFilename = rightVideoFile.fileName();
const QString oldRightNoExtension = oldRightFilename.left(oldRightFilename.lastIndexOf("."));
const QString rightExtension = oldRightFilename.right(oldRightFilename.length() - oldRightFilename.lastIndexOf("."));
const QString newLeftFilename = QStringLiteral("%1%2").arg(oldRightNoExtension, leftExtension);
const QString newLeftPathAndFilename = QStringLiteral("%1/%2").arg(leftPathname, newLeftFilename);
const QString newRightFilename = QStringLiteral("%1%2").arg(oldLeftNoExtension, rightExtension);
const QString newRightPathAndFilename = QStringLiteral("%1/%2").arg(rightPathname, newRightFilename);
QFile leftFile(_videos[_leftVideo]->filename); //rename files
QFile rightFile(_videos[_rightVideo]->filename);
leftFile.rename(QStringLiteral("%1/VidupeRenamedVideo.avi").arg(leftPathname));
rightFile.rename(newRightPathAndFilename);
leftFile.rename(newLeftPathAndFilename);
_videos[_leftVideo]->filename = newLeftPathAndFilename; //update filename in object
_videos[_rightVideo]->filename = newRightPathAndFilename;
ui->leftFileName->setText(newLeftFilename); //update UI
ui->rightFileName->setText(newRightFilename);
Db cache(_videos[_leftVideo]->filename);
cache.removeVideo(cache.uniqueId(oldLeftFilename)); //remove both videos from cache
cache.removeVideo(cache.uniqueId(oldRightFilename));
}
void Comparison::on_thresholdSlider_valueChanged(const int &value)
{
_prefs._thresholdSSIM = value / 100.0;
const int matchingBitsOf64 = static_cast<int>(round(64 * _prefs._thresholdSSIM));
_prefs._thresholdPhash = matchingBitsOf64;
const QString thresholdMessage = QStringLiteral(
"Threshold: %1% (%2/64 bits = match) Default: 89%\n"
"Smaller: less strict, can match different videos (false positive)\n"
"Larger: more strict, can miss identical videos (false negative)").arg(value).arg(matchingBitsOf64);
ui->thresholdSlider->setToolTip(thresholdMessage);
emit adjustThresholdSlider(ui->thresholdSlider->value());
}
void Comparison::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event)
if(ui->leftFileName->text().isEmpty() || _leftVideo >= _prefs._numberOfVideos || _rightVideo >= _prefs._numberOfVideos)
return; //automatic initial resize event can happen before closing when values went over limit
QImage image;
QBuffer leftPixels(&_videos[_leftVideo]->thumbnail);
image.load(&leftPixels, QByteArrayLiteral("JPG"));
ui->leftImage->setPixmap(QPixmap::fromImage(image).scaled(
ui->leftImage->width(), ui->leftImage->height(), Qt::KeepAspectRatio));
QBuffer rightPixels(&_videos[_rightVideo]->thumbnail);
image.load(&rightPixels, QByteArrayLiteral("JPG"));
ui->rightImage->setPixmap(QPixmap::fromImage(image).scaled(
ui->rightImage->width(), ui->rightImage->height(), Qt::KeepAspectRatio));
}
void Comparison::wheelEvent(QWheelEvent *event)
{
const QPoint pos = QCursor::pos();
if(!QApplication::widgetAt(pos))
return;
ClickableLabel *imagePtr;
if(QApplication::widgetAt(pos)->objectName() == QLatin1String("leftImage"))
imagePtr = ui->leftImage;
else if(QApplication::widgetAt(pos)->objectName() == QLatin1String("rightImage"))
imagePtr = ui->rightImage;
else
return;
const int wmax = imagePtr->mapToGlobal(QPoint(imagePtr->pixmap()->width(), 0)).x(); //image right edge
const int hmax = imagePtr->mapToGlobal(QPoint(0, imagePtr->pixmap()->height())).y(); //image bottom edge
const double ratiox = 1-static_cast<double>(wmax-pos.x()) / imagePtr->pixmap()->width(); //mouse pos inside image
const double ratioy = 1-static_cast<double>(hmax-pos.y()) / imagePtr->pixmap()->height();
const int widescreenBlack = (imagePtr->height() - imagePtr->pixmap()->height()) / 2;
const int imgTop = imagePtr->mapToGlobal(QPoint(0,0)).y() + widescreenBlack;
const int imgBtm = imgTop + imagePtr->pixmap()->height();
if(pos.x() > wmax || pos.y() < imgTop || pos.y() > imgBtm) //image is smaller than label underneath
return;
if(_zoomLevel == 0) //first mouse wheel movement: retrieve actual screen captures in full resolution
{
QApplication::setOverrideCursor(Qt::WaitCursor);
QImage image;
image = _videos[_leftVideo]->captureAt(10);
ui->leftImage->setPixmap(QPixmap::fromImage(image).scaled(
ui->leftImage->width(), ui->leftImage->height(), Qt::KeepAspectRatio));
_leftZoomed = QPixmap::fromImage(image); //keep it in memory
_leftW = image.width();
_leftH = image.height();
image = _videos[_rightVideo]->captureAt(10);
ui->rightImage->setPixmap(QPixmap::fromImage(image).scaled(
ui->rightImage->width(), ui->rightImage->height(), Qt::KeepAspectRatio));
_rightZoomed = QPixmap::fromImage(image);
_rightW = image.width();
_rightH = image.height();
_zoomLevel = 1;
QApplication::restoreOverrideCursor();
return;
}
if(event->delta() > 0 && _zoomLevel < 10) //mouse wheel up
_zoomLevel = _zoomLevel * 2;
if(event->delta() < 0 && _zoomLevel > 1) //mouse wheel down
_zoomLevel = _zoomLevel / 2;
QPixmap pix;
pix = _leftZoomed.copy(static_cast<int>(_leftW*ratiox-_leftW*ratiox/_zoomLevel),
static_cast<int>(_leftH*ratioy-_leftH*ratioy/_zoomLevel),
_leftW/_zoomLevel, _leftH/_zoomLevel);
ui->leftImage->setPixmap(pix.scaled(ui->leftImage->width(), ui->leftImage->height(),
Qt::KeepAspectRatio, Qt::FastTransformation));
pix = _rightZoomed.copy(static_cast<int>(_rightW*ratiox-_rightW*ratiox/_zoomLevel),
static_cast<int>(_rightH*ratioy-_rightH*ratioy/_zoomLevel),
_rightW/_zoomLevel, _rightH/_zoomLevel);
ui->rightImage->setPixmap(pix.scaled(ui->rightImage->width(), ui->rightImage->height(),
Qt::KeepAspectRatio, Qt::FastTransformation));
}