File indexing completed on 2024-04-28 04:21:27

0001 /* SPDX-FileCopyrightText: 2012-2020 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "VideoShooter.h"
0007 
0008 #include "InfoBox.h"
0009 #include "VideoDisplay.h"
0010 #include "ViewerWidget.h"
0011 
0012 #include <BackgroundJobs/HandleVideoThumbnailRequestJob.h>
0013 #include <DB/ImageInfo.h>
0014 #include <MainWindow/Window.h>
0015 #include <kpathumbnails/ThumbnailCache.h>
0016 
0017 #include <QApplication>
0018 #include <QTimer>
0019 
0020 /**
0021   \class Viewer::VideoShooter
0022   \brief Utility class for making screenshots from a video
0023 
0024   While Phonon has an API for implementing a screenshot, it unfortunately doesn't work.
0025   We therefore need to make a screenshot using QPixmap::grabWindow (grabWidget doesn't work either).
0026   As grabWindow takes the pixels directly from the window, we need to make sure that it isn't covered with the infobox or the context menu.
0027   This class takes care of that, namely waiting for the context menu to disapear, hide the infobox, stop the video, and then shoot the snapshot
0028  */
0029 
0030 Viewer::VideoShooter *Viewer::VideoShooter::s_instance = nullptr;
0031 
0032 Viewer::VideoShooter::VideoShooter()
0033 {
0034 }
0035 
0036 void Viewer::VideoShooter::go(const DB::ImageInfoPtr &info, Viewer::ViewerWidget *viewer)
0037 {
0038     if (!s_instance)
0039         s_instance = new VideoShooter;
0040 
0041     s_instance->start(info, viewer);
0042 }
0043 
0044 void Viewer::VideoShooter::start(const DB::ImageInfoPtr &info, ViewerWidget *viewer)
0045 {
0046     qApp->setOverrideCursor(QCursor(Qt::BusyCursor));
0047     m_info = info;
0048     m_viewer = viewer;
0049 
0050     // Hide the info box
0051     m_infoboxVisible = m_viewer->m_infoBox->isVisible();
0052     if (m_infoboxVisible)
0053         m_viewer->m_infoBox->hide();
0054 
0055     // Stop playback
0056     m_wasPlaying = !m_viewer->m_videoDisplay->isPaused();
0057     if (m_wasPlaying)
0058         m_viewer->m_videoDisplay->playPause();
0059 
0060     // Wait a bit for the context menu to disapear
0061     QTimer::singleShot(200, this, &VideoShooter::doShoot);
0062 }
0063 
0064 void Viewer::VideoShooter::doShoot()
0065 {
0066     // Make the screenshot and save it
0067     const QImage image = m_viewer->m_videoDisplay->screenShoot();
0068     const DB::FileName fileName = m_info->fileName();
0069     MainWindow::Window::theMainWindow()->thumbnailCache()->removeThumbnail(fileName);
0070     BackgroundJobs::HandleVideoThumbnailRequestJob::saveFullScaleFrame(fileName, image);
0071 
0072     // Show the infobox again
0073     if (m_infoboxVisible)
0074         m_viewer->m_infoBox->show();
0075 
0076     // Restart the video
0077     if (m_wasPlaying)
0078         m_viewer->m_videoDisplay->playPause();
0079 
0080     qApp->restoreOverrideCursor();
0081 }
0082 // vi:expandtab:tabstop=4 shiftwidth=4:
0083 
0084 #include "moc_VideoShooter.cpp"