2 Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "film_editor.h"
23 #include "timeline_time_axis_view.h"
24 #include "timeline_reels_view.h"
25 #include "timeline_labels_view.h"
26 #include "timeline_video_content_view.h"
27 #include "timeline_audio_content_view.h"
28 #include "timeline_text_content_view.h"
29 #include "timeline_atmos_content_view.h"
30 #include "content_panel.h"
33 #include "lib/playlist.h"
34 #include "lib/image_content.h"
35 #include "lib/timer.h"
36 #include "lib/audio_content.h"
37 #include "lib/text_content.h"
38 #include "lib/video_content.h"
39 #include "lib/atmos_mxf_content.h"
40 #include <wx/graphics.h>
41 #include <boost/weak_ptr.hpp>
42 #include <boost/foreach.hpp>
52 using boost::shared_ptr;
53 using boost::weak_ptr;
54 using boost::dynamic_pointer_cast;
56 using boost::optional;
57 using namespace dcpomatic;
59 /* 3 hours in 640 pixels */
60 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
61 int const Timeline::_minimum_pixels_per_track = 16;
63 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
64 : wxPanel (parent, wxID_ANY)
65 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
66 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
69 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
70 , _reels_view (new TimelineReelsView (*this, 32))
71 , _labels_view (new TimelineLabelsView (*this))
74 , _down_view_position (0)
81 , _pixels_per_track (48)
82 , _first_resize (true)
85 _labels_canvas->SetDoubleBuffered (true);
86 _main_canvas->SetDoubleBuffered (true);
89 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
90 sizer->Add (_labels_canvas, 0, wxEXPAND);
91 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
92 sizer->Add (_main_canvas, 1, wxEXPAND);
95 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
96 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
97 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
98 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
99 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
100 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
101 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
102 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
103 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
104 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
105 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
106 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
107 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
108 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
110 film_change (CHANGE_TYPE_DONE, Film::CONTENT);
112 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
114 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
115 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
118 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
122 Timeline::set_pixels_per_second (double pps)
124 _pixels_per_second = max (_minimum_pixels_per_second, pps);
128 Timeline::paint_labels ()
130 wxPaintDC dc (_labels_canvas);
132 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
138 _labels_canvas->GetViewStart (&vsx, &vsy);
139 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
141 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
147 Timeline::paint_main ()
149 wxPaintDC dc (_main_canvas);
150 _main_canvas->DoPrepareDC (dc);
152 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
158 _main_canvas->GetViewStart (&vsx, &vsy);
159 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
161 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
163 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
165 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
167 /* Find areas of overlap with other content views, so that we can plot them */
168 list<dcpomatic::Rect<int> > overlaps;
169 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
170 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
171 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
172 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
176 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
178 overlaps.push_back (r.get ());
182 i->paint (gc, overlaps);
186 /* Translate back as _down_point and _zoom_point do not take scroll into account */
187 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
188 gc->SetPen (*wxBLACK_PEN);
189 gc->SetBrush (*wxTRANSPARENT_BRUSH);
191 min (_down_point.x, _zoom_point->x),
192 min (_down_point.y, _zoom_point->y),
193 abs (_down_point.x - _zoom_point->x),
194 abs (_down_point.y - _zoom_point->y)
202 Timeline::film_change (ChangeType type, Film::Property p)
204 if (type != CHANGE_TYPE_DONE) {
208 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
211 } else if (p == Film::CONTENT_ORDER) {
217 Timeline::recreate_views ()
219 shared_ptr<const Film> film = _film.lock ();
225 _views.push_back (_time_axis_view);
226 _views.push_back (_reels_view);
228 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
230 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
233 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
234 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
237 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
238 _views.push_back (shared_ptr<TimelineView> (new TimelineTextContentView (*this, i, j)));
241 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
242 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
252 Timeline::film_content_change (ChangeType type, int property, bool frequent)
254 if (type != CHANGE_TYPE_DONE) {
260 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
262 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
263 _reels_view->force_redraw ();
264 } else if (!frequent) {
272 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
274 int const base = tracks;
276 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
277 if (!dynamic_pointer_cast<T>(i)) {
281 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
285 shared_ptr<Content> content = cv->content();
286 DCPTimePeriod const content_period (content->position(), content->end(film));
289 TimelineViewList::iterator j = views.begin();
290 while (j != views.end()) {
291 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
297 shared_ptr<Content> test_content = test->content();
299 test->track() && test->track().get() == t &&
300 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
301 /* we have an overlap on track `t' */
309 if (j == views.end ()) {
310 /* no overlap on `t' */
316 tracks = max (tracks, t + 1);
319 return tracks - base;
322 /** Compare the mapped output channels of two TimelineViews, so we can into
323 * order of first mapped DCP channel.
325 struct AudioMappingComparator {
326 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
328 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
330 list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
331 la = *min_element(boost::begin(oc), boost::end(oc));
334 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
336 list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
337 lb = *min_element(boost::begin(oc), boost::end(oc));
344 Timeline::assign_tracks ()
347 Video (mono or left-eye)
358 shared_ptr<const Film> film = _film.lock ();
359 DCPOMATIC_ASSERT (film);
363 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
364 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
372 bool have_3d = false;
373 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
374 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
379 /* Video on tracks 0 and maybe 1 (left and right eye) */
380 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
382 _tracks = max (_tracks, 2);
389 _tracks = max (_tracks, 1);
393 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
397 bool have_atmos = false;
398 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
399 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
403 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
404 cv->set_track (_tracks - 1);
413 /* Audio. We're sorting the views so that we get the audio views in order of increasing
417 TimelineViewList views = _views;
418 sort(views.begin(), views.end(), AudioMappingComparator());
419 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
421 _labels_view->set_3d (have_3d);
422 _labels_view->set_audio_tracks (audio_tracks);
423 _labels_view->set_text_tracks (text_tracks);
424 _labels_view->set_atmos (have_atmos);
426 _time_axis_view->set_y (tracks());
427 _reels_view->set_y (8);
431 Timeline::tracks () const
437 Timeline::setup_scrollbars ()
439 shared_ptr<const Film> film = _film.lock ();
440 if (!film || !_pixels_per_second) {
444 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
446 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
447 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
448 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
449 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
452 shared_ptr<TimelineView>
453 Timeline::event_to_view (wxMouseEvent& ev)
455 /* Search backwards through views so that we find the uppermost one first */
456 TimelineViewList::reverse_iterator i = _views.rbegin();
457 Position<int> const p (ev.GetX(), ev.GetY());
458 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
459 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
463 if (i == _views.rend ()) {
464 return shared_ptr<TimelineView> ();
471 Timeline::left_down (wxMouseEvent& ev)
474 _down_point = ev.GetPosition ();
478 left_down_select (ev);
490 Timeline::left_down_select (wxMouseEvent& ev)
492 shared_ptr<TimelineView> view = event_to_view (ev);
493 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
498 _down_view = content_view;
499 _down_view_position = content_view->content()->position ();
502 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
503 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
508 if (!ev.ShiftDown ()) {
509 cv->set_selected (view == *i);
513 if (content_view && ev.ShiftDown ()) {
514 content_view->set_selected (!content_view->selected ());
520 /* Pre-compute the points that we might snap to */
521 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
522 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
523 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
527 shared_ptr<Film> film = _film.lock ();
528 DCPOMATIC_ASSERT (film);
530 _start_snaps.push_back (cv->content()->position());
531 _end_snaps.push_back (cv->content()->position());
532 _start_snaps.push_back (cv->content()->end(film));
533 _end_snaps.push_back (cv->content()->end(film));
535 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points(film)) {
536 _start_snaps.push_back (i);
540 /* Tell everyone that things might change frequently during the drag */
541 _down_view->content()->set_change_signals_frequent (true);
546 Timeline::left_up (wxMouseEvent& ev)
565 Timeline::left_up_select (wxMouseEvent& ev)
568 _down_view->content()->set_change_signals_frequent (false);
571 _content_panel->set_selection (selected_content ());
572 /* Since we may have just set change signals back to `not-frequent', we have to
573 make sure this position change is signalled, even if the position value has
574 not changed since the last time it was set (with frequent=true). This is
577 set_position_from_event (ev, true);
579 /* Clear up up the stuff we don't do during drag */
584 _start_snaps.clear ();
589 Timeline::left_up_zoom (wxMouseEvent& ev)
591 _zoom_point = ev.GetPosition ();
594 _main_canvas->GetViewStart (&vsx, &vsy);
596 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
597 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
599 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
600 /* Very small zoom rectangle: we assume it wasn't intentional */
604 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
605 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
606 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
608 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
609 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
610 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
613 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
614 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
615 _labels_canvas->Scroll (0, y);
617 _zoom_point = optional<wxPoint> ();
622 Timeline::set_pixels_per_track (int h)
624 _pixels_per_track = max(_minimum_pixels_per_track, h);
628 Timeline::mouse_moved (wxMouseEvent& ev)
632 mouse_moved_select (ev);
635 mouse_moved_zoom (ev);
645 Timeline::mouse_moved_select (wxMouseEvent& ev)
651 set_position_from_event (ev);
655 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
661 _zoom_point = ev.GetPosition ();
666 Timeline::right_down (wxMouseEvent& ev)
670 right_down_select (ev);
674 set_pixels_per_second (*_pixels_per_second / 2);
675 set_pixels_per_track (_pixels_per_track / 2);
687 Timeline::right_down_select (wxMouseEvent& ev)
689 shared_ptr<TimelineView> view = event_to_view (ev);
690 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
695 if (!cv->selected ()) {
697 cv->set_selected (true);
700 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
704 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
706 DCPTime const d = a - b;
707 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
708 nearest_distance = d;
713 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
715 if (!_pixels_per_second) {
719 double const pps = _pixels_per_second.get ();
721 wxPoint const p = ev.GetPosition();
724 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
725 before the drag is considered to have started.
727 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
738 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
740 shared_ptr<Film> film = _film.lock ();
741 DCPOMATIC_ASSERT (film);
744 DCPTime const new_end = new_position + _down_view->content()->length_after_trim(film);
745 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
748 optional<DCPTime> nearest_distance;
750 /* Find the nearest snap point */
752 BOOST_FOREACH (DCPTime i, _start_snaps) {
753 maybe_snap (i, new_position, nearest_distance);
756 BOOST_FOREACH (DCPTime i, _end_snaps) {
757 maybe_snap (i, new_end, nearest_distance);
760 if (nearest_distance) {
761 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
762 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
763 new_position += nearest_distance.get ();
768 if (new_position < DCPTime ()) {
769 new_position = DCPTime ();
772 _down_view->content()->set_position (film, new_position, force_emit);
774 film->set_sequence (false);
778 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
780 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
783 shared_ptr<const Film>
784 Timeline::film () const
786 return _film.lock ();
792 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
794 _first_resize = false;
800 Timeline::clear_selection ()
802 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
803 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
805 cv->set_selected (false);
810 TimelineContentViewList
811 Timeline::selected_views () const
813 TimelineContentViewList sel;
815 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
816 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
817 if (cv && cv->selected()) {
826 Timeline::selected_content () const
829 TimelineContentViewList views = selected_views ();
831 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
832 sel.push_back ((*i)->content ());
839 Timeline::set_selection (ContentList selection)
841 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
842 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
844 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
850 Timeline::tracks_y_offset () const
852 return _reels_view->bbox().height + 4;
856 Timeline::width () const
858 return _main_canvas->GetVirtualSize().GetWidth();
862 Timeline::scrolled (wxScrollWinEvent& ev)
864 if (ev.GetOrientation() == wxVERTICAL) {
866 _main_canvas->GetViewStart (&x, &y);
867 _labels_canvas->Scroll (0, y);
873 Timeline::tool_clicked (Tool t)
890 Timeline::zoom_all ()
892 shared_ptr<Film> film = _film.lock ();
893 DCPOMATIC_ASSERT (film);
894 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
895 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
897 _main_canvas->Scroll (0, 0);
898 _labels_canvas->Scroll (0, 0);