Odd const fix.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2016 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 <boost/make_shared.hpp>
44 #include <list>
45 #include <iostream>
46
47 using std::list;
48 using std::cout;
49 using std::max;
50 using boost::shared_ptr;
51 using boost::make_shared;
52 using boost::weak_ptr;
53 using boost::dynamic_pointer_cast;
54 using boost::bind;
55 using boost::optional;
56
57 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
58         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
59         , _content_panel (cp)
60         , _film (film)
61         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
62         , _reels_view (new TimelineReelsView (*this, 32))
63         , _labels_view (new TimelineLabelsView (*this))
64         , _tracks (0)
65         , _left_down (false)
66         , _down_view_position (0)
67         , _first_move (false)
68         , _menu (this)
69         , _snap (true)
70 {
71 #ifndef __WXOSX__
72         SetDoubleBuffered (true);
73 #endif
74
75         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
76         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
77         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
78         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
79         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
80         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
81
82         film_changed (Film::CONTENT);
83
84         SetMinSize (wxSize (640, tracks() * track_height() + 96));
85
86         _tracks_position = Position<int> (_labels_view->bbox().width, 32);
87
88         _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
89         _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
90 }
91
92 void
93 Timeline::paint ()
94 {
95         wxPaintDC dc (this);
96
97         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
98         if (!gc) {
99                 return;
100         }
101
102         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
103
104         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
105
106                 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
107
108                 /* Find areas of overlap */
109                 list<dcpomatic::Rect<int> > overlaps;
110                 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
111                         shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
112                         /* No overlap with non-content views, views no different tracks, audio views or non-active views */
113                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
114                                 continue;
115                         }
116
117                         optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
118                         if (r) {
119                                 overlaps.push_back (r.get ());
120                         }
121                 }
122
123                 i->paint (gc, overlaps);
124         }
125
126         delete gc;
127 }
128
129 void
130 Timeline::film_changed (Film::Property p)
131 {
132         if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
133                 ensure_ui_thread ();
134                 recreate_views ();
135         } else if (p == Film::CONTENT_ORDER) {
136                 Refresh ();
137         }
138 }
139
140 void
141 Timeline::recreate_views ()
142 {
143         shared_ptr<const Film> film = _film.lock ();
144         if (!film) {
145                 return;
146         }
147
148         _views.clear ();
149         _views.push_back (_time_axis_view);
150         _views.push_back (_reels_view);
151         _views.push_back (_labels_view);
152
153         /* XXX: make_shared does not work here on some compilers due to some strange const problem */
154
155         BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
156                 if (i->video) {
157                         _views.push_back (shared_ptr<TimelineContentView> (new TimelineVideoContentView (*this, i)));
158                 }
159
160                 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
161                         _views.push_back (shared_ptr<TimelineContentView> (new TimelineAudioContentView (*this, i)));
162                 }
163
164                 if (i->subtitle) {
165                         _views.push_back (shared_ptr<TimelineContentView> (new TimelineSubtitleContentView (*this, i)));
166                 }
167
168                 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
169                         _views.push_back (shared_ptr<TimelineContentView> (new TimelineAtmosContentView (*this, i)));
170                 }
171         }
172
173         assign_tracks ();
174         setup_pixels_per_second ();
175         Refresh ();
176 }
177
178 void
179 Timeline::film_content_changed (int property, bool frequent)
180 {
181         ensure_ui_thread ();
182
183         if (property == AudioContentProperty::STREAMS) {
184                 recreate_views ();
185         } else if (!frequent) {
186                 setup_pixels_per_second ();
187                 Refresh ();
188         }
189 }
190
191 void
192 Timeline::assign_tracks ()
193 {
194         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
195                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
196                 if (c) {
197                         c->unset_track ();
198                 }
199         }
200
201         /* See if we have any subtitle / atmos / right-eye views */
202         bool have_3d = false;
203         bool have_subtitle = false;
204         bool have_atmos = false;
205         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
206                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
207                 if (!cv) {
208                         continue;
209                 }
210
211                 if (dynamic_pointer_cast<TimelineVideoContentView> (i)) {
212                         if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
213                                 have_3d = true;
214                         }
215                 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (i)) {
216                         have_subtitle = true;
217                 } else if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
218                         have_atmos = true;
219                 }
220         }
221
222         _labels_view->set_3d (have_3d);
223         _labels_view->set_subtitle (have_subtitle);
224         _labels_view->set_atmos (have_atmos);
225
226         /* Hence decide where to start subtitle, atmos and audio tracks */
227         int const subtitle = have_3d ? 2 : 1;
228         int const atmos = have_subtitle ? subtitle + 1 : subtitle;
229         int const audio = have_atmos ? atmos + 1: atmos;
230
231         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
232                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
233                 if (!cv) {
234                         continue;
235                 }
236
237                 if (dynamic_pointer_cast<TimelineVideoContentView> (*i)) {
238                         /* Video on tracks 0 and maybe 1 (left and right eye) */
239                         cv->set_track (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT ? 1 : 0);
240                         _tracks = max (_tracks, have_3d ? 2 : 1);
241                         continue;
242                 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (*i)) {
243                         cv->set_track (subtitle);
244                         _tracks = max (_tracks, subtitle + 1);
245                         continue;
246                 } else if (dynamic_pointer_cast<TimelineAtmosContentView> (*i)) {
247                         cv->set_track (atmos);
248                         _tracks = max (_tracks, atmos + 1);
249                         continue;
250                 }
251
252                 int t = audio;
253
254                 shared_ptr<Content> content = cv->content();
255                 DCPTimePeriod content_period (content->position(), content->end());
256
257                 while (true) {
258                         TimelineViewList::iterator j = _views.begin();
259                         while (j != _views.end()) {
260                                 shared_ptr<TimelineAudioContentView> test = dynamic_pointer_cast<TimelineAudioContentView> (*j);
261                                 if (!test) {
262                                         ++j;
263                                         continue;
264                                 }
265
266                                 shared_ptr<Content> test_content = test->content();
267
268                                 if (test && test->track() && test->track().get() == t) {
269                                         if (content_period.overlaps (DCPTimePeriod(test_content->position(), test_content->end()))) {
270                                                 /* we have an overlap on track `t' */
271                                                 ++t;
272                                                 break;
273                                         }
274                                 }
275
276                                 ++j;
277                         }
278
279                         if (j == _views.end ()) {
280                                 /* no overlap on `t' */
281                                 break;
282                         }
283                 }
284
285                 cv->set_track (t);
286                 _tracks = max (_tracks, t + 1);
287         }
288
289         _time_axis_view->set_y (tracks() * track_height() + 64);
290         _reels_view->set_y (8);
291 }
292
293 int
294 Timeline::tracks () const
295 {
296         return _tracks;
297 }
298
299 void
300 Timeline::setup_pixels_per_second ()
301 {
302         shared_ptr<const Film> film = _film.lock ();
303         if (!film || film->length() == DCPTime ()) {
304                 return;
305         }
306
307         _pixels_per_second = static_cast<double>(width() - tracks_position().x * 2) / film->length().seconds ();
308 }
309
310 shared_ptr<TimelineView>
311 Timeline::event_to_view (wxMouseEvent& ev)
312 {
313         /* Search backwards through views so that we find the uppermost one first */
314         TimelineViewList::reverse_iterator i = _views.rbegin();
315         Position<int> const p (ev.GetX(), ev.GetY());
316         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
317                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
318                 ++i;
319         }
320
321         if (i == _views.rend ()) {
322                 return shared_ptr<TimelineView> ();
323         }
324
325         return *i;
326 }
327
328 void
329 Timeline::left_down (wxMouseEvent& ev)
330 {
331         shared_ptr<TimelineView> view = event_to_view (ev);
332         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
333
334         _down_view.reset ();
335
336         if (content_view) {
337                 _down_view = content_view;
338                 _down_view_position = content_view->content()->position ();
339         }
340
341         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
342                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
343                 if (!cv) {
344                         continue;
345                 }
346
347                 if (!ev.ShiftDown ()) {
348                         cv->set_selected (view == *i);
349                 }
350         }
351
352         if (content_view && ev.ShiftDown ()) {
353                 content_view->set_selected (!content_view->selected ());
354         }
355
356         _left_down = true;
357         _down_point = ev.GetPosition ();
358         _first_move = false;
359
360         if (_down_view) {
361                 /* Pre-compute the points that we might snap to */
362                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
363                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
364                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
365                                 continue;
366                         }
367
368                         _start_snaps.push_back (cv->content()->position());
369                         _end_snaps.push_back (cv->content()->position());
370                         _start_snaps.push_back (cv->content()->end());
371                         _end_snaps.push_back (cv->content()->end());
372
373                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
374                                 _start_snaps.push_back (i);
375                         }
376                 }
377
378                 /* Tell everyone that things might change frequently during the drag */
379                 _down_view->content()->set_change_signals_frequent (true);
380         }
381 }
382
383 void
384 Timeline::left_up (wxMouseEvent& ev)
385 {
386         _left_down = false;
387
388         if (_down_view) {
389                 _down_view->content()->set_change_signals_frequent (false);
390                 _content_panel->set_selection (_down_view->content ());
391         }
392
393         set_position_from_event (ev);
394
395         /* Clear up up the stuff we don't do during drag */
396         assign_tracks ();
397         setup_pixels_per_second ();
398         Refresh ();
399
400         _start_snaps.clear ();
401         _end_snaps.clear ();
402 }
403
404 void
405 Timeline::mouse_moved (wxMouseEvent& ev)
406 {
407         if (!_left_down) {
408                 return;
409         }
410
411         set_position_from_event (ev);
412 }
413
414 void
415 Timeline::right_down (wxMouseEvent& ev)
416 {
417         shared_ptr<TimelineView> view = event_to_view (ev);
418         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
419         if (!cv) {
420                 return;
421         }
422
423         if (!cv->selected ()) {
424                 clear_selection ();
425                 cv->set_selected (true);
426         }
427
428         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
429 }
430
431 void
432 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
433 {
434         DCPTime const d = a - b;
435         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
436                 nearest_distance = d;
437         }
438 }
439
440 void
441 Timeline::set_position_from_event (wxMouseEvent& ev)
442 {
443         if (!_pixels_per_second) {
444                 return;
445         }
446
447         double const pps = _pixels_per_second.get ();
448
449         wxPoint const p = ev.GetPosition();
450
451         if (!_first_move) {
452                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
453                    before the drag is considered to have started.
454                 */
455                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
456                 if (dist < 8) {
457                         return;
458                 }
459                 _first_move = true;
460         }
461
462         if (!_down_view) {
463                 return;
464         }
465
466         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
467
468         if (_snap) {
469
470                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
471                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
472                    positive is right).
473                 */
474                 optional<DCPTime> nearest_distance;
475
476                 /* Find the nearest snap point */
477
478                 BOOST_FOREACH (DCPTime i, _start_snaps) {
479                         maybe_snap (i, new_position, nearest_distance);
480                 }
481
482                 BOOST_FOREACH (DCPTime i, _end_snaps) {
483                         maybe_snap (i, new_end, nearest_distance);
484                 }
485
486                 if (nearest_distance) {
487                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
488                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
489                                 new_position += nearest_distance.get ();
490                         }
491                 }
492         }
493
494         if (new_position < DCPTime ()) {
495                 new_position = DCPTime ();
496         }
497
498         _down_view->content()->set_position (new_position);
499
500         shared_ptr<Film> film = _film.lock ();
501         DCPOMATIC_ASSERT (film);
502         film->set_sequence (false);
503 }
504
505 void
506 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
507 {
508         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
509 }
510
511 shared_ptr<const Film>
512 Timeline::film () const
513 {
514         return _film.lock ();
515 }
516
517 void
518 Timeline::resized ()
519 {
520         setup_pixels_per_second ();
521 }
522
523 void
524 Timeline::clear_selection ()
525 {
526         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
527                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
528                 if (cv) {
529                         cv->set_selected (false);
530                 }
531         }
532 }
533
534 TimelineContentViewList
535 Timeline::selected_views () const
536 {
537         TimelineContentViewList sel;
538
539         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
540                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
541                 if (cv && cv->selected()) {
542                         sel.push_back (cv);
543                 }
544         }
545
546         return sel;
547 }
548
549 ContentList
550 Timeline::selected_content () const
551 {
552         ContentList sel;
553         TimelineContentViewList views = selected_views ();
554
555         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
556                 sel.push_back ((*i)->content ());
557         }
558
559         return sel;
560 }
561
562 void
563 Timeline::set_selection (ContentList selection)
564 {
565         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
566                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
567                 if (cv) {
568                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
569                 }
570         }
571 }