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