File indexing completed on 2024-05-12 04:19:36

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "animateddocumentloadedimpl.h"
0023 
0024 // Qt
0025 #include <QBuffer>
0026 #include <QImage>
0027 #include <QMovie>
0028 
0029 // KF
0030 
0031 // Local
0032 #include "gwenview_lib_debug.h"
0033 
0034 namespace Gwenview
0035 {
0036 struct AnimatedDocumentLoadedImplPrivate {
0037     QByteArray mRawData;
0038     QBuffer mMovieBuffer;
0039     QMovie mMovie;
0040 };
0041 
0042 AnimatedDocumentLoadedImpl::AnimatedDocumentLoadedImpl(Document *document, const QByteArray &rawData)
0043     : AbstractDocumentImpl(document)
0044     , d(new AnimatedDocumentLoadedImplPrivate)
0045 {
0046     d->mRawData = rawData;
0047 
0048     connect(&d->mMovie, &QMovie::frameChanged, this, &AnimatedDocumentLoadedImpl::slotFrameChanged);
0049 
0050     d->mMovieBuffer.setBuffer(&d->mRawData);
0051     d->mMovieBuffer.open(QIODevice::ReadOnly);
0052     d->mMovie.setDevice(&d->mMovieBuffer);
0053 }
0054 
0055 AnimatedDocumentLoadedImpl::~AnimatedDocumentLoadedImpl()
0056 {
0057     delete d;
0058 }
0059 
0060 void AnimatedDocumentLoadedImpl::init()
0061 {
0062     Q_EMIT isAnimatedUpdated();
0063     if (!document()->image().isNull()) {
0064         // We may reach this point without an image if the first frame got
0065         // downsampled by LoadingDocumentImpl (unlikely for now because the gif
0066         // io handler does not support the QImageIOHandler::ScaledSize option)
0067         Q_EMIT imageRectUpdated(document()->image().rect());
0068         Q_EMIT loaded();
0069     }
0070 }
0071 
0072 Document::LoadingState AnimatedDocumentLoadedImpl::loadingState() const
0073 {
0074     return Document::Loaded;
0075 }
0076 
0077 QByteArray AnimatedDocumentLoadedImpl::rawData() const
0078 {
0079     return d->mRawData;
0080 }
0081 
0082 void AnimatedDocumentLoadedImpl::slotFrameChanged(int /*frameNumber*/)
0083 {
0084     QImage image = d->mMovie.currentImage();
0085     setDocumentImage(image);
0086     Q_EMIT imageRectUpdated(image.rect());
0087 }
0088 
0089 bool AnimatedDocumentLoadedImpl::isAnimated() const
0090 {
0091     return true;
0092 }
0093 
0094 void AnimatedDocumentLoadedImpl::startAnimation()
0095 {
0096     d->mMovie.start();
0097     if (d->mMovie.state() == QMovie::NotRunning) {
0098         // This is true with qt-copy as of 2008.08.23
0099         // qCDebug(GWENVIEW_LIB_LOG) << "QMovie didn't start. This can happen in some cases when starting for the second time.";
0100         // qCDebug(GWENVIEW_LIB_LOG) << "Trying to start again, it usually fixes the bug.";
0101         d->mMovie.start();
0102     }
0103 }
0104 
0105 void AnimatedDocumentLoadedImpl::stopAnimation()
0106 {
0107     d->mMovie.stop();
0108 }
0109 
0110 } // namespace
0111 
0112 #include "moc_animateddocumentloadedimpl.cpp"