File indexing completed on 2025-01-05 03:51:26

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2022-04-02
0007  * Description : JXL image Converter batch tool.
0008  *
0009  * SPDX-FileCopyrightText: 2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "converttojxl.h"
0016 
0017 // Qt includes
0018 
0019 #include <QFileInfo>
0020 #include <QWidget>
0021 
0022 // KDE includes
0023 
0024 #include <kconfiggroup.h>
0025 #include <klocalizedstring.h>
0026 #include <ksharedconfig.h>
0027 
0028 // Local includes
0029 
0030 #include "dimg.h"
0031 #include "dpluginloader.h"
0032 
0033 namespace DigikamBqmConvertToJxlPlugin
0034 {
0035 
0036 ConvertToJXL::ConvertToJXL(QObject* const parent)
0037     : BatchTool(QLatin1String("ConvertToJXL"), ConvertTool, parent),
0038       m_changeSettings(true)
0039 {
0040 }
0041 
0042 ConvertToJXL::~ConvertToJXL()
0043 {
0044 }
0045 
0046 BatchTool* ConvertToJXL::clone(QObject* const parent) const
0047 {
0048     return new ConvertToJXL(parent);
0049 }
0050 
0051 void ConvertToJXL::registerSettingsWidget()
0052 {
0053     DImgLoaderSettings* const JXLBox = DPluginLoader::instance()->exportWidget(QLatin1String("JXL"));
0054 
0055     if (JXLBox)
0056     {
0057         connect(JXLBox, SIGNAL(signalSettingsChanged()),
0058                 this, SLOT(slotSettingsChanged()));
0059     }
0060 
0061     m_settingsWidget = JXLBox;
0062 
0063     BatchTool::registerSettingsWidget();
0064 }
0065 
0066 BatchToolSettings ConvertToJXL::defaultSettings()
0067 {
0068     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0069     KConfigGroup group        = config->group(QLatin1String("ImageViewer Settings"));
0070     int compression           = group.readEntry(QLatin1String("JXLCompression"), 75);
0071     bool lossLessCompression  = group.readEntry(QLatin1String("JXLLossLess"),    true);
0072     BatchToolSettings settings;
0073     settings.insert(QLatin1String("quality"),  compression);
0074     settings.insert(QLatin1String("lossless"), lossLessCompression);
0075 
0076     return settings;
0077 }
0078 
0079 void ConvertToJXL::slotAssignSettings2Widget()
0080 {
0081     m_changeSettings                 = false;
0082     DImgLoaderSettings* const JXLBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget);
0083 
0084     if (JXLBox)
0085     {
0086         DImgLoaderPrms set;
0087         set.insert(QLatin1String("quality"),  settings()[QLatin1String("quality")].toInt());
0088         set.insert(QLatin1String("lossless"), settings()[QLatin1String("lossless")].toBool());
0089         JXLBox->setSettings(set);
0090     }
0091 
0092     m_changeSettings                 = true;
0093 }
0094 
0095 void ConvertToJXL::slotSettingsChanged()
0096 {
0097     if (m_changeSettings)
0098     {
0099         DImgLoaderSettings* const JXLBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget);
0100 
0101         if (JXLBox)
0102         {
0103             BatchToolSettings settings;
0104             settings.insert(QLatin1String("quality"),  JXLBox->settings()[QLatin1String("quality")].toInt());
0105             settings.insert(QLatin1String("lossless"), JXLBox->settings()[QLatin1String("lossless")].toBool());
0106             BatchTool::slotSettingsChanged(settings);
0107         }
0108     }
0109 }
0110 
0111 QString ConvertToJXL::outputSuffix() const
0112 {
0113     return QLatin1String("jxl");
0114 }
0115 
0116 bool ConvertToJXL::toolOperations()
0117 {
0118     if (!loadToDImg())
0119     {
0120         return false;
0121     }
0122 
0123     bool lossless = settings()[QLatin1String("lossless")].toBool();
0124     image().setAttribute(QLatin1String("quality"), lossless ? 100 : settings()[QLatin1String("quality")].toInt());
0125 
0126     return (savefromDImg());
0127 }
0128 
0129 } // namespace DigikamBqmConvertToJxlPlugin
0130 
0131 #include "moc_converttojxl.cpp"