File indexing completed on 2025-03-09 03:58:50

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-11-24
0007  * Description : Batch Tool Container.
0008  *
0009  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #ifndef DIGIKAM_BQM_BATCH_TOOL_H
0016 #define DIGIKAM_BQM_BATCH_TOOL_H
0017 
0018 // Qt includes
0019 
0020 #include <QObject>
0021 #include <QIcon>
0022 
0023 // Local includes
0024 
0025 #include "digikam_export.h"
0026 #include "drawdecodersettings.h"
0027 #include "dimg.h"
0028 #include "iteminfo.h"
0029 #include "queuesettings.h"
0030 #include "iofilesettings.h"
0031 
0032 class QWidget;
0033 
0034 namespace Digikam
0035 {
0036 
0037 class DImgBuiltinFilter;
0038 class DImgThreadedFilter;
0039 class DPluginBqm;
0040 
0041 /**
0042  * A map of batch tool settings (setting key, setting value).
0043  */
0044 typedef QMap<QString, QVariant> BatchToolSettings;
0045 
0046 class DIGIKAM_GUI_EXPORT BatchTool : public QObject
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051 
0052     enum BatchToolGroup
0053     {
0054         BaseTool = 0,             ///< digiKam core tools.
0055         CustomTool,               ///< List of tools grouped and customized by users.
0056 
0057         ColorTool,                ///< Tools to manage image colors (Curves, BCG, etc...)
0058         EnhanceTool,              ///< Tools to enhance images (NR, sharp, etc...)
0059         TransformTool,            ///< Tools to transform images geometry (resize, rotate, flip, etc...)
0060         DecorateTool,             ///< Tools to decorate images (Border, watermark, etc...)
0061         FiltersTool,              ///< Tools to apply filters and special effects (film grain, BlurFx, etc...)
0062         ConvertTool,              ///< Tools to convert images format (PNG, JPEG, TIFF, etc...)
0063         MetadataTool              ///< Tools to play with metadata.
0064     };
0065     Q_ENUM(BatchToolGroup)
0066 
0067 /**
0068  * Tool data and properties management. NOTE: these methods can be used safely in multi-threading part (ActionThread).
0069  */
0070 public:
0071 
0072     explicit BatchTool(const QString& name, BatchToolGroup group, QObject* const parent = nullptr);
0073     ~BatchTool() override;
0074 
0075     void setPlugin(DPluginBqm* const plugin);
0076     DPluginBqm* plugin()                                    const;
0077 
0078     /**
0079      * Get description of an error which appear during apply() method.
0080      */
0081     QString errorDescription()                              const;
0082 
0083     /**
0084      * Return group of tool. See BatchToolGroup enum for details.
0085      */
0086     BatchToolGroup toolGroup()                              const;
0087 
0088     /**
0089      * Return group of tool name as string.
0090      */
0091     QString toolGroupToString()                             const;
0092 
0093     /**
0094      * Manage Tool title.
0095      */
0096     void setToolTitle(const QString& toolTitle);
0097     QString toolTitle()                                     const;
0098 
0099     /**
0100      * Manage Tool description.
0101      */
0102     void setToolDescription(const QString& toolDescription);
0103     QString toolDescription()                               const;
0104 
0105     /**
0106      * Manage Tool icon name.
0107      */
0108     void setToolIconName(const QString& iconName);
0109     void setToolIcon(const QIcon& icon);
0110     QIcon toolIcon()                                        const;
0111 
0112     /**
0113      * Manage settings values to tool. See BatchToolSettings container for details.
0114      */
0115     void setSettings(const BatchToolSettings& settings);
0116     BatchToolSettings settings()                            const;
0117 
0118     /**
0119      * Manage current input url processed by this tool.
0120      */
0121     void setInputUrl(const QUrl& inputUrl);
0122     QUrl inputUrl()                                         const;
0123 
0124     /**
0125      * Manage current output url processed by this tool.
0126      */
0127     void setOutputUrl(const QUrl& outputUrl);
0128     QUrl outputUrl()                                        const;
0129 
0130     /**
0131      * Manage current working url used by this tool to process items.
0132      */
0133     void setWorkingUrl(const QUrl& workingUrl);
0134     QUrl workingUrl()                                       const;
0135 
0136     /**
0137      * Manage instance of current image data container loaded by this tool.
0138      */
0139     void setImageData(const DImg& img);
0140     DImg imageData()                                        const;
0141 
0142     /**
0143      * Manage instance of current image info loaded by this tool.
0144      */
0145     void setItemInfo(const ItemInfo& info);
0146     ItemInfo imageInfo()                                    const;
0147 
0148     /**
0149      * Manage flag properties to indicate if this tool is last one to process on current item.
0150      */
0151     void setLastChainedTool(bool last);
0152     bool isLastChainedTool()                                const;
0153 
0154     /**
0155      * Set output url using input url content + annotation based on time stamp + file
0156      * extension defined by outputSuffix().
0157      * if outputSuffix() return null, file extension is the same than original.
0158      */
0159     void setOutputUrlFromInputUrl();
0160 
0161     /**
0162      * Load image data using input Url set by setInputUrl() to instance of internal
0163      * DImg container.
0164      */
0165     bool loadToDImg()                                       const;
0166 
0167     /**
0168      * Save image data from instance of internal DImg container using :
0169      * - output Url set by setOutputUrl() or setOutputUrlFromInputUrl()
0170      * - output file format set by outputSuffix(). If this one is empty,
0171      *   format of original image is used instead.
0172      */
0173     bool savefromDImg()                                     const;
0174 
0175     /**
0176      * Set that the Exif orientation flag is allowed be reset to NORMAL after tool operation
0177      */
0178     void setResetExifOrientationAllowed(bool reset);
0179 
0180     /**
0181      * Returns true if the Exif orientation tag is allowed to be reset after tool operation
0182      */
0183     bool getResetExifOrientationAllowed()                   const;
0184 
0185     /**
0186      * Set that the Exif orientation flag should be reset to NORMAL after tool operation
0187      */
0188     void setNeedResetExifOrientation(bool reset);
0189 
0190     /**
0191      * Returns true if the Exif orientation tag should be reset after tool operation
0192      */
0193     bool getNeedResetExifOrientation()                      const;
0194 
0195     /**
0196      * Set that RAW files loading rule to use (demosaicing or JPEG embedded).
0197      */
0198     void setRawLoadingRules(QueueSettings::RawLoadingRule rule);
0199 
0200     /**
0201      * Sets if the history added by tools shall be made a branch (new version).
0202      */
0203     void setSaveAsNewVersion(bool fork = true);
0204 
0205     /**
0206      * Applies only when the file is actually saved on disk, and takes the history
0207      * since the loading from disk to set the first added step as creating a branch.
0208      */
0209     void setBranchHistory(bool branch = true);
0210     bool getBranchHistory()                                 const;
0211 
0212     /**
0213      * Set-up RAW decoding settings no use during tool operations.
0214      */
0215     void setDRawDecoderSettings(const DRawDecoderSettings& settings);
0216 
0217     /**
0218      * Return RAW decoding settings used during tool operations.
0219      */
0220     DRawDecoderSettings rawDecodingSettings()               const;
0221 
0222     /**
0223      * Set-up IOFile settings no use during tool operations.
0224      */
0225     void setIOFileSettings(const IOFileSettings& settings);
0226 
0227     /**
0228      * Return IOFile settings used during tool operations.
0229      */
0230     IOFileSettings ioFileSettings()                         const;
0231 
0232     /**
0233      * Apply all change to perform by this tool. This method call customized toolOperations().
0234      */
0235     bool apply();
0236 
0237     /**
0238      * Return version of tool. By default, ID is 1. Re-implement this method and increase this ID when tool settings change.
0239      */
0240     virtual int toolVersion()                               const
0241     {
0242         return 1;
0243     };
0244 
0245     /**
0246      * Re-implement this method is you want customize cancellation of tool, for ex. to call
0247      * a dedicated method to kill sub-threads parented to this tool instance.
0248      * Unforget to call parent BatchTool::cancel() method in your customized implementation.
0249      */
0250     virtual void cancel();
0251 
0252     /**
0253      * Re-implement this method if tool change file extension during batch process (ex: "png").
0254      * Typically, this is used with tool which convert to new file format.
0255      * This method return and empty string by default.
0256      */
0257     virtual QString outputSuffix()                          const;
0258 
0259     /**
0260      * Re-implement this method to initialize Settings Widget value with default settings.
0261      */
0262     virtual BatchToolSettings defaultSettings()                   = 0;
0263 
0264     /**
0265      * Clone this tool without to create settings widget.
0266      * It's a safe construction of tools instance used in multithreading (ActionThread) to process items in parallel.
0267      */
0268     virtual BatchTool* clone(QObject* const parent = nullptr) const = 0;
0269 
0270 Q_SIGNALS:
0271 
0272     void signalSettingsChanged(const BatchToolSettings&);
0273     void signalVisible(bool);
0274 
0275 public Q_SLOTS:
0276 
0277     void slotResetSettingsToDefault();
0278     void slotSettingsChanged(const BatchToolSettings& settings);
0279 
0280 protected:
0281 
0282     /**
0283      * Method to check if file pointed by url is a RAW image
0284      */
0285     bool isRawFile(const QUrl& url)                         const;
0286 
0287     /**
0288      * Set string to describe an error which appear during apply() method.
0289      */
0290     void setErrorDescription(const QString& errmsg);
0291 
0292     /**
0293      * Return a reference of internal DImg container used to modify image data.
0294      */
0295     DImg& image()                                           const;
0296 
0297     /**
0298      * Return true if cancel() have been called. Use this method to stop loop in your toolOperations() implementation.
0299      */
0300     bool isCancelled()                                      const;
0301 
0302     /**
0303      * Use this if you have a filter ready to run.
0304      * Will call startFilterDirectly and apply the result to image().
0305      */
0306     void applyFilter(DImgThreadedFilter* const filter);
0307     void applyFilterChangedProperties(DImgThreadedFilter* const filter);
0308     void applyFilter(DImgBuiltinFilter* const filter);
0309 
0310     /**
0311      * Re-implement this method to customize all batch operations done by this tool.
0312      * This method is called by apply().
0313      */
0314     virtual bool toolOperations() = 0;
0315 
0316 protected Q_SLOTS:
0317 
0318     virtual void slotSettingsChanged() = 0;
0319 
0320 /// Settings widget management. NOTE: do not use these methods in multi-threading part (ActionThread), only in main thread (GUI)
0321 
0322 public:
0323 
0324     /**
0325      * Return dedicated settings widget registered with registerSettingsWidget().
0326      */
0327     QWidget* settingsWidget()                               const;
0328 
0329     /**
0330      * Delete dedicated settings widget registered with registerSettingsWidget().
0331      */
0332     void deleteSettingsWidget();
0333 
0334     /**
0335      * Setup dedicated settings widget. Default implementation assign no settings view (a message label is just displayed).
0336      * You need to call default implementation in your child class to init default signals and slots connections,
0337      * after to have instanced your dedicated settings widget.
0338      */
0339     virtual void registerSettingsWidget();
0340 
0341 Q_SIGNALS:
0342 
0343     /**
0344      * Only used internally. See registerSettingsWidget() implementation.
0345      */
0346     void signalAssignSettings2Widget();
0347 
0348 protected:
0349 
0350     /**
0351      * Host settings widget instance.
0352      */
0353     QWidget* m_settingsWidget;
0354 
0355 protected Q_SLOTS:
0356 
0357     /**
0358      * Re-implement this method to customize how all settings values must be assigned to settings widget.
0359      * This method is called by setSettings() through signalAssignSettings2Widget().
0360      */
0361     virtual void slotAssignSettings2Widget() = 0;
0362 
0363 /// Private section
0364 
0365 public:
0366 
0367     // Declared as public due to BatchToolObserver class.
0368     class Private;
0369 
0370 private:
0371 
0372     Private* const d;
0373 };
0374 
0375 } // namespace Digikam
0376 
0377 #endif // DIGIKAM_BQM_BATCH_TOOL_H