File: splitdocument.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 (201 lines) | stat: -rw-r--r-- 4,326 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
/*
 *	subtitleeditor -- a tool to create or edit subtitle
 *
 *	https://kitone.github.io/subtitleeditor/
 *	https://github.com/kitone/subtitleeditor/
 *
 *	Copyright @ 2005-2014, 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 <utility.h>
#include <documentsystem.h>
#include <gtkmm_utility.h>
#include <memory>

/*
 *
 */
class DialogSplitDocument : public Gtk::Dialog
{
public:
	DialogSplitDocument(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
	:Gtk::Dialog(cobject)
	{
		utility::set_transient_parent(*this);
		
		builder->get_widget("spin-number", m_spinNumber);

		set_default_response(Gtk::RESPONSE_OK);
	}

	/*
	 *
	 */
	void execute(Document *doc)
	{
		g_return_if_fail(doc);

		unsigned int size = doc->subtitles().size();

		if(size == 0)
		{
			dialog_warning(
					_("You can't use <i>split</i> with this document."), 
					build_message("The document <b>%s</b> has not subtitle, it's empty.", doc->getName().c_str()));
			return;
		}

		m_spinNumber->set_range(1,size);

		// set by default the first selected subtitle
		{
			Subtitle selected = doc->subtitles().get_first_selected();
			if(selected)
				m_spinNumber->set_value(selected.get_num());

		}
		show();

		if(run() == Gtk::RESPONSE_OK)
		{
			unsigned int number = (unsigned int)m_spinNumber->get_value();

			split_doc(doc, number);
		}
		
		hide();
	}

	/*
	 * Split the document in two and return the new one
	 */
	Document *split_doc(Document *doc, unsigned int number)
	{
		// Create new document based on the first one and rename it
		Document *newdoc = new Document(*doc, true);
		newdoc->setFilename(newdoc->getFilename() + "-par2");
		newdoc->subtitles().remove(1, number-1);

		DocumentSystem::getInstance().append(newdoc);

		// Remove subtitles used by the new one
		doc->start_command(_("Split document"));
		doc->subtitles().remove(number, doc->subtitles().size());
		doc->finish_command();

		return newdoc;
	}

protected:
	Gtk::SpinButton* m_spinNumber;
};

/*
 *
 */
class SplitDocumentPlugin : public Action
{
public:

	SplitDocumentPlugin()
	{
		activate();
		update_ui();
	}

	~SplitDocumentPlugin()
	{
		deactivate();
	}

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

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

		action_group->add(
				Gtk::Action::create("split-document", Gtk::Stock::CUT, _("Spl_it Document"), _("Split the current document in two")), 
					sigc::mem_fun(*this, &SplitDocumentPlugin::on_execute));

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

		ui_id = ui->new_merge_id();

		ui->insert_action_group(action_group);

		ui->add_ui(ui_id, "/menubar/menu-tools/split-document", "split-document", "split-document");
	}

	/*
	 *
	 */
	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 visible = (get_current_document() != NULL);

		action_group->get_action("split-document")->set_sensitive(visible);
	}

protected:

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

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

		// create dialog
		std::unique_ptr<DialogSplitDocument> dialog(
				gtkmm_utility::get_widget_derived<DialogSplitDocument>(
						SE_DEV_VALUE(SE_PLUGIN_PATH_UI, SE_PLUGIN_PATH_DEV),
						"dialog-split-document.ui", 
						"dialog-split-document"));

		dialog->execute(doc);
	}
	
protected:
	Gtk::UIManager::ui_merge_id ui_id;
	Glib::RefPtr<Gtk::ActionGroup> action_group;
};

REGISTER_EXTENSION(SplitDocumentPlugin)