1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2017-06-04
7  * Description : A label to show transition preview
8  *
9  * Copyright (C) 2017-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "transitionpreview.h"
25 
26 // Qt includes
27 
28 #include <QTimer>
29 #include <QImage>
30 #include <QPixmap>
31 #include <QStandardPaths>
32 
33 // Local includes
34 
35 #include "frameutils.h"
36 #include "digikam_debug.h"
37 
38 namespace Digikam
39 {
40 
41 class Q_DECL_HIDDEN TransitionPreview::Private
42 {
43 public:
44 
Private()45     explicit Private()
46       : mngr         (nullptr),
47         curTransition(TransitionMngr::None),
48         previewSize  (QSize(192, 144))
49     {
50     }
51 
52     QTimer                    restartTimer;
53     QTimer                    transTimer;
54     TransitionMngr*           mngr;
55     TransitionMngr::TransType curTransition;
56     QSize                     previewSize;
57 };
58 
TransitionPreview(QWidget * const parent)59 TransitionPreview::TransitionPreview(QWidget* const parent)
60     : QLabel(parent),
61       d     (new Private)
62 {
63     setFixedSize(d->previewSize);
64     setContentsMargins(QMargins());
65     setScaledContents(false);
66     setOpenExternalLinks(false);
67     setFocusPolicy(Qt::NoFocus);
68     setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
69 
70     d->mngr = new TransitionMngr;
71     d->mngr->setOutputSize(d->previewSize);
72 
73     connect(&d->transTimer, SIGNAL(timeout()),
74             this, SLOT(slotProgressTransition()));
75 
76     connect(&d->restartTimer, SIGNAL(timeout()),
77             this, SLOT(slotRestart()));
78 }
79 
~TransitionPreview()80 TransitionPreview::~TransitionPreview()
81 {
82     delete d;
83 }
84 
setImagesList(const QList<QUrl> & images)85 void TransitionPreview::setImagesList(const QList<QUrl>& images)
86 {
87     if (!images.isEmpty())
88     {
89         d->mngr->setInImage(FrameUtils::makeFramedImage(images[0].toLocalFile(), d->previewSize));
90 
91         if (images.count() > 1)
92         {
93             d->mngr->setOutImage(FrameUtils::makeFramedImage(images[1].toLocalFile(), d->previewSize));
94         }
95         else
96         {
97             QImage blank(d->previewSize, QImage::Format_ARGB32);
98             blank.fill(Qt::black);
99             d->mngr->setOutImage(blank);
100         }
101     }
102     else
103     {
104         QImage sample = QImage(QStandardPaths::locate(QStandardPaths::GenericDataLocation,
105                                                       QLatin1String("digikam/data/sample-aix.png")));
106         d->mngr->setInImage(sample);
107 
108         QImage blank(d->previewSize, QImage::Format_ARGB32);
109         blank.fill(Qt::black);
110         d->mngr->setOutImage(blank);
111     }
112 }
113 
startPreview(TransitionMngr::TransType eff)114 void TransitionPreview::startPreview(TransitionMngr::TransType eff)
115 {
116     stopPreview();
117     d->curTransition = eff;
118     d->mngr->setTransition(eff);
119     d->transTimer.start(100);
120 }
121 
slotProgressTransition()122 void TransitionPreview::slotProgressTransition()
123 {
124     int tmout  = -1;
125     QImage img = d->mngr->currentFrame(tmout);
126     setPixmap(QPixmap::fromImage(img));
127 
128     if (tmout == -1)
129     {
130         stopPreview();
131         d->restartTimer.start(1000);
132     }
133 }
134 
stopPreview()135 void TransitionPreview::stopPreview()
136 {
137     d->transTimer.stop();
138     d->restartTimer.stop();
139 }
140 
slotRestart()141 void TransitionPreview::slotRestart()
142 {
143     startPreview(d->curTransition);
144 }
145 
146 } // namespace Digikam
147