File indexing completed on 2024-04-14 04:52:54

0001 /* This file is part of the KDE project
0002 
0003     SPDX-FileCopyrightText: 2001, 2003 Lukas Tinkl <lukas@kde.org>
0004     SPDX-FileCopyrightText: Andreas Schlapbach <schlpbch@iam.unibe.ch>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include "imgallerydialog.h"
0010 
0011 #include <QComboBox>
0012 #include <QCheckBox>
0013 #include <QLineEdit>
0014 #include <QSpinBox>
0015 #include <QFormLayout>
0016 #include <QFontDatabase>
0017 #include <QSpinBox>
0018 
0019 #include <klocalizedstring.h>
0020 #include <kcolorbutton.h>
0021 #include <kurlrequester.h>
0022 #include <kconfig.h>
0023 #include <kconfiggroup.h>
0024 #include <kguiitem.h>
0025 #include <kfontchooser.h>
0026 #include <kwidgetsaddons_version.h>
0027 
0028 KIGPDialog::KIGPDialog(QWidget *parent, const QString &path)
0029     : KPageDialog(parent)
0030 {
0031     setStandardButtons(QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0032     buttonBox()->button(QDialogButtonBox::Ok)->setDefault(true);
0033     setModal(true);
0034     setFaceType(List);
0035 
0036     m_path = path;
0037     setWindowTitle(i18nc("@title:window", "Create Image Gallery"));
0038     KGuiItem::assign(buttonBox()->button(QDialogButtonBox::Ok), KGuiItem(i18n("Create"), QStringLiteral("imagegallery")));
0039     m_config = new KConfig(QStringLiteral("kimgallerypluginrc"), KConfig::NoGlobals);
0040     setupLookPage(path);
0041     setupDirectoryPage(path);
0042     setupThumbnailPage(path);
0043     connect(buttonBox()->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &KIGPDialog::slotDefault);
0044 }
0045 
0046 void KIGPDialog::slotDefault()
0047 {
0048     m_title->setText(i18n("Image Gallery for %1", m_path));
0049     m_imagesPerRow->setValue(4);
0050     m_imageName->setChecked(true);
0051     m_imageSize->setChecked(false);
0052     m_imageProperty->setChecked(false);
0053     m_fontName->setItemText(m_fontName->currentIndex(), QFontDatabase::systemFont(QFontDatabase::GeneralFont).family());
0054     m_fontSize->setValue(14);
0055     m_foregroundColor->setColor(QColor(QStringLiteral("#d0ffd0")));
0056     m_backgroundColor->setColor(QColor(QStringLiteral("#333333")));
0057 
0058     m_imageNameReq->setUrl(QUrl::fromLocalFile(m_path + "images.html"));
0059     m_recurseSubDir->setChecked(false);
0060     m_recursionLevel->setEnabled(false);
0061     m_recursionLevel->setValue(0);
0062     m_copyOriginalFiles->setChecked(false);
0063     m_useCommentFile->setChecked(false);
0064     m_commentFileReq->setUrl(QUrl::fromLocalFile(m_path + "comments"));
0065     m_commentFileReq->setEnabled(false);
0066 
0067     m_imageFormat->setItemText(m_imageFormat->currentIndex(), QStringLiteral("JPEG"));
0068     m_thumbnailSize->setValue(140);
0069     m_colorDepthSet->setChecked(false);
0070     m_colorDepth->setItemText(m_colorDepth->currentIndex(), QStringLiteral("8"));
0071 }
0072 
0073 void KIGPDialog::setupLookPage(const QString &path)
0074 {
0075     QFrame *page = new QFrame();
0076     KPageWidgetItem *pageItem = new KPageWidgetItem(page, i18n("Look"));
0077     pageItem->setHeader(i18n("Page Look"));
0078     pageItem->setIcon(QIcon::fromTheme("fill-color"));
0079     addPage(pageItem);
0080 
0081     KConfigGroup look = m_config->group("Look");
0082     QFormLayout *form = new QFormLayout(page);
0083 
0084     m_title = new QLineEdit(i18n("Image Gallery for %1", path), page);
0085     form->addRow(i18n("&Page title:"), m_title);
0086 
0087     m_imagesPerRow = new QSpinBox(page);
0088     m_imagesPerRow->setMinimumWidth(100);
0089     m_imagesPerRow->setRange(1, 8);
0090     m_imagesPerRow->setSingleStep(1);
0091     m_imagesPerRow->setValue(look.readEntry("ImagesPerRow", 4));
0092     //m_imagesPerRow->setSliderEnabled(true);
0093     form->addRow(i18n("I&mages per row:"), m_imagesPerRow);
0094 
0095     m_imageName = new QCheckBox(i18n("Show image file &name"), page);
0096     m_imageName->setChecked(look.readEntry("ImageName", true));
0097     form->addRow(QString(), m_imageName);
0098 
0099     m_imageSize = new QCheckBox(i18n("Show image file &size"), page);
0100     m_imageSize->setChecked(look.readEntry("ImageSize", false));
0101     form->addRow(QString(), m_imageSize);
0102 
0103     m_imageProperty = new QCheckBox(i18n("Show image &dimensions"), page);
0104     m_imageProperty->setChecked(look.readEntry("ImageProperty", false));
0105     form->addRow(QString(), m_imageProperty);
0106 
0107     m_fontName = new QComboBox(page);
0108     m_fontName->setSizePolicy(QSizePolicy::MinimumExpanding, m_fontName->sizePolicy().verticalPolicy());
0109 
0110     const QStringList standardFonts = KFontChooser::createFontList(KFontChooser::NoDisplayFlags);
0111 
0112     m_fontName->addItems(standardFonts);
0113     m_fontName->setItemText(m_fontName->currentIndex(), look.readEntry("FontName", QFontDatabase::systemFont(QFontDatabase::GeneralFont).family()));
0114     form->addRow(i18n("Fon&t name:"), m_fontName);
0115 
0116     m_fontSize = new QSpinBox(page);
0117     m_fontSize->setMinimumWidth(100);
0118     m_fontSize->setMinimum(6);
0119     m_fontSize->setMaximum(15);
0120     m_fontSize->setSingleStep(1);
0121     m_fontSize->setValue(look.readEntry("FontSize", 14));
0122     form->addRow(i18n("Font si&ze:"), m_fontSize);
0123 
0124     m_foregroundColor = new KColorButton(page);
0125     m_foregroundColor->setColor(QColor(look.readEntry("ForegroundColor", "#d0ffd0")));
0126     form->addRow(i18n("&Foreground color:"), m_foregroundColor);
0127 
0128     m_backgroundColor = new KColorButton(page);
0129     m_backgroundColor->setColor(QColor(look.readEntry("BackgroundColor", "#333333")));
0130     form->addRow(i18n("&Background color:"), m_backgroundColor);
0131 }
0132 
0133 void KIGPDialog::setupDirectoryPage(const QString &path)
0134 {
0135     QFrame *page = new QFrame();
0136     KPageWidgetItem *pageItem = new KPageWidgetItem(page, i18n("Folders"));
0137     pageItem->setHeader(i18n("Folders"));
0138     pageItem->setIcon(QIcon::fromTheme("folder"));
0139     addPage(pageItem);
0140 
0141     KConfigGroup group =  m_config->group("Directory");
0142     QFormLayout *form = new QFormLayout(page);
0143 
0144     m_imageNameReq = new KUrlRequester(QUrl::fromLocalFile(QString(path + "images.html")), page);
0145     form->addRow(i18n("&Save to HTML file:"), m_imageNameReq);
0146     connect(m_imageNameReq, &KUrlRequester::textChanged, this, &KIGPDialog::imageUrlChanged);
0147     m_imageNameReq->setToolTip(i18n("<p>The name of the HTML file this gallery will be saved to.</p>"));
0148 
0149     const bool recurseSubDir = group.readEntry("RecurseSubDirectories", false);
0150     m_recurseSubDir = new QCheckBox(i18n("&Recurse subfolders"), page);
0151     m_recurseSubDir->setChecked(recurseSubDir);
0152     m_recurseSubDir->setToolTip(i18n("<p>Whether subfolders should be included for the "
0153                                        "image gallery creation or not.</p>"));
0154     form->addRow(QString(), m_recurseSubDir);
0155 
0156     const int recursionLevel = group.readEntry("RecursionLevel", 0);
0157     m_recursionLevel = new QSpinBox(page);
0158     m_recursionLevel->setMinimumWidth(100);
0159     m_recursionLevel->setRange(0, 99);
0160     m_recursionLevel->setSingleStep(1);
0161     m_recursionLevel->setValue(recursionLevel);
0162     //m_recursionLevel->setSliderEnabled(true);
0163     m_recursionLevel->setSpecialValueText(i18n("Endless"));
0164     m_recursionLevel->setEnabled(recurseSubDir);
0165     m_recursionLevel->setToolTip(i18n("<p>You can limit the number of folders the "
0166                                         "image gallery creator will traverse to by setting an "
0167                                         "upper bound for the recursion depth.</p>"));
0168     form->addRow(i18n("Rec&ursion depth:"), m_recursionLevel);
0169 
0170     connect(m_recurseSubDir, &QAbstractButton::toggled, m_recursionLevel, &QWidget::setEnabled);
0171 
0172     m_copyOriginalFiles = new QCheckBox(i18n("Copy or&iginal files"), page);
0173     m_copyOriginalFiles->setChecked(group.readEntry("CopyOriginalFiles", false));
0174     m_copyOriginalFiles->setToolTip(i18n("<p>This makes a copy of all images and the gallery will refer "
0175                                            "to these copies instead of the original images.</p>"));
0176     form->addRow(QString(), m_copyOriginalFiles);
0177 
0178     const bool useCommentFile = group.readEntry("UseCommentFile", false);
0179     m_useCommentFile = new QCheckBox(i18n("Use &comment file"), page);
0180     m_useCommentFile->setChecked(useCommentFile);
0181     m_useCommentFile->setToolTip(i18n("<p>If you enable this option you can specify "
0182                                         "a comment file which will be used for generating "
0183                                         "subtitles for the images.</p>"
0184                                         "<p>For details about the file format please see "
0185                                         "the \"What's This?\" help below.</p>"));
0186     form->addRow(QString(), m_useCommentFile);
0187 
0188     m_commentFileReq = new KUrlRequester(QUrl::fromLocalFile(QString(path + "comments")), page);
0189     m_commentFileReq->setEnabled(useCommentFile);
0190     m_commentFileReq->setToolTip(i18n("<p>You can specify the name of the comment file here. "
0191                                         "The comment file contains the subtitles for the images. "
0192                                         "The format of this file is:</p>"
0193                                         "<p>FILENAME1:"
0194                                         "<br />Description"
0195                                         "<br />"
0196                                         "<br />FILENAME2:"
0197                                         "<br />Description"
0198                                         "<br />"
0199                                         "<br />and so on</p>"));
0200     form->addRow(i18n("Comments &file:"), m_commentFileReq);
0201 
0202     connect(m_useCommentFile, &QAbstractButton::toggled, m_commentFileReq, &QWidget::setEnabled);
0203 }
0204 
0205 void KIGPDialog::setupThumbnailPage(const QString &path)
0206 {
0207     Q_UNUSED(path);
0208 
0209     QFrame *page = new QFrame();
0210     KPageWidgetItem *pageItem = new KPageWidgetItem(page, i18n("Thumbnails"));
0211     pageItem->setHeader(i18n("Thumbnails"));
0212     pageItem->setIcon(QIcon::fromTheme("view-preview"));
0213     addPage(pageItem);
0214 
0215     KConfigGroup group =  m_config->group("Thumbnails");
0216     QFormLayout *form = new QFormLayout(page);
0217 
0218     m_imageFormat = new QComboBox(page);
0219     m_imageFormat->setSizePolicy(QSizePolicy::MinimumExpanding, m_imageFormat->sizePolicy().verticalPolicy());
0220     QStringList lstImgageFormat;
0221     lstImgageFormat << QStringLiteral("JPEG") << QStringLiteral("PNG");
0222     m_imageFormat->addItems(lstImgageFormat);
0223     m_imageFormat->setItemText(m_imageFormat->currentIndex(), group.readEntry("ImageFormat", "JPEG"));
0224     form->addRow(i18n("Image f&ormat:"), m_imageFormat);
0225 
0226     m_thumbnailSize = new QSpinBox(page);
0227     m_thumbnailSize->setMinimumWidth(100);
0228     m_thumbnailSize->setRange(10, 1000);
0229     m_thumbnailSize->setSingleStep(1);
0230     m_thumbnailSize->setValue(group.readEntry("ThumbnailSize", 140));
0231     //m_thumbnailSize->setSliderEnabled(true);
0232     form->addRow(i18n("Thumbnail size:"), m_thumbnailSize);
0233 
0234     const bool colorDepthSet = group.readEntry("ColorDepthSet", false);
0235     m_colorDepthSet = new QCheckBox(i18n("&Set different color depth:"), page);
0236     m_colorDepthSet->setChecked(colorDepthSet);
0237     form->addRow(QString(), m_colorDepthSet);
0238 
0239     m_colorDepth = new QComboBox(page);
0240     m_colorDepth->setSizePolicy(QSizePolicy::MinimumExpanding, m_colorDepth->sizePolicy().verticalPolicy());
0241     QStringList lst;
0242     lst << QStringLiteral("1") << QStringLiteral("8") << QStringLiteral("16") << QStringLiteral("32");
0243     m_colorDepth->addItems(lst);
0244     m_colorDepth->setCurrentIndex(m_colorDepth->findText(group.readEntry("ColorDepth", "8")));
0245     m_colorDepth->setEnabled(colorDepthSet);
0246     form->addRow(i18n("Color depth:"), m_colorDepth);
0247 
0248     connect(m_colorDepthSet, &QAbstractButton::toggled, m_colorDepth, &QWidget::setEnabled);
0249 }
0250 
0251 void KIGPDialog::writeConfig()
0252 {
0253     KConfigGroup group = m_config->group("Look");
0254     group.writeEntry("ImagesPerRow", getImagesPerRow());
0255     group.writeEntry("ImageName", printImageName());
0256     group.writeEntry("ImageSize", printImageSize());
0257     group.writeEntry("ImageProperty", printImageProperty());
0258     group.writeEntry("FontName", getFontName());
0259     group.writeEntry("FontSize", getFontSize());
0260     group.writeEntry("ForegroundColor", getForegroundColor().name());
0261     group.writeEntry("BackgroundColor", getBackgroundColor().name());
0262 
0263     group = m_config->group("Directory");
0264     group.writeEntry("RecurseSubDirectories", recurseSubDirectories());
0265     group.writeEntry("RecursionLevel", recursionLevel());
0266     group.writeEntry("CopyOriginalFiles", copyOriginalFiles());
0267     group.writeEntry("UseCommentFile", useCommentFile());
0268 
0269     group = m_config->group("Thumbnails");
0270     group.writeEntry("ThumbnailSize", getThumbnailSize());
0271     group.writeEntry("ColorDepth", getColorDepth());
0272     group.writeEntry("ColorDepthSet", colorDepthSet());
0273     group.writeEntry("ImageFormat", getImageFormat());
0274     group.sync();
0275 }
0276 
0277 KIGPDialog::~KIGPDialog()
0278 {
0279     delete m_config;
0280 }
0281 
0282 void KIGPDialog::imageUrlChanged(const QString &url)
0283 {
0284     buttonBox()->button(QDialogButtonBox::Ok)->setEnabled(!url.isEmpty());
0285 }
0286 
0287 bool  KIGPDialog::printImageName()  const
0288 {
0289     return m_imageName->isChecked();
0290 }
0291 
0292 bool  KIGPDialog::printImageSize() const
0293 {
0294     return m_imageSize->isChecked();
0295 }
0296 
0297 bool  KIGPDialog::printImageProperty() const
0298 {
0299     return m_imageProperty->isChecked();
0300 }
0301 
0302 bool KIGPDialog::recurseSubDirectories() const
0303 {
0304     return m_recurseSubDir->isChecked();
0305 }
0306 
0307 int KIGPDialog::recursionLevel() const
0308 {
0309     return m_recursionLevel->value();
0310 }
0311 
0312 bool KIGPDialog::copyOriginalFiles() const
0313 {
0314     return m_copyOriginalFiles->isChecked();
0315 }
0316 
0317 bool KIGPDialog::useCommentFile() const
0318 {
0319     return m_useCommentFile->isChecked();
0320 }
0321 
0322 int KIGPDialog::getImagesPerRow() const
0323 {
0324     return m_imagesPerRow->value();
0325 }
0326 
0327 int KIGPDialog::getThumbnailSize() const
0328 {
0329     return m_thumbnailSize->value();
0330 }
0331 
0332 int KIGPDialog::getColorDepth() const
0333 {
0334     return m_colorDepth->currentText().toInt();
0335 }
0336 
0337 bool KIGPDialog::colorDepthSet() const
0338 {
0339     return m_colorDepthSet->isChecked();
0340 }
0341 
0342 const QString KIGPDialog::getTitle() const
0343 {
0344     return m_title->text();
0345 }
0346 
0347 const QUrl KIGPDialog::getImageUrl() const
0348 {
0349     return m_imageNameReq->url();
0350 }
0351 
0352 const QString KIGPDialog::getCommentFile() const
0353 {
0354     return m_commentFileReq->url().toLocalFile();
0355 }
0356 
0357 const QString KIGPDialog::getFontName() const
0358 {
0359     return m_fontName->currentText();
0360 }
0361 
0362 const QString KIGPDialog::getFontSize() const
0363 {
0364     return m_fontSize->text();
0365 }
0366 
0367 const QColor KIGPDialog::getBackgroundColor() const
0368 {
0369     return m_backgroundColor->color();
0370 }
0371 
0372 const QColor KIGPDialog::getForegroundColor() const
0373 {
0374     return m_foregroundColor->color();
0375 }
0376 
0377 const QString KIGPDialog::getImageFormat() const
0378 {
0379     return m_imageFormat->currentText();
0380 }