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 : 2008-11-24 0007 * Description : JPEG image Converter batch tool. 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 #include "converttojpeg.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 "dimgloader.h" 0032 #include "dpluginloader.h" 0033 0034 namespace DigikamBqmConvertToJpegPlugin 0035 { 0036 0037 ConvertToJPEG::ConvertToJPEG(QObject* const parent) 0038 : BatchTool(QLatin1String("ConvertToJPEG"), ConvertTool, parent), 0039 m_changeSettings(true) 0040 { 0041 } 0042 0043 ConvertToJPEG::~ConvertToJPEG() 0044 { 0045 } 0046 0047 BatchTool* ConvertToJPEG::clone(QObject* const parent) const 0048 { 0049 return new ConvertToJPEG(parent); 0050 } 0051 0052 void ConvertToJPEG::registerSettingsWidget() 0053 { 0054 DImgLoaderSettings* const JPGBox = DPluginLoader::instance()->exportWidget(QLatin1String("JPEG")); 0055 0056 connect(JPGBox, SIGNAL(signalSettingsChanged()), 0057 this, SLOT(slotSettingsChanged())); 0058 0059 m_settingsWidget = JPGBox; 0060 0061 BatchTool::registerSettingsWidget(); 0062 } 0063 0064 BatchToolSettings ConvertToJPEG::defaultSettings() 0065 { 0066 KSharedConfig::Ptr config = KSharedConfig::openConfig(); 0067 KConfigGroup group = config->group(QLatin1String("ImageViewer Settings")); 0068 int compression = group.readEntry(QLatin1String("JPEGCompression"), 75); 0069 int subSampling = group.readEntry(QLatin1String("JPEGSubSampling"), 1); // Medium subsampling 0070 BatchToolSettings settings; 0071 settings.insert(QLatin1String("quality"), compression); 0072 settings.insert(QLatin1String("subsampling"), subSampling); 0073 0074 return settings; 0075 } 0076 0077 void ConvertToJPEG::slotAssignSettings2Widget() 0078 { 0079 m_changeSettings = false; 0080 0081 DImgLoaderSettings* const JPGBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget); 0082 0083 if (JPGBox) 0084 { 0085 DImgLoaderPrms set; 0086 set.insert(QLatin1String("quality"), settings()[QLatin1String("quality")].toInt()); 0087 set.insert(QLatin1String("subsampling"), settings()[QLatin1String("subsampling")].toInt()); 0088 JPGBox->setSettings(set); 0089 } 0090 0091 m_changeSettings = true; 0092 } 0093 0094 void ConvertToJPEG::slotSettingsChanged() 0095 { 0096 if (m_changeSettings) 0097 { 0098 DImgLoaderSettings* const JPGBox = dynamic_cast<DImgLoaderSettings*>(m_settingsWidget); 0099 0100 if (JPGBox) 0101 { 0102 BatchToolSettings settings; 0103 settings.insert(QLatin1String("quality"), JPGBox->settings()[QLatin1String("quality")].toInt()); 0104 settings.insert(QLatin1String("subsampling"), JPGBox->settings()[QLatin1String("subsampling")].toInt()); 0105 BatchTool::slotSettingsChanged(settings); 0106 } 0107 } 0108 } 0109 0110 QString ConvertToJPEG::outputSuffix() const 0111 { 0112 return QLatin1String("jpg"); 0113 } 0114 0115 bool ConvertToJPEG::toolOperations() 0116 { 0117 if (!loadToDImg()) 0118 { 0119 return false; 0120 } 0121 0122 int JPEGCompression = DImgLoader::convertCompressionForLibJpeg(settings()[QLatin1String("quality")].toInt()); 0123 image().setAttribute(QLatin1String("quality"), JPEGCompression); 0124 image().setAttribute(QLatin1String("subsampling"), settings()[QLatin1String("subsampling")].toInt()); 0125 0126 return (savefromDImg()); 0127 } 0128 0129 } // namespace DigikamBqmConvertToJpegPlugin 0130 0131 #include "moc_converttojpeg.cpp"