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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2022-04-02
0007  * Description : WEBP 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 "converttowebp.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 DigikamBqmConvertToWebpPlugin
0034 {
0035 
0036 ConvertToWEBP::ConvertToWEBP(QObject* const parent)
0037     : BatchTool(QLatin1String("ConvertToWEBP"), ConvertTool, parent),
0038       m_changeSettings(true)
0039 {
0040 }
0041 
0042 ConvertToWEBP::~ConvertToWEBP()
0043 {
0044 }
0045 
0046 BatchTool* ConvertToWEBP::clone(QObject* const parent) const
0047 {
0048     return new ConvertToWEBP(parent);
0049 }
0050 
0051 void ConvertToWEBP::registerSettingsWidget()
0052 {
0053     DImgLoaderSettings* const WEBPBox = DPluginLoader::instance()->exportWidget(QLatin1String("WEBP"));
0054 
0055     if (WEBPBox)
0056     {
0057         connect(WEBPBox, SIGNAL(signalSettingsChanged()),
0058                 this, SLOT(slotSettingsChanged()));
0059     }
0060 
0061     m_settingsWidget = WEBPBox;
0062 
0063     BatchTool::registerSettingsWidget();
0064 }
0065 
0066 BatchToolSettings ConvertToWEBP::defaultSettings()
0067 {
0068     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0069     KConfigGroup group        = config->group(QLatin1String("ImageViewer Settings"));
0070     int compression           = group.readEntry(QLatin1String("WEBPCompression"), 75);
0071     bool lossLessCompression  = group.readEntry(QLatin1String("WEBPLossLess"),    true);
0072     BatchToolSettings settings;
0073     settings.insert(QLatin1String("quality"),  compression);
0074     settings.insert(QLatin1String("lossless"), lossLessCompression);
0075 
0076     return settings;
0077 }
0078 
0079 void ConvertToWEBP::slotAssignSettings2Widget()
0080 {
0081     m_changeSettings                  = false;
0082     DImgLoaderSettings* const WEBPBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget);
0083 
0084     if (WEBPBox)
0085     {
0086         DImgLoaderPrms set;
0087         set.insert(QLatin1String("quality"),  settings()[QLatin1String("quality")].toInt());
0088         set.insert(QLatin1String("lossless"), settings()[QLatin1String("lossless")].toBool());
0089         WEBPBox->setSettings(set);
0090     }
0091 
0092     m_changeSettings                 = true;
0093 }
0094 
0095 void ConvertToWEBP::slotSettingsChanged()
0096 {
0097     if (m_changeSettings)
0098     {
0099         DImgLoaderSettings* const WEBPBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget);
0100 
0101         if (WEBPBox)
0102         {
0103             BatchToolSettings settings;
0104             settings.insert(QLatin1String("quality"),  WEBPBox->settings()[QLatin1String("quality")].toInt());
0105             settings.insert(QLatin1String("lossless"), WEBPBox->settings()[QLatin1String("lossless")].toBool());
0106             BatchTool::slotSettingsChanged(settings);
0107         }
0108     }
0109 }
0110 
0111 QString ConvertToWEBP::outputSuffix() const
0112 {
0113     return QLatin1String("webp");
0114 }
0115 
0116 bool ConvertToWEBP::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 DigikamBqmConvertToWebpPlugin
0130 
0131 #include "moc_converttowebp.cpp"