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 (false)
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 = true;
228 _film->set_left_crop (_left_crop->GetValue ());
229 _ignore_changes = false;
232 /** Called when the right crop widget has been changed */
234 FilmEditor::right_crop_changed (wxCommandEvent &)
240 _ignore_changes = true;
241 _film->set_right_crop (_right_crop->GetValue ());
242 _ignore_changes = false;
245 /** Called when the top crop widget has been changed */
247 FilmEditor::top_crop_changed (wxCommandEvent &)
253 _ignore_changes = true;
254 _film->set_top_crop (_top_crop->GetValue ());
255 _ignore_changes = false;
258 /** Called when the bottom crop value has been changed */
260 FilmEditor::bottom_crop_changed (wxCommandEvent &)
266 _ignore_changes = true;
267 _film->set_bottom_crop (_bottom_crop->GetValue ());
268 _ignore_changes = false;
271 /** Called when the content filename has been changed */
273 FilmEditor::content_changed (wxCommandEvent &)
279 _ignore_changes = true;
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 = false;
295 /** Called when the DCP A/B switch has been toggled */
297 FilmEditor::dcp_ab_toggled (wxCommandEvent &)
303 _ignore_changes = true;
304 _film->set_dcp_ab (_dcp_ab->GetValue ());
305 _ignore_changes = false;
308 /** Called when the name widget has been changed */
310 FilmEditor::name_changed (wxCommandEvent &)
316 _ignore_changes = true;
317 _film->set_name (string (_name->GetValue().mb_str()));
318 _ignore_changes = false;
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) {
336 _content->SetPath (std_to_wx (_film->content ()));
340 _format->SetSelection (Format::as_index (_film->format ()));
342 case Film::LEFT_CROP:
343 _left_crop->SetValue (_film->left_crop ());
345 case Film::RIGHT_CROP:
346 _right_crop->SetValue (_film->right_crop ());
349 _top_crop->SetValue (_film->top_crop ());
351 case Film::BOTTOM_CROP:
352 _bottom_crop->SetValue (_film->bottom_crop ());
356 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
357 if (p.first.empty () && p.second.empty ()) {
358 _filters->SetLabel (_("None"));
360 string const b = p.first + " " + p.second;
361 _filters->SetLabel (std_to_wx (b));
367 _name->ChangeValue (std_to_wx (_film->name ()));
369 case Film::FRAMES_PER_SECOND:
372 s << fixed << setprecision(2) << _film->frames_per_second();
373 _frames_per_second->ChangeValue (std_to_wx (s.str ()));
376 case Film::AUDIO_CHANNELS:
377 case Film::AUDIO_SAMPLE_RATE:
378 if (_film->audio_channels() == 0 && _film->audio_sample_rate() == 0) {
379 _audio->SetLabel (wxT (""));
381 s << _film->audio_channels () << " channels, " << _film->audio_sample_rate() << "Hz";
382 _audio->SetLabel (std_to_wx (s.str ()));
386 if (_film->size().width == 0 && _film->size().height == 0) {
387 _original_size->SetLabel (wxT (""));
389 s << _film->size().width << " x " << _film->size().height;
390 _original_size->SetLabel (std_to_wx (s.str ()));
394 if (_film->frames_per_second() > 0 && _film->length() > 0) {
395 s << _film->length() << " frames; " << seconds_to_hms (_film->length() / _film->frames_per_second());
396 } else if (_film->length() > 0) {
397 s << _film->length() << " frames";
399 _length->SetLabel (std_to_wx (s.str ()));
401 case Film::DCP_CONTENT_TYPE:
402 _dcp_content_type->SetSelection (DCPContentType::as_index (_film->dcp_content_type ()));
406 case Film::DCP_FRAMES:
407 if (_film->dcp_frames() == 0) {
408 _dcp_range->SetLabel (wxT ("Whole film"));
411 s << "First " << _film->dcp_frames() << " frames";
412 _dcp_range->SetLabel (std_to_wx (s.str ()));
416 case Film::DCP_TRIM_ACTION:
419 _dcp_ab->SetValue (_film->dcp_ab ());
422 _scaler->SetSelection (Scaler::as_index (_film->scaler ()));
424 case Film::AUDIO_GAIN:
425 _audio_gain->SetValue (_film->audio_gain ());
427 case Film::AUDIO_DELAY:
428 _audio_delay->SetValue (_film->audio_delay ());
430 case Film::STILL_DURATION:
431 _still_duration->SetValue (_film->still_duration ());
436 /** Called when the format widget has been changed */
438 FilmEditor::format_changed (wxCommandEvent &)
444 _ignore_changes = true;
445 int const n = _format->GetSelection ();
447 _film->set_format (Format::from_index (n));
449 _ignore_changes = false;
452 /** Called when the DCP content type widget has been changed */
454 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
460 _ignore_changes = true;
461 int const n = _dcp_content_type->GetSelection ();
463 _film->set_dcp_content_type (DCPContentType::from_index (n));
465 _ignore_changes = false;
468 /** Sets the Film that we are editing */
470 FilmEditor::set_film (Film* f)
474 set_things_sensitive (_film != 0);
477 _film->Changed.connect (sigc::mem_fun (*this, &FilmEditor::film_changed));
481 FileChanged (_film->directory ());
486 film_changed (Film::NAME);
487 film_changed (Film::CONTENT);
488 film_changed (Film::DCP_CONTENT_TYPE);
489 film_changed (Film::FORMAT);
490 film_changed (Film::LEFT_CROP);
491 film_changed (Film::RIGHT_CROP);
492 film_changed (Film::TOP_CROP);
493 film_changed (Film::BOTTOM_CROP);
494 film_changed (Film::FILTERS);
495 film_changed (Film::DCP_FRAMES);
496 film_changed (Film::DCP_TRIM_ACTION);
497 film_changed (Film::DCP_AB);
498 film_changed (Film::SIZE);
499 film_changed (Film::LENGTH);
500 film_changed (Film::FRAMES_PER_SECOND);
501 film_changed (Film::AUDIO_CHANNELS);
502 film_changed (Film::AUDIO_SAMPLE_RATE);
503 film_changed (Film::SCALER);
504 film_changed (Film::AUDIO_GAIN);
505 film_changed (Film::AUDIO_DELAY);
506 film_changed (Film::STILL_DURATION);
509 /** Updates the sensitivity of lots of widgets to a given value.
510 * @param s true to make sensitive, false to make insensitive.
513 FilmEditor::set_things_sensitive (bool s)
516 _frames_per_second->Enable (s);
518 _content->Enable (s);
519 _left_crop->Enable (s);
520 _right_crop->Enable (s);
521 _top_crop->Enable (s);
522 _bottom_crop->Enable (s);
523 _filters_button->Enable (s);
525 _dcp_content_type->Enable (s);
526 _dcp_range->Enable (s);
527 _change_dcp_range_button->Enable (s);
529 _audio_gain->Enable (s);
530 _audio_delay->Enable (s);
531 _still_duration->Enable (s);
534 /** Called when the `Edit filters' button has been clicked */
536 FilmEditor::edit_filters_clicked (wxCommandEvent &)
538 FilterDialog* d = new FilterDialog (this, _film->filters ());
539 d->ActiveChanged.connect (sigc::mem_fun (*_film, &Film::set_filters));
544 /** Called when the scaler widget has been changed */
546 FilmEditor::scaler_changed (wxCommandEvent &)
552 _ignore_changes = true;
553 int const n = _scaler->GetSelection ();
555 _film->set_scaler (Scaler::from_index (n));
557 _ignore_changes = false;
560 /** Called when the frames per second widget has been changed */
562 FilmEditor::frames_per_second_changed (wxCommandEvent &)
568 _ignore_changes = true;
569 _film->set_frames_per_second (boost::lexical_cast<float> (wx_to_std (_frames_per_second->GetValue ())));
570 _ignore_changes = false;
574 FilmEditor::audio_gain_changed (wxCommandEvent &)
580 _ignore_changes = true;
581 _film->set_audio_gain (_audio_gain->GetValue ());
582 _ignore_changes = false;
586 FilmEditor::audio_delay_changed (wxCommandEvent &)
592 _ignore_changes = true;
593 _film->set_audio_delay (_audio_delay->GetValue ());
594 _ignore_changes = false;
598 FilmEditor::video_control (wxControl* c)
600 _video_controls.push_back (c);
605 FilmEditor::still_control (wxControl* c)
607 _still_controls.push_back (c);
612 FilmEditor::setup_visibility ()
614 ContentType c = VIDEO;
617 c = _film->content_type ();
620 for (list<wxControl*>::iterator i = _video_controls.begin(); i != _video_controls.end(); ++i) {
621 (*i)->Show (c == VIDEO);
624 for (list<wxControl*>::iterator i = _still_controls.begin(); i != _still_controls.end(); ++i) {
625 (*i)->Show (c == STILL);
632 FilmEditor::still_duration_changed (wxCommandEvent &)
638 _ignore_changes = true;
639 _film->set_still_duration (_still_duration->GetValue ());
640 _ignore_changes = false;
644 FilmEditor::change_dcp_range_clicked (wxCommandEvent &)
646 DCPRangeDialog* d = new DCPRangeDialog (this, _film);
647 d->Changed.connect (sigc::mem_fun (*this, &FilmEditor::dcp_range_changed));
652 FilmEditor::dcp_range_changed (int frames, TrimAction action)
654 _film->set_dcp_frames (frames);
655 _film->set_dcp_trim_action (action);