1 /* This file is part of the KDE project
2 
3     SPDX-FileCopyrightText: 2001, 2003 Lukas Tinkl <lukas@kde.org>
4     SPDX-FileCopyrightText: Andreas Schlapbach <schlpbch@iam.unibe.ch>
5 
6     SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "imgallerydialog.h"
10 
11 #include <QComboBox>
12 #include <QCheckBox>
13 #include <QLineEdit>
14 #include <QSpinBox>
15 #include <QFormLayout>
16 #include <QFontDatabase>
17 #include <QSpinBox>
18 
19 #include <klocalizedstring.h>
20 #include <kcolorbutton.h>
21 #include <kurlrequester.h>
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kguiitem.h>
25 #include <kfontchooser.h>
26 #include <kwidgetsaddons_version.h>
27 
KIGPDialog(QWidget * parent,const QString & path)28 KIGPDialog::KIGPDialog(QWidget *parent, const QString &path)
29     : KPageDialog(parent)
30 {
31     setStandardButtons(QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
32     buttonBox()->button(QDialogButtonBox::Ok)->setDefault(true);
33     setModal(true);
34     setFaceType(List);
35 
36     m_path = path;
37     setWindowTitle(i18nc("@title:window", "Create Image Gallery"));
38     KGuiItem::assign(buttonBox()->button(QDialogButtonBox::Ok), KGuiItem(i18n("Create"), QStringLiteral("imagegallery")));
39     m_config = new KConfig(QStringLiteral("kimgallerypluginrc"), KConfig::NoGlobals);
40     setupLookPage(path);
41     setupDirectoryPage(path);
42     setupThumbnailPage(path);
43     connect(buttonBox()->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &KIGPDialog::slotDefault);
44 }
45 
slotDefault()46 void KIGPDialog::slotDefault()
47 {
48     m_title->setText(i18n("Image Gallery for %1", m_path));
49     m_imagesPerRow->setValue(4);
50     m_imageName->setChecked(true);
51     m_imageSize->setChecked(false);
52     m_imageProperty->setChecked(false);
53     m_fontName->setItemText(m_fontName->currentIndex(), QFontDatabase::systemFont(QFontDatabase::GeneralFont).family());
54     m_fontSize->setValue(14);
55     m_foregroundColor->setColor(QColor(QStringLiteral("#d0ffd0")));
56     m_backgroundColor->setColor(QColor(QStringLiteral("#333333")));
57 
58     m_imageNameReq->setUrl(QUrl::fromLocalFile(m_path + "images.html"));
59     m_recurseSubDir->setChecked(false);
60     m_recursionLevel->setEnabled(false);
61     m_recursionLevel->setValue(0);
62     m_copyOriginalFiles->setChecked(false);
63     m_useCommentFile->setChecked(false);
64     m_commentFileReq->setUrl(QUrl::fromLocalFile(m_path + "comments"));
65     m_commentFileReq->setEnabled(false);
66 
67     m_imageFormat->setItemText(m_imageFormat->currentIndex(), QStringLiteral("JPEG"));
68     m_thumbnailSize->setValue(140);
69     m_colorDepthSet->setChecked(false);
70     m_colorDepth->setItemText(m_colorDepth->currentIndex(), QStringLiteral("8"));
71 }
72 
setupLookPage(const QString & path)73 void KIGPDialog::setupLookPage(const QString &path)
74 {
75     QFrame *page = new QFrame();
76     KPageWidgetItem *pageItem = new KPageWidgetItem(page, i18n("Look"));
77     pageItem->setHeader(i18n("Page Look"));
78     pageItem->setIcon(QIcon::fromTheme("fill-color"));
79     addPage(pageItem);
80 
81     KConfigGroup look = m_config->group("Look");
82     QFormLayout *form = new QFormLayout(page);
83 
84     m_title = new QLineEdit(i18n("Image Gallery for %1", path), page);
85     form->addRow(i18n("&Page title:"), m_title);
86 
87     m_imagesPerRow = new QSpinBox(page);
88     m_imagesPerRow->setMinimumWidth(100);
89     m_imagesPerRow->setRange(1, 8);
90     m_imagesPerRow->setSingleStep(1);
91     m_imagesPerRow->setValue(look.readEntry("ImagesPerRow", 4));
92     //m_imagesPerRow->setSliderEnabled(true);
93     form->addRow(i18n("I&mages per row:"), m_imagesPerRow);
94 
95     m_imageName = new QCheckBox(i18n("Show image file &name"), page);
96     m_imageName->setChecked(look.readEntry("ImageName", true));
97     form->addRow(QString(), m_imageName);
98 
99     m_imageSize = new QCheckBox(i18n("Show image file &size"), page);
100     m_imageSize->setChecked(look.readEntry("ImageSize", false));
101     form->addRow(QString(), m_imageSize);
102 
103     m_imageProperty = new QCheckBox(i18n("Show image &dimensions"), page);
104     m_imageProperty->setChecked(look.readEntry("ImageProperty", false));
105     form->addRow(QString(), m_imageProperty);
106 
107     m_fontName = new QComboBox(page);
108     m_fontName->setSizePolicy(QSizePolicy::MinimumExpanding, m_fontName->sizePolicy().verticalPolicy());
109 
110 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 86, 0)
111     const QStringList standardFonts = KFontChooser::createFontList(KFontChooser::NoDisplayFlags);
112 #else
113     QStringList standardFonts;
114     KFontChooser::getFontList(standardFonts, 0);
115 #endif
116 
117     m_fontName->addItems(standardFonts);
118     m_fontName->setItemText(m_fontName->currentIndex(), look.readEntry("FontName", QFontDatabase::systemFont(QFontDatabase::GeneralFont).family()));
119     form->addRow(i18n("Fon&t name:"), m_fontName);
120 
121     m_fontSize = new QSpinBox(page);
122     m_fontSize->setMinimumWidth(100);
123     m_fontSize->setMinimum(6);
124     m_fontSize->setMaximum(15);
125     m_fontSize->setSingleStep(1);
126     m_fontSize->setValue(look.readEntry("FontSize", 14));
127     form->addRow(i18n("Font si&ze:"), m_fontSize);
128 
129     m_foregroundColor = new KColorButton(page);
130     m_foregroundColor->setColor(QColor(look.readEntry("ForegroundColor", "#d0ffd0")));
131     form->addRow(i18n("&Foreground color:"), m_foregroundColor);
132 
133     m_backgroundColor = new KColorButton(page);
134     m_backgroundColor->setColor(QColor(look.readEntry("BackgroundColor", "#333333")));
135     form->addRow(i18n("&Background color:"), m_backgroundColor);
136 }
137 
setupDirectoryPage(const QString & path)138 void KIGPDialog::setupDirectoryPage(const QString &path)
139 {
140     QFrame *page = new QFrame();
141     KPageWidgetItem *pageItem = new KPageWidgetItem(page, i18n("Folders"));
142     pageItem->setHeader(i18n("Folders"));
143     pageItem->setIcon(QIcon::fromTheme("folder"));
144     addPage(pageItem);
145 
146     KConfigGroup group =  m_config->group("Directory");
147     QFormLayout *form = new QFormLayout(page);
148 
149     m_imageNameReq = new KUrlRequester(QUrl::fromLocalFile(QString(path + "images.html")), page);
150     form->addRow(i18n("&Save to HTML file:"), m_imageNameReq);
151     connect(m_imageNameReq, &KUrlRequester::textChanged, this, &KIGPDialog::imageUrlChanged);
152     m_imageNameReq->setToolTip(i18n("<p>The name of the HTML file this gallery will be saved to.</p>"));
153 
154     const bool recurseSubDir = group.readEntry("RecurseSubDirectories", false);
155     m_recurseSubDir = new QCheckBox(i18n("&Recurse subfolders"), page);
156     m_recurseSubDir->setChecked(recurseSubDir);
157     m_recurseSubDir->setToolTip(i18n("<p>Whether subfolders should be included for the "
158                                        "image gallery creation or not.</p>"));
159     form->addRow(QString(), m_recurseSubDir);
160 
161     const int recursionLevel = group.readEntry("RecursionLevel", 0);
162     m_recursionLevel = new QSpinBox(page);
163     m_recursionLevel->setMinimumWidth(100);
164     m_recursionLevel->setRange(0, 99);
165     m_recursionLevel->setSingleStep(1);
166     m_recursionLevel->setValue(recursionLevel);
167     //m_recursionLevel->setSliderEnabled(true);
168     m_recursionLevel->setSpecialValueText(i18n("Endless"));
169     m_recursionLevel->setEnabled(recurseSubDir);
170     m_recursionLevel->setToolTip(i18n("<p>You can limit the number of folders the "
171                                         "image gallery creator will traverse to by setting an "
172                                         "upper bound for the recursion depth.</p>"));
173     form->addRow(i18n("Rec&ursion depth:"), m_recursionLevel);
174 
175     connect(m_recurseSubDir, &QAbstractButton::toggled, m_recursionLevel, &QWidget::setEnabled);
176 
177     m_copyOriginalFiles = new QCheckBox(i18n("Copy or&iginal files"), page);
178     m_copyOriginalFiles->setChecked(group.readEntry("CopyOriginalFiles", false));
179     m_copyOriginalFiles->setToolTip(i18n("<p>This makes a copy of all images and the gallery will refer "
180                                            "to these copies instead of the original images.</p>"));
181     form->addRow(QString(), m_copyOriginalFiles);
182 
183     const bool useCommentFile = group.readEntry("UseCommentFile", false);
184     m_useCommentFile = new QCheckBox(i18n("Use &comment file"), page);
185     m_useCommentFile->setChecked(useCommentFile);
186     m_useCommentFile->setToolTip(i18n("<p>If you enable this option you can specify "
187                                         "a comment file which will be used for generating "
188                                         "subtitles for the images.</p>"
189                                         "<p>For details about the file format please see "
190                                         "the \"What's This?\" help below.</p>"));
191     form->addRow(QString(), m_useCommentFile);
192 
193     m_commentFileReq = new KUrlRequester(QUrl::fromLocalFile(QString(path + "comments")), page);
194     m_commentFileReq->setEnabled(useCommentFile);
195     m_commentFileReq->setToolTip(i18n("<p>You can specify the name of the comment file here. "
196                                         "The comment file contains the subtitles for the images. "
197                                         "The format of this file is:</p>"
198                                         "<p>FILENAME1:"
199                                         "<br />Description"
200                                         "<br />"
201                                         "<br />FILENAME2:"
202                                         "<br />Description"
203                                         "<br />"
204                                         "<br />and so on</p>"));
205     form->addRow(i18n("Comments &file:"), m_commentFileReq);
206 
207     connect(m_useCommentFile, &QAbstractButton::toggled, m_commentFileReq, &QWidget::setEnabled);
208 }
209 
setupThumbnailPage(const QString & path)210 void KIGPDialog::setupThumbnailPage(const QString &path)
211 {
212     Q_UNUSED(path);
213 
214     QFrame *page = new QFrame();
215     KPageWidgetItem *pageItem = new KPageWidgetItem(page, i18n("Thumbnails"));
216     pageItem->setHeader(i18n("Thumbnails"));
217     pageItem->setIcon(QIcon::fromTheme("view-preview"));
218     addPage(pageItem);
219 
220     KConfigGroup group =  m_config->group("Thumbnails");
221     QFormLayout *form = new QFormLayout(page);
222 
223     m_imageFormat = new QComboBox(page);
224     m_imageFormat->setSizePolicy(QSizePolicy::MinimumExpanding, m_imageFormat->sizePolicy().verticalPolicy());
225     QStringList lstImgageFormat;
226     lstImgageFormat << QStringLiteral("JPEG") << QStringLiteral("PNG");
227     m_imageFormat->addItems(lstImgageFormat);
228     m_imageFormat->setItemText(m_imageFormat->currentIndex(), group.readEntry("ImageFormat", "JPEG"));
229     form->addRow(i18n("Image f&ormat:"), m_imageFormat);
230 
231     m_thumbnailSize = new QSpinBox(page);
232     m_thumbnailSize->setMinimumWidth(100);
233     m_thumbnailSize->setRange(10, 1000);
234     m_thumbnailSize->setSingleStep(1);
235     m_thumbnailSize->setValue(group.readEntry("ThumbnailSize", 140));
236     //m_thumbnailSize->setSliderEnabled(true);
237     form->addRow(i18n("Thumbnail size:"), m_thumbnailSize);
238 
239     const bool colorDepthSet = group.readEntry("ColorDepthSet", false);
240     m_colorDepthSet = new QCheckBox(i18n("&Set different color depth:"), page);
241     m_colorDepthSet->setChecked(colorDepthSet);
242     form->addRow(QString(), m_colorDepthSet);
243 
244     m_colorDepth = new QComboBox(page);
245     m_colorDepth->setSizePolicy(QSizePolicy::MinimumExpanding, m_colorDepth->sizePolicy().verticalPolicy());
246     QStringList lst;
247     lst << QStringLiteral("1") << QStringLiteral("8") << QStringLiteral("16") << QStringLiteral("32");
248     m_colorDepth->addItems(lst);
249     m_colorDepth->setCurrentIndex(m_colorDepth->findText(group.readEntry("ColorDepth", "8")));
250     m_colorDepth->setEnabled(colorDepthSet);
251     form->addRow(i18n("Color depth:"), m_colorDepth);
252 
253     connect(m_colorDepthSet, &QAbstractButton::toggled, m_colorDepth, &QWidget::setEnabled);
254 }
255 
writeConfig()256 void KIGPDialog::writeConfig()
257 {
258     KConfigGroup group = m_config->group("Look");
259     group.writeEntry("ImagesPerRow", getImagesPerRow());
260     group.writeEntry("ImageName", printImageName());
261     group.writeEntry("ImageSize", printImageSize());
262     group.writeEntry("ImageProperty", printImageProperty());
263     group.writeEntry("FontName", getFontName());
264     group.writeEntry("FontSize", getFontSize());
265     group.writeEntry("ForegroundColor", getForegroundColor().name());
266     group.writeEntry("BackgroundColor", getBackgroundColor().name());
267 
268     group = m_config->group("Directory");
269     group.writeEntry("RecurseSubDirectories", recurseSubDirectories());
270     group.writeEntry("RecursionLevel", recursionLevel());
271     group.writeEntry("CopyOriginalFiles", copyOriginalFiles());
272     group.writeEntry("UseCommentFile", useCommentFile());
273 
274     group = m_config->group("Thumbnails");
275     group.writeEntry("ThumbnailSize", getThumbnailSize());
276     group.writeEntry("ColorDepth", getColorDepth());
277     group.writeEntry("ColorDepthSet", colorDepthSet());
278     group.writeEntry("ImageFormat", getImageFormat());
279     group.sync();
280 }
281 
~KIGPDialog()282 KIGPDialog::~KIGPDialog()
283 {
284     delete m_config;
285 }
286 
imageUrlChanged(const QString & url)287 void KIGPDialog::imageUrlChanged(const QString &url)
288 {
289     buttonBox()->button(QDialogButtonBox::Ok)->setEnabled(!url.isEmpty());
290 }
291 
printImageName() const292 bool  KIGPDialog::printImageName()  const
293 {
294     return m_imageName->isChecked();
295 }
296 
printImageSize() const297 bool  KIGPDialog::printImageSize() const
298 {
299     return m_imageSize->isChecked();
300 }
301 
printImageProperty() const302 bool  KIGPDialog::printImageProperty() const
303 {
304     return m_imageProperty->isChecked();
305 }
306 
recurseSubDirectories() const307 bool KIGPDialog::recurseSubDirectories() const
308 {
309     return m_recurseSubDir->isChecked();
310 }
311 
recursionLevel() const312 int KIGPDialog::recursionLevel() const
313 {
314     return m_recursionLevel->value();
315 }
316 
copyOriginalFiles() const317 bool KIGPDialog::copyOriginalFiles() const
318 {
319     return m_copyOriginalFiles->isChecked();
320 }
321 
useCommentFile() const322 bool KIGPDialog::useCommentFile() const
323 {
324     return m_useCommentFile->isChecked();
325 }
326 
getImagesPerRow() const327 int KIGPDialog::getImagesPerRow() const
328 {
329     return m_imagesPerRow->value();
330 }
331 
getThumbnailSize() const332 int KIGPDialog::getThumbnailSize() const
333 {
334     return m_thumbnailSize->value();
335 }
336 
getColorDepth() const337 int KIGPDialog::getColorDepth() const
338 {
339     return m_colorDepth->currentText().toInt();
340 }
341 
colorDepthSet() const342 bool KIGPDialog::colorDepthSet() const
343 {
344     return m_colorDepthSet->isChecked();
345 }
346 
getTitle() const347 const QString KIGPDialog::getTitle() const
348 {
349     return m_title->text();
350 }
351 
getImageUrl() const352 const QUrl KIGPDialog::getImageUrl() const
353 {
354     return m_imageNameReq->url();
355 }
356 
getCommentFile() const357 const QString KIGPDialog::getCommentFile() const
358 {
359     return m_commentFileReq->url().toLocalFile();
360 }
361 
getFontName() const362 const QString KIGPDialog::getFontName() const
363 {
364     return m_fontName->currentText();
365 }
366 
getFontSize() const367 const QString KIGPDialog::getFontSize() const
368 {
369     return m_fontSize->text();
370 }
371 
getBackgroundColor() const372 const QColor KIGPDialog::getBackgroundColor() const
373 {
374     return m_backgroundColor->color();
375 }
376 
getForegroundColor() const377 const QColor KIGPDialog::getForegroundColor() const
378 {
379     return m_foregroundColor->color();
380 }
381 
getImageFormat() const382 const QString KIGPDialog::getImageFormat() const
383 {
384     return m_imageFormat->currentText();
385 }
386