2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "subtitle_appearance_dialog.h"
23 #include "rgba_colour_picker.h"
24 #include "static_text.h"
25 #include "check_box.h"
26 #include "dcpomatic_button.h"
27 #include "lib/string_text_file_content.h"
28 #include "lib/text_content.h"
29 #include "lib/ffmpeg_subtitle_stream.h"
30 #include "lib/ffmpeg_content.h"
31 #include "lib/examine_ffmpeg_subtitles_job.h"
32 #include "lib/job_manager.h"
33 #include "lib/warnings.h"
34 DCPOMATIC_DISABLE_WARNINGS
36 #include <wx/clrpicker.h>
37 #include <wx/spinctrl.h>
38 #include <wx/gbsizer.h>
39 DCPOMATIC_ENABLE_WARNINGS
44 using std::shared_ptr;
46 using std::dynamic_pointer_cast;
47 using boost::optional;
48 using namespace dcpomatic;
49 #if BOOST_VERSION >= 106100
50 using namespace boost::placeholders;
54 int const SubtitleAppearanceDialog::NONE = 0;
55 int const SubtitleAppearanceDialog::OUTLINE = 1;
56 int const SubtitleAppearanceDialog::SHADOW = 2;
59 SubtitleAppearanceDialog::SubtitleAppearanceDialog (wxWindow* parent, shared_ptr<const Film> film, shared_ptr<Content> content, shared_ptr<TextContent> text)
60 : wxDialog (parent, wxID_ANY, _("Subtitle appearance"))
65 auto ff = dynamic_pointer_cast<FFmpegContent> (content);
67 _stream = ff->subtitle_stream ();
68 /* XXX: assuming that all FFmpeg streams have bitmap subs */
69 if (_stream->colours().empty()) {
70 _job_manager_connection = JobManager::instance()->ActiveJobsChanged.connect(boost::bind(&SubtitleAppearanceDialog::active_jobs_changed, this, _1));
71 _job = JobManager::instance()->add(shared_ptr<Job>(new ExamineFFmpegSubtitlesJob(film, ff)));
75 _overall_sizer = new wxBoxSizer (wxVERTICAL);
76 SetSizer (_overall_sizer);
78 _table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
80 _overall_sizer->Add (_table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
84 add_label_to_sizer (_table, this, _("Colour"), true, wxGBPosition(r, 0));
85 _force_colour = set_to (_colour = new wxColourPickerCtrl (this, wxID_ANY), r);
87 add_label_to_sizer (_table, this, _("Effect"), true, wxGBPosition(r, 0));
88 _force_effect = set_to (_effect = new wxChoice (this, wxID_ANY), r);
90 add_label_to_sizer (_table, this, _("Effect colour"), true, wxGBPosition(r, 0));
91 _force_effect_colour = set_to (_effect_colour = new wxColourPickerCtrl (this, wxID_ANY), r);
93 add_label_to_sizer (_table, this, _("Outline width"), true, wxGBPosition(r, 0));
94 _outline_width = new wxSpinCtrl (this, wxID_ANY);
95 _table->Add (_outline_width, wxGBPosition(r, 1));
98 add_label_to_sizer (_table, this, _("Fade in time"), true, wxGBPosition(r, 0));
99 _force_fade_in = set_to (_fade_in = new Timecode<ContentTime> (this), r);
101 add_label_to_sizer (_table, this, _("Fade out time"), true, wxGBPosition(r, 0));
102 _force_fade_out = set_to (_fade_out = new Timecode<ContentTime> (this), r);
105 _colours_panel = new wxScrolled<wxPanel> (this);
106 _colours_panel->EnableScrolling (false, true);
107 _colours_panel->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_ALWAYS);
108 _colours_panel->SetScrollRate (0, 16);
110 _colour_table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
111 _colour_table->AddGrowableCol (1, 1);
113 wxStaticText* t = new StaticText (_colours_panel, "");
114 t->SetLabelMarkup (_("<b>Original colour</b>"));
115 _colour_table->Add (t, 1, wxEXPAND);
116 t = new StaticText (_colours_panel, "", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
117 t->SetLabelMarkup (_("<b>New colour</b>"));
118 _colour_table->Add (t, 1, wxALIGN_CENTER);
120 shared_ptr<Job> job = _job.lock ();
121 if (!job || job->finished()) {
125 _colours_panel->SetSizer (_colour_table);
127 /* XXX: still assuming that all FFmpeg streams have bitmap subs */
128 if (_stream->colours().empty()) {
129 _finding = new wxStaticText(this, wxID_ANY, _("Finding the colours in these subtitles..."));
130 _overall_sizer->Add (_finding, 0, wxALL, DCPOMATIC_DIALOG_BORDER);
131 _colours_panel->Show (false);
134 _overall_sizer->Add (_colours_panel, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
136 wxButton* restore = new Button (this, _("Restore to original colours"));
137 restore->Bind (wxEVT_BUTTON, bind (&SubtitleAppearanceDialog::restore, this));
138 _overall_sizer->Add (restore, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
141 auto buttons = CreateSeparatedButtonSizer (wxOK);
143 _overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
146 _overall_sizer->Layout ();
147 _overall_sizer->SetSizeHints (this);
149 /* Keep these Appends() up to date with NONE/OUTLINE/SHADOW variables */
150 _effect->Append (_("None"));
151 _effect->Append (_("Outline"));
152 _effect->Append (_("Shadow"));;
154 auto colour = _text->colour();
155 _force_colour->SetValue (static_cast<bool>(colour));
157 _colour->SetColour (wxColour (colour->r, colour->g, colour->b));
159 _colour->SetColour (wxColour (255, 255, 255));
162 auto effect = _text->effect();
163 _force_effect->SetValue (static_cast<bool>(effect));
166 case dcp::Effect::NONE:
167 _effect->SetSelection (NONE);
169 case dcp::Effect::BORDER:
170 _effect->SetSelection (OUTLINE);
172 case dcp::Effect::SHADOW:
173 _effect->SetSelection (SHADOW);
177 _effect->SetSelection (NONE);
180 auto effect_colour = _text->effect_colour();
181 _force_effect_colour->SetValue (static_cast<bool>(effect_colour));
183 _effect_colour->SetColour (wxColour (effect_colour->r, effect_colour->g, effect_colour->b));
185 _effect_colour->SetColour (wxColour (0, 0, 0));
188 auto fade_in = _text->fade_in();
189 _force_fade_in->SetValue (static_cast<bool>(fade_in));
191 _fade_in->set (*fade_in, _content->active_video_frame_rate(film));
193 _fade_in->set (ContentTime(), _content->active_video_frame_rate(film));
196 auto fade_out = _text->fade_out();
197 _force_fade_out->SetValue (static_cast<bool>(fade_out));
199 _fade_out->set (*fade_out, _content->active_video_frame_rate(film));
201 _fade_out->set (ContentTime(), _content->active_video_frame_rate(film));
204 _outline_width->SetValue (_text->outline_width ());
206 _force_colour->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
207 _force_effect_colour->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
208 _force_effect->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
209 _force_fade_in->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
210 _force_fade_out->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
211 _effect->Bind (wxEVT_CHOICE, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
212 _content_connection = _content->Change.connect (bind (&SubtitleAppearanceDialog::content_change, this, _1));
214 setup_sensitivity ();
218 SubtitleAppearanceDialog::content_change (ChangeType type)
220 if (type == ChangeType::DONE) {
221 setup_sensitivity ();
226 SubtitleAppearanceDialog::set_to (wxWindow* w, int& r)
228 auto s = new wxBoxSizer (wxHORIZONTAL);
229 auto set_to = new CheckBox (this, _("Set to"));
230 s->Add (set_to, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 8);
231 s->Add (w, 0, wxALIGN_CENTER_VERTICAL);
232 _table->Add (s, wxGBPosition(r, 1));
238 SubtitleAppearanceDialog::apply ()
240 auto film = _film.lock ();
242 if (_force_colour->GetValue ()) {
243 auto const c = _colour->GetColour ();
244 _text->set_colour (dcp::Colour (c.Red(), c.Green(), c.Blue()));
246 _text->unset_colour ();
248 if (_force_effect->GetValue()) {
249 switch (_effect->GetSelection()) {
251 _text->set_effect (dcp::Effect::NONE);
254 _text->set_effect (dcp::Effect::BORDER);
257 _text->set_effect (dcp::Effect::SHADOW);
261 _text->unset_effect ();
263 if (_force_effect_colour->GetValue ()) {
264 auto const ec = _effect_colour->GetColour ();
265 _text->set_effect_colour (dcp::Colour (ec.Red(), ec.Green(), ec.Blue()));
267 _text->unset_effect_colour ();
269 if (_force_fade_in->GetValue ()) {
270 _text->set_fade_in (_fade_in->get(_content->active_video_frame_rate(film)));
272 _text->unset_fade_in ();
274 if (_force_fade_out->GetValue ()) {
275 _text->set_fade_out (_fade_out->get(_content->active_video_frame_rate(film)));
277 _text->unset_fade_out ();
279 _text->set_outline_width (_outline_width->GetValue ());
282 for (auto const& i: _pickers) {
283 _stream->set_colour (i.first, i.second->colour());
287 auto fc = dynamic_pointer_cast<FFmpegContent> (_content);
289 fc->signal_subtitle_stream_changed ();
294 SubtitleAppearanceDialog::restore ()
296 for (auto const& i: _pickers) {
297 i.second->set (i.first);
302 SubtitleAppearanceDialog::setup_sensitivity ()
304 _colour->Enable (_force_colour->GetValue ());
305 _effect_colour->Enable (_force_effect_colour->GetValue ());
306 _effect->Enable (_force_effect->GetValue ());
307 _fade_in->Enable (_force_fade_in->GetValue ());
308 _fade_out->Enable (_force_fade_out->GetValue ());
310 bool const can_outline_width = _effect->GetSelection() == OUTLINE && _text->burn ();
311 _outline_width->Enable (can_outline_width);
312 if (can_outline_width) {
313 _outline_width->UnsetToolTip ();
315 _outline_width->SetToolTip (_("Outline width cannot be set unless you are burning in subtitles."));
320 SubtitleAppearanceDialog::active_jobs_changed (optional<string> last)
322 if (last && *last == "examine_subtitles") {
323 _colours_panel->Show (true);
325 _finding->Show (false);
328 _overall_sizer->Layout ();
329 _overall_sizer->SetSizeHints (this);
334 SubtitleAppearanceDialog::add_colours ()
336 auto colours = _stream->colours ();
337 for (auto const& i: _stream->colours()) {
338 auto from = new wxPanel(_colours_panel, wxID_ANY);
339 from->SetBackgroundColour(wxColour(i.first.r, i.first.g, i.first.b, i.first.a));
340 _colour_table->Add (from, 1, wxEXPAND);
341 auto to = new RGBAColourPicker(_colours_panel, i.second);
342 _colour_table->Add (to, 1, wxEXPAND);
343 _pickers[i.first] = to;