File: adjusttime.cc

package info (click to toggle)
subtitleeditor 0.56.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,880 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (304 lines) | stat: -rw-r--r-- 10,536 bytes parent folder | download
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
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// 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/>.

#include <debug.h>
#include <extension/action.h>
#include <i18n.h>

#include "timeutility.h"

class AdjustTimePlugin : public Action {
  public:
   AdjustTimePlugin() {
      activate();
      update_ui();
   }

   ~AdjustTimePlugin() {
      deactivate();
   }

   void activate() {
      se_dbg(SE_DBG_PLUGINS);

      // actions
      action_group = Gtk::ActionGroup::create("AdjustTimePlugin");

      // -- 100 ms --

      // menu add
      action_group->add(Gtk::Action::create("menu-adjust-time-add", Gtk::Stock::ADD, _("Add 100 Milliseconds"), _("Add 100 Milliseconds")));

      action_group->add(Gtk::Action::create("add-to-start", _("To Start"), _("Add 100 Milliseconds to start for all subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_add_to_start));

      action_group->add(Gtk::Action::create("add-to-duration", _("To Duration"), _("Add 100 Milliseconds to duration for all subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_add_to_duration));

      action_group->add(
         Gtk::Action::create("add-to-start-and-to-duration", _("To Start And To Duration"), _("Add 100 Milliseconds to all subtitles selected")),
         sigc::mem_fun(*this, &AdjustTimePlugin::on_add_to_start_and_to_duration));

      // menu remove
      action_group->add(
         Gtk::Action::create("menu-adjust-time-remove", Gtk::Stock::REMOVE, _("Remove 100 Milliseconds"), _("Remove 100 Milliseconds")));

      action_group->add(Gtk::Action::create("remove-from-start", _("From Start"), _("Remove 100 Milliseconds from start for all subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_remove_from_start));

      action_group->add(Gtk::Action::create("remove-from-duration",
                                            _("From Duration"),
                                            _("Remove 100 Milliseconds from duration for all "
                                              "subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_remove_from_duration));

      action_group->add(
         Gtk::Action::create(
            "remove-from-start-and-from-duration", _("From Start And From Duration"), _("Remove 100 Milliseconds from all subtitles selected")),
         sigc::mem_fun(*this, &AdjustTimePlugin::on_remove_from_start_and_from_duration));

      // -- 1 frame --

      // menu add
      action_group->add(Gtk::Action::create("menu-adjust-time-add-frame", Gtk::Stock::ADD, _("Add 1 Frame"), _("Add 1 Frame")));

      action_group->add(Gtk::Action::create("add-frame-to-start", _("To Start"), _("Add 1 Frame to start for all subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_add_frame_to_start));

      action_group->add(Gtk::Action::create("add-frame-to-duration", _("To Duration"), _("Add 1 Frame to duration for all subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_add_frame_to_duration));

      action_group->add(
         Gtk::Action::create("add-frame-to-start-and-to-duration", _("To Start And To Duration"), _("Add 1 Frame to all subtitles selected")),
         sigc::mem_fun(*this, &AdjustTimePlugin::on_add_frame_to_start_and_to_duration));

      // menu remove
      action_group->add(Gtk::Action::create("menu-adjust-time-remove-frame", Gtk::Stock::REMOVE, _("Remove 1 Frame"), _("Remove 1 Frame")));

      action_group->add(Gtk::Action::create("remove-frame-from-start", _("From Start"), _("Remove 1 Frame from start for all subtitles selected")),
                        sigc::mem_fun(*this, &AdjustTimePlugin::on_remove_frame_from_start));

      action_group->add(
         Gtk::Action::create("remove-frame-from-duration", _("From Duration"), _("Remove 1 Frame from duration for all subtitles selected")),
         sigc::mem_fun(*this, &AdjustTimePlugin::on_remove_frame_from_duration));

      action_group->add(
         Gtk::Action::create(
            "remove-frame-from-start-and-from-duration", _("From Start And From Duration"), _("Remove 1 Frame from all subtitles selected")),
         sigc::mem_fun(*this, &AdjustTimePlugin::on_remove_frame_from_start_and_from_duration));

      // ui
      Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();

      ui->insert_action_group(action_group);

      Glib::ustring submenu = R"(
      <ui>
        <menubar name='menubar'>
          <menu name='menu-timings' action='menu-timings'>
            <placeholder name='adjust-time'>
              <menu action='menu-adjust-time-add'>
                <menuitem action='add-to-start'/>
                <menuitem action='add-to-duration'/>
                <menuitem action='add-to-start-and-to-duration'/>
              </menu>
              <menu action='menu-adjust-time-remove'>
                <menuitem action='remove-from-start'/>
                <menuitem action='remove-from-duration'/>
                <menuitem action='remove-from-start-and-from-duration'/>
              </menu>
              <menu action='menu-adjust-time-add-frame'>
                <menuitem action='add-frame-to-start'/>
                <menuitem action='add-frame-to-duration'/>
                <menuitem action='add-frame-to-start-and-to-duration'/>
              </menu>
              <menu action='menu-adjust-time-remove-frame'>
                <menuitem action='remove-frame-from-start'/>
                <menuitem action='remove-frame-from-duration'/>
                <menuitem action='remove-frame-from-start-and-from-duration'/>
              </menu>
            </placeholder>
          </menu>
        </menubar>
      </ui>
    )";

      ui_id = ui->add_ui_from_string(submenu);
   }

   void deactivate() {
      se_dbg(SE_DBG_PLUGINS);

      Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();

      ui->remove_ui(ui_id);
      ui->remove_action_group(action_group);
   }

   void update_ui() {
      se_dbg(SE_DBG_PLUGINS);

      bool visible = (get_current_document() != NULL);

      action_group->get_action("menu-adjust-time-add")->set_sensitive(visible);
      action_group->get_action("menu-adjust-time-remove")->set_sensitive(visible);
      action_group->get_action("menu-adjust-time-add-frame")->set_sensitive(visible);
      action_group->get_action("menu-adjust-time-remove-frame")->set_sensitive(visible);
   }

  protected:
   enum TYPE { START, END, START_AND_END };

   enum UNITS { MSEC, FRAMES };

   void on_add_to_start() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START, 100);
   }

   void on_add_to_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(END, 100);
   }

   void on_add_to_start_and_to_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START_AND_END, 100);
   }

   void on_remove_from_start() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START, -100);
   }

   void on_remove_from_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(END, -100);
   }

   void on_remove_from_start_and_from_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START_AND_END, -100);
   }

   // --- FRAME VERSIONS ---

   void on_add_frame_to_start() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START, 1, FRAMES);
   }

   void on_add_frame_to_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(END, 1, FRAMES);
   }

   void on_add_frame_to_start_and_to_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START_AND_END, 1, FRAMES);
   }

   void on_remove_frame_from_start() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START, -1, FRAMES);
   }

