aeidon/agents/edit.py¶
Source code for module aeidon.agents.edit from file aeidon/agents/edit.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | # -*- coding: utf-8 -*-
# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Basic editing of entire subtitles."""
import aeidon
_ = aeidon.i18n._
class EditAgent(aeidon.Delegate):
"""Basic editing of entire subtitles."""
@aeidon.deco.export
@aeidon.deco.revertable
def clear_texts(self, indices, doc, register=-1):
"""Set texts to blank strings."""
new_texts = [""] * len(indices)
self.replace_texts(indices, doc, new_texts, register=register)
self.set_action_description(register, _("Clearing texts"))
@aeidon.deco.revertable
@aeidon.deco.notify_frozen
def _insert_blank_subtitles(self, indices, register=-1):
"""Insert new blank subtitles at `indices`."""
for rindices in aeidon.util.get_ranges(indices):
first_start = 0.0
if self.subtitles:
start = self.subtitles[0].start_seconds
first_start = (0.0 if start >= 0 else start - 3.0)
if rindices[0] > 0:
subtitle = self.subtitles[rindices[0] - 1]
first_start = subtitle.end_seconds
duration = 3.0
if rindices[0] < len(self.subtitles):
subtitle = self.subtitles[rindices[0]]
window = subtitle.start_seconds - first_start
duration = window / len(rindices)
for i, index in enumerate(rindices):
subtitle = self.new_subtitle()
subtitle.start_seconds = first_start + i*duration
subtitle.duration_seconds = duration
self.subtitles.insert(index, subtitle)
action = aeidon.RevertableAction(register=register)
action.docs = tuple(aeidon.documents)
action.description = _("Inserting subtitles")
action.revert_function = self.remove_subtitles
action.revert_args = (indices,)
self.register_action(action)
self.emit("subtitles-inserted", indices)
@aeidon.deco.export
@aeidon.deco.revertable
@aeidon.deco.notify_frozen
def insert_subtitles(self, indices, subtitles=None, register=-1):
"""
Insert `subtitles` at `indices`.
If `subtitles` is ``None``, insert blank subtitles with sensible
equal duration positions within given window or with 3 second durations
if window not limited.
"""
if subtitles is None:
return self._insert_blank_subtitles(indices, register=register)
for i, index in enumerate(indices):
self.subtitles.insert(index, subtitles[i])
action = aeidon.RevertableAction(register=register)
action.docs = tuple(aeidon.documents)
action.description = _("Inserting subtitles")
action.revert_function = self.remove_subtitles
action.revert_args = (indices,)
self.register_action(action)
self.emit("subtitles-inserted", indices)
@aeidon.deco.export
@aeidon.deco.revertable
def merge_subtitles(self, indices, register=-1):
"""Merge subtitles at `indices` to form one subtitle."""
indices = sorted(indices)
subtitle = self.new_subtitle()
subtitle.start = self.subtitles[indices[0]].start
subtitle.end = self.subtitles[indices[-1]].end
main_texts = [self.subtitles[i].main_text for i in indices]
tran_texts = [self.subtitles[x].tran_text for x in indices]
subtitle.main_text = "\n".join(filter(None, main_texts))
subtitle.tran_text = "\n".join(filter(None, tran_texts))
self.remove_subtitles(indices, register=register)
self.insert_subtitles([indices[0]], [subtitle], register=register)
self.group_actions(register, 2, _("Merging subtitles"))
@aeidon.deco.export
@aeidon.deco.revertable
@aeidon.deco.notify_frozen
def remove_subtitles(self, indices, register=-1):
"""Remove subtitles at `indices`."""
indices = sorted(indices)
subtitles = [self.subtitles.pop(i) for i in reversed(indices)][::-1]
action = aeidon.RevertableAction(register=register)
action.docs = tuple(aeidon.documents)
action.description = _("Removing subtitles")
action.revert_function = self.insert_subtitles
action.revert_args = (indices, subtitles)
self.register_action(action)
self.emit("subtitles-removed", indices)
@aeidon.deco.export
@aeidon.deco.revertable
@aeidon.deco.notify_frozen
def replace_positions(self, indices, subtitles, register=-1):
"""Replace positions at `indices` with those from `subtitles`."""
orig_subtitles = [self.subtitles[i].copy() for i in indices]
for i, index in enumerate(indices):
self.subtitles[index].start = subtitles[i].start
self.subtitles[index].end = subtitles[i].end
action = aeidon.RevertableAction(register=register)
action.docs = tuple(aeidon.documents)
action.description = _("Replacing positions")
action.revert_function = self.replace_positions
action.revert_args = (indices, orig_subtitles)
self.register_action(action)
self.emit("positions-changed", indices)
@aeidon.deco.export
@aeidon.deco.revertable
@aeidon.deco.notify_frozen
def replace_texts(self, indices, doc, texts, register=-1):
"""Replace texts in `doc`'s `indices` with `texts`."""
orig_texts = [self.subtitles[i].get_text(doc) for i in indices]
for i, index in enumerate(indices):
self.subtitles[index].set_text(doc, texts[i])
action = aeidon.RevertableAction(register=register)
action.docs = (doc,)
action.description = _("Replacing texts")
action.revert_function = self.replace_texts
action.revert_args = (indices, doc, orig_texts)
self.register_action(action)
self.emit(self.get_text_signal(doc), indices)
@aeidon.deco.export
@aeidon.deco.revertable
def split_subtitle(self, index, register=-1):
"""Split subtitle in two equal duration ones."""
subtitle = self.subtitles[index]
middle = self.calc.get_middle(subtitle.start, subtitle.end)
subtitle_1 = self.new_subtitle()
subtitle_1.start = subtitle.start
subtitle_1.end = middle
subtitle_1.main_text = subtitle.main_text
subtitle_1.tran_text = subtitle.tran_text
subtitle_2 = self.new_subtitle()
subtitle_2.start = middle
subtitle_2.end = subtitle.end
self.remove_subtitles((index,), register=register)
indices = (index, index+1)
subtitles = (subtitle_1, subtitle_2)
self.insert_subtitles(indices, subtitles, register=register)
self.group_actions(register, 2, _("Splitting subtitle"))
|