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