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