2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/film_editor.cc
21 * @brief A wx widget to edit a film's metadata, and perform various functions.
27 #include <boost/thread.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include "lib/format.h"
32 #include "lib/transcode_job.h"
33 #include "lib/exceptions.h"
34 #include "lib/ab_transcode_job.h"
35 #include "lib/thumbs_job.h"
36 #include "lib/job_manager.h"
37 #include "lib/filter.h"
38 #include "lib/screen.h"
39 #include "lib/config.h"
40 #include "filter_dialog.h"
42 #include "film_editor.h"
43 #include "dcp_range_dialog.h"
46 using namespace boost;
48 /** @param f Film to edit */
49 FilmEditor::FilmEditor (Film* f, wxWindow* parent)
51 , _ignore_changes (Film::NONE)
54 _sizer = new wxFlexGridSizer (2, 4, 4);
57 add_label_to_sizer (_sizer, this, "Name");
58 _name = new wxTextCtrl (this, wxID_ANY);
59 _sizer->Add (_name, 1, wxEXPAND);
61 add_label_to_sizer (_sizer, this, "Content");
62 _content = new wxFilePickerCtrl (this, wxID_ANY, wxT (""), wxT ("Select Content File"), wxT("*.*"));
63 _sizer->Add (_content, 1, wxEXPAND);
65 add_label_to_sizer (_sizer, this, "Content Type");
66 _dcp_content_type = new wxComboBox (this, wxID_ANY);
67 _sizer->Add (_dcp_content_type);
69 add_label_to_sizer (_sizer, this, "Format");
70 _format = new wxComboBox (this, wxID_ANY);
71 _sizer->Add (_format);
74 add_label_to_sizer (_sizer, this, "Crop");
75 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
77 add_label_to_sizer (s, this, "L");
78 _left_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
79 s->Add (_left_crop, 0);
80 add_label_to_sizer (s, this, "R");
81 _right_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
82 s->Add (_right_crop, 0);
83 add_label_to_sizer (s, this, "T");
84 _top_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
85 s->Add (_top_crop, 0);
86 add_label_to_sizer (s, this, "B");
87 _bottom_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
88 s->Add (_bottom_crop, 0);
93 /* VIDEO-only stuff */
95 video_control (add_label_to_sizer (_sizer, this, "Filters"));
96 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
97 _filters = new wxStaticText (this, wxID_ANY, wxT ("None"));
98 video_control (_filters);
99 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
100 _filters_button = new wxButton (this, wxID_ANY, wxT ("Edit..."));
101 video_control (_filters_button);
102 s->Add (_filters_button, 0);
106 video_control (add_label_to_sizer (_sizer, this, "Scaler"));
107 _scaler = new wxComboBox (this, wxID_ANY);
108 _sizer->Add (video_control (_scaler), 1);
111 video_control (add_label_to_sizer (_sizer, this, "Audio Gain"));
112 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
113 _audio_gain = new wxSpinCtrl (this);
114 s->Add (video_control (_audio_gain), 1);
115 video_control (add_label_to_sizer (s, this, "dB"));
120 video_control (add_label_to_sizer (_sizer, this, "Audio Delay"));
121 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
122 _audio_delay = new wxSpinCtrl (this);
123 s->Add (video_control (_audio_delay), 1);
124 video_control (add_label_to_sizer (s, this, "ms"));
128 video_control (add_label_to_sizer (_sizer, this, "Frames Per Second"));
129 _frames_per_second = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxDefaultSize, 0, wxTextValidator (wxFILTER_NUMERIC));
130 _sizer->Add (video_control (_frames_per_second));
132 video_control (add_label_to_sizer (_sizer, this, "Original Size"));
133 _original_size = new wxStaticText (this, wxID_ANY, wxT (""));
134 _sizer->Add (video_control (_original_size), 1, wxALIGN_CENTER_VERTICAL);
136 video_control (add_label_to_sizer (_sizer, this, "Length"));
137 _length = new wxStaticText (this, wxID_ANY, wxT (""));
138 _sizer->Add (video_control (_length), 1, wxALIGN_CENTER_VERTICAL);
140 video_control (add_label_to_sizer (_sizer, this, "Audio"));
141 _audio = new wxStaticText (this, wxID_ANY, wxT (""));
142 _sizer->Add (video_control (_audio), 1, wxALIGN_CENTER_VERTICAL);
145 video_control (add_label_to_sizer (_sizer, this, "Range"));
146 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
147 _dcp_range = new wxStaticText (this, wxID_ANY, wxT (""));
148 s->Add (video_control (_dcp_range), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
149 _change_dcp_range_button = new wxButton (this, wxID_ANY, wxT ("Edit..."));
150 s->Add (video_control (_change_dcp_range_button), 0, 0, 6);
154 _dcp_ab = new wxCheckBox (this, wxID_ANY, wxT ("A/B"));
155 video_control (_dcp_ab);
156 _sizer->Add (_dcp_ab, 1);
157 _sizer->AddSpacer (0);
159 /* STILL-only stuff */
161 still_control (add_label_to_sizer (_sizer, this, "Duration"));
162 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
163 _still_duration = new wxSpinCtrl (this);
164 still_control (_still_duration);
165 s->Add (_still_duration, 1, wxEXPAND);
166 still_control (add_label_to_sizer (s, this, "s"));
170 /* Set up our editing widgets */
172 _left_crop->SetRange (0, 1024);
173 _top_crop->SetRange (0, 1024);
174 _right_crop->SetRange (0, 1024);
175 _bottom_crop->SetRange (0, 1024);
176 _audio_gain->SetRange (-60, 60);
177 _audio_delay->SetRange (-1000, 1000);
178 _still_duration->SetRange (0, 60 * 60);
180 vector<Format const *> fmt = Format::all ();
181 for (vector<Format const *>::iterator i = fmt.begin(); i != fmt.end(); ++i) {
182 _format->Append (std_to_wx ((*i)->name ()));
185 vector<DCPContentType const *> const ct = DCPContentType::all ();
186 for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
187 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
190 vector<Scaler const *> const sc = Scaler::all ();
191 for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
192 _scaler->Append (std_to_wx ((*i)->name()));
195 /* And set their values from the Film */
198 /* Now connect to them, since initial values are safely set */
199 _name->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (FilmEditor::name_changed), 0, this);
200 _frames_per_second->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (FilmEditor::frames_per_second_changed), 0, this);
201 _format->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::format_changed), 0, this);
202 _content->Connect (wxID_ANY, wxEVT_COMMAND_FILEPICKER_CHANGED, wxCommandEventHandler (FilmEditor::content_changed), 0, this);
203 _left_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
204 _right_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
205 _top_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
206 _bottom_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
207 _filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
208 _scaler->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
209 _dcp_content_type->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
210 _dcp_ab->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::dcp_ab_toggled), 0, this);
211 _audio_gain->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
212 _audio_delay->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
213 _still_duration->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::still_duration_changed), 0, this);
214 _change_dcp_range_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::change_dcp_range_clicked), 0, this);
219 /** Called when the left crop widget has been changed */
221 FilmEditor::left_crop_changed (wxCommandEvent &)
227 _ignore_changes = Film::LEFT_CROP;
228 _film->set_left_crop (_left_crop->GetValue ());
229 _ignore_changes = Film::NONE;
232 /** Called when the right crop widget has been changed */
234 FilmEditor::right_crop_changed (wxCommandEvent &)
240 _ignore_changes = Film::RIGHT_CROP;
241 _film->set_right_crop (_right_crop->GetValue ());
242 _ignore_changes = Film::NONE;
245 /** Called when the top crop widget has been changed */
247 FilmEditor::top_crop_changed (wxCommandEvent &)
253 _ignore_changes = Film::TOP_CROP;
254 _film->set_top_crop (_top_crop->GetValue ());
255 _ignore_changes = Film::NONE;
258 /** Called when the bottom crop value has been changed */
260 FilmEditor::bottom_crop_changed (wxCommandEvent &)
266 _ignore_changes = Film::BOTTOM_CROP;
267 _film->set_bottom_crop (_bottom_crop->GetValue ());
268 _ignore_changes = Film::NONE;
271 /** Called when the content filename has been changed */
273 FilmEditor::content_changed (wxCommandEvent &)
279 _ignore_changes = Film::CONTENT;
282 _film->set_content (wx_to_std (_content->GetPath ()));
283 } catch (std::exception& e) {
284 _content->SetPath (std_to_wx (_film->directory ()));
286 m << "Could not set content: " << e.what() << ".";
287 error_dialog (this, m.str ());
290 _ignore_changes = Film::NONE;
295 /** Called when the DCP A/B switch has been toggled */
297 FilmEditor::dcp_ab_toggled (wxCommandEvent &)
303 _ignore_changes = Film::DCP_AB;
304 _film->set_dcp_ab (_dcp_ab->GetValue ());
305 _ignore_changes = Film::NONE;
308 /** Called when the name widget has been changed */
310 FilmEditor::name_changed (wxCommandEvent &)
316 _ignore_changes = Film::NAME;
317 _film->set_name (string (_name->GetValue().mb_str()));
318 _ignore_changes = Film::NONE;
321 /** Called when the metadata stored in the Film object has changed;
322 * so that we can update the GUI.
323 * @param p Property of the Film that has changed.
326 FilmEditor::film_changed (Film::Property p)
328 if (!_film || _ignore_changes == p) {
338 _content->SetPath (std_to_wx (_film->content ()));
342 _format->SetSelection (Format::as_index (_film->format ()));
344 case Film::LEFT_CROP:
345 _left_crop->SetValue (_film->left_crop ());
347 case Film::RIGHT_CROP:
348 _right_crop->SetValue (_film->right_crop ());
351 _top_crop->SetValue (_film->top_crop ());
353 case Film::BOTTOM_CROP:
354 _bottom_crop->SetValue (_film->bottom_crop ());
358 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
359 if (p.first.empty () && p.second.empty ()) {
360 _filters->SetLabel (_("None"));
362 string const b = p.first + " " + p.second;
363 _filters->SetLabel (std_to_wx (b));
369 _name->ChangeValue (std_to_wx (_film->name ()));
371 case Film::FRAMES_PER_SECOND:
374 s << fixed << setprecision(2) << _film->frames_per_second();
375 _frames_per_second->ChangeValue (std_to_wx (s.str ()));
378 case Film::AUDIO_CHANNELS:
379 case Film::AUDIO_SAMPLE_RATE:
380 if (_film->audio_channels() == 0 && _film->audio_sample_rate() == 0) {
381 _audio->SetLabel (wxT (""));
383 s << _film->audio_channels () << " channels, " << _film->audio_sample_rate() << "Hz";
384 _audio->SetLabel (std_to_wx (s.str ()));
388 if (_film->size().width == 0 && _film->size().height == 0) {
389 _original_size->SetLabel (wxT (""));
391 s << _film->size().width << " x " << _film->size().height;
392 _original_size->SetLabel (std_to_wx (s.str ()));
396 if (_film->frames_per_second() > 0 && _film->length() > 0) {
397 s << _film->length() << " frames; " << seconds_to_hms (_film->length() / _film->frames_per_second());
398 } else if (_film->length() > 0) {
399 s << _film->length() << " frames";
401 _length->SetLabel (std_to_wx (s.str ()));
403 case Film::DCP_CONTENT_TYPE:
404 _dcp_content_type->SetSelection (DCPContentType::as_index (_film->dcp_content_type ()));
408 case Film::DCP_FRAMES:
409 if (_film->dcp_frames() == 0) {
410 _dcp_range->SetLabel (wxT ("Whole film"));
413 s << "First " << _film->dcp_frames() << " frames";
414 _dcp_range->SetLabel (std_to_wx (s.str ()));
418 case Film::DCP_TRIM_ACTION:
421 _dcp_ab->SetValue (_film->dcp_ab ());
424 _scaler->SetSelection (Scaler::as_index (_film->scaler ()));
426 case Film::AUDIO_GAIN:
427 _audio_gain->SetValue (_film->audio_gain ());
429 case Film::AUDIO_DELAY:
430 _audio_delay->SetValue (_film->audio_delay ());
432 case Film::STILL_DURATION:
433 _still_duration->SetValue (_film->still_duration ());
438 /** Called when the format widget has been changed */
440 FilmEditor::format_changed (wxCommandEvent &)
446 _ignore_changes = Film::FORMAT;
447 int const n = _format->GetSelection ();
449 _film->set_format (Format::from_index (n));
451 _ignore_changes = Film::NONE;
454 /** Called when the DCP content type widget has been changed */
456 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
462 _ignore_changes = Film::DCP_CONTENT_TYPE;
463 int const n = _dcp_content_type->GetSelection ();
465 _film->set_dcp_content_type (DCPContentType::from_index (n));
467 _ignore_changes = Film::NONE;
470 /** Sets the Film that we are editing */
472 FilmEditor::set_film (Film* f)
476 set_things_sensitive (_film != 0);
479 _film->Changed.connect (sigc::mem_fun (*this, &FilmEditor::film_changed));
483 FileChanged (_film->directory ());
488 film_changed (Film::NAME);
489 film_changed (Film::CONTENT);
490 film_changed (Film::DCP_CONTENT_TYPE);
491 film_changed (Film::FORMAT);
492 film_changed (Film::LEFT_CROP);
493 film_changed (Film::RIGHT_CROP);
494 film_changed (Film::TOP_CROP);
495 film_changed (Film::BOTTOM_CROP);
496 film_changed (Film::FILTERS);
497 film_changed (Film::DCP_FRAMES);
498 film_changed (Film::DCP_TRIM_ACTION);
499 film_changed (Film::DCP_AB);
500 film_changed (Film::SIZE);
501 film_changed (Film::LENGTH);
502 film_changed (Film::FRAMES_PER_SECOND);
503 film_changed (Film::AUDIO_CHANNELS);
504 film_changed (Film::AUDIO_SAMPLE_RATE);
505 film_changed (Film::SCALER);
506 film_changed (Film::AUDIO_GAIN);
507 film_changed (Film::AUDIO_DELAY);
508 film_changed (Film::STILL_DURATION);
511 /** Updates the sensitivity of lots of widgets to a given value.
512 * @param s true to make sensitive, false to make insensitive.
515 FilmEditor::set_things_sensitive (bool s)
518 _frames_per_second->Enable (s);
520 _content->Enable (s);
521 _left_crop->Enable (s);
522 _right_crop->Enable (s);
523 _top_crop->Enable (s);
524 _bottom_crop->Enable (s);
525 _filters_button->Enable (s);
527 _dcp_content_type->Enable (s);
528 _dcp_range->Enable (s);
529 _change_dcp_range_button->Enable (s);
531 _audio_gain->Enable (s);
532 _audio_delay->Enable (s);
533 _still_duration->Enable (s);
536 /** Called when the `Edit filters' button has been clicked */
538 FilmEditor::edit_filters_clicked (wxCommandEvent &)
540 FilterDialog* d = new FilterDialog (this, _film->filters ());
541 d->ActiveChanged.connect (sigc::mem_fun (*_film, &Film::set_filters));
546 /** Called when the scaler widget has been changed */
548 FilmEditor::scaler_changed (wxCommandEvent &)
554 _ignore_changes = Film::SCALER;
555 int const n = _scaler->GetSelection ();
557 _film->set_scaler (Scaler::from_index (n));
559 _ignore_changes = Film::NONE;
562 /** Called when the frames per second widget has been changed */
564 FilmEditor::frames_per_second_changed (wxCommandEvent &)
570 _ignore_changes = Film::FRAMES_PER_SECOND;
571 _film->set_frames_per_second (boost::lexical_cast<float> (wx_to_std (_frames_per_second->GetValue ())));
572 _ignore_changes = Film::NONE;
576 FilmEditor::audio_gain_changed (wxCommandEvent &)
582 _ignore_changes = Film::AUDIO_GAIN;
583 _film->set_audio_gain (_audio_gain->GetValue ());
584 _ignore_changes = Film::NONE;
588 FilmEditor::audio_delay_changed (wxCommandEvent &)
594 _ignore_changes = Film::AUDIO_DELAY;
595 _film->set_audio_delay (_audio_delay->GetValue ());
596 _ignore_changes = Film::NONE;
600 FilmEditor::video_control (wxControl* c)
602 _video_controls.push_back (c);
607 FilmEditor::still_control (wxControl* c)
609 _still_controls.push_back (c);
614 FilmEditor::setup_visibility ()
616 ContentType c = VIDEO;
619 c = _film->content_type ();
622 for (list<wxControl*>::iterator i = _video_controls.begin(); i != _video_controls.end(); ++i) {
623 (*i)->Show (c == VIDEO);
626 for (list<wxControl*>::iterator i = _still_controls.begin(); i != _still_controls.end(); ++i) {
627 (*i)->Show (c == STILL);
634 FilmEditor::still_duration_changed (wxCommandEvent &)
640 _ignore_changes = Film::STILL_DURATION;
641 _film->set_still_duration (_still_duration->GetValue ());
642 _ignore_changes = Film::NONE;
646 FilmEditor::change_dcp_range_clicked (wxCommandEvent &)
648 DCPRangeDialog* d = new DCPRangeDialog (this, _film);
649 d->Changed.connect (sigc::mem_fun (*this, &FilmEditor::dcp_range_changed));
654 FilmEditor::dcp_range_changed (int frames, TrimAction action)
656 _film->set_dcp_frames (frames);
657 _film->set_dcp_trim_action (action);