Basic zoom.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "film_editor.h"
22 #include "timeline.h"
23 #include "timeline_time_axis_view.h"
24 #include "timeline_reels_view.h"
25 #include "timeline_labels_view.h"
26 #include "timeline_video_content_view.h"
27 #include "timeline_audio_content_view.h"
28 #include "timeline_subtitle_content_view.h"
29 #include "timeline_atmos_content_view.h"
30 #include "content_panel.h"
31 #include "wx_util.h"
32 #include "lib/film.h"
33 #include "lib/playlist.h"
34 #include "lib/image_content.h"
35 #include "lib/timer.h"
36 #include "lib/audio_content.h"
37 #include "lib/subtitle_content.h"
38 #include "lib/video_content.h"
39 #include "lib/atmos_mxf_content.h"
40 #include <wx/graphics.h>
41 #include <boost/weak_ptr.hpp>
42 #include <boost/foreach.hpp>
43 #include <list>
44 #include <iostream>
45
46 using std::list;
47 using std::cout;
48 using std::min;
49 using std::max;
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
53 using boost::bind;
54 using boost::optional;
55
56 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
57         : wxScrolledCanvas (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
58         , _content_panel (cp)
59         , _film (film)
60         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
61         , _reels_view (new TimelineReelsView (*this, 32))
62         , _labels_view (new TimelineLabelsView (*this))
63         , _tracks (0)
64         , _left_down (false)
65         , _down_view_position (0)
66         , _first_move (false)
67         , _menu (this)
68         , _snap (true)
69         , _tool (SELECT)
70         , _x_scroll_rate (16)
71         , _y_scroll_rate (16)
72 {
73 #ifndef __WXOSX__
74         SetDoubleBuffered (true);
75 #endif
76
77         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
78         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
79         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
80         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
81         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
82         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
83
84         film_changed (Film::CONTENT);
85
86         SetMinSize (wxSize (640, 4 * track_height() + 96));
87
88         _tracks_position = Position<int> (_labels_view->bbox().width, 32);
89
90         _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
91         _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
92
93         _pixels_per_second = max (0.01, static_cast<double>(640 - tracks_position().x * 2) / film->length().seconds ());
94
95         setup_scrollbars ();
96         EnableScrolling (true, true);
97 }
98
99 void
100 Timeline::paint ()
101 {
102         wxPaintDC dc (this);
103         DoPrepareDC (dc);
104
105         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
106         if (!gc) {
107                 return;
108         }
109
110         int vsx, vsy;
111         GetViewStart (&vsx, &vsy);
112         gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
113
114         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
115
116         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
117
118                 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
119
120                 /* Find areas of overlap with other content views, so that we can plot them */
121                 list<dcpomatic::Rect<int> > overlaps;
122                 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
123                         shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
124                         /* No overlap with non-content views, views no different tracks, audio views or non-active views */
125                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
126                                 continue;
127                         }
128
129                         optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
130                         if (r) {
131                                 overlaps.push_back (r.get ());
132                         }
133                 }
134
135                 i->paint (gc, overlaps);
136         }
137
138         if (_zoom_point) {
139                 /* Translate back as _down_point and _zoom_point do not take scroll into account */
140                 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
141                 gc->SetPen (*wxBLACK_PEN);
142                 gc->SetBrush (*wxTRANSPARENT_BRUSH);
143                 gc->DrawRectangle (
144                         min (_down_point.x, _zoom_point->x),
145                         min (_down_point.y, _zoom_point->y),
146                         fabs (_down_point.x - _zoom_point->x),
147                         fabs (_down_point.y - _zoom_point->y)
148                         );
149         }
150
151         delete gc;
152 }
153
154 void
155 Timeline::film_changed (Film::Property p)
156 {
157         if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
158                 ensure_ui_thread ();
159                 recreate_views ();
160         } else if (p == Film::CONTENT_ORDER) {
161                 Refresh ();
162         }
163 }
164
165 void
166 Timeline::recreate_views ()
167 {
168         shared_ptr<const Film> film = _film.lock ();
169         if (!film) {
170                 return;
171         }
172
173         _views.clear ();
174         _views.push_back (_time_axis_view);
175         _views.push_back (_reels_view);
176         _views.push_back (_labels_view);
177
178         BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
179                 if (i->video) {
180                         _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
181                 }
182
183                 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
184                         _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
185                 }
186
187                 if (i->subtitle) {
188                         _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
189                 }
190
191                 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
192                         _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
193                 }
194         }
195
196         assign_tracks ();
197         setup_scrollbars ();
198         Refresh ();
199 }
200
201 void
202 Timeline::film_content_changed (int property, bool frequent)
203 {
204         ensure_ui_thread ();
205
206         if (property == AudioContentProperty::STREAMS) {
207                 recreate_views ();
208         } else if (!frequent) {
209                 setup_scrollbars ();
210                 Refresh ();
211         }
212 }
213
214 template <class T>
215 int
216 place (TimelineViewList& views, int& tracks)
217 {
218         int const base = tracks;
219
220         BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
221                 if (!dynamic_pointer_cast<T>(i)) {
222                         continue;
223                 }
224
225                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
226
227                 int t = base;
228
229                 shared_ptr<Content> content = cv->content();
230                 DCPTimePeriod const content_period (content->position(), content->end());
231
232                 while (true) {
233                         TimelineViewList::iterator j = views.begin();
234                         while (j != views.end()) {
235                                 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
236                                 if (!test) {
237                                         ++j;
238                                         continue;
239                                 }
240
241                                 shared_ptr<Content> test_content = test->content();
242                                 if (
243                                         test->track() && test->track().get() == t &&
244                                         content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
245                                         /* we have an overlap on track `t' */
246                                         ++t;
247                                         break;
248                                 }
249
250                                 ++j;
251                         }
252
253                         if (j == views.end ()) {
254                                 /* no overlap on `t' */
255                                 break;
256                         }
257                 }
258
259                 cv->set_track (t);
260                 tracks = max (tracks, t + 1);
261         }
262
263         return tracks - base;
264 }
265
266 void
267 Timeline::assign_tracks ()
268 {
269         /* Tracks are:
270            Video (mono or left-eye)
271            Video (right-eye)
272            Subtitle 1
273            Subtitle 2
274            Subtitle N
275            Atmos
276            Audio 1
277            Audio 2
278            Audio N
279         */
280
281         _tracks = 0;
282
283         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
284                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
285                 if (c) {
286                         c->unset_track ();
287                 }
288         }
289
290         /* Video */
291
292         bool have_3d = false;
293         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
294                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
295                 if (!cv) {
296                         continue;
297                 }
298
299                 /* Video on tracks 0 and maybe 1 (left and right eye) */
300                 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
301                         cv->set_track (1);
302                         _tracks = max (_tracks, 2);
303                         have_3d = true;
304                 } else {
305                         cv->set_track (0);
306                 }
307         }
308
309         _tracks = max (_tracks, 1);
310
311         /* Subtitle */
312
313         int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
314
315         /* Atmos */
316
317         bool have_atmos = false;
318         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
319                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
320                 if (!cv) {
321                         continue;
322                 }
323                 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
324                         cv->set_track (_tracks - 1);
325                         have_atmos = true;
326                 }
327         }
328
329         if (have_atmos) {
330                 ++_tracks;
331         }
332
333         /* Audio */
334
335         place<TimelineAudioContentView> (_views, _tracks);
336
337         _labels_view->set_3d (have_3d);
338         _labels_view->set_subtitle_tracks (subtitle_tracks);
339         _labels_view->set_atmos (have_atmos);
340
341         _time_axis_view->set_y (tracks() * track_height() + 64);
342         _reels_view->set_y (8);
343 }
344
345 int
346 Timeline::tracks () const
347 {
348         return _tracks;
349 }
350
351 void
352 Timeline::setup_scrollbars ()
353 {
354         shared_ptr<const Film> film = _film.lock ();
355         if (!film || !_pixels_per_second) {
356                 return;
357         }
358         SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
359         SetScrollRate (_x_scroll_rate, _y_scroll_rate);
360 }
361
362 shared_ptr<TimelineView>
363 Timeline::event_to_view (wxMouseEvent& ev)
364 {
365         /* Search backwards through views so that we find the uppermost one first */
366         TimelineViewList::reverse_iterator i = _views.rbegin();
367         Position<int> const p (ev.GetX(), ev.GetY());
368         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
369                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
370                 ++i;
371         }
372
373         if (i == _views.rend ()) {
374                 return shared_ptr<TimelineView> ();
375         }
376
377         return *i;
378 }
379
380 void
381 Timeline::left_down (wxMouseEvent& ev)
382 {
383         _left_down = true;
384         _down_point = ev.GetPosition ();
385
386         switch (_tool) {
387         case SELECT:
388                 left_down_select (ev);
389                 break;
390         case ZOOM:
391                 /* Nothing to do */
392                 break;
393         }
394 }
395
396 void
397 Timeline::left_down_select (wxMouseEvent& ev)
398 {
399         shared_ptr<TimelineView> view = event_to_view (ev);
400         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
401
402         _down_view.reset ();
403
404         if (content_view) {
405                 _down_view = content_view;
406                 _down_view_position = content_view->content()->position ();
407         }
408
409         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
410                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
411                 if (!cv) {
412                         continue;
413                 }
414
415                 if (!ev.ShiftDown ()) {
416                         cv->set_selected (view == *i);
417                 }
418         }
419
420         if (content_view && ev.ShiftDown ()) {
421                 content_view->set_selected (!content_view->selected ());
422         }
423
424         _first_move = false;
425
426         if (_down_view) {
427                 /* Pre-compute the points that we might snap to */
428                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
429                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
430                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
431                                 continue;
432                         }
433
434                         _start_snaps.push_back (cv->content()->position());
435                         _end_snaps.push_back (cv->content()->position());
436                         _start_snaps.push_back (cv->content()->end());
437                         _end_snaps.push_back (cv->content()->end());
438
439                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
440                                 _start_snaps.push_back (i);
441                         }
442                 }
443
444                 /* Tell everyone that things might change frequently during the drag */
445                 _down_view->content()->set_change_signals_frequent (true);
446         }
447 }
448
449 void
450 Timeline::left_up (wxMouseEvent& ev)
451 {
452         _left_down = false;
453
454         switch (_tool) {
455         case SELECT:
456                 left_up_select (ev);
457                 break;
458         case ZOOM:
459                 left_up_zoom (ev);
460                 break;
461         }
462 }
463
464 void
465 Timeline::left_up_select (wxMouseEvent& ev)
466 {
467         if (_down_view) {
468                 _down_view->content()->set_change_signals_frequent (false);
469         }
470
471         _content_panel->set_selection (selected_content ());
472         set_position_from_event (ev);
473
474         /* Clear up up the stuff we don't do during drag */
475         assign_tracks ();
476         setup_scrollbars ();
477         Refresh ();
478
479         _start_snaps.clear ();
480         _end_snaps.clear ();
481 }
482
483 void
484 Timeline::left_up_zoom (wxMouseEvent& ev)
485 {
486         _zoom_point = ev.GetPosition ();
487
488         int vsx, vsy;
489         GetViewStart (&vsx, &vsy);
490
491         wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
492         wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
493
494         DCPTime time_left = DCPTime::from_seconds((top_left.x + vsx - _tracks_position.x) / *_pixels_per_second);
495         DCPTime time_right = DCPTime::from_seconds((bottom_right.x + vsx - _tracks_position.x) / *_pixels_per_second);
496         _pixels_per_second = GetSize().GetWidth() / (time_right.seconds() - time_left.seconds());
497         cout << "Zoom range " << to_string(time_left) << " " << to_string(time_right) << " " << *_pixels_per_second << "\n";
498         setup_scrollbars ();
499         Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, wxDefaultCoord);
500         cout << "Offset " << (time_left.seconds() * *_pixels_per_second / _x_scroll_rate) << "\n";
501
502         _zoom_point = optional<wxPoint> ();
503         Refresh ();
504 }
505
506 void
507 Timeline::mouse_moved (wxMouseEvent& ev)
508 {
509         switch (_tool) {
510         case SELECT:
511                 mouse_moved_select (ev);
512                 break;
513         case ZOOM:
514                 mouse_moved_zoom (ev);
515                 break;
516         }
517 }
518
519 void
520 Timeline::mouse_moved_select (wxMouseEvent& ev)
521 {
522         if (!_left_down) {
523                 return;
524         }
525
526         set_position_from_event (ev);
527 }
528
529 void
530 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
531 {
532         if (!_left_down) {
533                 return;
534         }
535
536         _zoom_point = ev.GetPosition ();
537         Refresh ();
538 }
539
540 void
541 Timeline::right_down (wxMouseEvent& ev)
542 {
543         switch (_tool) {
544         case SELECT:
545                 right_down_select (ev);
546                 break;
547         case ZOOM:
548                 /* Zoom out */
549                 _pixels_per_second = *_pixels_per_second / 2;
550                 setup_scrollbars ();
551                 break;
552         }
553 }
554
555 void
556 Timeline::right_down_select (wxMouseEvent& ev)
557 {
558         shared_ptr<TimelineView> view = event_to_view (ev);
559         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
560         if (!cv) {
561                 return;
562         }
563
564         if (!cv->selected ()) {
565                 clear_selection ();
566                 cv->set_selected (true);
567         }
568
569         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
570 }
571
572 void
573 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
574 {
575         DCPTime const d = a - b;
576         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
577                 nearest_distance = d;
578         }
579 }
580
581 void
582 Timeline::set_position_from_event (wxMouseEvent& ev)
583 {
584         if (!_pixels_per_second) {
585                 return;
586         }
587
588         double const pps = _pixels_per_second.get ();
589
590         wxPoint const p = ev.GetPosition();
591
592         if (!_first_move) {
593                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
594                    before the drag is considered to have started.
595                 */
596                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
597                 if (dist < 8) {
598                         return;
599                 }
600                 _first_move = true;
601         }
602
603         if (!_down_view) {
604                 return;
605         }
606
607         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
608
609         if (_snap) {
610
611                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
612                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
613                    positive is right).
614                 */
615                 optional<DCPTime> nearest_distance;
616
617                 /* Find the nearest snap point */
618
619                 BOOST_FOREACH (DCPTime i, _start_snaps) {
620                         maybe_snap (i, new_position, nearest_distance);
621                 }
622
623                 BOOST_FOREACH (DCPTime i, _end_snaps) {
624                         maybe_snap (i, new_end, nearest_distance);
625                 }
626
627                 if (nearest_distance) {
628                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
629                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
630                                 new_position += nearest_distance.get ();
631                         }
632                 }
633         }
634
635         if (new_position < DCPTime ()) {
636                 new_position = DCPTime ();
637         }
638
639         _down_view->content()->set_position (new_position);
640
641         shared_ptr<Film> film = _film.lock ();
642         DCPOMATIC_ASSERT (film);
643         film->set_sequence (false);
644 }
645
646 void
647 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
648 {
649         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
650 }
651
652 shared_ptr<const Film>
653 Timeline::film () const
654 {
655         return _film.lock ();
656 }
657
658 void
659 Timeline::resized ()
660 {
661         setup_scrollbars ();
662 }
663
664 void
665 Timeline::clear_selection ()
666 {
667         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
668                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
669                 if (cv) {
670                         cv->set_selected (false);
671                 }
672         }
673 }
674
675 TimelineContentViewList
676 Timeline::selected_views () const
677 {
678         TimelineContentViewList sel;
679
680         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
681                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
682                 if (cv && cv->selected()) {
683                         sel.push_back (cv);
684                 }
685         }
686
687         return sel;
688 }
689
690 ContentList
691 Timeline::selected_content () const
692 {
693         ContentList sel;
694         TimelineContentViewList views = selected_views ();
695
696         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
697                 sel.push_back ((*i)->content ());
698         }
699
700         return sel;
701 }
702
703 void
704 Timeline::set_selection (ContentList selection)
705 {
706         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
707                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
708                 if (cv) {
709                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
710                 }
711         }
712 }