File indexing completed on 2025-01-05 03:57:25

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 transition 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 "transitionpreview.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 TransitionPreview::Private
0033 {
0034 public:
0035 
0036     explicit Private()
0037       : mngr         (nullptr),
0038         curTransition(TransitionMngr::None),
0039         previewSize  (QSize(192, 144))
0040     {
0041     }
0042 
0043     QTimer                    restartTimer;
0044     QTimer                    transTimer;
0045     TransitionMngr*           mngr;
0046     TransitionMngr::TransType curTransition;
0047     QSize                     previewSize;
0048 };
0049 
0050 TransitionPreview::TransitionPreview(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 TransitionMngr;
0062     d->mngr->setOutputSize(d->previewSize);
0063 
0064     connect(&d->transTimer, SIGNAL(timeout()),
0065             this, SLOT(slotProgressTransition()));
0066 
0067     connect(&d->restartTimer, SIGNAL(timeout()),
0068             this, SLOT(slotRestart()));
0069 }
0070 
0071 TransitionPreview::~TransitionPreview()
0072 {
0073     delete d;
0074 }
0075 
0076 void TransitionPreview::setImagesList(const QList<QUrl>& images)
0077 {
0078     if (!images.isEmpty())
0079     {
0080         d->mngr->setInImage(FrameUtils::makeFramedImage(images[0].toLocalFile(), d->previewSize));
0081 
0082         if (images.count() > 1)
0083         {
0084             d->mngr->setOutImage(FrameUtils::makeFramedImage(images[1].toLocalFile(), d->previewSize));
0085         }
0086         else
0087         {
0088             QImage blank(d->previewSize, QImage::Format_ARGB32);
0089             blank.fill(Qt::black);
0090             d->mngr->setOutImage(blank);
0091         }
0092     }
0093     else
0094     {
0095         QImage sample = QImage(QStandardPaths::locate(QStandardPaths::GenericDataLocation,
0096                                                       QLatin1String("digikam/data/sample-aix.png")));
0097         d->mngr->setInImage(sample);
0098 
0099         QImage blank(d->previewSize, QImage::Format_ARGB32);
0100         blank.fill(Qt::black);
0101         d->mngr->setOutImage(blank);
0102     }
0103 }
0104 
0105 void TransitionPreview::startPreview(TransitionMngr::TransType eff)
0106 {
0107     stopPreview();
0108     d->curTransition = eff;
0109     d->mngr->setTransition(eff);
0110     d->transTimer.start(100);
0111 }
0112 
0113 void TransitionPreview::slotProgressTransition()
0114 {
0115     int tmout  = -1;
0116     QImage img = d->mngr->currentFrame(tmout);
0117     setPixmap(QPixmap::fromImage(img));
0118 
0119     if (tmout == -1)
0120     {
0121         stopPreview();
0122         d->restartTimer.start(1000);
0123     }
0124 }
0125 
0126 void TransitionPreview::stopPreview()
0127 {
0128     d->transTimer.stop();
0129     d->restartTimer.stop();
0130 }
0131 
0132 void TransitionPreview::slotRestart()
0133 {
0134     startPreview(d->curTransition);
0135 }
0136 
0137 } // namespace Digikam
0138 
0139 #include "moc_transitionpreview.cpp"