File indexing completed on 2025-02-02 04:26:10

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 David Redondo <kde@david-redondo.de>
0003  *  SPDX-FileCopyrightText: 2015 Boudhayan Gupta <bgupta@kde.org>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "ImageSaveOptionsPage.h"
0009 
0010 #include "ExportManager.h"
0011 #include "SaveOptionsUtils.h"
0012 #include "ui_ImageSaveOptions.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QCheckBox>
0017 #include <QComboBox>
0018 #include <QFontDatabase>
0019 #include <QImageWriter>
0020 #include <QLabel>
0021 #include <QLineEdit>
0022 
0023 using namespace Qt::StringLiterals;
0024 
0025 ImageSaveOptionsPage::ImageSaveOptionsPage(QWidget *parent)
0026     : QWidget(parent)
0027     , m_ui(new Ui_ImageSaveOptions)
0028 {
0029     m_ui->setupUi(this);
0030 
0031     m_ui->imageCompressionQualityHelpLable->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
0032     const int sliderSpinboxHeightDiff = m_ui->qualitySpinner->sizeHint().height() - m_ui->kcfg_imageCompressionQuality->sizeHint().height();
0033     const int smallLabelLineEditHeightDiff =
0034         m_ui->kcfg_imageFilenameTemplate->sizeHint().height() - m_ui->imageCompressionQualityHelpLable->sizeHint().height();
0035     m_ui->qualityVLayout->setContentsMargins({
0036         0,
0037         std::max(0, qRound(sliderSpinboxHeightDiff / 2.0)),
0038         0,
0039         std::max(0, qRound(smallLabelLineEditHeightDiff / 2.0)),
0040     });
0041 
0042     connect(m_ui->kcfg_imageFilenameTemplate, &QLineEdit::textEdited, this, [&](const QString &newText) {
0043         QString fmt;
0044         const auto imageFormats = QImageWriter::supportedImageFormats();
0045         for (const auto &item : imageFormats) {
0046             fmt = QString::fromLocal8Bit(item);
0047             if (newText.endsWith(u'.' + fmt, Qt::CaseInsensitive)) {
0048                 QString txtCopy = newText;
0049                 txtCopy.chop(fmt.length() + 1);
0050                 m_ui->kcfg_imageFilenameTemplate->setText(txtCopy);
0051                 m_ui->kcfg_preferredImageFormat->setCurrentIndex(m_ui->kcfg_preferredImageFormat->findText(fmt.toUpper()));
0052             }
0053         }
0054     });
0055     connect(m_ui->kcfg_imageFilenameTemplate, &QLineEdit::textChanged, this, &ImageSaveOptionsPage::updateFilenamePreview);
0056 
0057     m_ui->preview->setFixedHeight(m_ui->kcfg_imageFilenameTemplate->height());
0058 
0059     m_ui->kcfg_preferredImageFormat->addItems([&]() {
0060         QStringList items;
0061         const auto formats = QImageWriter::supportedImageFormats();
0062         items.reserve(formats.count());
0063         for (const auto &fmt : formats) {
0064             items.append(QString::fromLocal8Bit(fmt).toUpper());
0065         }
0066         return items;
0067     }());
0068     connect(m_ui->kcfg_preferredImageFormat, &QComboBox::currentTextChanged, this, &ImageSaveOptionsPage::updateFilenamePreview);
0069 
0070     m_ui->captureInstructionLabel->setText(captureInstructions(false));
0071     connect(m_ui->captureInstructionLabel, &QLabel::linkActivated, this, [this](const QString &link) {
0072         if (link == u"showmore"_s) {
0073             m_ui->captureInstructionLabel->setText(captureInstructions(true));
0074         } else if (link == u"showfewer"_s) {
0075             m_ui->captureInstructionLabel->setText(captureInstructions(false));
0076         } else {
0077             m_ui->kcfg_imageFilenameTemplate->insert(link);
0078         }
0079     });
0080 }
0081 
0082 ImageSaveOptionsPage::~ImageSaveOptionsPage() = default;
0083 
0084 void ImageSaveOptionsPage::updateFilenamePreview()
0085 {
0086     const auto extension = m_ui->kcfg_preferredImageFormat->currentText().toLower();
0087     const auto templateBasename = m_ui->kcfg_imageFilenameTemplate->text();
0088     ::updateFilenamePreview(m_ui->preview, templateBasename + u'.' + extension);
0089 }
0090 
0091 #include "moc_ImageSaveOptionsPage.cpp"