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