Bump version
[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().get_value_or (0);
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().get_value_or(0) + 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().get_value_or(0), _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                 if (!_timeline.pixels_per_time_unit()) {
273                         return;
274                 }
275
276                 double const pptu = _timeline.pixels_per_time_unit().get ();
277                 
278                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
279                 
280                 int mark_interval = rint (128 / (TIME_HZ * pptu));
281                 if (mark_interval > 5) {
282                         mark_interval -= mark_interval % 5;
283                 }
284                 if (mark_interval > 10) {
285                         mark_interval -= mark_interval % 10;
286                 }
287                 if (mark_interval > 60) {
288                         mark_interval -= mark_interval % 60;
289                 }
290                 if (mark_interval > 3600) {
291                         mark_interval -= mark_interval % 3600;
292                 }
293                 
294                 if (mark_interval < 1) {
295                         mark_interval = 1;
296                 }
297
298                 wxGraphicsPath path = gc->CreatePath ();
299                 path.MoveToPoint (_timeline.x_offset(), _y);
300                 path.AddLineToPoint (_timeline.width(), _y);
301                 gc->StrokePath (path);
302
303                 Time t = 0;
304                 while ((t * pptu) < _timeline.width()) {
305                         wxGraphicsPath path = gc->CreatePath ();
306                         path.MoveToPoint (time_x (t), _y - 4);
307                         path.AddLineToPoint (time_x (t), _y + 4);
308                         gc->StrokePath (path);
309
310                         int tc = t / TIME_HZ;
311                         int const h = tc / 3600;
312                         tc -= h * 3600;
313                         int const m = tc / 60;
314                         tc -= m * 60;
315                         int const s = tc;
316                         
317                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
318                         wxDouble str_width;
319                         wxDouble str_height;
320                         wxDouble str_descent;
321                         wxDouble str_leading;
322                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
323                         
324                         int const tx = _timeline.x_offset() + t * pptu;
325                         if ((tx + str_width) < _timeline.width()) {
326                                 gc->DrawText (str, time_x (t), _y + 16);
327                         }
328                         
329                         t += mark_interval * TIME_HZ;
330                 }
331         }
332
333 private:
334         int _y;
335 };
336
337
338 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
339         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
340         , _film_editor (ed)
341         , _film (film)
342         , _time_axis_view (new TimeAxisView (*this, 32))
343         , _tracks (0)
344         , _left_down (false)
345         , _down_view_position (0)
346         , _first_move (false)
347         , _menu (this)
348         , _snap (true)
349 {
350 #ifndef __WXOSX__
351         SetDoubleBuffered (true);
352 #endif  
353
354         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
355         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
356         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
357         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
358         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
359         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
360
361         playlist_changed ();
362
363         SetMinSize (wxSize (640, tracks() * track_height() + 96));
364
365         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
366 }
367
368 void
369 Timeline::paint ()
370 {
371         wxPaintDC dc (this);
372
373         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
374         if (!gc) {
375                 return;
376         }
377
378         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
379
380         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
381                 (*i)->paint (gc);
382         }
383
384         delete gc;
385 }
386
387 void
388 Timeline::playlist_changed ()
389 {
390         ensure_ui_thread ();
391         
392         shared_ptr<const Film> fl = _film.lock ();
393         if (!fl) {
394                 return;
395         }
396
397         _views.clear ();
398         _views.push_back (_time_axis_view);
399
400         ContentList content = fl->playlist()->content ();
401
402         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
403                 if (dynamic_pointer_cast<VideoContent> (*i)) {
404                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
405                 }
406                 if (dynamic_pointer_cast<AudioContent> (*i)) {
407                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
408                 }
409         }
410
411         assign_tracks ();
412         setup_pixels_per_time_unit ();
413         Refresh ();
414 }
415
416 void
417 Timeline::assign_tracks ()
418 {
419         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
420                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
421                 if (!cv) {
422                         continue;
423                 }
424         
425                 shared_ptr<Content> content = cv->content();
426
427                 int t = 0;
428                 while (1) {
429                         ViewList::iterator j = _views.begin();
430                         while (j != _views.end()) {
431                                 shared_ptr<ContentView> test = dynamic_pointer_cast<ContentView> (*j);
432                                 if (!test) {
433                                         ++j;
434                                         continue;
435                                 }
436                                 
437                                 shared_ptr<Content> test_content = test->content();
438                                         
439                                 if (test && test->track() && test->track().get() == t) {
440                                         bool const no_overlap =
441                                                 (content->position() < test_content->position() && content->end() < test_content->position()) ||
442                                                 (content->position() > test_content->end()      && content->end() > test_content->end());
443                                         
444                                         if (!no_overlap) {
445                                                 /* we have an overlap on track `t' */
446                                                 ++t;
447                                                 break;
448                                         }
449                                 }
450                                 
451                                 ++j;
452                         }
453
454                         if (j == _views.end ()) {
455                                 /* no overlap on `t' */
456                                 break;
457                         }
458                 }
459
460                 cv->set_track (t);
461                 _tracks = max (_tracks, t + 1);
462         }
463
464         _time_axis_view->set_y (tracks() * track_height() + 32);
465 }
466
467 int
468 Timeline::tracks () const
469 {
470         return _tracks;
471 }
472
473 void
474 Timeline::setup_pixels_per_time_unit ()
475 {
476         shared_ptr<const Film> film = _film.lock ();
477         if (!film || film->length() == 0) {
478                 return;
479         }
480
481         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
482 }
483
484 shared_ptr<View>
485 Timeline::event_to_view (wxMouseEvent& ev)
486 {
487         ViewList::iterator i = _views.begin();
488         Position<int> const p (ev.GetX(), ev.GetY());
489         while (i != _views.end() && !(*i)->bbox().contains (p)) {
490                 ++i;
491         }
492
493         if (i == _views.end ()) {
494                 return shared_ptr<View> ();
495         }
496
497         return *i;
498 }
499
500 void
501 Timeline::left_down (wxMouseEvent& ev)
502 {
503         shared_ptr<View> view = event_to_view (ev);
504         shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
505
506         _down_view.reset ();
507
508         if (content_view) {
509                 _down_view = content_view;
510                 _down_view_position = content_view->content()->position ();
511         }
512
513         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
514                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
515                 if (!cv) {
516                         continue;
517                 }
518                 
519                 if (!ev.ShiftDown ()) {
520                         cv->set_selected (view == *i);
521                 }
522                 
523                 if (view == *i) {
524                         _film_editor->set_selection (cv->content ());
525                 }
526         }
527
528         if (content_view && ev.ShiftDown ()) {
529                 content_view->set_selected (!content_view->selected ());
530         }
531
532         _left_down = true;
533         _down_point = ev.GetPosition ();
534         _first_move = false;
535
536         if (_down_view) {
537                 _down_view->content()->set_change_signals_frequent (true);
538         }
539 }
540
541 void
542 Timeline::left_up (wxMouseEvent& ev)
543 {
544         _left_down = false;
545
546         if (_down_view) {
547                 _down_view->content()->set_change_signals_frequent (false);
548         }
549
550         set_position_from_event (ev);
551 }
552
553 void
554 Timeline::mouse_moved (wxMouseEvent& ev)
555 {
556         if (!_left_down) {
557                 return;
558         }
559
560         set_position_from_event (ev);
561 }
562
563 void
564 Timeline::right_down (wxMouseEvent& ev)
565 {
566         shared_ptr<View> view = event_to_view (ev);
567         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
568         if (!cv) {
569                 return;
570         }
571
572         if (!cv->selected ()) {
573                 clear_selection ();
574                 cv->set_selected (true);
575         }
576
577         _menu.popup (_film, selected_content (), ev.GetPosition ());
578 }
579
580 void
581 Timeline::set_position_from_event (wxMouseEvent& ev)
582 {
583         if (!_pixels_per_time_unit) {
584                 return;
585         }
586
587         double const pptu = _pixels_per_time_unit.get ();
588
589         wxPoint const p = ev.GetPosition();
590
591         if (!_first_move) {
592                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
593                    before the drag is considered to have started.
594                 */
595                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
596                 if (dist < 8) {
597                         return;
598                 }
599                 _first_move = true;
600         }
601
602         if (!_down_view) {
603                 return;
604         }
605         
606         Time new_position = _down_view_position + (p.x - _down_point.x) / pptu;
607         
608         if (_snap) {
609                 
610                 bool first = true;
611                 Time nearest_distance = TIME_MAX;
612                 Time nearest_new_position = TIME_MAX;
613                 
614                 /* Find the nearest content edge; this is inefficient */
615                 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
616                         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
617                         if (!cv || cv == _down_view) {
618                                 continue;
619                         }
620                         
621                         {
622                                 /* Snap starts to ends */
623                                 Time const d = abs (cv->content()->end() - new_position);
624                                 if (first || d < nearest_distance) {
625                                         nearest_distance = d;
626                                         nearest_new_position = cv->content()->end();
627                                 }
628                         }
629                         
630                         {
631                                 /* Snap ends to starts */
632                                 Time const d = abs (cv->content()->position() - (new_position + _down_view->content()->length_after_trim()));
633                                 if (d < nearest_distance) {
634                                         nearest_distance = d;
635                                         nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
636                                 }
637                         }
638                         
639                         first = false;
640                 }
641                 
642                 if (!first) {
643                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
644                         if (nearest_distance < (width() / pptu) / 32) {
645                                 new_position = nearest_new_position;
646                         }
647                 }
648         }
649         
650         if (new_position < 0) {
651                 new_position = 0;
652         }
653         
654         _down_view->content()->set_position (new_position);
655         
656         shared_ptr<Film> film = _film.lock ();
657         assert (film);
658         film->set_sequence_video (false);
659 }
660
661 void
662 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
663 {
664         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
665 }
666
667 shared_ptr<const Film>
668 Timeline::film () const
669 {
670         return _film.lock ();
671 }
672
673 void
674 Timeline::resized ()
675 {
676         setup_pixels_per_time_unit ();
677 }
678
679 void
680 Timeline::clear_selection ()
681 {
682         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
683                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
684                 if (cv) {
685                         cv->set_selected (false);
686                 }
687         }
688 }
689
690 Timeline::ContentViewList
691 Timeline::selected_views () const
692 {
693         ContentViewList sel;
694         
695         for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
696                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
697                 if (cv && cv->selected()) {
698                         sel.push_back (cv);
699                 }
700         }
701
702         return sel;
703 }
704
705 ContentList
706 Timeline::selected_content () const
707 {
708         ContentList sel;
709         ContentViewList views = selected_views ();
710         
711         for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
712                 sel.push_back ((*i)->content ());
713         }
714
715         return sel;
716 }