Support for keeping video in sequence when changing lengths; tie selection in timelin...
[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 Rect 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         Rect _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         Rect 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 Rect ();
92                 }
93                 
94                 return Rect (
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 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
144                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
145                 if (_selected) {
146                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
147                 } else {
148                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
149                 }
150 #else                   
151                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
152                 if (_selected) {
153                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
154                 } else {
155                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
156                 }
157 #endif
158                 
159                 wxGraphicsPath path = gc->CreatePath ();
160                 path.MoveToPoint    (time_x (start),       y_pos (_track) + 4);
161                 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
162                 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
163                 path.AddLineToPoint (time_x (start),       y_pos (_track + 1) - 4);
164                 path.AddLineToPoint (time_x (start),       y_pos (_track) + 4);
165                 gc->StrokePath (path);
166                 gc->FillPath (path);
167
168                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).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 (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
176                 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 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)
186         {
187                 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
188                         force_redraw ();
189                 }
190         }
191
192         boost::weak_ptr<Content> _content;
193         int _track;
194         bool _selected;
195
196         boost::signals2::scoped_connection _content_connection;
197 };
198
199 class AudioContentView : public ContentView
200 {
201 public:
202         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
203                 : ContentView (tl, c, t)
204         {}
205         
206 private:
207         wxString type () const
208         {
209                 return _("audio");
210         }
211
212         wxColour colour () const
213         {
214                 return wxColour (149, 121, 232, 255);
215         }
216 };
217
218 class VideoContentView : public ContentView
219 {
220 public:
221         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
222                 : ContentView (tl, c, t)
223         {}
224
225 private:        
226
227         wxString type () const
228         {
229                 return _("video");
230         }
231
232         wxColour colour () const
233         {
234                 return wxColour (242, 92, 120, 255);
235         }
236 };
237
238 class TimeAxisView : public View
239 {
240 public:
241         TimeAxisView (Timeline& tl, int y)
242                 : View (tl)
243                 , _y (y)
244         {}
245
246         void do_paint (wxGraphicsContext* gc)
247         {
248 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
249                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
250 #else               
251                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
252 #endif              
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         Rect bbox () const
308         {
309                 return Rect (0, _y - 4, _timeline.width(), 24);
310         }
311
312 private:
313         int _y;
314 };
315
316 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
317         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
318         , _film_editor (ed)
319         , _film (film)
320         , _tracks (0)
321         , _pixels_per_time_unit (0)
322         , _left_down (false)
323         , _down_view_start (0)
324         , _first_move (false)
325 {
326         SetDoubleBuffered (true);
327
328         setup_pixels_per_time_unit ();
329         
330         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
331         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
332         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
333         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
334         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
335
336         playlist_changed ();
337
338         SetMinSize (wxSize (640, tracks() * track_height() + 96));
339
340         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
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         
385         _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
386                 
387         Refresh ();
388 }
389
390 void
391 Timeline::assign_tracks ()
392 {
393         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
394                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
395                 if (cv) {
396                         cv->set_track (0);
397                 }
398         }
399
400         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
401                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
402                 if (!acv) {
403                         continue;
404                 }
405         
406                 shared_ptr<Content> acv_content = acv->content().lock ();
407                 assert (acv_content);
408                 
409                 int t = 1;
410                 while (1) {
411                         list<shared_ptr<View> >::iterator j = _views.begin();
412                         while (j != _views.end()) {
413                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
414                                 if (!test) {
415                                         ++j;
416                                         continue;
417                                 }
418                                 
419                                 shared_ptr<Content> test_content = test->content().lock ();
420                                 assert (test_content);
421                                         
422                                 if (test && test->track() == t) {
423                                         if ((acv_content->start() <= test_content->start() && test_content->start() <= acv_content->end()) ||
424                                             (acv_content->start() <= test_content->end()   && test_content->end()   <= acv_content->end())) {
425                                                 /* we have an overlap on track `t' */
426                                                 ++t;
427                                                 break;
428                                         }
429                                 }
430                                 
431                                 ++j;
432                         }
433
434                         if (j == _views.end ()) {
435                                 /* no overlap on `t' */
436                                 break;
437                         }
438                 }
439
440                 acv->set_track (t);
441                 _tracks = max (_tracks, t + 1);
442         }
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 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
498 void
499 Timeline::left_up (wxMouseEvent &)
500 {
501         _left_down = false;
502 }
503
504 void
505 Timeline::mouse_moved (wxMouseEvent& ev)
506 {
507         if (!_left_down) {
508                 return;
509         }
510
511         wxPoint const p = ev.GetPosition();
512
513         if (!_first_move) {
514                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
515                 if (dist < 8) {
516                         return;
517                 }
518                 _first_move = true;
519         }
520
521         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
522         if (_down_view) {
523                 shared_ptr<Content> c = _down_view->content().lock();
524                 if (c) {
525                         c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
526
527                         shared_ptr<Film> film = _film.lock ();
528                         assert (film);
529                         film->set_sequence_video (false);
530                 }
531         }
532 }
533
534 void
535 Timeline::force_redraw (Rect const & r)
536 {
537         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
538 }
539
540 shared_ptr<const Film>
541 Timeline::film () const
542 {
543         return _film.lock ();
544 }
545
546 void
547 Timeline::resized (wxSizeEvent &)
548 {
549         setup_pixels_per_time_unit ();
550 }