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

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2007 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, Boston, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "documentloadedimpl.h"
0023 
0024 // Qt
0025 #include <QByteArray>
0026 #include <QImage>
0027 #include <QImageWriter>
0028 #include <QTransform>
0029 #include <QUrl>
0030 
0031 // KF
0032 
0033 // Local
0034 #include "documentjob.h"
0035 #include "gwenviewconfig.h"
0036 #include "imageutils.h"
0037 #include "savejob.h"
0038 
0039 namespace Gwenview
0040 {
0041 struct DocumentLoadedImplPrivate {
0042     QByteArray mRawData;
0043     bool mQuietInit;
0044 };
0045 
0046 DocumentLoadedImpl::DocumentLoadedImpl(Document *document, const QByteArray &rawData, bool quietInit)
0047     : AbstractDocumentImpl(document)
0048     , d(new DocumentLoadedImplPrivate)
0049 {
0050     if (document->keepRawData()) {
0051         d->mRawData = rawData;
0052     }
0053     d->mQuietInit = quietInit;
0054 }
0055 
0056 DocumentLoadedImpl::~DocumentLoadedImpl()
0057 {
0058     delete d;
0059 }
0060 
0061 void DocumentLoadedImpl::init()
0062 {
0063     if (!d->mQuietInit) {
0064         Q_EMIT imageRectUpdated(document()->image().rect());
0065         Q_EMIT loaded();
0066     }
0067 }
0068 
0069 bool DocumentLoadedImpl::isEditable() const
0070 {
0071     return true;
0072 }
0073 
0074 Document::LoadingState DocumentLoadedImpl::loadingState() const
0075 {
0076     return Document::Loaded;
0077 }
0078 
0079 bool DocumentLoadedImpl::saveInternal(QIODevice *device, const QByteArray &format)
0080 {
0081     QImageWriter writer(device, format);
0082     // If we're saving a non-JPEG image as a JPEG, respect the quality setting
0083     if (format == QByteArrayLiteral("jpeg") || format == QByteArrayLiteral("jxl") || format == QByteArrayLiteral("webp") || format == QByteArrayLiteral("avif")
0084         || format == QByteArrayLiteral("heif") || format == QByteArrayLiteral("heic")) {
0085         writer.setQuality(GwenviewConfig::jPEGQuality());
0086     }
0087     bool ok = writer.write(document()->image());
0088     if (ok) {
0089         setDocumentFormat(format);
0090     } else {
0091         setDocumentErrorString(writer.errorString());
0092     }
0093     return ok;
0094 }
0095 
0096 DocumentJob *DocumentLoadedImpl::save(const QUrl &url, const QByteArray &format)
0097 {
0098     return new SaveJob(this, url, format);
0099 }
0100 
0101 AbstractDocumentEditor *DocumentLoadedImpl::editor()
0102 {
0103     return this;
0104 }
0105 
0106 void DocumentLoadedImpl::setImage(const QImage &image)
0107 {
0108     setDocumentImage(image);
0109     Q_EMIT imageRectUpdated(image.rect());
0110 }
0111 
0112 void DocumentLoadedImpl::applyTransformation(Orientation orientation)
0113 {
0114     QImage image = document()->image();
0115     const QTransform matrix = ImageUtils::transformMatrix(orientation);
0116     image = image.transformed(matrix);
0117     setDocumentImage(image);
0118     Q_EMIT imageRectUpdated(image.rect());
0119 }
0120 
0121 QByteArray DocumentLoadedImpl::rawData() const
0122 {
0123     return d->mRawData;
0124 }
0125 
0126 } // namespace
0127 
0128 #include "moc_documentloadedimpl.cpp"