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