File indexing completed on 2024-04-28 15:40:13

0001 // SPDX-FileCopyrightText: 2012 Jesper K. Pedersen <jesper.pedersen@kdab.com>
0002 // SPDX-FileCopyrightText: 2013-2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 // SPDX-FileCopyrightText: 2020 Tobias Leupold <tl@stonemx.de>
0004 //
0005 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 
0007 #include "UpdateVideoThumbnail.h"
0008 
0009 #include "Logging.h"
0010 #include "Window.h"
0011 
0012 #include <BackgroundJobs/HandleVideoThumbnailRequestJob.h>
0013 #include <ThumbnailView/CellGeometry.h>
0014 #include <Utilities/FileUtil.h>
0015 #include <kpabase/FileExtensions.h>
0016 #include <kpathumbnails/ThumbnailCache.h>
0017 
0018 #include <QLoggingCategory>
0019 
0020 namespace
0021 {
0022 
0023 DB::FileName nextExistingImage(const DB::FileName &fileName, int frame, int direction)
0024 {
0025     // starting with i=1 is *not* an off-by-one error
0026     for (int i = 1; i < 10; ++i) {
0027         const int nextIndex = (frame + 10 + direction * i) % 10;
0028         const DB::FileName file = BackgroundJobs::HandleVideoThumbnailRequestJob::frameName(fileName, nextIndex);
0029         if (file.exists()) {
0030             qCDebug(MainWindowLog) << "Next frame for video" << file.relative() << " has index" << nextIndex;
0031             return file;
0032         }
0033     }
0034     const DB::FileName file = BackgroundJobs::HandleVideoThumbnailRequestJob::frameName(fileName, 0);
0035     qCDebug(MainWindowLog) << "No next thumbnail found for video" << fileName.relative()
0036                            << "(frame:" << frame << ", direction:" << direction << ") - thumbnail base name:" << file.absolute();
0037     // the thumbnail may not have been written to disk yet
0038     return DB::FileName();
0039 }
0040 
0041 void update(const DB::FileName &fileName, int direction)
0042 {
0043     const DB::FileName baseImageName = BackgroundJobs::HandleVideoThumbnailRequestJob::pathForRequest(fileName);
0044     QImage baseImage(baseImageName.absolute());
0045 
0046     int frame = 0;
0047     for (; frame < 10; ++frame) {
0048         const DB::FileName frameFile = BackgroundJobs::HandleVideoThumbnailRequestJob::frameName(fileName, frame);
0049         QImage frameImage(frameFile.absolute());
0050         if (frameImage.isNull())
0051             continue;
0052         if (baseImage == frameImage) {
0053             break;
0054         }
0055     }
0056 
0057     const DB::FileName newImageName = nextExistingImage(fileName, frame, direction);
0058     if (newImageName.isNull())
0059         return;
0060 
0061     Utilities::copyOrOverwrite(newImageName.absolute(), baseImageName.absolute());
0062 
0063     QImage image = QImage(newImageName.absolute()).scaled(ThumbnailView::CellGeometry::preferredIconSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
0064     MainWindow::Window::theMainWindow()->thumbnailCache()->insert(fileName, image);
0065     MainWindow::Window::theMainWindow()->reloadThumbnails();
0066 }
0067 
0068 void update(const DB::FileNameList &list, int direction)
0069 {
0070     for (const DB::FileName &fileName : list) {
0071         if (KPABase::isVideo(fileName))
0072             update(fileName, direction);
0073     }
0074 }
0075 } // namespace
0076 
0077 void MainWindow::UpdateVideoThumbnail::useNext(const DB::FileNameList &list)
0078 {
0079     update(list, +1);
0080 }
0081 
0082 void MainWindow::UpdateVideoThumbnail::usePrevious(const DB::FileNameList &list)
0083 {
0084     update(list, -1);
0085 }
0086 
0087 // vi:expandtab:tabstop=4 shiftwidth=4: