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