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 #ifndef DOCUMENTJOB_H
0022 #define DOCUMENTJOB_H
0023 
0024 #include <lib/gwenviewlib_export.h>
0025 
0026 // Qt
0027 
0028 // KF
0029 #include <KCompositeJob>
0030 
0031 // Local
0032 #include <lib/document/document.h>
0033 
0034 namespace Gwenview
0035 {
0036 struct DocumentJobPrivate;
0037 
0038 /**
0039  * Represent an asynchronous task to be executed on a document
0040  * The task must be created on the heap and passed to an instance of Document
0041  * via Document::enqueueJob()
0042  *
0043  * The task behavior must be implemented in run()
0044  *
0045  * Tasks are always started from the GUI thread, and are never parallelized.
0046  * You can of course use threading inside your task implementation to speed it
0047  * up.
0048  */
0049 class GWENVIEWLIB_EXPORT DocumentJob : public KCompositeJob
0050 {
0051     Q_OBJECT
0052 public:
0053     enum {
0054         NoDocumentEditorError = UserDefinedError + 1,
0055     };
0056     DocumentJob();
0057     ~DocumentJob() override;
0058 
0059     Document::Ptr document() const;
0060 
0061     void start() override;
0062 
0063 protected Q_SLOTS:
0064     /**
0065      * Implement this method to provide the task behavior.
0066      * You *must* emit the result() signal when your work is finished, but it
0067      * does not have to be finished when run() returns.
0068      * If you are not emitting it from the GUI thread, then use a queued
0069      * connection to emit it.
0070      */
0071     virtual void doStart() = 0;
0072 
0073     /**
0074      * slot-ification of emitResult()
0075      */
0076     void emitResult()
0077     {
0078         KJob::emitResult();
0079     }
0080 
0081 protected:
0082     /**
0083      * Convenience method which checks document()->editor() is valid
0084      * and set the job error properties if it's not.
0085      * @return true if the editor is valid.
0086      */
0087     bool checkDocumentEditor();
0088 
0089 private:
0090     void setDocument(const Document::Ptr &);
0091 
0092     DocumentJobPrivate *const d;
0093 
0094     friend class Document;
0095 };
0096 
0097 /**
0098  * A document job whose action is started in a separate thread
0099  */
0100 class ThreadedDocumentJob : public DocumentJob
0101 {
0102 public:
0103     /**
0104      * Must be reimplemented to apply the action to the document.
0105      * This method is never called from the GUI thread.
0106      */
0107     virtual void threadedStart() = 0;
0108 
0109 protected:
0110     void doStart() override;
0111 };
0112 
0113 } // namespace
0114 
0115 #endif /* DOCUMENTJOB_H */