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 2010 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 "documentjob.h"
0023 
0024 // Qt
0025 #include <QApplication>
0026 #include <QFuture>
0027 #include <QFutureWatcher>
0028 #include <QtConcurrentRun>
0029 
0030 // KF
0031 #include <KDialogJobUiDelegate>
0032 #include <KLocalizedString>
0033 
0034 // Local
0035 #include "gwenview_lib_debug.h"
0036 
0037 namespace Gwenview
0038 {
0039 struct DocumentJobPrivate {
0040     Document::Ptr mDoc;
0041 };
0042 
0043 DocumentJob::DocumentJob()
0044     : KCompositeJob(nullptr)
0045     , d(new DocumentJobPrivate)
0046 {
0047     auto delegate = new KDialogJobUiDelegate;
0048     delegate->setWindow(qApp->activeWindow());
0049     delegate->setAutoErrorHandlingEnabled(true);
0050     setUiDelegate(delegate);
0051 }
0052 
0053 DocumentJob::~DocumentJob()
0054 {
0055     delete d;
0056 }
0057 
0058 Document::Ptr DocumentJob::document() const
0059 {
0060     return d->mDoc;
0061 }
0062 
0063 void DocumentJob::setDocument(const Document::Ptr &doc)
0064 {
0065     d->mDoc = doc;
0066 }
0067 
0068 void DocumentJob::start()
0069 {
0070     QMetaObject::invokeMethod(this, &DocumentJob::doStart, Qt::QueuedConnection);
0071 }
0072 
0073 bool DocumentJob::checkDocumentEditor()
0074 {
0075     if (!document()->editor()) {
0076         setError(NoDocumentEditorError);
0077         setErrorText(i18nc("@info", "Gwenview cannot edit this kind of image."));
0078         return false;
0079     }
0080     return true;
0081 }
0082 
0083 void ThreadedDocumentJob::doStart()
0084 {
0085     QFuture<void> future = QtConcurrent::run(&ThreadedDocumentJob::threadedStart, this);
0086     auto watcher = new QFutureWatcher<void>(this);
0087     connect(watcher, SIGNAL(finished()), SLOT(emitResult()));
0088     watcher->setFuture(future);
0089 }
0090 
0091 } // namespace
0092 
0093 #include "moc_documentjob.cpp"