File indexing completed on 2025-01-19 03:50:53

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