File: insertsubtitlefromkeyframe.cc

package info (click to toggle)
subtitleeditor 0.54.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 10,388 kB
  • sloc: cpp: 29,756; sh: 4,409; makefile: 2,118; perl: 434; xml: 142
file content (247 lines) | stat: -rw-r--r-- 6,236 bytes parent folder | download | duplicates (3)
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
/*
 *	subtitleeditor -- a tool to create or edit subtitle
 *
 *	https://kitone.github.io/subtitleeditor/
 *	https://github.com/kitone/subtitleeditor/
 *
 *	Copyright @ 2005-2011, 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 <extension/action.h>
#include <i18n.h>
#include <debug.h>
#include <player.h>
#include <keyframes.h>

class InsertSubtitleFromKeyframePlugin : public Action
{
public:

	InsertSubtitleFromKeyframePlugin()
	{
		activate();
		update_ui();
	}

	~InsertSubtitleFromKeyframePlugin()
	{
		deactivate();
	}

	/*
	 */
	void activate()
	{
		se_debug(SE_DEBUG_PLUGINS);

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

		action_group->add(
				Gtk::Action::create("insert-subtitle-between-keyframes", Gtk::Stock::ADD, _("Create Subtitle From Player And Keyframes"), _("Create subtitle automatically according to keyframes around the position of the player.")), 
					sigc::mem_fun(*this, &InsertSubtitleFromKeyframePlugin::on_insert_subtitle_between_keyframes));
		
		action_group->add(
				Gtk::Action::create("insert-subtitle-between-each-keyframes", Gtk::Stock::ADD, _("Create Subtitles According Keyframes"), _("Create subtitles automatically according to keyframes")), 
					sigc::mem_fun(*this, &InsertSubtitleFromKeyframePlugin::on_insert_subtitle_between_each_keyframes));

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

		ui->insert_action_group(action_group);

		Glib::ustring submenu = 
			"<ui>"
			"	<menubar name='menubar'>"
			"		<menu name='menu-keyframes' action='menu-keyframes'>"
			"			<placeholder name='placeholder-2'>"
			"				<separator />"
			"				<menuitem action='insert-subtitle-between-keyframes'/>"
			"				<menuitem action='insert-subtitle-between-each-keyframes'/>"
			"				<separator />"
			"			</placeholder>"
			"		</menu>"
			"	</menubar>"
			"</ui>";

		ui_id = ui->add_ui_from_string(submenu);

		// connect the player state signals
		player()->signal_message().connect(
				sigc::mem_fun(*this, &InsertSubtitleFromKeyframePlugin::on_player_message));
	}

	/*
	 */
	void deactivate()
	{
		se_debug(SE_DEBUG_PLUGINS);

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

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

	/*
	 */
	void update_ui()
	{
		se_debug(SE_DEBUG_PLUGINS);

		bool has_doc = (get_current_document() != NULL);
		bool has_kf =  (bool)player()->get_keyframes();
		bool has_media = player()->get_state() != Player::NONE;

		action_group->get_action("insert-subtitle-between-keyframes")->set_sensitive(has_doc && has_kf && has_media);
		action_group->get_action("insert-subtitle-between-each-keyframes")->set_sensitive(has_doc && has_kf);
	}

	/*
	 */
	void on_player_message(Player::Message msg)
	{
		// only if the player is enable or disable
		// don't update if is playing or paused
		if(msg == Player::STREAM_READY || msg == Player::STATE_NONE)
			update_ui();
		else if(msg == Player::KEYFRAME_CHANGED)
			update_ui();
	}

protected:

	/*
	 */
	Player* player()
	{
		return get_subtitleeditor_window()->get_player();
	}

	/*
	 */
	void on_insert_subtitle_between_keyframes()
	{
		se_debug(SE_DEBUG_PLUGINS);

		long start = 0, end = 0;

		if(!get_keyframes_from_player(start, end))
			return;

		Document *doc = get_current_document();
		g_return_if_fail(doc);

		doc->start_command(_("Insert Subtitle Between Keyframes"));

		Subtitles subtitles = doc->subtitles();

		Subtitle sub = subtitles.append();

		sub.set_start_and_end(start, end);
 
		doc->subtitles().select(sub);
		doc->subtitles().sort_by_time();

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

	/*
	 * Sets the value of the keyframes positions around the player position and return true.
	 */
	bool get_keyframes_from_player(long &start, long &end)
	{
		Glib::RefPtr<KeyFrames> keyframes = player()->get_keyframes();
		g_return_val_if_fail(keyframes, false);

		long pos = player()->get_position();

		KeyFrames::const_iterator prev = keyframes->begin();
		for(KeyFrames::const_iterator it = keyframes->begin(); it != keyframes->end(); ++it)
		{
			if(*it > pos)
			{
				if(*it != *prev)
				{
					start = *prev;
					end = *it;
					return true;
				}
			}
			prev = it;
		}
		return false;
	}


	/*
	 */
	void on_insert_subtitle_between_each_keyframes()
	{
		Document *doc = get_current_document();
		g_return_if_fail(doc);

		Player* player = get_subtitleeditor_window()->get_player();

		Glib::RefPtr<KeyFrames> keyframes = player->get_keyframes();
		g_return_if_fail(keyframes);

		if(keyframes->size() < 2)
		{
			doc->flash_message(_("Can't insert subtitle between keyframes, not enough keyframes."));
			return;
		}
		
		int min_display = get_config().get_value_int("timing", "min-display");
		int count = 0;

		doc->start_command(_("Insert Subtitle Between Each Keyframes"));

		Subtitles subtitles = doc->subtitles();

		KeyFrames::const_iterator it = keyframes->begin();
		KeyFrames::const_iterator prev = it;

		for(++it; it != keyframes->end(); ++it)
		{
			// Only if the min display is respected
			if((*it-*prev) >= min_display)
			{
				Subtitle sub = subtitles.append();
				sub.set_start_and_end(*prev, *it);
				++count;
			}
			prev = it;
		}
		doc->subtitles().sort_by_time();
		doc->finish_command();
		doc->emit_signal("subtitle-time-changed");

		doc->flash_message(ngettext(
					"1 subtitle has been inserted.",
					"%d subtitles have been inserted.", 
					count), count);
	}


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


REGISTER_EXTENSION(InsertSubtitleFromKeyframePlugin)