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"
32 #include "film_viewer.h"
34 #include "lib/playlist.h"
35 #include "lib/image_content.h"
36 #include "lib/timer.h"
37 #include "lib/audio_content.h"
38 #include "lib/text_content.h"
39 #include "lib/video_content.h"
40 #include "lib/atmos_mxf_content.h"
41 #include <wx/graphics.h>
42 #include <boost/weak_ptr.hpp>
43 #include <boost/foreach.hpp>
53 using boost::shared_ptr;
54 using boost::weak_ptr;
55 using boost::dynamic_pointer_cast;
57 using boost::optional;
58 using namespace dcpomatic;
59 #if BOOST_VERSION >= 106100
60 using namespace boost::placeholders;
63 /* 3 hours in 640 pixels */
64 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
65 int const Timeline::_minimum_pixels_per_track = 16;
67 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
68 : wxPanel (parent, wxID_ANY)
69 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
70 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
74 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
75 , _reels_view (new TimelineReelsView (*this, 32))
76 , _labels_view (new TimelineLabelsView (*this))
79 , _down_view_position (0)
86 , _pixels_per_track (48)
87 , _first_resize (true)
91 _labels_canvas->SetDoubleBuffered (true);
92 _main_canvas->SetDoubleBuffered (true);
95 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
96 sizer->Add (_labels_canvas, 0, wxEXPAND);
97 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
98 sizer->Add (_main_canvas, 1, wxEXPAND);
101 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
102 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
103 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
104 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
105 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
106 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
107 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
108 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
109 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
110 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
111 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
112 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
113 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
114 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
116 film_change (CHANGE_TYPE_DONE, Film::CONTENT);
118 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
120 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
121 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
123 Bind (wxEVT_TIMER, boost::bind(&Timeline::update_playhead, this));
124 _timer.Start (200, wxTIMER_CONTINUOUS);
127 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
131 Timeline::update_playhead ()
137 Timeline::set_pixels_per_second (double pps)
139 _pixels_per_second = max (_minimum_pixels_per_second, pps);
143 Timeline::paint_labels ()
145 wxPaintDC dc (_labels_canvas);
147 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
153 _labels_canvas->GetViewStart (&vsx, &vsy);
154 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
156 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
162 Timeline::paint_main ()
164 wxPaintDC dc (_main_canvas);
165 _main_canvas->DoPrepareDC (dc);
167 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
173 _main_canvas->GetViewStart (&vsx, &vsy);
174 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
176 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
178 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
180 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
182 /* Find areas of overlap with other content views, so that we can plot them */
183 list<dcpomatic::Rect<int> > overlaps;
184 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
185 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
186 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
187 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
191 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
193 overlaps.push_back (r.get ());
197 i->paint (gc, overlaps);
201 /* Translate back as _down_point and _zoom_point do not take scroll into account */
202 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
203 gc->SetPen (*wxBLACK_PEN);
204 gc->SetBrush (*wxTRANSPARENT_BRUSH);
206 min (_down_point.x, _zoom_point->x),
207 min (_down_point.y, _zoom_point->y),
208 abs (_down_point.x - _zoom_point->x),
209 abs (_down_point.y - _zoom_point->y)
215 shared_ptr<FilmViewer> vp = _viewer.lock ();
216 DCPOMATIC_ASSERT (vp);
218 gc->SetPen (*wxRED_PEN);
219 wxGraphicsPath path = gc->CreatePath ();
220 double const ph = vp->position().seconds() * pixels_per_second().get_value_or(0);
221 path.MoveToPoint (ph, 0);
222 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
223 gc->StrokePath (path);
229 Timeline::film_change (ChangeType type, Film::Property p)
231 if (type != CHANGE_TYPE_DONE) {
235 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
238 } else if (p == Film::CONTENT_ORDER) {
244 Timeline::recreate_views ()
246 shared_ptr<const Film> film = _film.lock ();
252 _views.push_back (_time_axis_view);
253 _views.push_back (_reels_view);
255 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
257 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
260 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
261 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
264 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
265 _views.push_back (shared_ptr<TimelineView> (new TimelineTextContentView (*this, i, j)));
269 _views.push_back (shared_ptr<TimelineView>(new TimelineAtmosContentView(*this, i)));
279 Timeline::film_content_change (ChangeType type, int property, bool frequent)
281 if (type != CHANGE_TYPE_DONE) {
287 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
289 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
290 _reels_view->force_redraw ();
291 } else if (!frequent) {
299 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
301 int const base = tracks;
303 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
304 if (!dynamic_pointer_cast<T>(i)) {
308 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
312 shared_ptr<Content> content = cv->content();
313 DCPTimePeriod const content_period (content->position(), content->end(film));
316 TimelineViewList::iterator j = views.begin();
317 while (j != views.end()) {
318 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
324 shared_ptr<Content> test_content = test->content();
326 test->track() && test->track().get() == t &&
327 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
328 /* we have an overlap on track `t' */
336 if (j == views.end ()) {
337 /* no overlap on `t' */
343 tracks = max (tracks, t + 1);
346 return tracks - base;
349 /** Compare the mapped output channels of two TimelineViews, so we can into
350 * order of first mapped DCP channel.
352 struct AudioMappingComparator {
353 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
355 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
357 list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
358 la = *min_element(boost::begin(oc), boost::end(oc));
361 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
363 list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
364 lb = *min_element(boost::begin(oc), boost::end(oc));
371 Timeline::assign_tracks ()
374 Video (mono or left-eye)
385 shared_ptr<const Film> film = _film.lock ();
386 DCPOMATIC_ASSERT (film);
390 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
391 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
399 bool have_3d = false;
400 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
401 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
406 /* Video on tracks 0 and maybe 1 (left and right eye) */
407 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
409 _tracks = max (_tracks, 2);
416 _tracks = max (_tracks, 1);
420 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
424 bool have_atmos = false;
425 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
426 shared_ptr<TimelineAtmosContentView> cv = dynamic_pointer_cast<TimelineAtmosContentView>(i);
428 cv->set_track (_tracks);
437 /* Audio. We're sorting the views so that we get the audio views in order of increasing
441 TimelineViewList views = _views;
442 sort(views.begin(), views.end(), AudioMappingComparator());
443 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
445 _labels_view->set_3d (have_3d);
446 _labels_view->set_audio_tracks (audio_tracks);
447 _labels_view->set_text_tracks (text_tracks);
448 _labels_view->set_atmos (have_atmos);
450 _time_axis_view->set_y (tracks());
451 _reels_view->set_y (8);
455 Timeline::tracks () const
461 Timeline::setup_scrollbars ()
463 shared_ptr<const Film> film = _film.lock ();
464 if (!film || !_pixels_per_second) {
468 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
470 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
471 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
472 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
473 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
476 shared_ptr<TimelineView>
477 Timeline::event_to_view (wxMouseEvent& ev)
479 /* Search backwards through views so that we find the uppermost one first */
480 TimelineViewList::reverse_iterator i = _views.rbegin();
483 _main_canvas->GetViewStart (&vsx, &vsy);
484 Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
486 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
487 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
491 if (i == _views.rend ()) {
492 return shared_ptr<TimelineView> ();
499 Timeline::left_down (wxMouseEvent& ev)
502 _down_point = ev.GetPosition ();
506 left_down_select (ev);
518 Timeline::left_down_select (wxMouseEvent& ev)
520 shared_ptr<TimelineView> view = event_to_view (ev);
521 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
526 _down_view = content_view;
527 _down_view_position = content_view->content()->position ();
530 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
531 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
536 if (!ev.ShiftDown ()) {
537 cv->set_selected (view == *i);
541 if (content_view && ev.ShiftDown ()) {
542 content_view->set_selected (!content_view->selected ());
548 /* Pre-compute the points that we might snap to */
549 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
550 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
551 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
555 shared_ptr<Film> film = _film.lock ();
556 DCPOMATIC_ASSERT (film);
558 _start_snaps.push_back (cv->content()->position());
559 _end_snaps.push_back (cv->content()->position());
560 _start_snaps.push_back (cv->content()->end(film));
561 _end_snaps.push_back (cv->content()->end(film));
563 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points(film)) {
564 _start_snaps.push_back (i);
568 /* Tell everyone that things might change frequently during the drag */
569 _down_view->content()->set_change_signals_frequent (true);
574 Timeline::left_up (wxMouseEvent& ev)
593 Timeline::left_up_select (wxMouseEvent& ev)
596 _down_view->content()->set_change_signals_frequent (false);
599 _content_panel->set_selection (selected_content ());
600 /* Since we may have just set change signals back to `not-frequent', we have to
601 make sure this position change is signalled, even if the position value has
602 not changed since the last time it was set (with frequent=true). This is
605 set_position_from_event (ev, true);
607 /* Clear up up the stuff we don't do during drag */
612 _start_snaps.clear ();
617 Timeline::left_up_zoom (wxMouseEvent& ev)
619 _zoom_point = ev.GetPosition ();
622 _main_canvas->GetViewStart (&vsx, &vsy);
624 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
625 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
627 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
628 /* Very small zoom rectangle: we assume it wasn't intentional */
632 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
633 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
634 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
636 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
637 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
638 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
641 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
642 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
643 _labels_canvas->Scroll (0, y);
645 _zoom_point = optional<wxPoint> ();
650 Timeline::set_pixels_per_track (int h)
652 _pixels_per_track = max(_minimum_pixels_per_track, h);
656 Timeline::mouse_moved (wxMouseEvent& ev)
660 mouse_moved_select (ev);
663 mouse_moved_zoom (ev);
673 Timeline::mouse_moved_select (wxMouseEvent& ev)
679 set_position_from_event (ev);
683 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
689 _zoom_point = ev.GetPosition ();
694 Timeline::right_down (wxMouseEvent& ev)
698 right_down_select (ev);
702 set_pixels_per_second (*_pixels_per_second / 2);
703 set_pixels_per_track (_pixels_per_track / 2);
715 Timeline::right_down_select (wxMouseEvent& ev)
717 shared_ptr<TimelineView> view = event_to_view (ev);
718 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
723 if (!cv->selected ()) {
725 cv->set_selected (true);
728 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
732 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
734 DCPTime const d = a - b;
735 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
736 nearest_distance = d;
741 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
743 if (!_pixels_per_second) {
747 double const pps = _pixels_per_second.get ();
749 wxPoint const p = ev.GetPosition();
752 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
753 before the drag is considered to have started.
755 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
766 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
768 shared_ptr<Film> film = _film.lock ();
769 DCPOMATIC_ASSERT (film);
772 DCPTime const new_end = new_position + _down_view->content()->length_after_trim(film);
773 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
776 optional<DCPTime> nearest_distance;
778 /* Find the nearest snap point */
780 BOOST_FOREACH (DCPTime i, _start_snaps) {
781 maybe_snap (i, new_position, nearest_distance);
784 BOOST_FOREACH (DCPTime i, _end_snaps) {
785 maybe_snap (i, new_end, nearest_distance);
788 if (nearest_distance) {
789 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
790 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
791 new_position += nearest_distance.get ();
796 if (new_position < DCPTime ()) {
797 new_position = DCPTime ();
800 _down_view->content()->set_position (film, new_position, force_emit);
802 film->set_sequence (false);
806 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
808 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
811 shared_ptr<const Film>
812 Timeline::film () const
814 return _film.lock ();
820 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
822 _first_resize = false;
828 Timeline::clear_selection ()
830 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
831 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
833 cv->set_selected (false);
838 TimelineContentViewList
839 Timeline::selected_views () const
841 TimelineContentViewList sel;
843 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
844 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
845 if (cv && cv->selected()) {
854 Timeline::selected_content () const
857 TimelineContentViewList views = selected_views ();
859 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
860 sel.push_back ((*i)->content ());
867 Timeline::set_selection (ContentList selection)
869 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
870 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
872 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
878 Timeline::tracks_y_offset () const
880 return _reels_view->bbox().height + 4;
884 Timeline::width () const
886 return _main_canvas->GetVirtualSize().GetWidth();
890 Timeline::scrolled (wxScrollWinEvent& ev)
892 if (ev.GetOrientation() == wxVERTICAL) {
894 _main_canvas->GetViewStart (&x, &y);
895 _labels_canvas->Scroll (0, y);
901 Timeline::tool_clicked (Tool t)
918 Timeline::zoom_all ()
920 shared_ptr<Film> film = _film.lock ();
921 DCPOMATIC_ASSERT (film);
922 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
923 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
925 _main_canvas->Scroll (0, 0);
926 _labels_canvas->Scroll (0, 0);