2 Copyright (C) 2012-2016 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/>.
21 #include "content_panel.h"
23 #include "video_panel.h"
24 #include "audio_panel.h"
25 #include "subtitle_panel.h"
26 #include "timing_panel.h"
27 #include "timeline_dialog.h"
28 #include "image_sequence_dialog.h"
29 #include "film_viewer.h"
30 #include "lib/audio_content.h"
31 #include "lib/subtitle_content.h"
32 #include "lib/video_content.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/content_factory.h"
35 #include "lib/image_content.h"
36 #include "lib/dcp_content.h"
37 #include "lib/case_insensitive_sorter.h"
38 #include "lib/playlist.h"
39 #include "lib/config.h"
41 #include "lib/compose.hpp"
43 #include <wx/notebook.h>
44 #include <wx/listctrl.h>
45 #include <boost/filesystem.hpp>
46 #include <boost/foreach.hpp>
54 using boost::shared_ptr;
55 using boost::weak_ptr;
56 using boost::dynamic_pointer_cast;
57 using boost::optional;
59 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
61 ContentPanel::ContentPanel (wxNotebook* n, boost::shared_ptr<Film> film, FilmViewer* viewer)
62 : _timeline_dialog (0)
65 , _film_viewer (viewer)
66 , _generally_sensitive (true)
68 _panel = new wxPanel (n);
69 _sizer = new wxBoxSizer (wxVERTICAL);
70 _panel->SetSizer (_sizer);
72 _menu = new ContentMenu (_panel);
75 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
77 _content = new wxListCtrl (_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
78 _content->DragAcceptFiles (true);
79 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
81 _content->InsertColumn (0, wxT(""));
82 _content->SetColumnWidth (0, 512);
84 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
86 _add_file = new wxButton (_panel, wxID_ANY, _("Add file(s)..."));
87 _add_file->SetToolTip (_("Add video, image, sound or subtitle files to the film."));
88 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
90 _add_folder = new wxButton (_panel, wxID_ANY, _("Add folder..."));
91 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a folder of sound files."));
92 b->Add (_add_folder, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
94 _add_dcp = new wxButton (_panel, wxID_ANY, _("Add DCP..."));
95 _add_dcp->SetToolTip (_("Add a DCP."));
96 b->Add (_add_dcp, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
98 _remove = new wxButton (_panel, wxID_ANY, _("Remove"));
99 _remove->SetToolTip (_("Remove the selected piece of content from the film."));
100 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
102 _earlier = new wxButton (_panel, wxID_ANY, _("Earlier"));
103 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
104 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
106 _later = new wxButton (_panel, wxID_ANY, _("Later"));
107 _later->SetToolTip (_("Move the selected piece of content later in the film."));
108 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
110 _timeline = new wxButton (_panel, wxID_ANY, _("Timeline..."));
111 _timeline->SetToolTip (_("Open the timeline for the film."));
112 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
114 s->Add (b, 0, wxALL, 4);
116 _sizer->Add (s, 0, wxEXPAND | wxALL, 6);
119 _notebook = new wxNotebook (_panel, wxID_ANY);
120 _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
122 _video_panel = new VideoPanel (this);
123 _panels.push_back (_video_panel);
124 _audio_panel = new AudioPanel (this);
125 _panels.push_back (_audio_panel);
126 _subtitle_panel = new SubtitlePanel (this);
127 _panels.push_back (_subtitle_panel);
128 _timing_panel = new TimingPanel (this, _film_viewer);
129 _panels.push_back (_timing_panel);
131 _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::selection_changed, this));
132 _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::selection_changed, this));
133 _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
134 _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
135 _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
136 _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
137 _add_dcp->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_dcp_clicked, this));
138 _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
139 _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
140 _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
141 _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
145 ContentPanel::selected ()
150 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
155 if (s < int (_film->content().size ())) {
156 sel.push_back (_film->content()[s]);
164 ContentPanel::selected_video ()
168 BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
178 ContentPanel::selected_audio ()
182 BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
192 ContentPanel::selected_subtitle ()
196 BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
206 ContentPanel::selected_ffmpeg ()
208 FFmpegContentList sc;
210 BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
211 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (i);
221 ContentPanel::film_changed (Film::Property p)
225 case Film::CONTENT_ORDER:
232 BOOST_FOREACH (ContentSubPanel* i, _panels) {
238 ContentPanel::selection_changed ()
240 if (_last_selected == selected()) {
241 /* This was triggered by a re-build of the view but the selection
242 did not really change.
247 _last_selected = selected ();
249 setup_sensitivity ();
251 BOOST_FOREACH (ContentSubPanel* i, _panels) {
252 i->content_selection_changed ();
255 optional<DCPTime> go_to;
256 BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
257 if (!go_to || i->position() < go_to.get()) {
258 go_to = i->position ();
262 if (go_to && Config::instance()->jump_to_selected ()) {
263 _film_viewer->set_position (go_to.get ());
266 if (_timeline_dialog) {
267 _timeline_dialog->set_selection (selected ());
274 ContentPanel::add_file_clicked ()
276 /* This method is also called when Ctrl-A is pressed, so check that our notebook page
279 if (_parent->GetCurrentPage() != _panel || !_film) {
283 /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
284 non-Latin filenames or paths.
286 wxFileDialog* d = new wxFileDialog (
288 _("Choose a file or files"),
291 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
292 wxFD_MULTIPLE | wxFD_CHANGE_DIR
295 int const r = d->ShowModal ();
304 list<boost::filesystem::path> path_list;
305 for (unsigned int i = 0; i < paths.GetCount(); ++i) {
306 path_list.push_back (wx_to_std (paths[i]));
308 add_files (path_list);
314 ContentPanel::add_folder_clicked ()
316 wxDirDialog* d = new wxDirDialog (_panel, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
317 int r = d->ShowModal ();
318 boost::filesystem::path const path (wx_to_std (d->GetPath ()));
325 list<shared_ptr<Content> > content;
328 content = content_factory (_film, path);
329 } catch (exception& e) {
330 error_dialog (_parent, e.what());
334 if (content.empty ()) {
335 error_dialog (_parent, _("No content found in this folder."));
339 BOOST_FOREACH (shared_ptr<Content> i, content) {
340 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
342 ImageSequenceDialog* e = new ImageSequenceDialog (_panel);
344 float const frame_rate = e->frame_rate ();
351 ic->set_video_frame_rate (frame_rate);
354 _film->examine_and_add_content (i);
359 ContentPanel::add_dcp_clicked ()
361 wxDirDialog* d = new wxDirDialog (_panel, _("Choose a DCP folder"), wxT (""), wxDD_DIR_MUST_EXIST);
362 int r = d->ShowModal ();
363 boost::filesystem::path const path (wx_to_std (d->GetPath ()));
371 _film->examine_and_add_content (shared_ptr<Content> (new DCPContent (_film, path)));
372 } catch (exception& e) {
373 error_dialog (_parent, e.what());
377 /** @return true if this remove "click" should be ignored */
379 ContentPanel::remove_clicked (bool hotkey)
381 /* If the method was called because Delete was pressed check that our notebook page
382 is visible and that the content list is focussed.
384 if (hotkey && (_parent->GetCurrentPage() != _panel || !_content->HasFocus())) {
388 BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
389 _film->remove_content (i);
392 selection_changed ();
397 ContentPanel::timeline_clicked ()
399 if (_timeline_dialog) {
400 _timeline_dialog->Destroy ();
401 _timeline_dialog = 0;
404 _timeline_dialog = new TimelineDialog (this, _film);
405 _timeline_dialog->Show ();
409 ContentPanel::right_click (wxListEvent& ev)
411 _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
414 /** Set up broad sensitivity based on the type of content that is selected */
416 ContentPanel::setup_sensitivity ()
418 _add_file->Enable (_generally_sensitive);
419 _add_folder->Enable (_generally_sensitive);
420 _add_dcp->Enable (_generally_sensitive);
422 ContentList selection = selected ();
423 ContentList video_selection = selected_video ();
424 ContentList audio_selection = selected_audio ();
426 _remove->Enable (_generally_sensitive && !selection.empty());
427 _earlier->Enable (_generally_sensitive && selection.size() == 1);
428 _later->Enable (_generally_sensitive && selection.size() == 1);
429 _timeline->Enable (_generally_sensitive && _film && !_film->content().empty());
431 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
432 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
433 _subtitle_panel->Enable (_generally_sensitive && selection.size() == 1 && selection.front()->subtitle);
434 _timing_panel->Enable (_generally_sensitive);
438 ContentPanel::set_film (shared_ptr<Film> film)
440 _audio_panel->set_film (film);
444 film_changed (Film::CONTENT);
445 film_changed (Film::AUDIO_CHANNELS);
446 selection_changed ();
447 setup_sensitivity ();
451 ContentPanel::set_general_sensitivity (bool s)
453 _generally_sensitive = s;
454 setup_sensitivity ();
458 ContentPanel::earlier_clicked ()
460 ContentList sel = selected ();
461 if (sel.size() == 1) {
462 _film->move_content_earlier (sel.front ());
463 selection_changed ();
468 ContentPanel::later_clicked ()
470 ContentList sel = selected ();
471 if (sel.size() == 1) {
472 _film->move_content_later (sel.front ());
473 selection_changed ();
478 ContentPanel::set_selection (weak_ptr<Content> wc)
480 ContentList content = _film->content ();
481 for (size_t i = 0; i < content.size(); ++i) {
482 if (content[i] == wc.lock ()) {
483 _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
485 _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
491 ContentPanel::set_selection (ContentList cl)
493 ContentList content = _film->content ();
494 for (size_t i = 0; i < content.size(); ++i) {
495 if (find(cl.begin(), cl.end(), content[i]) != cl.end()) {
496 _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
498 _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
504 ContentPanel::film_content_changed (int property)
507 property == ContentProperty::PATH ||
508 property == DCPContentProperty::NEEDS_ASSETS ||
509 property == DCPContentProperty::NEEDS_KDM ||
510 property == DCPContentProperty::NAME
516 BOOST_FOREACH (ContentSubPanel* i, _panels) {
517 i->film_content_changed (property);
522 ContentPanel::setup ()
524 ContentList content = _film->content ();
526 Content* selected_content = 0;
527 int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
531 item.SetMask (wxLIST_MASK_DATA);
532 _content->GetItem (item);
533 selected_content = reinterpret_cast<Content*> (item.GetData ());
536 _content->DeleteAllItems ();
538 BOOST_FOREACH (shared_ptr<Content> i, content) {
539 int const t = _content->GetItemCount ();
540 bool const valid = i->paths_valid ();
542 /* Temporary debugging for Igor */
543 BOOST_FOREACH (boost::filesystem::path j, i->paths()) {
544 LOG_GENERAL ("Check %1 %2 answer %3", j.string(), boost::filesystem::exists(j) ? "yes" : "no", valid ? "yes" : "no");
547 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i);
548 bool const needs_kdm = dcp && dcp->needs_kdm ();
549 bool const needs_assets = dcp && dcp->needs_assets ();
551 wxString s = std_to_wx (i->summary ());
554 s = _("MISSING: ") + s;
558 s = _("NEEDS KDM: ") + s;
562 s = _("NEEDS OV: ") + s;
568 item.SetData (i.get ());
569 _content->InsertItem (item);
571 if (i.get() == selected_content) {
572 _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
575 if (!valid || needs_kdm || needs_assets) {
576 _content->SetItemTextColour (t, *wxRED);
580 if (!selected_content && !content.empty ()) {
581 /* Select the item of content if none was selected before */
582 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
585 setup_sensitivity ();
589 ContentPanel::files_dropped (wxDropFilesEvent& event)
595 wxString* paths = event.GetFiles ();
596 list<boost::filesystem::path> path_list;
597 for (int i = 0; i < event.GetNumberOfFiles(); i++) {
598 path_list.push_back (wx_to_std (paths[i]));
601 add_files (path_list);
605 ContentPanel::add_files (list<boost::filesystem::path> paths)
607 /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
608 I can't reproduce that, but sort them anyway. Don't use ImageFilenameSorter as a normal
609 alphabetical sort is expected here.
612 paths.sort (CaseInsensitiveSorter ());
614 /* XXX: check for lots of files here and do something */
617 BOOST_FOREACH (boost::filesystem::path i, paths) {
618 BOOST_FOREACH (shared_ptr<Content> j, content_factory (_film, i)) {
619 _film->examine_and_add_content (j);
622 } catch (exception& e) {
623 error_dialog (_parent, e.what());