File indexing completed on 2024-04-28 04:18:50

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 "abstractimageoperation.h"
0023 
0024 // Qt
0025 #include <QTimer>
0026 #include <QUrl>
0027 
0028 // KF
0029 #include <KJob>
0030 
0031 // Local
0032 #include "document/documentfactory.h"
0033 #include "document/documentjob.h"
0034 
0035 namespace Gwenview
0036 {
0037 class ImageOperationCommand : public QUndoCommand
0038 {
0039 public:
0040     ImageOperationCommand(AbstractImageOperation *op)
0041         : mOp(op)
0042     {
0043     }
0044 
0045     ~ImageOperationCommand() override
0046     {
0047         delete mOp;
0048     }
0049 
0050     void undo() override
0051     {
0052         mOp->undo();
0053     }
0054 
0055     void redo() override
0056     {
0057         mOp->redo();
0058     }
0059 
0060 private:
0061     AbstractImageOperation *const mOp;
0062 };
0063 
0064 struct AbstractImageOperationPrivate {
0065     QString mText;
0066     QUrl mUrl;
0067     ImageOperationCommand *mCommand;
0068 };
0069 
0070 AbstractImageOperation::AbstractImageOperation()
0071     : d(new AbstractImageOperationPrivate)
0072 {
0073 }
0074 
0075 AbstractImageOperation::~AbstractImageOperation()
0076 {
0077     delete d;
0078 }
0079 
0080 void AbstractImageOperation::applyToDocument(Document::Ptr doc)
0081 {
0082     d->mUrl = doc->url();
0083 
0084     d->mCommand = new ImageOperationCommand(this);
0085     d->mCommand->setText(d->mText);
0086     // QUndoStack::push() executes command by calling its redo() function
0087     doc->undoStack()->push(d->mCommand);
0088 }
0089 
0090 Document::Ptr AbstractImageOperation::document() const
0091 {
0092     Document::Ptr doc = DocumentFactory::instance()->load(d->mUrl);
0093     doc->startLoadingFullImage();
0094     return doc;
0095 }
0096 
0097 void AbstractImageOperation::finish(bool ok)
0098 {
0099     if (ok) {
0100         // Give QUndoStack time to update in case the redo/undo is executed immediately
0101         // (e.g. undo crop just sets the previous image)
0102         QTimer::singleShot(0, document().data(), &Document::imageOperationCompleted);
0103     } else {
0104         // Remove command from undo stack without executing undo()
0105         d->mCommand->setObsolete(true);
0106         document()->undoStack()->undo();
0107     }
0108 }
0109 
0110 void AbstractImageOperation::finishFromKJob(KJob *job)
0111 {
0112     finish(job->error() == KJob::NoError);
0113 }
0114 
0115 void AbstractImageOperation::setText(const QString &text)
0116 {
0117     d->mText = text;
0118 }
0119 
0120 void AbstractImageOperation::redoAsDocumentJob(DocumentJob *job)
0121 {
0122     connect(job, &KJob::result, this, &AbstractImageOperation::finishFromKJob);
0123     document()->enqueueJob(job);
0124 }
0125 
0126 } // namespace
0127 
0128 #include "moc_abstractimageoperation.cpp"