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