File indexing completed on 2025-01-19 03:55:43

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-09-13
0007  * Description : a widget to provide options to save image.
0008  *
0009  * SPDX-FileCopyrightText: 2006-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 "dsavesettingswidget.h"
0016 
0017 // Qt includes
0018 
0019 #include <QGridLayout>
0020 #include <QLabel>
0021 #include <QApplication>
0022 #include <QStyle>
0023 #include <QComboBox>
0024 
0025 // KDE includes
0026 
0027 #include <kconfiggroup.h>
0028 #include <klocalizedstring.h>
0029 
0030 namespace Digikam
0031 {
0032 
0033 class Q_DECL_HIDDEN DSaveSettingsWidget::Private
0034 {
0035 public:
0036 
0037     explicit Private()
0038       : formatLabel     (nullptr),
0039         grid            (nullptr),
0040         formatComboBox  (nullptr),
0041         conflictBox     (nullptr)
0042     {
0043     }
0044 
0045     QLabel*              formatLabel;
0046 
0047     QGridLayout*         grid;
0048 
0049     QComboBox*           formatComboBox;
0050 
0051     FileSaveConflictBox* conflictBox;
0052 };
0053 
0054 DSaveSettingsWidget::DSaveSettingsWidget(QWidget* const parent)
0055     : QWidget(parent),
0056       d      (new Private)
0057 {
0058     setAttribute(Qt::WA_DeleteOnClose);
0059 
0060     const int spacing = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0061                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0062 
0063     d->grid           = new QGridLayout(this);
0064     d->formatLabel    = new QLabel(i18n("Output file format:"), this);
0065     d->formatComboBox = new QComboBox(this);
0066     d->formatComboBox->setWhatsThis(i18n("<p>Set the output file format to use here:</p>"
0067                                          "<p><b>JPEG</b>: output the processed image in JPEG format. "
0068                                          "This format will give smaller-sized files.</p>"
0069                                          "<p><b>Warning: Due to the destructive compression algorithm, "
0070                                          "JPEG is a lossy quality format.</b></p>"
0071                                          "<p><b>TIFF</b>: output the processed image in TIFF format. "
0072                                          "This generates large files, without "
0073                                          "losing quality. Image is compressed.</p>"
0074                                          "<p><b>PNG</b>: output the processed image in PNG format. "
0075                                          "This generates large files, without "
0076                                          "losing quality. Image is compressed.</p>"
0077                                          "<p><b>PPM</b>: output the processed image in PPM format. "
0078                                          "This generates the largest files, without "
0079                                          "losing quality. Image is not compressed.</p>"));
0080     slotPopulateImageFormat(false);
0081 
0082     d->conflictBox    = new FileSaveConflictBox(this);
0083 
0084     d->grid->addWidget(d->formatLabel,    0, 0, 1, 1);
0085     d->grid->addWidget(d->formatComboBox, 0, 1, 1, 1);
0086     d->grid->addWidget(d->conflictBox,    1, 0, 1, 2);
0087     d->grid->setRowStretch(3, 10);
0088     d->grid->setContentsMargins(spacing, spacing, spacing, spacing);
0089     d->grid->setSpacing(spacing);
0090 
0091     connect(d->formatComboBox, SIGNAL(activated(int)),
0092             this, SIGNAL(signalSaveFormatChanged()));
0093 
0094     connect(d->conflictBox, SIGNAL(signalConflictButtonChanged(int)),
0095             this, SIGNAL(signalConflictButtonChanged(int)));
0096 }
0097 
0098 DSaveSettingsWidget::~DSaveSettingsWidget()
0099 {
0100     delete d;
0101 }
0102 
0103 void DSaveSettingsWidget::setCustomSettingsWidget(QWidget* const custom)
0104 {
0105     d->grid->addWidget(custom, 2, 0, 1, 2);
0106 }
0107 
0108 void DSaveSettingsWidget::resetToDefault()
0109 {
0110     setFileFormat(OUTPUT_PNG);
0111     d->conflictBox->resetToDefault();
0112 }
0113 
0114 DSaveSettingsWidget::OutputFormat DSaveSettingsWidget::fileFormat() const
0115 {
0116     return (OutputFormat)(d->formatComboBox->currentIndex());
0117 }
0118 
0119 void DSaveSettingsWidget::setFileFormat(OutputFormat f)
0120 {
0121     d->formatComboBox->setCurrentIndex((int)f);
0122 }
0123 
0124 FileSaveConflictBox::ConflictRule DSaveSettingsWidget::conflictRule() const
0125 {
0126     return d->conflictBox->conflictRule();
0127 }
0128 
0129 void DSaveSettingsWidget::setConflictRule(FileSaveConflictBox::ConflictRule r)
0130 {
0131     d->conflictBox->setConflictRule(r);
0132 }
0133 
0134 void DSaveSettingsWidget::readSettings(KConfigGroup& group)
0135 {
0136     setFileFormat((DSaveSettingsWidget::OutputFormat)group.readEntry("Output Format", (int)(DSaveSettingsWidget::OUTPUT_PNG)));
0137     d->conflictBox->readSettings(group);
0138 }
0139 
0140 void DSaveSettingsWidget::writeSettings(KConfigGroup& group)
0141 {
0142     group.writeEntry("Output Format", (int)fileFormat());
0143     d->conflictBox->writeSettings(group);
0144 }
0145 
0146 void DSaveSettingsWidget::slotPopulateImageFormat(bool sixteenBits)
0147 {
0148     d->formatComboBox->clear();
0149     d->formatComboBox->insertItem( OUTPUT_PNG,  QLatin1String("PNG") );
0150     d->formatComboBox->insertItem( OUTPUT_TIFF, QLatin1String("TIFF") );
0151 
0152     if (!sixteenBits)
0153     {
0154         d->formatComboBox->insertItem( OUTPUT_JPEG, QLatin1String("JPEG") );
0155         d->formatComboBox->insertItem( OUTPUT_PPM,  QLatin1String("PPM") );
0156     }
0157 
0158     Q_EMIT signalSaveFormatChanged();
0159 }
0160 
0161 QString DSaveSettingsWidget::extension() const
0162 {
0163     return extensionForFormat(fileFormat());
0164 }
0165 
0166 QString DSaveSettingsWidget::typeMime() const
0167 {
0168     QString mime;
0169 
0170     switch (fileFormat())
0171     {
0172         case OUTPUT_JPEG:
0173         {
0174             mime = QLatin1String("image/jpeg");
0175             break;
0176         }
0177 
0178         case OUTPUT_TIFF:
0179         {
0180             mime = QLatin1String("image/tiff");
0181             break;
0182         }
0183 
0184         case OUTPUT_PPM:
0185         {
0186             mime = QLatin1String("image/ppm");
0187             break;
0188         }
0189 
0190         case OUTPUT_PNG:
0191         {
0192             mime = QLatin1String("image/png");
0193             break;
0194         }
0195     }
0196 
0197     return mime;
0198 }
0199 
0200 QString DSaveSettingsWidget::extensionForFormat(DSaveSettingsWidget::OutputFormat format)
0201 {
0202     QString ext;
0203 
0204     switch (format)
0205     {
0206         case OUTPUT_JPEG:
0207         {
0208             ext = QLatin1String(".jpg");
0209             break;
0210         }
0211 
0212         case OUTPUT_TIFF:
0213         {
0214             ext = QLatin1String(".tif");
0215             break;
0216         }
0217 
0218         case OUTPUT_PPM:
0219         {
0220             ext = QLatin1String(".ppm");
0221             break;
0222         }
0223 
0224         case OUTPUT_PNG:
0225         {
0226             ext = QLatin1String(".png");
0227             break;
0228         }
0229     }
0230 
0231     return ext;
0232 }
0233 
0234 } // namespace Digikam
0235 
0236 #include "moc_dsavesettingswidget.cpp"