Add has_subtitles method to SubtitleContent; tidy up timeline display a bit.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2014 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 <list>
21 #include <wx/graphics.h>
22 #include <boost/weak_ptr.hpp>
23 #include "lib/film.h"
24 #include "lib/playlist.h"
25 #include "lib/image_content.h"
26 #include "film_editor.h"
27 #include "timeline.h"
28 #include "wx_util.h"
29
30 using std::list;
31 using std::cout;
32 using std::max;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
36 using boost::bind;
37 using boost::optional;
38
39 /** @class View
40  *  @brief Parent class for components of the timeline (e.g. a piece of content or an axis).
41  */
42 class View : public boost::noncopyable
43 {
44 public:
45         View (Timeline& t)
46                 : _timeline (t)
47         {
48
49         }
50
51         virtual ~View () {}
52                 
53         void paint (wxGraphicsContext* g)
54         {
55                 _last_paint_bbox = bbox ();
56                 do_paint (g);
57         }
58         
59         void force_redraw ()
60         {
61                 _timeline.force_redraw (_last_paint_bbox);
62                 _timeline.force_redraw (bbox ());
63         }
64
65         virtual dcpomatic::Rect<int> bbox () const = 0;
66
67 protected:
68         virtual void do_paint (wxGraphicsContext *) = 0;
69         
70         int time_x (DCPTime t) const
71         {
72                 return _timeline.tracks_position().x + t.seconds() * _timeline.pixels_per_second().get_value_or (0);
73         }
74         
75         Timeline& _timeline;
76
77 private:
78         dcpomatic::Rect<int> _last_paint_bbox;
79 };
80
81
82 /** @class ContentView
83  *  @brief Parent class for views of pieces of content.
84  */
85 class ContentView : public View
86 {
87 public:
88         ContentView (Timeline& tl, shared_ptr<Content> c)
89                 : View (tl)
90                 , _content (c)
91                 , _selected (false)
92         {
93                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
94         }
95
96         dcpomatic::Rect<int> bbox () const
97         {
98                 assert (_track);
99
100                 shared_ptr<const Film> film = _timeline.film ();
101                 shared_ptr<const Content> content = _content.lock ();
102                 if (!film || !content) {
103                         return dcpomatic::Rect<int> ();
104                 }
105                 
106                 return dcpomatic::Rect<int> (
107                         time_x (content->position ()) - 8,
108                         y_pos (_track.get()) - 8,
109                         content->length_after_trim().seconds() * _timeline.pixels_per_second().get_value_or(0) + 16,
110                         _timeline.track_height() + 16
111                         );
112         }
113
114         void set_selected (bool s) {
115                 _selected = s;
116                 force_redraw ();
117         }
118         
119         bool selected () const {
120                 return _selected;
121         }
122
123         shared_ptr<Content> content () const {
124                 return _content.lock ();
125         }
126
127         void set_track (int t) {
128                 _track = t;
129         }
130
131         void unset_track () {
132                 _track = boost::optional<int> ();
133         }
134
135         optional<int> track () const {
136                 return _track;
137         }
138
139         virtual wxString type () const = 0;
140         virtual wxColour colour () const = 0;
141         
142 private:
143
144         void do_paint (wxGraphicsContext* gc)
145         {
146                 assert (_track);
147
148                 shared_ptr<const Film> film = _timeline.film ();
149                 shared_ptr<const Content> cont = content ();
150                 if (!film || !cont) {
151                         return;
152                 }
153
154                 DCPTime const position = cont->position ();
155                 DCPTime const len = cont->length_after_trim ();
156
157                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
158
159                 gc->SetPen (*wxBLACK_PEN);
160                 
161                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
162                 if (_selected) {
163                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
164                 } else {
165                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
166                 }
167
168                 wxGraphicsPath path = gc->CreatePath ();
169                 path.MoveToPoint    (time_x (position),       y_pos (_track.get()) + 4);
170                 path.AddLineToPoint (time_x (position + len), y_pos (_track.get()) + 4);
171                 path.AddLineToPoint (time_x (position + len), y_pos (_track.get() + 1) - 4);
172                 path.AddLineToPoint (time_x (position),       y_pos (_track.get() + 1) - 4);
173                 path.AddLineToPoint (time_x (position),       y_pos (_track.get()) + 4);
174                 gc->StrokePath (path);
175                 gc->FillPath (path);
176
177                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->summary()).data(), type().data());
178                 wxDouble name_width;
179                 wxDouble name_height;
180                 wxDouble name_descent;
181                 wxDouble name_leading;
182                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
183                 
184                 gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.track_height()));
185                 gc->DrawText (name, time_x (position) + 12, y_pos (_track.get() + 1) - name_height - 4);
186                 gc->ResetClip ();
187         }
188
189         int y_pos (int t) const
190         {
191                 return _timeline.tracks_position().y + t * _timeline.track_height();
192         }
193
194         void content_changed (int p, bool frequent)
195         {
196                 ensure_ui_thread ();
197                 
198                 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
199                         force_redraw ();
200                 }
201
202                 if (!frequent) {
203                         _timeline.setup_pixels_per_second ();
204                         _timeline.Refresh ();
205                 }
206         }
207
208         boost::weak_ptr<Content> _content;
209         optional<int> _track;
210         bool _selected;
211
212         boost::signals2::scoped_connection _content_connection;
213 };
214
215 class AudioContentView : public ContentView
216 {
217 public:
218         AudioContentView (Timeline& tl, shared_ptr<Content> c)
219                 : ContentView (tl, c)
220         {}
221         
222 private:
223         wxString type () const
224         {
225                 return _("audio");
226         }
227
228         wxColour colour () const
229         {
230                 return wxColour (149, 121, 232, 255);
231         }
232 };
233
234 class VideoContentView : public ContentView
235 {
236 public:
237         VideoContentView (Timeline& tl, shared_ptr<Content> c)
238                 : ContentView (tl, c)
239         {}
240
241 private:        
242
243         wxString type () const
244         {
245                 if (dynamic_pointer_cast<ImageContent> (content ()) && content()->number_of_paths() == 1) {
246                         return _("still");
247                 } else {
248                         return _("video");
249                 }
250         }
251
252         wxColour colour () const
253         {
254                 return wxColour (242, 92, 120, 255);
255         }
256 };
257
258 class SubtitleContentView : public ContentView
259 {
260 public:
261         SubtitleContentView (Timeline& tl, shared_ptr<Content> c)
262                 : ContentView (tl, c)
263         {}
264
265 private:
266         wxString type () const
267         {
268                 return _("subtitles");
269         }
270
271         wxColour colour () const
272         {
273                 return wxColour (163, 255, 154, 255);
274         }
275 };
276
277 class TimeAxisView : public View
278 {
279 public:
280         TimeAxisView (Timeline& tl, int y)
281                 : View (tl)
282                 , _y (y)
283         {}
284         
285         dcpomatic::Rect<int> bbox () const
286         {
287                 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
288         }
289
290         void set_y (int y)
291         {
292                 _y = y;
293                 force_redraw ();
294         }
295
296 private:        
297
298         void do_paint (wxGraphicsContext* gc)
299         {
300                 if (!_timeline.pixels_per_second()) {
301                         return;
302                 }
303
304                 double const pps = _timeline.pixels_per_second().get ();
305                 
306                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
307                 
308                 double mark_interval = rint (128 / pps);
309                 if (mark_interval > 5) {
310                         mark_interval -= int (rint (mark_interval)) % 5;
311                 }
312                 if (mark_interval > 10) {
313                         mark_interval -= int (rint (mark_interval)) % 10;
314                 }
315                 if (mark_interval > 60) {
316                         mark_interval -= int (rint (mark_interval)) % 60;
317                 }
318                 if (mark_interval > 3600) {
319                         mark_interval -= int (rint (mark_interval)) % 3600;
320                 }
321                 
322                 if (mark_interval < 1) {
323                         mark_interval = 1;
324                 }
325
326                 wxGraphicsPath path = gc->CreatePath ();
327                 path.MoveToPoint (_timeline.x_offset(), _y);
328                 path.AddLineToPoint (_timeline.width(), _y);
329                 gc->StrokePath (path);
330
331                 /* Time in seconds */
332                 DCPTime t;
333                 while ((t.seconds() * pps) < _timeline.width()) {
334                         wxGraphicsPath path = gc->CreatePath ();
335                         path.MoveToPoint (time_x (t), _y - 4);
336                         path.AddLineToPoint (time_x (t), _y + 4);
337                         gc->StrokePath (path);
338
339                         double tc = t.seconds ();
340                         int const h = tc / 3600;
341                         tc -= h * 3600;
342                         int const m = tc / 60;
343                         tc -= m * 60;
344                         int const s = tc;
345                         
346                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
347                         wxDouble str_width;
348                         wxDouble str_height;
349                         wxDouble str_descent;
350                         wxDouble str_leading;
351                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
352                         
353                         int const tx = _timeline.x_offset() + t.seconds() * pps;
354                         if ((tx + str_width) < _timeline.width()) {
355                                 gc->DrawText (str, time_x (t), _y + 16);
356                         }
357                         
358                         t += DCPTime::from_seconds (mark_interval);
359                 }
360         }
361
362 private:
363         int _y;
364 };
365
366
367 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
368         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
369         , _film_editor (ed)
370         , _film (film)
371         , _time_axis_view (new TimeAxisView (*this, 32))
372         , _tracks (0)
373         , _left_down (false)
374         , _down_view_position (0)
375         , _first_move (false)
376         , _menu (this)
377         , _snap (true)
378 {
379 #ifndef __WXOSX__
380         SetDoubleBuffered (true);
381 #endif  
382
383         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
384         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
385         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
386         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
387         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
388         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
389
390         playlist_changed ();
391
392         SetMinSize (wxSize (640, tracks() * track_height() + 96));
393
394         _playlist_changed_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
395         _playlist_content_changed_connection = film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_content_changed, this, _2));
396 }
397
398 void
399 Timeline::paint ()
400 {
401         wxPaintDC dc (this);
402
403         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
404         if (!gc) {
405                 return;
406         }
407
408         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
409
410         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
411                 (*i)->paint (gc);
412         }
413
414         delete gc;
415 }
416
417 void
418 Timeline::playlist_changed ()
419 {
420         ensure_ui_thread ();
421         
422         shared_ptr<const Film> fl = _film.lock ();
423         if (!fl) {
424                 return;
425         }
426
427         _views.clear ();
428         _views.push_back (_time_axis_view);
429
430         ContentList content = fl->playlist()->content ();
431
432         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
433                 if (dynamic_pointer_cast<VideoContent> (*i)) {
434                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
435                 }
436                 if (dynamic_pointer_cast<AudioContent> (*i)) {
437                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
438                 }
439
440                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (*i);
441                 if (sc && sc->has_subtitles ()) {
442                         _views.push_back (shared_ptr<View> (new SubtitleContentView (*this, *i)));
443                 }
444         }
445
446         assign_tracks ();
447         setup_pixels_per_second ();
448         Refresh ();
449 }
450
451 void
452 Timeline::playlist_content_changed (int property)
453 {
454         ensure_ui_thread ();
455
456         if (property == ContentProperty::POSITION) {
457                 assign_tracks ();
458                 setup_pixels_per_second ();
459                 Refresh ();
460         }
461 }
462
463 void
464 Timeline::assign_tracks ()
465 {
466         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
467                 shared_ptr<ContentView> c = dynamic_pointer_cast<ContentView> (*i);
468                 if (c) {
469                         c->unset_track ();
470                 }
471         }
472
473         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
474                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
475                 if (!cv) {
476                         continue;
477                 }
478
479                 shared_ptr<Content> content = cv->content();
480
481                 int t = 0;
482                 while (true) {
483                         ViewList::iterator j = _views.begin();
484                         while (j != _views.end()) {
485                                 shared_ptr<ContentView> test = dynamic_pointer_cast<ContentView> (*j);
486                                 if (!test) {
487                                         ++j;
488                                         continue;
489                                 }
490                                 
491                                 shared_ptr<Content> test_content = test->content();
492                                         
493                                 if (test && test->track() && test->track().get() == t) {
494                                         bool const no_overlap =
495                                                 (content->position() < test_content->position() && content->end() < test_content->position()) ||
496                                                 (content->position() > test_content->end()      && content->end() > test_content->end());
497                                         
498                                         if (!no_overlap) {
499                                                 /* we have an overlap on track `t' */
500                                                 ++t;
501                                                 break;
502                                         }
503                                 }
504                                 
505                                 ++j;
506                         }
507
508                         if (j == _views.end ()) {
509                                 /* no overlap on `t' */
510                                 break;
511                         }
512                 }
513
514                 cv->set_track (t);
515                 _tracks = max (_tracks, t + 1);
516         }
517
518         _time_axis_view->set_y (tracks() * track_height() + 32);
519 }
520
521 int
522 Timeline::tracks () const
523 {
524         return _tracks;
525 }
526
527 void
528 Timeline::setup_pixels_per_second ()
529 {
530         shared_ptr<const Film> film = _film.lock ();
531         if (!film || film->length() == DCPTime ()) {
532                 return;
533         }
534
535         _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
536 }
537
538 shared_ptr<View>
539 Timeline::event_to_view (wxMouseEvent& ev)
540 {
541         ViewList::iterator i = _views.begin();
542         Position<int> const p (ev.GetX(), ev.GetY());
543         while (i != _views.end() && !(*i)->bbox().contains (p)) {
544                 ++i;
545         }
546
547         if (i == _views.end ()) {
548                 return shared_ptr<View> ();
549         }
550
551         return *i;
552 }
553
554 void
555 Timeline::left_down (wxMouseEvent& ev)
556 {
557         shared_ptr<View> view = event_to_view (ev);
558         shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
559
560         _down_view.reset ();
561
562         if (content_view) {
563                 _down_view = content_view;
564                 _down_view_position = content_view->content()->position ();
565         }
566
567         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
568                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
569                 if (!cv) {
570                         continue;
571                 }
572                 
573                 if (!ev.ShiftDown ()) {
574                         cv->set_selected (view == *i);
575                 }
576                 
577                 if (view == *i) {
578                         _film_editor->set_selection (cv->content ());
579                 }
580         }
581
582         if (content_view && ev.ShiftDown ()) {
583                 content_view->set_selected (!content_view->selected ());
584         }
585
586         _left_down = true;
587         _down_point = ev.GetPosition ();
588         _first_move = false;
589
590         if (_down_view) {
591                 _down_view->content()->set_change_signals_frequent (true);
592         }
593 }
594
595 void
596 Timeline::left_up (wxMouseEvent& ev)
597 {
598         _left_down = false;
599
600         if (_down_view) {
601                 _down_view->content()->set_change_signals_frequent (false);
602         }
603
604         set_position_from_event (ev);
605 }
606
607 void
608 Timeline::mouse_moved (wxMouseEvent& ev)
609 {
610         if (!_left_down) {
611                 return;
612         }
613
614         set_position_from_event (ev);
615 }
616
617 void
618 Timeline::right_down (wxMouseEvent& ev)
619 {
620         shared_ptr<View> view = event_to_view (ev);
621         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
622         if (!cv) {
623                 return;
624         }
625
626         if (!cv->selected ()) {
627                 clear_selection ();
628                 cv->set_selected (true);
629         }
630
631         _menu.popup (_film, selected_content (), ev.GetPosition ());
632 }
633
634 void
635 Timeline::set_position_from_event (wxMouseEvent& ev)
636 {
637         if (!_pixels_per_second) {
638                 return;
639         }
640
641         double const pps = _pixels_per_second.get ();
642
643         wxPoint const p = ev.GetPosition();
644
645         if (!_first_move) {
646                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
647                    before the drag is considered to have started.
648                 */
649                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
650                 if (dist < 8) {
651                         return;
652                 }
653                 _first_move = true;
654         }
655
656         if (!_down_view) {
657                 return;
658         }
659         
660         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
661         
662         if (_snap) {
663                 
664                 bool first = true;
665                 DCPTime nearest_distance = DCPTime::max ();
666                 DCPTime nearest_new_position = DCPTime::max ();
667                 
668                 /* Find the nearest content edge; this is inefficient */
669                 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
670                         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
671                         if (!cv || cv == _down_view) {
672                                 continue;
673                         }
674                         
675                         {
676                                 /* Snap starts to ends */
677                                 DCPTime const d = DCPTime (cv->content()->end() - new_position).abs ();
678                                 if (first || d < nearest_distance) {
679                                         nearest_distance = d;
680                                         nearest_new_position = cv->content()->end();
681                                 }
682                         }
683                         
684                         {
685                                 /* Snap ends to starts */
686                                 DCPTime const d = DCPTime (
687                                         cv->content()->position() - (new_position + _down_view->content()->length_after_trim())
688                                         ).abs ();
689                                 
690                                 if (d < nearest_distance) {
691                                         nearest_distance = d;
692                                         nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
693                                 }
694                         }
695                         
696                         first = false;
697                 }
698                 
699                 if (!first) {
700                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
701                         if (nearest_distance < DCPTime::from_seconds ((width() / pps) / 32)) {
702                                 new_position = nearest_new_position;
703                         }
704                 }
705         }
706         
707         if (new_position < DCPTime ()) {
708                 new_position = DCPTime ();
709         }
710
711         _down_view->content()->set_position (new_position);
712         
713         shared_ptr<Film> film = _film.lock ();
714         assert (film);
715         film->set_sequence_video (false);
716 }
717
718 void
719 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
720 {
721         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
722 }
723
724 shared_ptr<const Film>
725 Timeline::film () const
726 {
727         return _film.lock ();
728 }
729
730 void
731 Timeline::resized ()
732 {
733         setup_pixels_per_second ();
734 }
735
736 void
737 Timeline::clear_selection ()
738 {
739         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
740                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
741                 if (cv) {
742                         cv->set_selected (false);
743                 }
744         }
745 }
746
747 Timeline::ContentViewList
748 Timeline::selected_views () const
749 {
750         ContentViewList sel;
751         
752         for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
753                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
754                 if (cv && cv->selected()) {
755                         sel.push_back (cv);
756                 }
757         }
758
759         return sel;
760 }
761
762 ContentList
763 Timeline::selected_content () const
764 {
765         ContentList sel;
766         ContentViewList views = selected_views ();
767         
768         for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
769                 sel.push_back ((*i)->content ());
770         }
771
772         return sel;
773 }