2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "film_editor.h"
22 #include "timeline_time_axis_view.h"
23 #include "timeline_reels_view.h"
24 #include "timeline_labels_view.h"
25 #include "timeline_video_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_subtitle_content_view.h"
28 #include "timeline_atmos_content_view.h"
29 #include "content_panel.h"
32 #include "lib/playlist.h"
33 #include "lib/image_content.h"
34 #include "lib/timer.h"
35 #include "lib/audio_content.h"
36 #include "lib/subtitle_content.h"
37 #include "lib/video_content.h"
38 #include "lib/atmos_mxf_content.h"
39 #include <wx/graphics.h>
40 #include <boost/weak_ptr.hpp>
41 #include <boost/foreach.hpp>
48 using boost::shared_ptr;
49 using boost::weak_ptr;
50 using boost::dynamic_pointer_cast;
52 using boost::optional;
54 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
55 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
58 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
59 , _reels_view (new TimelineReelsView (*this, 32))
60 , _labels_view (new TimelineLabelsView (*this))
63 , _down_view_position (0)
69 SetDoubleBuffered (true);
72 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
73 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
74 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
75 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
76 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
77 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
79 film_changed (Film::CONTENT);
81 SetMinSize (wxSize (640, tracks() * track_height() + 96));
83 _tracks_position = Position<int> (_labels_view->bbox().width, 32);
85 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
86 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
94 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
99 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
101 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
103 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
105 /* Find areas of overlap */
106 list<dcpomatic::Rect<int> > overlaps;
107 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
108 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
109 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
110 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
114 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
116 overlaps.push_back (r.get ());
120 i->paint (gc, overlaps);
127 Timeline::film_changed (Film::Property p)
129 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
132 } else if (p == Film::CONTENT_ORDER) {
138 Timeline::recreate_views ()
140 shared_ptr<const Film> film = _film.lock ();
146 _views.push_back (_time_axis_view);
147 _views.push_back (_reels_view);
148 _views.push_back (_labels_view);
150 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
152 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
155 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
156 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
160 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
163 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
164 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
169 setup_pixels_per_second ();
174 Timeline::film_content_changed (int property, bool frequent)
178 if (property == AudioContentProperty::STREAMS) {
180 } else if (!frequent) {
181 setup_pixels_per_second ();
187 Timeline::assign_tracks ()
189 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
190 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
196 /* See if we have any subtitle / atmos / right-eye views */
197 bool have_3d = false;
198 bool have_subtitle = false;
199 bool have_atmos = false;
200 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
201 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
206 if (dynamic_pointer_cast<TimelineVideoContentView> (i)) {
207 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
210 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (i)) {
211 have_subtitle = true;
212 } else if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
217 _labels_view->set_3d (have_3d);
218 _labels_view->set_subtitle (have_subtitle);
219 _labels_view->set_atmos (have_atmos);
221 /* Hence decide where to start subtitle, atmos and audio tracks */
222 int const subtitle = have_3d ? 2 : 1;
223 int const atmos = have_subtitle ? subtitle + 1 : subtitle;
224 int const audio = have_atmos ? atmos + 1: atmos;
226 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
227 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
232 if (dynamic_pointer_cast<TimelineVideoContentView> (*i)) {
233 /* Video on tracks 0 and maybe 1 (left and right eye) */
234 cv->set_track (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT ? 1 : 0);
235 _tracks = max (_tracks, have_3d ? 2 : 1);
237 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (*i)) {
238 cv->set_track (subtitle);
239 _tracks = max (_tracks, subtitle + 1);
241 } else if (dynamic_pointer_cast<TimelineAtmosContentView> (*i)) {
242 cv->set_track (atmos);
243 _tracks = max (_tracks, atmos + 1);
249 shared_ptr<Content> content = cv->content();
250 DCPTimePeriod content_period (content->position(), content->end());
253 TimelineViewList::iterator j = _views.begin();
254 while (j != _views.end()) {
255 shared_ptr<TimelineAudioContentView> test = dynamic_pointer_cast<TimelineAudioContentView> (*j);
261 shared_ptr<Content> test_content = test->content();
263 if (test && test->track() && test->track().get() == t) {
264 if (content_period.overlaps (DCPTimePeriod(test_content->position(), test_content->end()))) {
265 /* we have an overlap on track `t' */
274 if (j == _views.end ()) {
275 /* no overlap on `t' */
281 _tracks = max (_tracks, t + 1);
284 _time_axis_view->set_y (tracks() * track_height() + 64);
285 _reels_view->set_y (8);
289 Timeline::tracks () const
295 Timeline::setup_pixels_per_second ()
297 shared_ptr<const Film> film = _film.lock ();
298 if (!film || film->length() == DCPTime ()) {
302 _pixels_per_second = static_cast<double>(width() - tracks_position().x * 2) / film->length().seconds ();
305 shared_ptr<TimelineView>
306 Timeline::event_to_view (wxMouseEvent& ev)
308 /* Search backwards through views so that we find the uppermost one first */
309 TimelineViewList::reverse_iterator i = _views.rbegin();
310 Position<int> const p (ev.GetX(), ev.GetY());
311 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
312 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
316 if (i == _views.rend ()) {
317 return shared_ptr<TimelineView> ();
324 Timeline::left_down (wxMouseEvent& ev)
326 shared_ptr<TimelineView> view = event_to_view (ev);
327 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
332 _down_view = content_view;
333 _down_view_position = content_view->content()->position ();
336 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
337 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
342 if (!ev.ShiftDown ()) {
343 cv->set_selected (view == *i);
347 if (content_view && ev.ShiftDown ()) {
348 content_view->set_selected (!content_view->selected ());
352 _down_point = ev.GetPosition ();
356 /* Pre-compute the points that we might snap to */
357 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
358 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
359 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
363 _start_snaps.push_back (cv->content()->position());
364 _end_snaps.push_back (cv->content()->position());
365 _start_snaps.push_back (cv->content()->end());
366 _end_snaps.push_back (cv->content()->end());
368 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
369 _start_snaps.push_back (i);
373 /* Tell everyone that things might change frequently during the drag */
374 _down_view->content()->set_change_signals_frequent (true);
379 Timeline::left_up (wxMouseEvent& ev)
384 _down_view->content()->set_change_signals_frequent (false);
385 _content_panel->set_selection (_down_view->content ());
388 set_position_from_event (ev);
390 /* Clear up up the stuff we don't do during drag */
392 setup_pixels_per_second ();
395 _start_snaps.clear ();
400 Timeline::mouse_moved (wxMouseEvent& ev)
406 set_position_from_event (ev);
410 Timeline::right_down (wxMouseEvent& ev)
412 shared_ptr<TimelineView> view = event_to_view (ev);
413 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
418 if (!cv->selected ()) {
420 cv->set_selected (true);
423 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
427 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
429 DCPTime const d = a - b;
430 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
431 nearest_distance = d;
436 Timeline::set_position_from_event (wxMouseEvent& ev)
438 if (!_pixels_per_second) {
442 double const pps = _pixels_per_second.get ();
444 wxPoint const p = ev.GetPosition();
447 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
448 before the drag is considered to have started.
450 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
461 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
465 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
466 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
469 optional<DCPTime> nearest_distance;
471 /* Find the nearest snap point */
473 BOOST_FOREACH (DCPTime i, _start_snaps) {
474 maybe_snap (i, new_position, nearest_distance);
477 BOOST_FOREACH (DCPTime i, _end_snaps) {
478 maybe_snap (i, new_end, nearest_distance);
481 if (nearest_distance) {
482 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
483 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
484 new_position += nearest_distance.get ();
489 if (new_position < DCPTime ()) {
490 new_position = DCPTime ();
493 _down_view->content()->set_position (new_position);
495 shared_ptr<Film> film = _film.lock ();
496 DCPOMATIC_ASSERT (film);
497 film->set_sequence (false);
501 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
503 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
506 shared_ptr<const Film>
507 Timeline::film () const
509 return _film.lock ();
515 setup_pixels_per_second ();
519 Timeline::clear_selection ()
521 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
522 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
524 cv->set_selected (false);
529 TimelineContentViewList
530 Timeline::selected_views () const
532 TimelineContentViewList sel;
534 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
535 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
536 if (cv && cv->selected()) {
545 Timeline::selected_content () const
548 TimelineContentViewList views = selected_views ();
550 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
551 sel.push_back ((*i)->content ());
558 Timeline::set_selection (ContentList selection)
560 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
561 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
563 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());