File indexing completed on 2024-12-22 04:40:14

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "scripting_subtitle.h"
0009 
0010 #include "appglobal.h"
0011 #include "application.h"
0012 #include "scripting/scripting_subtitleline.h"
0013 #include "scripting/scripting_range.h"
0014 #include "scripting/scripting_rangelist.h"
0015 
0016 using namespace SubtitleComposer;
0017 
0018 Scripting::Subtitle::Subtitle(SubtitleComposer::Subtitle *backend, QObject *parent) :
0019     QObject(parent),
0020     m_backend(backend)
0021 {}
0022 
0023 double
0024 Scripting::Subtitle::framesPerSecond() const
0025 {
0026     return m_backend->framesPerSecond();
0027 }
0028 
0029 void
0030 Scripting::Subtitle::setFramesPerSecond(double framesPerSecond)
0031 {
0032     return m_backend->setFramesPerSecond(framesPerSecond);
0033 }
0034 
0035 void
0036 Scripting::Subtitle::changeFramesPerSecond(double toFramesPerSecond, double fromFramesPerSecond)
0037 {
0038     return m_backend->changeFramesPerSecond(toFramesPerSecond, fromFramesPerSecond);
0039 }
0040 
0041 bool
0042 Scripting::Subtitle::isEmpty() const
0043 {
0044     return m_backend->isEmpty();
0045 }
0046 
0047 int
0048 Scripting::Subtitle::linesCount() const
0049 {
0050     return m_backend->linesCount();
0051 }
0052 
0053 int
0054 Scripting::Subtitle::lastIndex() const
0055 {
0056     return m_backend->lastIndex();
0057 }
0058 
0059 QObject *
0060 Scripting::Subtitle::line(int index)
0061 {
0062     if(index < 0 || index > (m_backend->lastIndex()))
0063         return 0;
0064     return new Scripting::SubtitleLine(m_backend->line(index), this);
0065 }
0066 
0067 QObject *
0068 Scripting::Subtitle::firstLine()
0069 {
0070     if(m_backend->isEmpty())
0071         return 0;
0072     return new Scripting::SubtitleLine(m_backend->firstLine(), this);
0073 }
0074 
0075 QObject *
0076 Scripting::Subtitle::lastLine()
0077 {
0078     if(m_backend->isEmpty())
0079         return 0;
0080     return new Scripting::SubtitleLine(m_backend->lastLine(), this);
0081 }
0082 
0083 SubtitleComposer::RangeList
0084 Scripting::Subtitle::toRangesList(const QObject *object)
0085 {
0086     const Scripting::Range *range = qobject_cast<const Scripting::Range *>(object);
0087     if(range)
0088         return range->m_backend;
0089     const Scripting::RangeList *rangeList = qobject_cast<const Scripting::RangeList *>(object);
0090     if(rangeList)
0091         return rangeList->m_backend;
0092     return SubtitleComposer::RangeList();
0093 }
0094 
0095 static SubtitleTarget
0096 toTextTarget(int value, int opDefault)
0097 {
0098     if(value < Primary || value >= SubtitleTargetSize)
0099         return (SubtitleTarget)opDefault;
0100     return (SubtitleTarget)value;
0101 }
0102 
0103 Scripting::SubtitleLine *
0104 Scripting::Subtitle::insertNewLine(int index, bool timeAfter, int target)
0105 {
0106     if(index > m_backend->linesCount())
0107         return 0;
0108     static const int opDefault = Both;
0109     return new Scripting::SubtitleLine(m_backend->insertNewLine(index, timeAfter, toTextTarget(target, opDefault)), this);
0110 }
0111 
0112 void
0113 Scripting::Subtitle::removeLine(int index, int target)
0114 {
0115     if(index < 0 || index >= m_backend->linesCount())
0116         return;
0117     static const int opDefault = Both;
0118     m_backend->removeLines(SubtitleComposer::Range(index), toTextTarget(target, opDefault));
0119 }
0120 
0121 void
0122 Scripting::Subtitle::removeLines(QObject *ranges, int target)
0123 {
0124     static const int opDefault = Both;
0125     m_backend->removeLines(toRangesList(ranges), toTextTarget(target, opDefault));
0126 }
0127 
0128 void
0129 Scripting::Subtitle::swapTexts(QObject *ranges)
0130 {
0131     m_backend->swapTexts(toRangesList(ranges));
0132 }
0133 
0134 void
0135 Scripting::Subtitle::splitLines(QObject *ranges)
0136 {
0137     m_backend->splitLines(toRangesList(ranges));
0138 }
0139 
0140 void
0141 Scripting::Subtitle::joinLines(QObject *ranges)
0142 {
0143     m_backend->joinLines(toRangesList(ranges));
0144 }
0145 
0146 void
0147 Scripting::Subtitle::shiftLines(QObject *ranges, int msecs)
0148 {
0149     m_backend->shiftLines(toRangesList(ranges), msecs);
0150 }
0151 
0152 void
0153 Scripting::Subtitle::adjustLines(QObject *object, int firstTime, int lastTime)
0154 {
0155     if(const Scripting::Range * range = qobject_cast<const Scripting::Range *>(object))
0156         m_backend->adjustLines(range->m_backend, firstTime, lastTime);
0157 }
0158 
0159 void
0160 Scripting::Subtitle::sortLines(QObject *object)
0161 {
0162     if(const Scripting::Range * range = qobject_cast<const Scripting::Range *>(object))
0163         m_backend->sortLines(range->m_backend);
0164 }
0165 
0166 void
0167 Scripting::Subtitle::applyDurationLimits(QObject *ranges, int minDuration, int maxDuration, bool canOverlap)
0168 {
0169     m_backend->applyDurationLimits(toRangesList(ranges), minDuration, maxDuration, canOverlap);
0170 }
0171 
0172 void
0173 Scripting::Subtitle::setMaximumDurations(QObject *ranges)
0174 {
0175     m_backend->setMaximumDurations(toRangesList(ranges));
0176 }
0177 
0178 void
0179 Scripting::Subtitle::setAutoDurations(QObject *ranges, int msecsPerChar, int msecsPerWord, int msecsPerLine, bool canOverlap, int calculationTarget)
0180 {
0181     const int opDefault = app()->translationMode() ? Both : Primary;
0182 
0183     m_backend->setAutoDurations(toRangesList(ranges), msecsPerChar, msecsPerWord, msecsPerLine, canOverlap, toTextTarget(calculationTarget, opDefault));
0184 }
0185 
0186 void
0187 Scripting::Subtitle::fixOverlappingLines(QObject *ranges, int minInterval)
0188 {
0189     m_backend->fixOverlappingLines(toRangesList(ranges), minInterval);
0190 }
0191 
0192 void
0193 Scripting::Subtitle::fixPunctuation(QObject *ranges, bool spaces, bool quotes, bool englishI, bool ellipsis, int target)
0194 {
0195     const int opDefault = app()->translationMode() ? Both : Primary;
0196     m_backend->fixPunctuation(toRangesList(ranges), spaces, quotes, englishI, ellipsis, toTextTarget(target, opDefault));
0197 }
0198 
0199 void
0200 Scripting::Subtitle::lowerCase(QObject *ranges, int target)
0201 {
0202     const int opDefault = app()->translationMode() ? Both : Primary;
0203     m_backend->lowerCase(toRangesList(ranges), toTextTarget(target, opDefault));
0204 }
0205 
0206 void
0207 Scripting::Subtitle::upperCase(QObject *ranges, int target)
0208 {
0209     const int opDefault = app()->translationMode() ? Both : Primary;
0210     m_backend->upperCase(toRangesList(ranges), toTextTarget(target, opDefault));
0211 }
0212 
0213 void
0214 Scripting::Subtitle::titleCase(QObject *ranges, bool lowerFirst, int target)
0215 {
0216     const int opDefault = app()->translationMode() ? Both : Primary;
0217     m_backend->titleCase(toRangesList(ranges), lowerFirst, toTextTarget(target, opDefault));
0218 }
0219 
0220 void
0221 Scripting::Subtitle::sentenceCase(QObject *ranges, bool lowerFirst, int target)
0222 {
0223     const int opDefault = app()->translationMode() ? Both : Primary;
0224     m_backend->sentenceCase(toRangesList(ranges), lowerFirst, toTextTarget(target, opDefault));
0225 }
0226 
0227 void
0228 Scripting::Subtitle::breakLines(QObject *ranges, int minLengthForLineBreak, int target)
0229 {
0230     const int opDefault = app()->translationMode() ? Both : Primary;
0231     m_backend->breakLines(toRangesList(ranges), minLengthForLineBreak, toTextTarget(target, opDefault));
0232 }
0233 
0234 void
0235 Scripting::Subtitle::unbreakTexts(QObject *ranges, int target)
0236 {
0237     const int opDefault = app()->translationMode() ? Both : Primary;
0238     m_backend->unbreakTexts(toRangesList(ranges), toTextTarget(target, opDefault));
0239 }
0240 
0241 void
0242 Scripting::Subtitle::simplifyTextWhiteSpace(QObject *ranges, int target)
0243 {
0244     const int opDefault = app()->translationMode() ? Both : Primary;
0245     m_backend->simplifyTextWhiteSpace(toRangesList(ranges), toTextTarget(target, opDefault));
0246 }
0247 
0248 void
0249 Scripting::Subtitle::setMarked(QObject *ranges, bool value)
0250 {
0251     m_backend->setMarked(toRangesList(ranges), value);
0252 }
0253 
0254 void
0255 Scripting::Subtitle::clearErrors(QObject *ranges, int errorFlags)
0256 {
0257     m_backend->clearErrors(toRangesList(ranges), errorFlags);
0258 }
0259 
0260 void
0261 Scripting::Subtitle::checkErrors(QObject *ranges, int errorFlags)
0262 {
0263     m_backend->checkErrors(toRangesList(ranges), errorFlags);
0264 }
0265 
0266 void
0267 Scripting::Subtitle::recheckErrors(QObject *ranges)
0268 {
0269     m_backend->recheckErrors(toRangesList(ranges));
0270 }
0271 
0272