File indexing completed on 2025-01-19 03:56:58

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2017-06-04
0007  * Description : A label to show video frame effect preview
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "effectpreview.h"
0016 
0017 // Qt includes
0018 
0019 #include <QTimer>
0020 #include <QImage>
0021 #include <QPixmap>
0022 #include <QStandardPaths>
0023 
0024 // Local includes
0025 
0026 #include "frameutils.h"
0027 #include "digikam_debug.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 class Q_DECL_HIDDEN EffectPreview::Private
0033 {
0034 public:
0035 
0036     explicit Private()
0037       : mngr       (nullptr),
0038         curEffect  (EffectMngr::None),
0039         previewSize(QSize(192, 144))
0040     {
0041     }
0042 
0043     QTimer                 restartTimer;
0044     QTimer                 effTimer;
0045     EffectMngr*            mngr;
0046     EffectMngr::EffectType curEffect;
0047     QSize                  previewSize;
0048 };
0049 
0050 EffectPreview::EffectPreview(QWidget* const parent)
0051     : QLabel(parent),
0052       d     (new Private)
0053 {
0054     setFixedSize(d->previewSize);
0055     setContentsMargins(QMargins());
0056     setScaledContents(false);
0057     setOpenExternalLinks(false);
0058     setFocusPolicy(Qt::NoFocus);
0059     setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
0060 
0061     d->mngr = new EffectMngr;
0062     d->mngr->setOutputSize(d->previewSize);
0063 
0064     connect(&d->effTimer, SIGNAL(timeout()),
0065             this, SLOT(slotProgressEffect()));
0066 
0067     connect(&d->restartTimer, SIGNAL(timeout()),
0068             this, SLOT(slotRestart()));
0069 }
0070 
0071 EffectPreview::~EffectPreview()
0072 {
0073     delete d;
0074 }
0075 
0076 void EffectPreview::setImagesList(const QList<QUrl>& images)
0077 {
0078     if (!images.isEmpty())
0079     {
0080         d->mngr->setImage(FrameUtils::makeFramedImage(images[0].toLocalFile(), d->previewSize));
0081     }
0082     else
0083     {
0084         QImage sample = QImage(QStandardPaths::locate(QStandardPaths::GenericDataLocation,
0085                                                       QLatin1String("digikam/data/sample-aix.png")));
0086         d->mngr->setImage(sample.scaled(QSize(1024, 768), Qt::KeepAspectRatio));
0087     }
0088 }
0089 
0090 void EffectPreview::startPreview(EffectMngr::EffectType eff)
0091 {
0092     stopPreview();
0093     d->curEffect = eff;
0094     d->mngr->setEffect(eff);
0095     d->effTimer.start(50);
0096 }
0097 
0098 void EffectPreview::slotProgressEffect()
0099 {
0100     int tmout  = -1;
0101     QImage img = d->mngr->currentFrame(tmout);
0102     setPixmap(QPixmap::fromImage(img));
0103 
0104     if (tmout == -1)
0105     {
0106         stopPreview();
0107         d->restartTimer.start(1000);
0108     }
0109 }
0110 
0111 void EffectPreview::stopPreview()
0112 {
0113     d->effTimer.stop();
0114     d->restartTimer.stop();
0115 }
0116 
0117 void EffectPreview::slotRestart()
0118 {
0119     startPreview(d->curEffect);
0120 }
0121 
0122 } // namespace Digikam
0123 
0124 #include "moc_effectpreview.cpp"