   void on_remove_frame_from_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(END, -1, FRAMES);
   }

   void on_remove_frame_from_start_and_from_duration() {
      se_dbg(SE_DBG_PLUGINS);

      adjust(START_AND_END, -100);
   }

   bool adjust(TYPE type, const long& time_msecs = 100, UNITS units = MSEC) {
      long timeshift = time_msecs;

      se_dbg(SE_DBG_PLUGINS);

      Document* doc = get_current_document();

      g_return_val_if_fail(doc, false);

      Subtitles subtitles = doc->subtitles();

      std::vector<Subtitle> selection = subtitles.get_selection();

      if (selection.size() == 0) {
         doc->flash_message(_("Please select at least a subtitle."));
         return false;
      }

      doc->start_command(_("Adjust time"));

      if (units == FRAMES) {
         float framerate = get_framerate_value(doc->get_framerate());
         float msecperframe = (float)1000 / framerate;
         float floatshift = (float)timeshift * msecperframe;
         timeshift = (long)floatshift;
      }

      if (type == START) {
         for (unsigned int i = 0; i < selection.size(); ++i) {
            Subtitle subtitle = selection[i];

            subtitle.set_start(SubtitleTime(subtitle.get_start().totalmsecs + timeshift));
         }
      } else if (type == END) {
         for (unsigned int i = 0; i < selection.size(); ++i) {
            Subtitle subtitle = selection[i];

            subtitle.set_end(SubtitleTime(subtitle.get_end().totalmsecs + timeshift));
         }
      } else if (type == START_AND_END) {
         for (unsigned int i = 0; i < selection.size(); ++i) {
            Subtitle subtitle = selection[i];

            subtitle.set_start_and_end(SubtitleTime(subtitle.get_start().totalmsecs + timeshift),
                                       SubtitleTime(subtitle.get_end().totalmsecs + timeshift));
         }
      }

      doc->emit_signal("subtitle-time-changed");
      doc->finish_command();

      return true;
   }

  protected:
   Gtk::UIManager::ui_merge_id ui_id;
   Glib::RefPtr<Gtk::ActionGroup> action_group;
};

REGISTER_EXTENSION(AdjustTimePlugin)