Add simple copy and paste for content settings (#1051).
[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 "lib/config.h"
40 #include "lib/log.h"
41 #include "lib/compose.hpp"
42 #include <wx/wx.h>
43 #include <wx/notebook.h>
44 #include <wx/listctrl.h>
45 #include <boost/filesystem.hpp>
46 #include <boost/foreach.hpp>
47 #include <iostream>
48
49 using std::list;
50 using std::string;
51 using std::cout;
52 using std::vector;
53 using std::exception;
54 using boost::shared_ptr;
55 using boost::weak_ptr;
56 using boost::dynamic_pointer_cast;
57 using boost::optional;
58
59 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
60
61 ContentPanel::ContentPanel (wxNotebook* n, boost::shared_ptr<Film> film, FilmViewer* viewer)
62         : _timeline_dialog (0)
63         , _parent (n)
64         , _film (film)
65         , _film_viewer (viewer)
66         , _generally_sensitive (true)
67 {
68         _panel = new wxPanel (n);
69         _sizer = new wxBoxSizer (wxVERTICAL);
70         _panel->SetSizer (_sizer);
71
72         _menu = new ContentMenu (_panel);
73
74         {
75                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
76
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);
80
81                 _content->InsertColumn (0, wxT(""));
82                 _content->SetColumnWidth (0, 512);
83
84                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
85
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);
89
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);
93
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);
97
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);
101
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);
105
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);
109
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);
113
114                 s->Add (b, 0, wxALL, 4);
115
116                 _sizer->Add (s, 0, wxEXPAND | wxALL, 6);
117         }
118
119         _notebook = new wxNotebook (_panel, wxID_ANY);
120         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
121
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);
130
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));
142 }
143
144 ContentList
145 ContentPanel::selected ()
146 {
147         ContentList sel;
148         long int s = -1;
149         while (true) {
150                 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
151                 if (s == -1) {
152                         break;
153                 }
154
155                 if (s < int (_film->content().size ())) {
156                         sel.push_back (_film->content()[s]);
157                 }
158         }
159
160         return sel;
161 }
162
163 ContentList
164 ContentPanel::selected_video ()
165 {
166         ContentList vc;
167
168         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
169                 if (i->video) {
170                         vc.push_back (i);
171                 }
172         }
173
174         return vc;
175 }
176
177 ContentList
178 ContentPanel::selected_audio ()
179 {
180         ContentList ac;
181
182         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
183                 if (i->audio) {
184                         ac.push_back (i);
185                 }
186         }
187
188         return ac;
189 }
190
191 ContentList
192 ContentPanel::selected_subtitle ()
193 {
194         ContentList sc;
195
196         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
197                 if (i->subtitle) {
198                         sc.push_back (i);
199                 }
200         }
201
202         return sc;
203 }
204
205 FFmpegContentList
206 ContentPanel::selected_ffmpeg ()
207 {
208         FFmpegContentList sc;
209
210         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
211                 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (i);
212                 if (t) {
213                         sc.push_back (t);
214                 }
215         }
216
217         return sc;
218 }
219
220 void
221 ContentPanel::film_changed (Film::Property p)
222 {
223         switch (p) {
224         case Film::CONTENT:
225         case Film::CONTENT_ORDER:
226                 setup ();
227                 break;
228         default:
229                 break;
230         }
231
232         BOOST_FOREACH (ContentSubPanel* i, _panels) {
233                 i->film_changed (p);
234         }
235 }
236
237 void
238 ContentPanel::selection_changed ()
239 {
240         if (_last_selected == selected()) {
241                 /* This was triggered by a re-build of the view but the selection
242                    did not really change.
243                 */
244                 return;
245         }
246
247         _last_selected = selected ();
248
249         setup_sensitivity ();
250
251         BOOST_FOREACH (ContentSubPanel* i, _panels) {
252                 i->content_selection_changed ();
253         }
254
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 ();
259                 }
260         }
261
262         if (go_to && Config::instance()->jump_to_selected ()) {
263                 _film_viewer->set_position (go_to.get ());
264         }
265
266         if (_timeline_dialog) {
267                 _timeline_dialog->set_selection (selected ());
268         }
269
270         SelectionChanged ();
271 }
272
273 void
274 ContentPanel::add_file_clicked ()
275 {
276         /* This method is also called when Ctrl-A is pressed, so check that our notebook page
277            is visible.
278         */
279         if (_parent->GetCurrentPage() != _panel || !_film) {
280                 return;
281         }
282
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.
285         */
286         wxFileDialog* d = new wxFileDialog (
287                 _panel,
288                 _("Choose a file or files"),
289                 wxT (""),
290                 wxT (""),
291                 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
292                 wxFD_MULTIPLE | wxFD_CHANGE_DIR
293                 );
294
295         int const r = d->ShowModal ();
296
297         if (r != wxID_OK) {
298                 d->Destroy ();
299                 return;
300         }
301
302         wxArrayString paths;
303         d->GetPaths (paths);
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]));
307         }
308         add_files (path_list);
309
310         d->Destroy ();
311 }
312
313 void
314 ContentPanel::add_folder_clicked ()
315 {
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 ()));
319         d->Destroy ();
320
321         if (r != wxID_OK) {
322                 return;
323         }
324
325         list<shared_ptr<Content> > content;
326
327         try {
328                 content = content_factory (_film, path);
329         } catch (exception& e) {
330                 error_dialog (_parent, e.what());
331                 return;
332         }
333
334         if (content.empty ()) {
335                 error_dialog (_parent, _("No content found in this folder."));
336                 return;
337         }
338
339         BOOST_FOREACH (shared_ptr<Content> i, content) {
340                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
341                 if (ic) {
342                         ImageSequenceDialog* e = new ImageSequenceDialog (_panel);
343                         r = e->ShowModal ();
344                         float const frame_rate = e->frame_rate ();
345                         e->Destroy ();
346
347                         if (r != wxID_OK) {
348                                 return;
349                         }
350
351                         ic->set_video_frame_rate (frame_rate);
352                 }
353
354                 _film->examine_and_add_content (i);
355         }
356 }
357
358 void
359 ContentPanel::add_dcp_clicked ()
360 {
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 ()));
364         d->Destroy ();
365
366         if (r != wxID_OK) {
367                 return;
368         }
369
370         try {
371                 _film->examine_and_add_content (shared_ptr<Content> (new DCPContent (_film, path)));
372         } catch (exception& e) {
373                 error_dialog (_parent, e.what());
374         }
375 }
376
377 /** @return true if this remove "click" should be ignored */
378 bool
379 ContentPanel::remove_clicked (bool hotkey)
380 {
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.
383         */
384         if (hotkey && (_parent->GetCurrentPage() != _panel || !_content->HasFocus())) {
385                 return true;
386         }
387
388         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
389                 _film->remove_content (i);
390         }
391
392         selection_changed ();
393         return false;
394 }
395
396 void
397 ContentPanel::timeline_clicked ()
398 {
399         if (_timeline_dialog) {
400                 _timeline_dialog->Destroy ();
401                 _timeline_dialog = 0;
402         }
403
404         _timeline_dialog = new TimelineDialog (this, _film);
405         _timeline_dialog->Show ();
406 }
407
408 void
409 ContentPanel::right_click (wxListEvent& ev)
410 {
411         _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
412 }
413
414 /** Set up broad sensitivity based on the type of content that is selected */
415 void
416 ContentPanel::setup_sensitivity ()
417 {
418         _add_file->Enable (_generally_sensitive);
419         _add_folder->Enable (_generally_sensitive);
420         _add_dcp->Enable (_generally_sensitive);
421
422         ContentList selection = selected ();
423         ContentList video_selection = selected_video ();
424         ContentList audio_selection = selected_audio ();
425
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());
430
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);
435 }
436
437 void
438 ContentPanel::set_film (shared_ptr<Film> film)
439 {
440         _audio_panel->set_film (film);
441
442         _film = film;
443
444         film_changed (Film::CONTENT);
445         film_changed (Film::AUDIO_CHANNELS);
446         selection_changed ();
447         setup_sensitivity ();
448 }
449
450 void
451 ContentPanel::set_general_sensitivity (bool s)
452 {
453         _generally_sensitive = s;
454         setup_sensitivity ();
455 }
456
457 void
458 ContentPanel::earlier_clicked ()
459 {
460         ContentList sel = selected ();
461         if (sel.size() == 1) {
462                 _film->move_content_earlier (sel.front ());
463                 selection_changed ();
464         }
465 }
466
467 void
468 ContentPanel::later_clicked ()
469 {
470         ContentList sel = selected ();
471         if (sel.size() == 1) {
472                 _film->move_content_later (sel.front ());
473                 selection_changed ();
474         }
475 }
476
477 void
478 ContentPanel::set_selection (weak_ptr<Content> wc)
479 {
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);
484                 } else {
485                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
486                 }
487         }
488 }
489
490 void
491 ContentPanel::set_selection (ContentList cl)
492 {
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);
497                 } else {
498                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
499                 }
500         }
501 }
502
503 void
504 ContentPanel::film_content_changed (int property)
505 {
506         if (
507                 property == ContentProperty::PATH ||
508                 property == DCPContentProperty::NEEDS_ASSETS ||
509                 property == DCPContentProperty::NEEDS_KDM ||
510                 property == DCPContentProperty::NAME
511                 ) {
512
513                 setup ();
514         }
515
516         BOOST_FOREACH (ContentSubPanel* i, _panels) {
517                 i->film_content_changed (property);
518         }
519 }
520
521 void
522 ContentPanel::setup ()
523 {
524         ContentList content = _film->content ();
525
526         Content* selected_content = 0;
527         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
528         if (s != -1) {
529                 wxListItem item;
530                 item.SetId (s);
531                 item.SetMask (wxLIST_MASK_DATA);
532                 _content->GetItem (item);
533                 selected_content = reinterpret_cast<Content*> (item.GetData ());
534         }
535
536         _content->DeleteAllItems ();
537
538         BOOST_FOREACH (shared_ptr<Content> i, content) {
539                 int const t = _content->GetItemCount ();
540                 bool const valid = i->paths_valid ();
541
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");
545                 }
546
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 ();
550
551                 wxString s = std_to_wx (i->summary ());
552
553                 if (!valid) {
554                         s = _("MISSING: ") + s;
555                 }
556
557                 if (needs_kdm) {
558                         s = _("NEEDS KDM: ") + s;
559                 }
560
561                 if (needs_assets) {
562                         s = _("NEEDS OV: ") + s;
563                 }
564
565                 wxListItem item;
566                 item.SetId (t);
567                 item.SetText (s);
568                 item.SetData (i.get ());
569                 _content->InsertItem (item);
570
571                 if (i.get() == selected_content) {
572                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
573                 }
574
575                 if (!valid || needs_kdm || needs_assets) {
576                         _content->SetItemTextColour (t, *wxRED);
577                 }
578         }
579
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);
583         }
584
585         setup_sensitivity ();
586 }
587
588 void
589 ContentPanel::files_dropped (wxDropFilesEvent& event)
590 {
591         if (!_film) {
592                 return;
593         }
594
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]));
599         }
600
601         add_files (path_list);
602 }
603
604 void
605 ContentPanel::add_files (list<boost::filesystem::path> paths)
606 {
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.
610         */
611
612         paths.sort (CaseInsensitiveSorter ());
613
614         /* XXX: check for lots of files here and do something */
615
616         try {
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);
620                         }
621                 }
622         } catch (exception& e) {
623                 error_dialog (_parent, e.what());
624         }
625 }