54f3d75cf7f3143ee80ac18efee98ea0a16ab7ec
[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 "timeline.h"
25 #include "wx_util.h"
26 #include "playlist.h"
27
28 using std::list;
29 using std::cout;
30 using std::max;
31 using boost::shared_ptr;
32 using boost::weak_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::bind;
35
36 class View
37 {
38 public:
39         View (Timeline& t)
40                 : _timeline (t)
41         {
42
43         }
44                 
45         void paint (wxGraphicsContext* g)
46         {
47                 _last_paint_bbox = bbox ();
48                 do_paint (g);
49         }
50         
51         void force_redraw ()
52         {
53                 _timeline.force_redraw (_last_paint_bbox);
54                 _timeline.force_redraw (bbox ());
55         }
56
57         virtual Rect bbox () const = 0;
58
59 protected:
60         virtual void do_paint (wxGraphicsContext *) = 0;
61         
62         int time_x (Time t) const
63         {
64                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
65         }
66         
67         Timeline& _timeline;
68
69 private:
70         Rect _last_paint_bbox;
71 };
72
73 class ContentView : public View
74 {
75 public:
76         ContentView (Timeline& tl, shared_ptr<Content> c, int t)
77                 : View (tl)
78                 , _content (c)
79                 , _track (t)
80                 , _selected (false)
81         {
82                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2));
83         }
84
85         Rect bbox () const
86         {
87                 shared_ptr<const Film> film = _timeline.film ();
88                 shared_ptr<const Content> content = _content.lock ();
89                 if (!film || !content) {
90                         return Rect ();
91                 }
92                 
93                 return Rect (
94                         time_x (content->start ()) - 8,
95                         y_pos (_track) - 8,
96                         content->length () * _timeline.pixels_per_time_unit() + 16,
97                         _timeline.track_height() + 16
98                         );
99         }
100
101         void set_selected (bool s) {
102                 _selected = s;
103                 force_redraw ();
104         }
105         
106         bool selected () const {
107                 return _selected;
108         }
109
110         weak_ptr<Content> content () const {
111                 return _content;
112         }
113
114         void set_track (int t) {
115                 _track = t;
116         }
117
118         int track () const {
119                 return _track;
120         }
121
122         virtual wxString type () const = 0;
123         virtual wxColour colour () const = 0;
124         
125 private:
126
127         void do_paint (wxGraphicsContext* gc)
128         {
129                 shared_ptr<const Film> film = _timeline.film ();
130                 shared_ptr<const Content> content = _content.lock ();
131                 if (!film || !content) {
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 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
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 #else                   
150                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
151                 if (_selected) {
152                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
153                 } else {
154                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
155                 }
156 #endif
157                 
158                 wxGraphicsPath path = gc->CreatePath ();
159                 path.MoveToPoint    (time_x (start),       y_pos (_track) + 4);
160                 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
161                 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
162                 path.AddLineToPoint (time_x (start),       y_pos (_track + 1) - 4);
163                 path.AddLineToPoint (time_x (start),       y_pos (_track) + 4);
164                 gc->StrokePath (path);
165                 gc->FillPath (path);
166
167                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
168                 wxDouble name_width;
169                 wxDouble name_height;
170                 wxDouble name_descent;
171                 wxDouble name_leading;
172                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
173                 
174                 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
175                 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
176                 gc->ResetClip ();
177         }
178
179         int y_pos (int t) const
180         {
181                 return _timeline.tracks_position().y + t * _timeline.track_height();
182         }
183
184         void content_changed (int p)
185         {
186                 if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
187                         force_redraw ();
188                 }
189         }
190
191         boost::weak_ptr<Content> _content;
192         int _track;
193         bool _selected;
194
195         boost::signals2::scoped_connection _content_connection;
196 };
197
198 class AudioContentView : public ContentView
199 {
200 public:
201         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
202                 : ContentView (tl, c, t)
203         {}
204         
205 private:
206         wxString type () const
207         {
208                 return _("audio");
209         }
210
211         wxColour colour () const
212         {
213                 return wxColour (149, 121, 232, 255);
214         }
215 };
216
217 class VideoContentView : public ContentView
218 {
219 public:
220         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
221                 : ContentView (tl, c, t)
222         {}
223
224 private:        
225
226         wxString type () const
227         {
228                 return _("video");
229         }
230
231         wxColour colour () const
232         {
233                 return wxColour (242, 92, 120, 255);
234         }
235 };
236
237 class TimeAxisView : public View
238 {
239 public:
240         TimeAxisView (Timeline& tl, int y)
241                 : View (tl)
242                 , _y (y)
243         {}
244
245         void do_paint (wxGraphicsContext* gc)
246         {
247 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
248                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
249 #else               
250                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
251 #endif              
252                 
253                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
254                 if (mark_interval > 5) {
255                         mark_interval -= mark_interval % 5;
256                 }
257                 if (mark_interval > 10) {
258                         mark_interval -= mark_interval % 10;
259                 }
260                 if (mark_interval > 60) {
261                         mark_interval -= mark_interval % 60;
262                 }
263                 if (mark_interval > 3600) {
264                         mark_interval -= mark_interval % 3600;
265                 }
266                 
267                 if (mark_interval < 1) {
268                         mark_interval = 1;
269                 }
270
271                 wxGraphicsPath path = gc->CreatePath ();
272                 path.MoveToPoint (_timeline.x_offset(), _y);
273                 path.AddLineToPoint (_timeline.width(), _y);
274                 gc->StrokePath (path);
275
276                 Time t = 0;
277                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
278                         wxGraphicsPath path = gc->CreatePath ();
279                         path.MoveToPoint (time_x (t), _y - 4);
280                         path.AddLineToPoint (time_x (t), _y + 4);
281                         gc->StrokePath (path);
282
283                         int tc = t / TIME_HZ;
284                         int const h = tc / 3600;
285                         tc -= h * 3600;
286                         int const m = tc / 60;
287                         tc -= m * 60;
288                         int const s = tc;
289                         
290                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
291                         wxDouble str_width;
292                         wxDouble str_height;
293                         wxDouble str_descent;
294                         wxDouble str_leading;
295                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
296                         
297                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
298                         if ((tx + str_width) < _timeline.width()) {
299                                 gc->DrawText (str, time_x (t), _y + 16);
300                         }
301                         
302                         t += mark_interval * TIME_HZ;
303                 }
304         }
305
306         Rect bbox () const
307         {
308                 return Rect (0, _y - 4, _timeline.width(), 24);
309         }
310
311 private:
312         int _y;
313 };
314
315 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
316         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
317         , _film (film)
318         , _tracks (0)
319         , _pixels_per_time_unit (0)
320         , _left_down (false)
321         , _down_view_start (0)
322         , _first_move (false)
323 {
324         SetDoubleBuffered (true);
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
341 void
342 Timeline::paint (wxPaintEvent &)
343 {
344         wxPaintDC dc (this);
345
346         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
347         if (!gc) {
348                 return;
349         }
350
351         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
352
353         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
354                 (*i)->paint (gc);
355         }
356
357         delete gc;
358 }
359
360 void
361 Timeline::playlist_changed ()
362 {
363         shared_ptr<const Film> fl = _film.lock ();
364         if (!fl) {
365                 return;
366         }
367
368         _views.clear ();
369
370         Playlist::ContentList content = fl->playlist()->content ();
371
372         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
373                 if (dynamic_pointer_cast<VideoContent> (*i)) {
374                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
375                 }
376                 if (dynamic_pointer_cast<AudioContent> (*i)) {
377                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
378                 }
379         }
380
381         assign_tracks ();
382         
383         _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
384                 
385         Refresh ();
386 }
387
388 void
389 Timeline::assign_tracks ()
390 {
391         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
392                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
393                 if (cv) {
394                         cv->set_track (0);
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
443 int
444 Timeline::tracks () const
445 {
446         return _tracks;
447 }
448
449 void
450 Timeline::setup_pixels_per_time_unit ()
451 {
452         shared_ptr<const Film> film = _film.lock ();
453         if (!film) {
454                 return;
455         }
456
457         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
458 }
459
460 void
461 Timeline::left_down (wxMouseEvent& ev)
462 {
463         list<shared_ptr<View> >::iterator i = _views.begin();
464         Position const p (ev.GetX(), ev.GetY());
465         while (i != _views.end() && !(*i)->bbox().contains (p)) {
466                 ++i;
467         }
468
469         _down_view.reset ();
470
471         if (i != _views.end ()) {
472                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
473                 if (cv) {
474                         _down_view = cv;
475                         shared_ptr<Content> c = cv->content().lock();
476                         assert (c);
477                         _down_view_start = c->start ();
478                 }
479         }
480
481         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
482                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
483                 if (cv) {
484                         cv->set_selected (i == j);
485                 }
486         }
487
488         _left_down = true;
489         _down_point = ev.GetPosition ();
490         _first_move = false;
491 }
492
493 void
494 Timeline::left_up (wxMouseEvent &)
495 {
496         _left_down = false;
497 }
498
499 void
500 Timeline::mouse_moved (wxMouseEvent& ev)
501 {
502         if (!_left_down) {
503                 return;
504         }
505
506         wxPoint const p = ev.GetPosition();
507
508         if (!_first_move) {
509                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
510                 if (dist < 8) {
511                         return;
512                 }
513                 _first_move = true;
514         }
515
516         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
517         if (_down_view) {
518                 shared_ptr<Content> c = _down_view->content().lock();
519                 if (c) {
520                         c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
521                 }
522         }
523 }
524
525 void
526 Timeline::force_redraw (Rect const & r)
527 {
528         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
529 }
530
531 shared_ptr<const Film>
532 Timeline::film () const
533 {
534         return _film.lock ();
535 }
536
537 void
538 Timeline::resized (wxSizeEvent &)
539 {
540         setup_pixels_per_time_unit ();
541 }