1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "documentloadedimpl.h"
30 #include <QImageWriter>
35 #include <kio/netaccess.h>
37 #include <ksavefile.h>
38 #include <ktemporaryfile.h>
42 #include "imageutils.h"
47 struct DocumentLoadedImplPrivate
{
52 DocumentLoadedImpl::DocumentLoadedImpl(Document
* document
, const QByteArray
& rawData
)
53 : AbstractDocumentImpl(document
)
54 , d(new DocumentLoadedImplPrivate
) {
55 if (document
->keepRawData()) {
56 d
->mRawData
= rawData
;
61 DocumentLoadedImpl::~DocumentLoadedImpl() {
66 void DocumentLoadedImpl::init() {
70 bool DocumentLoadedImpl::isEditable() const {
75 bool DocumentLoadedImpl::isMetaInfoLoaded() const {
80 Document::LoadingState
DocumentLoadedImpl::loadingState() const {
81 return Document::Loaded
;
85 bool DocumentLoadedImpl::saveInternal(QIODevice
* device
, const QByteArray
& format
) {
86 QImageWriter
writer(device
, format
);
87 bool ok
= writer
.write(document()->image());
89 setDocumentFormat(format
);
91 setDocumentErrorString(writer
.errorString());
97 bool DocumentLoadedImpl::save(const KUrl
& url
, const QByteArray
& format
) {
100 // This tmp is used to save to remote urls.
101 // It's an auto_ptr, this way it's not instantiated for local urls, but
102 // for remote urls we are sure it will remove its file when we leave the
104 std::auto_ptr
<KTemporaryFile
> tmp
;
106 if (url
.isLocalFile()) {
107 fileName
= url
.path();
109 tmp
.reset(new KTemporaryFile
);
110 tmp
->setAutoRemove(true);
112 fileName
= tmp
->fileName();
115 KSaveFile
file(fileName
);
119 dirUrl
.setFileName(QString());
120 setDocumentErrorString(i18nc("@info", "Could not open file for writing, check that you have the necessary rights in <filename>%1</filename>.", dirUrl
.pathOrUrl()));
124 if (!saveInternal(&file
, format
)) {
129 if (!file
.finalize()) {
130 setDocumentErrorString(i18nc("@info", "Could not overwrite file, check that you have the necessary rights to write in <filename>%1</filename>.", url
.pathOrUrl()));
134 if (!url
.isLocalFile()) {
135 if (!KIO::NetAccess::upload(fileName
, url
, 0)) {
136 setDocumentErrorString(i18nc("@info", "Could not upload file."));
145 AbstractDocumentEditor
* DocumentLoadedImpl::editor() {
150 void DocumentLoadedImpl::setImage(const QImage
& image
) {
151 setDocumentImage(image
);
152 imageRectUpdated(image
.rect());
156 void DocumentLoadedImpl::applyTransformation(Orientation orientation
) {
157 QImage image
= document()->image();
158 QMatrix matrix
= ImageUtils::transformMatrix(orientation
);
159 image
= image
.transformed(matrix
);
160 setDocumentImage(image
);
161 imageRectUpdated(image
.rect());
165 QByteArray
DocumentLoadedImpl::rawData() const {