Beispiel #1
0
void
Subtitle::joinLines(const RangeList &ranges)
{
	beginCompositeAction(i18n("Join Lines"));

	RangeList obsoletedRanges;

	for(RangeList::ConstIterator rangesIt = ranges.begin(), end = ranges.end(); rangesIt != end; ++rangesIt) {
		int rangeStart = (*rangesIt).start();
		int rangeEnd = normalizeRangeIndex((*rangesIt).end());

		if(rangeStart >= rangeEnd)
			continue;

		SubtitleLine *firstLine = m_lines.at(rangeStart);
		SubtitleLine *lastLine = m_lines.at(rangeEnd);

		SString primaryText, secondaryText;

		for(SubtitleIterator it(*this, Range(rangeStart, rangeEnd - 1)); it.current(); ++it) {
			if(!it.current()->primaryText().isEmpty()) {
				primaryText.append(it.current()->primaryText());
				primaryText.append("\n");
			}

			if(!it.current()->secondaryText().isEmpty()) {
				secondaryText.append(it.current()->secondaryText());
				secondaryText.append("\n");
			}
		}

		primaryText.append(lastLine->primaryText());
		secondaryText.append(lastLine->secondaryText());

		firstLine->setTexts(primaryText, secondaryText);
		firstLine->setHideTime(lastLine->hideTime());

		obsoletedRanges << Range(rangeStart + 1, rangeEnd);
	}

	removeLines(obsoletedRanges, Both);

	endCompositeAction();
}
Beispiel #2
0
SubtitleLine *
Subtitle::insertNewLine(int index, bool timeAfter, TextTarget target)
{
	Q_ASSERT(index <= m_lines.count());

	if(index < 0)
		index = m_lines.count();

	SubtitleLine *newLine = new SubtitleLine();
	int newLineIndex = (target == Secondary) ? m_lines.count() : index;

	if(timeAfter) {
		if(newLineIndex) {              // there is a previous line
			SubtitleLine *prevLine = m_lines.value(newLineIndex - 1);
			newLine->setTimes(prevLine->hideTime() + 100, prevLine->hideTime() + 1000);
		} else if(newLineIndex < m_lines.count()) {     // there is a next line
			SubtitleLine *nextLine = m_lines.value(newLineIndex);
			newLine->setTimes(nextLine->showTime() - 1100, nextLine->showTime() - 100);
		} else
			newLine->setHideTime(1000);
	} else {                                        // ! timeAfter
		if(newLineIndex < m_lines.count()) {    // there is a next line
			SubtitleLine *nextLine = m_lines.at(newLineIndex);
			newLine->setTimes(nextLine->showTime() - 1100, nextLine->showTime() - 100);
		} else if(newLineIndex) {       // there is a previous line
			SubtitleLine *prevLine = m_lines.at(newLineIndex - 1);
			newLine->setTimes(prevLine->hideTime() + 100, prevLine->hideTime() + 1000);
		} else
			newLine->setHideTime(1000);
	}

	if(target == Both || index == m_lines.count()) {
		insertLine(newLine, newLineIndex);
	} else if(target == Primary) {
		beginCompositeAction(i18n("Insert Line"));

		insertLine(newLine, newLineIndex);

		SubtitleLine *line = newLine;
		SubtitleIterator it(*this, Range::full(), false);
		for(it.toIndex(newLineIndex + 1); it.current(); ++it) {
			line->setSecondaryText(it.current()->secondaryText());
			line = it.current();
		}
		line->setSecondaryText(SString());

		endCompositeAction();
	} else if(target == Secondary) {
		beginCompositeAction(i18n("Insert Line"));

		insertLine(newLine, newLineIndex);

		SubtitleIterator it(*this, Range::full(), true);
		SubtitleLine *line = it.current();
		for(--it; it.index() >= index; --it) {
			line->setSecondaryText(it.current()->secondaryText());
			line = it.current();
		}
		line->setSecondaryText(SString());

		newLine = line;

		endCompositeAction();
	}

	return newLine;
}