gaupol/agents/edit.py¶
Source code for module gaupol.agents.edit from file gaupol/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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | # -*- 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/>.
"""Editing subtitle data."""
import aeidon
import bisect
import gaupol
_ = aeidon.i18n._
class EditAgent(aeidon.Delegate):
"""Editing subtitle data."""
def __init__(self, master):
"""Initialize an :class:`EditAgent` instance."""
aeidon.Delegate.__init__(self, master)
self._pref_dialog = None
@aeidon.deco.export
def _on_clear_texts_activate(self, *args):
"""Clear the selected texts."""
page = self.get_current_page()
rows = page.view.get_selected_rows()
col = page.view.get_focus()[1]
doc = page.text_column_to_document(col)
page.project.clear_texts(rows, doc)
@aeidon.deco.export
def _on_copy_texts_activate(self, *args):
"""Copy the selected texts to the clipboard."""
page = self.get_current_page()
rows = page.view.get_selected_rows()
col = page.view.get_focus()[1]
doc = page.text_column_to_document(col)
page.project.copy_texts(rows, doc)
self._sync_clipboards(page)
@aeidon.deco.export
def _on_cut_texts_activate(self, *args):
"""Cut the selected texts to the clipboard."""
page = self.get_current_page()
rows = page.view.get_selected_rows()
col = page.view.get_focus()[1]
doc = page.text_column_to_document(col)
page.project.cut_texts(rows, doc)
self._sync_clipboards(page)
@aeidon.deco.export
def _on_edit_next_value_activate(self, *args):
"""Edit the focused column of the next subtitle."""
page = self.get_current_page()
path, column = page.view.get_cursor()
row = gaupol.util.tree_path_to_row(path)
path = gaupol.util.tree_row_to_path(row+1)
page.view.set_cursor(path, column, True)
@aeidon.deco.export
def _on_edit_preferences_activate(self, *args):
"""Configure Gaupol."""
if self._pref_dialog is not None:
return self._pref_dialog.present()
self._pref_dialog = gaupol.PreferencesDialog(self.window, self)
aeidon.util.connect(self, "_pref_dialog", "response")
self._pref_dialog.show()
@aeidon.deco.export
def _on_edit_value_activate(self, *args):
"""Edit the focused cell."""
page = self.get_current_page()
path, column = page.view.get_cursor()
page.view.set_cursor(path, column, True)
@aeidon.deco.export
def _on_end_earlier_activate(self, *args):
"""End the selected subtitle earlier."""
page = self.get_current_page()
row = page.view.get_selected_rows()[0]
pos = page.project.subtitles[row].end_seconds
length = gaupol.conf.editor.stretch_length
value = ((pos-0.001) // length) * length
page.project.set_end(row, value)
register = aeidon.registers.DO
description = _("Stretching end position")
page.project.set_action_description(register, description)
# Group repeated stretches as one action.
if (len(page.project.undoables) > 1 and
page.project.undoables[1].description ==
page.project.undoables[0].description):
page.project.group_actions(register, 2, description)
@aeidon.deco.export
def _on_end_later_activate(self, *args):
"""End the selected subtitle later."""
page = self.get_current_page()
row = page.view.get_selected_rows()[0]
pos = page.project.subtitles[row].end_seconds
length = gaupol.conf.editor.stretch_length
value = (((pos+0.001) // length) + 1) * length
page.project.set_end(row, value)
register = aeidon.registers.DO
description = _("Stretching end position")
page.project.set_action_description(register, description)
# Group repeated stretches as one action.
if (len(page.project.undoables) > 1 and
page.project.undoables[1].description ==
page.project.undoables[0].description):
page.project.group_actions(register, 2, description)
@aeidon.deco.export
def _on_extend_selection_to_beginning_activate(self, *args):
"""Extend the selection up to the first subtitle."""
page = self.get_current_page()
row = page.view.get_selected_rows()[-1]
rows = list(range(0, row+1))
page.view.select_rows(rows)
@aeidon.deco.export
def _on_extend_selection_to_end_activate(self, *args):
"""Extend the selection up to the last subtitle."""
page = self.get_current_page()
row = page.view.get_selected_rows()[0]
rows = list(range(row, len(page.project.subtitles)))
page.view.select_rows(rows)
@aeidon.deco.export
def _on_insert_subtitle_at_video_position_activate(self, *args):
"""Insert a new subtitle at video position."""
mode = aeidon.modes.SECONDS
pos = self.player.get_position(mode)
page = self.get_current_page()
starts = [x.start_seconds for x in page.project.subtitles]
index = bisect.bisect_right(starts, pos)
subtitle = page.project.new_subtitle()
subtitle.start_seconds = pos
subtitle.end_seconds = pos + 3.0
subtitle.main_text = "[{:d}]".format(index+1)
page.project.insert_subtitles((index,), (subtitle,))
@aeidon.deco.export
def _on_insert_subtitles_activate(self, *args):
"""Insert subtitles."""
dialog = gaupol.InsertDialog(self.window, self)
gaupol.util.flash_dialog(dialog)
@aeidon.deco.export
def _on_invert_selection_activate(self, *args):
"""Invert the current selection."""
page = self.get_current_page()
rows = set(range(0, len(page.project.subtitles)))
rows -= set(page.view.get_selected_rows())
page.view.select_rows(rows)
@aeidon.deco.export
def _on_merge_subtitles_activate(self, *args):
"""Merge the selected subtitles."""
page = self.get_current_page()
rows = page.view.get_selected_rows()
page.project.merge_subtitles(rows)
@aeidon.deco.export
def _on_paste_texts_activate(self, *args):
"""Paste texts from the clipboard."""
page = self.get_current_page()
rows = page.view.get_selected_rows()
row, col = page.view.get_focus()
doc = page.text_column_to_document(col)
length = len(page.project.subtitles)
# Ensure that even if new subtitles need to be inserted,
# focus and scroll position are not moved to the end.
rect = page.view.get_visible_rect()
window = page.view.get_bin_window()
window.freeze_updates()
rows = page.project.paste_texts(rows[0], doc)
rows = page.view.get_selected_rows()
page.view.set_focus(row, col)
page.view.select_rows(rows)
page.view.scroll_to_point(rect.x, rect.y)
window.thaw_updates()
count = len(page.project.subtitles) - length
if count <= 0: return
self.flash_message(aeidon.i18n.ngettext(
"Inserted {:d} subtitle to fit clipboard contents",
"Inserted {:d} subtitles to fit clipboard contents",
count).format(count))
def _on_pref_dialog_response(self, *args):
"""Destroy the preferences dialog."""
self._pref_dialog.destroy()
self._pref_dialog = None
gaupol.conf.write_to_file()
@aeidon.deco.export
def _on_project_action_done(self, *args):
"""Update user interface and send a signal."""
page = self.get_current_page()
self.update_gui()
self.emit("page-changed", page)
@aeidon.deco.export
def _on_project_action_redone(self, *args):
"""Update user interface and send a signal."""
page = self.get_current_page()
row = page.view.get_focus()[0]
if row is not None:
page.view.scroll_to_row(row)
self.update_gui()
self.emit("page-changed", page)
@aeidon.deco.export
def _on_project_action_undone(self, *args):
"""Update user interface and send a signal."""
page = self.get_current_page()
row = page.view.get_focus()[0]
if row is not None:
page.view.scroll_to_row(row)
self.update_gui()
self.emit("page-changed", page)
@aeidon.deco.export
def _on_redo_action_activate(self, *args):
"""Redo the last undone action."""
self.redo()
@aeidon.deco.export
def _on_remove_subtitles_activate(self, *args):
"""Remove the selected subtitles."""
page = self.get_current_page()
rows = page.view.get_selected_rows()
page.project.remove_subtitles(rows)
@aeidon.deco.export
def _on_select_all_activate(self, *args):
"""Select all subtitles."""
page = self.get_current_page()
selection = page.view.get_selection()
selection.select_all()
@aeidon.deco.export
def _on_set_end_from_video_position_activate(self, *args):
"""Set subtitle end from video position."""
page = self.get_current_page()
mode = aeidon.modes.SECONDS
row = page.view.get_selected_rows()[0]
pos = self.player.get_position(mode)
page.project.set_end(row, pos)
@aeidon.deco.export
def _on_split_subtitle_activate(self, *args):
"""Split the selected subtitle."""
page = self.get_current_page()
row = page.view.get_selected_rows()[0]
page.project.split_subtitle(row)
@aeidon.deco.export
def _on_start_earlier_activate(self, *args):
"""Start the selected subtitle earlier."""
page = self.get_current_page()
row = page.view.get_selected_rows()[0]
pos = page.project.subtitles[row].start_seconds
length = gaupol.conf.editor.stretch_length
value = ((pos-0.001) // length) * length
page.project.set_start(row, value)
register = aeidon.registers.DO
description = _("Stretching start position")
page.project.set_action_description(register, description)
# Group repeated stretches as one action.
if (len(page.project.undoables) > 1 and
page.project.undoables[1].description ==
page.project.undoables[0].description):
page.project.group_actions(register, 2, description)
@aeidon.deco.export
def _on_start_later_activate(self, *args):
"""Start the selected subtitle later."""
page = self.get_current_page()
row = page.view.get_selected_rows()[0]
pos = page.project.subtitles[row].start_seconds
length = gaupol.conf.editor.stretch_length
value = (((pos+0.001) // length) + 1) * length
page.project.set_start(row, value)
register = aeidon.registers.DO
description = _("Stretching start position")
page.project.set_action_description(register, description)
# Group repeated stretches as one action.
if (len(page.project.undoables) > 1 and
page.project.undoables[1].description ==
page.project.undoables[0].description):
page.project.group_actions(register, 2, description)
@aeidon.deco.export
def _on_undo_action_activate(self, *args):
"""Undo the last action."""
self.undo()
@aeidon.deco.export
def _on_view_renderer_edited(self, renderer, path, value, column):
"""Save changes made while editing cell."""
self._set_unsafe_sensitivities(True)
self.show_message(None)
page = self.get_current_page()
row = gaupol.util.tree_path_to_row(path)
col = page.view.get_columns().index(column)
if page.view.is_position_column(col):
if not value: return
if page.edit_mode == aeidon.modes.FRAME:
with aeidon.util.silent(ValueError):
value = aeidon.as_frame(value)
if col == page.view.columns.START:
return page.project.set_start(row, value)
if col == page.view.columns.END:
return page.project.set_end(row, value)
if col == page.view.columns.DURATION:
if page.edit_mode == aeidon.modes.TIME:
value = value.replace(",", ".")
with aeidon.util.silent(ValueError):
value = aeidon.as_seconds(value)
return page.project.set_duration(row, value)
doc = page.text_column_to_document(col)
page.project.set_text(row, doc, value)
@aeidon.deco.export
def _on_view_renderer_editing_canceled(self, *args):
"""Unset state set for editing cell."""
self._set_unsafe_sensitivities(True)
self.show_message(None)
@aeidon.deco.export
def _on_view_renderer_editing_started(self, renderer, editor, path, column):
"""Set proper state for editing cell."""
self._set_unsafe_sensitivities(False)
page = self.get_current_page()
col = page.view.get_columns().index(column)
if not page.view.is_text_column(col): return
self.show_message(_("Use Shift+Return for line-break"))
@aeidon.deco.export
def redo(self, count=1):
"""Redo `count` amount of actions."""
gaupol.util.set_cursor_busy(self.window)
page = self.get_current_page()
if page.project.can_redo(count):
page.project.redo(count)
gaupol.util.set_cursor_normal(self.window)
def _set_unsafe_sensitivities(self, sensitive):
"""Set sensitivities of unsafe UI manager actions."""
action_group = self.get_action_group("main-unsafe")
action_group.set_sensitive(sensitive)
def _sync_clipboards(self, page):
"""Synchronize all clipboards to match that of `page`."""
texts = page.project.clipboard.get_texts()
self.clipboard.set_texts(texts)
for item in self.pages:
item.project.clipboard.set_texts(texts)
text = page.project.clipboard.get_string()
self.x_clipboard.set_text(text, -1)
self.update_gui()
@aeidon.deco.export
def undo(self, count=1):
"""Undo `count` amount of actions."""
gaupol.util.set_cursor_busy(self.window)
page = self.get_current_page()
if page.project.can_undo(count):
page.project.undo(count)
gaupol.util.set_cursor_normal(self.window)
|