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