File indexing completed on 2025-04-27 03:58:07

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-01-20
0007  * Description : image file IO threaded interface.
0008  *
0009  * SPDX-FileCopyrightText: 2005-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2005-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_LOAD_SAVE_TASK_H
0017 #define DIGIKAM_LOAD_SAVE_TASK_H
0018 
0019 // Qt includes
0020 
0021 #include <QList>
0022 #include <QEvent>
0023 
0024 // Local includes
0025 
0026 #include "dimg.h"
0027 #include "dimgloaderobserver.h"
0028 #include "loadingdescription.h"
0029 #include "loadingcache.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 class LoadSaveThread;
0035 
0036 class LoadSaveTask
0037 {
0038 public:
0039 
0040     enum TaskType
0041     {
0042         TaskTypeLoading,
0043         TaskTypeSaving
0044     };
0045 
0046 public:
0047 
0048     explicit LoadSaveTask(LoadSaveThread* const thread);
0049     virtual ~LoadSaveTask();
0050 
0051 public:
0052 
0053     virtual void execute()                    = 0;
0054     virtual TaskType type()                   = 0;
0055 
0056     virtual void progressInfo(float progress) = 0;
0057     virtual bool continueQuery()              = 0;
0058 
0059 protected:
0060 
0061     LoadSaveThread* m_thread;
0062 
0063 private:
0064 
0065     // Disable
0066     LoadSaveTask(const LoadSaveTask&)            = delete;
0067     LoadSaveTask& operator=(const LoadSaveTask&) = delete;
0068 };
0069 
0070 //---------------------------------------------------------------------------------------------------
0071 
0072 class LoadingTask : public LoadSaveTask,
0073                     public DImgLoaderObserver
0074 {
0075 public:
0076 
0077     enum LoadingTaskStatus
0078     {
0079         LoadingTaskStatusLoading,
0080         LoadingTaskStatusPreloading,
0081         LoadingTaskStatusStopping
0082     };
0083 
0084 public:
0085 
0086     explicit LoadingTask(LoadSaveThread* const thread,
0087                          const LoadingDescription& description,
0088                          LoadingTaskStatus loadingTaskStatus = LoadingTaskStatusLoading);
0089     ~LoadingTask() override;
0090 
0091     LoadingTaskStatus status()                     const;
0092     QString filePath()                             const;
0093 
0094     const LoadingDescription& loadingDescription() const;
0095 
0096     // LoadSaveTask
0097 
0098     void execute()                    override;
0099     TaskType type()                   override;
0100 
0101     // DImgLoaderObserver
0102 
0103     void progressInfo(float progress) override;
0104     bool continueQuery()              override;
0105 
0106     void setStatus(LoadingTaskStatus status);
0107 
0108 protected:
0109 
0110     LoadingDescription         m_loadingDescription;
0111     volatile LoadingTaskStatus m_loadingTaskStatus;
0112 
0113 private:
0114 
0115     // Disable
0116     LoadingTask(const LoadingTask&)            = delete;
0117     LoadingTask& operator=(const LoadingTask&) = delete;
0118 };
0119 
0120 //---------------------------------------------------------------------------------------------------
0121 
0122 class SharedLoadingTask : public LoadingTask,
0123                           public LoadingProcess,
0124                           public LoadingProcessListener
0125 {
0126 public:
0127 
0128     explicit SharedLoadingTask(LoadSaveThread* const thread,
0129                                const LoadingDescription& description,
0130                                LoadSaveThread::AccessMode mode = LoadSaveThread::AccessModeReadWrite,
0131                                LoadingTaskStatus loadingTaskStatus = LoadingTaskStatusLoading);
0132 
0133     void execute()                                                      override;
0134     void progressInfo(float progress)                                   override;
0135 
0136     bool needsPostProcessing()              const;
0137     virtual void postProcess();
0138 
0139     // LoadingProcess
0140 
0141     bool completed()                        const                       override;
0142     QString cacheKey()                      const                       override;
0143     void addListener(LoadingProcessListener* const listener)            override;
0144     void removeListener(LoadingProcessListener* const listener)         override;
0145     void notifyNewLoadingProcess(LoadingProcess* const process,
0146                                  const LoadingDescription& description) override;
0147 
0148     // LoadingProcessListener
0149 
0150     bool querySendNotifyEvent()             const                       override;
0151     void setResult(const LoadingDescription& loadingDescription,
0152                    const DImg& img)                                     override;
0153     LoadSaveNotifier* loadSaveNotifier()    const                       override;
0154     LoadSaveThread::AccessMode accessMode() const                       override;
0155 
0156     DImg img()                              const;
0157 
0158 protected:
0159 
0160     volatile bool                  m_completed;
0161     LoadSaveThread::AccessMode     m_accessMode;
0162     QList<LoadingProcessListener*> m_listeners;
0163     DImg                           m_img;
0164 
0165 private:
0166 
0167     // Disable
0168     SharedLoadingTask(const SharedLoadingTask&)            = delete;
0169     SharedLoadingTask& operator=(const SharedLoadingTask&) = delete;
0170 };
0171 
0172 //---------------------------------------------------------------------------------------------------
0173 
0174 class SavingTask : public LoadSaveTask,
0175                    public DImgLoaderObserver
0176 {
0177 public:
0178 
0179     enum SavingTaskStatus
0180     {
0181         SavingTaskStatusSaving,
0182         SavingTaskStatusStopping
0183     };
0184 
0185 public:
0186 
0187     explicit SavingTask(LoadSaveThread* const thread,
0188                         const DImg& img,
0189                         const QString& filePath,
0190                         const QString& format);
0191 
0192     SavingTaskStatus status() const;
0193     QString filePath()        const;
0194 
0195 public:
0196 
0197     void execute()                    override;
0198     TaskType type()                   override;
0199 
0200     void progressInfo(float progress) override;
0201     bool continueQuery()              override;
0202 
0203     void setStatus(SavingTaskStatus status);
0204 
0205 private:
0206 
0207     QString                   m_filePath;
0208     QString                   m_format;
0209     DImg                      m_img;
0210     volatile SavingTaskStatus m_savingTaskStatus;
0211 
0212 private:
0213 
0214     // Disable
0215     SavingTask(const SavingTask&)            = delete;
0216     SavingTask& operator=(const SavingTask&) = delete;
0217 };
0218 
0219 } // namespace Digikam
0220 
0221 #endif // DIGIKAM_LOAD_SAVE_TASK_H