1 /* ============================================================
2  *
3  * This file is a part of KDE project
4  *
5  *
6  * Date        : 2006-09-13
7  * Description : a widget to provide options to save image.
8  *
9  * Copyright (C) 2006-2018 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * ============================================================ */
22 
23 #include "kpsavesettingswidget.h"
24 
25 // Qt includes
26 
27 #include <QButtonGroup>
28 #include <QGridLayout>
29 #include <QLabel>
30 #include <QRadioButton>
31 #include <QVBoxLayout>
32 #include <QApplication>
33 #include <QStyle>
34 #include <QComboBox>
35 
36 // KDE includes
37 
38 #include <kconfiggroup.h>
39 #include <klocalizedstring.h>
40 
41 namespace KIPIPlugins
42 {
43 
44 class KPSaveSettingsWidget::Private
45 {
46 public:
47 
Private()48     Private()
49     {
50         formatLabel         = nullptr;
51         conflictLabel       = nullptr;
52         conflictButtonGroup = nullptr;
53         formatComboBox      = nullptr;
54         overwriteButton     = nullptr;
55         promptButton        = nullptr;
56         grid                = nullptr;
57     }
58 
59     QLabel*       formatLabel;
60     QLabel*       conflictLabel;
61 
62     QGridLayout*  grid;
63 
64     QButtonGroup* conflictButtonGroup;
65 
66     QComboBox*    formatComboBox;
67 
68     QRadioButton* overwriteButton;
69     QRadioButton* promptButton;
70 };
71 
KPSaveSettingsWidget(QWidget * const parent)72 KPSaveSettingsWidget::KPSaveSettingsWidget(QWidget* const parent)
73     : QWidget(parent),
74       d(new Private)
75 {
76     setAttribute(Qt::WA_DeleteOnClose);
77 
78     const int spacing = QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
79 
80     d->grid           = new QGridLayout(this);
81     d->formatLabel    = new QLabel(i18n("Output file format:"), this);
82     d->formatComboBox = new QComboBox( this );
83     d->formatComboBox->setWhatsThis(i18n("<p>Set the output file format to use here:</p>"
84                                          "<p><b>JPEG</b>: output the processed image in JPEG format. "
85                                          "This format will give smaller-sized files.</p>"
86                                          "<p><b>Warning: Due to the destructive compression algorithm, "
87                                          "JPEG is a lossy quality format.</b></p>"
88                                          "<p><b>TIFF</b>: output the processed image in TIFF format. "
89                                          "This generates large files, without "
90                                          "losing quality. Image is compressed.</p>"
91                                          "<p><b>PNG</b>: output the processed image in PNG format. "
92                                          "This generates large files, without "
93                                          "losing quality. Image is compressed.</p>"
94                                          "<p><b>PPM</b>: output the processed image in PPM format. "
95                                          "This generates the largest files, without "
96                                          "losing quality. Image is not compressed.</p>"));
97     slotPopulateImageFormat(false);
98 
99     d->conflictLabel           = new QLabel(i18n("If Target File Exists:"), this);
100     QWidget* const conflictBox = new QWidget(this);
101     QVBoxLayout* const vlay    = new QVBoxLayout(conflictBox);
102     d->conflictButtonGroup     = new QButtonGroup(conflictBox);
103     d->overwriteButton         = new QRadioButton(i18n("Overwrite automatically"), conflictBox);
104     d->promptButton            = new QRadioButton(i18n("Open rename-file dialog"), conflictBox);
105     d->conflictButtonGroup->addButton(d->overwriteButton, OVERWRITE);
106     d->conflictButtonGroup->addButton(d->promptButton,    ASKTOUSER);
107     d->conflictButtonGroup->setExclusive(true);
108     d->overwriteButton->setChecked(true);
109 
110     vlay->setContentsMargins(spacing, spacing, spacing, spacing);
111     vlay->setSpacing(spacing);
112     vlay->addWidget(d->overwriteButton);
113     vlay->addWidget(d->promptButton);
114 
115     d->grid->addWidget(d->formatLabel,    0, 0, 1, 1);
116     d->grid->addWidget(d->formatComboBox, 0, 1, 1, 1);
117     d->grid->addWidget(d->conflictLabel,  1, 0, 1, 2);
118     d->grid->addWidget(conflictBox,       2, 0, 1, 2);
119     d->grid->setRowStretch(4, 10);
120     d->grid->setContentsMargins(spacing, spacing, spacing, spacing);
121     d->grid->setSpacing(spacing);
122 
123     connect(d->formatComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
124             this, &KPSaveSettingsWidget::signalSaveFormatChanged);
125 
126     connect(d->conflictButtonGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
127             this, &KPSaveSettingsWidget::signalConflictButtonChanged);
128 }
129 
~KPSaveSettingsWidget()130 KPSaveSettingsWidget::~KPSaveSettingsWidget()
131 {
132     delete d;
133 }
134 
setCustomSettingsWidget(QWidget * const custom)135 void KPSaveSettingsWidget::setCustomSettingsWidget(QWidget* const custom)
136 {
137     d->grid->addWidget(custom, 3, 0, 1, 2);
138 }
139 
resetToDefault()140 void KPSaveSettingsWidget::resetToDefault()
141 {
142     setFileFormat(OUTPUT_PNG);
143     setConflictRule(OVERWRITE);
144 }
145 
fileFormat() const146 KPSaveSettingsWidget::OutputFormat KPSaveSettingsWidget::fileFormat() const
147 {
148     return(OutputFormat)(d->formatComboBox->currentIndex());
149 }
150 
setFileFormat(OutputFormat f)151 void KPSaveSettingsWidget::setFileFormat(OutputFormat f)
152 {
153     d->formatComboBox->setCurrentIndex((int)f);
154 }
155 
setPromptButtonText(const QString & str)156 void KPSaveSettingsWidget::setPromptButtonText(const QString& str)
157 {
158     d->promptButton->setText(str);
159 }
160 
conflictRule() const161 KPSaveSettingsWidget::ConflictRule KPSaveSettingsWidget::conflictRule() const
162 {
163     return((ConflictRule)(d->conflictButtonGroup->checkedId()));
164 }
165 
setConflictRule(ConflictRule r)166 void KPSaveSettingsWidget::setConflictRule(ConflictRule r)
167 {
168     d->conflictButtonGroup->button((int)r)->setChecked(true);
169 }
170 
readSettings(KConfigGroup & group)171 void KPSaveSettingsWidget::readSettings(KConfigGroup& group)
172 {
173     setFileFormat((KPSaveSettingsWidget::OutputFormat)group.readEntry("Output Format", (int)(KPSaveSettingsWidget::OUTPUT_PNG)));
174     setConflictRule((KPSaveSettingsWidget::ConflictRule)group.readEntry("Conflict",    (int)(KPSaveSettingsWidget::OVERWRITE)));
175 }
176 
writeSettings(KConfigGroup & group)177 void KPSaveSettingsWidget::writeSettings(KConfigGroup& group)
178 {
179     group.writeEntry("Output Format", (int)fileFormat());
180     group.writeEntry("Conflict",      (int)conflictRule());
181 }
182 
slotPopulateImageFormat(bool sixteenBits)183 void KPSaveSettingsWidget::slotPopulateImageFormat(bool sixteenBits)
184 {
185     d->formatComboBox->clear();
186     d->formatComboBox->insertItem( OUTPUT_PNG,  QString::fromLatin1("PNG") );
187     d->formatComboBox->insertItem( OUTPUT_TIFF, QString::fromLatin1("TIFF") );
188 
189     if (!sixteenBits)
190     {
191         d->formatComboBox->insertItem( OUTPUT_JPEG, QString::fromLatin1("JPEG") );
192         d->formatComboBox->insertItem( OUTPUT_PPM,  QString::fromLatin1("PPM") );
193     }
194 
195     emit signalSaveFormatChanged();
196 }
197 
extension() const198 QString KPSaveSettingsWidget::extension() const
199 {
200     return extensionForFormat(fileFormat());
201 }
202 
typeMime() const203 QString KPSaveSettingsWidget::typeMime() const
204 {
205     QString mime;
206 
207     switch(fileFormat())
208     {
209         case OUTPUT_JPEG:
210             mime = QString::fromLatin1("image/jpeg");
211             break;
212         case OUTPUT_TIFF:
213             mime = QString::fromLatin1("image/tiff");
214             break;
215         case OUTPUT_PPM:
216             mime = QString::fromLatin1("image/ppm");
217             break;
218         case OUTPUT_PNG:
219             mime = QString::fromLatin1("image/png");
220             break;
221     }
222 
223     return mime;
224 }
225 
extensionForFormat(KPSaveSettingsWidget::OutputFormat format)226 QString KPSaveSettingsWidget::extensionForFormat(KPSaveSettingsWidget::OutputFormat format)
227 {
228     QString ext;
229 
230     switch(format)
231     {
232         case OUTPUT_JPEG:
233             ext = QString::fromLatin1(".jpg");
234             break;
235         case OUTPUT_TIFF:
236             ext = QString::fromLatin1(".tif");
237             break;
238         case OUTPUT_PPM:
239             ext = QString::fromLatin1(".ppm");
240             break;
241         case OUTPUT_PNG:
242             ext = QString::fromLatin1(".png");
243             break;
244     }
245 
246     return ext;
247 }
248 
249 } // namespace KIPIPlugins
250