gaupol/dialogs/position_shift.py

Source code for module gaupol.dialogs.position_shift from file gaupol/dialogs/position_shift.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
# -*- 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/>.

"""Dialogs for shifting positions."""

import aeidon
import gaupol
_ = aeidon.i18n._

from gi.repository import Gtk

__all__ = ("FrameShiftDialog", "TimeShiftDialog")


class PositionShiftDialog(gaupol.BuilderDialog):

    """Base class for dialogs for shifting positions."""

    _widgets = ("amount_spin",
                "current_radio",
                "preview_button",
                "selected_radio",
                "to_end_radio",
                "unit_label")

    def __init__(self, parent, application):
        """Initialize a :class:`PositionShiftDialog` instance."""
        gaupol.BuilderDialog.__init__(self, "position-shift-dialog.ui")
        self.application = application
        self._init_widgets()
        self._init_values()
        self._dialog.set_transient_for(parent)
        self._dialog.set_default_response(Gtk.ResponseType.OK)

    def _get_preview_row(self):
        """Return row to start preview from."""
        page = self.application.get_current_page()
        rows = page.view.get_selected_rows()
        return (rows[0] if rows else 0)

    def _get_target(self):
        """Return the selected target."""
        if self._selected_radio.get_active():
            return gaupol.targets.SELECTED
        if self._to_end_radio.get_active():
            return gaupol.targets.SELECTED_TO_END
        if self._current_radio.get_active():
            return gaupol.targets.CURRENT
        raise ValueError("Invalid target radio state")

    def _init_values(self):
        """Intialize default values for widgets."""
        target = gaupol.conf.position_shift.target
        self._selected_radio.set_active(target == gaupol.targets.SELECTED)
        self._to_end_radio.set_active(target == gaupol.targets.SELECTED_TO_END)
        self._current_radio.set_active(target == gaupol.targets.CURRENT)
        page = self.application.get_current_page()
        rows = page.view.get_selected_rows()
        if not rows and target in (
                gaupol.targets.SELECTED,
                gaupol.targets.SELECTED_TO_END):
            self._current_radio.set_active(True)
        self._selected_radio.set_sensitive(bool(rows))
        self._to_end_radio.set_sensitive(bool(rows))
        if (page.project.video_path is None or
            page.project.main_file is None):
            self._preview_button.set_sensitive(False)
        self._amount_spin.emit("value-changed")

    def _init_widgets(self):
        """Initialize widgets."""
        raise NotImplementedError

    def _on_amount_spin_value_changed(self, spin_button):
        """Set response sensitivity."""
        has_value = (spin_button.get_value() != 0.0)
        self.set_response_sensitive(Gtk.ResponseType.OK, has_value)

    def _on_preview_button_clicked(self, *args):
        """Preview shift changes with a video player."""
        page = self.application.get_current_page()
        target = self._get_target()
        rows = self.application.get_target_rows(target)
        self.application.preview_changes(page,
                                         self._get_preview_row(),
                                         aeidon.documents.MAIN,
                                         page.project.shift_positions,
                                         (rows, self._get_amount()))

    def _on_response(self, dialog, response):
        """Save target and shift positions."""
        gaupol.conf.position_shift.target = self._get_target()
        if response == Gtk.ResponseType.OK:
            self._shift_positions()

    def _shift_positions(self):
        """Shift positions in subtitles."""
        gaupol.util.set_cursor_busy(self)
        target = self._get_target()
        rows = self.application.get_target_rows(target)
        amount = self._get_amount()
        for page in self.application.get_target_pages(target):
            page.project.shift_positions(rows, amount)
        gaupol.util.set_cursor_normal(self)


class FrameShiftDialog(PositionShiftDialog):

    """Dialog for shifting frames."""

    def _get_amount(self):
        """Return the amount of frames to shift."""
        return aeidon.as_frame(self._amount_spin.get_value_as_int())

    def _init_widgets(self):
        """Initialize widgets."""
        self._amount_spin.set_numeric(True)
        self._amount_spin.set_digits(0)
        self._amount_spin.set_increments(1, 10)
        self._amount_spin.set_range(-9999999, 9999999)
        self._amount_spin.set_value(0)
        self._unit_label.set_text(_("frames"))


class TimeShiftDialog(PositionShiftDialog):

    """Dialog for shifting times."""

    def _get_amount(self):
        """Return the amount of seconds to shift."""
        return aeidon.as_seconds(self._amount_spin.get_value())

    def _init_widgets(self):
        """Initialize widgets."""
        self._amount_spin.set_numeric(True)
        self._amount_spin.set_digits(3)
        self._amount_spin.set_increments(0.1, 1)
        self._amount_spin.set_range(-99999, 99999)
        self._amount_spin.set_value(0.000)
        self._unit_label.set_text(_("seconds"))