File indexing completed on 2024-04-21 04:51:24

0001 /*
0002     SPDX-FileCopyrightText: 2020 Jean-Baptiste Mardelle
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "subtitleedit.h"
0007 #include "bin/model/subtitlemodel.hpp"
0008 #include "monitor/monitor.h"
0009 
0010 #include "core.h"
0011 #include "kdenlivesettings.h"
0012 #include "widgets/timecodedisplay.h"
0013 
0014 #include "QTextEdit"
0015 #include "klocalizedstring.h"
0016 
0017 #include <QEvent>
0018 #include <QKeyEvent>
0019 #include <QMenu>
0020 #include <QToolButton>
0021 
0022 ShiftEnterFilter::ShiftEnterFilter(QObject *parent)
0023     : QObject(parent)
0024 {
0025 }
0026 
0027 bool ShiftEnterFilter::eventFilter(QObject *obj, QEvent *event)
0028 {
0029     if (event->type() == QEvent::KeyPress) {
0030         auto *keyEvent = static_cast<QKeyEvent *>(event);
0031         if ((keyEvent->modifiers() & Qt::ShiftModifier) && ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))) {
0032             Q_EMIT triggerUpdate();
0033             return true;
0034         }
0035     }
0036     if (event->type() == QEvent::FocusOut) {
0037         Q_EMIT triggerUpdate();
0038         return true;
0039     }
0040     return QObject::eventFilter(obj, event);
0041 }
0042 
0043 SubtitleEdit::SubtitleEdit(QWidget *parent)
0044     : QWidget(parent)
0045     , m_model(nullptr)
0046 {
0047     setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
0048     setupUi(this);
0049     auto *keyFilter = new ShiftEnterFilter(this);
0050     subText->installEventFilter(keyFilter);
0051     connect(keyFilter, &ShiftEnterFilter::triggerUpdate, this, &SubtitleEdit::updateSubtitle);
0052     connect(subText, &KTextEdit::textChanged, this, [this]() {
0053         if (m_activeSub > -1) {
0054             buttonApply->setEnabled(true);
0055         }
0056         updateCharInfo();
0057     });
0058     connect(subText, &KTextEdit::cursorPositionChanged, this, [this]() {
0059         if (m_activeSub > -1) {
0060             buttonCut->setEnabled(true);
0061         }
0062         updateCharInfo();
0063     });
0064 
0065     connect(buttonStyle, &QToolButton::toggled, this, [this](bool toggle) { stackedWidget->setCurrentIndex(toggle ? 1 : 0); });
0066 
0067     frame_position->setEnabled(false);
0068     buttonDelete->setEnabled(false);
0069 
0070     connect(tc_position, &TimecodeDisplay::timeCodeEditingFinished, this, [this](int value) {
0071         updateSubtitle();
0072         if (buttonLock->isChecked()) {
0073             // Perform a move instead of a resize
0074             m_model->requestSubtitleMove(m_activeSub, GenTime(value, pCore->getCurrentFps()));
0075         } else {
0076             GenTime duration = m_endPos - GenTime(value, pCore->getCurrentFps());
0077             m_model->requestResize(m_activeSub, duration.frames(pCore->getCurrentFps()), false);
0078         }
0079     });
0080     connect(tc_end, &TimecodeDisplay::timeCodeEditingFinished, this, [this](int value) {
0081         updateSubtitle();
0082         if (buttonLock->isChecked()) {
0083             // Perform a move instead of a resize
0084             m_model->requestSubtitleMove(m_activeSub, GenTime(value, pCore->getCurrentFps()) - (m_endPos - m_startPos));
0085         } else {
0086             GenTime duration = GenTime(value, pCore->getCurrentFps()) - m_startPos;
0087             m_model->requestResize(m_activeSub, duration.frames(pCore->getCurrentFps()), true);
0088         }
0089     });
0090     connect(tc_duration, &TimecodeDisplay::timeCodeEditingFinished, this, [this](int value) {
0091         updateSubtitle();
0092         m_model->requestResize(m_activeSub, value, true);
0093     });
0094     connect(buttonAdd, &QToolButton::clicked, this, [this]() { Q_EMIT addSubtitle(subText->toPlainText()); });
0095     connect(buttonCut, &QToolButton::clicked, this, [this]() {
0096         if (m_activeSub > -1 && subText->hasFocus()) {
0097             int pos = subText->textCursor().position();
0098             updateSubtitle();
0099             Q_EMIT cutSubtitle(m_activeSub, pos);
0100         }
0101     });
0102     connect(buttonApply, &QToolButton::clicked, this, &SubtitleEdit::updateSubtitle);
0103     connect(buttonPrev, &QToolButton::clicked, this, &SubtitleEdit::goToPrevious);
0104     connect(buttonNext, &QToolButton::clicked, this, &SubtitleEdit::goToNext);
0105     connect(buttonIn, &QToolButton::clicked, []() { pCore->triggerAction(QStringLiteral("resize_timeline_clip_start")); });
0106     connect(buttonOut, &QToolButton::clicked, []() { pCore->triggerAction(QStringLiteral("resize_timeline_clip_end")); });
0107     connect(buttonDelete, &QToolButton::clicked, []() { pCore->triggerAction(QStringLiteral("delete_timeline_clip")); });
0108     buttonNext->setToolTip(i18n("Go to next subtitle"));
0109     buttonNext->setWhatsThis(xi18nc("@info:whatsthis", "Moves the playhead in the timeline to the beginning of the subtitle to the right."));
0110     buttonPrev->setToolTip(i18n("Go to previous subtitle"));
0111     buttonPrev->setWhatsThis(xi18nc("@info:whatsthis", "Moves the playhead in the timeline to the beginning of the subtitle to the left."));
0112     buttonAdd->setToolTip(i18n("Add subtitle"));
0113     buttonAdd->setWhatsThis(xi18nc("@info:whatsthis", "Creates a new subtitle with the default length (set in <interface>Settings->Configure "
0114                                                       "Kdenlive…->Misc</interface>) at the current playhead position/frame."));
0115     buttonCut->setToolTip(i18n("Split subtitle at cursor position"));
0116     buttonCut->setWhatsThis(
0117         xi18nc("@info:whatsthis", "Cuts the subtitle text at the cursor position and creates a new subtitle to the right (like cutting a clip)."));
0118     buttonApply->setToolTip(i18n("Update subtitle text"));
0119     buttonApply->setWhatsThis(xi18nc("@info:whatsthis", "Updates the subtitle display in the timeline."));
0120     buttonStyle->setToolTip(i18n("Show style options"));
0121     buttonStyle->setWhatsThis(xi18nc("@info:whatsthis", "Toggles a list to adjust font, size, color, outline style, shadow, position and background."));
0122     buttonDelete->setToolTip(i18n("Delete subtitle"));
0123     buttonDelete->setWhatsThis(xi18nc("@info:whatsthis", "Deletes the currently selected subtitle (doesn’t work on multiple subtitles)."));
0124 
0125     // Styling dialog
0126     connect(fontSize, QOverload<int>::of(&QSpinBox::valueChanged), this, &SubtitleEdit::updateStyle);
0127     connect(outlineSize, QOverload<int>::of(&QSpinBox::valueChanged), this, &SubtitleEdit::updateStyle);
0128     connect(shadowSize, QOverload<int>::of(&QSpinBox::valueChanged), this, &SubtitleEdit::updateStyle);
0129     connect(fontFamily, &QFontComboBox::currentFontChanged, this, &SubtitleEdit::updateStyle);
0130     connect(fontColor, &KColorButton::changed, this, &SubtitleEdit::updateStyle);
0131     connect(outlineColor, &KColorButton::changed, this, &SubtitleEdit::updateStyle);
0132     connect(checkFont, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0133     connect(checkFontSize, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0134     connect(checkFontColor, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0135     connect(checkOutlineColor, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0136     connect(checkOutlineSize, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0137     connect(checkPosition, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0138     connect(checkShadowSize, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0139     connect(checkOpaque, &QCheckBox::toggled, this, &SubtitleEdit::updateStyle);
0140     alignment->addItem(i18n("Bottom Center"), 2);
0141     alignment->addItem(i18n("Bottom Left"), 1);
0142     alignment->addItem(i18n("Bottom Right"), 3);
0143     alignment->addItem(i18n("Center Left"), 9);
0144     alignment->addItem(i18n("Center"), 10);
0145     alignment->addItem(i18n("Center Right"), 11);
0146     alignment->addItem(i18n("Top Left"), 4);
0147     alignment->addItem(i18n("Top Center"), 6);
0148     alignment->addItem(i18n("Top Right"), 7);
0149     connect(alignment, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SubtitleEdit::updateStyle);
0150     QAction *zoomIn = new QAction(QIcon::fromTheme(QStringLiteral("zoom-in")), i18n("Zoom In"), this);
0151     connect(zoomIn, &QAction::triggered, this, &SubtitleEdit::slotZoomIn);
0152     QAction *zoomOut = new QAction(QIcon::fromTheme(QStringLiteral("zoom-out")), i18n("Zoom Out"), this);
0153     connect(zoomOut, &QAction::triggered, this, &SubtitleEdit::slotZoomOut);
0154     QMenu *menu = new QMenu(this);
0155     menu->addAction(zoomIn);
0156     menu->addAction(zoomOut);
0157     subMenu->setMenu(menu);
0158     applyFontSize();
0159 }
0160 
0161 void SubtitleEdit::slotZoomIn()
0162 {
0163     QTextCursor cursor = subText->textCursor();
0164     subText->selectAll();
0165     qreal fontSize = QFontInfo(subText->currentFont()).pointSizeF() * 1.2;
0166     KdenliveSettings::setSubtitleEditFontSize(fontSize);
0167     subText->setFontPointSize(KdenliveSettings::subtitleEditFontSize());
0168     subText->setTextCursor(cursor);
0169 }
0170 
0171 void SubtitleEdit::slotZoomOut()
0172 {
0173     QTextCursor cursor = subText->textCursor();
0174     subText->selectAll();
0175     qreal fontSize = QFontInfo(subText->currentFont()).pointSizeF() / 1.2;
0176     fontSize = qMax(fontSize, QFontInfo(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont)).pointSizeF());
0177     KdenliveSettings::setSubtitleEditFontSize(fontSize);
0178     subText->setFontPointSize(KdenliveSettings::subtitleEditFontSize());
0179     subText->setTextCursor(cursor);
0180 }
0181 
0182 void SubtitleEdit::applyFontSize()
0183 {
0184     if (KdenliveSettings::subtitleEditFontSize() > 0) {
0185         QTextCursor cursor = subText->textCursor();
0186         subText->selectAll();
0187         subText->setFontPointSize(KdenliveSettings::subtitleEditFontSize());
0188         subText->setTextCursor(cursor);
0189     }
0190 }
0191 
0192 void SubtitleEdit::updateStyle()
0193 {
0194     QStringList styleString;
0195     if (fontFamily->isEnabled()) {
0196         styleString << QStringLiteral("Fontname=%1").arg(fontFamily->currentFont().family());
0197     }
0198     if (fontSize->isEnabled()) {
0199         styleString << QStringLiteral("Fontsize=%1").arg(fontSize->value());
0200     }
0201     if (fontColor->isEnabled()) {
0202         QColor color = fontColor->color();
0203         QColor destColor(color.blue(), color.green(), color.red(), 255 - color.alpha());
0204         // Strip # character
0205         QString colorName = destColor.name(QColor::HexArgb);
0206         colorName.remove(0, 1);
0207         styleString << QStringLiteral("PrimaryColour=&H%1").arg(colorName);
0208     }
0209     if (outlineSize->isEnabled()) {
0210         styleString << QStringLiteral("Outline=%1").arg(outlineSize->value());
0211     }
0212     if (shadowSize->isEnabled()) {
0213         styleString << QStringLiteral("Shadow=%1").arg(shadowSize->value());
0214     }
0215     if (outlineColor->isEnabled()) {
0216         // Qt AARRGGBB must be converted to AABBGGRR where AA is 255-AA
0217         QColor color = outlineColor->color();
0218         QColor destColor(color.blue(), color.green(), color.red(), 255 - color.alpha());
0219         // Strip # character
0220         QString colorName = destColor.name(QColor::HexArgb);
0221         colorName.remove(0, 1);
0222         styleString << QStringLiteral("OutlineColour=&H%1").arg(colorName);
0223     }
0224     if (checkOpaque->isChecked()) {
0225         QColor color = outlineColor->color();
0226         if (color.alpha() < 255) {
0227             // To avoid alpha overlay with multi lines, draw only one box
0228             QColor destColor(color.blue(), color.green(), color.red(), 255 - color.alpha());
0229             // Strip # character
0230             QString colorName = destColor.name(QColor::HexArgb);
0231             colorName.remove(0, 1);
0232             styleString << QStringLiteral("BorderStyle=4") << QStringLiteral("BackColour=&H%1").arg(colorName);
0233         } else {
0234             styleString << QStringLiteral("BorderStyle=3");
0235         }
0236     }
0237     if (alignment->isEnabled()) {
0238         styleString << QStringLiteral("Alignment=%1").arg(alignment->currentData().toInt());
0239     }
0240     m_model->setStyle(styleString.join(QLatin1Char(',')));
0241 }
0242 
0243 void SubtitleEdit::setModel(std::shared_ptr<SubtitleModel> model)
0244 {
0245     m_model = model;
0246     m_activeSub = -1;
0247     buttonApply->setEnabled(false);
0248     buttonCut->setEnabled(false);
0249     if (m_model == nullptr) {
0250         QSignalBlocker bk(subText);
0251         subText->clear();
0252         loadStyle(QString());
0253         frame_position->setEnabled(false);
0254     } else {
0255         connect(m_model.get(), &SubtitleModel::updateSubtitleStyle, this, &SubtitleEdit::loadStyle);
0256         connect(m_model.get(), &SubtitleModel::dataChanged, this, [this](const QModelIndex &start, const QModelIndex &, const QVector<int> &roles) {
0257             if (m_activeSub > -1 && start.row() == m_model->getRowForId(m_activeSub)) {
0258                 if (roles.contains(SubtitleModel::SubtitleRole) || roles.contains(SubtitleModel::StartFrameRole) ||
0259                     roles.contains(SubtitleModel::EndFrameRole)) {
0260                     setActiveSubtitle(m_activeSub);
0261                 }
0262             }
0263         });
0264         frame_position->setEnabled(true);
0265         stackedWidget->widget(0)->setEnabled(false);
0266     }
0267 }
0268 
0269 void SubtitleEdit::loadStyle(const QString &style)
0270 {
0271     QStringList params = style.split(QLatin1Char(','));
0272     // Read style params
0273     QSignalBlocker bk1(checkFont);
0274     QSignalBlocker bk2(checkFontSize);
0275     QSignalBlocker bk3(checkFontColor);
0276     QSignalBlocker bk4(checkOutlineColor);
0277     QSignalBlocker bk5(checkOutlineSize);
0278     QSignalBlocker bk6(checkShadowSize);
0279     QSignalBlocker bk7(checkPosition);
0280     QSignalBlocker bk8(checkOpaque);
0281 
0282     checkFont->setChecked(false);
0283     checkFontSize->setChecked(false);
0284     checkFontColor->setChecked(false);
0285     checkOutlineColor->setChecked(false);
0286     checkOutlineSize->setChecked(false);
0287     checkShadowSize->setChecked(false);
0288     checkPosition->setChecked(false);
0289     checkOpaque->setChecked(false);
0290 
0291     fontFamily->setEnabled(false);
0292     fontSize->setEnabled(false);
0293     fontColor->setEnabled(false);
0294     outlineColor->setEnabled(false);
0295     outlineSize->setEnabled(false);
0296     shadowSize->setEnabled(false);
0297     alignment->setEnabled(false);
0298 
0299     for (const QString &p : params) {
0300         const QString pName = p.section(QLatin1Char('='), 0, 0);
0301         QString pValue = p.section(QLatin1Char('='), 1);
0302         if (pName == QLatin1String("Fontname")) {
0303             checkFont->setChecked(true);
0304             QFont font(pValue);
0305             QSignalBlocker bk(fontFamily);
0306             fontFamily->setEnabled(true);
0307             fontFamily->setCurrentFont(font);
0308         } else if (pName == QLatin1String("Fontsize")) {
0309             checkFontSize->setChecked(true);
0310             QSignalBlocker bk(fontSize);
0311             fontSize->setEnabled(true);
0312             fontSize->setValue(pValue.toInt());
0313         } else if (pName == QLatin1String("OutlineColour")) {
0314             checkOutlineColor->setChecked(true);
0315             pValue.replace(QLatin1String("&H"), QLatin1String("#"));
0316             QColor col(pValue);
0317             QColor result(col.blue(), col.green(), col.red(), 255 - col.alpha());
0318             QSignalBlocker bk(outlineColor);
0319             outlineColor->setEnabled(true);
0320             outlineColor->setColor(result);
0321         } else if (pName == QLatin1String("Outline")) {
0322             checkOutlineSize->setChecked(true);
0323             QSignalBlocker bk(outlineSize);
0324             outlineSize->setEnabled(true);
0325             outlineSize->setValue(pValue.toInt());
0326         } else if (pName == QLatin1String("Shadow")) {
0327             checkShadowSize->setChecked(true);
0328             QSignalBlocker bk(shadowSize);
0329             shadowSize->setEnabled(true);
0330             shadowSize->setValue(pValue.toInt());
0331         } else if (pName == QLatin1String("BorderStyle")) {
0332             checkOpaque->setChecked(true);
0333         } else if (pName == QLatin1String("Alignment")) {
0334             checkPosition->setChecked(true);
0335             QSignalBlocker bk(alignment);
0336             alignment->setEnabled(true);
0337             int ix = alignment->findData(pValue.toInt());
0338             if (ix > -1) {
0339                 alignment->setCurrentIndex(ix);
0340             }
0341         } else if (pName == QLatin1String("PrimaryColour")) {
0342             checkFontColor->setChecked(true);
0343             pValue.replace(QLatin1String("&H"), QLatin1String("#"));
0344             QColor col(pValue);
0345             QColor result(col.blue(), col.green(), col.red(), 255 - col.alpha());
0346             QSignalBlocker bk(fontColor);
0347             fontColor->setEnabled(true);
0348             fontColor->setColor(result);
0349         }
0350     }
0351 }
0352 
0353 void SubtitleEdit::updateSubtitle()
0354 {
0355     if (!buttonApply->isEnabled()) {
0356         return;
0357     }
0358     buttonApply->setEnabled(false);
0359     if (m_activeSub > -1 && m_model) {
0360         QString txt = subText->toPlainText().trimmed();
0361         txt.replace(QLatin1String("\n\n"), QStringLiteral("\n"));
0362         if (subText->document()->defaultTextOption().textDirection() == Qt::RightToLeft && !txt.startsWith(QChar(0x200E))) {
0363             txt.prepend(QChar(0x200E));
0364         }
0365         m_model->setText(m_activeSub, txt);
0366     }
0367 }
0368 
0369 void SubtitleEdit::setActiveSubtitle(int id)
0370 {
0371     m_activeSub = id;
0372     buttonApply->setEnabled(false);
0373     buttonCut->setEnabled(false);
0374     if (m_model && id > -1) {
0375         subText->setEnabled(true);
0376         QSignalBlocker bk(subText);
0377         stackedWidget->widget(0)->setEnabled(true);
0378         buttonDelete->setEnabled(true);
0379         QSignalBlocker bk2(tc_position);
0380         QSignalBlocker bk3(tc_end);
0381         QSignalBlocker bk4(tc_duration);
0382         subText->setPlainText(m_model->getText(id));
0383         m_startPos = m_model->getStartPosForId(id);
0384         GenTime duration = GenTime(m_model->getSubtitlePlaytime(id), pCore->getCurrentFps());
0385         m_endPos = m_startPos + duration;
0386         tc_position->setValue(m_startPos);
0387         tc_end->setValue(m_endPos);
0388         tc_duration->setValue(duration);
0389         tc_position->setEnabled(true);
0390         tc_end->setEnabled(true);
0391         tc_duration->setEnabled(true);
0392     } else {
0393         tc_position->setEnabled(false);
0394         tc_end->setEnabled(false);
0395         tc_duration->setEnabled(false);
0396         stackedWidget->widget(0)->setEnabled(false);
0397         buttonDelete->setEnabled(false);
0398         QSignalBlocker bk(subText);
0399         subText->clear();
0400     }
0401     updateCharInfo();
0402     applyFontSize();
0403 }
0404 
0405 void SubtitleEdit::goToPrevious()
0406 {
0407     if (m_model) {
0408         int id = -1;
0409         if (m_activeSub > -1) {
0410             id = m_model->getPreviousSub(m_activeSub);
0411         } else {
0412             // Start from timeline cursor position
0413             int cursorPos = pCore->getMonitorPosition();
0414             std::unordered_set<int> sids = m_model->getItemsInRange(cursorPos, cursorPos);
0415             if (sids.empty()) {
0416                 sids = m_model->getItemsInRange(0, cursorPos);
0417                 for (int s : sids) {
0418                     if (id == -1 || m_model->getSubtitleEnd(s) > m_model->getSubtitleEnd(id)) {
0419                         id = s;
0420                     }
0421                 }
0422             } else {
0423                 id = m_model->getPreviousSub(*sids.begin());
0424             }
0425         }
0426         if (id > -1) {
0427             if (buttonApply->isEnabled()) {
0428                 updateSubtitle();
0429             }
0430             GenTime prev = m_model->getStartPosForId(id);
0431             pCore->getMonitor(Kdenlive::ProjectMonitor)->requestSeek(prev.frames(pCore->getCurrentFps()));
0432             pCore->selectTimelineItem(id);
0433         }
0434     }
0435     updateCharInfo();
0436 }
0437 
0438 void SubtitleEdit::goToNext()
0439 {
0440     if (m_model) {
0441         int id = -1;
0442         if (m_activeSub > -1) {
0443             id = m_model->getNextSub(m_activeSub);
0444         } else {
0445             // Start from timeline cursor position
0446             int cursorPos = pCore->getMonitorPosition();
0447             std::unordered_set<int> sids = m_model->getItemsInRange(cursorPos, cursorPos);
0448             if (sids.empty()) {
0449                 sids = m_model->getItemsInRange(cursorPos, -1);
0450                 for (int s : sids) {
0451                     if (id == -1 || m_model->getStartPosForId(s) < m_model->getStartPosForId(id)) {
0452                         id = s;
0453                     }
0454                 }
0455             } else {
0456                 id = m_model->getNextSub(*sids.begin());
0457             }
0458         }
0459         if (id > -1) {
0460             if (buttonApply->isEnabled()) {
0461                 updateSubtitle();
0462             }
0463             GenTime prev = m_model->getStartPosForId(id);
0464             pCore->getMonitor(Kdenlive::ProjectMonitor)->requestSeek(prev.frames(pCore->getCurrentFps()));
0465             pCore->selectTimelineItem(id);
0466         }
0467     }
0468     updateCharInfo();
0469 }
0470 
0471 void SubtitleEdit::updateCharInfo()
0472 {
0473     char_count->setText(i18n("Character: %1, total: <b>%2</b>", subText->textCursor().position(), subText->document()->characterCount()));
0474 }