File indexing completed on 2022-11-22 14:07:16

0001 /*
0002 SPDX-FileCopyrightText: 2016 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 This file is part of Kdenlive. See www.kdenlive.org.
0004 
0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "titletemplatedialog.h"
0009 #include "doc/kthumb.h"
0010 #include "kdenlivesettings.h"
0011 
0012 #include "klocalizedstring.h"
0013 #include <KComboBox>
0014 #include <QDir>
0015 #include <QStandardPaths>
0016 
0017 TitleTemplateDialog::TitleTemplateDialog(const QString &folder, QWidget *parent)
0018     : QDialog(parent)
0019 {
0020     m_view.setupUi(this);
0021     // Get the list of existing templates
0022     const QStringList filter = {QStringLiteral("*.kdenlivetitle")};
0023     const QString path = folder + QStringLiteral("/titles/");
0024 
0025     // Project templates
0026     QDir dir(path);
0027     const QStringList templateFiles = dir.entryList(filter, QDir::Files);
0028     for (const QString &fname : templateFiles) {
0029         m_view.template_list->comboBox()->addItem(fname, dir.absoluteFilePath(fname));
0030     }
0031 
0032     // System templates
0033     QStringList titleTemplates = QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, QStringLiteral("titles/"), QStandardPaths::LocateDirectory);
0034     titleTemplates.removeDuplicates();
0035     for (const QString &folderpath : qAsConst(titleTemplates)) {
0036         QDir sysdir(folderpath);
0037         const QStringList filesnames = sysdir.entryList(filter, QDir::Files);
0038         for (const QString &fname : filesnames) {
0039             m_view.template_list->comboBox()->addItem(fname, sysdir.absoluteFilePath(fname));
0040         }
0041     }
0042 
0043     if (m_view.template_list->comboBox()->count() > 0) {
0044         m_view.buttonBox->button(QDialogButtonBox::Ok)->setFocus();
0045     }
0046     int current = m_view.template_list->comboBox()->findText(KdenliveSettings::selected_template());
0047     if (current > -1) {
0048         m_view.template_list->comboBox()->setCurrentIndex(current);
0049     }
0050     const QStringList mimeTypeFilters = {QStringLiteral("application/x-kdenlivetitle")};
0051     m_view.template_list->setMimeTypeFilters(mimeTypeFilters);
0052     connect(m_view.template_list->comboBox(), static_cast<void (KComboBox::*)(int)>(&KComboBox::currentIndexChanged), this,
0053             &TitleTemplateDialog::updatePreview);
0054     updatePreview();
0055 }
0056 
0057 QString TitleTemplateDialog::selectedTemplate() const
0058 {
0059     QString textTemplate = m_view.template_list->comboBox()->itemData(m_view.template_list->comboBox()->currentIndex()).toString();
0060     if (textTemplate.isEmpty()) {
0061         textTemplate = m_view.template_list->comboBox()->currentText();
0062     }
0063     return textTemplate;
0064 }
0065 
0066 QString TitleTemplateDialog::selectedText() const
0067 {
0068     return m_view.description->toPlainText();
0069 }
0070 
0071 void TitleTemplateDialog::resizeEvent(QResizeEvent *event)
0072 {
0073     updatePreview();
0074     QWidget::resizeEvent(event);
0075 }
0076 
0077 void TitleTemplateDialog::updatePreview()
0078 {
0079     QString textTemplate = m_view.template_list->comboBox()->itemData(m_view.template_list->comboBox()->currentIndex()).toString();
0080     if (textTemplate.isEmpty()) {
0081         textTemplate = m_view.template_list->comboBox()->currentText();
0082     }
0083     QPixmap pix = KThumb::getImage(QUrl::fromLocalFile(textTemplate), m_view.preview->width());
0084     m_view.preview->setPixmap(pix);
0085     KdenliveSettings::setSelected_template(m_view.template_list->comboBox()->currentText());
0086 }