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