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