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        : 2019-09-02
0007  * Description : HEIF image Converter batch tool.
0008  *
0009  * SPDX-FileCopyrightText: 2020-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 #include "converttoheif.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 DigikamBqmConvertToHeifPlugin
0034 {
0035 
0036 ConvertToHEIF::ConvertToHEIF(QObject* const parent)
0037     : BatchTool(QLatin1String("ConvertToHEIF"), ConvertTool, parent),
0038       m_changeSettings(true)
0039 {
0040 }
0041 
0042 ConvertToHEIF::~ConvertToHEIF()
0043 {
0044 }
0045 
0046 BatchTool* ConvertToHEIF::clone(QObject* const parent) const
0047 {
0048     return new ConvertToHEIF(parent);
0049 }
0050 
0051 void ConvertToHEIF::registerSettingsWidget()
0052 {
0053     DImgLoaderSettings* const HEIFBox = DPluginLoader::instance()->exportWidget(QLatin1String("HEIF"));
0054 
0055     connect(HEIFBox, SIGNAL(signalSettingsChanged()),
0056             this, SLOT(slotSettingsChanged()));
0057 
0058     m_settingsWidget            = HEIFBox;
0059 
0060     BatchTool::registerSettingsWidget();
0061 }
0062 
0063 BatchToolSettings ConvertToHEIF::defaultSettings()
0064 {
0065     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0066     KConfigGroup group        = config->group(QLatin1String("ImageViewer Settings"));
0067     int compression           = group.readEntry(QLatin1String("HEIFCompression"), 3);
0068     bool lossLessCompression  = group.readEntry(QLatin1String("HEIFLossLess"),    true);
0069     BatchToolSettings settings;
0070     settings.insert(QLatin1String("quality"),  compression);
0071     settings.insert(QLatin1String("lossless"), lossLessCompression);
0072 
0073     return settings;
0074 }
0075 
0076 void ConvertToHEIF::slotAssignSettings2Widget()
0077 {
0078     m_changeSettings = false;
0079 
0080     DImgLoaderSettings* const HEIFBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget);
0081 
0082     if (HEIFBox)
0083     {
0084         DImgLoaderPrms set;
0085         set.insert(QLatin1String("quality"),  settings()[QLatin1String("quality")].toInt());
0086         set.insert(QLatin1String("lossless"), settings()[QLatin1String("lossless")].toBool());
0087         HEIFBox->setSettings(set);
0088     }
0089 
0090     m_changeSettings = true;
0091 }
0092 
0093 void ConvertToHEIF::slotSettingsChanged()
0094 {
0095     if (m_changeSettings)
0096     {
0097         DImgLoaderSettings* const HEIFBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget);
0098 
0099         if (HEIFBox)
0100         {
0101             BatchToolSettings settings;
0102             settings.insert(QLatin1String("quality"),  HEIFBox->settings()[QLatin1String("quality")].toInt());
0103             settings.insert(QLatin1String("lossless"), HEIFBox->settings()[QLatin1String("lossless")].toBool());
0104             BatchTool::slotSettingsChanged(settings);
0105         }
0106     }
0107 }
0108 
0109 QString ConvertToHEIF::outputSuffix() const
0110 {
0111     return QLatin1String("heif");
0112 }
0113 
0114 bool ConvertToHEIF::toolOperations()
0115 {
0116     if (!loadToDImg())
0117     {
0118         return false;
0119     }
0120 
0121     bool lossless = settings()[QLatin1String("lossless")].toBool();
0122     image().setAttribute(QLatin1String("quality"), lossless ? 0 : settings()[QLatin1String("quality")].toInt());
0123 
0124     return (savefromDImg());
0125 }
0126 
0127 } // namespace DigikamBqmConvertToHeifPlugin
0128 
0129 #include "moc_converttoheif.cpp"