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