Allow content factory to return multiple content.
[dcpomatic.git] / src / wx / content_panel.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "content_panel.h"
22 #include "wx_util.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 <wx/wx.h>
40 #include <wx/notebook.h>
41 #include <wx/listctrl.h>
42 #include <boost/filesystem.hpp>
43 #include <boost/foreach.hpp>
44 #include <iostream>
45
46 using std::list;
47 using std::string;
48 using std::cout;
49 using std::vector;
50 using std::exception;
51 using boost::shared_ptr;
52 using boost::weak_ptr;
53 using boost::dynamic_pointer_cast;
54 using boost::optional;
55
56 ContentPanel::ContentPanel (wxNotebook* n, boost::shared_ptr<Film> film, FilmViewer* viewer)
57         : _timeline_dialog (0)
58         , _parent (n)
59         , _film (film)
60         , _film_viewer (viewer)
61         , _generally_sensitive (true)
62 {
63         _panel = new wxPanel (n);
64         _sizer = new wxBoxSizer (wxVERTICAL);
65         _panel->SetSizer (_sizer);
66
67         _menu = new ContentMenu (_panel);
68
69         {
70                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
71
72                 _content = new wxListCtrl (_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
73                 _content->DragAcceptFiles (true);
74                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
75
76                 _content->InsertColumn (0, wxT(""));
77                 _content->SetColumnWidth (0, 512);
78
79                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
80
81                 _add_file = new wxButton (_panel, wxID_ANY, _("Add file(s)..."));
82                 _add_file->SetToolTip (_("Add video, image or sound files to the film."));
83                 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
84
85                 _add_folder = new wxButton (_panel, wxID_ANY, _("Add folder..."));
86                 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a DCP."));
87                 b->Add (_add_folder, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
88
89                 _remove = new wxButton (_panel, wxID_ANY, _("Remove"));
90                 _remove->SetToolTip (_("Remove the selected piece of content from the film."));
91                 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
92
93                 _earlier = new wxButton (_panel, wxID_ANY, _("Earlier"));
94                 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
95                 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
96
97                 _later = new wxButton (_panel, wxID_ANY, _("Later"));
98                 _later->SetToolTip (_("Move the selected piece of content later in the film."));
99                 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
100
101                 _timeline = new wxButton (_panel, wxID_ANY, _("Timeline..."));
102                 _timeline->SetToolTip (_("Open the timeline for the film."));
103                 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
104
105                 s->Add (b, 0, wxALL, 4);
106
107                 _sizer->Add (s, 0, wxEXPAND | wxALL, 6);
108         }
109
110         _notebook = new wxNotebook (_panel, wxID_ANY);
111         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
112
113         _video_panel = new VideoPanel (this);
114         _panels.push_back (_video_panel);
115         _audio_panel = new AudioPanel (this);
116         _panels.push_back (_audio_panel);
117         _subtitle_panel = new SubtitlePanel (this);
118         _panels.push_back (_subtitle_panel);
119         _timing_panel = new TimingPanel (this, _film_viewer);
120         _panels.push_back (_timing_panel);
121
122         _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::selection_changed, this));
123         _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::selection_changed, this));
124         _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
125         _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
126         _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
127         _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
128         _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
129         _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
130         _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
131         _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
132 }
133
134 ContentList
135 ContentPanel::selected ()
136 {
137         ContentList sel;
138         long int s = -1;
139         while (true) {
140                 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
141                 if (s == -1) {
142                         break;
143                 }
144
145                 if (s < int (_film->content().size ())) {
146                         sel.push_back (_film->content()[s]);
147                 }
148         }
149
150         return sel;
151 }
152
153 ContentList
154 ContentPanel::selected_video ()
155 {
156         ContentList vc;
157
158         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
159                 if (i->video) {
160                         vc.push_back (i);
161                 }
162         }
163
164         return vc;
165 }
166
167 ContentList
168 ContentPanel::selected_audio ()
169 {
170         ContentList ac;
171
172         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
173                 if (i->audio) {
174                         ac.push_back (i);
175                 }
176         }
177
178         return ac;
179 }
180
181 ContentList
182 ContentPanel::selected_subtitle ()
183 {
184         ContentList sc;
185
186         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
187                 if (i->subtitle) {
188                         sc.push_back (i);
189                 }
190         }
191
192         return sc;
193 }
194
195 FFmpegContentList
196 ContentPanel::selected_ffmpeg ()
197 {
198         FFmpegContentList sc;
199
200         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
201                 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (i);
202                 if (t) {
203                         sc.push_back (t);
204                 }
205         }
206
207         return sc;
208 }
209
210 void
211 ContentPanel::film_changed (Film::Property p)
212 {
213         switch (p) {
214         case Film::CONTENT:
215         case Film::CONTENT_ORDER:
216                 setup ();
217                 break;
218         default:
219                 break;
220         }
221
222         BOOST_FOREACH (ContentSubPanel* i, _panels) {
223                 i->film_changed (p);
224         }
225 }
226
227 void
228 ContentPanel::selection_changed ()
229 {
230         if (_last_selected == selected()) {
231                 /* This was triggered by a re-build of the view but the selection
232                    did not really change.
233                 */
234                 return;
235         }
236
237         _last_selected = selected ();
238
239         setup_sensitivity ();
240
241         BOOST_FOREACH (ContentSubPanel* i, _panels) {
242                 i->content_selection_changed ();
243         }
244
245         optional<DCPTime> go_to;
246         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
247                 if (!go_to || i->position() < go_to.get()) {
248                         go_to = i->position ();
249                 }
250         }
251
252         if (go_to) {
253                 _film_viewer->set_position (go_to.get ());
254         }
255
256         if (_timeline_dialog) {
257                 _timeline_dialog->set_selection (selected ());
258         }
259 }
260
261 void
262 ContentPanel::add_file_clicked ()
263 {
264         /* This method is also called when Ctrl-A is pressed, so check that our notebook page
265            is visible.
266         */
267         if (_parent->GetCurrentPage() != _panel) {
268                 return;
269         }
270
271         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
272            non-Latin filenames or paths.
273         */
274         wxFileDialog* d = new wxFileDialog (
275                 _panel,
276                 _("Choose a file or files"),
277                 wxT (""),
278                 wxT (""),
279                 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
280                 wxFD_MULTIPLE | wxFD_CHANGE_DIR
281                 );
282
283         int const r = d->ShowModal ();
284
285         if (r != wxID_OK) {
286                 d->Destroy ();
287                 return;
288         }
289
290         wxArrayString paths;
291         d->GetPaths (paths);
292         list<boost::filesystem::path> path_list;
293         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
294                 path_list.push_back (wx_to_std (paths[i]));
295         }
296         add_files (path_list);
297
298         d->Destroy ();
299 }
300
301 void
302 ContentPanel::add_folder_clicked ()
303 {
304         wxDirDialog* d = new wxDirDialog (_panel, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
305         int r = d->ShowModal ();
306         boost::filesystem::path const path (wx_to_std (d->GetPath ()));
307         d->Destroy ();
308
309         if (r != wxID_OK) {
310                 return;
311         }
312
313         list<shared_ptr<Content> > content;
314
315         try {
316                 content = content_factory (_film, path);
317         } catch (exception& e) {
318                 error_dialog (_parent, e.what());
319                 return;
320         }
321
322         if (content.empty ()) {
323                 error_dialog (_parent, _("No content found in this folder."));
324                 return;
325         }
326
327         BOOST_FOREACH (shared_ptr<Content> i, content) {
328                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
329                 if (ic) {
330                         ImageSequenceDialog* e = new ImageSequenceDialog (_panel);
331                         r = e->ShowModal ();
332                         float const frame_rate = e->frame_rate ();
333                         e->Destroy ();
334
335                         if (r != wxID_OK) {
336                                 return;
337                         }
338
339                         ic->set_video_frame_rate (frame_rate);
340                 }
341
342                 _film->examine_and_add_content (i);
343         }
344 }
345
346 /** @return true if this remove "click" should be ignored */
347 bool
348 ContentPanel::remove_clicked (bool hotkey)
349 {
350         /* If the method was called because Delete was pressed check that our notebook page
351            is visible and that the content list is focussed.
352         */
353         if (hotkey && (_parent->GetCurrentPage() != _panel || !_content->HasFocus())) {
354                 return true;
355         }
356
357         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
358                 _film->remove_content (i);
359         }
360
361         selection_changed ();
362         return false;
363 }
364
365 void
366 ContentPanel::timeline_clicked ()
367 {
368         if (_timeline_dialog) {
369                 _timeline_dialog->Destroy ();
370                 _timeline_dialog = 0;
371         }
372
373         _timeline_dialog = new TimelineDialog (this, _film);
374         _timeline_dialog->Show ();
375 }
376
377 void
378 ContentPanel::right_click (wxListEvent& ev)
379 {
380         _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
381 }
382
383 /** Set up broad sensitivity based on the type of content that is selected */
384 void
385 ContentPanel::setup_sensitivity ()
386 {
387         _add_file->Enable (_generally_sensitive);
388         _add_folder->Enable (_generally_sensitive);
389
390         ContentList selection = selected ();
391         ContentList video_selection = selected_video ();
392         ContentList audio_selection = selected_audio ();
393
394         _remove->Enable   (!selection.empty() && _generally_sensitive);
395         _earlier->Enable  (selection.size() == 1 && _generally_sensitive);
396         _later->Enable    (selection.size() == 1 && _generally_sensitive);
397         _timeline->Enable (!_film->content().empty() && _generally_sensitive);
398
399         _video_panel->Enable    (video_selection.size() > 0 && _generally_sensitive);
400         _audio_panel->Enable    (audio_selection.size() > 0 && _generally_sensitive);
401         _subtitle_panel->Enable (selection.size() == 1 && selection.front()->subtitle && _generally_sensitive);
402         _timing_panel->Enable   (_generally_sensitive);
403 }
404
405 void
406 ContentPanel::set_film (shared_ptr<Film> film)
407 {
408         _audio_panel->set_film (film);
409
410         _film = film;
411
412         film_changed (Film::CONTENT);
413         film_changed (Film::AUDIO_CHANNELS);
414         selection_changed ();
415         setup_sensitivity ();
416 }
417
418 void
419 ContentPanel::set_general_sensitivity (bool s)
420 {
421         _generally_sensitive = s;
422
423         _content->Enable (s);
424         _add_file->Enable (s);
425         _add_folder->Enable (s);
426         _remove->Enable (s);
427         _earlier->Enable (s);
428         _later->Enable (s);
429         _timeline->Enable (s);
430
431         /* Set the panels in the content notebook */
432         BOOST_FOREACH (ContentSubPanel* i, _panels) {
433                 i->Enable (s);
434         }
435 }
436
437 void
438 ContentPanel::earlier_clicked ()
439 {
440         ContentList sel = selected ();
441         if (sel.size() == 1) {
442                 _film->move_content_earlier (sel.front ());
443                 selection_changed ();
444         }
445 }
446
447 void
448 ContentPanel::later_clicked ()
449 {
450         ContentList sel = selected ();
451         if (sel.size() == 1) {
452                 _film->move_content_later (sel.front ());
453                 selection_changed ();
454         }
455 }
456
457 void
458 ContentPanel::set_selection (weak_ptr<Content> wc)
459 {
460         ContentList content = _film->content ();
461         for (size_t i = 0; i < content.size(); ++i) {
462                 if (content[i] == wc.lock ()) {
463                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
464                 } else {
465                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
466                 }
467         }
468 }
469
470 void
471 ContentPanel::set_selection (ContentList cl)
472 {
473         ContentList content = _film->content ();
474         for (size_t i = 0; i < content.size(); ++i) {
475                 if (find(cl.begin(), cl.end(), content[i]) != cl.end()) {
476                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
477                 } else {
478                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
479                 }
480         }
481 }
482
483 void
484 ContentPanel::film_content_changed (int property)
485 {
486         if (
487                 property == ContentProperty::PATH ||
488                 property == DCPContentProperty::NEEDS_ASSETS ||
489                 property == DCPContentProperty::NEEDS_KDM ||
490                 property == DCPContentProperty::NAME
491                 ) {
492
493                 setup ();
494         }
495
496         BOOST_FOREACH (ContentSubPanel* i, _panels) {
497                 i->film_content_changed (property);
498         }
499 }
500
501 void
502 ContentPanel::setup ()
503 {
504         ContentList content = _film->content ();
505
506         Content* selected_content = 0;
507         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
508         if (s != -1) {
509                 wxListItem item;
510                 item.SetId (s);
511                 item.SetMask (wxLIST_MASK_DATA);
512                 _content->GetItem (item);
513                 selected_content = reinterpret_cast<Content*> (item.GetData ());
514         }
515
516         _content->DeleteAllItems ();
517
518         BOOST_FOREACH (shared_ptr<Content> i, content) {
519                 int const t = _content->GetItemCount ();
520                 bool const valid = i->paths_valid ();
521                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i);
522                 bool const needs_kdm = dcp && dcp->needs_kdm ();
523                 bool const needs_assets = dcp && dcp->needs_assets ();
524
525                 wxString s = std_to_wx (i->summary ());
526
527                 if (!valid) {
528                         s = _("MISSING: ") + s;
529                 }
530
531                 if (needs_kdm) {
532                         s = _("NEEDS KDM: ") + s;
533                 }
534
535                 if (needs_assets) {
536                         s = _("NEEDS OV: ") + s;
537                 }
538
539                 wxListItem item;
540                 item.SetId (t);
541                 item.SetText (s);
542                 item.SetData (i.get ());
543                 _content->InsertItem (item);
544
545                 if (i.get() == selected_content) {
546                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
547                 }
548
549                 if (!valid || needs_kdm || needs_assets) {
550                         _content->SetItemTextColour (t, *wxRED);
551                 }
552         }
553
554         if (!selected_content && !content.empty ()) {
555                 /* Select the item of content if none was selected before */
556                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
557         }
558 }
559
560 void
561 ContentPanel::files_dropped (wxDropFilesEvent& event)
562 {
563         if (!_film) {
564                 return;
565         }
566
567         wxString* paths = event.GetFiles ();
568         list<boost::filesystem::path> path_list;
569         for (int i = 0; i < event.GetNumberOfFiles(); i++) {
570                 path_list.push_back (wx_to_std (paths[i]));
571         }
572
573         add_files (path_list);
574 }
575
576 void
577 ContentPanel::add_files (list<boost::filesystem::path> paths)
578 {
579         /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
580            I can't reproduce that, but sort them anyway.  Don't use ImageFilenameSorter as a normal
581            alphabetical sort is expected here.
582         */
583
584         paths.sort (CaseInsensitiveSorter ());
585
586         /* XXX: check for lots of files here and do something */
587
588         BOOST_FOREACH (boost::filesystem::path i, paths) {
589                 BOOST_FOREACH (shared_ptr<Content> j, content_factory (_film, i)) {
590                         _film->examine_and_add_content (j);
591                 }
592         }
593 }