File indexing completed on 2025-02-09 05:17:56

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 "VideoSaveOptionsPage.h"
0009 
0010 #include "Platforms/VideoPlatform.h"
0011 #include "SpectacleCore.h"
0012 #include "ExportManager.h"
0013 #include "SaveOptionsUtils.h"
0014 #include "VideoFormatModel.h"
0015 #include "ui_VideoSaveOptions.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <QCheckBox>
0020 #include <QComboBox>
0021 #include <QFontDatabase>
0022 #include <QImageWriter>
0023 #include <QLabel>
0024 #include <QLineEdit>
0025 
0026 using namespace Qt::StringLiterals;
0027 
0028 VideoSaveOptionsPage::VideoSaveOptionsPage(QWidget *parent)
0029     : QWidget(parent)
0030     , m_ui(new Ui_VideoSaveOptions)
0031 {
0032     m_ui->setupUi(this);
0033 
0034     const auto videoFormatModel = SpectacleCore::instance()->videoFormatModel();
0035 
0036     // Auto select the correct format if the user types an extension in the filename template.
0037     connect(m_ui->kcfg_videoFilenameTemplate, &QLineEdit::textEdited, this, [this, videoFormatModel](const QString &text) {
0038         const auto count = videoFormatModel->rowCount();
0039         for (auto i = 0; i < count; ++i) {
0040             auto index = videoFormatModel->index(i);
0041             auto extension = index.data(VideoFormatModel::ExtensionRole).toString();
0042             if (text.endsWith(u'.' + extension, Qt::CaseInsensitive)) {
0043                 m_ui->kcfg_videoFilenameTemplate->setText(text.chopped(extension.length() + 1));
0044                 m_ui->videoFormatComboBox->setCurrentIndex(i);
0045             }
0046         }
0047     });
0048     connect(m_ui->kcfg_videoFilenameTemplate, &QLineEdit::textChanged,
0049             this, &VideoSaveOptionsPage::updateFilenamePreview);
0050 
0051     m_ui->preview->setFixedHeight(m_ui->kcfg_videoFilenameTemplate->height());
0052 
0053     m_ui->videoFormatComboBox->setModel(videoFormatModel);
0054     const auto format = static_cast<VideoPlatform::Format>(Settings::preferredVideoFormat());
0055     m_ui->videoFormatComboBox->setCurrentIndex(videoFormatModel->indexOfFormat(format));
0056     connect(m_ui->videoFormatComboBox, &QComboBox::currentIndexChanged, this, [this] {
0057         const auto format = m_ui->videoFormatComboBox->currentData(VideoFormatModel::FormatRole);
0058         Settings::setPreferredVideoFormat(format.value<VideoPlatform::Format>());
0059     });
0060     connect(m_ui->videoFormatComboBox, &QComboBox::currentTextChanged, this, &VideoSaveOptionsPage::updateFilenamePreview);
0061 
0062     m_ui->captureInstructionLabel->setText(captureInstructions(false));
0063     connect(m_ui->captureInstructionLabel, &QLabel::linkActivated, this, [this](const QString &link) {
0064         if (link == u"showmore"_s) {
0065             m_ui->captureInstructionLabel->setText(captureInstructions(true));
0066         } else if (link == u"showfewer"_s) {
0067             m_ui->captureInstructionLabel->setText(captureInstructions(false));
0068         } else {
0069             m_ui->kcfg_videoFilenameTemplate->insert(link);
0070         }
0071     });
0072 }
0073 
0074 VideoSaveOptionsPage::~VideoSaveOptionsPage() = default;
0075 
0076 void VideoSaveOptionsPage::updateFilenamePreview()
0077 {
0078     const auto extension = m_ui->videoFormatComboBox->currentData(VideoFormatModel::ExtensionRole).toString();
0079     const auto templateBasename = m_ui->kcfg_videoFilenameTemplate->text();
0080     ::updateFilenamePreview(m_ui->preview, templateBasename + u'.' + extension);
0081 }
0082 
0083 #include "moc_VideoSaveOptionsPage.cpp"