Sélectionner une révision Git
auto4_lua_dialog.cpp
-
Thomas Goyne a rédigé
Originally committed to SVN as r6181.
Thomas Goyne a rédigéOriginally committed to SVN as r6181.
Line.cc 4,13 Kio
#include "Line.hh"
#include "AssFactory.hh"
using namespace Vivy::Ass;
Line::Line(AssFactory *const factory, const QString &lineString)
: assFactory(factory)
{
enum LineIndex : int {
Layer = 0, // int
Start = 1, // time
End = 2, // time
Style = 3, // text
Name = 4, // text
MarginL = 5, // int
MarginR = 6, // int
MarginV = 7, // int
Effect = 8, // text
Text = 9, // text
PastLastCode
// NOTE: time is of the form: `h:mm:ss.cc`
};
const QString lineHeader = QStringLiteral("Dialogue: ");
isComment = !lineString.startsWith(lineHeader);
const QString lineContent = lineString.mid(lineString.indexOf(": ") + 2 /* strlen ": " */);
QStringList contentList = lineContent.split(",", Qt::KeepEmptyParts, Qt::CaseInsensitive);
if (contentList.size() < LineIndex::PastLastCode)
throw std::runtime_error(("invalid number of items " + QString::number(contentList.size()) +
" instead of something superiror or equal to " +
QString::number(PastLastCode) + " for line: " + lineContent)
.toStdString());
layer = Utils::decodeLineToInteger(contentList[LineIndex::Layer], "Layer is not an integer");
effect = contentList[LineIndex::Effect];
nameOrActor = contentList[LineIndex::Name];
start = Utils::Time::fromString(contentList[LineIndex::Start]).toUInt();
end = Utils::Time::fromString(contentList[LineIndex::End]).toUInt();
styleProperties.marginL =
Utils::decodeLineToInteger(contentList[LineIndex::MarginL], "MarginL is not an integer");
styleProperties.marginR =
Utils::decodeLineToInteger(contentList[LineIndex::MarginR], "MarginR is not an integer");
styleProperties.marginV =
Utils::decodeLineToInteger(contentList[LineIndex::MarginV], "MarginV is not an integer");
const QString style = contentList[LineIndex::Style];
if (!assFactory->hasStyle(style))
throw std::runtime_error(("Invalid or not declared style name: " + style).toStdString());
lineStyle = assFactory->getStyle(style);
// Pop all but the text, it may contains `,` characters
for (int i = 0; i < LineIndex::Text; ++i)
contentList.removeFirst();
___contentAsText = contentList.join("");
initSylFromString(___contentAsText);
}
void
Line::initSylFromString(const QString &line) noexcept
{
// Matches syllabes like: `{\toto}{\alpha&HFF}content`
QRegularExpression re("((?:{[^}]*})+[^{]*)");
if (!re.isValid())
logFatal() << "The regex " << VIVY_LOG_QUOTED(re.pattern().toStdString().c_str())
<< " is not valid...";
bool once = false;
try {
QRegularExpressionMatchIterator it = re.globalMatch(line);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
content.append(Syl(this, match.captured(1)));
once |= true;
}
} catch (const std::runtime_error &e) {
qCritical() << "Failed to init syllabes with line: " << VIVY_LOG_QUOTED(line)
<< ". Error was: " << e.what() << ". Fallback to all line is one syllabe";
once = false;
}
if (!once) {
content.clear();
content.append(Syl(this, line, Syl::ConstructMode::Raw));
}
}
void
Line::setStart(quint64 s) noexcept
{
start = s;
if (s > end)
end = s;
}
void
Line::setEnd(quint64 s) noexcept
{
end = s;
if (start > s)
start = s;
}
quint64
Line::getDuration() const noexcept
{
return end - start;
}
StyleProperties
Line::getStyleProperties() const noexcept
{
return styleProperties;
}
StyleWeakPtr
Line::getStyle() const noexcept
{
return lineStyle;
}
const QVector<Syl> &
Line::getContent() const noexcept
{
return content;
}
bool
Line::getIsComment() const noexcept
{
return isComment;
}
quint64
Line::getStart() const noexcept
{
return start;
}
quint64
Line::getEnd() const noexcept
{
return end;
}
QString
Line::getContentAsText() const noexcept
{
return ___contentAsText;
}